1 |
unit Test08; |
2 |
|
3 |
{$mode objfpc}{$H+} |
4 |
|
5 |
{Test 8: TIBDataset: Locate, Bookmark and Lookup} |
6 |
|
7 |
{ This test uses 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 |
It includes SQL parsing, and parameter setting prior to opening. The dataset |
10 |
is also searched abd bookmarked |
11 |
|
12 |
} |
13 |
|
14 |
interface |
15 |
|
16 |
uses |
17 |
Classes, SysUtils, TestApplication, IBXTestBase, DB, IB, IBCustomDataSet, IBDatabase, IBQuery; |
18 |
|
19 |
const |
20 |
aTestID = '08'; |
21 |
aTestTitle = 'TIBDataset: Locate, Bookmark and Lookup'; |
22 |
|
23 |
type |
24 |
|
25 |
{ TCalcQuery } |
26 |
|
27 |
TCalcQuery = class(TIBQuery) |
28 |
protected |
29 |
procedure CreateFields; override; |
30 |
end; |
31 |
|
32 |
{ TTest08 } |
33 |
|
34 |
TTest08 = class(TIBXTestBase) |
35 |
private |
36 |
FQuery: TIBQuery; |
37 |
procedure HandleBeforeOpen(DataSet: TDataSet); |
38 |
procedure HandleCalcFields(DataSet: TDataSet); |
39 |
procedure PrintFields(aDataSet: TDataSet); |
40 |
protected |
41 |
procedure CreateObjects(Application: TTestApplication); override; |
42 |
function GetTestID: AnsiString; override; |
43 |
function GetTestTitle: AnsiString; override; |
44 |
procedure InitTest; override; |
45 |
public |
46 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
47 |
end; |
48 |
|
49 |
|
50 |
implementation |
51 |
|
52 |
uses Variants, DateUtils; |
53 |
|
54 |
const |
55 |
sqlExample = |
56 |
'with recursive Depts As ( '+ |
57 |
'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+ |
58 |
'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+ |
59 |
'From DEPARTMENT Where HEAD_DEPT is NULL '+ |
60 |
'UNION ALL '+ |
61 |
'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+ |
62 |
'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+ |
63 |
'From DEPARTMENT D '+ |
64 |
'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+ |
65 |
')'+ |
66 |
|
67 |
'Select A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+ |
68 |
'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+ |
69 |
'From EMPLOYEE A '+ |
70 |
'JOIN Depts D On D.DEPT_NO = A.DEPT_NO order by 1'; |
71 |
|
72 |
{ TCalcQuery } |
73 |
|
74 |
procedure TCalcQuery.CreateFields; |
75 |
var f: TFieldDef; |
76 |
begin |
77 |
FieldDefs.Update; |
78 |
f := FieldDefs.AddFieldDef; |
79 |
f.Name := 'HireYear'; |
80 |
f.DataType := ftInteger; |
81 |
f.InternalCalcField := true; |
82 |
f := FieldDefs.AddFieldDef; |
83 |
f.Name := 'HireDatePlus1'; |
84 |
f.DataType := ftDate; |
85 |
f.InternalCalcField := true; |
86 |
inherited CreateFields; |
87 |
FieldByName('HireYear').FieldKind := fkCalculated; |
88 |
FieldByName('HireDatePlus1').FieldKind := fkCalculated; |
89 |
end; |
90 |
|
91 |
{ TTest08 } |
92 |
|
93 |
procedure TTest08.HandleBeforeOpen(DataSet: TDataSet); |
94 |
begin |
95 |
(Dataset as TIBParserDataset).Parser.Add2WhereClause('A.HIRE_DATE < :HIREDATE'); |
96 |
(DataSet as TIBQuery).ParamByName('HireDate').AsString := '1/5/1991'; |
97 |
end; |
98 |
|
99 |
procedure TTest08.HandleCalcFields(DataSet: TDataSet); |
100 |
begin |
101 |
DataSet.FieldByName('HireYear').AsInteger := YearOf(DataSet.FieldByName('HIRE_DATE').AsDateTime); |
102 |
DataSet.FieldByName('HireDatePlus1').AsDateTime := DataSet.FieldByName('HIRE_DATE').AsDateTime + 1; |
103 |
end; |
104 |
|
105 |
procedure TTest08.PrintFields(aDataSet: TDataSet); |
106 |
var i: integer; |
107 |
begin |
108 |
for i := 0 to aDataSet.Fields.Count - 1 do |
109 |
with aDataSet.Fields[i] do |
110 |
writeln(OutFile,'Field No ',FieldNo,' Name = ',FieldName, ' DataType = ',DataType); |
111 |
end; |
112 |
|
113 |
procedure TTest08.CreateObjects(Application: TTestApplication); |
114 |
begin |
115 |
inherited CreateObjects(Application); |
116 |
FQuery := TCalcQuery.Create(Application); |
117 |
FQuery.Database := IBDatabase; |
118 |
FQuery.BeforeOpen := @HandleBeforeOpen; |
119 |
FQuery.OnCalcFields := @HandleCalcFields; |
120 |
end; |
121 |
|
122 |
function TTest08.GetTestID: AnsiString; |
123 |
begin |
124 |
Result := aTestID; |
125 |
end; |
126 |
|
127 |
function TTest08.GetTestTitle: AnsiString; |
128 |
begin |
129 |
Result := aTestTitle; |
130 |
end; |
131 |
|
132 |
procedure TTest08.InitTest; |
133 |
begin |
134 |
inherited InitTest; |
135 |
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName; |
136 |
ReadOnlyTransaction; |
137 |
FQuery.SQL.Text := sqlExample; |
138 |
end; |
139 |
|
140 |
procedure TTest08.RunTest(CharSet: AnsiString; SQLDialect: integer); |
141 |
var stats: TPerfCounters; |
142 |
aBookmark: TBookmark; |
143 |
aResultFields: variant; |
144 |
i: integer; |
145 |
begin |
146 |
with FQuery do |
147 |
begin |
148 |
AllowAutoActivateTransaction := true; |
149 |
Active := true; |
150 |
PrintFields(FQuery); |
151 |
if Locate('EMP_NO', 12, []) then |
152 |
PrintDataSetRow(FQuery) |
153 |
else |
154 |
begin |
155 |
writeln(OutFile,'Error: Row not found for EMP_NO = 12'); |
156 |
Exit; |
157 |
end; |
158 |
|
159 |
aBookmark := Bookmark; |
160 |
if Locate('FIRST_NAME;LAST_NAME', VarArrayOf(['roger','Reeves']), [loCaseInsensitive]) then |
161 |
PrintDataSetRow(FQuery) |
162 |
else |
163 |
begin |
164 |
writeln(OutFile,'Error: Row not found for Roger Reeves'); |
165 |
Exit; |
166 |
end; |
167 |
Bookmark := aBookmark; |
168 |
writeln(OutFile,'Back to EMP_NO = 12'); |
169 |
PrintDataSetRow(FQuery); |
170 |
|
171 |
writeln(OutFile,'Locate Employee 20, First Name and Last Name'); |
172 |
aResultFields := Lookup('EMP_NO',20,'FIRST_NAME;LAST_NAME'); |
173 |
if varType(aResultFields) <> varNull then |
174 |
begin |
175 |
for i := VarArrayLowBound(aResultFields,1) to VarArrayHighBound(aResultFields,1) do |
176 |
writeln(OutFile,'Field No. ',i,' = ',aResultFields[i]); |
177 |
end |
178 |
else |
179 |
begin |
180 |
writeln(OutFile,'Lookup Failed'); |
181 |
Exit; |
182 |
end; |
183 |
|
184 |
|
185 |
writeln(OutFile); |
186 |
writeln(OutFile,'Print All'); |
187 |
PrintDataSet(FQuery); |
188 |
Active := false; |
189 |
end; |
190 |
end; |
191 |
|
192 |
initialization |
193 |
RegisterTest(TTest08); |
194 |
|
195 |
end. |
196 |
|