1 |
tony |
402 |
(* |
2 |
|
|
* IBX Test suite. This program is used to test the IBX non-visual |
3 |
|
|
* components and provides a semi-automated pass/fail check for each test. |
4 |
|
|
* |
5 |
|
|
* The contents of this file are subject to the Initial Developer's |
6 |
|
|
* Public License Version 1.0 (the "License"); you may not use this |
7 |
|
|
* file except in compliance with the License. You may obtain a copy |
8 |
|
|
* of the License here: |
9 |
|
|
* |
10 |
|
|
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
11 |
|
|
* |
12 |
|
|
* Software distributed under the License is distributed on an "AS |
13 |
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
14 |
|
|
* implied. See the License for the specific language governing rights |
15 |
|
|
* and limitations under the License. |
16 |
|
|
* |
17 |
|
|
* The Initial Developer of the Original Code is Tony Whyman. |
18 |
|
|
* |
19 |
|
|
* The Original Code is (C) 2021 Tony Whyman, MWA Software |
20 |
|
|
* (http://www.mwasoftware.co.uk). |
21 |
|
|
* |
22 |
|
|
* All Rights Reserved. |
23 |
|
|
* |
24 |
|
|
* Contributor(s): ______________________________________. |
25 |
|
|
* |
26 |
|
|
*) |
27 |
|
|
unit Test29; |
28 |
|
|
|
29 |
|
|
{$mode objfpc}{$H+} |
30 |
|
|
|
31 |
|
|
{Test 29: Journalling} |
32 |
|
|
|
33 |
|
|
{ Description |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
interface |
37 |
|
|
|
38 |
|
|
uses |
39 |
|
|
Classes, SysUtils, TestApplication, IBXTestBase, DB, IB, IBJournal, |
40 |
|
|
IBCustomDataSet, IBUtils, IBExtract; |
41 |
|
|
|
42 |
|
|
const |
43 |
|
|
aTestID = '29'; |
44 |
|
|
aTestTitle = 'IBJournal Tests'; |
45 |
|
|
|
46 |
|
|
type |
47 |
|
|
|
48 |
|
|
{ TTest29 } |
49 |
|
|
|
50 |
|
|
TTest29 = class(TIBXTestBase) |
51 |
|
|
private |
52 |
|
|
FDataSet: TIBDataSet; |
53 |
|
|
FJournal: TIBJournal; |
54 |
|
|
FExtract: TIBExtract; |
55 |
|
|
FCreateArrayOnInsert: boolean; |
56 |
|
|
procedure HandleAfterInsert(DataSet: TDataSet); |
57 |
|
|
procedure HandleOnJournalEntry(Sender: TObject; aJnlEntry: PJnlEntry); |
58 |
|
|
procedure DoPlayback(dbDumpFile: string); |
59 |
|
|
protected |
60 |
|
|
procedure CreateObjects(Application: TTestApplication); override; |
61 |
|
|
function GetTestID: AnsiString; override; |
62 |
|
|
function GetTestTitle: AnsiString; override; |
63 |
|
|
procedure InitTest; override; |
64 |
|
|
public |
65 |
|
|
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
66 |
|
|
end; |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
implementation |
70 |
|
|
|
71 |
|
|
uses DateUtils; |
72 |
|
|
|
73 |
|
|
{ TTest29 } |
74 |
|
|
|
75 |
|
|
procedure TTest29.HandleAfterInsert(DataSet: TDataSet); |
76 |
|
|
var S, F: TStream; |
77 |
|
|
Str: TStringList; |
78 |
|
|
i,j: integer; |
79 |
|
|
ar: IArray; |
80 |
|
|
begin |
81 |
|
|
with DataSet do |
82 |
|
|
begin |
83 |
|
|
FieldByName('F1').AsInteger := 2; |
84 |
|
|
FieldByName('f2').AsFloat := 0.314; |
85 |
|
|
FieldByName('f3').AsFloat := 0.31412345678; |
86 |
|
|
FieldByName('F4').AsFloat := 101.314; |
87 |
|
|
FieldByName('F5').AsCurrency := 101.99; |
88 |
|
|
FieldByName('F6').AsDateTime := EncodeDateTime(2007,12,25,12,30,15,0); |
89 |
|
|
FieldByName('F7').AsDateTime := EncodeDateTime(2007,12,25,12,30,29,130); |
90 |
|
|
FieldByName('F8').AsString := 'XX'; |
91 |
|
|
FieldByName('F9').AsString := 'The Quick Brown Fox jumps over the lazy dog'; |
92 |
|
|
S := CreateBlobStream(FieldByName('F10'),bmWrite); |
93 |
|
|
F := TFileStream.Create('resources/Test29.dat',fmOpenRead); |
94 |
|
|
try |
95 |
|
|
S.CopyFrom(F,0); |
96 |
|
|
finally |
97 |
|
|
S.Free; |
98 |
|
|
F.Free; |
99 |
|
|
end; |
100 |
|
|
FieldByName('F11').AsLargeInt := 9223372036854775807; |
101 |
|
|
FieldByName('F12').AsInteger := 65566; |
102 |
|
|
FieldByName('F13').AsDateTime := EncodeDateTime(2007,12,26,12,30,45,0); |
103 |
|
|
Str := TStringList.Create; |
104 |
|
|
try |
105 |
|
|
Str.LoadFromFile('resources/Test04.txt'); |
106 |
|
|
FieldByName('F14').AsString := Str.Text; |
107 |
|
|
finally |
108 |
|
|
Str.Free; |
109 |
|
|
end; |
110 |
|
|
if FCreateArrayOnInsert then |
111 |
|
|
ar := TIBArrayField(FieldByName('MyArray')).CreateArray |
112 |
|
|
else |
113 |
|
|
ar := (DataSet as TIBCustomDataset).GetArray(TIBArrayField(FieldByName('MyArray'))); |
114 |
|
|
j := 100; |
115 |
|
|
for i := 0 to 16 do |
116 |
|
|
begin |
117 |
|
|
ar.SetAsInteger([i],j); |
118 |
|
|
dec(j); |
119 |
|
|
end; |
120 |
|
|
TIBArrayField(FieldByName('MyArray')).ArrayIntf := ar; |
121 |
|
|
// WriteArray(TIBArrayField(FieldByName('MyArray')).ArrayIntf); |
122 |
|
|
end; |
123 |
|
|
end; |
124 |
|
|
|
125 |
|
|
procedure TTest29.HandleOnJournalEntry(Sender: TObject; aJnlEntry: PJnlEntry); |
126 |
|
|
begin |
127 |
|
|
writeln(OutFile,'Journal Entry Made'); |
128 |
|
|
writeln(OutFile,IBFormatJnlEntry(aJnlEntry)); |
129 |
|
|
writeln(OutFile); |
130 |
|
|
end; |
131 |
|
|
|
132 |
|
|
procedure TTest29.DoPlayback(dbDumpFile: string); |
133 |
|
|
begin |
134 |
|
|
with TJournalPlayer.Create do |
135 |
|
|
try |
136 |
|
|
FJournal.ReplayJournal(FJournal.JournalFilePath); |
137 |
|
|
IBTransaction.Active := true; |
138 |
|
|
FExtract.ExtractObject(eoDatabase,'',[etData,etGrantsToUser]); |
139 |
|
|
FExtract.Items.SaveToFile(dbDumpFile); |
140 |
|
|
IBTransaction.Active := false; |
141 |
|
|
finally |
142 |
|
|
Free; |
143 |
|
|
end; |
144 |
|
|
end; |
145 |
|
|
|
146 |
|
|
procedure TTest29.CreateObjects(Application: TTestApplication); |
147 |
|
|
begin |
148 |
|
|
inherited CreateObjects(Application); |
149 |
|
|
FDataSet := TIBDataSet.Create(Application); |
150 |
|
|
with FDataSet do |
151 |
|
|
begin |
152 |
|
|
Name := 'Dataset' + GetTestID; |
153 |
|
|
Database := IBDatabase; |
154 |
|
|
Transaction := IBTransaction; |
155 |
|
|
SelectSQL.Text := 'Select A.TABLEKEY, A.F1, A.F2, A.F3, A.F4, A.F5, A.F6,'+ |
156 |
|
|
' A.F7, A.F8, A.F9, A.F10, A.F11, A."f12", A.F13, A.F14, A.F15, A.MyArray, A.'+ |
157 |
|
|
'GRANTS, A."My Field" as MYFIELD1, A."MY Field" as MYFIELD2 From IBXTEST A'; |
158 |
|
|
InsertSQL.Text := |
159 |
|
|
'Insert Into IBXTEST(TABLEKEY, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, "f12", F13, F14, MyArray, '+ |
160 |
|
|
' GRANTS) Values(:TABLEKEY, :F1, :F2, :F3, :F4, :F5, :F6, :F7,'+ |
161 |
|
|
':F8, :F9, :F10, :F11, :F12, :F13, :F14, :MyArray, :GRANTS) Returning MyArray, F15'; |
162 |
|
|
RefreshSQL.Text := |
163 |
|
|
'Select A.TABLEKEY, A.F1, A.F2, A.F3, A.F4, A.F5, A.F6,' + |
164 |
|
|
' A.F7, A.F8, A.F9, A.F10, A.F11, A."f12", A.F13, A.F14, A.MyArray, A.'+ |
165 |
|
|
'GRANTS, A."My Field" as MYFIELD1, A."MY Field" as MYFIELD2 From IBXTEST A '+ |
166 |
|
|
'Where A.TABLEKEY = :TABLEKEY'; |
167 |
|
|
ModifySQL.Text := |
168 |
|
|
'Update IBXTEST A Set ' + |
169 |
|
|
' A.F1 = :F1,' + |
170 |
|
|
' A.F2 = :F2,' + |
171 |
|
|
' A.F3 = :F3,' + |
172 |
|
|
' A.F4 = :F4,' + |
173 |
|
|
' A.F5 = :F5,' + |
174 |
|
|
' A.F6 = :F6,' + |
175 |
|
|
' A.F7 = :F7,' + |
176 |
|
|
' A.F8 = :F8,' + |
177 |
|
|
' A.F9 = :F9,' + |
178 |
|
|
' A.F10 = :F10,' + |
179 |
|
|
' A.F11 = :F11,' + |
180 |
|
|
' A."f12" = :F12,' + |
181 |
|
|
' A.F13 = :F13,' + |
182 |
|
|
' A.F14 = :F14,' + |
183 |
|
|
' A.MyArray = :MyArray, ' + |
184 |
|
|
' A."My Field" = :MYFIELD1,'+ |
185 |
|
|
' A."MY Field" = :MYFIELD2,'+ |
186 |
|
|
' A.GRANTS = :GRANTS '+ |
187 |
|
|
'Where A.TABLEKEY = :OLD_TABLEKEY RETURNING A.MyArray, A.F15'; |
188 |
|
|
DeleteSQL.Text := |
189 |
|
|
'Delete From IBXTEST A '+ |
190 |
|
|
'Where A.TABLEKEY = :OLD_TABLEKEY'; |
191 |
|
|
DataSetCloseAction := dcSaveChanges; |
192 |
|
|
AutoCommit := acDisabled; |
193 |
|
|
GeneratorField.Generator := 'IBXGEN'; |
194 |
|
|
GeneratorField.Field := 'TABLEKEY'; |
195 |
|
|
GeneratorField.ApplyOnEvent := gaeOnNewRecord; |
196 |
|
|
AfterInsert := @HandleAfterInsert; |
197 |
|
|
end; |
198 |
|
|
FJournal := TIBJournal.Create(Application); |
199 |
|
|
with FJournal do |
200 |
|
|
begin |
201 |
|
|
Database := IBDatabase; |
202 |
|
|
ApplicationName := 'Test29'; |
203 |
|
|
RetainJournal := true; |
204 |
|
|
OnJournalEntry := @HandleOnJournalEntry; |
205 |
|
|
end; |
206 |
|
|
FExtract := TIBExtract.Create(Application); |
207 |
|
|
FExtract.Database := IBDatabase; |
208 |
|
|
FExtract.Transaction := IBTransaction; |
209 |
|
|
end; |
210 |
|
|
|
211 |
|
|
function TTest29.GetTestID: AnsiString; |
212 |
|
|
begin |
213 |
|
|
Result := aTestID; |
214 |
|
|
end; |
215 |
|
|
|
216 |
|
|
function TTest29.GetTestTitle: AnsiString; |
217 |
|
|
begin |
218 |
|
|
Result := aTestTitle; |
219 |
|
|
end; |
220 |
|
|
|
221 |
|
|
procedure TTest29.InitTest; |
222 |
|
|
begin |
223 |
|
|
IBDatabase.DatabaseName := Owner.GetNewDatabaseName; |
224 |
|
|
IBDatabase.CreateIfNotExists := true; |
225 |
|
|
ReadWriteTransaction; |
226 |
|
|
end; |
227 |
|
|
|
228 |
|
|
procedure TTest29.RunTest(CharSet: AnsiString; SQLDialect: integer); |
229 |
|
|
var OldDefaultFormatSettings: TFormatSettings; |
230 |
|
|
i: integer; |
231 |
|
|
dbDumpFile: string; |
232 |
|
|
begin |
233 |
|
|
OldDefaultFormatSettings := DefaultFormatSettings; |
234 |
|
|
IBDatabase.CreateDatabase; |
235 |
|
|
try |
236 |
|
|
try |
237 |
|
|
DefaultFormatSettings.LongTimeFormat := 'HH:MM:SS.zzzz'; |
238 |
|
|
FJournal.Enabled := true; |
239 |
|
|
IBTransaction.Active := true; |
240 |
|
|
FDataSet.Active := true; |
241 |
|
|
ListFields(FDataset); |
242 |
|
|
writeln(OutFile,'Add a record'); |
243 |
|
|
FDataSet.Append; |
244 |
|
|
FDataSet.Post; |
245 |
|
|
writeln(OutFile,'Add and edit a record'); |
246 |
|
|
FCreateArrayOnInsert := true; |
247 |
|
|
FDataSet.Append; |
248 |
|
|
FDataSet.Post; |
249 |
|
|
FDataSet.Edit; |
250 |
|
|
FDataSet.FieldByName('F1').AsInteger := 199; |
251 |
|
|
FDataSet.FieldByName('MYField1').AsString := 'My Field'; |
252 |
|
|
FDataSet.FieldByName('MYFIELD2').AsString := 'MY Field'; |
253 |
|
|
PrintDataSetRow(FDataSet); |
254 |
|
|
FDataSet.Post; |
255 |
|
|
IBTransaction.Commit; |
256 |
|
|
|
257 |
|
|
IBTransaction.Active := true; |
258 |
|
|
FDataSet.Active := true; |
259 |
|
|
PrintDataSet(FDataSet); |
260 |
|
|
writeln(OutFile,'Delete a record'); |
261 |
|
|
FDataSet.First; |
262 |
|
|
FDataSet.Delete; |
263 |
|
|
PrintDataSet(FDataSet); |
264 |
|
|
writeln(OutFile,'Rollback Retaining'); |
265 |
|
|
IBTransaction.RollbackRetaining; |
266 |
|
|
FDataSet.Active := false; |
267 |
|
|
FDataSet.Active := true; |
268 |
|
|
PrintDataSet(FDataSet); |
269 |
|
|
writeln(OutFile,'Delete a record'); |
270 |
|
|
FDataSet.First; |
271 |
|
|
FDataSet.Delete; |
272 |
|
|
PrintDataSet(FDataSet); |
273 |
|
|
writeln(OutFile,'Rollback'); |
274 |
|
|
IBTransaction.Rollback; |
275 |
|
|
IBTransaction.Active := true; |
276 |
|
|
FDataSet.Active := true; |
277 |
|
|
PrintDataSet(FDataSet); |
278 |
|
|
writeln(OutFile,'Commit Retaining'); |
279 |
|
|
FDataSet.Append; |
280 |
|
|
FDataSet.Post; |
281 |
|
|
IBTransaction.CommitRetaining; |
282 |
|
|
FDataSet.Active := false; |
283 |
|
|
FDataSet.Active := true; |
284 |
|
|
PrintDataSet(FDataSet); |
285 |
|
|
writeln(OutFile,'Commit'); |
286 |
|
|
IBTransaction.Commit; |
287 |
|
|
IBTransaction.Active := true; |
288 |
|
|
FDataSet.Active := true; |
289 |
|
|
PrintDataSet(FDataSet); |
290 |
|
|
IBTransaction.Active := false; |
291 |
|
|
PrintJournalTable(IBDatabase.Attachment); |
292 |
|
|
|
293 |
|
|
FJournal.Enabled := false; |
294 |
|
|
writeln(OutFile); |
295 |
|
|
{print out journal} |
296 |
|
|
writeln(OutFile,'Low Level Journal File Print out'); |
297 |
|
|
PrintJournalFile(FJournal.JournalFilePath); |
298 |
|
|
writeln(OutFile); |
299 |
|
|
writeln(OutFile,'Print out Journal File using TIBJournal'); |
300 |
|
|
with TJournalPlayer.Create do |
301 |
|
|
try |
302 |
|
|
LoadJournalFile(FJournal.JournalFilePath, FJournal.Database); |
303 |
|
|
for i := 0 to JnlEntryCount - 1 do |
304 |
|
|
writeln(OutFile,IBFormatJnlEntry(JnlEntry[i])); |
305 |
|
|
writeln(OutFile); |
306 |
|
|
finally |
307 |
|
|
Free; |
308 |
|
|
end; |
309 |
|
|
|
310 |
|
|
{Dump Database to text file} |
311 |
|
|
dbDumpFile := ChangeFileExt(GetOutFile,'.db1'); |
312 |
|
|
IBTransaction.Active := true; |
313 |
|
|
{Drop journaling suuport before dumping database} |
314 |
|
|
IBDatabase.Attachment.ExecImmediate(IBTransaction.TransactionIntf,'Drop Table IBX$JOURNALS'); |
315 |
|
|
IBDatabase.Attachment.ExecImmediate(IBTransaction.TransactionIntf,'Drop Sequence IBX$SESSIONS'); |
316 |
|
|
IBTransaction.Commit; |
317 |
|
|
IBTransaction.Active := true; |
318 |
|
|
FExtract.ExtractObject(eoDatabase,'',[etData,etGrantsToUser]); |
319 |
|
|
FExtract.Items.SaveToFile(dbDumpFile); |
320 |
|
|
IBTransaction.Active := false; |
321 |
|
|
|
322 |
|
|
finally |
323 |
|
|
DefaultFormatSettings := OldDefaultFormatSettings; |
324 |
|
|
IBDatabase.DropDatabase; |
325 |
|
|
end; |
326 |
|
|
|
327 |
|
|
{now playback the log and restore the database} |
328 |
|
|
IBDatabase.CreateDatabase; |
329 |
|
|
try |
330 |
|
|
DefaultFormatSettings.LongTimeFormat := 'HH:MM:SS.zzzz'; |
331 |
|
|
DoPlayback(ChangeFileExt(dbDumpFile,'.db2')); |
332 |
|
|
finally |
333 |
|
|
DefaultFormatSettings := OldDefaultFormatSettings; |
334 |
|
|
IBDatabase.DropDatabase; |
335 |
|
|
end; |
336 |
|
|
|
337 |
|
|
writeln(OutFile); |
338 |
|
|
writeln(OutFile,'Comparing original database with restored database'); |
339 |
|
|
CompareFiles(dbDumpFile, ChangeFileExt(dbDumpFile,'.db2')); |
340 |
|
|
except on E: Exception do |
341 |
|
|
begin |
342 |
|
|
writeln(OutFile,'Terminated with Exception: ' + E.Message); |
343 |
|
|
IBDatabase.ForceClose; |
344 |
|
|
end; |
345 |
|
|
end; |
346 |
|
|
end; |
347 |
|
|
|
348 |
|
|
initialization |
349 |
|
|
RegisterTest(TTest29); |
350 |
|
|
|
351 |
|
|
end. |
352 |
|
|
|