ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/journaling/fbintf/client/FBAttachment.pas
Revision: 118
Committed: Mon Jan 22 13:58:14 2018 UTC (6 years, 10 months ago) by tony
Content type: text/x-pascal
Original Path: ibx/trunk/fbintf/client/FBAttachment.pas
File size: 30032 byte(s)
Log Message:
Fixes Merged

File Contents

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