Skip to content

Commit

Permalink
Remember when a table is only a temporary table
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MKleusberg committed Jan 16, 2017
1 parent 4ecb502 commit cd2f14e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/EditTableDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sqlb::Table, bool> 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
Expand Down
21 changes: 11 additions & 10 deletions src/sqlitedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,24 +1208,25 @@ 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(),
&vm, &tail);
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{
Expand Down
7 changes: 5 additions & 2 deletions src/sqlitedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/sqlitetypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void Table::clear()
m_fields.clear();
m_constraints.clear();
m_virtual = QString();
m_temporary = false;
}
Table::~Table()
{
Expand Down Expand Up @@ -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");

Expand Down
6 changes: 5 additions & 1 deletion src/sqlitetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ typedef QMultiHash<FieldVector, ConstraintPtr> 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; }
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -229,6 +232,7 @@ class Table
QString m_rowidColumn;
ConstraintMap m_constraints;
QString m_virtual;
bool m_temporary;
};

/**
Expand Down

0 comments on commit cd2f14e

Please sign in to comment.