Skip to content

Commit 2c48500

Browse files
committed
fix: use connection specific hex prefix and postfix when editing binary values in grid
Refs #1980 Co-authored-by: ansgarbecker <ansgarbecker@users.noreply.github.com>
1 parent 770f19c commit 2c48500

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎source/bineditor.pas‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ interface
44

55
uses
66
Winapi.Windows, System.Classes, Vcl.Graphics, Vcl.Forms, Vcl.Controls, Vcl.StdCtrls, VirtualTrees,
7-
Vcl.ComCtrls, Vcl.ToolWin, Vcl.Dialogs, System.SysUtils, gnugettext, extra_controls;
7+
Vcl.ComCtrls, Vcl.ToolWin, Vcl.Dialogs, System.SysUtils, gnugettext, extra_controls,
8+
dbconnection;
89

910
{$I const.inc}
1011

@@ -61,9 +62,16 @@ function TfrmBinEditor.GetText: String;
6162
end;
6263

6364
procedure TfrmBinEditor.SetText(text: String);
64-
begin
65-
// Skip '0x'.
66-
memoText.Text := Copy(text, 3);
65+
var
66+
Conn: TDBConnection;
67+
begin
68+
// Skip hex prefix and postfix, connection specific
69+
Conn := MainForm.ActiveConnection;
70+
if not Conn.HexPrefix.IsEmpty then
71+
Delete(text, 1, Length(Conn.HexPrefix));
72+
if not Conn.HexPostfix.IsEmpty then
73+
Delete(text, Length(text) - Length(Conn.HexPostfix) + 1, Length(Conn.HexPostfix));
74+
memoText.Text := text;
6775
end;
6876

6977

‎source/dbconnection.pas‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ TDBConnection = class(TComponent)
497497
FCaseSensitivity: Integer;
498498
FSQLFunctions: TSQLFunctionList;
499499
FNamedEnums: TStringList;
500+
FHexPrefix, FHexPostfix: String;
500501
procedure SetActive(Value: Boolean); virtual; abstract;
501502
procedure DoBeforeConnect; virtual;
502503
procedure StartSSHTunnel(var FinalHost: String; var FinalPort: Integer);
@@ -637,6 +638,8 @@ TDBConnection = class(TComponent)
637638
property SqlProvider: TSqlProvider read FSqlProvider;
638639
property NamedEnums: TStringList read FNamedEnums;
639640
procedure GetColumnDefaultExpressions(Items: TStrings); virtual;
641+
property HexPrefix: String read FHexPrefix;
642+
property HexPostfix: String read FHexPostfix;
640643
published
641644
property Active: Boolean read FActive write SetActive default False;
642645
property Database: String read FDatabase write SetDatabase;
@@ -2167,6 +2170,8 @@ constructor TDBConnection.Create(AOwner: TComponent);
21672170
FQuoteChar := '"';
21682171
FQuoteChars := '"[]';
21692172
FNamedEnums := TStringList.Create;
2173+
FHexPrefix := '';
2174+
FHexPostfix := '';
21702175
end;
21712176

21722177

@@ -2176,6 +2181,8 @@ constructor TMySQLConnection.Create(AOwner: TComponent);
21762181
begin
21772182
inherited;
21782183
FQuoteChar := '`';
2184+
FHexPrefix := '0x';
2185+
FHexPostfix := '';
21792186
FQuoteChars := '`"';
21802187
FStatementNum := 0;
21812188
// The compiler complains that dynamic and static arrays are incompatible, so this does not work:
@@ -2220,6 +2227,8 @@ constructor TSQLiteConnection.Create(AOwner: TComponent);
22202227
i: Integer;
22212228
begin
22222229
inherited;
2230+
FHexPrefix := 'x''';
2231+
FHexPostfix := '''';
22232232
SetLength(FDatatypes, Length(SQLiteDatatypes));
22242233
for i:=0 to High(SQLiteDatatypes) do
22252234
FDatatypes[i] := SQLiteDatatypes[i];
@@ -5275,7 +5284,7 @@ function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String
52755284
ValuePrefix := '';
52765285
case Datatype.Category of
52775286
// Some special cases
5278-
dtcBinary: begin
5287+
dtcBinary, dtcSpatial: begin
52795288
if IsHex(Text) then
52805289
DoQuote := False;
52815290
end;
@@ -5374,7 +5383,7 @@ function TDBConnection.EscapeBin(BinValue: String): String;
53745383
end else begin
53755384
SetLength(Result, BinLen*2);
53765385
BinToHex(PAnsiChar(Ansi), PChar(Result), BinLen);
5377-
Result := '0x' + Result;
5386+
Result := FHexPrefix + Result + FHexPostfix;
53785387
end;
53795388
if AppSettings.ReadBool(asLowercaseHex) then
53805389
Result := Result.ToLowerInvariant;
@@ -5388,16 +5397,16 @@ function TDBConnection.EscapeBin(var ByteData: TBytes): String;
53885397
Ansi: AnsiString;
53895398
begin
53905399
BinLen := Length(ByteData);
5391-
SetString(Ansi, PAnsiChar(ByteData), BinLen);
53925400
if BinLen = 0 then begin
53935401
Result := EscapeString('');
53945402
end else begin
5403+
SetString(Ansi, PAnsiChar(ByteData), BinLen);
53955404
if IsHex(String(Ansi)) then begin
53965405
Result := String(Ansi); // Already hex encoded
53975406
end else begin
53985407
SetLength(Result, BinLen*2);
53995408
BinToHex(PAnsiChar(Ansi), PChar(Result), BinLen);
5400-
Result := '0x' + Result;
5409+
Result := FHexPrefix + Result + FHexPostfix;
54015410
end;
54025411
if AppSettings.ReadBool(asLowercaseHex) then
54035412
Result := Result.ToLowerInvariant;
@@ -6772,7 +6781,7 @@ function TDBConnection.IsHex(Text: String): Boolean;
67726781
Result := False;
67736782
Len := Length(Text);
67746783
if Len >= 3 then begin
6775-
Result := (Text[1] = '0') and (Text[2] = 'x');
6784+
Result := (FHexPrefix.IsEmpty or Text.StartsWith(FHexPrefix)) and (FHexPostfix.IsEmpty or Text.EndsWith(FHexPostfix));
67766785
if Result then begin
67776786
for i:=3 to SIZE_KB do begin
67786787
if not CharInSet(Text[i], HexChars) then begin
@@ -9209,10 +9218,14 @@ function TDBQuery.HexValue(Column: Integer; IgnoreErrors: Boolean=False): String
92099218
baData: TBytes;
92109219
begin
92119220
// Return a binary column value as hex AnsiString
9212-
if FConnection.Parameters.IsAnyMysql then begin
9221+
baData := [];
9222+
try
9223+
// Throws sNotImplemented on connections without an override
92139224
GetColBinData(Column, baData);
92149225
Result := FConnection.EscapeBin(baData);
9215-
end else
9226+
except
9227+
Result := FConnection.EscapeBin(Col(Column, IgnoreErrors));
9228+
end;
92169229
Result := FConnection.EscapeBin(Col(Column, IgnoreErrors));
92179230
end;
92189231

0 commit comments

Comments
 (0)