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 118 by tony, Mon Jan 22 13:58:14 2018 UTC

# Line 27 | Line 27
27   unit FBAttachment;
28   {$IFDEF MSWINDOWS}
29   {$DEFINE WINDOWS}
30 + {$IF 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 + end;
369 + {$ENDIF}
370 +
371   procedure TFBAttachment.SetParameters(SQLParams: ISQLParams;
372    params: array of const);
373   var i: integer;
# Line 182 | Line 380 | begin
380      case params[i].vtype of
381        vtinteger    :
382          SQLParams[i].AsInteger := params[i].vinteger;
383 +      vtInt64:
384 +        SQLParams[i].AsInt64 := params[i].VInt64^;
385 +      {$IF declared (vtQWord)}
386 +      vtQWord:
387 +        SQLParams[i].AsInt64 := params[i].VQWord^;
388 +      {$IFEND}
389        vtboolean    :
390          SQLParams[i].AsBoolean :=  params[i].vboolean;
391        vtchar       :
# Line 191 | Line 395 | begin
395        vtCurrency:
396          SQLParams[i].AsDouble := params[i].VCurrency^;
397        vtString     :
398 <        SQLParams[i].AsString := params[i].VString^;
398 >        SQLParams[i].AsString := strpas(PChar(params[i].VString));
399        vtPChar      :
400          SQLParams[i].AsString := strpas(params[i].VPChar);
401        vtAnsiString :
402 <        SQLParams[i].AsString := AnsiString(params[i].VAnsiString^);
402 >        SQLParams[i].AsString := strpas(PAnsiChar(params[i].VAnsiString));
403        vtVariant:
404          SQLParams[i].AsVariant := params[i].VVariant^;
405 +      vtWideChar:
406 +        SQLParams[i].AsString := UTF8Encode(WideCharLenToString(@params[i].VWideChar,1));
407 +      vtPWideChar:
408 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VPWideChar)));
409 +      vtWideString:
410 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VWideString)));
411 +      vtUnicodeString:
412 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VUnicodeString)));
413      else
414          IBError(ibxeInvalidVariantType,[nil]);
415      end;
# Line 239 | Line 451 | end;
451   function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
452    SQLDialect: integer; params: array of const): IResults;
453   begin
454 <  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,FSQLDialect,params);
454 >  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params);
455   end;
456  
457   function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
# Line 368 | Line 580 | begin
580    Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
581   end;
582  
583 + function TFBAttachment.GetConnectString: AnsiString;
584 + begin
585 +  Result := FDatabaseName;
586 + end;
587 +
588 + function TFBAttachment.GetRemoteProtocol: AnsiString;
589 + begin
590 +  Result := FRemoteProtocol;
591 + end;
592 +
593 + function TFBAttachment.GetODSMajorVersion: integer;
594 + begin
595 +  Result := FODSMajorVersion;
596 + end;
597 +
598 + function TFBAttachment.GetODSMinorVersion: integer;
599 + begin
600 +  Result := FODSMinorVersion;
601 + end;
602 +
603 + function TFBAttachment.HasDefaultCharSet: boolean;
604 + begin
605 +  Result := FHasDefaultCharSet
606 + end;
607 +
608 + function TFBAttachment.GetDefaultCharSetID: integer;
609 + begin
610 +  Result := FCharsetID;
611 + end;
612 +
613 + function TFBAttachment.GetCharsetName(CharSetID: integer): AnsiString;
614 + var i: integer;
615 + begin
616 +  Result := '';
617 +  if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
618 +                                  (CharSetMap[CharSetID].CharSetID = CharSetID) then
619 +    begin
620 +      Result := CharSetMap[CharSetID].CharSetName;
621 +      Exit;
622 +    end;
623 +
624 +  for i := 0 to Length(FUserCharSetMap) - 1 do
625 +    if FUserCharSetMap[i].CharSetID = CharSetID then
626 +    begin
627 +      Result := FUserCharSetMap[i].CharSetName;
628 +      Exit;
629 +    end;
630 + end;
631 +
632 + function TFBAttachment.CharSetID2CodePage(CharSetID: integer;
633 +  var CodePage: TSystemCodePage): boolean;
634 + var i: integer;
635 + begin
636 +  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
637 +               and (CharSetMap[CharSetID].CharSetID = CharSetID);
638 +  if Result then
639 +    begin
640 +      CodePage := CharSetMap[CharSetID].CodePage;
641 +      Result := true;
642 +      Exit;
643 +    end;
644 +
645 +  for i := 0 to Length(FUserCharSetMap) - 1 do
646 +    if FUserCharSetMap[i].CharSetID = CharSetID then
647 +    begin
648 +      CodePage := FUserCharSetMap[i].CodePage;
649 +      Result := true;
650 +      Exit;
651 +    end;
652 + end;
653 +
654 + function TFBAttachment.CodePage2CharSetID(CodePage: TSystemCodePage;
655 +  var CharSetID: integer): boolean;
656 + var i: integer;
657 + begin
658 +  Result := false;
659 +  for i := Low(CharSetMap) to High(CharSetMap) do
660 +    if (CharSetMap[i].AllowReverseLookup) and (CharSetMap[i].CodePage = CodePage) then
661 +    begin
662 +      CharSetID := CharSetMap[i].CharSetID;
663 +      Result := true;
664 +      Exit;
665 +    end;
666 +
667 +  for i := 0 to Length(FUserCharSetMap) - 1 do
668 +    if (FUserCharSetMap[i].AllowReverseLookup) and (FUserCharSetMap[i].CodePage = CodePage) then
669 +    begin
670 +      CharSetID := FUserCharSetMap[i].CharSetID;
671 +      Result := true;
672 +      Exit;
673 +    end;
674 + end;
675 +
676 + function TFBAttachment.CharSetName2CharSetID(CharSetName: AnsiString;
677 +  var CharSetID: integer): boolean;
678 + var i: integer;
679 + begin
680 +  Result := false;
681 +  for i := Low(CharSetMap) to High(CharSetMap) do
682 +    if AnsiCompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then
683 +    begin
684 +      CharSetID := CharSetMap[i].CharSetID;
685 +      Result := true;
686 +      Exit;
687 +    end;
688 +
689 +    for i := 0 to Length(FUserCharSetMap) - 1 do
690 +      if AnsiCompareStr(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then
691 +      begin
692 +        CharSetID := FUserCharSetMap[i].CharSetID;
693 +        Result := true;
694 +        Exit;
695 +      end;
696 + end;
697 +
698 + function TFBAttachment.CharSetWidth(CharSetID: integer; var Width: integer
699 +  ): boolean;
700 + var i: integer;
701 + begin
702 +  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
703 +               and (CharSetMap[CharSetID].CharSetID = CharSetID);
704 +  if Result then
705 +    begin
706 +      Width := CharSetMap[CharSetID].CharSetWidth;
707 +      Result := true;
708 +      Exit;
709 +    end;
710 +
711 +  for i := 0 to Length(FUserCharSetMap) - 1 do
712 +    if FUserCharSetMap[i].CharSetID = CharSetID then
713 +    begin
714 +      Width := FUserCharSetMap[i].CharSetWidth;
715 +      Result := true;
716 +      Exit;
717 +    end;
718 + end;
719 +
720 + const
721 +  sqlLookupCharSet = 'Select RDB$CHARACTER_SET_ID, RDB$BYTES_PER_CHARACTER From RDB$CHARACTER_SETS '+
722 +                     'Where RDB$SYSTEM_FLAG = 0 and RDB$CHARACTER_SET_NAME = UPPER(?)';
723 +
724 + procedure TFBAttachment.RegisterCharSet(CharSetName: AnsiString;
725 +  CodePage: TSystemCodePage; AllowReverseLookup: boolean; out CharSetID: integer
726 +  );
727 + var CharSets: IResultSet;
728 +    idx: integer;
729 + begin
730 +  if CharSetName2CharSetID(CharSetName,CharSetID) then
731 +    IBError(ibxeCharacterSetExists,[CharSetName]);
732 +
733 +  CharSets := OpenCursorAtStart(sqlLookupCharSet,[CharSetName]);
734 +  if CharSets.IsEof then
735 +    IBError(ibxeUnknownUserCharSet,[CharSetName]);
736 +
737 +  idx := Length(FUserCharSetMap);
738 +  SetLength(FUserCharSetMap,idx+1);
739 +  FUserCharSetMap[idx].AllowReverseLookup := AllowReverseLookup;
740 +  FUserCharSetMap[idx].CharSetID := CharSets[0].AsInteger;
741 +  FUserCharSetMap[idx].CharSetName := AnsiUpperCase(CharSetName);
742 +  FUserCharSetMap[idx].CharSetWidth := CharSets[1].AsInteger;
743 +  FUserCharSetMap[idx].CodePage := CodePage;
744 +  CharSetID := CharSets[0].AsInteger;
745 + end;
746 +
747   end.
748  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines