ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/journaling/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 120 by tony, Mon Jan 22 13:58:20 2018 UTC

# Line 32 | Line 32 | unit FBAttachment;
32   {$IFDEF FPC}
33   {$mode delphi}
34   {$interfaces COM}
35 + {$define HASREQEX}
36   {$ENDIF}
37  
38   interface
39  
40   uses
41 <  Classes, SysUtils, IB,  FBParamBlock, FBActivityMonitor;
41 >  Classes, SysUtils, {$IFDEF WINDOWS} windows, {$ENDIF} IB,  FBParamBlock, FBActivityMonitor;
42  
43   type
44 +  TCharsetMap = record
45 +    CharsetID: integer;
46 +    CharSetName: AnsiString;
47 +    CharSetWidth: integer;
48 +    CodePage: TSystemCodePage;
49 +    AllowReverseLookup: boolean; {used to ensure that lookup of CP_UTF* does not return UNICODE_FSS}
50 +  end;
51  
52    { TFBAttachment }
53  
# Line 47 | Line 55 | type
55    private
56      FDPB: IDPB;
57      FFirebirdAPI: IFirebirdAPI;
58 +    FODSMajorVersion: integer;
59 +    FODSMinorVersion: integer;
60 +    FUserCharSetMap: array of TCharSetMap;
61    protected
62      FDatabaseName: AnsiString;
63      FRaiseExceptionOnConnectError: boolean;
# Line 54 | Line 65 | type
65      FHasDefaultCharSet: boolean;
66      FCharSetID: integer;
67      FCodePage: TSystemCodePage;
68 +    FRemoteProtocol: AnsiString;
69      constructor Create(DatabaseName: AnsiString; DPB: IDPB;
70        RaiseExceptionOnConnectError: boolean);
71      procedure CheckHandle; virtual; abstract;
72      function GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString;
73 +    procedure GetODSAndConnectionInfo;
74 +    function IsConnected: boolean; virtual; abstract;
75      procedure EndAllTransactions;
76 +    procedure DPBFromCreateSQL(CreateSQL: AnsiString);
77      procedure SetParameters(SQLParams: ISQLParams; params: array of const);
78    public
79      destructor Destroy; override;
# Line 103 | Line 118 | type
118      function OpenBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BlobID: TISC_QUAD; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
119      function OpenBlob(transaction: ITransaction; Field: ISQLData; BPB: IBPB=nil): IBlob; overload;
120      property SQLDialect: integer read FSQLDialect;
106    property HasDefaultCharSet: boolean read FHasDefaultCharSet;
107    property CharSetID: integer read FCharSetID;
108    property CodePage: TSystemCodePage read FCodePage;
121      property DPB: IDPB read FDPB;
122 + public
123 +  function GetDBInformation(Requests: array of byte): IDBInformation; overload; virtual; abstract;
124 +  function GetDBInformation(Request: byte): IDBInformation; overload; virtual; abstract;
125 +  function GetConnectString: AnsiString;
126 +  function GetRemoteProtocol: AnsiString;
127 +  function GetODSMajorVersion: integer;
128 +  function GetODSMinorVersion: integer;
129 +  {Character Sets}
130 +  function HasDefaultCharSet: boolean;
131 +  function GetDefaultCharSetID: integer;
132 +  function GetCharsetName(CharSetID: integer): AnsiString;
133 +  function CharSetID2CodePage(CharSetID: integer; var CodePage: TSystemCodePage): boolean;
134 +  function CodePage2CharSetID(CodePage: TSystemCodePage; var CharSetID: integer): boolean;
135 +  function CharSetName2CharSetID(CharSetName: AnsiString; var CharSetID: integer): boolean;
136 +  function CharSetWidth(CharSetID: integer; var Width: integer): boolean;
137 +  procedure RegisterCharSet(CharSetName: AnsiString; CodePage: TSystemCodePage;
138 +    AllowReverseLookup:boolean; out CharSetID: integer);
139 +  property CharSetID: integer read FCharSetID;
140 +  property CodePage: TSystemCodePage read FCodePage;
141    end;
142  
143   implementation
144  
145 < uses FBMessages, FBTransaction;
145 > uses FBMessages, FBTransaction {$IFDEF HASREQEX}, RegExpr{$ENDIF};
146 >
147 > const
148 >  CharSetMap: array [0..69] of TCharsetMap = (
149 >  (CharsetID: 0; CharSetName: 'NONE'; CharSetWidth: 1; CodePage: CP_ACP; AllowReverseLookup: true),
150 >  (CharsetID: 1; CharSetName: 'OCTETS'; CharSetWidth: 1; CodePage: CP_NONE; AllowReverseLookup: true),
151 >  (CharsetID: 2; CharSetName: 'ASCII'; CharSetWidth: 1; CodePage: CP_ASCII; AllowReverseLookup: true),
152 >  (CharsetID: 3; CharSetName: 'UNICODE_FSS'; CharSetWidth: 3; CodePage: CP_UTF8; AllowReverseLookup: false),
153 >  (CharsetID: 4; CharSetName: 'UTF8'; CharSetWidth: 4; CodePage: CP_UTF8; AllowReverseLookup: true),
154 >  (CharsetID: 5; CharSetName: 'SJIS_0208'; CharSetWidth: 2; CodePage: 20932; AllowReverseLookup: true),
155 >  (CharsetID: 6; CharSetName: 'EUCJ_0208'; CharSetWidth: 2; CodePage: 20932; AllowReverseLookup: true),
156 >  (CharsetID: 7; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
157 >  (CharsetID: 8; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
158 >  (CharsetID: 9; CharSetName: 'DOS737'; CharSetWidth: 1; CodePage: 737; AllowReverseLookup: true),
159 >  (CharsetID: 10; CharSetName: 'DOS437'; CharSetWidth: 1; CodePage: 437; AllowReverseLookup: true),
160 >  (CharsetID: 11; CharSetName: 'DOS850'; CharSetWidth: 1; CodePage: 850; AllowReverseLookup: true),
161 >  (CharsetID: 12; CharSetName: 'DOS865'; CharSetWidth: 1; CodePage: 865; AllowReverseLookup: true),
162 >  (CharsetID: 13; CharSetName: 'DOS860'; CharSetWidth: 1; CodePage: 860; AllowReverseLookup: true),
163 >  (CharsetID: 14; CharSetName: 'DOS863'; CharSetWidth: 1; CodePage: 863; AllowReverseLookup: true),
164 >  (CharsetID: 15; CharSetName: 'DOS775'; CharSetWidth: 1; CodePage: 775; AllowReverseLookup: true),
165 >  (CharsetID: 16; CharSetName: 'DOS858'; CharSetWidth: 1; CodePage: 858; AllowReverseLookup: true),
166 >  (CharsetID: 17; CharSetName: 'DOS862'; CharSetWidth: 1; CodePage: 862; AllowReverseLookup: true),
167 >  (CharsetID: 18; CharSetName: 'DOS864'; CharSetWidth: 1; CodePage: 864; AllowReverseLookup: true),
168 >  (CharsetID: 19; CharSetName: 'NEXT'; CharSetWidth: 1; CodePage: CP_NONE; AllowReverseLookup: true),
169 >  (CharsetID: 20; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: true),
170 >  (CharsetID: 21; CharSetName: 'ISO8859_1'; CharSetWidth: 1; CodePage: 28591; AllowReverseLookup: true),
171 >  (CharsetID: 22; CharSetName: 'ISO8859_2'; CharSetWidth: 1; CodePage: 28592; AllowReverseLookup: true),
172 >  (CharsetID: 23; CharSetName: 'ISO8859_3'; CharSetWidth: 1; CodePage: 28593; AllowReverseLookup: true),
173 >  (CharsetID: 24; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
174 >  (CharsetID: 25; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
175 >  (CharsetID: 26; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
176 >  (CharsetID: 27; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
177 >  (CharsetID: 28; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
178 >  (CharsetID: 29; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
179 >  (CharsetID: 30; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
180 >  (CharsetID: 31; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
181 >  (CharsetID: 32; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
182 >  (CharsetID: 33; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
183 >  (CharsetID: 34; CharSetName: 'ISO8859_4'; CharSetWidth: 1; CodePage: 28594; AllowReverseLookup: true),
184 >  (CharsetID: 35; CharSetName: 'ISO8859_5'; CharSetWidth: 1; CodePage: 28595; AllowReverseLookup: true),
185 >  (CharsetID: 36; CharSetName: 'ISO8859_6'; CharSetWidth: 1; CodePage: 28596; AllowReverseLookup: true),
186 >  (CharsetID: 37; CharSetName: 'ISO8859_7'; CharSetWidth: 1; CodePage: 28597; AllowReverseLookup: true),
187 >  (CharsetID: 38; CharSetName: 'ISO8859_8'; CharSetWidth: 1; CodePage: 28598; AllowReverseLookup: true),
188 >  (CharsetID: 39; CharSetName: 'ISO8859_9'; CharSetWidth: 1; CodePage: 28599; AllowReverseLookup: true),
189 >  (CharsetID: 40; CharSetName: 'ISO8859_13'; CharSetWidth: 1; CodePage: 28603; AllowReverseLookup: true),
190 >  (CharsetID: 41; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
191 >  (CharsetID: 42; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
192 >  (CharsetID: 43; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
193 >  (CharsetID: 44; CharSetName: 'KSC_5601'; CharSetWidth: 2; CodePage: 949; AllowReverseLookup: true),
194 >  (CharsetID: 45; CharSetName: 'DOS852'; CharSetWidth: 1; CodePage: 852; AllowReverseLookup: true),
195 >  (CharsetID: 46; CharSetName: 'DOS857'; CharSetWidth: 1; CodePage: 857; AllowReverseLookup: true),
196 >  (CharsetID: 47; CharSetName: 'DOS861'; CharSetWidth: 1; CodePage: 861; AllowReverseLookup: true),
197 >  (CharsetID: 48; CharSetName: 'DOS866'; CharSetWidth: 1; CodePage: 866; AllowReverseLookup: true),
198 >  (CharsetID: 49; CharSetName: 'DOS869'; CharSetWidth: 1; CodePage: 869; AllowReverseLookup: true),
199 >  (CharsetID: 50; CharSetName: 'CYRL'; CharSetWidth: 1; CodePage: 1251; AllowReverseLookup: true),
200 >  (CharsetID: 51; CharSetName: 'WIN1250'; CharSetWidth: 1; CodePage: 1250; AllowReverseLookup: true),
201 >  (CharsetID: 52; CharSetName: 'WIN1251'; CharSetWidth: 1; CodePage: 1251; AllowReverseLookup: true),
202 >  (CharsetID: 53; CharSetName: 'WIN1252'; CharSetWidth: 1; CodePage: 1252; AllowReverseLookup: true),
203 >  (CharsetID: 54; CharSetName: 'WIN1253'; CharSetWidth: 1; CodePage: 1253; AllowReverseLookup: true),
204 >  (CharsetID: 55; CharSetName: 'WIN1254'; CharSetWidth: 1; CodePage: 1254; AllowReverseLookup: true),
205 >  (CharsetID: 56; CharSetName: 'BIG_5'; CharSetWidth: 2; CodePage: 950; AllowReverseLookup: true),
206 >  (CharsetID: 57; CharSetName: 'GB_2312'; CharSetWidth: 2; CodePage: 936; AllowReverseLookup: true),
207 >  (CharsetID: 58; CharSetName: 'WIN1255'; CharSetWidth: 1; CodePage: 1255; AllowReverseLookup: true),
208 >  (CharsetID: 59; CharSetName: 'WIN1256'; CharSetWidth: 1; CodePage: 1256; AllowReverseLookup: true),
209 >  (CharsetID: 60; CharSetName: 'WIN1257'; CharSetWidth: 1; CodePage: 1257; AllowReverseLookup: true),
210 >  (CharsetID: 61; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
211 >  (CharsetID: 62; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE; AllowReverseLookup: false),
212 >  (CharsetID: 63; CharSetName: 'KOI8R'; CharSetWidth: 1; CodePage: 20866; AllowReverseLookup: true),
213 >  (CharsetID: 64; CharSetName: 'KOI8U'; CharSetWidth: 1; CodePage: 21866; AllowReverseLookup: true),
214 >  (CharsetID: 65; CharSetName: 'WIN1258'; CharSetWidth: 1; CodePage: 1258; AllowReverseLookup: true),
215 >  (CharsetID: 66; CharSetName: 'TIS620'; CharSetWidth: 1; CodePage: 874; AllowReverseLookup: true),
216 >  (CharsetID: 67; CharSetName: 'GBK'; CharSetWidth: 2; CodePage: 936; AllowReverseLookup: true),
217 >  (CharsetID: 68; CharSetName: 'CP943C'; CharSetWidth: 2; CodePage: 943; AllowReverseLookup: true),
218 >  (CharsetID: 69; CharSetName: 'GB18030'; CharSetWidth: 4; CodePage: 54936; AllowReverseLookup: true)
219 > );
220 >
221 >
222 >
223  
224   { TFBAttachment }
225  
226 + procedure TFBAttachment.GetODSAndConnectionInfo;
227 + var DBInfo: IDBInformation;
228 +    i: integer;
229 +    Stmt: IStatement;
230 +    ResultSet: IResultSet;
231 +    Param: IDPBItem;
232 + begin
233 +  if not IsConnected then Exit;
234 +  DBInfo := GetDBInformation([isc_info_db_id,isc_info_ods_version,isc_info_ods_minor_version,
235 +                               isc_info_db_SQL_Dialect]);
236 +  for i := 0 to DBInfo.GetCount - 1 do
237 +    with DBInfo[i] do
238 +      case getItemType of
239 +      isc_info_ods_minor_version:
240 +        FODSMinorVersion := getAsInteger;
241 +      isc_info_ods_version:
242 +        FODSMajorVersion := getAsInteger;
243 +      isc_info_db_SQL_Dialect:
244 +        FSQLDialect := getAsInteger;
245 +      end;
246 +
247 +  if (FODSMajorVersion > 11) or ((FODSMajorVersion = 11) and (FODSMinorVersion >= 1)) then
248 +  begin
249 +    Stmt := Prepare(StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit),
250 +                    'Select MON$CHARACTER_SET_ID, MON$REMOTE_PROTOCOL From MON$ATTACHMENTS '+
251 +                    'Where MON$ATTACHMENT_ID = CURRENT_CONNECTION');
252 +    ResultSet := Stmt.OpenCursor;
253 +    if ResultSet.FetchNext then
254 +    begin
255 +      FCharSetID := ResultSet[0].AsInteger;
256 +      FRemoteProtocol := ResultSet[1].AsString;
257 +    end
258 +  end
259 +  else
260 +  if DPB <> nil then
261 +  begin
262 +    Param :=  DPB.Find(isc_dpb_lc_ctype);
263 +    if (Param = nil) or not CharSetName2CharSetID(Param.AsString,FCharSetID) then
264 +      FCharSetID := 0;
265 +      FRemoteProtocol := '';
266 +  end
267 +  else
268 +  begin
269 +    FCharSetID := 0;
270 +    FRemoteProtocol := '';
271 +  end;
272 +  FHasDefaultCharSet := CharSetID2CodePage(FCharSetID,FCodePage) and (FCharSetID > 1);
273 + end;
274 +
275   constructor TFBAttachment.Create(DatabaseName: AnsiString; DPB: IDPB;
276    RaiseExceptionOnConnectError: boolean);
277   begin
# Line 123 | Line 280 | begin
280    FSQLDialect := 3;
281    FDatabaseName := DatabaseName;
282    FDPB := DPB;
283 +  SetLength(FUserCharSetMap,0);
284    FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError;
285 +  FODSMajorVersion := 0;
286 +  FODSMinorVersion := 0;
287   end;
288  
289   function TFBAttachment.GenerateCreateDatabaseSQL(DatabaseName: AnsiString;  aDPB: IDPB): AnsiString;
# Line 170 | Line 330 | begin
330    end;
331   end;
332  
333 + {$IFDEF HASREQEX}
334 + procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString);
335 + var RegexObj: TRegExpr;
336 + begin
337 +  FDPB := FFirebirdAPI.AllocateDPB;
338 +  RegexObj := TRegExpr.Create;
339 +  try
340 +    {extact database file spec}
341 +    RegexObj.ModifierG := false; {turn off greedy matches}
342 +    RegexObj.ModifierI := true; {case insensitive match}
343 +    RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +''.*'' +USER +''(.+)'' PASSWORD +''(.+)''';
344 +    if RegexObj.Exec(CreateSQL) then
345 +    begin
346 +      DPB.Add(isc_dpb_user_name).AsString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]);
347 +      DPB.Add(isc_dpb_password).AsString := system.copy(CreateSQL,RegexObj.MatchPos[3],RegexObj.MatchLen[3]);
348 +    end
349 +    else
350 +    begin
351 +      RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +(''.*'') +USER +''(.+)''';
352 +      if RegexObj.Exec(CreateSQL) then
353 +        DPB.Add(isc_dpb_user_name).AsString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]);
354 +    end;
355 +  finally
356 +    RegexObj.Free;
357 +  end;
358 +  if FCharSetID > 0 then
359 +    DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID);
360 +  DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect);
361 + end;
362 + {$ELSE}
363 + procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString);
364 + begin
365 +  FDPB := FFirebirdAPI.AllocateDPB;
366 +  if FCharSetID > 0 then
367 +    DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID);
368 +  DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect);
369 + end;
370 + {$ENDIF}
371 +
372   procedure TFBAttachment.SetParameters(SQLParams: ISQLParams;
373    params: array of const);
374   var i: integer;
# Line 182 | Line 381 | begin
381      case params[i].vtype of
382        vtinteger    :
383          SQLParams[i].AsInteger := params[i].vinteger;
384 +      vtInt64:
385 +        SQLParams[i].AsInt64 := params[i].VInt64^;
386 +      {$IF declared (vtQWord)}
387 +      vtQWord:
388 +        SQLParams[i].AsInt64 := params[i].VQWord^;
389 +      {$IFEND}
390        vtboolean    :
391          SQLParams[i].AsBoolean :=  params[i].vboolean;
392        vtchar       :
# Line 191 | Line 396 | begin
396        vtCurrency:
397          SQLParams[i].AsDouble := params[i].VCurrency^;
398        vtString     :
399 <        SQLParams[i].AsString := params[i].VString^;
399 >        SQLParams[i].AsString := strpas(PChar(params[i].VString));
400        vtPChar      :
401          SQLParams[i].AsString := strpas(params[i].VPChar);
402        vtAnsiString :
403 <        SQLParams[i].AsString := AnsiString(params[i].VAnsiString^);
403 >        SQLParams[i].AsString := strpas(PAnsiChar(params[i].VAnsiString));
404        vtVariant:
405          SQLParams[i].AsVariant := params[i].VVariant^;
406 +      vtWideChar:
407 +        SQLParams[i].AsString := UTF8Encode(WideCharLenToString(@params[i].VWideChar,1));
408 +      vtPWideChar:
409 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VPWideChar)));
410 +      vtWideString:
411 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VWideString)));
412 +      vtUnicodeString:
413 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VUnicodeString)));
414      else
415          IBError(ibxeInvalidVariantType,[nil]);
416      end;
# Line 239 | Line 452 | end;
452   function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString;
453    SQLDialect: integer; params: array of const): IResults;
454   begin
455 <  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,FSQLDialect,params);
455 >  Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params);
456   end;
457  
458   function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString;
# Line 368 | Line 581 | begin
581    Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
582   end;
583  
584 + function TFBAttachment.GetConnectString: AnsiString;
585 + begin
586 +  Result := FDatabaseName;
587 + end;
588 +
589 + function TFBAttachment.GetRemoteProtocol: AnsiString;
590 + begin
591 +  Result := FRemoteProtocol;
592 + end;
593 +
594 + function TFBAttachment.GetODSMajorVersion: integer;
595 + begin
596 +  Result := FODSMajorVersion;
597 + end;
598 +
599 + function TFBAttachment.GetODSMinorVersion: integer;
600 + begin
601 +  Result := FODSMinorVersion;
602 + end;
603 +
604 + function TFBAttachment.HasDefaultCharSet: boolean;
605 + begin
606 +  Result := FHasDefaultCharSet
607 + end;
608 +
609 + function TFBAttachment.GetDefaultCharSetID: integer;
610 + begin
611 +  Result := FCharsetID;
612 + end;
613 +
614 + function TFBAttachment.GetCharsetName(CharSetID: integer): AnsiString;
615 + var i: integer;
616 + begin
617 +  Result := '';
618 +  if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
619 +                                  (CharSetMap[CharSetID].CharSetID = CharSetID) then
620 +    begin
621 +      Result := CharSetMap[CharSetID].CharSetName;
622 +      Exit;
623 +    end;
624 +
625 +  for i := 0 to Length(FUserCharSetMap) - 1 do
626 +    if FUserCharSetMap[i].CharSetID = CharSetID then
627 +    begin
628 +      Result := FUserCharSetMap[i].CharSetName;
629 +      Exit;
630 +    end;
631 + end;
632 +
633 + function TFBAttachment.CharSetID2CodePage(CharSetID: integer;
634 +  var CodePage: TSystemCodePage): boolean;
635 + var i: integer;
636 + begin
637 +  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
638 +               and (CharSetMap[CharSetID].CharSetID = CharSetID);
639 +  if Result then
640 +    begin
641 +      CodePage := CharSetMap[CharSetID].CodePage;
642 +      Result := true;
643 +      Exit;
644 +    end;
645 +
646 +  for i := 0 to Length(FUserCharSetMap) - 1 do
647 +    if FUserCharSetMap[i].CharSetID = CharSetID then
648 +    begin
649 +      CodePage := FUserCharSetMap[i].CodePage;
650 +      Result := true;
651 +      Exit;
652 +    end;
653 + end;
654 +
655 + function TFBAttachment.CodePage2CharSetID(CodePage: TSystemCodePage;
656 +  var CharSetID: integer): boolean;
657 + var i: integer;
658 + begin
659 +  Result := false;
660 +  for i := Low(CharSetMap) to High(CharSetMap) do
661 +    if (CharSetMap[i].AllowReverseLookup) and (CharSetMap[i].CodePage = CodePage) then
662 +    begin
663 +      CharSetID := CharSetMap[i].CharSetID;
664 +      Result := true;
665 +      Exit;
666 +    end;
667 +
668 +  for i := 0 to Length(FUserCharSetMap) - 1 do
669 +    if (FUserCharSetMap[i].AllowReverseLookup) and (FUserCharSetMap[i].CodePage = CodePage) then
670 +    begin
671 +      CharSetID := FUserCharSetMap[i].CharSetID;
672 +      Result := true;
673 +      Exit;
674 +    end;
675 + end;
676 +
677 + function TFBAttachment.CharSetName2CharSetID(CharSetName: AnsiString;
678 +  var CharSetID: integer): boolean;
679 + var i: integer;
680 + begin
681 +  Result := false;
682 +  for i := Low(CharSetMap) to High(CharSetMap) do
683 +    if AnsiCompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then
684 +    begin
685 +      CharSetID := CharSetMap[i].CharSetID;
686 +      Result := true;
687 +      Exit;
688 +    end;
689 +
690 +    for i := 0 to Length(FUserCharSetMap) - 1 do
691 +      if AnsiCompareStr(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then
692 +      begin
693 +        CharSetID := FUserCharSetMap[i].CharSetID;
694 +        Result := true;
695 +        Exit;
696 +      end;
697 + end;
698 +
699 + function TFBAttachment.CharSetWidth(CharSetID: integer; var Width: integer
700 +  ): boolean;
701 + var i: integer;
702 + begin
703 +  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
704 +               and (CharSetMap[CharSetID].CharSetID = CharSetID);
705 +  if Result then
706 +    begin
707 +      Width := CharSetMap[CharSetID].CharSetWidth;
708 +      Result := true;
709 +      Exit;
710 +    end;
711 +
712 +  for i := 0 to Length(FUserCharSetMap) - 1 do
713 +    if FUserCharSetMap[i].CharSetID = CharSetID then
714 +    begin
715 +      Width := FUserCharSetMap[i].CharSetWidth;
716 +      Result := true;
717 +      Exit;
718 +    end;
719 + end;
720 +
721 + const
722 +  sqlLookupCharSet = 'Select RDB$CHARACTER_SET_ID, RDB$BYTES_PER_CHARACTER From RDB$CHARACTER_SETS '+
723 +                     'Where RDB$SYSTEM_FLAG = 0 and RDB$CHARACTER_SET_NAME = UPPER(?)';
724 +
725 + procedure TFBAttachment.RegisterCharSet(CharSetName: AnsiString;
726 +  CodePage: TSystemCodePage; AllowReverseLookup: boolean; out CharSetID: integer
727 +  );
728 + var CharSets: IResultSet;
729 +    idx: integer;
730 + begin
731 +  if CharSetName2CharSetID(CharSetName,CharSetID) then
732 +    IBError(ibxeCharacterSetExists,[CharSetName]);
733 +
734 +  CharSets := OpenCursorAtStart(sqlLookupCharSet,[CharSetName]);
735 +  if CharSets.IsEof then
736 +    IBError(ibxeUnknownUserCharSet,[CharSetName]);
737 +
738 +  idx := Length(FUserCharSetMap);
739 +  SetLength(FUserCharSetMap,idx+1);
740 +  FUserCharSetMap[idx].AllowReverseLookup := AllowReverseLookup;
741 +  FUserCharSetMap[idx].CharSetID := CharSets[0].AsInteger;
742 +  FUserCharSetMap[idx].CharSetName := AnsiUpperCase(CharSetName);
743 +  FUserCharSetMap[idx].CharSetWidth := CharSets[1].AsInteger;
744 +  FUserCharSetMap[idx].CodePage := CodePage;
745 +  CharSetID := CharSets[0].AsInteger;
746 + end;
747 +
748   end.
749  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines