Skip to content

Commit 29d26b5

Browse files
committed
fix: wrong IsHex result on non-MySQL connections, prevent escaping quotes in SQLite hex values x'aa...'
Refs #1980
1 parent e06a4ff commit 29d26b5

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

‎source/dbconnection.pas‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,30 +5108,33 @@ function TDBConnection.EscapeString(Text: String; ProcessJokerChars: Boolean=fal
51085108

51095109
function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String;
51105110
var
5111-
DoQuote: Boolean;
5111+
DoEscape: Boolean;
51125112
ValuePrefix: String;
51135113
const
51145114
CategoriesNeedQuote = [dtcText, dtcBinary, dtcTemporal, dtcSpatial, dtcOther];
51155115
begin
5116-
// Quote text based on the passed datatype
5117-
DoQuote := Datatype.Category in CategoriesNeedQuote;
5116+
// Escape and quote text based on the passed datatype
5117+
DoEscape := Datatype.Category in CategoriesNeedQuote;
51185118
ValuePrefix := '';
51195119
case Datatype.Category of
51205120
// Some special cases
51215121
dtcBinary, dtcSpatial: begin
51225122
if IsHex(Text) then
5123-
DoQuote := False;
5123+
DoEscape := False;
51245124
end;
51255125
dtcInteger, dtcReal: begin
51265126
if (not IsNumeric(Text)) and (not IsHex(Text)) then
5127-
DoQuote := True;
5127+
DoEscape := True;
51285128
if (Datatype.Index = dbdtBit) and FParameters.IsAnyMySQL then begin
5129-
DoQuote := True;
5129+
DoEscape := True;
51305130
ValuePrefix := 'b';
51315131
end;
51325132
end;
51335133
end;
5134-
Result := ValuePrefix + EscapeString(Text, False, DoQuote);
5134+
if DoEscape then
5135+
Result := ValuePrefix + EscapeString(Text, False)
5136+
else
5137+
Result := ValuePrefix + Text;
51355138
end;
51365139

51375140

@@ -6618,23 +6621,31 @@ function TDBConnection.IsNumeric(Text: String): Boolean;
66186621

66196622
function TDBConnection.IsHex(Text: String): Boolean;
66206623
var
6621-
i, Len: Integer;
6624+
i, BodyLen: Integer;
6625+
HexBody: String;
66226626
const
66236627
HexChars: TSysCharSet = ['0'..'9','a'..'f', 'A'..'F'];
66246628
begin
66256629
// Check first kilobyte of passed text whether it's a hex encoded string. Hopefully faster than a regex.
66266630
Result := False;
6627-
Len := Length(Text);
6628-
if Len >= 3 then begin
6629-
Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and (FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix));
6631+
BodyLen := Length(Text) - Length(FHexPrefix) - Length(FHexPostfix);
6632+
6633+
if (BodyLen >= 0) and (not Odd(BodyLen)) then begin
6634+
Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and
6635+
(FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix));
66306636
if Result then begin
6631-
for i:=3 to SIZE_KB do begin
6632-
if not CharInSet(Text[i], HexChars) then begin
6637+
HexBody := Text;
6638+
if not FHexPrefix.IsEmpty then
6639+
Delete(HexBody, 1, Length(FHexPrefix));
6640+
if not FHexPostfix.IsEmpty then
6641+
Delete(HexBody, Length(HexBody) - Length(FHexPostfix) + 1, Length(FHexPostfix));
6642+
for i:=1 to Length(HexBody) do begin
6643+
if i > SIZE_KB then
6644+
Break;
6645+
if not CharInSet(HexBody[i], HexChars) then begin
66336646
Result := False;
66346647
Break;
66356648
end;
6636-
if i >= Len then
6637-
Break;
66386649
end;
66396650
end;
66406651
end;

0 commit comments

Comments
 (0)