From 5777a9c834b84942c5a8f0c46fe06cf6de365861 Mon Sep 17 00:00:00 2001 From: eelke Date: Fri, 2 Jul 2021 20:06:08 +0200 Subject: [PATCH] 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. --- pglab/ColumnPage.cpp | 63 ++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/pglab/ColumnPage.cpp b/pglab/ColumnPage.cpp index a2257cb..ee2f481 100644 --- a/pglab/ColumnPage.cpp +++ b/pglab/ColumnPage.cpp @@ -54,25 +54,48 @@ void ColumnPage::tableView_selectionChanged(const QItemSelection &/*selected*/, for (const auto &e : indexes) rijen.insert(m_sortFilterProxy->mapToSource(e).row()); + const QString alterTable = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName(); - QString drops; - QString addsql; - auto iter = rijen.begin(); - if (iter != rijen.end()) { - auto && col = m_columnModel->column(*iter); - drops = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName() % "\n DROP COLUMN " % quoteIdent(col.name); - addsql = "ALTER TABLE " % m_Class->fullyQualifiedQuotedObjectName() % "\n ADD COLUMN " % col.columnDefinition(*m_catalog); - for (++iter; iter != rijen.end(); ++iter) { - auto && col = m_columnModel->column(*iter); - drops += ",\n DROP COLUMN " % quoteIdent(col.name); - addsql += ",\n ADD COLUMN " % col.columnDefinition(*m_catalog); - } - drops += ";"; - addsql += ";"; - m_definitionView->setPlainText(drops % "\n\n" % addsql); - } - else { - m_definitionView->setPlainText(""); - } - + QString completeSql; + if (!rijen.empty()) { + QString drops; + QString addsql; + auto iter = rijen.begin(); + if (iter != rijen.end()) { + auto && col = m_columnModel->column(*iter); + drops = alterTable % "\n DROP COLUMN " % quoteIdent(col.name); + addsql = alterTable % "\n ADD COLUMN " % col.columnDefinition(*m_catalog); + for (++iter; iter != rijen.end(); ++iter) { + auto && col = m_columnModel->column(*iter); + drops += ",\n DROP COLUMN " % quoteIdent(col.name); + addsql += ",\n ADD COLUMN " % col.columnDefinition(*m_catalog); + } + drops += ";"; + addsql += ";"; + m_definitionView->setPlainText(drops % "\n\n" % addsql); + 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); }