ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/client/FBAttachment.pas
Revision: 60
Committed: Mon Mar 27 15:21:02 2017 UTC (7 years, 1 month ago) by tony
Content type: text/x-pascal
File size: 25199 byte(s)
Log Message:

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     {$ENDIF}
36    
37     interface
38    
39     uses
40 tony 60 Classes, SysUtils, {$IFDEF WINDOWS} windows, {$ENDIF} IB, FBParamBlock, FBActivityMonitor;
41 tony 45
42     type
43 tony 60 TCharsetMap = record
44     CharsetID: integer;
45     CharSetName: AnsiString;
46     CharSetWidth: integer;
47     CodePage: TSystemCodePage;
48     AllowReverseLookup: boolean; {used to ensure that lookup of CP_UTF* does not return UNICODE_FSS}
49     end;
50 tony 45
51     { TFBAttachment }
52    
53     TFBAttachment = class(TActivityHandler)
54     private
55     FDPB: IDPB;
56     FFirebirdAPI: IFirebirdAPI;
57 tony 60 FUserCharSetMap: array of TCharSetMap;
58 tony 45 protected
59 tony 56 FDatabaseName: AnsiString;
60 tony 45 FRaiseExceptionOnConnectError: boolean;
61     FSQLDialect: integer;
62     FHasDefaultCharSet: boolean;
63     FCharSetID: integer;
64     FCodePage: TSystemCodePage;
65 tony 56 constructor Create(DatabaseName: AnsiString; DPB: IDPB;
66 tony 45 RaiseExceptionOnConnectError: boolean);
67     procedure CheckHandle; virtual; abstract;
68 tony 56 function GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
69 tony 45 procedure EndAllTransactions;
70     procedure SetParameters(SQLParams: ISQLParams; params: array of const);
71     public
72     destructor Destroy; override;
73     function getDPB: IDPB;
74     function AllocateBPB: IBPB;
75     function StartTransaction(TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract;
76     function StartTransaction(TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract;
77     procedure Disconnect(Force: boolean=false); virtual; abstract;
78 tony 56 procedure ExecImmediate(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer); overload; virtual; abstract;
79     procedure ExecImmediate(TPB: array of byte; sql: AnsiString; aSQLDialect: integer); overload;
80     procedure ExecImmediate(transaction: ITransaction; sql: AnsiString); overload;
81     procedure ExecImmediate(TPB: array of byte; sql: AnsiString); overload;
82     function ExecuteSQL(TPB: array of byte; sql: AnsiString; SQLDialect: integer; params: array of const): IResults; overload;
83     function ExecuteSQL(transaction: ITransaction; sql: AnsiString; SQLDialect: integer; params: array of const): IResults; overload;
84     function ExecuteSQL(TPB: array of byte; sql: AnsiString; params: array of const): IResults; overload;
85     function ExecuteSQL(transaction: ITransaction; sql: AnsiString; params: array of const): IResults; overload;
86     function OpenCursor(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer): IResultSet; overload;
87     function OpenCursor(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer;
88 tony 45 params: array of const): IResultSet; overload;
89 tony 56 function OpenCursor(transaction: ITransaction; sql: AnsiString): IResultSet; overload;
90     function OpenCursor(transaction: ITransaction; sql: AnsiString;
91 tony 45 params: array of const): IResultSet; overload;
92 tony 56 function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer): IResultSet; overload;
93     function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer;
94 tony 45 params: array of const): IResultSet; overload;
95 tony 56 function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString): IResultSet; overload;
96     function OpenCursorAtStart(transaction: ITransaction; sql: AnsiString;
97 tony 45 params: array of const): IResultSet; overload;
98 tony 56 function OpenCursorAtStart(sql: AnsiString): IResultSet; overload;
99     function OpenCursorAtStart(sql: AnsiString;
100 tony 45 params: array of const): IResultSet; overload;
101 tony 56 function Prepare(transaction: ITransaction; sql: AnsiString; aSQLDialect: integer): IStatement; overload; virtual; abstract;
102     function Prepare(transaction: ITransaction; sql: AnsiString): IStatement; overload;
103     function PrepareWithNamedParameters(transaction: ITransaction; sql: AnsiString;
104 tony 45 aSQLDialect: integer; GenerateParamNames: boolean=false): IStatement; overload; virtual; abstract;
105 tony 56 function PrepareWithNamedParameters(transaction: ITransaction; sql: AnsiString;
106 tony 45 GenerateParamNames: boolean=false): IStatement; overload;
107     function GetEventHandler(Events: TStrings): IEvents; overload; virtual; abstract;
108 tony 56 function GetEventHandler(Event: AnsiString): IEvents; overload;
109 tony 45
110     function GetSQLDialect: integer;
111 tony 56 function OpenBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BlobID: TISC_QUAD; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
112 tony 45 function OpenBlob(transaction: ITransaction; Field: ISQLData; BPB: IBPB=nil): IBlob; overload;
113     property SQLDialect: integer read FSQLDialect;
114     property DPB: IDPB read FDPB;
115 tony 60 public
116     {Character Sets}
117     function GetCharsetName(CharSetID: integer): AnsiString;
118     function CharSetID2CodePage(CharSetID: integer; var CodePage: TSystemCodePage): boolean;
119     function CodePage2CharSetID(CodePage: TSystemCodePage; var CharSetID: integer): boolean;
120     function CharSetName2CharSetID(CharSetName: AnsiString; var CharSetID: integer): boolean;
121     function CharSetWidth(CharSetID: integer; var Width: integer): boolean;
122     procedure RegisterCharSet(CharSetName: AnsiString; CodePage: TSystemCodePage;
123     AllowReverseLookup:boolean; out CharSetID: integer);
124     property HasDefaultCharSet: boolean read FHasDefaultCharSet;
125     property CharSetID: integer read FCharSetID;
126     property CodePage: TSystemCodePage read FCodePage;
127 tony 45 end;
128    
129     implementation
130    
131     uses FBMessages, FBTransaction;
132    
133 tony 60 const
134     CharSetMap: array [0..69] of TCharsetMap = (
135     (CharsetID: 0; CharSetName: 'NONE'; CharSetWidth: 1; CodePage: CP_ACP; AllowReverseLookup: true),
136     (CharsetID: 1; CharSetName: 'OCTETS'; CharSetWidth: 1; CodePage: CP_NONE; AllowReverseLookup: true),
137     (CharsetID: 2; CharSetName: 'ASCII'; CharSetWidth: 1; CodePage: CP_ASCII; AllowReverseLookup: true),
138     (CharsetID: 3; CharSetName: 'UNICODE_FSS'; CharSetWidth: 3; CodePage: CP_UTF8; AllowReverseLookup: false),
139     (CharsetID: 4; CharSetName: 'UTF8'; CharSetWidth: 4; CodePage: CP_UTF8; AllowReverseLookup: true),
140     (CharsetID: 5; CharSetName: 'SJIS_0208'; CharSetWidth: 2; CodePage: 20932; AllowReverseLookup: true),
141     (CharsetID: 6; CharSetName: 'EUCJ_0208'; CharSetWidth: 2; CodePage: 20932; AllowReverseLookup: true),
142     (CharsetID: 7; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
143     (CharsetID: 8; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
144     (CharsetID: 9; CharSetName: 'DOS737'; CharSetWidth: 1; CodePage: 737; AllowReverseLookup: true),
145     (CharsetID: 10; CharSetName: 'DOS437'; CharSetWidth: 1; CodePage: 437; AllowReverseLookup: true),
146     (CharsetID: 11; CharSetName: 'DOS850'; CharSetWidth: 1; CodePage: 850; AllowReverseLookup: true),
147     (CharsetID: 12; CharSetName: 'DOS865'; CharSetWidth: 1; CodePage: 865; AllowReverseLookup: true),
148     (CharsetID: 13; CharSetName: 'DOS860'; CharSetWidth: 1; CodePage: 860; AllowReverseLookup: true),
149     (CharsetID: 14; CharSetName: 'DOS863'; CharSetWidth: 1; CodePage: 863; AllowReverseLookup: true),
150     (CharsetID: 15; CharSetName: 'DOS775'; CharSetWidth: 1; CodePage: 775; AllowReverseLookup: true),
151     (CharsetID: 16; CharSetName: 'DOS858'; CharSetWidth: 1; CodePage: 858; AllowReverseLookup: true),
152     (CharsetID: 17; CharSetName: 'DOS862'; CharSetWidth: 1; CodePage: 862; AllowReverseLookup: true),
153     (CharsetID: 18; CharSetName: 'DOS864'; CharSetWidth: 1; CodePage: 864; AllowReverseLookup: true),
154     (CharsetID: 19; CharSetName: 'NEXT'; CharSetWidth: 1; CodePage: CP_NONE; AllowReverseLookup: true),
155     (CharsetID: 20; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
156     (CharsetID: 21; CharSetName: 'ISO8859_1'; CharSetWidth: 1; CodePage: 28591; AllowReverseLookup: true),
157     (CharsetID: 22; CharSetName: 'ISO8859_2'; CharSetWidth: 1; CodePage: 28592; AllowReverseLookup: true),
158     (CharsetID: 23; CharSetName: 'ISO8859_3'; CharSetWidth: 1; CodePage: 28593; AllowReverseLookup: true),
159     (CharsetID: 24; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
160     (CharsetID: 25; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
161     (CharsetID: 26; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
162     (CharsetID: 27; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
163     (CharsetID: 28; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
164     (CharsetID: 29; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
165     (CharsetID: 30; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
166     (CharsetID: 31; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
167     (CharsetID: 32; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
168     (CharsetID: 33; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
169     (CharsetID: 34; CharSetName: 'ISO8859_4'; CharSetWidth: 1; CodePage: 28594; AllowReverseLookup: true),
170     (CharsetID: 35; CharSetName: 'ISO8859_5'; CharSetWidth: 1; CodePage: 28595; AllowReverseLookup: true),
171     (CharsetID: 36; CharSetName: 'ISO8859_6'; CharSetWidth: 1; CodePage: 28596; AllowReverseLookup: true),
172     (CharsetID: 37; CharSetName: 'ISO8859_7'; CharSetWidth: 1; CodePage: 28597; AllowReverseLookup: true),
173     (CharsetID: 38; CharSetName: 'ISO8859_8'; CharSetWidth: 1; CodePage: 28598; AllowReverseLookup: true),
174     (CharsetID: 39; CharSetName: 'ISO8859_9'; CharSetWidth: 1; CodePage: 28599; AllowReverseLookup: true),
175     (CharsetID: 40; CharSetName: 'ISO8859_13'; CharSetWidth: 1; CodePage: 28603; AllowReverseLookup: true),
176     (CharsetID: 41; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
177     (CharsetID: 42; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
178     (CharsetID: 43; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
179     (CharsetID: 44; CharSetName: 'KSC_5601'; CharSetWidth: 2; CodePage: 949; AllowReverseLookup: true),
180     (CharsetID: 45; CharSetName: 'DOS852'; CharSetWidth: 1; CodePage: 852; AllowReverseLookup: true),
181     (CharsetID: 46; CharSetName: 'DOS857'; CharSetWidth: 1; CodePage: 857; AllowReverseLookup: true),
182     (CharsetID: 47; CharSetName: 'DOS861'; CharSetWidth: 1; CodePage: 861; AllowReverseLookup: true),
183     (CharsetID: 48; CharSetName: 'DOS866'; CharSetWidth: 1; CodePage: 866; AllowReverseLookup: true),
184     (CharsetID: 49; CharSetName: 'DOS869'; CharSetWidth: 1; CodePage: 869; AllowReverseLookup: true),
185     (CharsetID: 50; CharSetName: 'CYRL'; CharSetWidth: 1; CodePage: 1251; AllowReverseLookup: true),
186     (CharsetID: 51; CharSetName: 'WIN1250'; CharSetWidth: 1; CodePage: 1250; AllowReverseLookup: true),
187     (CharsetID: 52; CharSetName: 'WIN1251'; CharSetWidth: 1; CodePage: 1251; AllowReverseLookup: true),
188     (CharsetID: 53; CharSetName: 'WIN1252'; CharSetWidth: 1; CodePage: 1252; AllowReverseLookup: true),
189     (CharsetID: 54; CharSetName: 'WIN1253'; CharSetWidth: 1; CodePage: 1253; AllowReverseLookup: true),
190     (CharsetID: 55; CharSetName: 'WIN1254'; CharSetWidth: 1; CodePage: 1254; AllowReverseLookup: true),
191     (CharsetID: 56; CharSetName: 'BIG_5'; CharSetWidth: 2; CodePage: 950; AllowReverseLookup: true),
192     (CharsetID: 57; CharSetName: 'GB_2312'; CharSetWidth: 2; CodePage: 936; AllowReverseLookup: true),
193     (CharsetID: 58; CharSetName: 'WIN1255'; CharSetWidth: 1; CodePage: 1255; AllowReverseLookup: true),
194     (CharsetID: 59; CharSetName: 'WIN1256'; CharSetWidth: 1; CodePage: 1256; AllowReverseLookup: true),
195     (CharsetID: 60; CharSetName: 'WIN1257'; CharSetWidth: 1; CodePage: 1257; AllowReverseLookup: true),
196     (CharsetID: 61; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
197     (CharsetID: 62; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
198     (CharsetID: 63; CharSetName: 'KOI8R'; CharSetWidth: 1; CodePage: 20866; AllowReverseLookup: true),
199     (CharsetID: 64; CharSetName: 'KOI8U'; CharSetWidth: 1; CodePage: 21866; AllowReverseLookup: true),
200     (CharsetID: 65; CharSetName: 'WIN1258'; CharSetWidth: 1; CodePage: 1258; AllowReverseLookup: true),
201     (CharsetID: 66; CharSetName: 'TIS620'; CharSetWidth: 1; CodePage: 874; AllowReverseLookup: true),
202     (CharsetID: 67; CharSetName: 'GBK'; CharSetWidth: 2; CodePage: 936; AllowReverseLookup: true),
203     (CharsetID: 68; CharSetName: 'CP943C'; CharSetWidth: 2; CodePage: 943; AllowReverseLookup: true),
204     (CharsetID: 69; CharSetName: 'GB18030'; CharSetWidth: 4; CodePage: 54936; AllowReverseLookup: true)
205     );
206    
207    
208    
209    
210 tony 45 { TFBAttachment }
211    
212 tony 56 constructor TFBAttachment.Create(DatabaseName: AnsiString; DPB: IDPB;
213 tony 45 RaiseExceptionOnConnectError: boolean);
214     begin
215     inherited Create;
216     FFirebirdAPI := FirebirdAPI; {Keep reference to interface}
217     FSQLDialect := 3;
218     FDatabaseName := DatabaseName;
219     FDPB := DPB;
220 tony 60 SetLength(FUserCharSetMap,0);
221 tony 45 FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError;
222     end;
223    
224 tony 56 function TFBAttachment.GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
225     var CreateParams: AnsiString;
226 tony 45 DPBItem: IDPBItem;
227     begin
228     CreateParams := '';
229    
230     if aDPB <> nil then
231     begin
232     DPBItem := aDPB.Find(isc_dpb_user_name);
233     if DPBItem <> nil then
234 tony 56 CreateParams := CreateParams + ' USER ''' + DPBItem.AsString + '''';
235 tony 45
236     DPBItem := aDPB.Find(isc_dpb_password);
237     if DPBItem <> nil then
238 tony 56 CreateParams := CreateParams + ' Password ''' + DPBItem.AsString + '''';
239 tony 45
240     DPBItem := aDPB.Find(isc_dpb_page_size);
241     if DPBItem <> nil then
242 tony 56 CreateParams := CreateParams + ' PAGE_SIZE ' + DPBItem.AsString;
243 tony 45
244     DPBItem := aDPB.Find(isc_dpb_lc_ctype);
245     if DPBItem <> nil then
246 tony 56 CreateParams := CreateParams + ' DEFAULT CHARACTER SET ' + DPBItem.AsString;
247 tony 45
248     DPBItem := aDPB.Find(isc_dpb_sql_dialect);
249     if DPBItem <> nil then
250     FSQLDialect := DPBItem.AsInteger;
251     end;
252    
253     Result := 'CREATE DATABASE ''' + DatabaseName + ''' ' + CreateParams; {do not localize}
254     end;
255    
256     procedure TFBAttachment.EndAllTransactions;
257     var i: integer;
258     intf: TInterfacedObject;
259     begin
260     for i := 0 to InterfaceCount - 1 do
261     begin
262     intf := GetInterface(i);
263     if (intf <> nil) and (intf is TFBTransaction) then
264     TFBTransaction(intf).DoDefaultTransactionEnd(true);
265     end;
266     end;
267    
268     procedure TFBAttachment.SetParameters(SQLParams: ISQLParams;
269     params: array of const);
270     var i: integer;
271     begin
272     if SQLParams.Count <> Length(params) then
273     IBError(ibxeInvalidParamCount,[SQLParams.Count,Length(params)]);
274    
275     for i := 0 to High(params) do
276     begin
277     case params[i].vtype of
278     vtinteger :
279     SQLParams[i].AsInteger := params[i].vinteger;
280     vtboolean :
281     SQLParams[i].AsBoolean := params[i].vboolean;
282     vtchar :
283     SQLParams[i].AsString := params[i].vchar;
284     vtextended :
285     SQLParams[i].AsDouble := params[i].VExtended^;
286     vtCurrency:
287     SQLParams[i].AsDouble := params[i].VCurrency^;
288     vtString :
289     SQLParams[i].AsString := params[i].VString^;
290     vtPChar :
291     SQLParams[i].AsString := strpas(params[i].VPChar);
292     vtAnsiString :
293     SQLParams[i].AsString := AnsiString(params[i].VAnsiString^);
294     vtVariant:
295     SQLParams[i].AsVariant := params[i].VVariant^;
296     else
297     IBError(ibxeInvalidVariantType,[nil]);
298     end;
299     end;
300     end;
301    
302     destructor TFBAttachment.Destroy;
303     begin
304     Disconnect(true);
305     inherited Destroy;
306     end;
307    
308     function TFBAttachment.getDPB: IDPB;
309     begin
310     Result := FDPB;
311     end;
312    
313     function TFBAttachment.AllocateBPB: IBPB;
314     begin
315     Result := TBPB.Create;
316     end;
317    
318 tony 56 procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString;
319 tony 45 aSQLDialect: integer);
320     begin
321     ExecImmediate(StartTransaction(TPB,taCommit),sql,aSQLDialect);
322     end;
323    
324 tony 56 procedure TFBAttachment.ExecImmediate(transaction: ITransaction; sql: AnsiString);
325 tony 45 begin
326     ExecImmediate(transaction,sql,FSQLDialect);
327     end;
328    
329 tony 56 procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString);
330 tony 45 begin
331     ExecImmediate(StartTransaction(TPB,taCommit),sql,FSQLDialect);
332     end;
333    
334 tony 56 function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
335 tony 45 SQLDialect: integer; params: array of const): IResults;
336     begin
337     Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,FSQLDialect,params);
338     end;
339    
340 tony 56 function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
341 tony 45 SQLDialect: integer; params: array of const): IResults;
342     begin
343     with Prepare(transaction,sql,SQLDialect) do
344     begin
345     SetParameters(SQLParams,params);
346     Result := Execute;
347     end;
348     end;
349    
350 tony 56 function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
351 tony 45 params: array of const): IResults;
352     begin
353     Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,params);
354     end;
355    
356 tony 56 function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
357 tony 45 params: array of const): IResults;
358     begin
359     with Prepare(transaction,sql,FSQLDialect) do
360     begin
361     SetParameters(SQLParams,params);
362     Result := Execute;
363     end;
364     end;
365    
366 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
367 tony 45 aSQLDialect: integer): IResultSet;
368     begin
369     Result := OpenCursor(transaction,sql,aSQLDialect,[]);
370     end;
371    
372 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
373 tony 45 aSQLDialect: integer; params: array of const): IResultSet;
374     var Statement: IStatement;
375     begin
376     CheckHandle;
377     Statement := Prepare(transaction,sql,aSQLDialect);
378     SetParameters(Statement.SQLParams,params);
379     Result := Statement.OpenCursor;
380     end;
381    
382 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString
383 tony 45 ): IResultSet;
384     begin
385     Result := OpenCursor(transaction,sql,FSQLDialect,[]);
386     end;
387    
388 tony 56 function TFBAttachment.OpenCursor(transaction: ITransaction; sql: AnsiString;
389 tony 45 params: array of const): IResultSet;
390     begin
391     Result := OpenCursor(transaction,sql,FSQLDialect,params);
392     end;
393    
394     function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
395 tony 56 sql: AnsiString; aSQLDialect: integer): IResultSet;
396 tony 45 begin
397     Result := OpenCursor(transaction,sql,aSQLDialect,[]);
398     Result.FetchNext;
399     end;
400    
401     function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
402 tony 56 sql: AnsiString; aSQLDialect: integer; params: array of const): IResultSet;
403 tony 45 begin
404     Result := OpenCursor(transaction,sql,aSQLDialect,params);
405     Result.FetchNext;
406     end;
407    
408 tony 56 function TFBAttachment.OpenCursorAtStart(transaction: ITransaction; sql: AnsiString
409 tony 45 ): IResultSet;
410     begin
411     Result := OpenCursorAtStart(transaction,sql,FSQLDialect,[]);
412     end;
413    
414     function TFBAttachment.OpenCursorAtStart(transaction: ITransaction;
415 tony 56 sql: AnsiString; params: array of const): IResultSet;
416 tony 45 begin
417     Result := OpenCursorAtStart(transaction,sql,FSQLDialect,params);
418     end;
419    
420 tony 56 function TFBAttachment.OpenCursorAtStart(sql: AnsiString): IResultSet;
421 tony 45 begin
422     Result := OpenCursorAtStart(sql,[]);
423     end;
424    
425 tony 56 function TFBAttachment.OpenCursorAtStart(sql: AnsiString;
426 tony 45 params: array of const): IResultSet;
427     begin
428     Result := OpenCursorAtStart(StartTransaction([isc_tpb_read,isc_tpb_wait,isc_tpb_concurrency],taCommit),sql,FSQLDialect,params);
429     end;
430    
431 tony 56 function TFBAttachment.Prepare(transaction: ITransaction; sql: AnsiString
432 tony 45 ): IStatement;
433     begin
434     Result := Prepare(transaction,sql,FSQLDialect);
435     end;
436    
437     function TFBAttachment.PrepareWithNamedParameters(transaction: ITransaction;
438 tony 56 sql: AnsiString; GenerateParamNames: boolean): IStatement;
439 tony 45 begin
440     Result := PrepareWithNamedParameters(transaction,sql,FSQLDialect,GenerateParamNames);
441     end;
442    
443 tony 56 function TFBAttachment.GetEventHandler(Event: AnsiString): IEvents;
444 tony 45 var S: TStringList;
445     begin
446     S := TStringList.Create;
447     try
448     S.Add(Event);
449     Result := GetEventHandler(S);
450     finally
451     S.Free;
452     end;
453     end;
454    
455     function TFBAttachment.GetSQLDialect: integer;
456     begin
457     Result := FSQLDialect;
458     end;
459    
460     function TFBAttachment.OpenBlob(transaction: ITransaction; Field: ISQLData;
461     BPB: IBPB): IBlob;
462     begin
463     Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
464     end;
465    
466 tony 60 function TFBAttachment.GetCharsetName(CharSetID: integer): AnsiString;
467     var i: integer;
468     begin
469     Result := '';
470     if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
471     (CharSetMap[CharSetID].CharSetID = CharSetID) then
472     begin
473     Result := CharSetMap[CharSetID].CharSetName;
474     Exit;
475     end;
476    
477     for i := 0 to Length(FUserCharSetMap) - 1 do
478     if FUserCharSetMap[i].CharSetID = CharSetID then
479     begin
480     Result := FUserCharSetMap[i].CharSetName;
481     Exit;
482     end;
483     end;
484    
485     function TFBAttachment.CharSetID2CodePage(CharSetID: integer;
486     var CodePage: TSystemCodePage): boolean;
487     var i: integer;
488     begin
489     Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
490     and (CharSetMap[CharSetID].CharSetID = CharSetID);
491     if Result then
492     begin
493     CodePage := CharSetMap[CharSetID].CodePage;
494     Result := true;
495     Exit;
496     end;
497    
498     for i := 0 to Length(FUserCharSetMap) - 1 do
499     if FUserCharSetMap[i].CharSetID = CharSetID then
500     begin
501     CodePage := FUserCharSetMap[i].CodePage;
502     Result := true;
503     Exit;
504     end;
505     end;
506    
507     function TFBAttachment.CodePage2CharSetID(CodePage: TSystemCodePage;
508     var CharSetID: integer): boolean;
509     var i: integer;
510     begin
511     Result := false;
512     for i := Low(CharSetMap) to High(CharSetMap) do
513     if (CharSetMap[i].AllowReverseLookup) and (CharSetMap[i].CodePage = CodePage) then
514     begin
515     CharSetID := CharSetMap[i].CharSetID;
516     Result := true;
517     Exit;
518     end;
519    
520     for i := 0 to Length(FUserCharSetMap) - 1 do
521     if (FUserCharSetMap[i].AllowReverseLookup) and (FUserCharSetMap[i].CodePage = CodePage) then
522     begin
523     CharSetID := FUserCharSetMap[i].CharSetID;
524     Result := true;
525     Exit;
526     end;
527     end;
528    
529     function TFBAttachment.CharSetName2CharSetID(CharSetName: AnsiString;
530     var CharSetID: integer): boolean;
531     var i: integer;
532     begin
533     Result := false;
534     for i := Low(CharSetMap) to High(CharSetMap) do
535     if AnsiCompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then
536     begin
537     CharSetID := CharSetMap[i].CharSetID;
538     Result := true;
539     Exit;
540     end;
541    
542     for i := 0 to Length(FUserCharSetMap) - 1 do
543     if AnsiCompareStr(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then
544     begin
545     CharSetID := FUserCharSetMap[i].CharSetID;
546     Result := true;
547     Exit;
548     end;
549     end;
550    
551     function TFBAttachment.CharSetWidth(CharSetID: integer; var Width: integer
552     ): boolean;
553     var i: integer;
554     begin
555     Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
556     and (CharSetMap[CharSetID].CharSetID = CharSetID);
557     if Result then
558     begin
559     Width := CharSetMap[CharSetID].CharSetWidth;
560     Result := true;
561     Exit;
562     end;
563    
564     for i := 0 to Length(FUserCharSetMap) - 1 do
565     if FUserCharSetMap[i].CharSetID = CharSetID then
566     begin
567     Width := FUserCharSetMap[i].CharSetWidth;
568     Result := true;
569     Exit;
570     end;
571     end;
572    
573     const
574     sqlLookupCharSet = 'Select RDB$CHARACTER_SET_ID, RDB$BYTES_PER_CHARACTER From RDB$CHARACTER_SETS '+
575     'Where RDB$SYSTEM_FLAG = 0 and RDB$CHARACTER_SET_NAME = UPPER(?)';
576    
577     procedure TFBAttachment.RegisterCharSet(CharSetName: AnsiString;
578     CodePage: TSystemCodePage; AllowReverseLookup: boolean; out CharSetID: integer
579     );
580     var CharSets: IResultSet;
581     idx: integer;
582     begin
583     if CharSetName2CharSetID(CharSetName,CharSetID) then
584     IBError(ibxeCharacterSetExists,[CharSetName]);
585    
586     CharSets := OpenCursorAtStart(sqlLookupCharSet,[CharSetName]);
587     if CharSets.IsEof then
588     IBError(ibxeUnknownUserCharSet,[CharSetName]);
589    
590     idx := Length(FUserCharSetMap);
591     SetLength(FUserCharSetMap,idx+1);
592     FUserCharSetMap[idx].AllowReverseLookup := AllowReverseLookup;
593     FUserCharSetMap[idx].CharSetID := CharSets[0].AsInteger;
594     FUserCharSetMap[idx].CharSetName := AnsiUpperCase(CharSetName);
595     FUserCharSetMap[idx].CharSetWidth := CharSets[1].AsInteger;
596     FUserCharSetMap[idx].CodePage := CodePage;
597     CharSetID := CharSets[0].AsInteger;
598     end;
599    
600 tony 45 end.
601