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