ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/iblocaldb/IBXUpgradeConfFile.pas
Revision: 37
Committed: Mon Feb 15 14:44:25 2016 UTC (8 years, 2 months ago) by tony
Content type: text/x-pascal
File size: 7429 byte(s)
Log Message:
Committing updates for Release R1-4-0

File Contents

# Content
1 (*
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 IBXUpgradeConfFile;
27
28 {$mode objfpc}{$H+}
29
30 {
31 TUpgradeConfFile encapsulated a text file in “ini” file format with
32 the following sections:
33
34 [status]
35
36 This should have a single named value “current” giving the current database
37 schema number as in integer e.g.
38
39 current = 2
40
41 This should normally be set to the same value as the RequiredVersionNo property
42 and acts as a check to ensure that both are in sync.
43
44 [Version.nnn]
45
46 Where nnn is an integer with leaving zeroes. For example, “Version.002” is
47 the section read to upgrade the database schema from version 1 to version 2.
48 This section can contain the following named values:
49
50 Name Type Use
51
52 Upgrade String Name and optional path to SQL script used to perform
53 the upgrade. May either be absolute path or relative
54 to the upgrade configuration file. Either forwards or
55 back slashes may be used as the path delimiter.
56
57 Msg string Text message displayed in progress dialog while script is
58 active. Defaults to “Upgrading Database Schema to Version nnn”.
59
60 BackupDatabase yes/no If present and set to “yes” then a database backup in
61 gbak format is made before the upgrade is performed. The backup file is
62 located in the same directory as the database file and is given the same
63 name as the database file with the extension replaced with “.nnn.gbak”.
64 Where “nnn” is the current schema version number (i.e. prior to running
65 the upgrade script).
66
67 <Parameter Name> string Name and optional path to binary data file. May either be
68 absolute path or relative to the upgrade configuration file.
69 Either forwards or back slashes may be used as the path delimiter.
70
71
72 For example:
73
74 [Version.002]
75 Msg = Upgrading to Version 2
76 BackupDatabase = yes
77 Upgrade = patches/02-patch.sql
78 mugshot = images/man.png.gz
79
80 Note that in the above, “mugshot” is intended to be used to resolve an Update,
81 Insert or Delete query parameter in the 02-patch.sql file. E.g.
82
83 Update EMPLOYEE Set Photo =:MUGSHOT Where Emp_no = 2;
84
85 This is only applicable to BLOB columns and the above is interpreted as update
86 the EMPLOYEE table where the Emp_no is “2” and set the value of the Photo column
87 to the binary data contained in the file “images/man.png.gz”.
88 }
89
90 interface
91
92 uses
93 Classes, SysUtils, IniFiles;
94
95 type
96 TUpgradeInfo = record
97 UpdateSQLFile,
98 UserMessage: string;
99 BackupDB: boolean;
100 end;
101
102 { TUpgradeConfFile }
103
104 TUpgradeConfFile = class
105 private
106 FConfFileName: string;
107 FCurrentVersion: string;
108 FUpgradeInfo: TIniFile;
109 function GetUpgradeAvailableToVersion: integer;
110 public
111 constructor Create(aFileName: string);
112 destructor Destroy; override;
113 class function IsAbsolutePath(aPath: string): boolean;
114 function CheckUpgradeAvailable(RequiredVersionNo: integer): boolean;
115 function GetUpgradeInfo(VersionNo: integer; var UpgradeInfo: TUpgradeInfo): boolean;
116 function GetSourceFile(aName: string; var FileName: string): boolean;
117 property UpgradeAvailableToVersion: integer read GetUpgradeAvailableToVersion;
118 end;
119
120 EUpgradeConfFileError = class(Exception);
121
122
123 implementation
124
125 const
126 sSectionheader = 'Version.%.3d';
127
128 resourcestring
129 sInvalidConfFile = 'Database Upgrade Required, but the Upgrade File (%s) is missing or not specified';
130 sUpgradeRequired = 'Database Upgrade Required, but the Upgrade File is out of Date. '+
131 'Required Version = %d, Upgrade available for version %d';
132 sNoInfo = 'Upgrading Database Schema to Version %d';
133
134 { TUpgradeConfFile }
135
136 function TUpgradeConfFile.GetUpgradeAvailableToVersion: integer;
137 begin
138 Result := StrToInt(FUpgradeInfo.ReadString('Status','Current','0'))
139 end;
140
141 constructor TUpgradeConfFile.Create(aFileName: string);
142 begin
143 inherited Create;
144 FConfFileName := aFileName;
145 if (FConfFileName = '') or not FileExists(FConfFileName) then
146 raise EUpgradeConfFileError.CreateFmt(sInvalidConfFile,[FConfFileName]);
147 FUpgradeInfo := TIniFile.Create(FConfFileName);
148 end;
149
150 destructor TUpgradeConfFile.Destroy;
151 begin
152 if assigned(FUpgradeInfo) then FUpgradeInfo.Free;
153 inherited Destroy;
154 end;
155
156 class function TUpgradeConfFile.IsAbsolutePath(aPath: string): boolean;
157 begin
158 Result := false;
159 {$IFDEF WINDOWS}
160 Result := (ExtractFileDrive(aPath) <> '') or
161 ((Length(aPath) > 0) and (aPath[1] = DirectorySeparator));
162 {$ENDIF}
163 {$IFDEF UNIX}
164 Result := (Length(aPath) > 0) and (aPath[1] = DirectorySeparator);
165 {$ENDIF}
166 end;
167
168 function TUpgradeConfFile.CheckUpgradeAvailable(RequiredVersionNo: integer
169 ): boolean;
170 var CurVersion: integer;
171 begin
172 CurVersion := GetUpgradeAvailableToVersion;
173 if CurVersion < RequiredVersionNo then
174 raise EUpgradeConfFileError.CreateFmt(sUpgradeRequired, [RequiredVersionNo,CurVersion]);
175 end;
176
177 function TUpgradeConfFile.GetUpgradeInfo(VersionNo: integer;
178 var UpgradeInfo: TUpgradeInfo): boolean;
179 begin
180 Result := false;
181 FCurrentVersion := Format(sSectionheader,[VersionNo]);
182 UpgradeInfo.UserMessage := FUpgradeInfo.ReadString(FCurrentVersion,'Msg',
183 Format(sNoInfo,[VersionNo]));
184 UpgradeInfo.UpdateSQLFile := FUpgradeInfo.ReadString(FCurrentVersion,'Upgrade','');
185 Result := UpgradeInfo.UpdateSQLFile <> '';
186 if Result then
187 begin
188 DoDirSeparators(UpgradeInfo.UpdateSQLFile); {Resolve Platform dependencies}
189 if not IsAbsolutePath(UpgradeInfo.UpdateSQLFile) then
190 UpgradeInfo.UpdateSQLFile := ExtractFilePath(FConfFileName) + UpgradeInfo.UpdateSQLFile;
191 UpgradeInfo.BackupDB := CompareText(FUpgradeInfo.ReadString(FCurrentVersion,'BackupDatabase','no'),'yes') = 0;
192 Result := FileExists(UpgradeInfo.UpdateSQLFile);
193 end;
194 end;
195
196 function TUpgradeConfFile.GetSourceFile(aName: string; var FileName: string
197 ): boolean;
198 begin
199 FileName := FUpgradeInfo.ReadString(FCurrentVersion,aName,'');
200 DoDirSeparators(FileName);
201 if not IsAbsolutePath(FileName) then
202 FileName := ExtractFilePath(FConfFileName) + FileName;
203 Result := FileExists(FileName);
204 end;
205
206 end.
207