1 |
tony |
315 |
unit Test01; |
2 |
|
|
|
3 |
|
|
{$mode objfpc}{$H+} |
4 |
|
|
|
5 |
|
|
{Test 1: Open and read from Employee Database} |
6 |
|
|
|
7 |
|
|
{ This is a simple use of IBX to access the employee database in console mode. |
8 |
|
|
The program opens the database, runs a query and writes the result to stdout. |
9 |
|
|
|
10 |
|
|
The test is run first with unidirectional buffering and then with bi-directional |
11 |
|
|
buffering. |
12 |
|
|
|
13 |
|
|
Both client side and server side filters are tested with bi-directional buffering. |
14 |
|
|
|
15 |
|
|
Finally, a beforeOpen handler is used to dynamically filter the results. |
16 |
|
|
|
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
interface |
20 |
|
|
|
21 |
|
|
uses |
22 |
|
|
Classes, SysUtils, TestApplication, IBXTestBase, DB, IB, IBCustomDataSet, IBDatabase, IBQuery; |
23 |
|
|
|
24 |
|
|
const |
25 |
|
|
aTestID = '01'; |
26 |
|
|
aTestTitle = 'Open and read from Employee Database'; |
27 |
|
|
|
28 |
|
|
type |
29 |
|
|
|
30 |
|
|
{ TTest1 } |
31 |
|
|
|
32 |
|
|
TTest1 = class(TIBXTestBase) |
33 |
|
|
private |
34 |
|
|
procedure ClientSideFilter(DataSet: TDataSet; var Accept: Boolean); |
35 |
|
|
procedure HandleBeforeOpen(DataSet: TDataSet); |
36 |
|
|
protected |
37 |
|
|
function GetTestID: AnsiString; override; |
38 |
|
|
function GetTestTitle: AnsiString; override; |
39 |
|
|
procedure InitTest; override; |
40 |
|
|
public |
41 |
|
|
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
42 |
|
|
end; |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
implementation |
46 |
|
|
|
47 |
|
|
const |
48 |
|
|
sqlExample = |
49 |
|
|
'with recursive Depts As ( '+ |
50 |
|
|
'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+ |
51 |
|
|
'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+ |
52 |
|
|
'From DEPARTMENT Where HEAD_DEPT is NULL '+ |
53 |
|
|
'UNION ALL '+ |
54 |
|
|
'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+ |
55 |
|
|
'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+ |
56 |
|
|
'From DEPARTMENT D '+ |
57 |
|
|
'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+ |
58 |
|
|
')'+ |
59 |
|
|
|
60 |
|
|
'Select A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+ |
61 |
|
|
'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+ |
62 |
|
|
'From EMPLOYEE A '+ |
63 |
|
|
'JOIN Depts D On D.DEPT_NO = A.DEPT_NO'; |
64 |
|
|
|
65 |
|
|
{ TTest1 } |
66 |
|
|
|
67 |
|
|
procedure TTest1.ClientSideFilter(DataSet: TDataSet; var Accept: Boolean); |
68 |
|
|
begin |
69 |
|
|
Accept := DataSet.FieldByName('HIRE_DATE').AsDateTime > EncodeDate(1994 ,1,1); |
70 |
|
|
end; |
71 |
|
|
|
72 |
|
|
procedure TTest1.HandleBeforeOpen(DataSet: TDataSet); |
73 |
|
|
begin |
74 |
|
|
(DataSet as TIBParserDataSet).Parser.Add2WhereClause('FIRST_NAME = :FN'); |
75 |
|
|
(DataSet as TIBQuery).ParamByName('FN').AsString := 'Claudia'; |
76 |
|
|
end; |
77 |
|
|
|
78 |
|
|
function TTest1.GetTestID: AnsiString; |
79 |
|
|
begin |
80 |
|
|
Result := aTestID; |
81 |
|
|
end; |
82 |
|
|
|
83 |
|
|
function TTest1.GetTestTitle: AnsiString; |
84 |
|
|
begin |
85 |
|
|
Result := aTestTitle; |
86 |
|
|
end; |
87 |
|
|
|
88 |
|
|
procedure TTest1.InitTest; |
89 |
|
|
begin |
90 |
|
|
inherited InitTest; |
91 |
|
|
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName; |
92 |
|
|
ReadOnlyTransaction; |
93 |
|
|
end; |
94 |
|
|
|
95 |
|
|
procedure TTest1.RunTest(CharSet: AnsiString; SQLDialect: integer); |
96 |
|
|
var stats: TPerfCounters; |
97 |
|
|
begin |
98 |
|
|
with IBQuery do |
99 |
|
|
begin |
100 |
|
|
AllowAutoActivateTransaction := true; |
101 |
|
|
writeln(OutFile,'Read dataset unidirectional buffering'); |
102 |
|
|
Unidirectional := true; |
103 |
|
|
SQL.Text := sqlExample; |
104 |
|
|
EnableStatistics := true; |
105 |
|
|
Active := true; |
106 |
|
|
PrintDataSet(IBQuery); |
107 |
|
|
|
108 |
|
|
if GetPerfStatistics(stats) then |
109 |
|
|
WritePerfStats(stats); |
110 |
|
|
PrintAffectedRows(IBQuery); |
111 |
|
|
writeln(OutFile); |
112 |
|
|
writeln(OutFile,'Reconnect'); |
113 |
|
|
writeln(OutFile,'Read dataset bidirectional buffering'); |
114 |
|
|
IBDatabase.ReConnect; |
115 |
|
|
Unidirectional := false; |
116 |
|
|
Active := true; |
117 |
|
|
PrintDataSet(IBQuery); |
118 |
|
|
Active := false; |
119 |
|
|
writeln(OutFile,'Server Side Filter: Hire Date < 1/1/90'); |
120 |
|
|
SQLFiltered := true; |
121 |
|
|
SQLFilterParams.Text := 'HIRE_DATE < ''1990.01.01'''; |
122 |
|
|
Active := true; |
123 |
|
|
PrintDataSet(IBQuery); |
124 |
|
|
Active := false; |
125 |
|
|
writeln(Outfile,'Client side Filter: Hire Date > 1/1/94'); |
126 |
|
|
SQLFiltered := false; |
127 |
|
|
Filtered := true; |
128 |
|
|
OnFilterRecord := @ClientSideFilter; |
129 |
|
|
Active := true; |
130 |
|
|
PrintDataSet(IBQuery); |
131 |
|
|
Active := false; |
132 |
|
|
Filtered := false; |
133 |
|
|
writeln(Outfile,'TIBQuery with open parameters - select only records with First Name = Claudia'); |
134 |
|
|
BeforeOpen := @HandleBeforeOpen; |
135 |
|
|
Active := true; |
136 |
|
|
PrintDataSet(IBQuery); |
137 |
|
|
|
138 |
|
|
end; |
139 |
|
|
end; |
140 |
|
|
|
141 |
|
|
initialization |
142 |
|
|
RegisterTest(TTest1); |
143 |
|
|
|
144 |
|
|
end. |
145 |
|
|
|