From d3080a08bb7cc7d5afb44e4227b15deefcbc6674 Mon Sep 17 00:00:00 2001 From: eelke Date: Tue, 24 May 2022 18:54:13 +0200 Subject: [PATCH] WIP createdb dialog --- pglab/CreateDatabaseDialog.cpp | 35 ++++++++++++++++++++-- pglab/CreateDatabaseDialog.h | 12 +++++++- pglab/CreateDatabaseDialog.ui | 40 ++++++++++++++++--------- pglab/serverinspector/DatabasesPage.cpp | 14 +++++++-- pglab/serverinspector/DatabasesPage.h | 1 - pglablib/catalog/PgDatabase.cpp | 31 +++++++++---------- pglablib/catalog/PgDatabase.h | 11 +++---- pglablib/catalog/PgObject.cpp | 7 ++++- pglablib/catalog/PgObject.h | 5 ++-- pglablib/catalog/PgServerObject.h | 2 +- 10 files changed, 113 insertions(+), 45 deletions(-) diff --git a/pglab/CreateDatabaseDialog.cpp b/pglab/CreateDatabaseDialog.cpp index a4f653e..3b37cdc 100644 --- a/pglab/CreateDatabaseDialog.cpp +++ b/pglab/CreateDatabaseDialog.cpp @@ -1,9 +1,12 @@ #include "CreateDatabaseDialog.h" #include "ui_CreateDatabaseDialog.h" +#include -CreateDatabaseDialog::CreateDatabaseDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::CreateDatabaseDialog) +CreateDatabaseDialog::CreateDatabaseDialog(std::shared_ptr catalog, QWidget *parent) + : QDialog(parent) + , ui(new Ui::CreateDatabaseDialog) + , m_catalog(catalog) + , definition(*catalog, InvalidOid, "") { ui->setupUi(this); } @@ -23,3 +26,29 @@ CreateDatabaseDialog::~CreateDatabaseDialog() // [ ALLOW_CONNECTIONS [=] allowconn ] // [ CONNECTION LIMIT [=] connlimit ] // [ IS_TEMPLATE [=] istemplate ] ] + +void CreateDatabaseDialog::on_tabWidget_currentChanged(int index) +{ + if (index == 0) + return; + + // update the sql + definition.setObjectName(ui->name->text()); + +// definition.setOwnerOid() + definition.dbTemplate = ui->dbTemplate->currentText(); + + //QString drop_sql = definition.dropSql(); + QString create_sql = definition.createSql(); + ui->sqlPreview->setPlainText(create_sql); + +// create_sql += "\n\n-- set Privileges\n"; +// create_sql += definition.grantSql() % "\n"; + +// create_sql += "-- set comment\n"; +// create_sql += definition.commentSql() % "\n"; + +// ui->sqlPreview->setPlainText(drop_sql % "\n\n" % create_sql); + +} + diff --git a/pglab/CreateDatabaseDialog.h b/pglab/CreateDatabaseDialog.h index eaedd1f..bcc00af 100644 --- a/pglab/CreateDatabaseDialog.h +++ b/pglab/CreateDatabaseDialog.h @@ -3,6 +3,10 @@ #include +#include "catalog\PgDatabase.h" + +class PgDatabaseCatalog; + namespace Ui { class CreateDatabaseDialog; } @@ -12,11 +16,17 @@ class CreateDatabaseDialog : public QDialog Q_OBJECT public: - explicit CreateDatabaseDialog(QWidget *parent = 0); + CreateDatabaseDialog(std::shared_ptr catalog, QWidget *parent = 0); ~CreateDatabaseDialog(); +private slots: + void on_tabWidget_currentChanged(int index); + private: Ui::CreateDatabaseDialog *ui; + + std::shared_ptr m_catalog; + PgDatabase definition; }; #endif // CREATEDATABASEDIALOG_H diff --git a/pglab/CreateDatabaseDialog.ui b/pglab/CreateDatabaseDialog.ui index 286745b..f17dbac 100644 --- a/pglab/CreateDatabaseDialog.ui +++ b/pglab/CreateDatabaseDialog.ui @@ -7,7 +7,7 @@ 0 0 506 - 371 + 542 @@ -15,11 +15,11 @@ - + - 0 + 1 - + Input @@ -32,7 +32,7 @@ - + @@ -42,7 +42,7 @@ - + @@ -52,7 +52,7 @@ - + @@ -62,7 +62,7 @@ - + @@ -72,7 +72,7 @@ - + @@ -82,7 +82,7 @@ - + @@ -92,10 +92,10 @@ - + - + @@ -105,14 +105,14 @@ - + Template database - + Allow connections @@ -124,6 +124,11 @@ SQL + + + + + @@ -139,6 +144,13 @@ + + + CodeEditor + QPlainTextEdit +
codeeditor\CodeEditor.h
+
+
diff --git a/pglab/serverinspector/DatabasesPage.cpp b/pglab/serverinspector/DatabasesPage.cpp index 9c2f4d8..60f1aa6 100644 --- a/pglab/serverinspector/DatabasesPage.cpp +++ b/pglab/serverinspector/DatabasesPage.cpp @@ -1,19 +1,23 @@ #include "DatabasesPage.h" - #include "catalog/models/DatabasesTableModel.h" #include "widgets/SqlCodePreview.h" #include "SqlFormattingUtils.h" #include "catalog/PgDatabaseCatalog.h" #include "util/PgLabTableView.h" +#include +#include #include - DatabasesPage::DatabasesPage(std::shared_ptr opendatabase, QWidget * parent) : QSplitter(Qt::Horizontal, parent) , m_databasesTableView(this, new DatabasesTableModel(opendatabase, this)) { auto tv = m_databasesTableView.tableView(); tv->setSelectionMode(QAbstractItemView::SingleSelection); + tv->setContextMenuPolicy(Qt::ActionsContextMenu); + + auto createDbAction = new QAction("Create database", this); + tv->addAction(createDbAction); addWidget(tv); m_tableSql = new SqlCodePreview(this); @@ -21,6 +25,12 @@ DatabasesPage::DatabasesPage(std::shared_ptr opendatabase, QWidget connect(m_databasesTableView.tableView()->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &DatabasesPage::databaseSelectionChanged); + connect(createDbAction, &QAction::triggered, + [this](auto checked) + { + auto dlg = new CreateDatabaseDialog(this->m_catalog, this); + dlg->show(); + }); } void DatabasesPage::setCatalog(std::shared_ptr cat) diff --git a/pglab/serverinspector/DatabasesPage.h b/pglab/serverinspector/DatabasesPage.h index 46f269e..e46db63 100644 --- a/pglab/serverinspector/DatabasesPage.h +++ b/pglab/serverinspector/DatabasesPage.h @@ -22,7 +22,6 @@ public: private: PgLabTableViewHelper m_databasesTableView; -// QTabWidget *m_detailsTabs = nullptr; SqlCodePreview *m_tableSql = nullptr; std::shared_ptr m_catalog; diff --git a/pglablib/catalog/PgDatabase.cpp b/pglablib/catalog/PgDatabase.cpp index 7b401d1..fb266fd 100644 --- a/pglablib/catalog/PgDatabase.cpp +++ b/pglablib/catalog/PgDatabase.cpp @@ -23,29 +23,30 @@ QString PgDatabase::dropSql() const QString PgDatabase::createSql() const { - QString s = "CREATE DATABASE " % quotedObjectName() - // TEMPLATE is missing as this is not stored in the catalog - % "\n OWNER " % quoteIdent(ownerName()) - % "\n ENCODING " % escapeLiteral(encodingString) - % "\n LC_COLLATE " % escapeLiteral(collate) - % "\n LC_TYPE " % escapeLiteral(ctype); - auto ns = getTablespaceDisplayString(catalog(), tablespace); - if (ns != "pg_default") + QString s = "CREATE DATABASE " % quotedObjectName(); + + if (hasOwner()) + s += "\n OWNER " % quoteIdent(ownerName()); + if (!dbTemplate.isEmpty()) + s += "\n TEMPLATE " % escapeLiteral(dbTemplate); + if (!encodingString.isEmpty()) + s += "\n ENCODING " % escapeLiteral(encodingString); + if (!collate.isEmpty()) + s += "\n LC_COLLATE " % escapeLiteral(collate); + if (!ctype.isEmpty()) + s += "\n LC_TYPE " % escapeLiteral(ctype); + if (tablespace != InvalidOid) { - s += "\n TABLESPACE " % quoteIdent(ns); + auto ns = getTablespaceDisplayString(catalog(), tablespace); + if (ns != "pg_default") + s += "\n TABLESPACE " % quoteIdent(ns); } if (!allowConn) - { s += "\n ALLOW_CONNECTIONS FALSE"; - } if (connLimit >= 0) - { s += "\n CONNECTION LIMIT " % QString::number(connLimit); - } if (isTemplate) - { s += "\n IS_TEMPLATE TRUE"; - } s += ";"; return s; } diff --git a/pglablib/catalog/PgDatabase.h b/pglablib/catalog/PgDatabase.h index da6cd06..5c03f8d 100644 --- a/pglablib/catalog/PgDatabase.h +++ b/pglablib/catalog/PgDatabase.h @@ -8,14 +8,15 @@ class PgDatabase: public PgServerObject { public: - int encoding; + int encoding = 0; QString encodingString; QString collate; QString ctype; - bool isTemplate; - bool allowConn; - int connLimit; - Oid tablespace; + QString dbTemplate; ///< Not stored in the catalog but can be present in CREATE DATABASE statement + bool isTemplate = false; + bool allowConn = true; + int connLimit = -1; + Oid tablespace = InvalidOid; using PgServerObject::PgServerObject; diff --git a/pglablib/catalog/PgObject.cpp b/pglablib/catalog/PgObject.cpp index 04b384c..e59a050 100644 --- a/pglablib/catalog/PgObject.cpp +++ b/pglablib/catalog/PgObject.cpp @@ -20,9 +20,14 @@ const QString& PgObject::objectName() const return m_name; } +void PgObject::setObjectName(const QString &name) +{ + m_name = name; +} + QString PgObject::quotedObjectName() const { - return quoteIdent(objectName()); + return quoteIdent(objectName()); } QString PgObject::fullyQualifiedQuotedObjectName() const diff --git a/pglablib/catalog/PgObject.h b/pglablib/catalog/PgObject.h index 57080ff..c66902a 100644 --- a/pglablib/catalog/PgObject.h +++ b/pglablib/catalog/PgObject.h @@ -13,6 +13,7 @@ public: Oid oid() const; const QString& objectName() const; + void setObjectName(const QString &name); /// Default implementation uses objectName and add quotes when needed. virtual QString quotedObjectName() const; /// By default same as quotedObjectName however for objects that reside in namespaces @@ -28,8 +29,8 @@ protected: const PgDatabaseCatalog& catalog() const; private: - PgDatabaseCatalog* m_catalog; - Oid m_oid; + PgDatabaseCatalog* m_catalog; + Oid m_oid; QString m_name; }; diff --git a/pglablib/catalog/PgServerObject.h b/pglablib/catalog/PgServerObject.h index 1098745..447c4f0 100644 --- a/pglablib/catalog/PgServerObject.h +++ b/pglablib/catalog/PgServerObject.h @@ -48,7 +48,7 @@ public: QString commentSql() const; private: Oid m_ownerOid = InvalidOid; - const PgAuthId * m_owner; + const PgAuthId * m_owner = nullptr; boost::optional m_acls; };