ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/client/FBAttachment.pas
(Generate patch)

Comparing ibx/trunk/fbintf/client/FBAttachment.pas (file contents):
Revision 56 by tony, Mon Mar 6 10:20:02 2017 UTC vs.
Revision 119 by tony, Mon Jan 22 13:58:18 2018 UTC

# Line 27 | Line 27
27   unit FBAttachment;
28   {$IFDEF MSWINDOWS}
29   {$DEFINE WINDOWS}
30 + {$IF defined(CompilerVersion) and (CompilerVersion >= 28)}
31 + {Delphi XE7 onwards}}
32 + {$define HASREQEX}
33 + {$IFEND}
34   {$ENDIF}
35  
36   {$IFDEF FPC}
37   {$mode delphi}
38   {$interfaces COM}
39 + {$define HASREQEX}
40   {$ENDIF}
41  
42   interface
43  
44   uses
45 <  Classes, SysUtils, IB,  FBParamBlock, FBActivityMonitor;
45 >  Classes, SysUtils, {$IFDEF WINDOWS} windows, {$ENDIF} IB,  FBParamBlock, FBActivityMonitor;
46  
47   type
48 +  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  
56    { TFBAttachment }
57  
# Line 47 | Line 59 | type
59    private
60      FDPB: IDPB;
61      FFirebirdAPI: IFirebirdAPI;
62 +    FODSMajorVersion: integer;
63 +    FODSMinorVersion: integer;
64 +    FUserCharSetMap: array of TCharSetMap;
65    protected
66      FDatabaseName: AnsiString;
67      FRaiseExceptionOnConnectError: boolean;
# Line 54 | Line 69 | type
69      FHasDefaultCharSet: boolean;
70      FCharSetID: integer;
71      FCodePage: TSystemCodePage;
72 +    FRemoteProtocol: AnsiString;
73      constructor Create(DatabaseName: AnsiString; DPB: IDPB;
74        RaiseExceptionOnConnectError: boolean);
75      procedure CheckHandle; virtual; abstract;
76      function GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
77 +    procedure GetODSAndConnectionInfo;
78 +    function IsConnected: boolean; virtual; abstract;
79      procedure EndAllTransactions;
80 +    procedure DPBFromCreateSQL(CreateSQL: AnsiString);
81      procedure SetParameters(SQLParams: ISQLParams; params: array of const);
82    public
83      destructor Destroy; override;
# Line 103 | Line 122 | type
122      function OpenBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BlobID: TISC_QUAD; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
123      function OpenBlob(transaction: ITransaction; Field: ISQLData; BPB: IBPB=nil): IBlob; overload;
124      property SQLDialect: integer read FSQLDialect;
106    property HasDefaultCharSet: boolean read FHasDefaultCharSet;
107    property CharSetID: integer read FCharSetID;
108    property CodePage: TSystemCodePage read FCodePage;
125      property DPB: IDPB read FDPB;
126 + public
127 +  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 +  function HasDefaultCharSet: boolean;
135 +  function GetDefaultCharSetID: integer;
136 +  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    end;
146  
147   implementation
148  
149 < uses FBMessages, FBTransaction;
149 > uses FBMessages, FBTransaction {$IFDEF HASREQEX}, RegExpr{$ENDIF};
150 >
151 > 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   { TFBAttachment }
229  
230 + 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   constructor TFBAttachment.Create(DatabaseName: AnsiString; DPB: IDPB;
280    RaiseExceptionOnConnectError: boolean);
281   begin
# Line 123 | Line 284 | begin
284    FSQLDialect := 3;
285    FDatabaseName := DatabaseName;
286    FDPB := DPB;
287 +  SetLength(FUserCharSetMap,0);
288    FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError;
289 +  FODSMajorVersion := 0;
290 +  FODSMinorVersion := 0;
291   end;
292  
293   function TFBAttachment.GenerateCreateDatabaseSQL(DatabaseName: AnsiString;  aDPB: IDPB): AnsiString;
# Line 170 | Line 334 | begin
334    end;
335   end;
336  
337 + {$IFDEF HASREQEX}
338 + 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 + {$ELSE}
367 + procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString);
368 + 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 + end;
374 + {$ENDIF}
375 +
376   procedure TFBAttachment.SetParameters(SQLParams: ISQLParams;
377    params: array of const);
378   var i: integer;
# Line 182 | Line 385 | begin
385      case params[i].vtype of
386        vtinteger    :
387          SQLParams[i].AsInteger := params[i].vinteger;
388 +      vtInt64:
389 +        SQLParams[i].AsInt64 := params[i].VInt64^;
390 +      {$IF declared (vtQWord)}
391 +      vtQWord:
392 +        SQLParams[i].AsInt64 := params[i].VQWord^;
393 +      {$IFEND}
394        vtboolean    :
395          SQLParams[i].AsBoolean :=  params[i].vboolean;
396        vtchar       :
# Line 191 | Line 400 | begin
400        vtCurrency:
401          SQLParams[i].AsDouble := params[i].VCurrency^;
402        vtString     :
403 <        SQLParams[i].AsString := params[i].VString^;
403 >        SQLParams[i].AsString := strpas(PChar(params[i].VString));
404        vtPChar      :
405          SQLParams[i].AsString := strpas(params[i].VPChar);
406        vtAnsiString :
407 <        SQLParams[i].AsString := AnsiString(params[i].VAnsiString^);
407 >        SQLParams[i].AsString := strpas(PAnsiChar(params[i].VAnsiString));
408        vtVariant:
409          SQLParams[i].AsVariant := params[i].VVariant^;
410 +      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      else
419          IBError(ibxeInvalidVariantType,[nil]);
420      end;
# Line 239 | Line 456 | end;
456   function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
457    SQLDialect: integer; params: array of const): IResults;
458   begin
459 <  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,FSQLDialect,params);
459 >  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params);
460   end;
461  
462   function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
# Line 368 | Line 585 | begin
585    Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
586   end;
587  
588 + 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 + 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 + 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   end.
753  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines