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 70 by tony, Thu Oct 26 12:59:51 2017 UTC

# Line 37 | Line 37 | unit FBAttachment;
37   interface
38  
39   uses
40 <  Classes, SysUtils, IB,  FBParamBlock, FBActivityMonitor;
40 >  Classes, SysUtils, {$IFDEF WINDOWS} windows, {$ENDIF} IB,  FBParamBlock, FBActivityMonitor;
41  
42   type
43 +  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  
51    { TFBAttachment }
52  
# Line 47 | Line 54 | type
54    private
55      FDPB: IDPB;
56      FFirebirdAPI: IFirebirdAPI;
57 +    FUserCharSetMap: array of TCharSetMap;
58    protected
59      FDatabaseName: AnsiString;
60      FRaiseExceptionOnConnectError: boolean;
# Line 103 | Line 111 | type
111      function OpenBlob(transaction: ITransaction; BlobMetaData: IBlobMetaData; BlobID: TISC_QUAD; BPB: IBPB=nil): IBlob; overload; virtual; abstract;
112      function OpenBlob(transaction: ITransaction; Field: ISQLData; BPB: IBPB=nil): IBlob; overload;
113      property SQLDialect: integer read FSQLDialect;
106    property HasDefaultCharSet: boolean read FHasDefaultCharSet;
107    property CharSetID: integer read FCharSetID;
108    property CodePage: TSystemCodePage read FCodePage;
114      property DPB: IDPB read FDPB;
115 + 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    end;
128  
129   implementation
130  
131   uses FBMessages, FBTransaction;
132  
133 + 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   { TFBAttachment }
211  
212   constructor TFBAttachment.Create(DatabaseName: AnsiString; DPB: IDPB;
# Line 123 | Line 217 | begin
217    FSQLDialect := 3;
218    FDatabaseName := DatabaseName;
219    FDPB := DPB;
220 +  SetLength(FUserCharSetMap,0);
221    FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError;
222   end;
223  
# Line 182 | Line 277 | begin
277      case params[i].vtype of
278        vtinteger    :
279          SQLParams[i].AsInteger := params[i].vinteger;
280 +      vtInt64:
281 +        SQLParams[i].AsInt64 := params[i].VInt64^;
282 +      {$IF declared (vtQWord)}
283 +      vtQWord:
284 +        SQLParams[i].AsInt64 := params[i].VQWord^;
285 +      {$IFEND}
286        vtboolean    :
287          SQLParams[i].AsBoolean :=  params[i].vboolean;
288        vtchar       :
# Line 191 | Line 292 | begin
292        vtCurrency:
293          SQLParams[i].AsDouble := params[i].VCurrency^;
294        vtString     :
295 <        SQLParams[i].AsString := params[i].VString^;
295 >        SQLParams[i].AsString := strpas(PChar(params[i].VString));
296        vtPChar      :
297          SQLParams[i].AsString := strpas(params[i].VPChar);
298        vtAnsiString :
299 <        SQLParams[i].AsString := AnsiString(params[i].VAnsiString^);
299 >        SQLParams[i].AsString := strpas(PAnsiChar(params[i].VAnsiString));
300        vtVariant:
301          SQLParams[i].AsVariant := params[i].VVariant^;
302 +      vtWideChar:
303 +        SQLParams[i].AsString := UTF8Encode(WideCharLenToString(@params[i].VWideChar,1));
304 +      vtPWideChar:
305 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VPWideChar)));
306 +      vtWideString:
307 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VWideString)));
308 +      vtUnicodeString:
309 +        SQLParams[i].AsString := UTF8Encode(strpas(PWideChar(params[i].VUnicodeString)));
310      else
311          IBError(ibxeInvalidVariantType,[nil]);
312      end;
# Line 368 | Line 477 | begin
477    Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB);
478   end;
479  
480 + function TFBAttachment.GetCharsetName(CharSetID: integer): AnsiString;
481 + var i: integer;
482 + begin
483 +  Result := '';
484 +  if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
485 +                                  (CharSetMap[CharSetID].CharSetID = CharSetID) then
486 +    begin
487 +      Result := CharSetMap[CharSetID].CharSetName;
488 +      Exit;
489 +    end;
490 +
491 +  for i := 0 to Length(FUserCharSetMap) - 1 do
492 +    if FUserCharSetMap[i].CharSetID = CharSetID then
493 +    begin
494 +      Result := FUserCharSetMap[i].CharSetName;
495 +      Exit;
496 +    end;
497 + end;
498 +
499 + function TFBAttachment.CharSetID2CodePage(CharSetID: integer;
500 +  var CodePage: TSystemCodePage): boolean;
501 + var i: integer;
502 + begin
503 +  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
504 +               and (CharSetMap[CharSetID].CharSetID = CharSetID);
505 +  if Result then
506 +    begin
507 +      CodePage := CharSetMap[CharSetID].CodePage;
508 +      Result := true;
509 +      Exit;
510 +    end;
511 +
512 +  for i := 0 to Length(FUserCharSetMap) - 1 do
513 +    if FUserCharSetMap[i].CharSetID = CharSetID then
514 +    begin
515 +      CodePage := FUserCharSetMap[i].CodePage;
516 +      Result := true;
517 +      Exit;
518 +    end;
519 + end;
520 +
521 + function TFBAttachment.CodePage2CharSetID(CodePage: TSystemCodePage;
522 +  var CharSetID: integer): boolean;
523 + var i: integer;
524 + begin
525 +  Result := false;
526 +  for i := Low(CharSetMap) to High(CharSetMap) do
527 +    if (CharSetMap[i].AllowReverseLookup) and (CharSetMap[i].CodePage = CodePage) then
528 +    begin
529 +      CharSetID := CharSetMap[i].CharSetID;
530 +      Result := true;
531 +      Exit;
532 +    end;
533 +
534 +  for i := 0 to Length(FUserCharSetMap) - 1 do
535 +    if (FUserCharSetMap[i].AllowReverseLookup) and (FUserCharSetMap[i].CodePage = CodePage) then
536 +    begin
537 +      CharSetID := FUserCharSetMap[i].CharSetID;
538 +      Result := true;
539 +      Exit;
540 +    end;
541 + end;
542 +
543 + function TFBAttachment.CharSetName2CharSetID(CharSetName: AnsiString;
544 +  var CharSetID: integer): boolean;
545 + var i: integer;
546 + begin
547 +  Result := false;
548 +  for i := Low(CharSetMap) to High(CharSetMap) do
549 +    if AnsiCompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then
550 +    begin
551 +      CharSetID := CharSetMap[i].CharSetID;
552 +      Result := true;
553 +      Exit;
554 +    end;
555 +
556 +    for i := 0 to Length(FUserCharSetMap) - 1 do
557 +      if AnsiCompareStr(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then
558 +      begin
559 +        CharSetID := FUserCharSetMap[i].CharSetID;
560 +        Result := true;
561 +        Exit;
562 +      end;
563 + end;
564 +
565 + function TFBAttachment.CharSetWidth(CharSetID: integer; var Width: integer
566 +  ): boolean;
567 + var i: integer;
568 + begin
569 +  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
570 +               and (CharSetMap[CharSetID].CharSetID = CharSetID);
571 +  if Result then
572 +    begin
573 +      Width := CharSetMap[CharSetID].CharSetWidth;
574 +      Result := true;
575 +      Exit;
576 +    end;
577 +
578 +  for i := 0 to Length(FUserCharSetMap) - 1 do
579 +    if FUserCharSetMap[i].CharSetID = CharSetID then
580 +    begin
581 +      Width := FUserCharSetMap[i].CharSetWidth;
582 +      Result := true;
583 +      Exit;
584 +    end;
585 + end;
586 +
587 + const
588 +  sqlLookupCharSet = 'Select RDB$CHARACTER_SET_ID, RDB$BYTES_PER_CHARACTER From RDB$CHARACTER_SETS '+
589 +                     'Where RDB$SYSTEM_FLAG = 0 and RDB$CHARACTER_SET_NAME = UPPER(?)';
590 +
591 + procedure TFBAttachment.RegisterCharSet(CharSetName: AnsiString;
592 +  CodePage: TSystemCodePage; AllowReverseLookup: boolean; out CharSetID: integer
593 +  );
594 + var CharSets: IResultSet;
595 +    idx: integer;
596 + begin
597 +  if CharSetName2CharSetID(CharSetName,CharSetID) then
598 +    IBError(ibxeCharacterSetExists,[CharSetName]);
599 +
600 +  CharSets := OpenCursorAtStart(sqlLookupCharSet,[CharSetName]);
601 +  if CharSets.IsEof then
602 +    IBError(ibxeUnknownUserCharSet,[CharSetName]);
603 +
604 +  idx := Length(FUserCharSetMap);
605 +  SetLength(FUserCharSetMap,idx+1);
606 +  FUserCharSetMap[idx].AllowReverseLookup := AllowReverseLookup;
607 +  FUserCharSetMap[idx].CharSetID := CharSets[0].AsInteger;
608 +  FUserCharSetMap[idx].CharSetName := AnsiUpperCase(CharSetName);
609 +  FUserCharSetMap[idx].CharSetWidth := CharSets[1].AsInteger;
610 +  FUserCharSetMap[idx].CodePage := CodePage;
611 +  CharSetID := CharSets[0].AsInteger;
612 + end;
613 +
614   end.
615  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines