ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/udr/client/FBAttachment.pas
Revision: 119
Committed: Mon Jan 22 13:58:18 2018 UTC (6 years, 10 months ago) by tony
Content type: text/x-pascal
Original Path: ibx/trunk/fbintf/client/FBAttachment.pas
File size: 30262 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 119 {$IF defined(CompilerVersion) and (CompilerVersion >= 28)}
31 tony 118 {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 tony 119 begin
369     FDPB := FFirebirdAPI.AllocateDPB;
370     if FCharSetID > 0 then
371     DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID);
372     DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect);
373 tony 118 end;
374     {$ENDIF}
375 tony 117
376 tony 45 procedure TFBAttachment.SetParameters(SQLParams: ISQLParams;
377     params: array of const);
378     var i: integer;
379     begin
380     if SQLParams.Count <> Length(params) then
381     IBError(ibxeInvalidParamCount,[SQLParams.Count,Length(params)]);
382    
383     for i := 0 to High(params) do
384     begin
385     case params[i].vtype of
386     vtinteger :
387     SQLParams[i].AsInteger := params[i].vinteger;
388 tony 70 vtInt64:
389     SQLParams[i].AsInt64 := params[i].VInt64^;
390     {$IF declared (vtQWord)}
391     vtQWord:
392     SQLParams[i].AsInt64 := params[i].VQWord^;
393     {$IFEND}
394 tony 45 vtboolean :
395     SQLParams[i].AsBoolean := params[i].vboolean;
396     vtchar :
397     SQLParams[i].AsString := params[i].vchar;
398     vtextended :
399     SQLParams[i].AsDouble := params[i].VExtended^;
400     vtCurrency:
401     SQLParams[i].AsDouble := params[i].VCurrency^;
402     vtString :
403 tony 70 SQLParams[i].AsString := strpas(PChar(params[i].VString));
404 tony 45 vtPChar :
405     SQLParams[i].AsString := strpas(params[i].VPChar);
406     vtAnsiString :
407 tony 70 SQLParams[i].AsString := strpas(PAnsiChar(params[i].VAnsiString));
408 tony 45 vtVariant:
409     SQLParams[i].AsVariant := params[i].VVariant^;
410 tony 70 vtWideChar:
411     SQLParams[i].AsString := UTF8Encode(WideCharLenToString(@params[i].VWideChar,1));
412     vtPWideChar:
413     SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VPWideChar)));
414     vtWideString:
415     SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VWideString)));
416     vtUnicodeString:
417     SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VUnicodeString)));
418 tony 45 else
419     IBError(ibxeInvalidVariantType,[nil]);
420     end;
421     end;
422     end;
423    
424     destructor TFBAttachment.Destroy;
425     begin
426     Disconnect(true);
427     inherited Destroy;
428     end;
429    
430     function TFBAttachment.getDPB: IDPB;
431     begin
432     Result := FDPB;
433     end;
434    
435     function TFBAttachment.AllocateBPB: IBPB;
436     begin
437     Result := TBPB.Create;
438     end;
439    
440 tony 56 procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString;
441 tony 45 aSQLDialect: integer);
442     begin
443     ExecImmediate(StartTransaction(TPB,taCommit),sql,aSQLDialect);
444     end;
445    
446 tony 56 procedure TFBAttachment.ExecImmediate(transaction: ITransaction; sql: AnsiString);
447 tony 45 begin
448     ExecImmediate(transaction,sql,FSQLDialect);
449     end;
450    
451 tony 56 procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString);
452 tony 45 begin
453     ExecImmediate(StartTransaction(TPB,taCommit),sql,FSQLDialect);
454     end;
455    
456 tony 56 function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
457 tony 45 SQLDialect: integer; params: array of const): IResults;
458     begin
459 tony 117 Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params);
460 tony 45 end;
461    
462 tony 56 function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
463 tony 45 SQLDialect: integer; params: array of const): IResults;
464     begin
465     with Prepare(transaction,sql,SQLDialect) do
466     begin
467     SetParameters(SQLParams,params);
468     Result := Execute;
469     end;
470     end;
471    
472 tony 56 function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
473 tony 45 params: array of const): IResults;
474     begin
475     Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,params);
476     end;
477    
478 tony 56 function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
479 tony 45 params: array of const): IResults;
480     begin
481     with Prepare(transaction,sql,FSQLDialect) do
482     begin
483     SetParameters(SQLParams,params);
484     Result := Execute;
485     end;
486     end;
487    
488 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
489 tony 45 aSQLDialect: integer): IResultSet;
490     begin
491     Result := OpenCursor(transaction,sql,aSQLDialect,[]);
492     end;
493    
494 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
495 tony 45 aSQLDialect: integer; params: array of const): IResultSet;
496     var Statement: IStatement;
497     begin
498     CheckHandle;
499     Statement := Prepare(transaction,sql,aSQLDialect);
500     SetParameters(Statement.SQLParams,params);
501     Result := Statement.OpenCursor;
502     end;
503    
504 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString
505 tony 45 ): IResultSet;
506     begin
507     Result := OpenCursor(transaction,sql,FSQLDialect,[]);
508     end;
509    
510 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
511 tony 45 params: array of const): IResultSet;
512     begin
513     Result := OpenCursor(transaction,sql,FSQLDialect,params);
514     end;
515    
516     function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
517 tony 56 sql: AnsiString; aSQLDialect: integer): IResultSet;
518 tony 45 begin
519     Result := OpenCursor(transaction,sql,aSQLDialect,[]);
520     Result.FetchNext;
521     end;
522    
523     function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
524 tony 56 sql: AnsiString; aSQLDialect: integer; params: array of const): IResultSet;
525 tony 45 begin
526     Result := OpenCursor(transaction,sql,aSQLDialect,params);
527     Result.FetchNext;
528     end;
529    
530 tony 56 function TFBAttachment.OpenCursorAtStart(transaction: ITransaction; sql: AnsiString
531 tony 45 ): IResultSet;
532     begin
533     Result := OpenCursorAtStart(transaction,sql,FSQLDialect,[]);
534     end;
535    
536     function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
537 tony 56 sql: AnsiString; params: array of const): IResultSet;
538 tony 45 begin
539     Result := OpenCursorAtStart(transaction,sql,FSQLDialect,params);
540     end;
541    
542 tony 56 function TFBAttachment.OpenCursorAtStart(sql: AnsiString): IResultSet;
543 tony 45 begin
544     Result := OpenCursorAtStart(sql,[]);
545     end;
546    
547 tony 56 function TFBAttachment.OpenCursorAtStart(sql: AnsiString;
548 tony 45 params: array of const): IResultSet;
549     begin
550     Result := OpenCursorAtStart(StartTransaction([isc_tpb_read,isc_tpb_wait,isc_tpb_concurrency],taCommit),sql,FSQLDialect,params);
551     end;
552    
553 tony 56 function TFBAttachment.Prepare(transaction: ITransaction; sql: AnsiString
554 tony 45 ): IStatement;
555     begin
556     Result := Prepare(transaction,sql,FSQLDialect);
557     end;
558    
559     function TFBAttachment.PrepareWithNamedParameters(transaction: ITransaction;
560 tony 56 sql: AnsiString; GenerateParamNames: boolean): IStatement;
561 tony 45 begin
562     Result := PrepareWithNamedParameters(transaction,sql,FSQLDialect,GenerateParamNames);
563     end;
564    
565 tony 56 function TFBAttachment.GetEventHandler(Event: AnsiString): IEvents;
566 tony 45 var S: TStringList;
567     begin
568     S := TStringList.Create;
569     try
570     S.Add(Event);
571     Result := GetEventHandler(S);
572     finally
573     S.Free;
574     end;
575     end;
576    
577     function TFBAttachment.GetSQLDialect: integer;
578     begin
579     Result := FSQLDialect;
580     end;
581    
582     function TFBAttachment.OpenBlob(transaction: ITransaction; Field: ISQLData;
583     BPB: IBPB): IBlob;
584     begin
585     Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
586     end;
587    
588 tony 117 function TFBAttachment.GetConnectString: AnsiString;
589     begin
590     Result := FDatabaseName;
591     end;
592    
593     function TFBAttachment.GetRemoteProtocol: AnsiString;
594     begin
595     Result := FRemoteProtocol;
596     end;
597    
598     function TFBAttachment.GetODSMajorVersion: integer;
599     begin
600     Result := FODSMajorVersion;
601     end;
602    
603     function TFBAttachment.GetODSMinorVersion: integer;
604     begin
605     Result := FODSMinorVersion;
606     end;
607    
608 tony 109 function TFBAttachment.HasDefaultCharSet: boolean;
609     begin
610     Result := FHasDefaultCharSet
611     end;
612    
613     function TFBAttachment.GetDefaultCharSetID: integer;
614     begin
615     Result := FCharsetID;
616     end;
617    
618 tony 60 function TFBAttachment.GetCharsetName(CharSetID: integer): AnsiString;
619     var i: integer;
620     begin
621     Result := '';
622     if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
623     (CharSetMap[CharSetID].CharSetID = CharSetID) then
624     begin
625     Result := CharSetMap[CharSetID].CharSetName;
626     Exit;
627     end;
628    
629     for i := 0 to Length(FUserCharSetMap) - 1 do
630     if FUserCharSetMap[i].CharSetID = CharSetID then
631     begin
632     Result := FUserCharSetMap[i].CharSetName;
633     Exit;
634     end;
635     end;
636    
637     function TFBAttachment.CharSetID2CodePage(CharSetID: integer;
638     var CodePage: TSystemCodePage): boolean;
639     var i: integer;
640     begin
641     Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
642     and (CharSetMap[CharSetID].CharSetID = CharSetID);
643     if Result then
644     begin
645     CodePage := CharSetMap[CharSetID].CodePage;
646     Result := true;
647     Exit;
648     end;
649    
650     for i := 0 to Length(FUserCharSetMap) - 1 do
651     if FUserCharSetMap[i].CharSetID = CharSetID then
652     begin
653     CodePage := FUserCharSetMap[i].CodePage;
654     Result := true;
655     Exit;
656     end;
657     end;
658    
659     function TFBAttachment.CodePage2CharSetID(CodePage: TSystemCodePage;
660     var CharSetID: integer): boolean;
661     var i: integer;
662     begin
663     Result := false;
664     for i := Low(CharSetMap) to High(CharSetMap) do
665     if (CharSetMap[i].AllowReverseLookup) and (CharSetMap[i].CodePage = CodePage) then
666     begin
667     CharSetID := CharSetMap[i].CharSetID;
668     Result := true;
669     Exit;
670     end;
671    
672     for i := 0 to Length(FUserCharSetMap) - 1 do
673     if (FUserCharSetMap[i].AllowReverseLookup) and (FUserCharSetMap[i].CodePage = CodePage) then
674     begin
675     CharSetID := FUserCharSetMap[i].CharSetID;
676     Result := true;
677     Exit;
678     end;
679     end;
680    
681     function TFBAttachment.CharSetName2CharSetID(CharSetName: AnsiString;
682     var CharSetID: integer): boolean;
683     var i: integer;
684     begin
685     Result := false;
686     for i := Low(CharSetMap) to High(CharSetMap) do
687     if AnsiCompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then
688     begin
689     CharSetID := CharSetMap[i].CharSetID;
690     Result := true;
691     Exit;
692     end;
693    
694     for i := 0 to Length(FUserCharSetMap) - 1 do
695     if AnsiCompareStr(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then
696     begin
697     CharSetID := FUserCharSetMap[i].CharSetID;
698     Result := true;
699     Exit;
700     end;
701     end;
702    
703     function TFBAttachment.CharSetWidth(CharSetID: integer; var Width: integer
704     ): boolean;
705     var i: integer;
706     begin
707     Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
708     and (CharSetMap[CharSetID].CharSetID = CharSetID);
709     if Result then
710     begin
711     Width := CharSetMap[CharSetID].CharSetWidth;
712     Result := true;
713     Exit;
714     end;
715    
716     for i := 0 to Length(FUserCharSetMap) - 1 do
717     if FUserCharSetMap[i].CharSetID = CharSetID then
718     begin
719     Width := FUserCharSetMap[i].CharSetWidth;
720     Result := true;
721     Exit;
722     end;
723     end;
724    
725     const
726     sqlLookupCharSet = 'Select RDB$CHARACTER_SET_ID, RDB$BYTES_PER_CHARACTER From RDB$CHARACTER_SETS '+
727     'Where RDB$SYSTEM_FLAG = 0 and RDB$CHARACTER_SET_NAME = UPPER(?)';
728    
729     procedure TFBAttachment.RegisterCharSet(CharSetName: AnsiString;
730     CodePage: TSystemCodePage; AllowReverseLookup: boolean; out CharSetID: integer
731     );
732     var CharSets: IResultSet;
733     idx: integer;
734     begin
735     if CharSetName2CharSetID(CharSetName,CharSetID) then
736     IBError(ibxeCharacterSetExists,[CharSetName]);
737    
738     CharSets := OpenCursorAtStart(sqlLookupCharSet,[CharSetName]);
739     if CharSets.IsEof then
740     IBError(ibxeUnknownUserCharSet,[CharSetName]);
741    
742     idx := Length(FUserCharSetMap);
743     SetLength(FUserCharSetMap,idx+1);
744     FUserCharSetMap[idx].AllowReverseLookup := AllowReverseLookup;
745     FUserCharSetMap[idx].CharSetID := CharSets[0].AsInteger;
746     FUserCharSetMap[idx].CharSetName := AnsiUpperCase(CharSetName);
747     FUserCharSetMap[idx].CharSetWidth := CharSets[1].AsInteger;
748     FUserCharSetMap[idx].CodePage := CodePage;
749     CharSetID := CharSets[0].AsInteger;
750     end;
751    
752 tony 45 end.
753