SqliteTypes: Move PK flag from table to field

Store the primary key flag(s) inside the sqlb::Field objects instead of
the sqlb::Table object. Technically this doesn't make a lot of sense but
then again it makes things a lot easier for us. So this should fix quite
a few issues in the entire program, especially - again - in
renameColumn(). It also magically fixes createColumn() which had no
chance of guessing which column should be a PK prior to this.

To benefit from these changes the EditTableDialog has changes as well.
- It should now be possible to set and unset PK and AI flags and they
  are actually saved.
- Setting the AI flag automatically sets the data type to Integer
  because that's the only type SQLite can handle in an autoincrement
  field.
- Clicking on the entry in the data type combobox which is currently
  selected doesn't update the DB anymore.

This commit also makes some changes to the unit tests to reflect the API
changes made. But it also adds missing quote characters in some
verification strings.

I hope this is a worthy 500th commit - at least it's got a long commit
message...
This commit is contained in:
Martin Kleusberg
2013-06-03 20:15:29 +02:00
parent 3a85d96a6d
commit 63cf871017
4 changed files with 60 additions and 96 deletions

View File

@@ -75,7 +75,6 @@ void EditTableDialog::populateFields()
tbitem->setText(kName, f->name());
QComboBox* typeBox = new QComboBox(ui->treeWidget);
typeBox->setProperty("column", f->name());
connect(typeBox, SIGNAL(activated(int)), this, SLOT(updateTypes()));
typeBox->setEditable(false);
typeBox->addItems(sqlb::Field::Datatypes);
int index = typeBox->findText(f->type(), Qt::MatchExactly);
@@ -86,10 +85,11 @@ void EditTableDialog::populateFields()
index = typeBox->count() - 1;
}
typeBox->setCurrentIndex(index);
connect(typeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTypes()));
ui->treeWidget->setItemWidget(tbitem, kType, typeBox);
tbitem->setCheckState(kNotNull, f->notnull() ? Qt::Checked : Qt::Unchecked);
tbitem->setCheckState(kPrimaryKey, m_table.primarykey().contains(f) ? Qt::Checked : Qt::Unchecked);
tbitem->setCheckState(kPrimaryKey, f->primaryKey() ? Qt::Checked : Qt::Unchecked);
tbitem->setCheckState(kAutoIncrement, f->autoIncrement() ? Qt::Checked : Qt::Unchecked);
tbitem->setText(kDefault, f->defaultValue());
tbitem->setText(kCheck, f->check());
@@ -204,28 +204,21 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
break;
case kPrimaryKey:
{
sqlb::FieldVector pks = m_table.primarykey();
field->setPrimaryKey(item->checkState(column) == Qt::Checked);
if(item->checkState(column) == Qt::Checked)
{
pks.append(field);
// this will unset any set autoincrement
// this will unset any other autoincrement
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i)
{
QTreeWidgetItem* tbitem = ui->treeWidget->topLevelItem(i);
if(tbitem != item)
{
tbitem->setCheckState(kAutoIncrement, Qt::Unchecked);
}
}
}
else
{
} else {
item->setCheckState(kAutoIncrement, Qt::Unchecked);
int index = pks.indexOf(field);
if(index != -1)
pks.remove(index);
}
m_table.setPrimaryKey(pks);
if(!m_bNewTable)
callRenameColumn = true;
}
break;
case kNotNull:
@@ -255,13 +248,14 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
case kAutoIncrement:
{
bool ischecked = item->checkState(column) == Qt::Checked;
field->setAutoIncrement(ischecked);
if(ischecked)
{
item->setCheckState(kPrimaryKey, Qt::Checked);
QComboBox* comboType = qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(item, kType));
comboType->setCurrentIndex(comboType->findText("INTEGER"));
item->setCheckState(kPrimaryKey, Qt::Checked);
// this will reset all other primary keys unset
// there can't be more then one autoincrement pk
// this will unset all other primary keys
// there can't be more than one autoincrement pk
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i)
{
QTreeWidgetItem* tbitem = ui->treeWidget->topLevelItem(i);
@@ -272,6 +266,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
}
}
}
field->setAutoIncrement(ischecked);
if(!m_bNewTable)
callRenameColumn = true;