diff --git a/src/ExportSqlDialog.cpp b/src/ExportSqlDialog.cpp index f33e2903..3db109b7 100644 --- a/src/ExportSqlDialog.cpp +++ b/src/ExportSqlDialog.cpp @@ -10,6 +10,14 @@ static QString sSettingsGroup("exportsql"); static QString sSettingsInsertColNames("insertcolnames"); static QString sSettingsInsertMultiple("insertmultiple"); +static QString sSettingsOldSchema("oldschema"); + +enum WhatComboEntries +{ + ExportEverything, + ExportSchemaOnly, + ExportDataOnly, +}; ExportSqlDialog::ExportSqlDialog(DBBrowserDB* db, QWidget* parent, const QString& selection) : QDialog(parent), @@ -23,6 +31,7 @@ ExportSqlDialog::ExportSqlDialog(DBBrowserDB* db, QWidget* parent, const QString settings.beginGroup(sSettingsGroup); ui->checkColNames->setChecked(settings.value(sSettingsInsertColNames, false).toBool()); ui->checkMultiple->setChecked(settings.value(sSettingsInsertMultiple, false).toBool()); + ui->comboOldSchema->setCurrentIndex(settings.value(sSettingsOldSchema, 0).toInt()); // Get list of tables to export objectMap objects = pdb->getBrowsableObjects(); @@ -93,6 +102,7 @@ void ExportSqlDialog::accept() settings.beginGroup(sSettingsGroup); settings.setValue(sSettingsInsertColNames, ui->checkColNames->isChecked()); settings.setValue(sSettingsInsertMultiple, ui->checkMultiple->isChecked()); + settings.setValue(sSettingsOldSchema, ui->comboOldSchema->currentIndex()); settings.endGroup(); QStringList tables; @@ -100,8 +110,9 @@ void ExportSqlDialog::accept() tables.push_back(item->text()); // Check what to export. The indices here depend on the order of the items in the combobox in the ui file - bool exportSchema = ui->comboWhat->currentIndex() == 0 || ui->comboWhat->currentIndex() == 1; - bool exportData = ui->comboWhat->currentIndex() == 0 || ui->comboWhat->currentIndex() == 2; + bool exportSchema = ui->comboWhat->currentIndex() == ExportEverything || ui->comboWhat->currentIndex() == ExportSchemaOnly; + bool exportData = ui->comboWhat->currentIndex() == ExportEverything || ui->comboWhat->currentIndex() == ExportDataOnly; + bool keepSchema = ui->comboOldSchema->currentIndex() == 0; // Perform actual export bool dumpOk = pdb->dump(fileName, @@ -109,7 +120,8 @@ void ExportSqlDialog::accept() ui->checkColNames->isChecked(), ui->checkMultiple->isChecked(), exportSchema, - exportData); + exportData, + keepSchema); if (dumpOk) QMessageBox::information(this, QApplication::applicationName(), tr("Export completed.")); else @@ -118,5 +130,12 @@ void ExportSqlDialog::accept() QDialog::accept(); } - - +void ExportSqlDialog::whatChanged(int index) +{ + // Only show the combobox for deciding what to do with the old schema when importing into an existing database when we're + // actually exporting the schema here + if(index != ExportDataOnly) + ui->comboOldSchema->setVisible(true); + else + ui->comboOldSchema->setVisible(false); +} diff --git a/src/ExportSqlDialog.h b/src/ExportSqlDialog.h index 02e7abae..3f455e99 100644 --- a/src/ExportSqlDialog.h +++ b/src/ExportSqlDialog.h @@ -21,6 +21,7 @@ private slots: virtual void accept(); void doSelectAll(); void doDeselectAll(); + void whatChanged(int index); private: Ui::ExportSqlDialog* ui; diff --git a/src/ExportSqlDialog.ui b/src/ExportSqlDialog.ui index 5b52d2d3..15c51d29 100644 --- a/src/ExportSqlDialog.ui +++ b/src/ExportSqlDialog.ui @@ -7,7 +7,7 @@ 0 0 497 - 338 + 352 @@ -96,7 +96,7 @@ - + Qt::Vertical @@ -128,6 +128,20 @@ + + + + + Keep old schema (CREATE TABLE IF NOT EXISTS) + + + + + Overwrite old schema (DROP TABLE, then CREATE TABLE) + + + + @@ -163,8 +177,8 @@ doDeselectAll() - 298 - 150 + 488 + 129 20 @@ -179,8 +193,8 @@ doSelectAll() - 80 - 150 + 140 + 129 20 @@ -204,8 +218,25 @@ + + comboWhat + currentIndexChanged(int) + ExportSqlDialog + whatChanged(int) + + + 320 + 234 + + + 492 + 226 + + + showCustomCharEdits() + whatChanged(int) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 3940b09f..6e25e220 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -453,7 +453,8 @@ bool DBBrowserDB::dump(const QString& filename, bool insertColNames, bool insertNewSyntx, bool exportSchema, - bool exportData) + bool exportData, + bool keepOldSchema) { // Open file QFile file(filename); @@ -501,8 +502,11 @@ bool DBBrowserDB::dump(const QString& filename, // Write the SQL string used to create this table to the output file if(exportSchema) { + if(!keepOldSchema) + stream << "DROP TABLE IF EXISTS " << sqlb::escapeIdentifier((*it)->name()) << ";\n"; + if((*it)->fullyParsed()) - stream << (*it)->sql() << "\n"; + stream << (*it)->sql(true) << "\n"; else stream << (*it)->originalSql() << ";\n"; } @@ -610,7 +614,7 @@ bool DBBrowserDB::dump(const QString& filename, if(!(*it)->originalSql().isEmpty()) { if((*it)->fullyParsed()) - stream << (*it)->sql() << "\n"; + stream << (*it)->sql(true) << "\n"; else stream << (*it)->originalSql() << ";\n"; } diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 21799d3d..eb22e329 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -34,7 +34,7 @@ public: bool revertToSavepoint(const QString& pointname = "RESTOREPOINT"); bool releaseAllSavepoints(); bool revertAll(); - bool dump(const QString & filename, const QStringList &tablesToDump, bool insertColNames, bool insertNew, bool exportSchema, bool exportData); + bool dump(const QString & filename, const QStringList &tablesToDump, bool insertColNames, bool insertNew, bool exportSchema, bool exportData, bool keepOldSchema); bool executeSQL(QString statement, bool dirtyDB = true, bool logsql = true); bool executeMultiSQL(const QString& statement, bool dirty = true, bool log = false); const QString& lastError() const { return lastErrorMessage; } diff --git a/src/sqlitetypes.cpp b/src/sqlitetypes.cpp index 661f1695..b1396b95 100644 --- a/src/sqlitetypes.cpp +++ b/src/sqlitetypes.cpp @@ -427,14 +427,17 @@ ObjectPtr Table::parseSQL(const QString &sSQL) return TablePtr(new Table("")); } -QString Table::sql() const +QString Table::sql(bool ifNotExists) const { // Special handling for virtual tables: just build an easy create statement and copy the using part in there if(isVirtual()) return QString("CREATE VIRTUAL TABLE %1 USING %2;").arg(escapeIdentifier(m_name)).arg(m_virtual); // This is a normal table, not a virtual one - QString sql = QString("CREATE %1TABLE %2 (\n").arg(m_temporary ? QString("TEMPORARY ") : QString("")).arg(escapeIdentifier(m_name)); + QString sql = QString("CREATE %1TABLE%2 %3 (\n") + .arg(m_temporary ? QString("TEMPORARY ") : QString("")) + .arg(ifNotExists ? QString(" IF NOT EXISTS") : QString("")) + .arg(escapeIdentifier(m_name)); sql += fieldList().join(",\n"); @@ -1137,11 +1140,12 @@ QStringList Index::columnSqlList() const return sl; } -QString Index::sql() const +QString Index::sql(bool ifNotExists) const { // Start CREATE (UNIQUE) INDEX statement - QString sql = QString("CREATE %1INDEX %2 ON %3 (\n") + QString sql = QString("CREATE %1INDEX%2 %3 ON %4 (\n") .arg(m_unique ? QString("UNIQUE ") : QString("")) + .arg(ifNotExists ? QString(" IF NOT EXISTS") : QString("")) .arg(escapeIdentifier(m_name)) .arg(escapeIdentifier(m_table)); diff --git a/src/sqlitetypes.h b/src/sqlitetypes.h index 662c8762..5e32d6d1 100644 --- a/src/sqlitetypes.h +++ b/src/sqlitetypes.h @@ -89,9 +89,10 @@ public: /** * @brief Returns the CREATE statement for this object + * @param ifNotExists If set to true the "IF NOT EXISTS" qualifier will be added to the create statement * @return A QString with the CREATE statement. */ - virtual QString sql() const = 0; + virtual QString sql(bool ifNotExists = false) const = 0; /** * @brief parseSQL Parses the CREATE statement in sSQL. @@ -282,7 +283,7 @@ public: * @brief Returns the CREATE TABLE statement for this table object * @return A QString with the CREATE TABLE object. */ - QString sql() const; + QString sql(bool ifNotExists = false) const; void addField(const FieldPtr& f); bool removeField(const QString& sFieldName); @@ -402,7 +403,7 @@ public: * @brief Returns the CREATE INDEX statement for this index object * @return A QString with the CREATE INDEX object. */ - QString sql() const; + QString sql(bool ifNotExists = false) const; /** * @brief parseSQL Parses the CREATE INDEX statement in sSQL. @@ -430,7 +431,7 @@ public: virtual Types type() const { return Object::View; } - QString sql() const { /* TODO */ return m_originalSql; } + QString sql(bool ifNotExists = false) const { /* TODO */ Q_UNUSED(ifNotExists); return m_originalSql; } static ObjectPtr parseSQL(const QString& sSQL); @@ -455,7 +456,7 @@ public: virtual Types type() const { return Object::Trigger; } - QString sql() const { /* TODO */ return m_originalSql; } + QString sql(bool ifNotExists = false) const { /* TODO */ Q_UNUSED(ifNotExists); return m_originalSql; } static ObjectPtr parseSQL(const QString& sSQL);