Some improvements to the Export SQL dialog

When possible, don't write 'CREATE TABLE/VIEW/... `name`' but 'CREATE
TABLE/VIEW/... IF NOT EXISTS `name`' to the file.

Add an option to add DROP TABLE statements before each create statement.
This needs to be enhanced to apply to views, indices, and triggers as
well. See issue #629.

Clean up code.
This commit is contained in:
Martin Kleusberg
2017-05-12 15:38:46 +02:00
parent c74204c700
commit 037b3c0113
7 changed files with 84 additions and 24 deletions

View File

@@ -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));