diff options
| author | 2015-11-25 16:48:49 -0500 | |
|---|---|---|
| committer | 2015-11-25 16:48:49 -0500 | |
| commit | 7412693e086a7eafaa7ea861164caf523943e5fa (patch) | |
| tree | 0aee322e40572df306b9813546c7a12b3093bcea /SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp | |
| parent | 640196993d31cf5d6fdf36386990ec05f473a048 (diff) | |
| parent | 8e640722c62692818ab840d50b3758f89a41a54e (diff) | |
Merge tag 'upstream/3.0.7'
Upstream version 3.0.7
# gpg: Signature made Wed 25 Nov 2015 04:48:48 PM EST using RSA key ID EBE9BD91
# gpg: Good signature from "Unit 193 <unit193@gmail.com>"
# gpg: aka "Unit 193 <unit193@ninthfloor.org>"
# gpg: aka "Unit 193 <unit193@ubuntu.com>"
# gpg: aka "Unit 193 <unit193@ninthfloor.com>"
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp')
| -rw-r--r-- | SQLiteStudio3/coreSQLiteStudio/tablemodifier.cpp | 59 |
1 files changed, 54 insertions, 5 deletions
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<SqliteSelect::Core::SingleSource*> selSources = select->getAllTypedStatements<SqliteSelect::Core::SingleSource>(); + TokenList tableTokens; + StrHash<SelectResolver::Table> 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<SelectResolver::Column> 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<SelectResolver::Table> TableModifier::tablesAsNameHash(const QSet<SelectResolver::Table>& resolvedTables) +{ + StrHash<SelectResolver::Table> result; + for (const SelectResolver::Table& tab : resolvedTables) + result[tab.table] = tab; + + return result; +} + +bool TableModifier::isTableAliasUsedForColumn(const TokenPtr &token, const StrHash<SelectResolver::Table> &resolvedTables, const QList<SqliteSelect::Core::SingleSource *> &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) |
