Skip to content

Commit

Permalink
Support creating databases in schemata other than main and temp
Browse files Browse the repository at this point in the history
This replaces the checkbox for creating tables in the temporary schema
by a dropdown box that lets you select between all available schemata,
i.e. main, temp, and all attached databases. This way it becomes
possible to create new tables in attached databases as well as move
existing tables between all schemata.
  • Loading branch information
MKleusberg committed Sep 4, 2017
1 parent 7db96cd commit 72d64ed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
19 changes: 12 additions & 7 deletions src/EditTableDialog.cpp
Expand Up @@ -36,15 +36,20 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
m_table = *(pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>());
ui->labelEditWarning->setVisible(!m_table.fullyParsed());

// Set without rowid and temporary checkboxex. No need to trigger any events here as we're only loading a table exactly as it is stored by SQLite, so no need
// Set without rowid checkbox and schema dropdown. No need to trigger any events here as we're only loading a table exactly as it is stored by SQLite, so no need
// for error checking etc.
ui->checkWithoutRowid->blockSignals(true);
ui->checkWithoutRowid->setChecked(m_table.isWithoutRowidTable());
ui->checkTemporary->setChecked(curTable.schema() == "temp");
ui->checkWithoutRowid->blockSignals(false);
ui->comboSchema->blockSignals(true);
ui->comboSchema->addItems(pdb.schemata.keys()); // Load list of database schemata
ui->comboSchema->setCurrentText(curTable.schema());
ui->comboSchema->blockSignals(false);

populateFields();
} else {
ui->comboSchema->addItems(pdb.schemata.keys()); // Load list of database schemata
ui->comboSchema->setCurrentText("main"); // Always create tables in the main schema by default
ui->labelEditWarning->setVisible(false);
}

Expand Down Expand Up @@ -149,7 +154,7 @@ void EditTableDialog::accept()
if(m_bNewTable)
{
// Creation of new table
if(!pdb.executeSQL(m_table.sql()))
if(!pdb.executeSQL(m_table.sql(ui->comboSchema->currentText())))
{
QMessageBox::warning(
this,
Expand All @@ -163,7 +168,7 @@ void EditTableDialog::accept()
// Rename table if necessary
if(ui->editTableName->text() != curTable.name())
{
if(!pdb.renameTable(curTable.schema(), curTable.name(), ui->editTableName->text()))
if(!pdb.renameTable(ui->comboSchema->currentText(), curTable.name(), ui->editTableName->text()))
{
QMessageBox::warning(this, QApplication::applicationName(), pdb.lastError());
return;
Expand All @@ -184,7 +189,7 @@ void EditTableDialog::reject()

void EditTableDialog::updateSqlText()
{
ui->sqlTextEdit->setText(m_table.sql());
ui->sqlTextEdit->setText(m_table.sql(ui->comboSchema->currentText()));
}

void EditTableDialog::checkInput()
Expand Down Expand Up @@ -718,15 +723,15 @@ void EditTableDialog::setWithoutRowid(bool without_rowid)
}
}

void EditTableDialog::setTemporary(bool is_temp)
void EditTableDialog::changeSchema(const QString& schema)
{
// Update the SQL preview
updateSqlText();

// Update table if we're editing an existing table
if(!m_bNewTable)
{
if(!pdb.renameColumn(curTable, m_table, QString(), sqlb::FieldPtr(), 0, is_temp ? "temp" : "main"))
if(!pdb.renameColumn(curTable, m_table, QString(), sqlb::FieldPtr(), 0, schema))
{
QMessageBox::warning(this, QApplication::applicationName(),
tr("Setting the temporary flag for the table failed. Error message:\n%1").arg(pdb.lastError()));
Expand Down
2 changes: 1 addition & 1 deletion src/EditTableDialog.h
Expand Up @@ -55,7 +55,7 @@ private slots:
void moveUp();
void moveDown();
void setWithoutRowid(bool without_rowid);
void setTemporary(bool is_temp);
void changeSchema(const QString& schema);

private:
Ui::EditTableDialog* ui;
Expand Down
42 changes: 26 additions & 16 deletions src/EditTableDialog.ui
Expand Up @@ -55,24 +55,34 @@
</item>
<item>
<widget class="QWidget" name="widgetExtension" native="true">
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QCheckBox" name="checkWithoutRowid">
<property name="toolTip">
<string>Make this a 'WITHOUT rowid' table. Setting this flag requires a field of type INTEGER with the primary key flag set and the auto increment flag unset.</string>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Database schema</string>
</property>
<property name="buddy">
<cstring>comboSchema</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboSchema"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_1">
<property name="text">
<string>Without Rowid</string>
</property>
<property name="buddy">
<cstring>checkWithoutRowid</cstring>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkTemporary">
<item row="1" column="1">
<widget class="QCheckBox" name="checkWithoutRowid">
<property name="toolTip">
<string>Set this to create a temporary table that is deleted when closing the database.</string>
</property>
<property name="text">
<string>Temporary table</string>
<string>Make this a 'WITHOUT rowid' table. Setting this flag requires a field of type INTEGER with the primary key flag set and the auto increment flag unset.</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -331,8 +341,8 @@
<tabstops>
<tabstop>editTableName</tabstop>
<tabstop>buttonMore</tabstop>
<tabstop>comboSchema</tabstop>
<tabstop>checkWithoutRowid</tabstop>
<tabstop>checkTemporary</tabstop>
<tabstop>addFieldButton</tabstop>
<tabstop>removeFieldButton</tabstop>
<tabstop>buttonMoveUp</tabstop>
Expand Down Expand Up @@ -521,10 +531,10 @@
</hints>
</connection>
<connection>
<sender>checkTemporary</sender>
<signal>toggled(bool)</signal>
<sender>comboSchema</sender>
<signal>currentIndexChanged(QString)</signal>
<receiver>EditTableDialog</receiver>
<slot>setTemporary(bool)</slot>
<slot>changeSchema(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>186</x>
Expand All @@ -547,6 +557,6 @@
<slot>moveUp()</slot>
<slot>moveDown()</slot>
<slot>setWithoutRowid(bool)</slot>
<slot>setTemporary(bool)</slot>
<slot>changeSchema(QString)</slot>
</slots>
</ui>

0 comments on commit 72d64ed

Please sign in to comment.