Skip to content

Commit 343a6db

Browse files
committed
#5184 Fixed completer to show tables from implicitly attached database in case of 'SELECT other_database.'
1 parent 0564089 commit 343a6db

10 files changed

Lines changed: 58 additions & 2 deletions

File tree

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 3.4.13
44
- BUGFIX: #5183 Fixed completer proposals in the column names of the INSERT INTO statement.
5+
- BUGFIX: #5184 Fixed completer to show tables from implicitly attached database in case of 'SELECT other_database.'
56
- BUGFIX: System tables (sqlite_*) are now pushed further in the competer proposals.
67
- BUGFIX: Restored missing completer entries: string, number, BLOB literal.
78

SQLiteStudio3/Tests/TestUtils/dbmanagermock.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ QStringList DbManagerMock::getDbNames()
4747
return QStringList();
4848
}
4949

50+
QStringList DbManagerMock::getValidDbNames()
51+
{
52+
return QStringList();
53+
}
54+
5055
Db* DbManagerMock::getByName(const QString&, Qt::CaseSensitivity)
5156
{
5257
return nullptr;

SQLiteStudio3/Tests/TestUtils/dbmanagermock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DbManagerMock : public DbManager
1616
QList<Db*> getValidDbList();
1717
QList<Db*> getConnectedDbList();
1818
QStringList getDbNames();
19+
QStringList getValidDbNames();
1920
Db* getByName(const QString&, Qt::CaseSensitivity);
2021
Db* getByPath(const QString&);
2122
Db* createInMemDb(bool = false);

SQLiteStudio3/coreSQLiteStudio/completionhelper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ QList<ExpectedTokenPtr> CompletionHelper::getColumns(const QString &prefixTable)
634634
label = prefixTable+" = "+table;
635635
}
636636

637+
if (!dbName.isNull())
638+
dbName = translateDatabase(dbName);
639+
637640
// CREATE TRIGGER has a special "old" and "new" keywords as aliases for deleted/inserted/updated rows.
638641
// They should refer to a table that the trigger is created for.
639642
QString tableLower = table;

SQLiteStudio3/coreSQLiteStudio/parser/ast/sqliteexpr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ QStringList SqliteExpr::getTablesInStatement()
456456

457457
QStringList SqliteExpr::getDatabasesInStatement()
458458
{
459+
if (database.isNull() && !table.isNull() && validDbNames.contains(table, Qt::CaseInsensitive))
460+
return getStrListFromValue(table); // it's a "db.", not a "db.table."
461+
459462
return getStrListFromValue(database);
460463
}
461464

@@ -494,7 +497,9 @@ TokenList SqliteExpr::getTableTokensInStatement()
494497
TokenList SqliteExpr::getDatabaseTokensInStatement()
495498
{
496499
TokenList list;
497-
if (!database.isNull())
500+
if (database.isNull() && !table.isNull() && validDbNames.contains(table, Qt::CaseInsensitive))
501+
list << tokens[0]; // it's a "db.", not a "db.table."
502+
else if (!database.isNull())
498503
list << tokens[0];
499504

500505
return list;

SQLiteStudio3/coreSQLiteStudio/parser/ast/sqlitestatement.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../token.h"
33
#include "../lexer.h"
44
#include "common/unused.h"
5+
#include "services/dbmanager.h"
56
#include <QDebug>
67

78
SqliteStatement::SqliteStatement()
@@ -35,6 +36,7 @@ QStringList SqliteStatement::getContextTables(bool checkParent, bool checkChilds
3536

3637
QStringList SqliteStatement::getContextDatabases(bool checkParent, bool checkChilds)
3738
{
39+
prepareDbNames();
3840
return getContextDatabases(this, checkParent, checkChilds);
3941
}
4042

@@ -50,6 +52,7 @@ TokenList SqliteStatement::getContextTableTokens(bool checkParent, bool checkChi
5052

5153
TokenList SqliteStatement::getContextDatabaseTokens(bool checkParent, bool checkChilds)
5254
{
55+
prepareDbNames();
5356
return getContextDatabaseTokens(this, checkParent, checkChilds);
5457
}
5558

@@ -125,7 +128,10 @@ QStringList SqliteStatement::getContextDatabases(SqliteStatement *caller, bool c
125128
{
126129
QStringList results = getDatabasesInStatement();
127130
for (SqliteStatement* stmt : getContextStatements(caller, checkParent, checkChilds))
131+
{
132+
stmt->validDbNames = this->validDbNames;
128133
results += stmt->getContextDatabases(this, checkParent, checkChilds);
134+
}
129135

130136
return results;
131137
}
@@ -152,7 +158,10 @@ TokenList SqliteStatement::getContextDatabaseTokens(SqliteStatement *caller, boo
152158
{
153159
TokenList results = getDatabaseTokensInStatement();
154160
for (SqliteStatement* stmt : getContextStatements(caller, checkParent, checkChilds))
161+
{
162+
stmt->validDbNames = this->validDbNames;
155163
results += stmt->getContextDatabaseTokens(this, checkParent, checkChilds);
164+
}
156165

157166
return results;
158167
}
@@ -236,6 +245,11 @@ QList<SqliteStatement *> SqliteStatement::getContextStatements(SqliteStatement *
236245
return results;
237246
}
238247

248+
void SqliteStatement::prepareDbNames()
249+
{
250+
validDbNames = DBLIST->getValidDbNames();
251+
}
252+
239253
TokenList SqliteStatement::extractPrintableTokens(const TokenList &tokens, bool skipMeaningless)
240254
{
241255
TokenList list;

SQLiteStudio3/coreSQLiteStudio/parser/ast/sqlitestatement.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,16 @@ class API_EXPORT SqliteStatement : public QObject
333333
*/
334334
TokenPtr dbTokenForFullObjects;
335335

336+
/**
337+
* @brief List of database names as seen in the side Database List.
338+
* It is resoled at top-level statement being queried for databases and then it's propagated down to all child statements.
339+
* It helps to identify whether the xyz in "xyz." is a table or a database prefix.
340+
*/
341+
QStringList validDbNames;
342+
336343
private:
337344
QList<SqliteStatement*> getContextStatements(SqliteStatement* caller, bool checkParent, bool checkChilds);
345+
void prepareDbNames();
338346
};
339347

340348
#endif // SQLITESTATEMENT_H

SQLiteStudio3/coreSQLiteStudio/services/dbmanager.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class API_EXPORT DbManager : public QObject
107107

108108
/**
109109
* @brief Gives list of valid databases.
110-
* @return List of open databases.
110+
* @return List of valid databases.
111111
*/
112112
virtual QList<Db*> getValidDbList() = 0;
113113

@@ -123,6 +123,12 @@ class API_EXPORT DbManager : public QObject
123123
*/
124124
virtual QStringList getDbNames() = 0;
125125

126+
/**
127+
* @brief Gives list of valid database names.
128+
* @return List of database names that are registered and valid (no errors) in the application.
129+
*/
130+
virtual QStringList getValidDbNames() = 0;
131+
126132
/**
127133
* @brief Gives database object by its name.
128134
* @param name Symbolic name of the database.

SQLiteStudio3/coreSQLiteStudio/services/impl/dbmanagerimpl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ QStringList DbManagerImpl::getDbNames()
247247
return nameToDb.keys();
248248
}
249249

250+
QStringList DbManagerImpl::getValidDbNames()
251+
{
252+
QReadLocker lock(&listLock);
253+
QStringList result;
254+
for (Db* db : dbList)
255+
{
256+
if (db->isValid())
257+
result << db->getName();
258+
}
259+
return result;
260+
}
261+
250262
Db* DbManagerImpl::getByName(const QString &name, Qt::CaseSensitivity cs)
251263
{
252264
QReadLocker lock(&listLock);

SQLiteStudio3/coreSQLiteStudio/services/impl/dbmanagerimpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class API_EXPORT DbManagerImpl : public DbManager
4040
QList<Db*> getValidDbList();
4141
QList<Db*> getConnectedDbList();
4242
QStringList getDbNames();
43+
QStringList getValidDbNames();
4344
Db* getByName(const QString& name, Qt::CaseSensitivity cs = Qt::CaseInsensitive);
4445
Db* getByPath(const QString& path);
4546
Db* createInMemDb(bool pureInit = false);

0 commit comments

Comments
 (0)