From 8e640722c62692818ab840d50b3758f89a41a54e Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Wed, 25 Nov 2015 16:48:41 -0500 Subject: Imported Upstream version 3.0.7 --- SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp | 59 ++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) (limited to 'SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp') diff --git a/SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp b/SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp index 97e1fb3..3e23239 100644 --- a/SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp +++ b/SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp @@ -2,7 +2,6 @@ #include "common/utils_sql.h" #include "parser/parser.h" #include "schemaresolver.h" -#include "selectresolver.h" #include "parser/ast/sqlitecreateindex.h" #include "parser/ast/sqlitecreatetrigger.h" #include "parser/ast/sqlitecreateview.h" @@ -530,17 +529,32 @@ SqliteQuery* TableModifier::handleTriggerQuery(SqliteQuery* query, const QString SqliteSelect* TableModifier::handleSelect(SqliteSelect* select, const QString& trigTable) { + SelectResolver selectResolver(db, select->detokenize()); + // Table name - TokenList tableTokens = select->getContextTableTokens(false); - foreach (TokenPtr token, tableTokens) + QList selSources = select->getAllTypedStatements(); + TokenList tableTokens; + StrHash resolvedTables; + for (SqliteSelect::Core* core : select->coreSelects) { - if (token->value.compare(originalTable, Qt::CaseInsensitive) == 0) + resolvedTables = tablesAsNameHash(selectResolver.resolveTables(core)); + + tableTokens = core->getContextTableTokens(false); + foreach (TokenPtr token, tableTokens) + { + if (token->value.compare(originalTable, Qt::CaseInsensitive) != 0) + continue; + + // Check if that table name is the same as its alias name, so we use alias name and we don't rename it here, cause it's alias, not table + if (isTableAliasUsedForColumn(token, resolvedTables, selSources)) + continue; + token->value = newName; + } } // Column names TokenList columnTokens = select->getContextColumnTokens(false); - SelectResolver selectResolver(db, select->detokenize()); QList columns = selectResolver.translateToColumns(select, columnTokens); TokenList columnTokensToChange; @@ -577,6 +591,41 @@ SqliteSelect* TableModifier::handleSelect(SqliteSelect* select, const QString& t return new SqliteSelect(*selectPtr.data()); } +StrHash TableModifier::tablesAsNameHash(const QSet& resolvedTables) +{ + StrHash result; + for (const SelectResolver::Table& tab : resolvedTables) + result[tab.table] = tab; + + return result; +} + +bool TableModifier::isTableAliasUsedForColumn(const TokenPtr &token, const StrHash &resolvedTables, const QList &selSources) +{ + // If we don't have the table token on the list of resolved select tables, we don't consider it as aliased + if (!resolvedTables.contains(token->value, Qt::CaseInsensitive)) + { + qWarning() << "Table" << token->value << "in table tokens processed by TableModifier, but not in resolved SELECT tables."; + return false; + } + + SelectResolver::Table table = resolvedTables.value(token->value, Qt::CaseInsensitive); + if (table.alias.isNull()) + return false; + + if (table.alias.compare(token->value), Qt::CaseInsensitive != 0) + return false; + + // If the table token is mentioned in FROM clause, it's not a subject for aliased usage, cuase it defines alias, not uses it. + for (SqliteSelect::Core::SingleSource* src : selSources) + { + if (src->tokens.contains(token)) + return false; + } + + return true; +} + SqliteUpdate* TableModifier::handleTriggerUpdate(SqliteUpdate* update, const QString& trigName, const QString& trigTable) { if (update->table.compare(originalTable, Qt::CaseInsensitive) == 0) -- cgit v1.2.3