From cd2f14e2dca3bde24a2a2a0228b5fc1dc4c5f709 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Mon, 16 Jan 2017 11:01:18 +0100 Subject: [PATCH] Remember when a table is only a temporary table You can create temporary table using CREATE TEMPORARY TABLE xxx statements. DB4S shows them as ordinary tables which is fine most of the time. However, it wouldn't remember the temporary status when editing them via the Edit Table dialog. This means that editing them would create a normal, non-temporary table. This is fixed by this commit. --- src/EditTableDialog.cpp | 4 +++- src/sqlitedb.cpp | 21 +++++++++++---------- src/sqlitedb.h | 7 +++++-- src/sqlitetypes.cpp | 3 ++- src/sqlitetypes.h | 6 +++++- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/EditTableDialog.cpp b/src/EditTableDialog.cpp index f830dfaff..48ee674ae 100644 --- a/src/EditTableDialog.cpp +++ b/src/EditTableDialog.cpp @@ -33,9 +33,11 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const QString& tableName, bool if(m_bNewTable == false) { // Existing table, so load and set the current layout - QString sTablesql = pdb.getObjectByName(curTable).getsql(); + DBBrowserObject obj = pdb.getObjectByName(curTable); + QString sTablesql = obj.getsql(); QPair parse_result = sqlb::Table::parseSQL(sTablesql); m_table = parse_result.first; + m_table.setTemporary(obj.isTemporary()); ui->labelEditWarning->setVisible(!parse_result.second); // Set without rowid checkbox. No need to trigger any events here as we're only loading a table exactly as it is stored by SQLite, so no need diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 99a734bcb..c74cee05f 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -1208,7 +1208,7 @@ void DBBrowserDB::updateSchema( ) if(!isOpen()) return; - QString statement = "SELECT type,name,sql,tbl_name FROM sqlite_master UNION SELECT type,name,sql,tbl_name FROM sqlite_temp_master;"; + QString statement = "SELECT type,name,sql,tbl_name,'0' AS temp FROM sqlite_master UNION SELECT type,name,sql,tbl_name,'1' AS temp FROM sqlite_temp_master;"; QByteArray utf8Statement = statement.toUtf8(); err=sqlite3_prepare_v2(_db, utf8Statement, utf8Statement.length(), @@ -1216,16 +1216,17 @@ void DBBrowserDB::updateSchema( ) if (err == SQLITE_OK){ logSQL(statement, kLogMsg_App); while ( sqlite3_step(vm) == SQLITE_ROW ){ - QString val1 = QString::fromUtf8((const char*)sqlite3_column_text(vm, 0)); - QString val2 = QString::fromUtf8((const char*)sqlite3_column_text(vm, 1)); - QString val3 = QString::fromUtf8((const char*)sqlite3_column_text(vm, 2)); - QString val4 = QString::fromUtf8((const char*)sqlite3_column_text(vm, 3)); - val3.replace("\r", ""); - - if(val1 == "table" || val1 == "index" || val1 == "view" || val1 == "trigger") - objMap.insert(val1, DBBrowserObject(val2, val3, val1, val4)); + QString val_type = QString::fromUtf8((const char*)sqlite3_column_text(vm, 0)); + QString val_name = QString::fromUtf8((const char*)sqlite3_column_text(vm, 1)); + QString val_sql = QString::fromUtf8((const char*)sqlite3_column_text(vm, 2)); + QString val_tblname = QString::fromUtf8((const char*)sqlite3_column_text(vm, 3)); + QString val_temp = QString::fromUtf8((const char*)sqlite3_column_text(vm, 4)); + val_sql.replace("\r", ""); + + if(val_type == "table" || val_type == "index" || val_type == "view" || val_type == "trigger") + objMap.insert(val_type, DBBrowserObject(val_name, val_sql, val_type, val_tblname, (val_temp == "1"))); else - qWarning() << tr("unknown object type %1").arg(val1); + qWarning() << tr("unknown object type %1").arg(val_type); } sqlite3_finalize(vm); }else{ diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 0cc791782..80b8091d5 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -22,20 +22,23 @@ class DBBrowserObject { public: DBBrowserObject() : table(""), name( "" ) { } - DBBrowserObject( const QString& wname,const QString& wsql, const QString& wtype, const QString& tbl_name ) - : table(wname), name( wname), sql( wsql ), type(wtype), table_name(tbl_name) + DBBrowserObject(const QString& wname, const QString& wsql, const QString& wtype, const QString& tbl_name, bool temp) + : table(wname), name( wname), sql( wsql ), type(wtype), table_name(tbl_name), temporary(temp) { } QString getname() const { return name; } QString getsql() const { return sql; } QString gettype() const { return type; } QString getTableName() const { return table_name; } + bool isTemporary() const { return temporary; } + sqlb::Table table; private: QString name; QString sql; QString type; QString table_name; // The name of the table this object references, interesting for views, triggers and indices + bool temporary; }; class DBBrowserDB : public QObject diff --git a/src/sqlitetypes.cpp b/src/sqlitetypes.cpp index f97a0e771..adbe8e3a9 100644 --- a/src/sqlitetypes.cpp +++ b/src/sqlitetypes.cpp @@ -138,6 +138,7 @@ void Table::clear() m_fields.clear(); m_constraints.clear(); m_virtual = QString(); + m_temporary = false; } Table::~Table() { @@ -297,7 +298,7 @@ QString Table::sql() const return QString("CREATE VIRTUAL TABLE %1 USING %2;").arg(escapeIdentifier(m_name)).arg(m_virtual); // This is a normal table, not a virtual one - QString sql = QString("CREATE TABLE %1 (\n").arg(escapeIdentifier(m_name)); + QString sql = QString("CREATE %1TABLE %2 (\n").arg(m_temporary ? QString("TEMPORARY ") : QString("")).arg(escapeIdentifier(m_name)); sql += fieldList().join(",\n"); diff --git a/src/sqlitetypes.h b/src/sqlitetypes.h index c3519795f..201a64649 100644 --- a/src/sqlitetypes.h +++ b/src/sqlitetypes.h @@ -160,7 +160,7 @@ typedef QMultiHash ConstraintMap; class Table { public: - explicit Table(const QString& name): m_name(name), m_rowidColumn("_rowid_") {} + explicit Table(const QString& name): m_name(name), m_rowidColumn("_rowid_"), m_temporary(false) {} virtual ~Table(); void setName(const QString& name) { m_name = name; } @@ -189,6 +189,9 @@ class Table QString virtualUsing() const { return m_virtual; } bool isVirtual() const { return !m_virtual.isEmpty(); } + void setTemporary(bool temp) { m_temporary = temp; } + bool isTemporary() const { return m_temporary; } + void clear(); void addConstraint(FieldVector fields, ConstraintPtr constraint); @@ -229,6 +232,7 @@ class Table QString m_rowidColumn; ConstraintMap m_constraints; QString m_virtual; + bool m_temporary; }; /**