diff --git a/src/EditTableForm.cpp b/src/EditTableForm.cpp index 50496002..ae7c0a11 100644 --- a/src/EditTableForm.cpp +++ b/src/EditTableForm.cpp @@ -133,17 +133,10 @@ void editTableForm::checkInput() void editTableForm::editField() { - if( !ui->treeWidget->currentItem()){ + if(!ui->treeWidget->currentItem()) return; - } - - // Cancel here when the user tries to edit an existing table - if(curTable != "") - { - QMessageBox::information(this, QApplication::applicationName(), tr("Sorry! This function is currently not implemented as SQLite does not support editing columns yet.")); - return; - } + // Show the edit dialog QTreeWidgetItem *item = ui->treeWidget->currentItem(); editFieldForm dialog(this); dialog.setInitialValues(pdb, curTable == "", curTable, item->text(0), item->text(1)); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 935e1a6a..17a77d34 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1103,16 +1103,14 @@ void MainWindow::editField(){ if(!ui->dbTreeWidget->currentItem()) return; - // TODO - QMessageBox::information(this, QApplication::applicationName(), tr("Sorry! This function is currently not implemented as SQLite does not support editing columns yet.")); - /*QTreeWidgetItem *item = ui->dbTreeWidget->currentItem(); + QTreeWidgetItem *item = ui->dbTreeWidget->currentItem(); editFieldForm dialog(this); dialog.setInitialValues(&db, false, item->parent()->text(0), item->text(0), item->text(2)); if(dialog.exec()) { item->setText(0, dialog.field_name); item->setText(2, dialog.field_type); - }*/ + } } void MainWindow::deleteField(){ diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 2ad7f456..eba42b47 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -420,11 +420,85 @@ bool DBBrowserDB::createColumn( QString tablename, QString fieldname, QString fi return executeSQL(sql); } -bool DBBrowserDB::renameColumn(QString tablename, QString from, QString to, QString type) { - qDebug("rename column"); - // TODO change column name and type. SQLite doesn't support natively... +bool DBBrowserDB::renameColumn(QString tablename, QString from, QString to, QString type) +{ + // NOTE: This function is working around the incomplete ALTER TABLE command in SQLite. If SQLite should fully support this command one day, this entire + // function can be changed to executing something like this: //QString sql = QString("ALTER TABLE `%1` MODIFY `%2` %3").arg(tablename).arg(to).arg(type); //return executeSQL(sql); + + // Collect information on the current DB layout + DBBrowserObject table = getObjectByName(tablename); + if(table.getname() == "" || table.getField(from).getname() == "") + { + lastErrorMessage = QString("renameColumn: cannot find table %1 with column %2").arg(tablename).arg(from); + qDebug(lastErrorMessage.toStdString().c_str()); + return false; + } + + // Create savepoint to be able to go back to it in case of any error + if(!executeSQL("SAVEPOINT sqlitebrowser_rename_column")) + { + lastErrorMessage = "renameColumn: creating savepoint failed"; + qDebug(lastErrorMessage.toStdString().c_str()); + 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 change + // of course + QString sql = QString("CREATE TABLE sqlitebrowser_rename_column_new_table ("); + for(int i=0;i