From db39e3e2937b764469276e9dbc65855637c746f1 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Tue, 4 Jun 2013 16:42:31 +0200 Subject: [PATCH] DBBrowserDB: Attempt to restore views, indices etc. in renameColumn() Save views, triggers and indices before deleting the old table in renameColumn() and try to create them again after renaming the new table. This is likely to fail and we'd probably need a grammar for these objects to make the appropriate changes, so in case it does fail this just prints a warning with some ideas of what to do. --- src/sqlitedb.cpp | 23 +++++++++++++++++++++-- src/sqlitedb.h | 6 ++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 5a5c7678..9dd49ac1 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -573,6 +573,15 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq return false; } + // Save all indices, triggers and views associated with this table because SQLite deletes them when we drop the table in the next step + QString otherObjectsSql; + for(objectMap::ConstIterator it=objMap.begin();it!=objMap.end();++it) + { + // If this object references the table and it's not the table itself save it's SQL string + if((*it).getTableName() == tablename && (*it).gettype() != "table") + otherObjectsSql += (*it).getsql() + "\n"; + } + // Delete the old table if(!executeSQL(QString("DROP TABLE `%1`;").arg(tablename))) { @@ -589,6 +598,15 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq return false; } + // Restore the saved triggers, views and indices + if(!executeMultiSQL(otherObjectsSql, true, true)) + { + QMessageBox::information(0, qApp->applicationName(), QObject::tr("Restoring some of the objects associated with this table failed. " + "This is most likely because some column namaes changed. " + "Here's the SQL statement which you might want to fix and execute manually:\n\n") + + otherObjectsSql); + } + // Release the savepoint - everything went fine if(!executeSQL("RELEASE SAVEPOINT sqlitebrowser_rename_column;")) { @@ -845,7 +863,7 @@ void DBBrowserDB::updateSchema( ) objMap.clear(); lastErrorMessage = QObject::tr("no error"); - QString statement = "SELECT type, name, sql FROM sqlite_master;"; + QString statement = "SELECT type, name, sql, tbl_name FROM sqlite_master;"; QByteArray utf8Statement = statement.toUtf8(); err=sqlite3_prepare_v2(_db, utf8Statement, utf8Statement.length(), @@ -856,9 +874,10 @@ void DBBrowserDB::updateSchema( ) 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)); if(val1 == "table" || val1 == "index" || val1 == "view" || val1 == "trigger") - objMap.insert(val1, DBBrowserObject(val2, val3, val1)); + objMap.insert(val1, DBBrowserObject(val2, val3, val1, val4)); else qWarning() << QObject::tr("unknown object type %1").arg(val1); } diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 024cb3d2..ed13790a 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -22,8 +22,8 @@ class DBBrowserObject { public: DBBrowserObject() : name( "" ) { } - DBBrowserObject( const QString& wname,const QString& wsql, const QString& wtype ) - : name( wname), sql( wsql ), type(wtype) + DBBrowserObject( const QString& wname,const QString& wsql, const QString& wtype, const QString& tbl_name ) + : name( wname), sql( wsql ), type(wtype), table_name(tbl_name) { } void addField(sqlb::FieldPtr field) { fldmap.push_back(field); } @@ -31,6 +31,7 @@ public: QString getname() const { return name; } QString getsql() const { return sql; } QString gettype() const { return type; } + QString getTableName() const { return table_name; } sqlb::FieldPtr getField(const QString& name) const { for(int i=0;i