From 35449e7c1aca6ea872cb104ab40c6c17eba8e18f Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Fri, 22 Mar 2013 15:56:07 +0100 Subject: [PATCH] rework the dropcolumn function it now uses the sqlb::Table object preserving: not null, default value, check constraint --- src/sqlitedb.cpp | 50 ++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index a8f26114..470f067c 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -1,5 +1,7 @@ #include "sqlitedb.h" + #include "MainWindow.h" +#include "sqlitetypes.h" #include #include @@ -600,11 +602,20 @@ bool DBBrowserDB::dropColumn(const QString& tablename, const QString& column) //return executeSQL(sql); // Collect information on the current DB layout - DBBrowserObject table = getObjectByName(tablename); - if(table.getname() == "" || table.getField(column).getname() == "" || table.fldmap.count() == 1) + QString sTableSql = getTableSQL(tablename); + if(sTableSql.isEmpty()) { - lastErrorMessage = QObject::tr("dropColumn: cannot find table %1 or column %2. Also you can not delete the last column").arg(tablename).arg(column); - qDebug(lastErrorMessage.toStdString().c_str()); + lastErrorMessage = QObject::tr("dropColumn: cannot find table %1.").arg(tablename); + qWarning() << lastErrorMessage; + return false; + } + + sqlb::Table oldSchema = sqlb::Table::parseSQL(sTableSql); + + if(oldSchema.findField(column) == -1 || oldSchema.fields().count() == 1) + { + lastErrorMessage = QObject::tr("dropColumn: cannot find column %1. Also you can not delete the last column").arg(column); + qWarning() << lastErrorMessage; return false; } @@ -612,31 +623,32 @@ bool DBBrowserDB::dropColumn(const QString& tablename, const QString& column) if(!executeSQL("SAVEPOINT sqlitebrowser_drop_column;", false)) { lastErrorMessage = QObject::tr("dropColumn: creating savepoint failed"); - qDebug(lastErrorMessage.toStdString().c_str()); + qWarning() << lastErrorMessage; return false; } // Create a new table with a name that hopefully doesn't exist yet. Its layout is exactly the same as the one of the table to change - except for the column to drop // of course. Also prepare the columns to be copied in a later step now. - QList new_table_structure; + sqlb::Table newSchema = oldSchema; + newSchema.setName("sqlitebrowser_drop_column_new_table"); + newSchema.removeField(column); + QString select_cols; - for(int i=0;iname())); } - if(!createTable("sqlitebrowser_drop_column_new_table", new_table_structure)) + // remove last , + select_cols.remove(select_cols.count() -1, 1); + + // create the new table + if(!executeSQL(newSchema.sql(), false)) { lastErrorMessage = QObject::tr("dropColumn: creating new table failed. DB says: %1").arg(lastErrorMessage); - qDebug(lastErrorMessage.toStdString().c_str()); + qWarning() << lastErrorMessage; executeSQL("ROLLBACK TO SAVEPOINT sqlitebrowser_drop_column;", false); return false; } - select_cols.remove(select_cols.count() - 1, 1); // Copy the data from the old table to the new one if(!executeSQL( @@ -644,7 +656,7 @@ bool DBBrowserDB::dropColumn(const QString& tablename, const QString& column) false)) { lastErrorMessage = QObject::tr("dropColumn: copying data to new table failed. DB says: %1").arg(lastErrorMessage); - qDebug(lastErrorMessage.toStdString().c_str()); + qWarning() << lastErrorMessage; executeSQL("ROLLBACK TO SAVEPOINT sqlitebrowser_drop_column;"); return false; } @@ -653,7 +665,7 @@ bool DBBrowserDB::dropColumn(const QString& tablename, const QString& column) if(!executeSQL(QString("DROP TABLE `%1`;").arg(tablename), false)) { lastErrorMessage = QObject::tr("dropColumn: deleting old table failed. DB says: %1").arg(lastErrorMessage); - qDebug(lastErrorMessage.toStdString().c_str()); + qWarning() << lastErrorMessage; executeSQL("ROLLBACK TO SAVEPOINT sqlitebrowser_drop_column;"); return false; } @@ -669,7 +681,7 @@ bool DBBrowserDB::dropColumn(const QString& tablename, const QString& column) if(!executeSQL("RELEASE SAVEPOINT sqlitebrowser_drop_column;", false)) { lastErrorMessage = QObject::tr("dropColumn: releasing savepoint failed. DB says: %1").arg(lastErrorMessage); - qDebug(lastErrorMessage.toStdString().c_str()); + qWarning() << lastErrorMessage; return false; }