additonal SQL on the columns page

After the "all in one" ALTER TABLE ADD COLUMN statement ALTER TABLE ALTER COLUMN statements are listed for
setting the default and the NOT NULL constraint.
This commit is contained in:
eelke 2021-07-02 20:06:08 +02:00
parent 53997f94da
commit 5777a9c834

View file

@ -54,25 +54,48 @@ void ColumnPage::tableView_selectionChanged(const QItemSelection &/*selected*/,
for (const auto &e : indexes) for (const auto &e : indexes)
rijen.insert(m_sortFilterProxy->mapToSource(e).row()); rijen.insert(m_sortFilterProxy->mapToSource(e).row());
const QString alterTable = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName();
QString drops; QString completeSql;
QString addsql; if (!rijen.empty()) {
auto iter = rijen.begin(); QString drops;
if (iter != rijen.end()) { QString addsql;
auto && col = m_columnModel->column(*iter); auto iter = rijen.begin();
drops = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName() % "\n DROP COLUMN " % quoteIdent(col.name); if (iter != rijen.end()) {
addsql = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName() % "\n ADD COLUMN " % col.columnDefinition(*m_catalog); auto && col = m_columnModel->column(*iter);
for (++iter; iter != rijen.end(); ++iter) { drops = alterTable % "\n DROP COLUMN " % quoteIdent(col.name);
auto && col = m_columnModel->column(*iter); addsql = alterTable % "\n ADD COLUMN " % col.columnDefinition(*m_catalog);
drops += ",\n DROP COLUMN " % quoteIdent(col.name); for (++iter; iter != rijen.end(); ++iter) {
addsql += ",\n ADD COLUMN " % col.columnDefinition(*m_catalog); auto && col = m_columnModel->column(*iter);
} drops += ",\n DROP COLUMN " % quoteIdent(col.name);
drops += ";"; addsql += ",\n ADD COLUMN " % col.columnDefinition(*m_catalog);
addsql += ";"; }
m_definitionView->setPlainText(drops % "\n\n" % addsql); drops += ";";
} addsql += ";";
else { m_definitionView->setPlainText(drops % "\n\n" % addsql);
m_definitionView->setPlainText(""); completeSql += drops % "\n\n" % addsql;
} }
else {
m_definitionView->setPlainText("");
}
completeSql += "\n-- SQL to correct just the defaults\n";
for (auto r : rijen) {
auto && col = m_columnModel->column(r);
completeSql += alterTable % " ALTER COLUMN " % quoteIdent(col.name);
if (col.hasdef)
completeSql += " SET DEFAULT " % col.defaultValue % ";\n";
else
completeSql += " DROP DEFAULT;\n";
}
completeSql += "\n-- SQL to correct NULLABLE\n";
for (auto r : rijen) {
auto && col = m_columnModel->column(r);
completeSql += alterTable % " ALTER COLUMN " % quoteIdent(col.name);
if (col.notnull)
completeSql += " SET NOT NULL;\n";
else
completeSql += " DROP NOT NULL;\n";
}
}
m_definitionView->setPlainText(completeSql);
} }