Tab with list of sequences and create sql for selected sequence.
This commit is contained in:
parent
769307c821
commit
437736a023
19 changed files with 507 additions and 63 deletions
|
|
@ -43,11 +43,9 @@ FunctionsPage::FunctionsPage(QWidget *parent)
|
|||
auto item_delegate = new PgLabItemDelegate(this);
|
||||
m_functionTable->setItemDelegate(item_delegate);
|
||||
|
||||
|
||||
connect(m_functionTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
||||
&FunctionsPage::functionTable_currentRowChanged);
|
||||
|
||||
|
||||
retranslateUi();
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +80,6 @@ void FunctionsPage::functionTable_currentRowChanged(const QModelIndex ¤t,
|
|||
|
||||
void FunctionsPage::selectedProcChanged(const std::optional<PgProc> &proc)
|
||||
{
|
||||
|
||||
updateSqlTab(proc);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "ui_MainWindow.h"
|
||||
#include "TablesPage.h"
|
||||
#include "FunctionsPage.h"
|
||||
#include "SequencesPage.h"
|
||||
|
||||
#include <QStandardPaths>
|
||||
#include <QFileDialog>
|
||||
|
|
@ -112,6 +113,10 @@ void MainWindow::catalogLoaded()
|
|||
functions_page->setCatalog(m_database->catalog());
|
||||
ui->tabWidget->addTab(functions_page, "Functions");
|
||||
|
||||
auto sequences_page = new SequencesPage(this);
|
||||
sequences_page->setCatalog(m_database->catalog());
|
||||
ui->tabWidget->addTab(sequences_page, "Sequences");
|
||||
|
||||
newSqlPage();
|
||||
newCreateTablePage();
|
||||
} catch (std::runtime_error &ex) {
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ public:
|
|||
|
||||
void newCrudPage(const PgClass &table);
|
||||
void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres);
|
||||
|
||||
private:
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
|
|
|
|||
|
|
@ -38,8 +38,7 @@ public:
|
|||
// Basic functionality:
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
PgProc proc(int row) const;
|
||||
|
||||
|
|
|
|||
82
pglab/SequenceModel.cpp
Normal file
82
pglab/SequenceModel.cpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include "SequenceModel.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include "catalog/PgSequenceContainer.h"
|
||||
|
||||
SequenceModel::SequenceModel(QObject * parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{}
|
||||
|
||||
QVariant SequenceModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (orientation == Qt::Horizontal) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (section) {
|
||||
case NameCol: return tr("Name");
|
||||
case SchemaCol: return tr("Schema");
|
||||
case OwnerCol: return tr("Owner");
|
||||
case LastCol: return tr("Last");
|
||||
case StartCol: return tr("Start");
|
||||
case MinCol: return tr("Min");
|
||||
case MaxCol: return tr("Max");
|
||||
case IncrementCol: return tr("Inc");
|
||||
case CacheCol: return tr("Cached");
|
||||
case CycledCol: return tr("Cycled");
|
||||
case CalledCol: return tr("Called");
|
||||
case AclCol: return tr("ACL");
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void SequenceModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
m_catalog = cat;
|
||||
m_sequences = cat->sequences();
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
PgSequence SequenceModel::sequence(int row) const
|
||||
{
|
||||
return m_sequences->getByIdx(row);
|
||||
}
|
||||
|
||||
int SequenceModel::rowCount(const QModelIndex &) const
|
||||
{
|
||||
return m_sequences ? static_cast<int>(m_sequences->count()) : 0;
|
||||
}
|
||||
|
||||
int SequenceModel::columnCount(const QModelIndex &) const
|
||||
{
|
||||
return colCount;
|
||||
}
|
||||
|
||||
QVariant SequenceModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!m_sequences)
|
||||
return {};
|
||||
|
||||
int row = index.row();
|
||||
auto && seq = m_sequences->getByIdx(row);
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (index.column()) {
|
||||
case NameCol: return seq.objectName();
|
||||
case SchemaCol: return seq.nsName();
|
||||
case OwnerCol: return seq.ownerName();
|
||||
case LastCol: return seq.last;
|
||||
case StartCol: return seq.start;
|
||||
case MinCol: return seq.min;
|
||||
case MaxCol: return seq.max;
|
||||
case IncrementCol: return seq.increment;
|
||||
case CacheCol: return seq.cache;
|
||||
case CycledCol: return seq.cycled;
|
||||
case CalledCol: return seq.called;
|
||||
case AclCol: return seq.aclString();
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
48
pglab/SequenceModel.h
Normal file
48
pglab/SequenceModel.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#ifndef SEQUENCEMODEL_H
|
||||
#define SEQUENCEMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
//#include "catalog/PgClass.h"
|
||||
#include "catalog/PgSequence.h"
|
||||
#include <memory>
|
||||
|
||||
class PgDatabaseCatalog;
|
||||
class PgSequenceContainer;
|
||||
|
||||
class SequenceModel: public QAbstractTableModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
enum e_Columns : int {
|
||||
NameCol,
|
||||
SchemaCol,
|
||||
OwnerCol,
|
||||
LastCol,
|
||||
StartCol,
|
||||
MinCol,
|
||||
MaxCol,
|
||||
IncrementCol,
|
||||
CacheCol,
|
||||
CycledCol,
|
||||
CalledCol,
|
||||
AclCol,
|
||||
|
||||
colCount
|
||||
};
|
||||
|
||||
SequenceModel(QObject * parent = nullptr);
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
void setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
PgSequence sequence(int row) const;
|
||||
private:
|
||||
std::shared_ptr<const PgDatabaseCatalog> m_catalog;
|
||||
std::shared_ptr<const PgSequenceContainer> m_sequences;
|
||||
};
|
||||
|
||||
#endif // SEQUENCEMODEL_H
|
||||
81
pglab/SequencesPage.cpp
Normal file
81
pglab/SequencesPage.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#include "SequencesPage.h"
|
||||
#include "ResultTableModelUtil.h"
|
||||
#include "CustomFilterSortModel.h"
|
||||
#include "CustomDataRole.h"
|
||||
#include "PgLabItemDelegate.h"
|
||||
#include "SequenceModel.h"
|
||||
#include "SqlCodePreview.h"
|
||||
#include <QTableView>
|
||||
|
||||
SequencesPage::SequencesPage(QWidget *parent)
|
||||
{
|
||||
m_sequenceTable = new QTableView(this);
|
||||
m_definitionView = new SqlCodePreview(this);
|
||||
|
||||
// build widget tree
|
||||
// add top level widgets to splitter
|
||||
addWidget(m_sequenceTable);
|
||||
addWidget(m_definitionView);
|
||||
|
||||
SetTableViewDefault(m_sequenceTable);
|
||||
|
||||
m_model = new SequenceModel(this);
|
||||
m_sortFilterProxy = new CustomFilterSortModel(this);
|
||||
m_sortFilterProxy->setSourceModel(m_model);
|
||||
m_sequenceTable->setModel(m_sortFilterProxy);
|
||||
m_sequenceTable->setSortingEnabled(true);
|
||||
m_sequenceTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
|
||||
auto item_delegate = new PgLabItemDelegate(this);
|
||||
m_sequenceTable->setItemDelegate(item_delegate);
|
||||
|
||||
connect(m_sequenceTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
||||
&SequencesPage::sequenceTable_currentRowChanged);
|
||||
|
||||
retranslateUi();
|
||||
}
|
||||
|
||||
void SequencesPage::retranslateUi()
|
||||
{
|
||||
// auto set_tabtext = [this] (QWidget *widget, QString translation) {
|
||||
// m_detailTabs->setTabText(m_detailTabs->indexOf(widget), translation);
|
||||
// };
|
||||
|
||||
// set_tabtext(m_definitionView, QApplication::translate("FunctionsPage", "SQL", nullptr));
|
||||
}
|
||||
|
||||
void SequencesPage::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
|
||||
{
|
||||
m_catalog = cat;
|
||||
m_model->setCatalog(cat);
|
||||
}
|
||||
|
||||
void SequencesPage::sequenceTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
||||
{
|
||||
if (current.row() != previous.row()) {
|
||||
if (current.isValid()) {
|
||||
auto source_index = m_sortFilterProxy->mapToSource(current);
|
||||
auto proc = m_model->sequence(source_index.row());
|
||||
|
||||
selectedSequenceChanged(proc);
|
||||
}
|
||||
else
|
||||
selectedSequenceChanged({});
|
||||
}
|
||||
}
|
||||
|
||||
void SequencesPage::selectedSequenceChanged(const std::optional<PgSequence> &seq)
|
||||
{
|
||||
updateSqlTab(seq);
|
||||
}
|
||||
|
||||
void SequencesPage::updateSqlTab(const std::optional<PgSequence> &seq)
|
||||
{
|
||||
if (!seq.has_value()) {
|
||||
m_definitionView->clear();
|
||||
return;
|
||||
}
|
||||
QString create_sql = seq->createSql();
|
||||
|
||||
m_definitionView->setPlainText(create_sql);
|
||||
}
|
||||
40
pglab/SequencesPage.h
Normal file
40
pglab/SequencesPage.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#ifndef SEQUENCESPAGES_H
|
||||
#define SEQUENCESPAGES_H
|
||||
|
||||
#include <QSplitter>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
class QTableView;
|
||||
class PgDatabaseCatalog;
|
||||
class SequenceModel;
|
||||
class CustomFilterSortModel;
|
||||
//class QTabWidget;
|
||||
class SqlCodePreview;
|
||||
class PgSequence;
|
||||
|
||||
|
||||
class SequencesPage : public QSplitter {
|
||||
Q_OBJECT
|
||||
public:
|
||||
SequencesPage(QWidget *parent = nullptr);
|
||||
|
||||
void setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat);
|
||||
public slots:
|
||||
|
||||
void sequenceTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
||||
|
||||
private:
|
||||
QTableView *m_sequenceTable = nullptr;
|
||||
//QTabWidget *m_detailTabs = nullptr;
|
||||
SqlCodePreview *m_definitionView = nullptr;
|
||||
SequenceModel *m_model = nullptr;
|
||||
CustomFilterSortModel *m_sortFilterProxy = nullptr;
|
||||
std::shared_ptr<const PgDatabaseCatalog> m_catalog;
|
||||
|
||||
void retranslateUi();
|
||||
void selectedSequenceChanged(const std::optional<PgSequence> &seq);
|
||||
void updateSqlTab(const std::optional<PgSequence> &seq);
|
||||
};
|
||||
|
||||
#endif // SEQUENCESPAGES_H
|
||||
|
|
@ -81,7 +81,9 @@ PropertyProxyModel.cpp \
|
|||
FunctionsPage.cpp \
|
||||
ColumnPage.cpp \
|
||||
EditTableWidget.cpp \
|
||||
EditColumnTableModel.cpp
|
||||
EditColumnTableModel.cpp \
|
||||
SequenceModel.cpp \
|
||||
SequencesPage.cpp
|
||||
|
||||
HEADERS += \
|
||||
QueryResultModel.h \
|
||||
|
|
@ -140,7 +142,9 @@ CustomDataRole.h \
|
|||
FunctionsPage.h \
|
||||
ColumnPage.h \
|
||||
EditTableWidget.h \
|
||||
EditColumnTableModel.h
|
||||
EditColumnTableModel.h \
|
||||
SequenceModel.h \
|
||||
SequencesPage.h
|
||||
|
||||
FORMS += mainwindow.ui \
|
||||
ConnectionManagerWindow.ui \
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ std::string PgClassContainer::getLoadQuery() const
|
|||
" reltuples, reltoastrelid, relisshared, relpersistence, "
|
||||
" relkind, relhasoids, relispopulated, relfrozenxid, relminmxid, "
|
||||
" reloptions, relacl \n"
|
||||
"FROM pg_catalog.pg_class";
|
||||
"FROM pg_catalog.pg_class \n"
|
||||
"WHERE relkind IN ('r', 'i', 'p', 'I')";
|
||||
}
|
||||
|
||||
PgClass PgClassContainer::loadElem(const Pgsql::Row &row)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "PgContainer.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
||||
IPgContainer::IPgContainer(PgDatabaseCatalog& cat)
|
||||
: m_catalog(cat)
|
||||
|
|
@ -9,3 +10,26 @@ bool IPgContainer::minimumVersion(int required_version) const
|
|||
{
|
||||
return m_catalog.serverVersion() >= required_version;
|
||||
}
|
||||
|
||||
bool IPgContainer::lessThenVersion(int required_version) const
|
||||
{
|
||||
return m_catalog.serverVersion() < required_version;
|
||||
}
|
||||
|
||||
void IPgContainer::loadAll(Pgsql::Connection &conn)
|
||||
{
|
||||
std::string q = getLoadQuery();
|
||||
Pgsql::Result result = conn.query(q.c_str());
|
||||
if (result && result.resultStatus() == PGRES_TUPLES_OK) {
|
||||
load(result);
|
||||
}
|
||||
else {
|
||||
auto details = result.diagDetails();
|
||||
if (details.state == "42501") { // permission denied
|
||||
// ignore this for now
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Query failed\n" + details.errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,8 +18,11 @@ public:
|
|||
|
||||
virtual std::string getLoadQuery() const = 0;
|
||||
virtual void load(const Pgsql::Result &res) = 0;
|
||||
virtual void loadAll(Pgsql::Connection &conn);
|
||||
|
||||
bool minimumVersion(int required_version) const;
|
||||
bool lessThenVersion(int required_version) const;
|
||||
|
||||
protected:
|
||||
PgDatabaseCatalog& m_catalog;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
#include "PgCollationContainer.h"
|
||||
#include "PgInheritsContainer.h"
|
||||
#include "PgLanguageContainer.h"
|
||||
#include "PgSequenceContainer.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include "Pgsql_oids.h"
|
||||
|
||||
|
|
@ -137,56 +138,69 @@ void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn,
|
|||
loadInfo(conn);
|
||||
const int count = 12;
|
||||
int n = 0;
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
|
||||
auto pf = [&] () -> bool {
|
||||
return progress_callback && !progress_callback(++n, count);
|
||||
};
|
||||
|
||||
if (pf()) return;
|
||||
|
||||
// First load server objects
|
||||
load2(m_authIds, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_tablespaces, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_databases, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
|
||||
// Load database objects
|
||||
load2(m_languages, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_namespaces, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_collations, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_classes, conn); // needs namespaces
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_attributes, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_constraints, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_indexes, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_ams, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_triggers, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_types, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_procs, conn);
|
||||
if (progress_callback && !progress_callback(++n, count))
|
||||
return;
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_inherits, conn);
|
||||
progress_callback && progress_callback(++n, count);
|
||||
if (pf()) return;
|
||||
|
||||
load2(m_sequences, conn);
|
||||
// {
|
||||
// if (!m_sequences)
|
||||
// m_sequences = std::make_shared<PgSequenceContainer>(*this);
|
||||
// m_sequences->load(conn);
|
||||
|
||||
// }
|
||||
pf();
|
||||
|
||||
refreshed(this, All);
|
||||
}
|
||||
|
|
@ -206,27 +220,6 @@ void PgDatabaseCatalog::loadInfo(Pgsql::Connection &conn)
|
|||
m_dbName = conn.getDBName();
|
||||
}
|
||||
|
||||
void load(Pgsql::Connection &conn, IPgContainer &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) {
|
||||
//boost::timer::auto_cpu_timer t;
|
||||
pg_cont.load(result);
|
||||
}
|
||||
else {
|
||||
auto details = result.diagDetails();
|
||||
if (details.state == "42501") { // permission denied
|
||||
// ignore this for now
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Query failed\n" + details.errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const QString& PgDatabaseCatalog::serverVersionString() const
|
||||
{
|
||||
return m_serverVersionString;
|
||||
|
|
@ -311,3 +304,8 @@ std::shared_ptr<const PgLanguageContainer> PgDatabaseCatalog::languages() const
|
|||
{
|
||||
return m_languages;
|
||||
}
|
||||
|
||||
std::shared_ptr<const PgSequenceContainer> PgDatabaseCatalog::sequences() const
|
||||
{
|
||||
return m_sequences;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ class PgProcContainer;
|
|||
class PgCollationContainer;
|
||||
class PgInheritsContainer;
|
||||
class PgLanguageContainer;
|
||||
class PgSequenceContainer;
|
||||
|
||||
class PgDatabaseCatalog: public QObject, public std::enable_shared_from_this<PgDatabaseCatalog> {
|
||||
Q_OBJECT
|
||||
|
|
@ -63,6 +64,7 @@ public:
|
|||
std::shared_ptr<const PgCollationContainer> collations() const;
|
||||
std::shared_ptr<const PgInheritsContainer> inherits() const;
|
||||
std::shared_ptr<const PgLanguageContainer> languages() const;
|
||||
std::shared_ptr<const PgSequenceContainer> sequences() const;
|
||||
|
||||
enum RefreshFlag {
|
||||
Attributes = 1,
|
||||
|
|
@ -103,6 +105,7 @@ private:
|
|||
std::shared_ptr<PgCollationContainer> m_collations;
|
||||
std::shared_ptr<PgInheritsContainer> m_inherits;
|
||||
std::shared_ptr<PgLanguageContainer> m_languages;
|
||||
std::shared_ptr<PgSequenceContainer> m_sequences;
|
||||
|
||||
template <typename T>
|
||||
void load2(std::shared_ptr<T> &ptr, Pgsql::Connection &conn)
|
||||
|
|
@ -110,7 +113,7 @@ private:
|
|||
if (!ptr)
|
||||
ptr = std::make_shared<T>(*this);
|
||||
|
||||
load(conn, *ptr);
|
||||
ptr->loadAll(conn);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
23
pglablib/catalog/PgSequence.cpp
Normal file
23
pglablib/catalog/PgSequence.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "PgSequence.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "PgTypeContainer.h"
|
||||
#include <QStringBuilder>
|
||||
|
||||
QString PgSequence::createSql() const
|
||||
{
|
||||
QString sql;
|
||||
sql = "CREATE";
|
||||
// [ TEMPORARY | TEMP ]
|
||||
sql += " SEQUENCE " % fullyQualifiedQuotedObjectName();
|
||||
if (catalog().serverVersion() >= 100000)
|
||||
sql += "\n AS " % catalog().types()->getByKey(typid)->objectName();
|
||||
sql += QString("\n INCREMENT %1\n MINVALUE %2"
|
||||
" MAXVALUE %3\n START %4 CACHE %5")
|
||||
.arg(increment).arg(min).arg(max).arg(start).arg(cache);
|
||||
if (cycled)
|
||||
sql += "\n CYCLE";
|
||||
|
||||
// [ OWNED BY { table_name.column_name | NONE } ]
|
||||
sql += ";";
|
||||
return sql;
|
||||
}
|
||||
23
pglablib/catalog/PgSequence.h
Normal file
23
pglablib/catalog/PgSequence.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef PGSEQUENCE_H
|
||||
#define PGSEQUENCE_H
|
||||
|
||||
#include "catalog/PgClass.h"
|
||||
|
||||
class PgSequence: public PgClass {
|
||||
public:
|
||||
Oid typid = InvalidOid;
|
||||
int64_t last = 0;
|
||||
int64_t start = 0;
|
||||
int64_t increment = 0;
|
||||
int64_t max = 0;
|
||||
int64_t min = 0;
|
||||
int64_t cache = 0;
|
||||
int64_t log = 0;
|
||||
bool cycled = false;
|
||||
bool called = false;
|
||||
|
||||
using PgClass::PgClass;
|
||||
QString createSql() const;
|
||||
};
|
||||
|
||||
#endif // PGSEQUENCE_H
|
||||
83
pglablib/catalog/PgSequenceContainer.cpp
Normal file
83
pglablib/catalog/PgSequenceContainer.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include "PgSequenceContainer.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include "Pgsql_Col.h"
|
||||
#include "PgDatabaseCatalog.h"
|
||||
#include "PgNamespaceContainer.h"
|
||||
#include <iterator>
|
||||
|
||||
std::string PgSequenceContainer::getLoadQuery() const
|
||||
{
|
||||
std::string select = "SELECT pg_class.oid, relname, relnamespace, reltype, reloftype, "
|
||||
" relowner, relam, relfilenode, reltablespace, relpages, "
|
||||
" reltuples, reltoastrelid, relisshared, relpersistence, "
|
||||
" relkind, relhasoids, relispopulated, relfrozenxid, relminmxid, "
|
||||
" reloptions, relacl \n";
|
||||
std::string from =
|
||||
"FROM pg_catalog.pg_class \n";
|
||||
|
||||
// Starting with pg10 constanst data of sequences is retrieved from pg_sequence
|
||||
// instead of SELECTing the sequence
|
||||
if (minimumVersion(100000)) {
|
||||
select +=
|
||||
" , seqtypid, seqstart, seqincrement, seqmax, \n"
|
||||
" seqmin, seqcache, seqcycle \n";
|
||||
from +=
|
||||
" JOIN pg_catalog.pg_sequence ON (pg_class.oid=seqrelid) \n";
|
||||
}
|
||||
|
||||
return select +
|
||||
from +
|
||||
"WHERE relkind='S'";
|
||||
}
|
||||
|
||||
PgSequence PgSequenceContainer::loadElem(const Pgsql::Row &row)
|
||||
{
|
||||
Pgsql::Col col(row);
|
||||
Oid class_oid = col.nextValue();
|
||||
QString name = col.nextValue();
|
||||
Oid schema_oid = col.nextValue();
|
||||
|
||||
PgSequence v(m_catalog, class_oid, name, schema_oid);
|
||||
Oid owner ;
|
||||
AclList acl_list;
|
||||
col >> v.type >> v.oftype
|
||||
>> owner >> v.am >> v.filenode >> v.tablespace >> v.pages_est
|
||||
>> v.tuples_est >> v.toastrelid >> v.isshared >> v.persistence
|
||||
>> v.kind >> v.hasoids >> v.ispopulated >> v.frozenxid >> v.minmxid
|
||||
>> v.options >> acl_list;
|
||||
// Read pg_sequence fields
|
||||
if (minimumVersion(100000))
|
||||
col >> v.typid >> v.start >> v.increment >> v.max >> v.min >> v.cache >> v.cycled;
|
||||
|
||||
v.setOwnerOid(owner);
|
||||
v.setAcls(std::move(acl_list));
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
void PgSequenceContainer::loadAll(Pgsql::Connection &conn)
|
||||
{
|
||||
IPgContainer::loadAll(conn);
|
||||
|
||||
for (auto && seq : m_container) {
|
||||
auto fqn = seq.fullyQualifiedQuotedObjectName().toUtf8();
|
||||
// SELECTs on a sequence return less fields starting with pg10
|
||||
// the missing fields are now in pg_sequence
|
||||
std::string q("SELECT last_value, log_cnt, is_called");
|
||||
if (lessThenVersion(100000)) {
|
||||
q += ", start_value, increment_by, max_value, \n"
|
||||
" min_value, cache_value, is_cycled";
|
||||
}
|
||||
q += "\nFROM " + std::string(fqn.data(), static_cast<size_t>(fqn.count()));
|
||||
auto && res = conn.query(q.c_str());
|
||||
if (res.rows() == 1) {
|
||||
Pgsql::Row row(res, 0);
|
||||
Pgsql::Col col(row);
|
||||
col >> seq.last >> seq.log >> seq.called;
|
||||
if (lessThenVersion(100000)) {
|
||||
col >> seq.start >> seq.increment >> seq.max >> seq.min >> seq.cache >> seq.cycled;
|
||||
seq.typid = Pgsql::int8_oid; // bigint used to be the only choice
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
pglablib/catalog/PgSequenceContainer.h
Normal file
25
pglablib/catalog/PgSequenceContainer.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef PGSEQUENCECONTAINER_H
|
||||
#define PGSEQUENCECONTAINER_H
|
||||
|
||||
#include "PgContainer.h"
|
||||
#include "PgSequence.h"
|
||||
|
||||
namespace Pgsql {
|
||||
|
||||
class Result;
|
||||
|
||||
}
|
||||
|
||||
class PgSequenceContainer: public PgContainer<PgSequence> {
|
||||
public:
|
||||
using PgContainer<PgSequence>::PgContainer;
|
||||
|
||||
std::string getLoadQuery() const override;
|
||||
|
||||
void loadAll(Pgsql::Connection &conn) override;
|
||||
protected:
|
||||
PgSequence loadElem(const Pgsql::Row &row) override;
|
||||
|
||||
};
|
||||
|
||||
#endif // PGSEQUENCECONTAINER_H
|
||||
|
|
@ -82,7 +82,9 @@ SOURCES += \
|
|||
model/CollationModelFactory.cpp \
|
||||
catalog/PgLanguageContainer.cpp \
|
||||
catalog/PgLanguage.cpp \
|
||||
catalog/PgAcl.cpp
|
||||
catalog/PgAcl.cpp \
|
||||
catalog/PgSequence.cpp \
|
||||
catalog/PgSequenceContainer.cpp
|
||||
|
||||
HEADERS += \
|
||||
Pglablib.h \
|
||||
|
|
@ -149,7 +151,9 @@ HEADERS += \
|
|||
model/CollationModelFactory.h \
|
||||
catalog/PgLanguageContainer.h \
|
||||
catalog/PgLanguage.h \
|
||||
catalog/PgAcl.h
|
||||
catalog/PgAcl.h \
|
||||
catalog/PgSequence.h \
|
||||
catalog/PgSequenceContainer.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue