Skip to content

Commit 72d64ed

Browse files
committed
Support creating databases in schemata other than main and temp
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.
1 parent 7db96cd commit 72d64ed

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

src/EditTableDialog.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,20 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
3636
m_table = *(pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>());
3737
ui->labelEditWarning->setVisible(!m_table.fullyParsed());
3838

39-
// 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
39+
// 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
4040
// for error checking etc.
4141
ui->checkWithoutRowid->blockSignals(true);
4242
ui->checkWithoutRowid->setChecked(m_table.isWithoutRowidTable());
43-
ui->checkTemporary->setChecked(curTable.schema() == "temp");
4443
ui->checkWithoutRowid->blockSignals(false);
44+
ui->comboSchema->blockSignals(true);
45+
ui->comboSchema->addItems(pdb.schemata.keys()); // Load list of database schemata
46+
ui->comboSchema->setCurrentText(curTable.schema());
47+
ui->comboSchema->blockSignals(false);
4548

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

@@ -149,7 +154,7 @@ void EditTableDialog::accept()
149154
if(m_bNewTable)
150155
{
151156
// Creation of new table
152-
if(!pdb.executeSQL(m_table.sql()))
157+
if(!pdb.executeSQL(m_table.sql(ui->comboSchema->currentText())))
153158
{
154159
QMessageBox::warning(
155160
this,
@@ -163,7 +168,7 @@ void EditTableDialog::accept()
163168
// Rename table if necessary
164169
if(ui->editTableName->text() != curTable.name())
165170
{
166-
if(!pdb.renameTable(curTable.schema(), curTable.name(), ui->editTableName->text()))
171+
if(!pdb.renameTable(ui->comboSchema->currentText(), curTable.name(), ui->editTableName->text()))
167172
{
168173
QMessageBox::warning(this, QApplication::applicationName(), pdb.lastError());
169174
return;
@@ -184,7 +189,7 @@ void EditTableDialog::reject()
184189

185190
void EditTableDialog::updateSqlText()
186191
{
187-
ui->sqlTextEdit->setText(m_table.sql());
192+
ui->sqlTextEdit->setText(m_table.sql(ui->comboSchema->currentText()));
188193
}
189194

190195
void EditTableDialog::checkInput()
@@ -718,15 +723,15 @@ void EditTableDialog::setWithoutRowid(bool without_rowid)
718723
}
719724
}
720725

721-
void EditTableDialog::setTemporary(bool is_temp)
726+
void EditTableDialog::changeSchema(const QString& schema)
722727
{
723728
// Update the SQL preview
724729
updateSqlText();
725730

726731
// Update table if we're editing an existing table
727732
if(!m_bNewTable)
728733
{
729-
if(!pdb.renameColumn(curTable, m_table, QString(), sqlb::FieldPtr(), 0, is_temp ? "temp" : "main"))
734+
if(!pdb.renameColumn(curTable, m_table, QString(), sqlb::FieldPtr(), 0, schema))
730735
{
731736
QMessageBox::warning(this, QApplication::applicationName(),
732737
tr("Setting the temporary flag for the table failed. Error message:\n%1").arg(pdb.lastError()));

src/EditTableDialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private slots:
5555
void moveUp();
5656
void moveDown();
5757
void setWithoutRowid(bool without_rowid);
58-
void setTemporary(bool is_temp);
58+
void changeSchema(const QString& schema);
5959

6060
private:
6161
Ui::EditTableDialog* ui;

src/EditTableDialog.ui

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,34 @@
5555
</item>
5656
<item>
5757
<widget class="QWidget" name="widgetExtension" native="true">
58-
<layout class="QVBoxLayout" name="verticalLayout_5">
59-
<item>
60-
<widget class="QCheckBox" name="checkWithoutRowid">
61-
<property name="toolTip">
62-
<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>
58+
<layout class="QFormLayout" name="formLayout">
59+
<item row="0" column="0">
60+
<widget class="QLabel" name="label">
61+
<property name="text">
62+
<string>Database schema</string>
63+
</property>
64+
<property name="buddy">
65+
<cstring>comboSchema</cstring>
6366
</property>
67+
</widget>
68+
</item>
69+
<item row="0" column="1">
70+
<widget class="QComboBox" name="comboSchema"/>
71+
</item>
72+
<item row="1" column="0">
73+
<widget class="QLabel" name="label_1">
6474
<property name="text">
6575
<string>Without Rowid</string>
6676
</property>
77+
<property name="buddy">
78+
<cstring>checkWithoutRowid</cstring>
79+
</property>
6780
</widget>
6881
</item>
69-
<item>
70-
<widget class="QCheckBox" name="checkTemporary">
82+
<item row="1" column="1">
83+
<widget class="QCheckBox" name="checkWithoutRowid">
7184
<property name="toolTip">
72-
<string>Set this to create a temporary table that is deleted when closing the database.</string>
73-
</property>
74-
<property name="text">
75-
<string>Temporary table</string>
85+
<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>
7686
</property>
7787
</widget>
7888
</item>
@@ -331,8 +341,8 @@
331341
<tabstops>
332342
<tabstop>editTableName</tabstop>
333343
<tabstop>buttonMore</tabstop>
344+
<tabstop>comboSchema</tabstop>
334345
<tabstop>checkWithoutRowid</tabstop>
335-
<tabstop>checkTemporary</tabstop>
336346
<tabstop>addFieldButton</tabstop>
337347
<tabstop>removeFieldButton</tabstop>
338348
<tabstop>buttonMoveUp</tabstop>
@@ -521,10 +531,10 @@
521531
</hints>
522532
</connection>
523533
<connection>
524-
<sender>checkTemporary</sender>
525-
<signal>toggled(bool)</signal>
534+
<sender>comboSchema</sender>
535+
<signal>currentIndexChanged(QString)</signal>
526536
<receiver>EditTableDialog</receiver>
527-
<slot>setTemporary(bool)</slot>
537+
<slot>changeSchema(QString)</slot>
528538
<hints>
529539
<hint type="sourcelabel">
530540
<x>186</x>
@@ -547,6 +557,6 @@
547557
<slot>moveUp()</slot>
548558
<slot>moveDown()</slot>
549559
<slot>setWithoutRowid(bool)</slot>
550-
<slot>setTemporary(bool)</slot>
560+
<slot>changeSchema(QString)</slot>
551561
</slots>
552562
</ui>

0 commit comments

Comments
 (0)