1 |
(*
|
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 Test07;
|
28 |
|
29 |
{$mode objfpc}{$H+}
|
30 |
|
31 |
{Test 7: Open and read from Employee Database using TIBSQL}
|
32 |
|
33 |
{ This is a simple use of IBX to access the employee database in console mode.
|
34 |
The program opens the database, runs a query and writes the result to stdout.
|
35 |
|
36 |
}
|
37 |
|
38 |
interface
|
39 |
|
40 |
uses
|
41 |
Classes, SysUtils, TestApplication, IBXTestBase, IB, IBCustomDataSet, IBDatabase, IBSQL;
|
42 |
|
43 |
const
|
44 |
aTestID = '07';
|
45 |
aTestTitle = 'Open and read from Employee Database using IBSQL';
|
46 |
|
47 |
type
|
48 |
|
49 |
{ TTest07 }
|
50 |
|
51 |
TTest07 = class(TIBXTestBase)
|
52 |
private
|
53 |
FIBSQL: TIBSQL;
|
54 |
protected
|
55 |
procedure CreateObjects(Application: TTestApplication); override;
|
56 |
function GetTestID: AnsiString; override;
|
57 |
function GetTestTitle: AnsiString; override;
|
58 |
procedure InitTest; override;
|
59 |
public
|
60 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
|
61 |
end;
|
62 |
|
63 |
|
64 |
implementation
|
65 |
|
66 |
const
|
67 |
sqlExample =
|
68 |
'with recursive Depts As ( '+
|
69 |
'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+
|
70 |
'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+
|
71 |
'From DEPARTMENT Where HEAD_DEPT is NULL '+
|
72 |
'UNION ALL '+
|
73 |
'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+
|
74 |
'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+
|
75 |
'From DEPARTMENT D '+
|
76 |
'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+
|
77 |
')'+
|
78 |
|
79 |
'Select A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+
|
80 |
'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+
|
81 |
'From EMPLOYEE A '+
|
82 |
'JOIN Depts D On D.DEPT_NO = A.DEPT_NO';
|
83 |
|
84 |
{ TTest07 }
|
85 |
|
86 |
procedure TTest07.CreateObjects(Application: TTestApplication);
|
87 |
begin
|
88 |
inherited CreateObjects(Application);
|
89 |
FIBSQL := TIBSQL.Create(Application);
|
90 |
FIBSQL.Database := IBDatabase;
|
91 |
end;
|
92 |
|
93 |
function TTest07.GetTestID: AnsiString;
|
94 |
begin
|
95 |
Result := aTestID;
|
96 |
end;
|
97 |
|
98 |
function TTest07.GetTestTitle: AnsiString;
|
99 |
begin
|
100 |
Result := aTestTitle;
|
101 |
end;
|
102 |
|
103 |
procedure TTest07.InitTest;
|
104 |
begin
|
105 |
inherited InitTest;
|
106 |
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName;
|
107 |
ReadWriteTransaction;
|
108 |
end;
|
109 |
|
110 |
procedure TTest07.RunTest(CharSet: AnsiString; SQLDialect: integer);
|
111 |
var stats: TPerfCounters;
|
112 |
begin
|
113 |
IBDatabase.Connected := true;
|
114 |
with FIBSQL do
|
115 |
begin
|
116 |
SQL.Text := sqlExample + ' Order By 1';
|
117 |
Transaction.Active := true;
|
118 |
Prepare;
|
119 |
PrintMetaData(MetaData);
|
120 |
Statement.EnableStatistics(true);
|
121 |
writeln(OutFile,Plan);
|
122 |
ExecQuery;
|
123 |
try
|
124 |
while not EOF do
|
125 |
begin
|
126 |
ReportResult(Current);
|
127 |
Next;
|
128 |
end;
|
129 |
finally
|
130 |
Close;
|
131 |
end;
|
132 |
|
133 |
if Statement.GetPerfStatistics(stats) then
|
134 |
WritePerfStats(stats);
|
135 |
PrintAffectedRows(IBQuery);
|
136 |
writeln(OutFile);
|
137 |
writeln(OutFile,'------------------------------------------------------');
|
138 |
writeln(OutFile,'With Named Parameter');
|
139 |
SQL.Text := sqlExample + ' Where Hire_Date < :HireDate';
|
140 |
Transaction.Active := true;
|
141 |
ParamByName('HireDate').AsDateTime := StrToDateTime('1/1/1991');
|
142 |
ExecQuery;
|
143 |
try
|
144 |
while not EOF do
|
145 |
begin
|
146 |
ReportResult(Current);
|
147 |
Next;
|
148 |
end;
|
149 |
finally
|
150 |
Close;
|
151 |
end;
|
152 |
writeln(OutFile);
|
153 |
writeln(OutFile,'With Positional Parameter');
|
154 |
ParamCheck := false;
|
155 |
SQL.Text := sqlExample + ' Where Hire_Date < ?';
|
156 |
Transaction.Active := true;
|
157 |
Params[0].AsDateTime := StrToDateTime('1/1/1990');
|
158 |
ExecQuery;
|
159 |
try
|
160 |
while not EOF do
|
161 |
begin
|
162 |
ReportResult(Current);
|
163 |
Next;
|
164 |
end;
|
165 |
finally
|
166 |
Close;
|
167 |
end;
|
168 |
|
169 |
writeln(OutFile);
|
170 |
writeln(OutFile,'Get Employee Project');
|
171 |
ParamCheck := true;
|
172 |
Transaction.Active := true;
|
173 |
SQL.Text := 'Select * From GET_EMP_PROJ(:EMP_NO)';
|
174 |
ParamByName('EMP_NO').AsInteger := 4;
|
175 |
PrintMetaData(MetaData);
|
176 |
ExecQuery;
|
177 |
try
|
178 |
while not EOF do
|
179 |
begin
|
180 |
ReportResult(Current);
|
181 |
Next;
|
182 |
end;
|
183 |
finally
|
184 |
Close;
|
185 |
end;
|
186 |
|
187 |
writeln(OutFile);
|
188 |
writeln(OutFile,'Call Delete Employee - exception expected');
|
189 |
SQL.Text := 'Execute Procedure Delete_EMPLOYEE :EMP_NO';
|
190 |
ParamByName('EMP_NO').AsInteger := 11;
|
191 |
try
|
192 |
ExecQuery;
|
193 |
except on E:Exception do
|
194 |
writeln(OutFile,'Terminated with Exception:',E.Message);
|
195 |
end;
|
196 |
Transaction.Rollback;
|
197 |
end;
|
198 |
end;
|
199 |
|
200 |
initialization
|
201 |
RegisterTest(TTest07);
|
202 |
|
203 |
end.
|
204 |
|