@@ -5108,30 +5108,33 @@ function TDBConnection.EscapeString(Text: String; ProcessJokerChars: Boolean=fal
51085108
51095109function TDBConnection.EscapeString (Text: String; Datatype: TDBDatatype): String;
51105110var
5111- DoQuote : Boolean;
5111+ DoEscape : Boolean;
51125112 ValuePrefix: String;
51135113const
51145114 CategoriesNeedQuote = [dtcText, dtcBinary, dtcTemporal, dtcSpatial, dtcOther];
51155115begin
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;
51355138end ;
51365139
51375140
@@ -6618,23 +6621,31 @@ function TDBConnection.IsNumeric(Text: String): Boolean;
66186621
66196622function TDBConnection.IsHex (Text: String): Boolean;
66206623var
6621- i, Len: Integer;
6624+ i, BodyLen: Integer;
6625+ HexBody: String;
66226626const
66236627 HexChars: TSysCharSet = [' 0' ..' 9' ,' a' ..' f' , ' A' ..' F' ];
66246628begin
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