ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/testsuite/Test14.pas
Revision: 319
Committed: Thu Feb 25 12:05:40 2021 UTC (3 years, 6 months ago) by tony
Content type: text/x-pascal
File size: 6630 byte(s)
Log Message:
Merge into public release

File Contents

# User Rev Content
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 tony 319 procedure ShowStatistics(Sender: TObject);
45 tony 315 protected
46     procedure CreateObjects(Application: TTestApplication); override;
47     function GetTestID: AnsiString; override;
48     function GetTestTitle: AnsiString; override;
49     procedure InitTest; override;
50     procedure ProcessResults; override;
51     public
52     destructor Destroy; override;
53     function ChildProcess: boolean; override;
54     procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
55     end;
56    
57    
58     implementation
59    
60     const
61     sqlExample =
62     'with recursive Depts As ( '+
63     'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+
64     'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+
65     'From DEPARTMENT Where HEAD_DEPT is NULL '+
66     'UNION ALL '+
67     'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+
68     'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+
69     'From DEPARTMENT D '+
70     'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+
71     ')'+
72    
73     'Select First 2 A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+
74     'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+
75     'From EMPLOYEE A '+
76     'JOIN Depts D On D.DEPT_NO = A.DEPT_NO';
77    
78     LogFileName = 'Test_ISQLMonitor' + '.log';
79    
80     { TTest14 }
81    
82     procedure TTest14.HandleOnSQL(EventText: String; EventTime: TDateTime);
83     begin
84     if FIsChild then
85     begin
86 tony 319 writeln(FLogFile,'*Monitor* '{+DateTimeToStr(EventTime)}+' '+EventText);
87 tony 315 Flush(FLogFile);
88     end;
89     end;
90    
91 tony 319 procedure TTest14.ShowStatistics(Sender: TObject);
92     begin
93     if ChildProcess then
94     begin
95     writeln(FLogFile,FIBSQLMonitor.ReadCount,' ISQL Monitor Messages Received');
96     Flush(FLogFile);
97     Close(FLogFile);
98     end;
99     end;
100    
101 tony 315 {$IFDEF UNIX}
102     procedure DoSigTerm(sig : cint);cdecl;
103     begin
104     TTest14.FTerminated := true;
105     end;
106    
107     procedure TTest14.SetupSignalHandler;
108     begin
109     FTerminated := false;
110     new(Fna);
111     new(Foa);
112     Fna^.sa_Handler:=SigActionHandler(@DoSigTerm);
113     fillchar(Fna^.Sa_Mask,sizeof(Fna^.sa_mask),#0);
114     Fna^.Sa_Flags:=0;
115     {$ifdef Linux} // Linux specific
116     Fna^.Sa_Restorer:=Nil;
117     {$endif}
118     if fpSigAction(SigTerm,Fna,Foa)<>0 then
119     begin
120 tony 319 writeln('Error setting signal handler: ',fpgeterrno,'.');
121 tony 315 halt(1);
122     end;
123     end;
124     {$ENDIF}
125    
126     procedure TTest14.CreateObjects(Application: TTestApplication);
127     begin
128     inherited CreateObjects(Application);
129 tony 319 if ChildProcess then
130     begin
131     FIBSQLMonitor := TIBSQLMonitor.Create(Application);
132     FIBSQLMonitor.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect,
133     tfTransact, tfBlob, tfService, tfMisc];
134     FIBSQLMonitor.OnSQL := @HandleOnSQL;
135     FIBSQLMonitor.OnMonitoringDisabled := @ShowStatistics;
136     end
137     else
138     begin
139     IBDatabase.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect,
140     tfTransact, tfBlob, tfService, tfMisc];
141     FLog := TStringList.Create;
142     FProcess := TProcess.Create(Application);
143     end;
144 tony 315 end;
145    
146     function TTest14.GetTestID: AnsiString;
147     begin
148     Result := aTestID;
149     end;
150    
151     function TTest14.GetTestTitle: AnsiString;
152     begin
153     Result := aTestTitle;
154     end;
155    
156     procedure TTest14.InitTest;
157     begin
158     inherited InitTest;
159     IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName;
160     ReadOnlyTransaction;
161     FIsChild := Owner.TestOption <> '';
162     if FIsChild then
163     begin
164     {$IFDEF UNIX}
165     SetupSignalHandler;
166     {$ENDIF}
167     FIBSQLMonitor.Enabled := true;
168     assignFile(FLogFile,Owner.TestOption);
169     Rewrite(FLogFile);
170     end
171     else
172     begin
173     FProcess.Executable := ParamStr(0);
174     FProcess.Parameters.Clear;
175     FProcess.Parameters.Add('-q');
176     FProcess.Parameters.Add('-t');
177     FProcess.Parameters.Add(GetTestID);
178     FProcess.Parameters.Add('-O');
179     FProcess.Parameters.Add(LogFileName);
180     FProcess.Options := [];
181     end;
182     end;
183    
184     procedure TTest14.ProcessResults;
185     begin
186     inherited ProcessResults;
187     if assigned(FLog) then
188     FreeAndNil(FLog);
189     if assigned(FProcess) then
190     FreeAndNil(FProcess);
191     end;
192    
193     destructor TTest14.Destroy;
194     begin
195     if assigned(FLog) then
196     FreeAndNil(FLog);
197     if assigned(FProcess) then
198     FreeAndNil(FProcess);
199     inherited Destroy;
200     end;
201    
202     function TTest14.ChildProcess: boolean;
203     begin
204     Result := Owner.TestOption <> '';
205     end;
206    
207     procedure TTest14.RunTest(CharSet: AnsiString; SQLDialect: integer);
208     var stats: TPerfCounters;
209     i: integer;
210     aLogFile: Text;
211     Line: string;
212     begin
213     writeln(OutFile);
214     EnableMonitoring;
215     if not FIsChild then
216     FProcess.Execute
217     else
218     begin
219     while not FTerminated do
220     CheckSynchronize(1); //loop until terminated
221     Exit;
222     end;
223    
224 tony 319 Sleep(1000); {wait for child to become ready}
225 tony 315 CheckSynchronize(1);
226     with IBQuery do
227     begin
228     AllowAutoActivateTransaction := true;
229     Unidirectional := true;
230     SQL.Text := sqlExample;
231     EnableStatistics := true;
232     Active := true;
233     PrintDataSet(IBQuery);
234    
235     if GetPerfStatistics(stats) then
236     WritePerfStats(stats);
237     PrintAffectedRows(IBQuery);
238     writeln(OutFile);
239     writeln(OutFile,'Reconnect');
240     IBDatabase.ReConnect;
241     Unidirectional := false;
242     Active := true;
243     PrintDataSet(IBQuery);
244     end;
245     IBDatabase.Connected := false;
246 tony 319 Sleep(1000);
247 tony 315 CheckSynchronize(1);
248 tony 319 Sleep(1000);
249 tony 315 DisableMonitoring;
250 tony 319 writeln(Outfile,MonitorHook.GetWriteCount,' ISQL Monitor Messages written');
251     Sleep(1000);
252 tony 315 FProcess.Terminate(0);
253     Sleep(1000);
254     assignFile(aLogFile,LogFileName);
255     Reset(aLogFile);
256     while not EOF(aLogFile) do
257     begin
258     readln(aLogFile,Line);
259     writeln(OutFile,Line);
260     end;
261     end;
262    
263     initialization
264     RegisterTest(TTest14);
265    
266     end.
267