Skip to content

Commit e06a4ff

Browse files
committed
fix: use connection specific hex prefix and postfix when editing binary values in grid
Refs #1980
1 parent b49cd3d commit e06a4ff

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎source/bineditor.pas‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface
66

77
uses
88
Classes, Graphics, Forms, Controls, StdCtrls, laz.VirtualTrees,
9-
ComCtrls, Dialogs, SysUtils, extra_controls, LCLType, extfiledialog;
9+
ComCtrls, Dialogs, SysUtils, extra_controls, LCLType, extfiledialog, dbconnection;
1010

1111
{$I const.inc}
1212

@@ -64,9 +64,16 @@ function TfrmBinEditor.GetText: String;
6464
end;
6565

6666
procedure TfrmBinEditor.SetText(text: String);
67+
var
68+
Conn: TDBConnection;
6769
begin
6870
// Skip '0x'.
69-
memoText.Text := Copy(text, 3);
71+
Conn := MainForm.ActiveConnection;
72+
if not Conn.HexPrefix.IsEmpty then
73+
Delete(text, 1, Length(Conn.HexPrefix));
74+
if not Conn.HexPostfix.IsEmpty then
75+
Delete(text, Length(text) - Length(Conn.HexPostfix) + 1, Length(Conn.HexPostfix));
76+
memoText.Text := text;
7077
end;
7178

7279

‎source/dbconnection.pas‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 := '';
20912096
end;
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;
21432150
begin
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;
52235232
begin
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;
90789087
begin
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;
90869097
end;
90879098

90889099

0 commit comments

Comments
 (0)