From 36e5526f5f0bcb3a4004a1a44890380d67ea6d24 Mon Sep 17 00:00:00 2001 From: eelke Date: Thu, 28 Dec 2017 07:29:07 +0100 Subject: [PATCH] Some stuff I had on another machine and which might provide useful. --- pglab/CreateDatabaseDialog.cpp | 14 +++ pglab/CreateDatabaseDialog.h | 22 +++++ pglab/CreateDatabaseDialog.ui | 162 +++++++++++++++++++++++++++++++++ pglab/OpenDatabase.cpp | 2 +- pglab/PgDatabaseCatalog.cpp | 24 ++++- pglab/PgDatabaseCatalog.h | 14 +-- pglab/pglab.pro | 3 + pgsql/Pgsql_PgException.cpp | 1 + pgsql/Pgsql_PgException.h | 94 +++++++++++++++++++ pgsql/pgsql.pro | 2 + 10 files changed, 325 insertions(+), 13 deletions(-) create mode 100644 pglab/CreateDatabaseDialog.cpp create mode 100644 pglab/CreateDatabaseDialog.h create mode 100644 pglab/CreateDatabaseDialog.ui create mode 100644 pgsql/Pgsql_PgException.cpp create mode 100644 pgsql/Pgsql_PgException.h diff --git a/pglab/CreateDatabaseDialog.cpp b/pglab/CreateDatabaseDialog.cpp new file mode 100644 index 0000000..422698f --- /dev/null +++ b/pglab/CreateDatabaseDialog.cpp @@ -0,0 +1,14 @@ +#include "CreateDatabaseDialog.h" +#include "ui_CreateDatabaseDialog.h" + +CreateDatabaseDialog::CreateDatabaseDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::CreateDatabaseDialog) +{ + ui->setupUi(this); +} + +CreateDatabaseDialog::~CreateDatabaseDialog() +{ + delete ui; +} diff --git a/pglab/CreateDatabaseDialog.h b/pglab/CreateDatabaseDialog.h new file mode 100644 index 0000000..eaedd1f --- /dev/null +++ b/pglab/CreateDatabaseDialog.h @@ -0,0 +1,22 @@ +#ifndef CREATEDATABASEDIALOG_H +#define CREATEDATABASEDIALOG_H + +#include + +namespace Ui { +class CreateDatabaseDialog; +} + +class CreateDatabaseDialog : public QDialog +{ + Q_OBJECT + +public: + explicit CreateDatabaseDialog(QWidget *parent = 0); + ~CreateDatabaseDialog(); + +private: + Ui::CreateDatabaseDialog *ui; +}; + +#endif // CREATEDATABASEDIALOG_H diff --git a/pglab/CreateDatabaseDialog.ui b/pglab/CreateDatabaseDialog.ui new file mode 100644 index 0000000..f4b9ae0 --- /dev/null +++ b/pglab/CreateDatabaseDialog.ui @@ -0,0 +1,162 @@ + + + CreateDatabaseDialog + + + + 0 + 0 + 506 + 371 + + + + Dialog + + + + + + + + Name + + + + + + + + + + Template + + + + + + + + + + Encoding + + + + + + + + + + Collation order + + + + + + + + + + Character classification + + + + + + + + + + Connection limit + + + + + + + + + + Tablespace + + + + + + + + + + Owner + + + + + + + + + + Template database + + + + + + + Allow connections + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + CreateDatabaseDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + CreateDatabaseDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/pglab/OpenDatabase.cpp b/pglab/OpenDatabase.cpp index e878c91..820ff95 100644 --- a/pglab/OpenDatabase.cpp +++ b/pglab/OpenDatabase.cpp @@ -32,7 +32,7 @@ bool OpenDatabase::Init() auto kw = m_config.getKeywords(); auto vals = m_config.getValues(); if (conn.connect(kw, vals, 0)) { - m_catalogue->loadAll(conn); + m_catalogue->loadAll(conn, nullptr); return true; } return false; diff --git a/pglab/PgDatabaseCatalog.cpp b/pglab/PgDatabaseCatalog.cpp index 64a7cde..7703423 100644 --- a/pglab/PgDatabaseCatalog.cpp +++ b/pglab/PgDatabaseCatalog.cpp @@ -1,5 +1,6 @@ #include "PgDatabaseCatalog.h" +#include "ASyncDBConnection.h" #include "PgAttributeContainer.h" #include "PgAuthIdContainer.h" #include "PgClassContainer.h" @@ -11,6 +12,8 @@ #include "Pgsql_Connection.h" #include "Pgsql_oids.h" +#include + using namespace Pgsql; QString getRoleNameFromOid(const PgDatabaseCatalog &cat, Oid oid) @@ -100,17 +103,35 @@ PgDatabaseCatalog::~PgDatabaseCatalog() { } -void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn) +void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn, + std::function progress_callback) { loadInfo(conn); + if (progress_callback && !progress_callback(1, 9)) + return; load2(m_attributes, conn); + if (progress_callback && !progress_callback(2, 9)) + return; load2(m_authIds, conn); + if (progress_callback && !progress_callback(3, 9)) + return; load2(m_classes, conn); + if (progress_callback && !progress_callback(4, 9)) + return; load2(m_constraints, conn); + if (progress_callback && !progress_callback(5, 9)) + return; load2(m_databases, conn); + if (progress_callback && !progress_callback(6, 9)) + return; load2(m_indexes, conn); + if (progress_callback && !progress_callback(7, 9)) + return; load2(m_namespaces, conn); + if (progress_callback && !progress_callback(8, 9)) + return; load2(m_types, conn); + progress_callback && progress_callback(9, 9); } void PgDatabaseCatalog::loadInfo(Pgsql::Connection &conn) @@ -128,6 +149,7 @@ void PgDatabaseCatalog::loadInfo(Pgsql::Connection &conn) void load(Pgsql::Connection &conn, IPgContainter &pg_cont) { + QThread::msleep(400); std::string q = pg_cont.getLoadQuery(); Pgsql::Result result = conn.query(q.c_str()); if (result && result.resultStatus() == PGRES_TUPLES_OK) diff --git a/pglab/PgDatabaseCatalog.h b/pglab/PgDatabaseCatalog.h index 4781371..955bce6 100644 --- a/pglab/PgDatabaseCatalog.h +++ b/pglab/PgDatabaseCatalog.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -26,22 +27,13 @@ public: PgDatabaseCatalog(); PgDatabaseCatalog(const PgDatabaseCatalog&) = delete; PgDatabaseCatalog& operator = (const PgDatabaseCatalog&) = delete; - ~PgDatabaseCatalog(); + void loadAll(Pgsql::Connection &conn, + std::function progress_callback); - void loadAll(Pgsql::Connection &conn); void loadInfo(Pgsql::Connection &conn); - //void loadAttributes(Pgsql::Connection &conn); -// void loadAuthIds(Pgsql::Connection &conn); -// void loadClasses(Pgsql::Connection &conn); -// void loadConstraints(Pgsql::Connection &conn); -// void loadDatabases(Pgsql::Connection &conn); -// void loadIndexes(Pgsql::Connection &conn); -// void loadNamespaces(Pgsql::Connection &conn); -// void loadTypes(Pgsql::Connection &conn); - const QString& serverVersionString() const; int serverVersion() const; diff --git a/pglab/pglab.pro b/pglab/pglab.pro index 11e507e..9ac795a 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -34,6 +34,7 @@ SOURCES += main.cpp\ QueryExplainModel.cpp \ ASyncDBConnection.cpp \ tsqueue.cpp \ + CreateDatabaseDialog.cpp \ DatabaseWindow.cpp \ ConnectionManagerWindow.cpp \ ConnectionListModel.cpp \ @@ -89,6 +90,7 @@ HEADERS += \ QueryExplainModel.h \ ASyncDBConnection.h \ tsqueue.h \ + CreateDatabaseDialog.h \ DatabaseWindow.h \ ConnectionManagerWindow.h \ ConnectionListModel.h \ @@ -141,6 +143,7 @@ HEADERS += \ FORMS += mainwindow.ui \ DatabaseWindow.ui \ ConnectionManagerWindow.ui \ + CreateDatabaseDialog.ui \ DatabaseInspectorWidget.ui \ TuplesResultWidget.ui \ QueryTab.ui \ diff --git a/pgsql/Pgsql_PgException.cpp b/pgsql/Pgsql_PgException.cpp new file mode 100644 index 0000000..381daae --- /dev/null +++ b/pgsql/Pgsql_PgException.cpp @@ -0,0 +1 @@ +#include "Pgsql_PgException.h" diff --git a/pgsql/Pgsql_PgException.h b/pgsql/Pgsql_PgException.h new file mode 100644 index 0000000..eba2945 --- /dev/null +++ b/pgsql/Pgsql_PgException.h @@ -0,0 +1,94 @@ +#ifndef PGEXCEPTION_H +#define PGEXCEPTION_H + +#include +#include + +namespace Pgsql { + + class PgException { + public: + PgException(const char *msg) + : message(msg) + {} + PgException(std::string msg) + : message(std::move(msg)) + {} + + + private: + std::string message; + + }; + + class PgResultError: public PgException { + public: + PgResultError(std::string msg, std::string result_code) + : PgException(std::move(msg)) + {} + }; + + class PgConnectionError: public PgResultError, std::runtime_error { + PgConnectionError(const std::string &msg, std::string result_code) + : PgResultError(msg, std::move(result_code)) + , std::runtime_error(msg) + {} + + }; + + + +/* postgresq error classes + * + * success + * warning + * no data + * + * 03 sql statement not yet complete + * 08 connection exception -> could be resolved by reconnecting / resetting connection + * 09 triggered action exception + * 0A feature not supported + * 0B invalid transaction initiation + * 0F locator exception + * 0L invalid grantor + * 0P invalid role + * 0Z diagnostic exception + * 20 case not fount + * 21 cardinality violation + * 22 data exception + * 23 integrity constraint error + * 24 invalid cursor statement + * 25 invalid transaction state + * 26 invalid sql statement name + * 27 triggered data change violation + * 28 invalid authorization specification + * 2B Dependent Privilege Descriptors Still Exist + * 2D Invalid Transaction Termination + * 2F SQL Routine Exception + * 34 Invalid Cursor Name + * 38 External Routine Exception + * 39 External Routine Invocation Exception + * 3B Savepoint Exception + * 3D Invalid Catalog Name + * 3F Invalid Schema Name + * 40 Transaction Rollback + * 42 Syntax Error or Access Rule Violation + * 44 WITH CHECK OPTION Violation + * 53 Insufficient Resources + * 54 Program Limit Exceeded + * 55 Object Not In Prerequisite State + * 57 Operator Intervention + * 58 System Error (errors external to PostgreSQL itself) + * 72 Snapshot Failure + * F0 Configuration File Error + * HV Foreign Data Wrapper Error (SQL/MED) + * P0 PL/pgSQL Error + * XX Internal Error + * + * At what levels can the communication with postgresql fail + * - network -> reset/reconnecting + * - + */ +} + +#endif // PGEXCEPTION_H diff --git a/pgsql/pgsql.pro b/pgsql/pgsql.pro index 6030e66..df08956 100644 --- a/pgsql/pgsql.pro +++ b/pgsql/pgsql.pro @@ -34,6 +34,7 @@ release { SOURCES += Pgsql_Connection.cpp \ Pgsql_Params.cpp \ + Pgsql_PgException.cpp \ Pgsql_Result.cpp \ Pgsql_Row.cpp \ Pgsql_Value.cpp \ @@ -43,6 +44,7 @@ SOURCES += Pgsql_Connection.cpp \ HEADERS += Pgsql_Connection.h \ Pgsql_Params.h \ + Pgsql_PgException.h \ Pgsql_Result.h \ Pgsql_Row.h \ Pgsql_Value.h \