1 |
tony |
315 |
unit Test13; |
2 |
|
|
|
3 |
|
|
{$mode objfpc}{$H+} |
4 |
|
|
|
5 |
|
|
{Test 13: Open and read from Employee Database with ISQLMonitor} |
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 |
|
|
ISQLMonitor is used to trace the activity. |
11 |
|
|
|
12 |
|
|
} |
13 |
|
|
|
14 |
|
|
interface |
15 |
|
|
|
16 |
|
|
uses |
17 |
|
|
Classes, SysUtils, TestApplication, IBXTestBase, IB, IBCustomDataSet, IBDatabase, |
18 |
|
|
IBQuery, IBInternals, IBSQLMonitor; |
19 |
|
|
|
20 |
|
|
const |
21 |
|
|
aTestID = '13'; |
22 |
|
|
aTestTitle = 'Open and read from Employee Database with ISQLMonitor'; |
23 |
|
|
|
24 |
|
|
type |
25 |
|
|
|
26 |
|
|
{ TTest13 } |
27 |
|
|
|
28 |
|
|
TTest13 = class(TIBXTestBase) |
29 |
|
|
private |
30 |
|
|
FIBSQLMonitor: TIBSQLMonitor; |
31 |
|
|
FLog: TStringList; |
32 |
|
|
procedure HandleOnSQL(EventText: String; EventTime : TDateTime); |
33 |
|
|
protected |
34 |
|
|
procedure CreateObjects(Application: TTestApplication); override; |
35 |
|
|
function GetTestID: AnsiString; override; |
36 |
|
|
function GetTestTitle: AnsiString; override; |
37 |
|
|
procedure InitTest; override; |
38 |
|
|
public |
39 |
|
|
destructor Destroy; override; |
40 |
|
|
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
41 |
|
|
end; |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
implementation |
45 |
|
|
|
46 |
|
|
const |
47 |
|
|
sqlExample = |
48 |
|
|
'with recursive Depts As ( '+ |
49 |
|
|
'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+ |
50 |
|
|
'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+ |
51 |
|
|
'From DEPARTMENT Where HEAD_DEPT is NULL '+ |
52 |
|
|
'UNION ALL '+ |
53 |
|
|
'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+ |
54 |
|
|
'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+ |
55 |
|
|
'From DEPARTMENT D '+ |
56 |
|
|
'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+ |
57 |
|
|
')'+ |
58 |
|
|
|
59 |
|
|
'Select First 2 A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+ |
60 |
|
|
'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+ |
61 |
|
|
'From EMPLOYEE A '+ |
62 |
|
|
'JOIN Depts D On D.DEPT_NO = A.DEPT_NO'; |
63 |
|
|
|
64 |
|
|
{ TTest13 } |
65 |
|
|
|
66 |
|
|
procedure TTest13.HandleOnSQL(EventText: String; EventTime: TDateTime); |
67 |
|
|
begin |
68 |
|
|
FLog.Add('*Monitor* '+DateTimeToStr(EventTime)+' '+EventText); |
69 |
|
|
end; |
70 |
|
|
|
71 |
|
|
procedure TTest13.CreateObjects(Application: TTestApplication); |
72 |
|
|
begin |
73 |
|
|
inherited CreateObjects(Application); |
74 |
|
|
FIBSQLMonitor := TIBSQLMonitor.Create(Application); |
75 |
|
|
FIBSQLMonitor.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect, |
76 |
|
|
tfTransact, tfBlob, tfService, tfMisc]; |
77 |
|
|
IBDatabase.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect, |
78 |
|
|
tfTransact, tfBlob, tfService, tfMisc]; |
79 |
|
|
FIBSQLMonitor.OnSQL := @HandleOnSQL; |
80 |
|
|
FIBSQLMonitor.Enabled := true; |
81 |
|
|
FLog := TStringList.Create; |
82 |
|
|
end; |
83 |
|
|
|
84 |
|
|
function TTest13.GetTestID: AnsiString; |
85 |
|
|
begin |
86 |
|
|
Result := aTestID; |
87 |
|
|
end; |
88 |
|
|
|
89 |
|
|
function TTest13.GetTestTitle: AnsiString; |
90 |
|
|
begin |
91 |
|
|
Result := aTestTitle; |
92 |
|
|
end; |
93 |
|
|
|
94 |
|
|
procedure TTest13.InitTest; |
95 |
|
|
begin |
96 |
|
|
inherited InitTest; |
97 |
|
|
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName; |
98 |
|
|
ReadOnlyTransaction; |
99 |
|
|
end; |
100 |
|
|
|
101 |
|
|
destructor TTest13.Destroy; |
102 |
|
|
begin |
103 |
|
|
FLog.Free; |
104 |
|
|
inherited Destroy; |
105 |
|
|
end; |
106 |
|
|
|
107 |
|
|
procedure TTest13.RunTest(CharSet: AnsiString; SQLDialect: integer); |
108 |
|
|
var stats: TPerfCounters; |
109 |
|
|
i: integer; |
110 |
|
|
begin |
111 |
|
|
writeln(OutFile); |
112 |
|
|
EnableMonitoring; |
113 |
|
|
CheckSynchronize(1); |
114 |
|
|
with IBQuery do |
115 |
|
|
begin |
116 |
|
|
AllowAutoActivateTransaction := true; |
117 |
|
|
Unidirectional := true; |
118 |
|
|
SQL.Text := sqlExample; |
119 |
|
|
EnableStatistics := true; |
120 |
|
|
Active := true; |
121 |
|
|
PrintDataSet(IBQuery); |
122 |
|
|
|
123 |
|
|
if GetPerfStatistics(stats) then |
124 |
|
|
WritePerfStats(stats); |
125 |
|
|
PrintAffectedRows(IBQuery); |
126 |
|
|
writeln(OutFile); |
127 |
|
|
writeln(OutFile,'Reconnect'); |
128 |
|
|
IBDatabase.ReConnect; |
129 |
|
|
Unidirectional := false; |
130 |
|
|
Active := true; |
131 |
|
|
PrintDataSet(IBQuery); |
132 |
|
|
end; |
133 |
|
|
IBDatabase.Connected := false; |
134 |
|
|
CheckSynchronize(1); |
135 |
|
|
DisableMonitoring; |
136 |
|
|
for i := 0 to FLog.Count - 1 do |
137 |
|
|
writeln(OutFile,FLog[i]); |
138 |
|
|
end; |
139 |
|
|
|
140 |
|
|
initialization |
141 |
|
|
RegisterTest(TTest13); |
142 |
|
|
|
143 |
|
|
end. |
144 |
|
|
|