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