Skip to content

Commit 71bb25b

Browse files
committed
fix: allow $$ as delimiter again on non-PostgreSQL connections, and ` on non-MySQL connections
Refs #2385
1 parent cb03bec commit 71bb25b

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

source/apphelpers.pas

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ TSQLSentence = class(TObject)
7373
TSQLBatch = class(TObjectList<TSQLSentence>)
7474
private
7575
FSQL: String;
76+
FQuotes: THashedStringList;
7677
procedure SetSQL(Value: String);
7778
function GetSize: Integer;
7879
function GetSQLWithoutComments: String; overload;
7980
public
81+
constructor Create(NetTypeGroup: TNetTypeGroup);
82+
destructor Destroy; overload;
8083
class function GetSQLWithoutComments(FullSQL: String): String; overload;
8184
property Size: Integer read GetSize;
8285
property SQL: String read FSQL write SetSQL;
@@ -3211,6 +3214,26 @@ function TSQLSentence.GetSQLWithoutComments: String;
32113214

32123215
{ TSQLBatch }
32133216

3217+
constructor TSQLBatch.Create(NetTypeGroup: TNetTypeGroup);
3218+
begin
3219+
inherited;
3220+
FQuotes := THashedStringList.Create;
3221+
FQuotes.CaseSensitive := True;
3222+
FQuotes.Sorted := True;
3223+
FQuotes.Add('"');
3224+
FQuotes.Add('''');
3225+
case NetTypeGroup of
3226+
ngMySQL: FQuotes.Add('`'); // MySQL/MariaDB only
3227+
ngPgSQL: FQuotes.Add('$$'); // PostgreSQL only ($abc$ unsupported)
3228+
end;
3229+
end;
3230+
3231+
destructor TSQLBatch.Destroy; overload;
3232+
begin
3233+
FQuotes.Free;
3234+
inherited;
3235+
end;
3236+
32143237
function TSQLBatch.GetSize: Integer;
32153238
var
32163239
Query: TSQLSentence;
@@ -3230,7 +3253,6 @@ procedure TSQLBatch.SetSQL(Value: String);
32303253
InString, InComment, InBigComment, InEscape: Boolean;
32313254
Marker: TSQLSentence;
32323255
rx: TRegExpr;
3233-
Quotes: THashedStringList;
32343256
const
32353257
NewLines = [#13, #10];
32363258
WhiteSpaces = NewLines + [#9, ' '];
@@ -3242,13 +3264,6 @@ procedure TSQLBatch.SetSQL(Value: String);
32423264
i := 0;
32433265
LastLeftOffset := 1;
32443266
Delim := Mainform.Delimiter;
3245-
Quotes := THashedStringList.Create;
3246-
Quotes.CaseSensitive := True;
3247-
Quotes.Sorted := True;
3248-
Quotes.Add('"');
3249-
Quotes.Add('''');
3250-
Quotes.Add('`'); // MySQL/MariaDB only
3251-
Quotes.Add('$$'); // PostgreSQL only ($abc$ unsupported)
32523267

32533268
InString := False; // c is in "enclosed string" or `identifier`
32543269
InComment := False; // c is in one-line comment (# or --)
@@ -3279,10 +3294,10 @@ procedure TSQLBatch.SetSQL(Value: String);
32793294
if InBigComment and (not InComment) and (not InString) and (c + n = '*/') then
32803295
InBigComment := False;
32813296
// Check for enclosed literals, so a query delimiter can be ignored
3282-
if (not InEscape) and (not InComment) and (not InBigComment) and (Quotes.Contains(c) or Quotes.Contains(cn)) then begin
3297+
if (not InEscape) and (not InComment) and (not InBigComment) and (FQuotes.Contains(c) or FQuotes.Contains(cn)) then begin
32833298
if not InString then begin
32843299
InString := True;
3285-
LastQuote := IfThen(Quotes.Contains(c), c, cn);
3300+
LastQuote := IfThen(FQuotes.Contains(c), c, cn);
32863301
end
32873302
else if (c = LastQuote) or (cn = LastQuote) then begin
32883303
InString := False;
@@ -3329,7 +3344,6 @@ procedure TSQLBatch.SetSQL(Value: String);
33293344
end;
33303345
end;
33313346

3332-
Quotes.Free;
33333347
end;
33343348

33353349
function TSQLBatch.GetSQLWithoutComments: String;

source/dbconnection.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,7 +4003,7 @@ function TSqlSrvConnection.GetLastResults: TDBQueryList;
40034003
Batch: TSQLBatch;
40044004
begin
40054005
Result := TDBQueryList.Create(False);
4006-
Batch := TSQLBatch.Create;
4006+
Batch := TSQLBatch.Create(FParameters.NetTypeGroup);
40074007
Batch.SQL := FLastQuerySQL;
40084008
for i:=0 to FLastRawResults.Count-1 do begin
40094009
r := Parameters.CreateQuery(Self);
@@ -4947,7 +4947,7 @@ procedure TDBConnection.PrefetchResults(SQL: String);
49474947
i: Integer;
49484948
begin
49494949
Query(SQL, True);
4950-
Batch := TSQLBatch.Create;
4950+
Batch := TSQLBatch.Create(FParameters.NetTypeGroup);
49514951
Batch.SQL := SQL;
49524952
FreeAndNil(FPrefetchResults);
49534953
FPrefetchResults := TDBQueryList.Create(True);

source/main.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ function TMainForm.GetCurrentQuery(Tab: TQueryTab): String;
30953095
begin
30963096
// Return SQL query on cursor position
30973097
Result := '';
3098-
BatchAll := TSQLBatch.Create;
3098+
BatchAll := TSQLBatch.Create(ActiveConnection.Parameters.NetTypeGroup);
30993099
BatchAll.SQL := Tab.Memo.Text;
31003100
PrevQuery := nil;
31013101
for Query in BatchAll do begin
@@ -3131,7 +3131,7 @@ procedure TMainForm.actExecuteQueryExecute(Sender: TObject);
31313131
DoExecute := True;
31323132

31333133
ShowStatusMsg(_('Splitting SQL queries ...'));
3134-
Batch := TSQLBatch.Create;
3134+
Batch := TSQLBatch.Create(ActiveConnection.Parameters.NetTypeGroup);
31353135
if Sender = actExecuteSelection then begin
31363136
Batch.SQL := Tab.Memo.SelText;
31373137
Tab.LeftOffsetInMemo := Tab.Memo.SelStart;
@@ -4081,7 +4081,7 @@ function TMainForm.RunQueryFile(FileName: String; Encoding: TEncoding; Conn: TDB
40814081
ErrorCount := 0;
40824082
RowsAffected := 0;
40834083
LinesRemain := '';
4084-
Queries := TSQLBatch.Create;
4084+
Queries := TSQLBatch.Create(Conn.Parameters.NetTypeGroup);
40854085

40864086
try
40874087
// Start file operations
@@ -4269,7 +4269,7 @@ function TMainform.InitConnection(Params: TConnectionParameters; ActivateMe: Boo
42694269
if not FileExists(StartupScript) then
42704270
ErrorDialog(f_('Startup script file not found: %s', [StartupScript]))
42714271
else begin
4272-
StartupBatch := TSQLBatch.Create;
4272+
StartupBatch := TSQLBatch.Create(Connection.Parameters.NetTypeGroup);
42734273
StartupBatch.SQL := ReadTextfile(StartupScript, nil);
42744274
for Query in StartupBatch do try
42754275
Connection.Query(Query.SQL);
@@ -5274,7 +5274,7 @@ procedure TMainForm.SetDelimiter(Value: String);
52745274
Msg := _('Empty value.')
52755275
else begin
52765276
rx := TRegExpr.Create;
5277-
rx.Expression := '(/\*|--|#|\''|\"|`|\$\$)';
5277+
rx.Expression := '(/\*|--|#|\''|\")';
52785278
if rx.Exec(Value) then
52795279
Msg := _('Start-of-comment tokens or string literal markers are not allowed.')
52805280
end;
@@ -6901,7 +6901,7 @@ procedure TMainForm.SynCompletionProposalExecute(Sender: TObject);
69016901
CurrentQuery := 'SELECT * FROM '+ActiveDbObj.QuotedName+' WHERE ' + Editor.Text;
69026902
end else begin
69036903
// In a query tab
6904-
Queries := TSQLBatch.Create;
6904+
Queries := TSQLBatch.Create(Conn.Parameters.NetTypeGroup);
69056905
Queries.SQL := Editor.Text;
69066906
for Query in Queries do begin
69076907
if (Query.LeftOffset <= Editor.SelStart) and (Editor.SelStart < Query.RightOffset) then begin

source/table_editor.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
928928
AddQuery(FKeys[i].SQLCode(DBObject.Name));
929929
end;
930930

931-
Result := TSQLBatch.Create;
931+
Result := TSQLBatch.Create(DBObject.Connection.Parameters.NetTypeGroup);
932932
Result.SQL := SQL;
933933

934934
FreeAndNil(Specs);
@@ -1022,7 +1022,7 @@ function TfrmTableEditor.ComposeCreateStatement: TSQLBatch;
10221022
end;
10231023
end;
10241024

1025-
Result := TSQLBatch.Create;
1025+
Result := TSQLBatch.Create(DBObject.Connection.Parameters.NetTypeGroup);
10261026
Result.SQL := Trim(SQL);
10271027
end;
10281028

source/view.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ function TfrmView.ComposeCreateStatement: TSQLBatch;
257257
sql := sql + 'RENAME TABLE '+ViewName + ' TO '+RenameView + ';' + sLineBreak;
258258
end;
259259

260-
Result := TSQLBatch.Create;
260+
Result := TSQLBatch.Create(DBObject.Connection.Parameters.NetTypeGroup);
261261
Result.SQL := Trim(SQL);
262262
end;
263263

0 commit comments

Comments
 (0)