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