WIP createdb dialog

This commit is contained in:
eelke 2022-05-24 18:54:13 +02:00
parent c20427e10d
commit d3080a08bb
10 changed files with 113 additions and 45 deletions

View file

@ -1,9 +1,12 @@
#include "CreateDatabaseDialog.h"
#include "ui_CreateDatabaseDialog.h"
#include <QStringBuilder>
CreateDatabaseDialog::CreateDatabaseDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CreateDatabaseDialog)
CreateDatabaseDialog::CreateDatabaseDialog(std::shared_ptr<PgDatabaseCatalog> 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);
}

View file

@ -3,6 +3,10 @@
#include <QDialog>
#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<PgDatabaseCatalog> catalog, QWidget *parent = 0);
~CreateDatabaseDialog();
private slots:
void on_tabWidget_currentChanged(int index);
private:
Ui::CreateDatabaseDialog *ui;
std::shared_ptr<PgDatabaseCatalog> m_catalog;
PgDatabase definition;
};
#endif // CREATEDATABASEDIALOG_H

View file

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>506</width>
<height>371</height>
<height>542</height>
</rect>
</property>
<property name="windowTitle">
@ -15,11 +15,11 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="Input" native="true">
<widget class="QWidget" name="Input">
<attribute name="title">
<string>Input</string>
</attribute>
@ -32,7 +32,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit"/>
<widget class="QLineEdit" name="name"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
@ -42,7 +42,7 @@
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboBox"/>
<widget class="QComboBox" name="dbTemplate"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
@ -52,7 +52,7 @@
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="comboBox_2"/>
<widget class="QComboBox" name="encoding"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
@ -62,7 +62,7 @@
</widget>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="comboBox_3"/>
<widget class="QComboBox" name="collationOrder"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_5">
@ -72,7 +72,7 @@
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="comboBox_4"/>
<widget class="QComboBox" name="characterClassification"/>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_6">
@ -82,7 +82,7 @@
</widget>
</item>
<item row="6" column="1">
<widget class="QSpinBox" name="spinBox"/>
<widget class="QSpinBox" name="connectionLimit"/>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_8">
@ -92,10 +92,10 @@
</widget>
</item>
<item row="7" column="1">
<widget class="QComboBox" name="comboBox_5"/>
<widget class="QComboBox" name="tablespace"/>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox_6"/>
<widget class="QComboBox" name="owner"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_7">
@ -105,14 +105,14 @@
</widget>
</item>
<item row="8" column="1">
<widget class="QCheckBox" name="checkBox">
<widget class="QCheckBox" name="templateDatabase">
<property name="text">
<string>Template database</string>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="QCheckBox" name="checkBox_2">
<widget class="QCheckBox" name="allowConnections">
<property name="text">
<string>Allow connections</string>
</property>
@ -124,6 +124,11 @@
<attribute name="title">
<string>SQL</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="CodeEditor" name="sqlPreview"/>
</item>
</layout>
</widget>
</widget>
</item>
@ -139,6 +144,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>CodeEditor</class>
<extends>QPlainTextEdit</extends>
<header>codeeditor\CodeEditor.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>

View file

@ -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 <CreateDatabaseDialog.h>
#include <QAction>
#include <QStringBuilder>
DatabasesPage::DatabasesPage(std::shared_ptr<OpenDatabase> 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> 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<PgDatabaseCatalog> cat)

View file

@ -22,7 +22,6 @@ public:
private:
PgLabTableViewHelper<DatabasesTableModel> m_databasesTableView;
// QTabWidget *m_detailsTabs = nullptr;
SqlCodePreview *m_tableSql = nullptr;
std::shared_ptr<PgDatabaseCatalog> m_catalog;

View file

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

View file

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

View file

@ -20,6 +20,11 @@ const QString& PgObject::objectName() const
return m_name;
}
void PgObject::setObjectName(const QString &name)
{
m_name = name;
}
QString PgObject::quotedObjectName() const
{
return quoteIdent(objectName());

View file

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

View file

@ -48,7 +48,7 @@ public:
QString commentSql() const;
private:
Oid m_ownerOid = InvalidOid;
const PgAuthId * m_owner;
const PgAuthId * m_owner = nullptr;
boost::optional<AclList> m_acls;
};