From 6371ff5557ee4dd8ada9feafbfdea9861784e673 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Sat, 22 Jun 2013 04:45:53 +0000 Subject: [PATCH] Issue #3212: Introduce TDBConnection.QuotedDbAndTableName(), callable with a db and table string. Internally calls TDBObject.QuotedDbAndTableName(), so we get the schema between db and table if required. --- source/copytable.pas | 2 +- source/dbconnection.pas | 22 ++++++++++++++++++++++ source/insertfiles.pas | 3 +-- source/loaddata.pas | 6 +++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/source/copytable.pas b/source/copytable.pas index f263c90a..48090bbc 100644 --- a/source/copytable.pas +++ b/source/copytable.pas @@ -370,7 +370,7 @@ const ClausePattern: String = #9 + '%s,' + CRLF; begin // Compose and run CREATE query - TargetTable := FDBObj.Connection.QuoteIdent(comboDatabase.Text)+'.'+FDBObj.Connection.QuoteIdent(editNewTablename.Text); + TargetTable := FDBObj.Connection.QuotedDbAndTableName(comboDatabase.Text, editNewTablename.Text); TableExistance := FDBObj.Connection.GetVar('SHOW TABLES FROM '+FDBObj.Connection.QuoteIdent(comboDatabase.Text)+' LIKE '+esc(editNewTablename.Text)); if TableExistance <> '' then begin if MessageDialog(_('Target table exists. Drop it and overwrite?'), mtConfirmation, [mbYes, mbCancel]) = mrCancel then begin diff --git a/source/dbconnection.pas b/source/dbconnection.pas index b201927e..d592b327 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -316,6 +316,7 @@ type function EscapeString(Text: String; ProcessJokerChars: Boolean=False; DoQuote: Boolean=True): String; function QuoteIdent(Identifier: String; AlwaysQuote: Boolean=True; Glue: Char=#0): String; function DeQuoteIdent(Identifier: String; Glue: Char=#0): String; + function QuotedDbAndTableName(DB, Obj: String): String; function escChars(const Text: String; EscChar, Char1, Char2, Char3, Char4: Char): String; function UnescapeString(Text: String): String; function ConvertServerVersion(Version: Integer): String; virtual; abstract; @@ -2406,6 +2407,27 @@ begin end; +function TDBConnection.QuotedDbAndTableName(DB, Obj: String): String; +var + Objects: TDBObjectList; + o: TDBObject; +begin + // Call TDBObject.QuotedDbAndTableName for db and table string. + // Return fully qualified db and tablename, quoted, and including schema if required + Result := ''; + Objects := GetDBObjects(DB); + for o in Objects do begin + if o.Name = Obj then begin + Result := o.QuotedDbAndTableName(); + Break; + end; + end; + // Should not happen but who knows: Object name not found. + if Result = '' then + Result := QuoteIdent(DB) + '.' + QuoteIdent(Obj); +end; + + function TDBConnection.GetCol(SQL: String; Column: Integer=0): TStringList; var Results: TDBQuery; diff --git a/source/insertfiles.pas b/source/insertfiles.pas index a4d188d0..d391c7ad 100644 --- a/source/insertfiles.pas +++ b/source/insertfiles.pas @@ -620,8 +620,7 @@ begin FileInfo := ListFiles.GetNodeData(Node); FileSize := _GetFileSize(FileInfo.Filename); FileReadDone := False; - sql := 'INSERT INTO '+FConnection.QuoteIdent(comboDBs.Text)+'.'+FConnection.QuoteIdent(comboTables.Text) + - ' ('; + sql := 'INSERT INTO '+FConnection.QuotedDbAndTableName(comboDBs.Text, comboTables.Text) + ' ('; ColNode := ListColumns.GetFirst; while Assigned(ColNode) do begin ColInfo := ListColumns.GetNodeData(ColNode); diff --git a/source/loaddata.pas b/source/loaddata.pas index 170caccd..1dfdbf1b 100644 --- a/source/loaddata.pas +++ b/source/loaddata.pas @@ -285,7 +285,7 @@ begin // Truncate table before importing if chkTruncateTable.Checked then - FConnection.Query('TRUNCATE TABLE ' + FConnection.QuoteIdent(comboDatabase.Text) + '.' + FConnection.QuoteIdent(comboTable.Text)); + FConnection.Query('TRUNCATE TABLE ' + FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text)); ColumnCount := 0; for i:=0 to chkListColumns.Items.Count-1 do begin @@ -355,7 +355,7 @@ begin 1: SQL := SQL + 'IGNORE '; 2: SQL := SQL + 'REPLACE '; end; - SQL := SQL + 'INTO TABLE ' + FConnection.QuoteIdent(comboDatabase.Text) + '.' + FConnection.QuoteIdent(comboTable.Text) + ' '; + SQL := SQL + 'INTO TABLE ' + FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text) + ' '; if comboEncoding.ItemIndex > 0 then begin FConnection.CharsetTable.RecNo := comboEncoding.ItemIndex-1; @@ -462,7 +462,7 @@ var 1: SQL := 'INSERT '+LowPrio+'IGNORE '; 2: SQL := 'REPLACE '+LowPrio; end; - SQL := SQL + 'INTO '+FConnection.QuoteIdent(comboDatabase.Text)+'.'+FConnection.QuoteIdent(comboTable.Text)+' ('; + SQL := SQL + 'INTO '+FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text)+' ('; for i:=0 to chkListColumns.Items.Count-1 do begin if chkListColumns.Checked[i] then SQL := SQL + FConnection.QuoteIdent(chkListColumns.Items[i]) + ', ';