www.mwasoftware.co.uk

 

If you are using a local database and the embedded Firebird Server then you also need to set the environment variables correctly to avoid conflicts with other users, or just so that Firebird works correctly. The can be set outside of the program – but this is not always that user friendly. They can be set from within a program except that on a Linux/Unix hosts this requires some extra code as for some unknown reason, FPC does not have a native interface that allows you to change environment variables.

The following code snippet should work for most people. It also makes it easier to use standard Firebird packages.

{$IFDEF Unix}
function setenv(name:Pchar; value:Pchar;replace:integer):integer;cdecl;external clib name 'setenv';
function unsetenv(name:Pchar):integer;cdecl;external clib name 'unsetenv';

function SetEnvironmentVariable(name:PChar;value:PChar):boolean;
begin
result:=false; //assume failure
if value = '' then
begin
// Assume user wants to remove variable.
if unsetenv(name)=0 then result:=true;
end
else
begin
// Non empty so set the variable
if setenv(name, value, 1)=0 then
result:=true;
end;
end;
{$ENDIF}

procedure SetupFirebirdEnv;
var TmpDir: string;
begin
TmpDir := GetTempDir + DirectorySeparator + 'firebird_' + GetEnvironmentVariable('USER');
if GetEnvironmentVariable('FIREBIRD_TMP') = '' then
begin
if not DirectoryExists(tmpDir) then
mkdir(tmpDir);
SetEnvironmentVariable('FIREBIRD_TMP',PChar(TmpDir));
end;
  if GetEnvironmentVariable('FIREBIRD_LOCK') = '' then
begin
if not DirectoryExists(tmpDir) then
mkdir(tmpDir);
SetEnvironmentVariable('FIREBIRD_LOCK',PChar(TmpDir));
end;
  {$IFDEF WINDOWS}
  if FileExists(Application.Location + 'firebird.msg') then
    SetEnvironmentVariable('FIREBIRD',PChar(ExtractFileDir(Application.ExeName)));
  {$ENDIF}
end;            

Under Linux

The procedure SetupFirebirdEnv does most of the work. An underlying assumption is that the Firebird embedded server has been installed from a standard package, such as libfbembed2.5 (Debian/Ubuntu). The Firebird files are thus all in standard locations and there is no need to set the FIREBIRD environment variable. However, using the default TMP or LOCK directories could give rise to conflicts or access rights issues.

The procedure will create, if necessary a temporary directory for the FIREBIRD_TMP and FIREBIRD_LOCK environment variables to point to and then set those Environment Variable to the path to that directory. It will not, however, overwrite existing values for either variable.

The problem is that “SetEnvironmentVariable” only exists under Windows. We thus have to include our own version which makes direct use of “libc”. You will have also to include “Initc” your “uses” list in order to use libc.

Under Windows.

The procedure SetupFirebirdEnv does most of the work. An underlying assumption is that the Firebird embedded server has been installed in the same directory as your program.

The procedure SetupFirebirdEnv does the same as for Linux. Additionally, it has to point the FIREBIRD environment variable at the directory holding the Firebird messages file. However, there is no need here for your own SetEnvironmentVariable as this is available under Windows. You do have to include the "Windows" unit in your "uses" list.