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,14 +54,17 @@ 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 completeSql;
if (!rijen.empty()) {
QString drops; QString drops;
QString addsql; QString addsql;
auto iter = rijen.begin(); auto iter = rijen.begin();
if (iter != rijen.end()) { if (iter != rijen.end()) {
auto && col = m_columnModel->column(*iter); auto && col = m_columnModel->column(*iter);
drops = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName() % "\n DROP COLUMN " % quoteIdent(col.name); drops = alterTable % "\n DROP COLUMN " % quoteIdent(col.name);
addsql = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName() % "\n ADD COLUMN " % col.columnDefinition(*m_catalog); addsql = alterTable % "\n ADD COLUMN " % col.columnDefinition(*m_catalog);
for (++iter; iter != rijen.end(); ++iter) { for (++iter; iter != rijen.end(); ++iter) {
auto && col = m_columnModel->column(*iter); auto && col = m_columnModel->column(*iter);
drops += ",\n DROP COLUMN " % quoteIdent(col.name); drops += ",\n DROP COLUMN " % quoteIdent(col.name);
@ -70,9 +73,29 @@ void ColumnPage::tableView_selectionChanged(const QItemSelection &/*selected*/,
drops += ";"; drops += ";";
addsql += ";"; addsql += ";";
m_definitionView->setPlainText(drops % "\n\n" % addsql); m_definitionView->setPlainText(drops % "\n\n" % addsql);
completeSql += drops % "\n\n" % addsql;
} }
else { else {
m_definitionView->setPlainText(""); 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);
} }