ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/testsuite/Test11.pas
Revision: 56
Committed: Mon Mar 6 10:20:02 2017 UTC (7 years, 9 months ago) by tony
Content type: text/x-pascal
File size: 8137 byte(s)
Log Message:
Committing updates for Trunk

File Contents

# User Rev Content
1 tony 45 unit Test11;
2 tony 56 {$IFDEF MSWINDOWS}
3     {$DEFINE WINDOWS}
4     {$ENDIF}
5 tony 45
6 tony 56 {$IFDEF FPC}
7     {$mode delphi}
8 tony 45 {$codepage utf8}
9 tony 56 {$ENDIF}
10 tony 45
11     {Test 11: Services API}
12    
13     {
14     This test attaches to the Server Manager associated with the Employee Database
15     and then accesses and prints out:
16    
17     Version Info
18     Config Params
19     Database Information
20     User Information
21     Statistics
22     Licence Info
23     Licence Mask Info
24     Capabilities
25     limbo transactions
26    
27     Note that two exceptions are normal as Firebird does not provide licence info.
28     }
29    
30     interface
31    
32     uses
33     Classes, SysUtils, TestManager, IB;
34    
35     type
36    
37     { TTest11 }
38    
39     TTest11 = class(TTestBase)
40     private
41 tony 56 procedure GetStatistics(Service: IServiceManager; DBName: AnsiString);
42     procedure BackupRestore(Service: IServiceManager; DBName: AnsiString);
43 tony 45 public
44 tony 56 function TestTitle: AnsiString; override;
45     procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
46 tony 45 end;
47    
48     implementation
49    
50     { TTest11 }
51    
52 tony 56 procedure TTest11.GetStatistics(Service: IServiceManager; DBName: AnsiString);
53 tony 45 var Req: ISRB;
54     Results: IServiceQueryResults;
55     begin
56     {Version Info}
57     Req := Service.AllocateSRB;
58     Req.Add(isc_info_svc_version);
59     Req.Add(isc_info_svc_server_version);
60     Req.Add(isc_info_svc_implementation);
61     Results := Service.Query(nil,Req);
62     WriteServiceQueryResult(Results);
63    
64     {Config Params}
65     Req := Service.AllocateSRB;
66     Req.Add(isc_info_svc_get_env_lock);
67     Req.Add(isc_info_svc_get_config);
68     Req.Add(isc_info_svc_get_env_msg);
69     Req.Add(isc_info_svc_user_dbpath);
70     Results := Service.Query(nil,Req);
71     WriteServiceQueryResult(Results);
72    
73     {Database Information}
74     Req := Service.AllocateSRB;
75     Req.Add(isc_info_svc_svr_db_info);
76     Results := Service.Query(nil,Req);
77     WriteServiceQueryResult(Results);
78    
79     {User Information}
80     Req := Service.AllocateSRB;
81     Req.Add(isc_action_svc_display_user);
82     Service.Start(Req);
83     Req := Service.AllocateSRB;
84     Req.Add(isc_info_svc_get_users);
85     Results := Service.Query(nil,Req);
86     WriteServiceQueryResult(Results);
87    
88     Service.Detach;
89     Service.Attach;
90    
91     {Statistics}
92    
93     if Owner.ShowStatistics then
94     begin
95     writeln(OutFile,'Get Statistics');
96     writeln(OutFile);
97     Req := Service.AllocateSRB;
98     Req.Add(isc_action_svc_db_stats);
99     Req.Add(isc_spb_dbname).SetAsString(DBName);
100     Req.Add(isc_spb_options).SetAsInteger(isc_spb_sts_data_pages or {
101     isc_spb_sts_hdr_pages or} isc_spb_sts_idx_pages or
102     isc_spb_sts_sys_relations);
103    
104     try
105     Service.Start(Req);
106     Req := Service.AllocateSRB;
107     Req.Add(isc_info_svc_line);
108     repeat
109     Results := Service.Query(nil,Req);
110     until not WriteServiceQueryResult(Results);
111     except on E: Exception do
112     writeln(OutFile,'Statistics Service Start: ',E.Message);
113     end;
114     writeln(OutFile);
115     end;
116    
117     {Licence Info}
118     Req := Service.AllocateSRB;
119     Req.Add(isc_info_svc_get_license);
120     Req.Add(isc_info_svc_get_licensed_users);
121     try
122     Results := Service.Query(nil,Req);
123     WriteServiceQueryResult(Results);
124     except on E: Exception do
125     writeln(OutFile,'Licence Info: ',E.Message);
126     end;
127     writeln(OutFile);
128    
129     {Licence Mask Info}
130     Req := Service.AllocateSRB;
131     Req.Add(isc_info_svc_get_license_mask);
132     try
133     Results := Service.Query(nil,Req);
134     WriteServiceQueryResult(Results);
135     except on E: Exception do
136     writeln(OutFile,'Licence Mask Info: ',E.Message);
137     end;
138     writeln(OutFile);
139    
140     {Capabilities}
141     Req := Service.AllocateSRB;
142     Req.Add(isc_info_svc_capabilities);
143     try
144     Results := Service.Query(nil,Req);
145     WriteServiceQueryResult(Results);
146     except on E: Exception do
147     writeln(OutFile,'Capabilities: ',E.Message);
148     end;
149     writeln(OutFile);
150    
151     {limbo transactions}
152    
153     writeln(OutFile,'Get Limbo transactions');
154     writeln(OutFile);
155     Req := Service.AllocateSRB;
156     Req.Add(isc_info_svc_limbo_trans);
157     try
158     Results := Service.Query(nil,Req);
159     WriteServiceQueryResult(Results);
160     except on E: Exception do
161     writeln(OutFile,'limbo transactions: ',E.Message);
162     end;
163     writeln(OutFile);
164     end;
165    
166 tony 56 procedure TTest11.BackupRestore(Service: IServiceManager; DBName: AnsiString);
167 tony 45 var Req: ISRB;
168     Results: IServiceQueryResults;
169     BakFile: TFileStream;
170     QueryResultsItem: IServiceQueryResultItem;
171     ReqLength: integer;
172     SQPB: ISQPB;
173     bytesWritten: integer;
174     bytesAvailable: integer;
175     i: integer;
176 tony 56 RestoreDBName: AnsiString;
177 tony 45 Attachment: IAttachment;
178     DPB: IDPB;
179     begin
180     {Local Backup}
181    
182     writeln(OutFile,'Local Backup');
183     Req := Service.AllocateSRB;
184     Req.Add(isc_action_svc_backup);
185     Req.Add(isc_spb_dbname).AsString := DBName;
186     Req.Add(isc_spb_bkp_file).AsString := 'stdout';
187     try
188     BakFile := TFileStream.Create(Owner.GetBackupFileName,fmCreate);
189     try
190     Service.Start(Req);
191     Req := Service.AllocateSRB;
192     Req.Add(isc_info_svc_to_eof);
193     repeat
194     bytesWritten := 0;
195     Results := Service.Query(Req);
196     QueryResultsItem := Results.Find(isc_info_svc_to_eof);
197     if QueryResultsItem <> nil then
198     bytesWritten := QueryResultsItem.CopyTo(BakFile,0);
199     until (bytesWritten = 0) or not WriteServiceQueryResult(Results);
200     writeln(OutFile,'Local Backup Complete');
201     finally
202     BakFile.Free;
203     end;
204     except on E: Exception do
205     writeln(OutFile,'Local Backup Service: ',E.Message);
206     end;
207     writeln(OutFile);
208    
209     {Local Restore}
210     writeln(OutFile,'Local Restore');
211     RestoreDBName := Owner.GetNewDatabaseName;
212     i := Pos(':',RestoreDBName);
213     if i > 0 then
214     system.Delete(RestoreDBName,1,i);
215     Req := Service.AllocateSRB;
216     Req.Add(isc_action_svc_restore);
217     Req.Add(isc_spb_dbname).AsString := RestoreDBName;
218     Req.Add(isc_spb_verbose);
219     Req.Add(isc_spb_bkp_file).AsString := 'stdin';
220     Req.Add(isc_spb_res_access_mode).AsByte := isc_spb_prp_am_readwrite;
221     Req.Add(isc_spb_options).SetAsInteger(isc_spb_res_create);
222     BakFile := TFileStream.Create(Owner.GetBackupFileName,fmOpenRead);
223     try
224     bytesAvailable := BakFile.Size;
225     try
226     Service.Start(Req);
227     ReqLength := 0;
228     repeat
229     SQPB := Service.AllocateSQPB;
230     if ReqLength > 0 then
231     bytesWritten := SQPB.Add(isc_info_svc_line).CopyFrom(BakFile,ReqLength);
232 tony 56 bytesAvailable := bytesAvailable - bytesWritten;
233 tony 45 Req := Service.AllocateSRB;
234     Req.Add(isc_info_svc_stdin);
235     Req.Add(isc_info_svc_line);
236     Results := Service.Query(SQPB,Req);
237     QueryResultsItem := Results.Find(isc_info_svc_stdin);
238     if QueryResultsItem <> nil then
239     ReqLength := QueryResultsItem.AsInteger;
240     WriteServiceQueryResult(Results);
241     until (ReqLength = 0) ;
242     writeln(OutFile,'Local Restore Complete');
243     except on E: Exception do
244     writeln(OutFile,'Local Restore Service: ',E.Message);
245     end;
246     finally
247     BakFile.free;
248     end;
249     writeln(OutFile);
250     writeln(OutFile,'Open Database Check');
251     DPB := FirebirdAPI.AllocateDPB;
252     DPB.Add(isc_dpb_user_name).AsString := Owner.GetUserName;
253     DPB.Add(isc_dpb_password).AsString := Owner.GetPassword;
254     Attachment := FirebirdAPI.OpenDatabase(Owner.GetNewDatabaseName,DPB);
255     if Attachment <> nil then
256     writeln(OutFile,'Database OK');
257     Attachment.DropDatabase;
258     writeln(OutFile,'Database Dropped');
259     end;
260    
261 tony 56 function TTest11.TestTitle: AnsiString;
262 tony 45 begin
263     Result := 'Test 11: Services API';
264     end;
265    
266 tony 56 procedure TTest11.RunTest(CharSet: AnsiString; SQLDialect: integer);
267 tony 45 var SPB: ISPB;
268     Service: IServiceManager;
269     I: integer;
270 tony 56 ServerName: AnsiString;
271     DBName: AnsiString;
272 tony 45 begin
273     if not FirebirdAPI.HasServiceAPI then Exit;
274    
275     ServerName := Owner.GetEmployeeDatabaseName;
276     I := Pos(':',ServerName);
277     if i > 0 then
278     DBName := system.copy(ServerName,i+1,length(ServerName) - 2);
279     system.Delete(ServerName,i,Length(ServerName)-i+1);
280    
281     SPB := FirebirdAPI.AllocateSPB;
282     SPB.Add(isc_spb_user_name).setAsString(Owner.GetUserName);
283     SPB.Add(isc_spb_password).setAsString(Owner.GetPassword);
284     Service := FirebirdAPI.GetServiceManager(ServerName,TCP,SPB);
285    
286     GetStatistics(Service,DBName);
287     BackupRestore(Service,DBName);
288    
289     end;
290    
291     initialization
292     RegisterTest(TTest11);
293    
294     end.
295