@@ -497,6 +497,7 @@ TDBConnection = class(TComponent)
497497 FCaseSensitivity: Integer;
498498 FSQLFunctions: TSQLFunctionList;
499499 FNamedEnums: TStringList;
500+ FHexPrefix, FHexPostfix: String;
500501 procedure SetActive (Value : Boolean); virtual ; abstract ;
501502 procedure DoBeforeConnect ; virtual ;
502503 procedure StartSSHTunnel (var FinalHost: String; var FinalPort: Integer);
@@ -637,6 +638,8 @@ TDBConnection = class(TComponent)
637638 property SqlProvider: TSqlProvider read FSqlProvider;
638639 property NamedEnums: TStringList read FNamedEnums;
639640 procedure GetColumnDefaultExpressions (Items: TStrings); virtual ;
641+ property HexPrefix: String read FHexPrefix;
642+ property HexPostfix: String read FHexPostfix;
640643 published
641644 property Active: Boolean read FActive write SetActive default False;
642645 property Database: String read FDatabase write SetDatabase;
@@ -2167,6 +2170,8 @@ constructor TDBConnection.Create(AOwner: TComponent);
21672170 FQuoteChar := ' "' ;
21682171 FQuoteChars := ' "[]' ;
21692172 FNamedEnums := TStringList.Create;
2173+ FHexPrefix := ' ' ;
2174+ FHexPostfix := ' ' ;
21702175end ;
21712176
21722177
@@ -2176,6 +2181,8 @@ constructor TMySQLConnection.Create(AOwner: TComponent);
21762181begin
21772182 inherited ;
21782183 FQuoteChar := ' `' ;
2184+ FHexPrefix := ' 0x' ;
2185+ FHexPostfix := ' ' ;
21792186 FQuoteChars := ' `"' ;
21802187 FStatementNum := 0 ;
21812188 // The compiler complains that dynamic and static arrays are incompatible, so this does not work:
@@ -2220,6 +2227,8 @@ constructor TSQLiteConnection.Create(AOwner: TComponent);
22202227 i: Integer;
22212228begin
22222229 inherited ;
2230+ FHexPrefix := ' x'' ' ;
2231+ FHexPostfix := ' '' ' ;
22232232 SetLength(FDatatypes, Length(SQLiteDatatypes));
22242233 for i:=0 to High(SQLiteDatatypes) do
22252234 FDatatypes[i] := SQLiteDatatypes[i];
@@ -5275,7 +5284,7 @@ function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String
52755284 ValuePrefix := ' ' ;
52765285 case Datatype.Category of
52775286 // Some special cases
5278- dtcBinary: begin
5287+ dtcBinary, dtcSpatial : begin
52795288 if IsHex(Text) then
52805289 DoQuote := False;
52815290 end ;
@@ -5374,7 +5383,7 @@ function TDBConnection.EscapeBin(BinValue: String): String;
53745383 end else begin
53755384 SetLength(Result, BinLen*2 );
53765385 BinToHex(PAnsiChar(Ansi), PChar(Result), BinLen);
5377- Result := ' 0x ' + Result;
5386+ Result := FHexPrefix + Result + FHexPostfix ;
53785387 end ;
53795388 if AppSettings.ReadBool(asLowercaseHex) then
53805389 Result := Result.ToLowerInvariant;
@@ -5388,16 +5397,16 @@ function TDBConnection.EscapeBin(var ByteData: TBytes): String;
53885397 Ansi: AnsiString;
53895398begin
53905399 BinLen := Length(ByteData);
5391- SetString(Ansi, PAnsiChar(ByteData), BinLen);
53925400 if BinLen = 0 then begin
53935401 Result := EscapeString(' ' );
53945402 end else begin
5403+ SetString(Ansi, PAnsiChar(ByteData), BinLen);
53955404 if IsHex(String(Ansi)) then begin
53965405 Result := String(Ansi); // Already hex encoded
53975406 end else begin
53985407 SetLength(Result, BinLen*2 );
53995408 BinToHex(PAnsiChar(Ansi), PChar(Result), BinLen);
5400- Result := ' 0x ' + Result;
5409+ Result := FHexPrefix + Result + FHexPostfix ;
54015410 end ;
54025411 if AppSettings.ReadBool(asLowercaseHex) then
54035412 Result := Result.ToLowerInvariant;
@@ -6772,7 +6781,7 @@ function TDBConnection.IsHex(Text: String): Boolean;
67726781 Result := False;
67736782 Len := Length(Text);
67746783 if Len >= 3 then begin
6775- Result := (Text[ 1 ] = ' 0 ' ) and (Text[ 2 ] = ' x ' );
6784+ Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and (FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix) );
67766785 if Result then begin
67776786 for i:=3 to SIZE_KB do begin
67786787 if not CharInSet(Text[i], HexChars) then begin
@@ -9209,10 +9218,14 @@ function TDBQuery.HexValue(Column: Integer; IgnoreErrors: Boolean=False): String
92099218 baData: TBytes;
92109219begin
92119220 // Return a binary column value as hex AnsiString
9212- if FConnection.Parameters.IsAnyMysql then begin
9221+ baData := [];
9222+ try
9223+ // Throws sNotImplemented on connections without an override
92139224 GetColBinData(Column, baData);
92149225 Result := FConnection.EscapeBin(baData);
9215- end else
9226+ except
9227+ Result := FConnection.EscapeBin(Col(Column, IgnoreErrors));
9228+ end ;
92169229 Result := FConnection.EscapeBin(Col(Column, IgnoreErrors));
92179230end ;
92189231
0 commit comments