1 |
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 |
procedure ShowStatistics(Sender: TObject);
|
34 |
protected
|
35 |
procedure CreateObjects(Application: TTestApplication); override;
|
36 |
function GetTestID: AnsiString; override;
|
37 |
function GetTestTitle: AnsiString; override;
|
38 |
procedure InitTest; override;
|
39 |
public
|
40 |
destructor Destroy; override;
|
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 First 2 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 |
{ TTest13 }
|
66 |
|
67 |
procedure TTest13.HandleOnSQL(EventText: String; EventTime: TDateTime);
|
68 |
begin
|
69 |
FLog.Add('*Monitor* ' {+DateTimeToStr(EventTime)}+' '+EventText);
|
70 |
end;
|
71 |
|
72 |
procedure TTest13.ShowStatistics(Sender: TObject);
|
73 |
begin
|
74 |
writeln(OutFile,FIBSQLMonitor.ReadCount,' ISQL Monitor Messages Received');
|
75 |
end;
|
76 |
|
77 |
procedure TTest13.CreateObjects(Application: TTestApplication);
|
78 |
begin
|
79 |
inherited CreateObjects(Application);
|
80 |
FIBSQLMonitor := TIBSQLMonitor.Create(Application);
|
81 |
FIBSQLMonitor.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect,
|
82 |
tfTransact, tfBlob, tfService, tfMisc];
|
83 |
IBDatabase.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect,
|
84 |
tfTransact, tfBlob, tfService, tfMisc];
|
85 |
FIBSQLMonitor.OnSQL := @HandleOnSQL;
|
86 |
FIBSQLMonitor.OnMonitoringDisabled := @ShowStatistics;
|
87 |
FIBSQLMonitor.Enabled := true;
|
88 |
FLog := TStringList.Create;
|
89 |
end;
|
90 |
|
91 |
function TTest13.GetTestID: AnsiString;
|
92 |
begin
|
93 |
Result := aTestID;
|
94 |
end;
|
95 |
|
96 |
function TTest13.GetTestTitle: AnsiString;
|
97 |
begin
|
98 |
Result := aTestTitle;
|
99 |
end;
|
100 |
|
101 |
procedure TTest13.InitTest;
|
102 |
begin
|
103 |
inherited InitTest;
|
104 |
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName;
|
105 |
ReadOnlyTransaction;
|
106 |
end;
|
107 |
|
108 |
destructor TTest13.Destroy;
|
109 |
begin
|
110 |
FLog.Free;
|
111 |
inherited Destroy;
|
112 |
end;
|
113 |
|
114 |
procedure TTest13.RunTest(CharSet: AnsiString; SQLDialect: integer);
|
115 |
var stats: TPerfCounters;
|
116 |
i: integer;
|
117 |
begin
|
118 |
writeln(OutFile);
|
119 |
EnableMonitoring;
|
120 |
CheckSynchronize(1);
|
121 |
with IBQuery do
|
122 |
begin
|
123 |
AllowAutoActivateTransaction := true;
|
124 |
Unidirectional := true;
|
125 |
SQL.Text := sqlExample;
|
126 |
EnableStatistics := true;
|
127 |
Active := true;
|
128 |
PrintDataSet(IBQuery);
|
129 |
|
130 |
if GetPerfStatistics(stats) then
|
131 |
WritePerfStats(stats);
|
132 |
PrintAffectedRows(IBQuery);
|
133 |
writeln(OutFile);
|
134 |
writeln(OutFile,'Reconnect');
|
135 |
IBDatabase.ReConnect;
|
136 |
Unidirectional := false;
|
137 |
Active := true;
|
138 |
PrintDataSet(IBQuery);
|
139 |
end;
|
140 |
IBDatabase.Connected := false;
|
141 |
CheckSynchronize(1);
|
142 |
DisableMonitoring;
|
143 |
writeln(Outfile,MonitorHook.GetWriteCount,' ISQL Monitor Messages written');
|
144 |
Sleep(1000);
|
145 |
for i := 0 to FLog.Count - 1 do
|
146 |
writeln(OutFile,FLog[i]);
|
147 |
writeln(OutFile,FIBSQLMonitor.ReadCount,' ISQL Monitor Messages Received');
|
148 |
end;
|
149 |
|
150 |
initialization
|
151 |
RegisterTest(TTest13);
|
152 |
|
153 |
end.
|
154 |
|