1 |
tony |
315 |
unit Test14; |
2 |
|
|
|
3 |
|
|
{$mode objfpc}{$H+} |
4 |
|
|
|
5 |
|
|
{Test 14: 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 |
|
|
The Monitor is in a separate process |
13 |
|
|
|
14 |
|
|
} |
15 |
|
|
|
16 |
|
|
interface |
17 |
|
|
|
18 |
|
|
uses |
19 |
|
|
Classes, SysUtils, TestApplication, IBXTestBase, IB, IBCustomDataSet, IBDatabase, |
20 |
|
|
IBQuery, IBInternals, IBSQLMonitor, Process {$IFDEF UNIX}, BaseUnix {$ENDIF}; |
21 |
|
|
|
22 |
|
|
const |
23 |
|
|
aTestID = '14'; |
24 |
|
|
aTestTitle = 'Open and read from Employee Database with ISQLMonitor and external monitor'; |
25 |
|
|
|
26 |
|
|
type |
27 |
|
|
|
28 |
|
|
{ TTest14 } |
29 |
|
|
|
30 |
|
|
TTest14 = class(TIBXTestBase) |
31 |
|
|
private |
32 |
|
|
class var FTerminated: boolean; |
33 |
|
|
private |
34 |
|
|
FIBSQLMonitor: TIBSQLMonitor; |
35 |
|
|
FLog: TStringList; |
36 |
|
|
FProcess: TProcess; |
37 |
|
|
FIsChild: boolean; |
38 |
|
|
FLogFile: Text; |
39 |
|
|
{$IFDEF UNIX} |
40 |
|
|
Foa,Fna : PSigActionRec; |
41 |
|
|
procedure SetupSignalHandler; |
42 |
|
|
{$ENDIF} |
43 |
|
|
procedure HandleOnSQL(EventText: String; EventTime : TDateTime); |
44 |
|
|
protected |
45 |
|
|
procedure CreateObjects(Application: TTestApplication); override; |
46 |
|
|
function GetTestID: AnsiString; override; |
47 |
|
|
function GetTestTitle: AnsiString; override; |
48 |
|
|
procedure InitTest; override; |
49 |
|
|
procedure ProcessResults; override; |
50 |
|
|
public |
51 |
|
|
destructor Destroy; override; |
52 |
|
|
function ChildProcess: boolean; override; |
53 |
|
|
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
54 |
|
|
end; |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
implementation |
58 |
|
|
|
59 |
|
|
const |
60 |
|
|
sqlExample = |
61 |
|
|
'with recursive Depts As ( '+ |
62 |
|
|
'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+ |
63 |
|
|
'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+ |
64 |
|
|
'From DEPARTMENT Where HEAD_DEPT is NULL '+ |
65 |
|
|
'UNION ALL '+ |
66 |
|
|
'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+ |
67 |
|
|
'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+ |
68 |
|
|
'From DEPARTMENT D '+ |
69 |
|
|
'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+ |
70 |
|
|
')'+ |
71 |
|
|
|
72 |
|
|
'Select First 2 A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+ |
73 |
|
|
'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+ |
74 |
|
|
'From EMPLOYEE A '+ |
75 |
|
|
'JOIN Depts D On D.DEPT_NO = A.DEPT_NO'; |
76 |
|
|
|
77 |
|
|
LogFileName = 'Test_ISQLMonitor' + '.log'; |
78 |
|
|
|
79 |
|
|
{ TTest14 } |
80 |
|
|
|
81 |
|
|
procedure TTest14.HandleOnSQL(EventText: String; EventTime: TDateTime); |
82 |
|
|
begin |
83 |
|
|
if FIsChild then |
84 |
|
|
begin |
85 |
|
|
writeln(FLogFile,'*Monitor* '+DateTimeToStr(EventTime)+' '+EventText); |
86 |
|
|
Flush(FLogFile); |
87 |
|
|
end; |
88 |
|
|
end; |
89 |
|
|
|
90 |
|
|
{$IFDEF UNIX} |
91 |
|
|
procedure DoSigTerm(sig : cint);cdecl; |
92 |
|
|
begin |
93 |
|
|
TTest14.FTerminated := true; |
94 |
|
|
end; |
95 |
|
|
|
96 |
|
|
procedure TTest14.SetupSignalHandler; |
97 |
|
|
begin |
98 |
|
|
FTerminated := false; |
99 |
|
|
new(Fna); |
100 |
|
|
new(Foa); |
101 |
|
|
Fna^.sa_Handler:=SigActionHandler(@DoSigTerm); |
102 |
|
|
fillchar(Fna^.Sa_Mask,sizeof(Fna^.sa_mask),#0); |
103 |
|
|
Fna^.Sa_Flags:=0; |
104 |
|
|
{$ifdef Linux} // Linux specific |
105 |
|
|
Fna^.Sa_Restorer:=Nil; |
106 |
|
|
{$endif} |
107 |
|
|
if fpSigAction(SigTerm,Fna,Foa)<>0 then |
108 |
|
|
begin |
109 |
|
|
writeln(OutFile,'Error setting signal handler: ',fpgeterrno,'.'); |
110 |
|
|
halt(1); |
111 |
|
|
end; |
112 |
|
|
end; |
113 |
|
|
{$ENDIF} |
114 |
|
|
|
115 |
|
|
procedure TTest14.CreateObjects(Application: TTestApplication); |
116 |
|
|
begin |
117 |
|
|
inherited CreateObjects(Application); |
118 |
|
|
FIBSQLMonitor := TIBSQLMonitor.Create(Application); |
119 |
|
|
FIBSQLMonitor.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect, |
120 |
|
|
tfTransact, tfBlob, tfService, tfMisc]; |
121 |
|
|
IBDatabase.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect, |
122 |
|
|
tfTransact, tfBlob, tfService, tfMisc]; |
123 |
|
|
FIBSQLMonitor.OnSQL := @HandleOnSQL; |
124 |
|
|
FLog := TStringList.Create; |
125 |
|
|
FProcess := TProcess.Create(Application); |
126 |
|
|
end; |
127 |
|
|
|
128 |
|
|
function TTest14.GetTestID: AnsiString; |
129 |
|
|
begin |
130 |
|
|
Result := aTestID; |
131 |
|
|
end; |
132 |
|
|
|
133 |
|
|
function TTest14.GetTestTitle: AnsiString; |
134 |
|
|
begin |
135 |
|
|
Result := aTestTitle; |
136 |
|
|
end; |
137 |
|
|
|
138 |
|
|
procedure TTest14.InitTest; |
139 |
|
|
begin |
140 |
|
|
inherited InitTest; |
141 |
|
|
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName; |
142 |
|
|
ReadOnlyTransaction; |
143 |
|
|
FIsChild := Owner.TestOption <> ''; |
144 |
|
|
if FIsChild then |
145 |
|
|
begin |
146 |
|
|
{$IFDEF UNIX} |
147 |
|
|
SetupSignalHandler; |
148 |
|
|
{$ENDIF} |
149 |
|
|
FIBSQLMonitor.Enabled := true; |
150 |
|
|
assignFile(FLogFile,Owner.TestOption); |
151 |
|
|
Rewrite(FLogFile); |
152 |
|
|
end |
153 |
|
|
else |
154 |
|
|
begin |
155 |
|
|
FProcess.Executable := ParamStr(0); |
156 |
|
|
FProcess.Parameters.Clear; |
157 |
|
|
FProcess.Parameters.Add('-q'); |
158 |
|
|
FProcess.Parameters.Add('-t'); |
159 |
|
|
FProcess.Parameters.Add(GetTestID); |
160 |
|
|
FProcess.Parameters.Add('-O'); |
161 |
|
|
FProcess.Parameters.Add(LogFileName); |
162 |
|
|
FProcess.Options := []; |
163 |
|
|
end; |
164 |
|
|
end; |
165 |
|
|
|
166 |
|
|
procedure TTest14.ProcessResults; |
167 |
|
|
begin |
168 |
|
|
inherited ProcessResults; |
169 |
|
|
if assigned(FLog) then |
170 |
|
|
FreeAndNil(FLog); |
171 |
|
|
if assigned(FProcess) then |
172 |
|
|
FreeAndNil(FProcess); |
173 |
|
|
end; |
174 |
|
|
|
175 |
|
|
destructor TTest14.Destroy; |
176 |
|
|
begin |
177 |
|
|
if assigned(FLog) then |
178 |
|
|
FreeAndNil(FLog); |
179 |
|
|
if assigned(FProcess) then |
180 |
|
|
FreeAndNil(FProcess); |
181 |
|
|
inherited Destroy; |
182 |
|
|
end; |
183 |
|
|
|
184 |
|
|
function TTest14.ChildProcess: boolean; |
185 |
|
|
begin |
186 |
|
|
Result := Owner.TestOption <> ''; |
187 |
|
|
end; |
188 |
|
|
|
189 |
|
|
procedure TTest14.RunTest(CharSet: AnsiString; SQLDialect: integer); |
190 |
|
|
var stats: TPerfCounters; |
191 |
|
|
i: integer; |
192 |
|
|
aLogFile: Text; |
193 |
|
|
Line: string; |
194 |
|
|
begin |
195 |
|
|
writeln(OutFile); |
196 |
|
|
EnableMonitoring; |
197 |
|
|
if not FIsChild then |
198 |
|
|
FProcess.Execute |
199 |
|
|
else |
200 |
|
|
begin |
201 |
|
|
while not FTerminated do |
202 |
|
|
CheckSynchronize(1); //loop until terminated |
203 |
|
|
Close(FLogFile); |
204 |
|
|
Exit; |
205 |
|
|
end; |
206 |
|
|
|
207 |
|
|
CheckSynchronize(1); |
208 |
|
|
with IBQuery do |
209 |
|
|
begin |
210 |
|
|
AllowAutoActivateTransaction := true; |
211 |
|
|
Unidirectional := true; |
212 |
|
|
SQL.Text := sqlExample; |
213 |
|
|
EnableStatistics := true; |
214 |
|
|
Active := true; |
215 |
|
|
PrintDataSet(IBQuery); |
216 |
|
|
|
217 |
|
|
if GetPerfStatistics(stats) then |
218 |
|
|
WritePerfStats(stats); |
219 |
|
|
PrintAffectedRows(IBQuery); |
220 |
|
|
writeln(OutFile); |
221 |
|
|
writeln(OutFile,'Reconnect'); |
222 |
|
|
IBDatabase.ReConnect; |
223 |
|
|
Unidirectional := false; |
224 |
|
|
Active := true; |
225 |
|
|
PrintDataSet(IBQuery); |
226 |
|
|
end; |
227 |
|
|
IBDatabase.Connected := false; |
228 |
|
|
CheckSynchronize(1); |
229 |
|
|
DisableMonitoring; |
230 |
|
|
FProcess.Terminate(0); |
231 |
|
|
Sleep(1000); |
232 |
|
|
assignFile(aLogFile,LogFileName); |
233 |
|
|
Reset(aLogFile); |
234 |
|
|
while not EOF(aLogFile) do |
235 |
|
|
begin |
236 |
|
|
readln(aLogFile,Line); |
237 |
|
|
writeln(OutFile,Line); |
238 |
|
|
end; |
239 |
|
|
end; |
240 |
|
|
|
241 |
|
|
initialization |
242 |
|
|
RegisterTest(TTest14); |
243 |
|
|
|
244 |
|
|
end. |
245 |
|
|
|