Skip to content

Commit 68a291a

Browse files
committed
fix: use correct hex prefix/postfix on MS SQL and PostgreSQL, allow odd length of hex body in MySQL
Refs #1980 Co-authored-by: ansgarbecker <ansgarbecker@users.noreply.github.com>
1 parent 53df50a commit 68a291a

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

‎source/dbconnection.pas‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,7 @@ constructor TDBConnection.Create(AOwner: TComponent);
21702170
FQuoteChar := '"';
21712171
FQuoteChars := '"[]';
21722172
FNamedEnums := TStringList.Create;
2173-
FHexPrefix := '';
2173+
FHexPrefix := '0x'; // MySQL and MSSQL. Override if required.
21742174
FHexPostfix := '';
21752175
end;
21762176

@@ -2181,8 +2181,6 @@ constructor TMySQLConnection.Create(AOwner: TComponent);
21812181
begin
21822182
inherited;
21832183
FQuoteChar := '`';
2184-
FHexPrefix := '0x';
2185-
FHexPostfix := '';
21862184
FQuoteChars := '`"';
21872185
FStatementNum := 0;
21882186
// The compiler complains that dynamic and static arrays are incompatible, so this does not work:
@@ -2211,6 +2209,8 @@ constructor TPgConnection.Create(AOwner: TComponent);
22112209
i: Integer;
22122210
begin
22132211
inherited;
2212+
FHexPrefix := '\x';
2213+
FHexPostfix := '';
22142214
FQuoteChars := '"';
22152215
SetLength(FDatatypes, Length(PostGreSQLDatatypes));
22162216
for i:=0 to High(PostGreSQLDatatypes) do
@@ -6778,13 +6778,22 @@ function TDBConnection.IsHex(Text: String): Boolean;
67786778
var
67796779
i, BodyLen: Integer;
67806780
HexBody: String;
6781+
AllowOddHexLen: Boolean;
67816782
const
67826783
HexChars: TSysCharSet = ['0'..'9','a'..'f', 'A'..'F'];
67836784
begin
67846785
// Check first kilobyte of passed text whether it's a hex encoded string. Hopefully faster than a regex.
67856786
Result := False;
67866787
BodyLen := Length(Text) - Length(FHexPrefix) - Length(FHexPostfix);
6787-
if (BodyLen >= 0) and (not Odd(BodyLen)) then begin
6788+
AllowOddHexLen := FParameters.IsAnyMySQL;
6789+
6790+
if BodyLen < 0 then begin
6791+
Result := False;
6792+
end
6793+
else if (not AllowOddHexLen) and Odd(BodyLen) then begin
6794+
Result := False;
6795+
end
6796+
else begin
67886797
Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and
67896798
(FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix));
67906799
if Result then begin

0 commit comments

Comments
 (0)