EditTableDialog: Allow some more changes to existing tables

Allow setting the default value, the check values and under some
circumstances the not null flag when editing existing tables.

This required some changes to DBBrowserDB::renameColumn() which is now
using some more features of sqlitetypes.cpp but could still be improved.
This commit is contained in:
Martin Kleusberg
2013-05-31 16:13:58 +02:00
parent 82d78384e0
commit b9afbe2aea
4 changed files with 47 additions and 32 deletions

View File

@@ -194,17 +194,17 @@ void EditTableDialog::updateTypes()
{
QString type = typeBox->currentText();
QString column = sender()->property("column").toString();
if(m_bNewTable || pdb->renameColumn(curTable, column, column, type))
int index;
for(index=0;index<m_table.fields().size();index++)
{
for(int i=0;i<m_table.fields().size();i++)
{
if(m_table.fields().at(i)->name() == column)
{
m_table.fields().at(i)->setType(type);
break;
}
}
if(m_table.fields().at(index)->name() == column)
break;
}
m_table.fields().at(index)->setType(type);
if(!m_bNewTable)
pdb->renameColumn(curTable, column, m_table.fields().at(index));
checkInput();
}
}
@@ -215,14 +215,15 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
if(index < m_table.fields().count())
{
sqlb::FieldPtr field = m_table.fields().at(index);
bool callRenameColumn = false;
switch(column)
{
case kName:
if(m_bNewTable || pdb->renameColumn(curTable, field->name(), item->text(column), field->type()))
{
qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(item, kType))->setProperty("column", item->text(column));
field->setName(item->text(column));
}
field->setName(item->text(column));
qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(item, kType))->setProperty("column", item->text(column));
if(!m_bNewTable)
callRenameColumn = true;
break;
case kType:
// see updateTypes() SLOT
@@ -256,6 +257,8 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
case kNotNull:
{
field->setNotNull(item->checkState(column) == Qt::Checked);
if(!m_bNewTable)
callRenameColumn = true;
}
break;
case kAutoIncrement:
@@ -278,11 +281,25 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
}
}
}
if(!m_bNewTable)
callRenameColumn = true;
}
break;
case kDefault: field->setDefaultValue(item->text(column)); break;
case kCheck: field->setCheck(item->text(column)); break;
case kDefault:
field->setDefaultValue(item->text(column));
if(!m_bNewTable)
callRenameColumn = true;
break;
case kCheck:
field->setCheck(item->text(column));
if(!m_bNewTable)
callRenameColumn = true;
break;
}
if(callRenameColumn)
pdb->renameColumn(curTable, field->name(), field);
}
checkInput();

View File

@@ -47,7 +47,7 @@ void ImportCsvDialog::accept()
return;
// Generate field names. These are either taken from the first CSV row or are generated in the format of "fieldXY" depending on the user input
QList<DBBrowserField> fieldList;
QStringList fieldList;
if(ui->checkboxHeader->isChecked())
{
int cfieldnum = 0;
@@ -65,13 +65,13 @@ void ImportCsvDialog::accept()
if(thisfield.isEmpty())
thisfield = QString("field%1").arg(cfieldnum+1);
fieldList.push_back(DBBrowserField(thisfield, ""));
fieldList.push_back(thisfield);
cfieldnum++;
curList.pop_front();
}
} else {
for(int i=0;i<numfields;i++)
fieldList.push_back(DBBrowserField(QString("field%1").arg(i+1), ""));
fieldList.push_back(QString("field%1").arg(i+1));
}
// Show progress dialog

View File

@@ -500,12 +500,12 @@ bool DBBrowserDB::updateRecord(const QString& table, const QString& column, int
}
}
bool DBBrowserDB::createTable(const QString& name, const QList<DBBrowserField>& structure)
bool DBBrowserDB::createTable(const QString& name, const QStringList& structure)
{
// Build SQL statement
QString sql = QString("CREATE TABLE `%1` (").arg(name);
for(int i=0;i<structure.count();i++)
sql.append(QString("`%1` %2,").arg(structure.at(i).getname()).arg(structure.at(i).gettype()));
sql.append(QString("%1,").arg(structure.at(i)));
sql.remove(sql.count() - 1, 1); // Remove last comma
sql.append(");");
@@ -525,7 +525,7 @@ bool DBBrowserDB::addColumn(const QString& tablename, const sqlb::FieldPtr& fiel
return result;
}
bool DBBrowserDB::renameColumn(const QString& tablename, const QString& from, const QString& to, const QString& type)
bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to)
{
// NOTE: This function is working around the incomplete ALTER TABLE command in SQLite.
// If SQLite should fully support this command one day, this entire
@@ -535,9 +535,9 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& from, co
// Collect information on the current DB layout
DBBrowserObject table = getObjectByName(tablename);
if(table.getname() == "" || table.getField(from).getname() == "")
if(table.getname() == "" || table.getField(name).getname() == "")
{
lastErrorMessage = QObject::tr("renameColumn: cannot find table %1 with column %2").arg(tablename).arg(from);
lastErrorMessage = QObject::tr("renameColumn: cannot find table %1 with column %2").arg(tablename).arg(name);
qWarning() << lastErrorMessage;
return false;
}
@@ -553,16 +553,14 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& from, co
// 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
QList<DBBrowserField> new_table_structure;
QStringList new_table_structure;
for(int i=0;i<table.fldmap.count();i++)
{
// Is this the column to rename?
if(table.fldmap.value(i).getname() == from)
new_table_structure.push_back(DBBrowserField(to, type));
if(table.fldmap.value(i).getname() == name)
new_table_structure.push_back(to->toString());
else
new_table_structure.push_back(
DBBrowserField(table.fldmap.value(i).getname(), table.fldmap.value(i).gettype())
);
new_table_structure.push_back(QString("`%1` %2").arg(table.fldmap.value(i).getname()).arg(table.fldmap.value(i).gettype()));
}
if(!createTable("sqlitebrowser_rename_column_new_table", new_table_structure))
{

View File

@@ -90,10 +90,10 @@ public:
bool deleteRecord(const QString& table, int rowid);
bool updateRecord(const QString& table, const QString& column, int row, const QByteArray& value);
bool createTable(const QString& name, const QList<DBBrowserField>& structure);
bool createTable(const QString& name, const QStringList& structure);
bool renameTable(const QString& from_table, const QString& to_table);
bool addColumn(const QString& table, const sqlb::FieldPtr& field);
bool renameColumn(const QString& tablename, const QString& from, const QString& to, const QString& type);
bool renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to);
bool dropColumn(const QString& tablename, const QString& column);
QStringList getTableFields(const QString & tablename) const;