mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 19:11:39 -06:00
Fix editing table name and table columns at once
Renaming a table always happens *after* editing all table columns (at least that's the order we're doing it currently). So for all renameColumn() names we'll need to use the table name as it is in the database, *not* as it is in the table name widget because that might have been changed in the meantime.
This commit is contained in:
@@ -216,7 +216,7 @@ void EditTableDialog::updateTypes()
|
||||
|
||||
m_table.fields().at(index)->setType(type);
|
||||
if(!m_bNewTable)
|
||||
pdb.renameColumn(m_table, column, m_table.fields().at(index));
|
||||
pdb.renameColumn(curTable, m_table, column, m_table.fields().at(index));
|
||||
checkInput();
|
||||
}
|
||||
}
|
||||
@@ -459,7 +459,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
|
||||
|
||||
if(callRenameColumn)
|
||||
{
|
||||
if(!pdb.renameColumn(m_table, oldFieldName, field))
|
||||
if(!pdb.renameColumn(curTable, m_table, oldFieldName, field))
|
||||
QMessageBox::warning(this, qApp->applicationName(), tr("Modifying this column failed. Error returned from database:\n%1").arg(pdb.lastErrorMessage));
|
||||
}
|
||||
}
|
||||
@@ -546,7 +546,7 @@ void EditTableDialog::removeField()
|
||||
QString msg = tr("Are you sure you want to delete the field '%1'?\nAll data currently stored in this field will be lost.").arg(ui->treeWidget->currentItem()->text(0));
|
||||
if(QMessageBox::warning(this, QApplication::applicationName(), msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
|
||||
{
|
||||
if(!pdb.renameColumn(m_table, ui->treeWidget->currentItem()->text(0), sqlb::FieldPtr()))
|
||||
if(!pdb.renameColumn(curTable, m_table, ui->treeWidget->currentItem()->text(0), sqlb::FieldPtr()))
|
||||
{
|
||||
QMessageBox::warning(0, QApplication::applicationName(), pdb.lastErrorMessage);
|
||||
} else {
|
||||
@@ -625,6 +625,7 @@ void EditTableDialog::moveCurrentField(bool down)
|
||||
|
||||
// Move the actual column
|
||||
if(!pdb.renameColumn(
|
||||
curTable,
|
||||
m_table,
|
||||
ui->treeWidget->currentItem()->text(0),
|
||||
m_table.fields().at(ui->treeWidget->indexOfTopLevelItem(ui->treeWidget->currentItem())),
|
||||
@@ -681,7 +682,7 @@ void EditTableDialog::setWithoutRowid(bool without_rowid)
|
||||
// Update table if we're editing an existing table
|
||||
if(!m_bNewTable)
|
||||
{
|
||||
if(!pdb.renameColumn(m_table, QString(), sqlb::FieldPtr(), 0))
|
||||
if(!pdb.renameColumn(curTable, m_table, QString(), sqlb::FieldPtr(), 0))
|
||||
{
|
||||
QMessageBox::warning(this, QApplication::applicationName(),
|
||||
tr("Setting the rowid column for the table failed. Error message:\n%1").arg(pdb.lastErrorMessage));
|
||||
|
||||
@@ -967,7 +967,7 @@ bool DBBrowserDB::addColumn(const QString& tablename, const sqlb::FieldPtr& fiel
|
||||
return executeSQL(sql);
|
||||
}
|
||||
|
||||
bool DBBrowserDB::renameColumn(const sqlb::Table& table, const QString& name, sqlb::FieldPtr to, int move)
|
||||
bool DBBrowserDB::renameColumn(const QString& tablename, const sqlb::Table& table, const QString& name, sqlb::FieldPtr to, int move)
|
||||
{
|
||||
/*
|
||||
* USE CASES:
|
||||
@@ -994,10 +994,10 @@ bool DBBrowserDB::renameColumn(const sqlb::Table& table, const QString& name, sq
|
||||
// 3) Maybe rename this function to alterTable() or something
|
||||
|
||||
// Collect information on the current DB layout
|
||||
QString tableSql = getObjectByName(table.name()).getsql();
|
||||
QString tableSql = getObjectByName(tablename).getsql();
|
||||
if(tableSql.isEmpty())
|
||||
{
|
||||
lastErrorMessage = tr("renameColumn: cannot find table %1.").arg(table.name());
|
||||
lastErrorMessage = tr("renameColumn: cannot find table %1.").arg(tablename);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1069,7 +1069,7 @@ bool DBBrowserDB::renameColumn(const sqlb::Table& table, const QString& name, sq
|
||||
}
|
||||
|
||||
// Copy the data from the old table to the new one
|
||||
if(!executeSQL(QString("INSERT INTO sqlitebrowser_rename_column_new_table SELECT %1 FROM %2;").arg(select_cols).arg(sqlb::escapeIdentifier(table.name()))))
|
||||
if(!executeSQL(QString("INSERT INTO sqlitebrowser_rename_column_new_table SELECT %1 FROM %2;").arg(select_cols).arg(sqlb::escapeIdentifier(tablename))))
|
||||
{
|
||||
QString error(tr("renameColumn: copying data to new table failed. DB says:\n%1").arg(lastErrorMessage));
|
||||
revertToSavepoint("sqlitebrowser_rename_column");
|
||||
@@ -1082,7 +1082,7 @@ bool DBBrowserDB::renameColumn(const sqlb::Table& table, const QString& name, sq
|
||||
for(auto it=objMap.constBegin();it!=objMap.constEnd();++it)
|
||||
{
|
||||
// If this object references the table and it's not the table itself save it's SQL string
|
||||
if((*it).getTableName() == table.name() && (*it).gettype() != "table")
|
||||
if((*it).getTableName() == tablename && (*it).gettype() != "table")
|
||||
otherObjectsSql += (*it).getsql().trimmed() + ";\n";
|
||||
}
|
||||
|
||||
@@ -1095,7 +1095,7 @@ bool DBBrowserDB::renameColumn(const sqlb::Table& table, const QString& name, sq
|
||||
setPragma("defer_foreign_keys", "1");
|
||||
|
||||
// Delete the old table
|
||||
if(!executeSQL(QString("DROP TABLE %1;").arg(sqlb::escapeIdentifier(table.name())), true, true))
|
||||
if(!executeSQL(QString("DROP TABLE %1;").arg(sqlb::escapeIdentifier(tablename)), true, true))
|
||||
{
|
||||
QString error(tr("renameColumn: deleting old table failed. DB says: %1").arg(lastErrorMessage));
|
||||
revertToSavepoint("sqlitebrowser_rename_column");
|
||||
@@ -1104,7 +1104,7 @@ bool DBBrowserDB::renameColumn(const sqlb::Table& table, const QString& name, sq
|
||||
}
|
||||
|
||||
// Rename the temporary table
|
||||
if(!renameTable("sqlitebrowser_rename_column_new_table", table.name()))
|
||||
if(!renameTable("sqlitebrowser_rename_column_new_table", tablename))
|
||||
{
|
||||
revertToSavepoint("sqlitebrowser_rename_column");
|
||||
return false;
|
||||
|
||||
@@ -94,13 +94,14 @@ public:
|
||||
|
||||
/**
|
||||
* @brief renameColumn Can be used to rename, modify or drop an existing column of a given table
|
||||
* @param table Specifies the table to edit. The table name and the table constraints are used from this but not the columns
|
||||
* @param table_name Specifies the name of the table to edit
|
||||
* @param table Specifies the table to edit. The table constraints are used from this but not the columns
|
||||
* @param name Name of the column to edit
|
||||
* @param to The new field definition with changed name, type or the like. If Null-Pointer is given the column is dropped.
|
||||
* @param move Set this to a value != 0 to move the new column to a different position
|
||||
* @return true if renaming was successful, false if not. In the latter case also lastErrorMessage is set
|
||||
*/
|
||||
bool renameColumn(const sqlb::Table& table, const QString& name, sqlb::FieldPtr to, int move = 0);
|
||||
bool renameColumn(const QString& tablename, const sqlb::Table& table, const QString& name, sqlb::FieldPtr to, int move = 0);
|
||||
|
||||
objectMap getBrowsableObjects() const;
|
||||
DBBrowserObject getObjectByName(const QString& name) const;
|
||||
|
||||
Reference in New Issue
Block a user