ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/iblocaldb/nongui/IBXCustomIBLocalDBSupport.pas
Revision: 345
Committed: Mon Aug 23 14:22:29 2021 UTC (2 years, 8 months ago) by tony
Content type: text/x-pascal
File size: 21952 byte(s)
Log Message:
Merged into public release

File Contents

# User Rev Content
1 tony 209 (*
2     * IBX For Lazarus (Firebird Express)
3     *
4     * The contents of this file are subject to the Initial Developer's
5     * Public License Version 1.0 (the "License"); you may not use this
6     * file except in compliance with the License. You may obtain a copy
7     * of the License here:
8     *
9     * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10     *
11     * Software distributed under the License is distributed on an "AS
12     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
13     * implied. See the License for the specific language governing rights
14     * and limitations under the License.
15     *
16     * The Initial Developer of the Original Code is Tony Whyman.
17     *
18     * The Original Code is (C) 2014 Tony Whyman, MWA Software
19     * (http://www.mwasoftware.co.uk).
20     *
21     * All Rights Reserved.
22     *
23     * Contributor(s): ______________________________________.
24     *
25     *)
26     unit IBXCustomIBLocalDBSupport;
27    
28     {$mode objfpc}{$H+}
29    
30     interface
31    
32     uses
33     Classes, IBDatabase, SysUtils, IBXUpgradeConfFile, ibxscript,
34     IB, IBXServices;
35    
36     type
37    
38     { TCustomIBLocalDBSupport Properties
39    
40     * Database: reference to the TIBDatabase component for the local database
41     * DatabaseName: filename (no path) to use for the Firebird Database file.
42     * EmptyDBArchive: filename (optional path) holding the database initialisation archive.
43     May either be absolute path or relative to shared data directory.
44     * Enabled: when false component does nothing
45     * FirebirdDirectory: Full path to directory holding firebird.conf. May either be
46     absolute path or relative to the shared data directory. If empty, defaults to shared data directory.
47     * Name: Component Name
48     * Options:
49     iblAutoUpgrade: Automatically apply upgrade when database schema
50     version is lower than required.
51     IblAllowDowngrade: Automatically apply downgrade when available to
52     schema version compatible with the required version.
53     iblQuiet: true then no database overwrite warnings
54     * RequiredVersionNo: The schema version number required by the application.
55     TIBLocalDBSupport will normally try to upgrade/downgrade the schema to satisfy
56     this requirement.
57     * UpgradeConfFile: Path to upgrade configuration file. May either be absolute
58     path or relative to the shared data directory.
59     * VendorName: Used to construct path to Database Directory.
60    
61     Note that at design time paths may use '/' or '\' as directory separator. At
62     run time, they must be specified using the appropriate Directory Separator
63     for the current platform.
64    
65     Events:
66    
67     * OnGetDatabaseName: The database name is normally computed automatically.
68     However, this event allows an application to inspect and override the result.
69     * OnNewDatabaseOpen: called after the successful initialisation of an empty local database.
70     * OnGetDBVersionNo: called to get the current database schema version number.
71     If this event is not handled then schema upgrade/downgrade is never performed.
72     * OnGetSharedDataDir: The shared data directory is normally computed automatically.
73     However, this event allows an application to inspect and override the result.
74     }
75    
76     TOnGetDatabaseName = procedure(Sender: TObject; var DBName: string) of object;
77     TOnGetDBVersionNo = procedure (Sender: TObject; var VersionNo: integer) of object;
78     TOnGetSharedDataDir = procedure(Sender: TObject; var SharedDataDir: string) of object;
79    
80     TIBLocalOption = (iblAutoUpgrade, iblAllowDowngrade, iblQuiet);
81     TIBLocalOptions = set of TIBLocalOption;
82    
83     EIBLocalException = class(Exception);
84    
85    
86     { TCustomIBLocalDBSupport }
87    
88     TCustomIBLocalDBSupport = class(TComponent)
89     private
90 tony 315 FMinimumVersionNo: integer;
91 tony 345 FSectionHeaderTemplate: string;
92 tony 209 { Private declarations }
93     FServicesConnection: TIBXServicesConnection;
94     FBackupService: TIBXServerSideBackupService;
95     FRestoreService: TIBXServerSideRestoreService;
96     FActiveDatabasePathName: string;
97     FCurrentDBVersionNo: integer;
98     FEmptyDBArchive: string;
99     FEnabled: boolean;
100     FFirebirdDirectory: string;
101     FIBBase: TIBBase;
102     FDatabaseName: string;
103     FOnGetDatabaseName: TOnGetDatabaseName;
104     FOnGetDBVersionNo: TOnGetDBVersionNo;
105     FOnGetSharedDataDir: TOnGetSharedDataDir;
106     FOnNewDatabaseOpen: TNotifyEvent;
107     FOptions: TIBLocalOptions;
108     FRequiredVersionNo: integer;
109     FUpgradeConfFile: string;
110     FNewDBCreated: boolean;
111     FVendorName: string;
112     FInUpgrade: boolean;
113     FDownGradeArchive: string;
114     FSharedDataDir: string;
115     FUpgradeConf: TUpgradeConfFile;
116 tony 272 FInOnCreateDB: boolean;
117 tony 315 FUpgradeFailed: boolean;
118 tony 209 procedure CheckEnabled;
119     function GetDatabase: TIBDatabase;
120     function GetSharedDataDir: string;
121     procedure SetDatabase(AValue: TIBDatabase);
122     function GetDBNameAndPath: string;
123     function MapSharedDataDir(aDataDir: string): string;
124     procedure OnBeforeDatabaseConnect(Sender: TObject; DBParams: TStrings;
125     var DBName: string; var CreateIfNotExists: boolean);
126     procedure OnDatabaseConnected(Sender: TObject);
127     procedure OnAfterDatabaseDisconnect(Sender: TObject);
128     procedure OnCreateDatabase(Sender: TObject);
129     procedure PrepareDBParams(DBParams: TStrings);
130     procedure SetDatabaseName(AValue: string);
131     procedure SetFirebirdDirectory(AValue: string);
132     procedure SetupFirebirdEnv;
133     procedure UpgradeCheck;
134     protected
135     { Protected declarations }
136 tony 315 procedure Add2Log(Sender: TObject; Msg: string); virtual;
137 tony 209 function AllowInitialisation: boolean; virtual;
138     function AllowRestore: boolean; virtual;
139     procedure CreateDir(DirName: string);
140     function InternalCreateNewDatabase(DBArchive: string): boolean; virtual; abstract;
141     procedure Downgrade(DBArchive: string); virtual;
142     procedure DowngradeDone;
143     procedure Loaded; override;
144     function RestoreDatabaseFromArchive(aFilename: string): boolean; virtual; abstract;
145     function RunUpgradeDatabase(TargetVersionNo: integer): boolean; virtual; abstract;
146     function SaveDatabaseToArchive(aFilename: string): boolean; virtual; abstract;
147     function UpdateVersionNo: boolean;
148     property DownGradeArchive: string read FDownGradeArchive;
149     property UpgradeConf: TUpgradeConfFile read FUpgradeConf;
150     property RestoreService: TIBXServerSideRestoreService read FRestoreService;
151     property BackupService: TIBXServerSideBackupService read FBackupService;
152     public
153     { Public declarations }
154     constructor Create(aOwner: TComponent); override;
155     destructor Destroy; override;
156    
157     function DowngradePending: boolean;
158    
159     {NewDatabase: called to reinitialise the local database using the initialisation
160     archive. Overwrites existing data so use carefully.}
161     procedure NewDatabase;
162    
163     {Perform a downgrade to target version if backup archive available.
164     Note: normally called from UpgradeCheck if iblAllowDowngrade in Options}
165     procedure PerformDowngrade(TargetVersionNo: integer);
166    
167     {Perform schema upgrade to target version if upgrade scripts available.
168     Note: normally called from UpgradeCheck if iblAllowUpgrade in Options}
169     procedure PerformUpgrade(TargetVersionNo: integer);
170    
171     {RestoreDatabase: overwrites the existing local database with the specified
172     gbak format archive. User is prompted to locate archive if filename empty.}
173     procedure RestoreDatabase (filename: string = '');
174    
175     {SaveDatabase: Saves the current database into a gbak format archive. User
176     is prompted for archive filename if filename empty.}
177     procedure SaveDatabase(filename: string = '');
178    
179     property ActiveDatabasePathName: string read FActiveDatabasePathName;
180     property CurrentDBVersionNo: integer read FCurrentDBVersionNo;
181 tony 272 property InOnCreateDB: boolean read FInOnCreateDB;
182 tony 209 property SharedDataDir: string read GetSharedDataDir;
183     property ServicesConnection: TIBXServicesConnection read FServicesConnection;
184    
185     { Likely to be Published declarations }
186     property Database: TIBDatabase read GetDatabase write SetDatabase;
187     property DatabaseName: string read FDatabaseName write SetDatabaseName;
188     property Enabled: boolean read FEnabled write FEnabled default true;
189     property EmptyDBArchive: string read FEmptyDBArchive write FEmptyDBArchive;
190     property FirebirdDirectory: string read FFirebirdDirectory write SetFirebirdDirectory;
191     property Options: TIBLocalOptions read FOptions write FOptions;
192     property RequiredVersionNo: integer read FRequiredVersionNo write FRequiredVersionNo;
193 tony 315 property MinimumVersionNo: integer read FMinimumVersionNo write FMinimumVersionNo;
194 tony 209 property UpgradeConfFile: string read FUpgradeConfFile write FUpgradeConfFile;
195 tony 345 property SectionHeaderTemplate: string read FSectionHeaderTemplate write FSectionHeaderTemplate;
196 tony 209 property VendorName: string read FVendorName write FVendorName;
197     property OnGetDatabaseName: TOnGetDatabaseName read FOnGetDatabaseName write FOnGetDatabaseName;
198     property OnGetDBVersionNo: TOnGetDBVersionNo read FOnGetDBVersionNo write FOnGetDBVersionNo;
199     property OnNewDatabaseOpen: TNotifyEvent read FOnNewDatabaseOpen write FOnNewDatabaseOpen;
200     property OnGetSharedDataDir: TOnGetSharedDataDir read FOnGetSharedDataDir write FOnGetSharedDataDir;
201     end;
202    
203     EIBLocalFatalError = class(Exception);
204    
205     implementation
206    
207     {$IFDEF Unix} uses initc, regexpr {$ENDIF}
208 tony 315 {$IFDEF WINDOWS} uses Windows ,Windirs {$ENDIF}, IBUtils, IBMessages;
209 tony 209
210 tony 345 const
211     sSectionheader = 'Version.%.3d';
212    
213 tony 209 resourcestring
214     sNoDowngrade = 'Database Schema is %d. Unable to downgrade to version %d';
215     sLocalDBDisabled = 'Local Database Access Disabled';
216     sEmptyDBArchiveMissing = 'Unable to create database - no empty DB archive specified';
217     sEmptyDBArchiveNotFound = 'Unable to create database - empty DB archive file (%s) not found';
218     sNoEmbeddedServer = 'Firebird Embedded Server is required but is not installed';
219     sCreateFailed = 'Unable to Create Personal Database';
220 tony 315 sDowngrade = 'Downgrading to version %d';
221     sSkipUpgrade = 'Previous attempt at upgrade to %d failed. Skipping upgrade';
222 tony 209
223     { TCustomIBLocalDBSupport }
224    
225    
226     procedure TCustomIBLocalDBSupport.CheckEnabled;
227     begin
228     if not Enabled then
229     raise EIBLocalException.Create(sLocalDBDisabled);
230     end;
231    
232     function TCustomIBLocalDBSupport.GetDatabase: TIBDatabase;
233     begin
234     Result := FIBBase.Database;
235     end;
236    
237     function TCustomIBLocalDBSupport.GetSharedDataDir: string;
238     begin
239     if FSharedDataDir = '' then
240     FSharedDataDir := MapSharedDataDir(ExtractFilePath(ParamStr(0)));
241     Result := FSharedDataDir;
242     end;
243    
244     procedure TCustomIBLocalDBSupport.SetDatabase(AValue: TIBDatabase);
245     begin
246     FIBBase.Database := AValue;
247     end;
248    
249     function TCustomIBLocalDBSupport.MapSharedDataDir(aDataDir: string): string;
250     {$IFDEF UNIX}
251     var RegexObj: TRegExpr;
252     {$ENDIF}
253     begin
254     Result := aDataDir;
255     {$IFDEF UNIX}
256    
257     {Under Unix transform application exe paths that are in installed locations
258     e.g. /usr/local/bin to corresponding shared data locations e.g. /usr/local/shared}
259     RegexObj := TRegExpr.Create;
260     try
261     RegexObj.Expression := '^/usr(/local|)/(s|)bin/.*$';
262     if RegexObj.Exec(aDataDir) then
263     Result := '/usr' + RegexObj.Match[1] + '/share/' + ApplicationName + '/';
264     finally
265     RegexObj.Free;
266     end;
267     {$ENDIF}
268     if assigned (FOnGetSharedDataDir) then
269     OnGetSharedDataDir(self,Result);
270     {Ensure a trailing separator}
271     if (Length(Result) > 0) and (Result[Length(Result)] <> DirectorySeparator) then
272     Result := Result + DirectorySeparator;
273     end;
274    
275     {$IFDEF Unix}
276     function setenv(name:Pchar; value:Pchar; replace:integer):integer;cdecl;external clib name 'setenv';
277     function unsetenv(name:Pchar):integer;cdecl;external clib name 'unsetenv';
278     function SetEnvironmentVariable(name:PChar; value:PChar):boolean;
279     // Set environment variable; if empty string given, remove it.
280     begin
281     result:=false; //assume failure
282     if value = '' then
283     begin
284     // Assume user wants to remove variable.
285     if unsetenv(name)=0 then result:=true;
286     end
287     else
288     begin
289     // Non empty so set the variable
290     if setenv(name, value, 1)=0 then result:=true;
291     end;
292     end;
293     {$ENDIF}
294    
295     procedure TCustomIBLocalDBSupport.OnBeforeDatabaseConnect(Sender: TObject;
296     DBParams: TStrings; var DBName: string; var CreateIfNotExists: boolean);
297     begin
298 tony 272 if FInOnCreateDB or not Enabled or (csDesigning in ComponentState) then Exit;
299 tony 209
300 tony 263 if not assigned(Database) or not Database.FirebirdAPI.IsEmbeddedServer then
301 tony 209 raise EIBLocalFatalError.Create(sNoEmbeddedServer);
302    
303     DBName := GetDBNameAndPath;
304     CreateDir(ExtractFileDir(DBName));
305     FActiveDatabasePathName := DBName;
306     SetupFirebirdEnv;
307     CreateIfNotExists := true;
308     PrepareDBParams(DBParams);
309 tony 315 ServicesConnection.SetDBParams(DBParams);
310 tony 209 end;
311    
312     procedure TCustomIBLocalDBSupport.OnDatabaseConnected(Sender: TObject);
313     begin
314 tony 272 if FInOnCreateDB then Exit;
315 tony 209 if not Enabled or (csDesigning in ComponentState) or
316     FInUpgrade then Exit; {Avoids problem if RECONNECT used in script}
317    
318     UpgradeCheck;
319    
320     if not DowngradePending and FNewDBCreated and assigned(FOnNewDatabaseOpen) then
321     OnNewDatabaseOpen(self);
322     FNewDBCreated := false;
323     end;
324    
325     procedure TCustomIBLocalDBSupport.OnAfterDatabaseDisconnect(Sender: TObject);
326     begin
327     FActiveDatabasePathName := '';
328     end;
329    
330     procedure TCustomIBLocalDBSupport.OnCreateDatabase(Sender: TObject);
331     var DBArchive: string;
332     begin
333 tony 272 if FInOnCreateDB then Exit;
334 tony 209
335 tony 272 FInOnCreateDB := true;
336     try
337     CheckEnabled;
338     DBArchive := EmptyDBArchive;
339     if DBArchive = '' then
340     raise EIBLocalException.Create(sEmptyDBArchiveMissing);
341 tony 209
342 tony 272 if not TUpgradeConfFile.IsAbsolutePath(DBArchive) then
343     DBArchive := SharedDataDir + DBArchive;
344 tony 209
345 tony 272 if not FileExists(DBArchive) then
346     raise EIBLocalException.CreateFmt(sEmptyDBArchiveNotFound,[DBArchive]);
347    
348     ServicesConnection.ConnectUsing(Database);
349     try
350     if not InternalCreateNewDatabase(DBArchive) then
351     begin
352     Database.DropDatabase;
353     raise EIBLocalException.Create(sCreateFailed);
354     end;
355     finally
356     ServicesConnection.Connected := false;
357 tony 209 end;
358 tony 272 FNewDBCreated := true;
359 tony 209 finally
360 tony 272 FInOnCreateDB := false;
361 tony 209 end;
362     end;
363    
364     procedure TCustomIBLocalDBSupport.SetFirebirdDirectory(AValue: string);
365     begin
366     if FFirebirdDirectory = AValue then Exit;
367     FFirebirdDirectory := ExcludeTrailingPathDelimiter(AValue);
368     end;
369    
370     procedure TCustomIBLocalDBSupport.SetupFirebirdEnv;
371     var sdd: string;
372     begin
373     if sysutils.GetEnvironmentVariable('FIREBIRD') = '' then
374     begin
375     if FirebirdDirectory <> '' then
376     begin
377     if not TUpgradeConfFile.IsAbsolutePath(FirebirdDirectory) then
378     FirebirdDirectory := SharedDataDir + FirebirdDirectory;
379     if FileExists(FirebirdDirectory + DirectorySeparator + 'firebird.conf') then
380     begin
381     SetEnvironmentVariable('FIREBIRD',PChar(FirebirdDirectory));
382     Exit;
383     end;
384     end;
385     if FileExists(SharedDataDir + 'firebird.conf') then
386     begin
387     sdd := SharedDataDir;
388     SetEnvironmentVariable('FIREBIRD',PChar(sdd));
389     end;
390     end;
391     end;
392    
393     procedure TCustomIBLocalDBSupport.UpgradeCheck;
394     begin
395     if not UpdateVersionNo then
396     Exit;
397    
398     if (CurrentDBVersionNo > RequiredVersionNo) and (iblAllowDowngrade in FOptions)then
399     {Possible recovery after failed upgrade}
400     PerformDowngrade(RequiredVersionNo)
401     else
402     if (CurrentDBVersionNo < RequiredVersionNo) and (iblAutoUpgrade in FOptions) then
403 tony 315 begin
404     if FUpgradeFailed then
405     begin
406     Add2Log(self,Format(sSkipUpgrade,[RequiredVersionNo]));
407     if MinimumVersionNo > CurrentDBVersionNo then
408     begin
409     Database.ForceClose;
410     IBError(ibxDBVersionProblem,[CurrentDBVersionNo, MinimumVersionNo]);
411     end
412     end
413     else
414     PerformUpgrade(RequiredVersionNo);
415     end;
416 tony 209 end;
417    
418 tony 315 procedure TCustomIBLocalDBSupport.Add2Log(Sender: TObject; Msg: string);
419     begin
420     //Do nothing
421     end;
422    
423 tony 209 function TCustomIBLocalDBSupport.AllowInitialisation: boolean;
424     begin
425     Result := true;
426     end;
427    
428     function TCustomIBLocalDBSupport.AllowRestore: boolean;
429     begin
430     Result := true;
431     end;
432    
433     procedure TCustomIBLocalDBSupport.CreateDir(DirName: string);
434     var ParentDirName: string;
435     begin
436     ParentDirName := ExtractFileDir(DirName);
437     if not DirectoryExists(ParentDirName) and (ParentDirName <> DirName) then
438     CreateDir(ParentDirName);
439     if not DirectoryExists(DirName) then
440     mkdir(DirName);
441     end;
442    
443     procedure TCustomIBLocalDBSupport.Downgrade(DBArchive: string);
444     begin
445     FDownGradeArchive := DBArchive;
446     end;
447    
448     procedure TCustomIBLocalDBSupport.DowngradeDone;
449     begin
450     FDownGradeArchive := '';
451     UpdateVersionNo;
452     end;
453    
454     function TCustomIBLocalDBSupport.GetDBNameAndPath: string;
455     begin
456     Result := DatabaseName;
457     if VendorName <> '' then
458     Result := VendorName + DirectorySeparator + Result
459     else
460     if Sysutils.VendorName <> '' then
461     Result := SysUtils.VendorName + DirectorySeparator + Result;
462     {$IFDEF UNIX}
463     Result := GetUserDir + '.' + Result;
464     {$ENDIF}
465     {$IFDEF WINDOWS}
466     Result := GetWindowsSpecialDir(CSIDL_LOCAL_APPDATA) + Result;
467     {$ENDIF}
468     if assigned(OnGetDatabaseName) then
469     OnGetDatabaseName(self,Result);
470     end;
471    
472     procedure TCustomIBLocalDBSupport.PrepareDBParams(DBParams: TStrings);
473    
474     procedure Remove(s: string);
475     var i: integer;
476     begin
477     i := DBParams.IndexOfName(s);
478     if i <> -1 then
479     DBParams.Delete(i);
480     end;
481    
482     begin
483 tony 315 {$IFDEF UNIX}
484     if Database.FirebirdAPI.GetClientMajor >= 3 then
485     begin
486 tony 209 Remove('user_name');
487     Remove('password');
488     DBParams.Values['user_name'] := 'SYSDBA';
489 tony 315 end;
490     {$ENDIF}
491     {$IFDEF WINDOWS}
492     DBParams.Values['user_name'] := 'SYSDBA';
493     DBParams.Values['password'] := 'masterkey';
494     {$ENDIF}
495 tony 209 end;
496    
497     procedure TCustomIBLocalDBSupport.SetDatabaseName(AValue: string);
498     begin
499     if FDatabaseName = AValue then Exit;
500     FDatabaseName := ExtractFileName(AValue);
501     end;
502    
503     function TCustomIBLocalDBSupport.UpdateVersionNo: boolean;
504     begin
505     Result := assigned(OnGetDBVersionNo);
506     if Result then
507     OnGetDBVersionNo(self,FCurrentDBVersionNo); {Update Version No}
508     end;
509    
510     constructor TCustomIBLocalDBSupport.Create(aOwner: TComponent);
511     begin
512     inherited Create(aOwner);
513     FEnabled := true;
514     FIBBase := TIBBase.Create(self);
515     FIBBase.AfterDatabaseConnect := @OnDatabaseConnected;
516     FIBBase.BeforeDatabaseConnect := @OnBeforeDatabaseConnect;
517     FIBBase.AfterDatabaseDisconnect := @OnAfterDatabaseDisconnect;
518     FIBBase.OnCreateDatabase := @OnCreateDatabase;
519     FUpgradeConfFile := 'upgrade.conf';
520     FOptions := [iblAutoUpgrade, iblAllowDowngrade];
521     FServicesConnection := TIBXServicesConnection.Create(self);
522     FServicesConnection.LoginPrompt := false;
523     FServicesConnection.Params.Values['user_name'] := 'SYSDBA';
524     FBackupService := TIBXServerSideBackupService.Create(self);
525 tony 315 FBackupService.ServicesConnection := ServicesConnection;
526 tony 209 FBackupService.Verbose := true;
527     FRestoreService := TIBXServerSideRestoreService.Create(self);
528 tony 315 FRestoreService.ServicesConnection := ServicesConnection;
529 tony 209 FRestoreService.Verbose := true;
530 tony 345 FSectionHeaderTemplate := sSectionheader;
531 tony 209 end;
532    
533     destructor TCustomIBLocalDBSupport.Destroy;
534     begin
535     if assigned(FIBBase) then FreeAndNil(FIBBase);
536     inherited Destroy;
537     end;
538    
539     function TCustomIBLocalDBSupport.DowngradePending: boolean;
540     begin
541     Result := FDownGradeArchive <> '';
542     end;
543    
544     procedure TCustomIBLocalDBSupport.NewDatabase;
545     begin
546     CheckEnabled;
547     if not AllowInitialisation then Exit;
548    
549     Database.DropDatabase;
550     Database.Connected := true;
551     end;
552    
553     procedure TCustomIBLocalDBSupport.PerformDowngrade(TargetVersionNo: integer);
554     var DBArchive: string;
555     begin
556     DBArchive := ChangeFileExt(ActiveDatabasePathName,'') +
557     '.' + IntToStr(TargetVersionNo) + '.gbk';
558     if FileExists(DBArchive) then
559 tony 315 begin
560     Add2Log(self,Format(sDowngrade,[TargetVersionNo]));
561 tony 209 Downgrade(DBArchive)
562 tony 315 end
563 tony 209 else
564     raise EIBLocalFatalError.CreateFmt(sNoDowngrade,[CurrentDBVersionNo,TargetVersionNo]);
565     end;
566    
567     procedure TCustomIBLocalDBSupport.PerformUpgrade(TargetVersionNo: integer);
568    
569     function GetUpgradeConfFile: string;
570     begin
571     Result := UpgradeConfFile;
572     if Result = '' then Exit;
573    
574     if not TUpgradeConfFile.IsAbsolutePath(Result) then
575     Result := SharedDataDir + Result;
576     end;
577    
578 tony 315 var OldVersionNo: integer;
579    
580 tony 209 begin
581     if FInUpgrade then Exit;
582    
583 tony 315 OldVersionNo := CurrentDBVersionNo;
584 tony 209 FUpgradeConf := TUpgradeConfFile.Create(GetUpgradeConfFile);
585     try
586     FUpgradeConf.CheckUpgradeAvailable(TargetVersionNo);
587     FInUpgrade := true;
588     try
589     ServicesConnection.ConnectUsing(Database);
590     try
591 tony 315 if not RunUpgradeDatabase(TargetVersionNo) then
592     begin
593     {DownGrade if possible}
594     PerformDowngrade(OldVersionNo);
595     Database.ForceClose;
596     FUpgradeFailed := true;
597     IBError(ibxeUpgradeFailed,[CurrentDBVersionNo]);
598     end;
599 tony 209 finally
600     ServicesConnection.Connected := false;
601     end;
602     finally
603     FInUpgrade := false;
604     end;
605     finally
606     FreeAndNil(FUpgradeConf);
607     end;
608 tony 315 FUpgradeFailed := false;
609     if CurrentDBVersionNo < MinimumVersionNo then
610     begin
611     Database.ForceClose;
612     IBError(ibxDBVersionProblem,[CurrentDBVersionNo,MinimumVersionNo]);
613     end;
614 tony 209 end;
615    
616     procedure TCustomIBLocalDBSupport.Loaded;
617     begin
618     inherited Loaded;
619     if (Database <> nil) and (Database.DatabaseName = '') then
620     Database.DatabaseName := ' '; {Avoid CheckDatabaseName Error}
621     DoDirSeparators(FEmptyDBArchive);
622     DoDirSeparators(FFirebirdDirectory);
623     DoDirSeparators(FUpgradeConfFile);
624     end;
625    
626     procedure TCustomIBLocalDBSupport.RestoreDatabase(filename: string);
627     begin
628     CheckEnabled;
629     if not AllowRestore then Exit;
630    
631     ServicesConnection.ConnectUsing(Database);
632     Database.Connected := false;
633     try
634     RestoreDatabaseFromArchive(filename);
635     finally
636     ServicesConnection.Connected := false;
637     Database.Connected := true;
638     end;
639     end;
640    
641     procedure TCustomIBLocalDBSupport.SaveDatabase(filename: string);
642     begin
643     CheckEnabled;
644     ServicesConnection.ConnectUsing(Database);
645     try
646     SaveDatabaseToArchive(filename);
647     finally
648     ServicesConnection.Connected := false;
649     end;
650     end;
651    
652     end.