1 |
tony |
45 |
(* |
2 |
|
|
* Firebird Interface (fbintf). The fbintf components provide a set of |
3 |
|
|
* Pascal language bindings for the Firebird API. Although predominantly |
4 |
|
|
* a new development they include source code taken from IBX and may be |
5 |
|
|
* considered a derived product. This software thus also includes the copyright |
6 |
|
|
* notice and license conditions from IBX. |
7 |
|
|
* |
8 |
|
|
* Except for those parts dervied from IBX, contents of this file are subject |
9 |
|
|
* to the Initial Developer's Public License Version 1.0 (the "License"); you |
10 |
|
|
* may not use this file except in compliance with the License. You may obtain a |
11 |
|
|
* copy of the License here: |
12 |
|
|
* |
13 |
|
|
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
14 |
|
|
* |
15 |
|
|
* Software distributed under the License is distributed on an "AS |
16 |
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
17 |
|
|
* implied. See the License for the specific language governing rights |
18 |
|
|
* and limitations under the License. |
19 |
|
|
* |
20 |
|
|
* The Initial Developer of the Original Code is Tony Whyman. |
21 |
|
|
* |
22 |
|
|
* The Original Code is (C) 2016 Tony Whyman, MWA Software |
23 |
|
|
* (http://www.mwasoftware.co.uk). |
24 |
|
|
* |
25 |
|
|
* All Rights Reserved. |
26 |
|
|
* |
27 |
|
|
* Contributor(s): ______________________________________. |
28 |
|
|
* |
29 |
|
|
*) |
30 |
|
|
unit FBServices; |
31 |
tony |
56 |
{$IFDEF MSWINDOWS} |
32 |
|
|
{$DEFINE WINDOWS} |
33 |
|
|
{$ENDIF} |
34 |
tony |
45 |
|
35 |
|
|
{$IFDEF FPC} |
36 |
tony |
56 |
{$mode delphi} |
37 |
tony |
45 |
{$interfaces COM} |
38 |
|
|
{$ENDIF} |
39 |
|
|
|
40 |
|
|
interface |
41 |
|
|
|
42 |
|
|
uses |
43 |
tony |
263 |
Classes, SysUtils, IB, FBParamBlock, FBActivityMonitor, FBClientAPI; |
44 |
tony |
45 |
|
45 |
|
|
type |
46 |
|
|
|
47 |
|
|
{ TFBServiceManager } |
48 |
|
|
|
49 |
|
|
TFBServiceManager = class(TFBInterfacedObject) |
50 |
|
|
private |
51 |
|
|
FFirebirdAPI: IFirebirdAPI; |
52 |
|
|
FProtocol: TProtocol; |
53 |
tony |
56 |
FServerName: AnsiString; |
54 |
tony |
143 |
FPort: AnsiString; |
55 |
tony |
45 |
procedure CheckServerName; |
56 |
|
|
protected |
57 |
|
|
FSPB: ISPB; |
58 |
tony |
56 |
procedure InternalAttach(ConnectString: AnsiString); virtual; abstract; |
59 |
tony |
421 |
procedure IBDatabaseError; |
60 |
tony |
45 |
public |
61 |
tony |
263 |
constructor Create(api: TFBClientAPI; ServerName: AnsiString; Protocol: TProtocol; SPB: ISPB; Port: AnsiString = ''); |
62 |
tony |
45 |
destructor Destroy; override; |
63 |
|
|
public |
64 |
|
|
{IServiceManager} |
65 |
tony |
263 |
function getFirebirdAPI: IFirebirdAPI; |
66 |
tony |
45 |
function getSPB: ISPB; |
67 |
tony |
56 |
function getServerName: AnsiString; |
68 |
tony |
209 |
function getProtocol: TProtocol; |
69 |
|
|
function getPortNo: AnsiString; |
70 |
tony |
45 |
procedure Attach; |
71 |
|
|
procedure Detach(Force: boolean=false); virtual; abstract; |
72 |
|
|
function AllocateSRB: ISRB; |
73 |
|
|
function AllocateSQPB: ISQPB; |
74 |
tony |
209 |
function Query(SQPB: ISQPB; Request: ISRB; RaiseExceptionOnError: boolean=true): IServiceQueryResults; overload; virtual; abstract; |
75 |
|
|
function Query(Request: ISRB; RaiseExceptionOnError: boolean=true): IServiceQueryResults; overload; |
76 |
tony |
45 |
end; |
77 |
|
|
|
78 |
tony |
315 |
{ TSPBItem } |
79 |
|
|
|
80 |
|
|
TSPBItem = class(TParamBlockItem,ISPBItem) |
81 |
|
|
public |
82 |
|
|
function getParamTypeName: AnsiString; override; |
83 |
|
|
class function LookupParamTypeName(ParamType: byte): AnsiString; |
84 |
|
|
end; |
85 |
|
|
|
86 |
|
|
{ TSPB } |
87 |
|
|
|
88 |
|
|
TSPB = class (TCustomParamBlock<TSPBItem,ISPBItem>, ISPB) |
89 |
|
|
protected |
90 |
|
|
function LookupItemType(ParamTypeName: AnsiString): byte; override; |
91 |
|
|
public |
92 |
|
|
constructor Create(api: TFBClientAPI); |
93 |
tony |
345 |
function GetParamTypeName(ParamType: byte): Ansistring; |
94 |
|
|
function ISPB.GetDPBParamTypeName = GetParamTypeName; |
95 |
tony |
315 |
end; |
96 |
|
|
|
97 |
tony |
45 |
implementation |
98 |
|
|
|
99 |
tony |
263 |
uses FBMessages, IBUtils; |
100 |
tony |
45 |
|
101 |
tony |
315 |
const |
102 |
|
|
SPBPrefix = 'isc_spb_'; |
103 |
|
|
isc_spb_last_spb_constant = 13; |
104 |
|
|
SPBConstantNames: array[1..isc_spb_last_spb_constant] of String = ( |
105 |
|
|
'user_name', |
106 |
|
|
'sys_user_name', |
107 |
|
|
'sys_user_name_enc', |
108 |
|
|
'password', |
109 |
|
|
'password_enc', |
110 |
|
|
'command_line', |
111 |
|
|
'db_name', |
112 |
|
|
'verbose', |
113 |
|
|
'options', |
114 |
|
|
'connect_timeout', |
115 |
|
|
'dummy_packet_interval', |
116 |
|
|
'sql_role_name', |
117 |
|
|
'expected_db' |
118 |
|
|
); |
119 |
|
|
|
120 |
|
|
SPBConstantValues: array[1..isc_spb_last_spb_constant] of Integer = ( |
121 |
|
|
isc_spb_user_name, |
122 |
|
|
isc_spb_sys_user_name, |
123 |
|
|
isc_spb_sys_user_name_enc, |
124 |
|
|
isc_spb_password, |
125 |
|
|
isc_spb_password_enc, |
126 |
|
|
isc_spb_command_line, |
127 |
|
|
isc_spb_dbname, |
128 |
|
|
isc_spb_verbose, |
129 |
|
|
isc_spb_options, |
130 |
|
|
isc_spb_connect_timeout, |
131 |
|
|
isc_spb_dummy_packet_interval, |
132 |
|
|
isc_spb_sql_role_name, |
133 |
|
|
isc_spb_expected_db |
134 |
|
|
); |
135 |
|
|
|
136 |
|
|
{ TSPBItem } |
137 |
|
|
|
138 |
|
|
function TSPBItem.getParamTypeName: AnsiString; |
139 |
|
|
begin |
140 |
|
|
Result := LookupParamTypeName(getParamType); |
141 |
|
|
end; |
142 |
|
|
|
143 |
|
|
class function TSPBItem.LookupParamTypeName(ParamType: byte): AnsiString; |
144 |
|
|
var i: integer; |
145 |
|
|
begin |
146 |
|
|
Result := ''; |
147 |
|
|
for i := 1 to isc_spb_last_spb_constant do |
148 |
|
|
if SPBConstantValues [i] = ParamType then |
149 |
|
|
begin |
150 |
|
|
Result := SPBConstantNames[i]; |
151 |
|
|
break; |
152 |
|
|
end; |
153 |
|
|
end; |
154 |
|
|
|
155 |
tony |
45 |
{ TFBServiceManager } |
156 |
|
|
|
157 |
|
|
procedure TFBServiceManager.CheckServerName; |
158 |
|
|
begin |
159 |
|
|
if (FServerName = '') and (FProtocol <> Local) then |
160 |
|
|
IBError(ibxeServerNameMissing, [nil]); |
161 |
|
|
end; |
162 |
|
|
|
163 |
tony |
421 |
procedure TFBServiceManager.IBDatabaseError; |
164 |
|
|
begin |
165 |
|
|
raise EIBInterBaseError.Create(FFirebirdAPI.GetStatus,cp_utf8); |
166 |
|
|
end; |
167 |
|
|
|
168 |
tony |
263 |
constructor TFBServiceManager.Create(api: TFBClientAPI; ServerName: AnsiString; |
169 |
tony |
143 |
Protocol: TProtocol; SPB: ISPB; Port: AnsiString); |
170 |
tony |
45 |
begin |
171 |
|
|
inherited Create; |
172 |
tony |
263 |
FFirebirdAPI := api.getAPI; {Keep reference to interface} |
173 |
tony |
45 |
FProtocol := Protocol; |
174 |
|
|
FSPB := SPB; |
175 |
|
|
FServerName := ServerName; |
176 |
tony |
143 |
FPort := Port; |
177 |
tony |
45 |
Attach; |
178 |
|
|
end; |
179 |
|
|
|
180 |
|
|
destructor TFBServiceManager.Destroy; |
181 |
|
|
begin |
182 |
|
|
Detach(true); |
183 |
|
|
inherited Destroy; |
184 |
|
|
end; |
185 |
|
|
|
186 |
tony |
263 |
function TFBServiceManager.getFirebirdAPI: IFirebirdAPI; |
187 |
|
|
begin |
188 |
|
|
Result := FFirebirdAPI; |
189 |
|
|
end; |
190 |
|
|
|
191 |
tony |
45 |
function TFBServiceManager.getSPB: ISPB; |
192 |
|
|
begin |
193 |
|
|
Result := FSPB; |
194 |
|
|
end; |
195 |
|
|
|
196 |
tony |
56 |
function TFBServiceManager.getServerName: AnsiString; |
197 |
tony |
45 |
begin |
198 |
|
|
Result := FServerName; |
199 |
|
|
end; |
200 |
|
|
|
201 |
tony |
209 |
function TFBServiceManager.getProtocol: TProtocol; |
202 |
|
|
begin |
203 |
|
|
Result := FProtocol; |
204 |
|
|
end; |
205 |
|
|
|
206 |
|
|
function TFBServiceManager.getPortNo: AnsiString; |
207 |
|
|
begin |
208 |
|
|
Result := FPort; |
209 |
|
|
end; |
210 |
|
|
|
211 |
tony |
45 |
procedure TFBServiceManager.Attach; |
212 |
tony |
56 |
var ConnectString: AnsiString; |
213 |
tony |
45 |
begin |
214 |
tony |
143 |
ConnectString := MakeConnectString(FServerName,'service_mgr',FProtocol,FPort); |
215 |
tony |
45 |
InternalAttach(ConnectString); |
216 |
|
|
end; |
217 |
|
|
|
218 |
|
|
function TFBServiceManager.AllocateSRB: ISRB; |
219 |
|
|
begin |
220 |
tony |
263 |
Result := TSRB.Create(FFirebirdAPI as TFBClientAPI); |
221 |
tony |
45 |
end; |
222 |
|
|
|
223 |
|
|
function TFBServiceManager.AllocateSQPB: ISQPB; |
224 |
|
|
begin |
225 |
tony |
263 |
Result := TSQPB.Create(FFirebirdAPI as TFBClientAPI); |
226 |
tony |
45 |
end; |
227 |
|
|
|
228 |
tony |
209 |
function TFBServiceManager.Query(Request: ISRB; RaiseExceptionOnError: boolean |
229 |
|
|
): IServiceQueryResults; |
230 |
tony |
45 |
begin |
231 |
tony |
209 |
Result := Query(nil,Request,RaiseExceptionOnError); |
232 |
tony |
45 |
end; |
233 |
|
|
|
234 |
tony |
315 |
{ TSPB } |
235 |
|
|
|
236 |
|
|
function TSPB.LookupItemType(ParamTypeName: AnsiString): byte; |
237 |
|
|
var i: integer; |
238 |
|
|
begin |
239 |
|
|
Result := 0; |
240 |
|
|
ParamTypeName := LowerCase(ParamTypeName); |
241 |
|
|
if Pos(SPBPrefix,ParamTypeName) = 1 then |
242 |
|
|
system.Delete(ParamTypeName,1,Length(SPBPrefix)); |
243 |
|
|
for i := 1 to isc_spb_last_spb_constant do |
244 |
|
|
if SPBConstantNames [i] = ParamTypeName then |
245 |
|
|
begin |
246 |
|
|
Result := SPBConstantValues[i]; |
247 |
|
|
break; |
248 |
|
|
end; |
249 |
|
|
end; |
250 |
|
|
|
251 |
|
|
constructor TSPB.Create(api: TFBClientAPI); |
252 |
|
|
begin |
253 |
|
|
inherited Create(api); |
254 |
|
|
FDataLength := 2; |
255 |
|
|
FBuffer^ := isc_spb_version; |
256 |
|
|
(FBuffer+1)^ := isc_spb_current_version; |
257 |
|
|
end; |
258 |
|
|
|
259 |
tony |
345 |
function TSPB.GetParamTypeName(ParamType: byte): Ansistring; |
260 |
tony |
315 |
begin |
261 |
|
|
Result := TSPBItem.LookupParamTypeName(ParamType); |
262 |
|
|
end; |
263 |
|
|
|
264 |
tony |
45 |
end. |
265 |
|
|
|