ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/client/FBAttachment.pas
Revision: 270
Committed: Fri Jan 18 11:10:37 2019 UTC (5 years, 10 months ago) by tony
Content type: text/x-pascal
File size: 32807 byte(s)
Log Message:
Fixes merged

File Contents

# Content
1 (*
2 * Firebird Interface (fbintf). The fbintf components provide a set of
3 * Pascal language bindings for the Firebird API.
4 *
5 * The contents of this file are subject to the Initial Developer's
6 * Public License Version 1.0 (the "License"); you may not use this
7 * file except in compliance with the License. You may obtain a copy
8 * of the License here:
9 *
10 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
11 *
12 * Software distributed under the License is distributed on an "AS
13 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing rights
15 * and limitations under the License.
16 *
17 * The Initial Developer of the Original Code is Tony Whyman.
18 *
19 * The Original Code is (C) 2016 Tony Whyman, MWA Software
20 * (http://www.mwasoftware.co.uk).
21 *
22 * All Rights Reserved.
23 *
24 * Contributor(s): ______________________________________.
25 *
26 *)
27 unit FBAttachment;
28 {$IFDEF MSWINDOWS}
29 {$DEFINE WINDOWS}
30 {$ENDIF}
31
32 {$IFDEF FPC}
33 {$mode delphi}
34 {$interfaces COM}
35 {$define HASREQEX}
36 {$ENDIF}
37
38 interface
39
40 uses
41 Classes, SysUtils, {$IFDEF WINDOWS} windows, {$ENDIF} IB, FBParamBlock,
42 FBActivityMonitor, FBClientAPI;
43
44 type
45 TCharsetMap = record
46 CharsetID: integer;
47 CharSetName: AnsiString;
48 CharSetWidth: integer;
49 CodePage: TSystemCodePage;
50 AllowReverseLookup: boolean; {used to ensure that lookup of CP_UTF* does not return UNICODE_FSS}
51 end;
52
53 { TFBAttachment }
54
55 TFBAttachment = class(TActivityHandler)
56 private
57 FDPB: IDPB;
58 FFirebirdAPI: IFirebirdAPI;
59 FODSMajorVersion: integer;
60 FODSMinorVersion: integer;
61 FUserCharSetMap: array of TCharSetMap;
62 FSecDatabase: AnsiString;
63 protected
64 FDatabaseName: AnsiString;
65 FRaiseExceptionOnConnectError: boolean;
66 FSQLDialect: integer;
67 FHasDefaultCharSet: boolean;
68 FCharSetID: integer;
69 FCodePage: TSystemCodePage;
70 FRemoteProtocol: AnsiString;
71 FAuthMethod: AnsiString;
72 constructor Create(api: TFBClientAPI; DatabaseName: AnsiString; DPB: IDPB;
73 RaiseExceptionOnConnectError: boolean);
74 procedure CheckHandle; virtual; abstract;
75 function GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
76 procedure GetODSAndConnectionInfo;
77 function GetDBInfo(ReqBuffer: PByte; ReqBufLen: integer): IDBInformation; virtual; abstract;
78 function IsConnected: boolean; virtual; abstract;
79 procedure EndAllTransactions;
80 procedure DPBFromCreateSQL(CreateSQL: AnsiString);
81 procedure SetParameters(SQLParams: ISQLParams; params: array of const);
82 public
83 destructor Destroy; override;
84 function getFirebirdAPI: IFirebirdAPI;
85 function getDPB: IDPB;
86 function AllocateBPB: IBPB;
87 function AllocateDIRB: IDIRB;
88 function StartTransaction(TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract;
89 function StartTransaction(TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract;
90 procedure Disconnect(Force: boolean=false); virtual; abstract;
91 procedure ExecImmediate(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer); overload; virtual; abstract;
92 procedure ExecImmediate(TPB: array of byte; sql: AnsiString; aSQLDialect: integer); overload;
93 procedure ExecImmediate(transaction: ITransaction; sql: AnsiString); overload;
94 procedure ExecImmediate(TPB: array of byte; sql: AnsiString); overload;
95 function ExecuteSQL(TPB: array of byte; sql: AnsiString; SQLDialect: integer; params: array of const): IResults; overload;
96 function ExecuteSQL(transaction: ITransaction; sql: AnsiString; SQLDialect: integer; params: array of const): IResults; overload;
97 function ExecuteSQL(TPB: array of byte; sql: AnsiString; params: array of const): IResults; overload;
98 function ExecuteSQL(transaction: ITransaction; sql: AnsiString; params: array of const): IResults; overload;
99 function OpenCursor(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer): IResultSet; overload;
100 function OpenCursor(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer;
101 params: array of const): IResultSet; overload;
102 function OpenCursor(transaction: ITransaction; sql: AnsiString): IResultSet; overload;
103 function OpenCursor(transaction: ITransaction; sql: AnsiString;
104 params: array of const): IResultSet; overload;
105 function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer): IResultSet; overload;
106 function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer;
107 params: array of const): IResultSet; overload;
108 function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString): IResultSet; overload;
109 function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString;
110 params: array of const): IResultSet; overload;
111 function OpenCursorAtStart(sql: AnsiString): IResultSet; overload;
112 function OpenCursorAtStart(sql: AnsiString;
113 params: array of const): IResultSet; overload;
114 function Prepare(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer): IStatement; overload; virtual; abstract;
115 function Prepare(transaction: ITransaction; sql: AnsiString): IStatement; overload;
116 function PrepareWithNamedParameters(transaction: ITransaction; sql: AnsiString;
117 aSQLDialect: integer; GenerateParamNames: boolean=false;
118 CaseSensitiveParams: boolean = false): IStatement; overload; virtual; abstract;
119 function PrepareWithNamedParameters(transaction: ITransaction; sql: AnsiString;
120 GenerateParamNames: boolean=false;
121 CaseSensitiveParams: boolean = false): IStatement; overload;
122 function GetEventHandler(Events: TStrings): IEvents; overload; virtual; abstract;
123 function GetEventHandler(Event: AnsiString): IEvents; overload;
124
125 function GetSQLDialect: integer;
126 function OpenBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BlobID: TISC_QUAD; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
127 function OpenBlob(transaction: ITransaction; Field: ISQLData; BPB: IBPB=nil): IBlob; overload;
128 property SQLDialect: integer read FSQLDialect;
129 property DPB: IDPB read FDPB;
130 public
131 function GetDBInformation(Requests: array of byte): IDBInformation; overload;
132 function GetDBInformation(Request: byte): IDBInformation; overload;
133 function GetDBInformation(Requests: IDIRB): IDBInformation; overload;
134 function GetConnectString: AnsiString;
135 function GetRemoteProtocol: AnsiString;
136 function GetAuthenticationMethod: AnsiString;
137 function GetSecurityDatabase: AnsiString;
138 function GetODSMajorVersion: integer;
139 function GetODSMinorVersion: integer;
140 {Character Sets}
141 function HasDefaultCharSet: boolean;
142 function GetDefaultCharSetID: integer;
143 function GetCharsetName(CharSetID: integer): AnsiString;
144 function CharSetID2CodePage(CharSetID: integer; var CodePage: TSystemCodePage): boolean;
145 function CodePage2CharSetID(CodePage: TSystemCodePage; var CharSetID: integer): boolean;
146 function CharSetName2CharSetID(CharSetName: AnsiString; var CharSetID: integer): boolean;
147 function CharSetWidth(CharSetID: integer; var Width: integer): boolean;
148 procedure RegisterCharSet(CharSetName: AnsiString; CodePage: TSystemCodePage;
149 AllowReverseLookup:boolean; out CharSetID: integer);
150 property CharSetID: integer read FCharSetID;
151 property CodePage: TSystemCodePage read FCodePage;
152 end;
153
154 implementation
155
156 uses FBMessages, IBUtils, FBTransaction {$IFDEF HASREQEX}, RegExpr{$ENDIF};
157
158 const
159 CharSetMap: array [0..69] of TCharsetMap = (
160 (CharsetID: 0; CharSetName: 'NONE'; CharSetWidth: 1; CodePage: CP_ACP; AllowReverseLookup: true),
161 (CharsetID: 1; CharSetName: 'OCTETS'; CharSetWidth: 1; CodePage: CP_NONE; AllowReverseLookup: true),
162 (CharsetID: 2; CharSetName: 'ASCII'; CharSetWidth: 1; CodePage: CP_ASCII; AllowReverseLookup: true),
163 (CharsetID: 3; CharSetName: 'UNICODE_FSS'; CharSetWidth: 3; CodePage: CP_UTF8; AllowReverseLookup: false),
164 (CharsetID: 4; CharSetName: 'UTF8'; CharSetWidth: 4; CodePage: CP_UTF8; AllowReverseLookup: true),
165 (CharsetID: 5; CharSetName: 'SJIS_0208'; CharSetWidth: 2; CodePage: 20932; AllowReverseLookup: true),
166 (CharsetID: 6; CharSetName: 'EUCJ_0208'; CharSetWidth: 2; CodePage: 20932; AllowReverseLookup: true),
167 (CharsetID: 7; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
168 (CharsetID: 8; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
169 (CharsetID: 9; CharSetName: 'DOS737'; CharSetWidth: 1; CodePage: 737; AllowReverseLookup: true),
170 (CharsetID: 10; CharSetName: 'DOS437'; CharSetWidth: 1; CodePage: 437; AllowReverseLookup: true),
171 (CharsetID: 11; CharSetName: 'DOS850'; CharSetWidth: 1; CodePage: 850; AllowReverseLookup: true),
172 (CharsetID: 12; CharSetName: 'DOS865'; CharSetWidth: 1; CodePage: 865; AllowReverseLookup: true),
173 (CharsetID: 13; CharSetName: 'DOS860'; CharSetWidth: 1; CodePage: 860; AllowReverseLookup: true),
174 (CharsetID: 14; CharSetName: 'DOS863'; CharSetWidth: 1; CodePage: 863; AllowReverseLookup: true),
175 (CharsetID: 15; CharSetName: 'DOS775'; CharSetWidth: 1; CodePage: 775; AllowReverseLookup: true),
176 (CharsetID: 16; CharSetName: 'DOS858'; CharSetWidth: 1; CodePage: 858; AllowReverseLookup: true),
177 (CharsetID: 17; CharSetName: 'DOS862'; CharSetWidth: 1; CodePage: 862; AllowReverseLookup: true),
178 (CharsetID: 18; CharSetName: 'DOS864'; CharSetWidth: 1; CodePage: 864; AllowReverseLookup: true),
179 (CharsetID: 19; CharSetName: 'NEXT'; CharSetWidth: 1; CodePage: CP_NONE; AllowReverseLookup: true),
180 (CharsetID: 20; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
181 (CharsetID: 21; CharSetName: 'ISO8859_1'; CharSetWidth: 1; CodePage: 28591; AllowReverseLookup: true),
182 (CharsetID: 22; CharSetName: 'ISO8859_2'; CharSetWidth: 1; CodePage: 28592; AllowReverseLookup: true),
183 (CharsetID: 23; CharSetName: 'ISO8859_3'; CharSetWidth: 1; CodePage: 28593; AllowReverseLookup: true),
184 (CharsetID: 24; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
185 (CharsetID: 25; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
186 (CharsetID: 26; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
187 (CharsetID: 27; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
188 (CharsetID: 28; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
189 (CharsetID: 29; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
190 (CharsetID: 30; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
191 (CharsetID: 31; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
192 (CharsetID: 32; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
193 (CharsetID: 33; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
194 (CharsetID: 34; CharSetName: 'ISO8859_4'; CharSetWidth: 1; CodePage: 28594; AllowReverseLookup: true),
195 (CharsetID: 35; CharSetName: 'ISO8859_5'; CharSetWidth: 1; CodePage: 28595; AllowReverseLookup: true),
196 (CharsetID: 36; CharSetName: 'ISO8859_6'; CharSetWidth: 1; CodePage: 28596; AllowReverseLookup: true),
197 (CharsetID: 37; CharSetName: 'ISO8859_7'; CharSetWidth: 1; CodePage: 28597; AllowReverseLookup: true),
198 (CharsetID: 38; CharSetName: 'ISO8859_8'; CharSetWidth: 1; CodePage: 28598; AllowReverseLookup: true),
199 (CharsetID: 39; CharSetName: 'ISO8859_9'; CharSetWidth: 1; CodePage: 28599; AllowReverseLookup: true),
200 (CharsetID: 40; CharSetName: 'ISO8859_13'; CharSetWidth: 1; CodePage: 28603; AllowReverseLookup: true),
201 (CharsetID: 41; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
202 (CharsetID: 42; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
203 (CharsetID: 43; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
204 (CharsetID: 44; CharSetName: 'KSC_5601'; CharSetWidth: 2; CodePage: 949; AllowReverseLookup: true),
205 (CharsetID: 45; CharSetName: 'DOS852'; CharSetWidth: 1; CodePage: 852; AllowReverseLookup: true),
206 (CharsetID: 46; CharSetName: 'DOS857'; CharSetWidth: 1; CodePage: 857; AllowReverseLookup: true),
207 (CharsetID: 47; CharSetName: 'DOS861'; CharSetWidth: 1; CodePage: 861; AllowReverseLookup: true),
208 (CharsetID: 48; CharSetName: 'DOS866'; CharSetWidth: 1; CodePage: 866; AllowReverseLookup: true),
209 (CharsetID: 49; CharSetName: 'DOS869'; CharSetWidth: 1; CodePage: 869; AllowReverseLookup: true),
210 (CharsetID: 50; CharSetName: 'CYRL'; CharSetWidth: 1; CodePage: 1251; AllowReverseLookup: true),
211 (CharsetID: 51; CharSetName: 'WIN1250'; CharSetWidth: 1; CodePage: 1250; AllowReverseLookup: true),
212 (CharsetID: 52; CharSetName: 'WIN1251'; CharSetWidth: 1; CodePage: 1251; AllowReverseLookup: true),
213 (CharsetID: 53; CharSetName: 'WIN1252'; CharSetWidth: 1; CodePage: 1252; AllowReverseLookup: true),
214 (CharsetID: 54; CharSetName: 'WIN1253'; CharSetWidth: 1; CodePage: 1253; AllowReverseLookup: true),
215 (CharsetID: 55; CharSetName: 'WIN1254'; CharSetWidth: 1; CodePage: 1254; AllowReverseLookup: true),
216 (CharsetID: 56; CharSetName: 'BIG_5'; CharSetWidth: 2; CodePage: 950; AllowReverseLookup: true),
217 (CharsetID: 57; CharSetName: 'GB_2312'; CharSetWidth: 2; CodePage: 936; AllowReverseLookup: true),
218 (CharsetID: 58; CharSetName: 'WIN1255'; CharSetWidth: 1; CodePage: 1255; AllowReverseLookup: true),
219 (CharsetID: 59; CharSetName: 'WIN1256'; CharSetWidth: 1; CodePage: 1256; AllowReverseLookup: true),
220 (CharsetID: 60; CharSetName: 'WIN1257'; CharSetWidth: 1; CodePage: 1257; AllowReverseLookup: true),
221 (CharsetID: 61; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
222 (CharsetID: 62; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
223 (CharsetID: 63; CharSetName: 'KOI8R'; CharSetWidth: 1; CodePage: 20866; AllowReverseLookup: true),
224 (CharsetID: 64; CharSetName: 'KOI8U'; CharSetWidth: 1; CodePage: 21866; AllowReverseLookup: true),
225 (CharsetID: 65; CharSetName: 'WIN1258'; CharSetWidth: 1; CodePage: 1258; AllowReverseLookup: true),
226 (CharsetID: 66; CharSetName: 'TIS620'; CharSetWidth: 1; CodePage: 874; AllowReverseLookup: true),
227 (CharsetID: 67; CharSetName: 'GBK'; CharSetWidth: 2; CodePage: 936; AllowReverseLookup: true),
228 (CharsetID: 68; CharSetName: 'CP943C'; CharSetWidth: 2; CodePage: 943; AllowReverseLookup: true),
229 (CharsetID: 69; CharSetName: 'GB18030'; CharSetWidth: 4; CodePage: 54936; AllowReverseLookup: true)
230 );
231
232
233
234
235 { TFBAttachment }
236
237 procedure TFBAttachment.GetODSAndConnectionInfo;
238 var DBInfo: IDBInformation;
239 i: integer;
240 Stmt: IStatement;
241 ResultSet: IResultSet;
242 Param: IDPBItem;
243 begin
244 if not IsConnected then Exit;
245 DBInfo := GetDBInformation([isc_info_db_id,isc_info_ods_version,isc_info_ods_minor_version,
246 isc_info_db_SQL_Dialect]);
247 for i := 0 to DBInfo.GetCount - 1 do
248 with DBInfo[i] do
249 case getItemType of
250 isc_info_ods_minor_version:
251 FODSMinorVersion := getAsInteger;
252 isc_info_ods_version:
253 FODSMajorVersion := getAsInteger;
254 isc_info_db_SQL_Dialect:
255 FSQLDialect := getAsInteger;
256 end;
257
258 FCharSetID := 0;
259 FRemoteProtocol := '';
260 FAuthMethod := 'Legacy_Auth';
261 FSecDatabase := 'Default';
262 if FODSMajorVersion > 11 then
263 begin
264 Stmt := Prepare(StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit),
265 'Select MON$CHARACTER_SET_ID, MON$REMOTE_PROTOCOL, MON$AUTH_METHOD, MON$SEC_DATABASE From MON$ATTACHMENTS, MON$DATABASE '+
266 'Where MON$ATTACHMENT_ID = CURRENT_CONNECTION');
267 ResultSet := Stmt.OpenCursor;
268 if ResultSet.FetchNext then
269 begin
270 FCharSetID := ResultSet[0].AsInteger;
271 FRemoteProtocol := Trim(ResultSet[1].AsString);
272 FAuthMethod := Trim(ResultSet[2].AsString);
273 FSecDatabase := Trim(ResultSet[3].AsString);
274 end
275 end
276 else
277 if (FODSMajorVersion = 11) and (FODSMinorVersion >= 1) then
278 begin
279 Stmt := Prepare(StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit),
280 'Select MON$CHARACTER_SET_ID, MON$REMOTE_PROTOCOL From MON$ATTACHMENTS '+
281 'Where MON$ATTACHMENT_ID = CURRENT_CONNECTION');
282 ResultSet := Stmt.OpenCursor;
283 if ResultSet.FetchNext then
284 begin
285 FCharSetID := ResultSet[0].AsInteger;
286 FRemoteProtocol := Trim(ResultSet[1].AsString);
287 end
288 end
289 else
290 if DPB <> nil then
291 begin
292 Param := DPB.Find(isc_dpb_lc_ctype);
293 if (Param = nil) or not CharSetName2CharSetID(Param.AsString,FCharSetID) then
294 FCharSetID := 0;
295 case GetProtocol(FDatabaseName) of
296 TCP: FRemoteProtocol := 'TCPv4';
297 Local: FRemoteProtocol := '';
298 NamedPipe: FRemoteProtocol := 'Netbui';
299 SPX: FRemoteProtocol := 'SPX'
300 end;
301 end;
302 FHasDefaultCharSet := CharSetID2CodePage(FCharSetID,FCodePage) and (FCharSetID > 1);
303 end;
304
305 constructor TFBAttachment.Create(api: TFBClientAPI; DatabaseName: AnsiString;
306 DPB: IDPB; RaiseExceptionOnConnectError: boolean);
307 begin
308 inherited Create;
309 FFirebirdAPI := api.GetAPI; {Keep reference to interface}
310 FSQLDialect := 3;
311 FDatabaseName := DatabaseName;
312 FDPB := DPB;
313 SetLength(FUserCharSetMap,0);
314 FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError;
315 FODSMajorVersion := 0;
316 FODSMinorVersion := 0;
317 end;
318
319 function TFBAttachment.GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
320 var CreateParams: AnsiString;
321 DPBItem: IDPBItem;
322 begin
323 CreateParams := '';
324
325 if aDPB <> nil then
326 begin
327 DPBItem := aDPB.Find(isc_dpb_user_name);
328 if DPBItem <> nil then
329 CreateParams := CreateParams + ' USER ''' + DPBItem.AsString + '''';
330
331 DPBItem := aDPB.Find(isc_dpb_password);
332 if DPBItem <> nil then
333 CreateParams := CreateParams + ' Password ''' + DPBItem.AsString + '''';
334
335 DPBItem := aDPB.Find(isc_dpb_page_size);
336 if DPBItem <> nil then
337 CreateParams := CreateParams + ' PAGE_SIZE ' + DPBItem.AsString;
338
339 DPBItem := aDPB.Find(isc_dpb_lc_ctype);
340 if DPBItem <> nil then
341 CreateParams := CreateParams + ' DEFAULT CHARACTER SET ' + DPBItem.AsString;
342
343 DPBItem := aDPB.Find(isc_dpb_sql_dialect);
344 if DPBItem <> nil then
345 FSQLDialect := DPBItem.AsInteger;
346 end;
347
348 Result := 'CREATE DATABASE ''' + DatabaseName + ''' ' + CreateParams; {do not localize}
349 end;
350
351 procedure TFBAttachment.EndAllTransactions;
352 var i: integer;
353 intf: TInterfacedObject;
354 begin
355 for i := 0 to InterfaceCount - 1 do
356 begin
357 intf := GetInterface(i);
358 if (intf <> nil) and (intf is TFBTransaction) then
359 TFBTransaction(intf).DoDefaultTransactionEnd(true);
360 end;
361 end;
362
363 {$IFDEF HASREQEX}
364 procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString);
365 var RegexObj: TRegExpr;
366 begin
367 FDPB := FFirebirdAPI.AllocateDPB;
368 RegexObj := TRegExpr.Create;
369 try
370 {extact database file spec}
371 RegexObj.ModifierG := false; {turn off greedy matches}
372 RegexObj.ModifierI := true; {case insensitive match}
373 RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +''.*'' +USER +''(.+)'' PASSWORD +''(.+)''';
374 if RegexObj.Exec(CreateSQL) then
375 begin
376 DPB.Add(isc_dpb_user_name).AsString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]);
377 DPB.Add(isc_dpb_password).AsString := system.copy(CreateSQL,RegexObj.MatchPos[3],RegexObj.MatchLen[3]);
378 end
379 else
380 begin
381 RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +(''.*'') +USER +''(.+)''';
382 if RegexObj.Exec(CreateSQL) then
383 DPB.Add(isc_dpb_user_name).AsString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]);
384 end;
385 finally
386 RegexObj.Free;
387 end;
388 if FCharSetID > 0 then
389 DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID);
390 DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect);
391 end;
392 {$ELSE}
393 procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString);
394 begin
395 FDPB := FFirebirdAPI.AllocateDPB;
396 if FCharSetID > 0 then
397 DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID);
398 DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect);
399 end;
400 {$ENDIF}
401
402 procedure TFBAttachment.SetParameters(SQLParams: ISQLParams;
403 params: array of const);
404 var i: integer;
405 begin
406 if SQLParams.Count <> Length(params) then
407 IBError(ibxeInvalidParamCount,[SQLParams.Count,Length(params)]);
408
409 for i := 0 to High(params) do
410 begin
411 case params[i].vtype of
412 vtinteger :
413 SQLParams[i].AsInteger := params[i].vinteger;
414 vtInt64:
415 SQLParams[i].AsInt64 := params[i].VInt64^;
416 {$IF declared (vtQWord)}
417 vtQWord:
418 SQLParams[i].AsInt64 := params[i].VQWord^;
419 {$IFEND}
420 vtboolean :
421 SQLParams[i].AsBoolean := params[i].vboolean;
422 vtchar :
423 SQLParams[i].AsString := params[i].vchar;
424 vtextended :
425 SQLParams[i].AsDouble := params[i].VExtended^;
426 vtCurrency:
427 SQLParams[i].AsDouble := params[i].VCurrency^;
428 vtString :
429 SQLParams[i].AsString := strpas(PChar(params[i].VString));
430 vtPChar :
431 SQLParams[i].AsString := strpas(params[i].VPChar);
432 vtAnsiString :
433 SQLParams[i].AsString := strpas(PAnsiChar(params[i].VAnsiString));
434 vtVariant:
435 SQLParams[i].AsVariant := params[i].VVariant^;
436 vtWideChar:
437 SQLParams[i].AsString := UTF8Encode(WideCharLenToString(@params[i].VWideChar,1));
438 vtPWideChar:
439 SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VPWideChar)));
440 vtWideString:
441 SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VWideString)));
442 vtUnicodeString:
443 SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VUnicodeString)));
444 else
445 IBError(ibxeInvalidVariantType,[nil]);
446 end;
447 end;
448 end;
449
450 destructor TFBAttachment.Destroy;
451 begin
452 Disconnect(true);
453 inherited Destroy;
454 end;
455
456 function TFBAttachment.getFirebirdAPI: IFirebirdAPI;
457 begin
458 Result := FFirebirdAPI;
459 end;
460
461 function TFBAttachment.getDPB: IDPB;
462 begin
463 Result := FDPB;
464 end;
465
466 function TFBAttachment.AllocateBPB: IBPB;
467 begin
468 Result := TBPB.Create(FFirebirdAPI as TFBClientAPI);
469 end;
470
471 function TFBAttachment.AllocateDIRB: IDIRB;
472 begin
473 Result := TDIRB.Create(FFirebirdAPI as TFBClientAPI);
474 end;
475
476 procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString;
477 aSQLDialect: integer);
478 begin
479 ExecImmediate(StartTransaction(TPB,taCommit),sql,aSQLDialect);
480 end;
481
482 procedure TFBAttachment.ExecImmediate(transaction: ITransaction; sql: AnsiString);
483 begin
484 ExecImmediate(transaction,sql,FSQLDialect);
485 end;
486
487 procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString);
488 begin
489 ExecImmediate(StartTransaction(TPB,taCommit),sql,FSQLDialect);
490 end;
491
492 function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
493 SQLDialect: integer; params: array of const): IResults;
494 begin
495 Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params);
496 end;
497
498 function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
499 SQLDialect: integer; params: array of const): IResults;
500 begin
501 with Prepare(transaction,sql,SQLDialect) do
502 begin
503 SetParameters(SQLParams,params);
504 Result := Execute;
505 end;
506 end;
507
508 function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
509 params: array of const): IResults;
510 begin
511 Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,params);
512 end;
513
514 function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
515 params: array of const): IResults;
516 begin
517 with Prepare(transaction,sql,FSQLDialect) do
518 begin
519 SetParameters(SQLParams,params);
520 Result := Execute;
521 end;
522 end;
523
524 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
525 aSQLDialect: integer): IResultSet;
526 begin
527 Result := OpenCursor(transaction,sql,aSQLDialect,[]);
528 end;
529
530 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
531 aSQLDialect: integer; params: array of const): IResultSet;
532 var Statement: IStatement;
533 begin
534 CheckHandle;
535 Statement := Prepare(transaction,sql,aSQLDialect);
536 SetParameters(Statement.SQLParams,params);
537 Result := Statement.OpenCursor;
538 end;
539
540 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString
541 ): IResultSet;
542 begin
543 Result := OpenCursor(transaction,sql,FSQLDialect,[]);
544 end;
545
546 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
547 params: array of const): IResultSet;
548 begin
549 Result := OpenCursor(transaction,sql,FSQLDialect,params);
550 end;
551
552 function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
553 sql: AnsiString; aSQLDialect: integer): IResultSet;
554 begin
555 Result := OpenCursor(transaction,sql,aSQLDialect,[]);
556 Result.FetchNext;
557 end;
558
559 function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
560 sql: AnsiString; aSQLDialect: integer; params: array of const): IResultSet;
561 begin
562 Result := OpenCursor(transaction,sql,aSQLDialect,params);
563 Result.FetchNext;
564 end;
565
566 function TFBAttachment.OpenCursorAtStart(transaction: ITransaction; sql: AnsiString
567 ): IResultSet;
568 begin
569 Result := OpenCursorAtStart(transaction,sql,FSQLDialect,[]);
570 end;
571
572 function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
573 sql: AnsiString; params: array of const): IResultSet;
574 begin
575 Result := OpenCursorAtStart(transaction,sql,FSQLDialect,params);
576 end;
577
578 function TFBAttachment.OpenCursorAtStart(sql: AnsiString): IResultSet;
579 begin
580 Result := OpenCursorAtStart(sql,[]);
581 end;
582
583 function TFBAttachment.OpenCursorAtStart(sql: AnsiString;
584 params: array of const): IResultSet;
585 begin
586 Result := OpenCursorAtStart(StartTransaction([isc_tpb_read,isc_tpb_wait,isc_tpb_concurrency],taCommit),sql,FSQLDialect,params);
587 end;
588
589 function TFBAttachment.Prepare(transaction: ITransaction; sql: AnsiString
590 ): IStatement;
591 begin
592 Result := Prepare(transaction,sql,FSQLDialect);
593 end;
594
595 function TFBAttachment.PrepareWithNamedParameters(transaction: ITransaction;
596 sql: AnsiString; GenerateParamNames: boolean; CaseSensitiveParams: boolean): IStatement;
597 begin
598 Result := PrepareWithNamedParameters(transaction,sql,FSQLDialect,GenerateParamNames,CaseSensitiveParams);
599 end;
600
601 function TFBAttachment.GetEventHandler(Event: AnsiString): IEvents;
602 var S: TStringList;
603 begin
604 S := TStringList.Create;
605 try
606 S.Add(Event);
607 Result := GetEventHandler(S);
608 finally
609 S.Free;
610 end;
611 end;
612
613 function TFBAttachment.GetSQLDialect: integer;
614 begin
615 Result := FSQLDialect;
616 end;
617
618 function TFBAttachment.OpenBlob(transaction: ITransaction; Field: ISQLData;
619 BPB: IBPB): IBlob;
620 begin
621 Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
622 end;
623
624 function TFBAttachment.GetDBInformation(Requests: array of byte
625 ): IDBInformation;
626 var ReqBuffer: PByte;
627 i: integer;
628 begin
629 CheckHandle;
630 if Length(Requests) = 1 then
631 Result := GetDBInformation(Requests[0])
632 else
633 begin
634 GetMem(ReqBuffer,Length(Requests));
635 try
636 for i := 0 to Length(Requests) - 1 do
637 ReqBuffer[i] := Requests[i];
638
639 Result := GetDBInfo(ReqBuffer,Length(Requests));
640
641 finally
642 FreeMem(ReqBuffer);
643 end;
644 end;
645 end;
646
647 function TFBAttachment.GetDBInformation(Request: byte): IDBInformation;
648 begin
649 CheckHandle;
650 Result := GetDBInfo(@Request,1);
651 end;
652
653 function TFBAttachment.GetDBInformation(Requests: IDIRB): IDBInformation;
654 begin
655 CheckHandle;
656 with Requests as TDIRB do
657 Result := GetDBInfo(getBuffer,getDataLength);
658 end;
659
660 function TFBAttachment.GetConnectString: AnsiString;
661 begin
662 Result := FDatabaseName;
663 end;
664
665 function TFBAttachment.GetRemoteProtocol: AnsiString;
666 begin
667 Result := FRemoteProtocol;
668 end;
669
670 function TFBAttachment.GetAuthenticationMethod: AnsiString;
671 begin
672 Result := FAuthMethod;
673 end;
674
675 function TFBAttachment.GetSecurityDatabase: AnsiString;
676 begin
677 Result := FSecDatabase;
678 end;
679
680 function TFBAttachment.GetODSMajorVersion: integer;
681 begin
682 Result := FODSMajorVersion;
683 end;
684
685 function TFBAttachment.GetODSMinorVersion: integer;
686 begin
687 Result := FODSMinorVersion;
688 end;
689
690 function TFBAttachment.HasDefaultCharSet: boolean;
691 begin
692 Result := FHasDefaultCharSet
693 end;
694
695 function TFBAttachment.GetDefaultCharSetID: integer;
696 begin
697 Result := FCharsetID;
698 end;
699
700 function TFBAttachment.GetCharsetName(CharSetID: integer): AnsiString;
701 var i: integer;
702 begin
703 Result := '';
704 if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
705 (CharSetMap[CharSetID].CharSetID = CharSetID) then
706 begin
707 Result := CharSetMap[CharSetID].CharSetName;
708 Exit;
709 end;
710
711 for i := 0 to Length(FUserCharSetMap) - 1 do
712 if FUserCharSetMap[i].CharSetID = CharSetID then
713 begin
714 Result := FUserCharSetMap[i].CharSetName;
715 Exit;
716 end;
717 end;
718
719 function TFBAttachment.CharSetID2CodePage(CharSetID: integer;
720 var CodePage: TSystemCodePage): boolean;
721 var i: integer;
722 begin
723 Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
724 and (CharSetMap[CharSetID].CharSetID = CharSetID);
725 if Result then
726 begin
727 CodePage := CharSetMap[CharSetID].CodePage;
728 Result := true;
729 Exit;
730 end;
731
732 for i := 0 to Length(FUserCharSetMap) - 1 do
733 if FUserCharSetMap[i].CharSetID = CharSetID then
734 begin
735 CodePage := FUserCharSetMap[i].CodePage;
736 Result := true;
737 Exit;
738 end;
739 end;
740
741 function TFBAttachment.CodePage2CharSetID(CodePage: TSystemCodePage;
742 var CharSetID: integer): boolean;
743 var i: integer;
744 begin
745 Result := false;
746 for i := Low(CharSetMap) to High(CharSetMap) do
747 if (CharSetMap[i].AllowReverseLookup) and (CharSetMap[i].CodePage = CodePage) then
748 begin
749 CharSetID := CharSetMap[i].CharSetID;
750 Result := true;
751 Exit;
752 end;
753
754 for i := 0 to Length(FUserCharSetMap) - 1 do
755 if (FUserCharSetMap[i].AllowReverseLookup) and (FUserCharSetMap[i].CodePage = CodePage) then
756 begin
757 CharSetID := FUserCharSetMap[i].CharSetID;
758 Result := true;
759 Exit;
760 end;
761 end;
762
763 function TFBAttachment.CharSetName2CharSetID(CharSetName: AnsiString;
764 var CharSetID: integer): boolean;
765 var i: integer;
766 begin
767 Result := false;
768 for i := Low(CharSetMap) to High(CharSetMap) do
769 if AnsiCompareText(CharSetMap[i].CharSetName, CharSetName) = 0 then
770 begin
771 CharSetID := CharSetMap[i].CharSetID;
772 Result := true;
773 Exit;
774 end;
775
776 for i := 0 to Length(FUserCharSetMap) - 1 do
777 if AnsiCompareText(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then
778 begin
779 CharSetID := FUserCharSetMap[i].CharSetID;
780 Result := true;
781 Exit;
782 end;
783 end;
784
785 function TFBAttachment.CharSetWidth(CharSetID: integer; var Width: integer
786 ): boolean;
787 var i: integer;
788 begin
789 Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
790 and (CharSetMap[CharSetID].CharSetID = CharSetID);
791 if Result then
792 begin
793 Width := CharSetMap[CharSetID].CharSetWidth;
794 Result := true;
795 Exit;
796 end;
797
798 for i := 0 to Length(FUserCharSetMap) - 1 do
799 if FUserCharSetMap[i].CharSetID = CharSetID then
800 begin
801 Width := FUserCharSetMap[i].CharSetWidth;
802 Result := true;
803 Exit;
804 end;
805 end;
806
807 const
808 sqlLookupCharSet = 'Select RDB$CHARACTER_SET_ID, RDB$BYTES_PER_CHARACTER From RDB$CHARACTER_SETS '+
809 'Where RDB$SYSTEM_FLAG = 0 and RDB$CHARACTER_SET_NAME = UPPER(?)';
810
811 procedure TFBAttachment.RegisterCharSet(CharSetName: AnsiString;
812 CodePage: TSystemCodePage; AllowReverseLookup: boolean; out CharSetID: integer
813 );
814 var CharSets: IResultSet;
815 idx: integer;
816 begin
817 if CharSetName2CharSetID(CharSetName,CharSetID) then
818 IBError(ibxeCharacterSetExists,[CharSetName]);
819
820 CharSets := OpenCursorAtStart(sqlLookupCharSet,[CharSetName]);
821 if CharSets.IsEof then
822 IBError(ibxeUnknownUserCharSet,[CharSetName]);
823
824 idx := Length(FUserCharSetMap);
825 SetLength(FUserCharSetMap,idx+1);
826 FUserCharSetMap[idx].AllowReverseLookup := AllowReverseLookup;
827 FUserCharSetMap[idx].CharSetID := CharSets[0].AsInteger;
828 FUserCharSetMap[idx].CharSetName := AnsiUpperCase(CharSetName);
829 FUserCharSetMap[idx].CharSetWidth := CharSets[1].AsInteger;
830 FUserCharSetMap[idx].CodePage := CodePage;
831 CharSetID := CharSets[0].AsInteger;
832 end;
833
834 end.
835