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.

This commit is contained in:
Ansgar Becker
2013-06-22 04:45:53 +00:00
parent 9b7393ee56
commit 6371ff5557
4 changed files with 27 additions and 6 deletions

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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]) + ', ';