@@ -489,6 +489,7 @@ TDBConnection = class(TComponent)
489489 FCaseSensitivity: Integer;
490490 FSQLFunctions: TSQLFunctionList;
491491 FNamedEnums: TStringList;
492+ FHexPrefix, FHexPostfix: String;
492493 procedure SetActive (Value : Boolean); virtual ; abstract ;
493494 procedure DoBeforeConnect ; virtual ;
494495 procedure StartSSHTunnel (var FinalHost: String; var FinalPort: Integer);
@@ -630,6 +631,8 @@ TDBConnection = class(TComponent)
630631 property SqlProvider: TSqlProvider read FSqlProvider;
631632 property NamedEnums: TStringList read FNamedEnums;
632633 procedure GetColumnDefaultExpressions (Items: TStrings); virtual ;
634+ property HexPrefix: String read FHexPrefix;
635+ property HexPostfix: String read FHexPostfix;
633636 published
634637 property Active: Boolean read FActive write SetActive default False;
635638 property Database: String read FDatabase write SetDatabase;
@@ -2088,6 +2091,8 @@ constructor TDBConnection.Create(AOwner: TComponent);
20882091 FQuoteChar := ' "' ;
20892092 FQuoteChars := ' "[]' ;
20902093 FNamedEnums := TStringList.Create;
2094+ FHexPrefix := ' ' ;
2095+ FHexPostfix := ' ' ;
20912096end ;
20922097
20932098
@@ -2098,6 +2103,8 @@ constructor TMySQLConnection.Create(AOwner: TComponent);
20982103 inherited ;
20992104 FQuoteChar := ' `' ;
21002105 FQuoteChars := ' `"' ;
2106+ FHexPrefix := ' 0x' ;
2107+ FHexPostfix := ' ' ;
21012108 FStatementNum := 0 ;
21022109 // The compiler complains that dynamic and static arrays are incompatible, so this does not work:
21032110 // FDatatypes := MySQLDatatypes
@@ -2142,6 +2149,8 @@ constructor TSQLiteConnection.Create(AOwner: TComponent);
21422149 i: Integer;
21432150begin
21442151 inherited ;
2152+ FHexPrefix := ' x'' ' ;
2153+ FHexPostfix := ' '' ' ;
21452154 SetLength(FDatatypes, Length(SQLiteDatatypes));
21462155 for i:=0 to High(SQLiteDatatypes) do
21472156 FDatatypes[i] := SQLiteDatatypes[i];
@@ -5109,7 +5118,7 @@ function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String
51095118 ValuePrefix := ' ' ;
51105119 case Datatype.Category of
51115120 // Some special cases
5112- dtcBinary: begin
5121+ dtcBinary, dtcSpatial : begin
51135122 if IsHex(Text) then
51145123 DoQuote := False;
51155124 end ;
@@ -5208,7 +5217,7 @@ function TDBConnection.EscapeBin(BinValue: String): String;
52085217 end else begin
52095218 SetLength(Result, BinLen*2 );
52105219 BinToHex(PAnsiChar(Ansi), PChar(Result), BinLen);
5211- Result := ' 0x ' + Result;
5220+ Result := FHexPrefix + Result + FHexPostfix ;
52125221 end ;
52135222 if AppSettings.ReadBool(asLowercaseHex) then
52145223 Result := Result.ToLowerInvariant;
@@ -5222,16 +5231,16 @@ function TDBConnection.EscapeBin(var ByteData: TBytes): String;
52225231 Ansi: AnsiString;
52235232begin
52245233 BinLen := Length(ByteData);
5225- SetString(Ansi, PAnsiChar(ByteData), BinLen);
52265234 if BinLen = 0 then begin
52275235 Result := EscapeString(' ' );
52285236 end else begin
5237+ SetString(Ansi, PAnsiChar(ByteData), BinLen);
52295238 if IsHex(String(Ansi)) then begin
52305239 Result := String(Ansi); // Already hex encoded
52315240 end else begin
52325241 SetLength(Result, BinLen*2 );
52335242 BinToHex(PAnsiChar(Ansi), PChar(Result), BinLen);
5234- Result := ' 0x ' + Result;
5243+ Result := FHexPrefix + Result + FHexPostfix ;
52355244 end ;
52365245 if AppSettings.ReadBool(asLowercaseHex) then
52375246 Result := Result.ToLowerInvariant;
@@ -6617,7 +6626,7 @@ function TDBConnection.IsHex(Text: String): Boolean;
66176626 Result := False;
66186627 Len := Length(Text);
66196628 if Len >= 3 then begin
6620- Result := (Text[ 1 ] = ' 0 ' ) and (Text[ 2 ] = ' x ' );
6629+ Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and (FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix) );
66216630 if Result then begin
66226631 for i:=3 to SIZE_KB do begin
66236632 if not CharInSet(Text[i], HexChars) then begin
@@ -9077,12 +9086,14 @@ function TDBQuery.HexValue(Column: Integer; IgnoreErrors: Boolean=False): String
90779086 baData: TBytes;
90789087begin
90799088 // Return a binary column value as hex AnsiString
9080- if FConnection.Parameters.IsAnyMysql then begin
9081- baData := [];
9089+ baData := [];
9090+ try
9091+ // Throws sNotImplemented on connections without an override
90829092 GetColBinData(Column, baData);
90839093 Result := FConnection.EscapeBin(baData);
9084- end else
9094+ except
90859095 Result := FConnection.EscapeBin(Col(Column, IgnoreErrors));
9096+ end ;
90869097end ;
90879098
90889099
0 commit comments