ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/client/FBAttachment.pas
Revision: 291
Committed: Fri Apr 17 10:26:08 2020 UTC (4 years, 8 months ago) by tony
Content type: text/x-pascal
File size: 34920 byte(s)
Log Message:
Changed for 2.3.4 merged into public release

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