Bunch of raw pointers replaced with smart pointers of references.
This commit is contained in:
parent
5a199c9138
commit
ea035f58c8
20 changed files with 67 additions and 79 deletions
|
|
@ -11,7 +11,7 @@ DatabasesTableModel::DatabasesTableModel(QObject *parent)
|
|||
{
|
||||
}
|
||||
|
||||
void DatabasesTableModel::setDatabaseList(const PgDatabaseCatalogue* cat)
|
||||
void DatabasesTableModel::setDatabaseList(std::shared_ptr<const PgDatabaseCatalogue> cat)
|
||||
{
|
||||
beginResetModel();
|
||||
m_catalog = cat;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define DATABASESTABLEMODEL_H
|
||||
|
||||
#include "BaseTableModel.h"
|
||||
#include <memory>
|
||||
|
||||
class PgDatabaseContainer;
|
||||
class PgDatabaseCatalogue;
|
||||
|
|
@ -22,7 +23,7 @@ public:
|
|||
|
||||
explicit DatabasesTableModel(QObject *parent);
|
||||
|
||||
void setDatabaseList(const PgDatabaseCatalogue* cat);
|
||||
void setDatabaseList(std::shared_ptr<const PgDatabaseCatalogue> cat);
|
||||
|
||||
// Header:
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
|
|
@ -34,11 +35,9 @@ public:
|
|||
virtual Oid getType(int column) const override;
|
||||
virtual QVariant getData(const QModelIndex &index) const override;
|
||||
|
||||
// QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
private:
|
||||
const PgDatabaseCatalogue *m_catalog = nullptr;
|
||||
const PgDatabaseContainer *m_databases = nullptr;
|
||||
std::shared_ptr<const PgDatabaseCatalogue> m_catalog;
|
||||
std::shared_ptr<const PgDatabaseContainer> m_databases;
|
||||
};
|
||||
|
||||
#endif // DATABASESTABLEMODEL_H
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include "OpenDatabase.h"
|
||||
#include "OpenDatabase.h"
|
||||
#include "PgDatabaseCatalogue.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include "TypeSelectionItemModel.h"
|
||||
|
|
@ -19,13 +19,12 @@ Expected<OpenDatabase*> OpenDatabase::createOpenDatabase(const ConnectionConfig
|
|||
OpenDatabase::OpenDatabase(const ConnectionConfig& cfg, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_config(cfg)
|
||||
, m_catalogue(new PgDatabaseCatalogue)
|
||||
, m_catalogue(std::make_shared<PgDatabaseCatalogue>())
|
||||
{
|
||||
}
|
||||
|
||||
OpenDatabase::~OpenDatabase()
|
||||
{
|
||||
delete m_catalogue;
|
||||
}
|
||||
|
||||
bool OpenDatabase::Init()
|
||||
|
|
@ -40,7 +39,7 @@ bool OpenDatabase::Init()
|
|||
return false;
|
||||
}
|
||||
|
||||
PgDatabaseCatalogue* OpenDatabase::catalogue()
|
||||
std::shared_ptr<PgDatabaseCatalogue> OpenDatabase::catalogue()
|
||||
{
|
||||
return m_catalogue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#ifndef OPENDATABASE_H
|
||||
#ifndef OPENDATABASE_H
|
||||
#define OPENDATABASE_H
|
||||
|
||||
#include <QObject>
|
||||
#include "ConnectionConfig.h"
|
||||
#include "Expected.h"
|
||||
#include <memory>
|
||||
|
||||
class PgDatabaseCatalogue;
|
||||
class TypeSelectionItemModel;
|
||||
|
|
@ -21,7 +22,7 @@ public:
|
|||
OpenDatabase& operator=(const OpenDatabase &) = delete;
|
||||
~OpenDatabase();
|
||||
|
||||
PgDatabaseCatalogue* catalogue();
|
||||
std::shared_ptr<PgDatabaseCatalogue> catalogue();
|
||||
TypeSelectionItemModel* typeSelectionModel();
|
||||
signals:
|
||||
|
||||
|
|
@ -29,7 +30,7 @@ public slots:
|
|||
|
||||
private:
|
||||
ConnectionConfig m_config;
|
||||
PgDatabaseCatalogue *m_catalogue;
|
||||
std::shared_ptr<PgDatabaseCatalogue> m_catalogue;
|
||||
|
||||
TypeSelectionItemModel *m_typeSelectionModel = nullptr;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,16 @@
|
|||
#include "PgAuthIdContainer.h"
|
||||
#include "PgAuthIdContainer.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include "PgDatabaseCatalogue.h"
|
||||
|
||||
PgAuthIdContainer::PgAuthIdContainer(PgDatabaseCatalogue *cat)
|
||||
: PgContainer<PgAuthId>(cat)
|
||||
{}
|
||||
|
||||
std::string PgAuthIdContainer::getLoadQuery() const
|
||||
{
|
||||
std::string result =
|
||||
"SELECT oid, rolname, rolsuper, rolinherit, rolcreaterole, rolcreatedb, "
|
||||
" rolcanlogin, rolreplication, rolconnlimit, rolvaliduntil";
|
||||
if (m_catalogue->serverVersion() >= 90500)
|
||||
auto cat = m_catalogue.lock();
|
||||
if (cat && cat->serverVersion() >= 90500)
|
||||
result += ", rolbypassrls";
|
||||
|
||||
result += "\n"
|
||||
"FROM pg_authid";
|
||||
return result;
|
||||
|
|
@ -23,7 +21,8 @@ void PgAuthIdContainer::load(const Pgsql::Result &res)
|
|||
const int n_rows = res.rows();
|
||||
m_container.clear();
|
||||
m_container.reserve(n_rows);
|
||||
bool with_rls = (m_catalogue->serverVersion() >= 90500);
|
||||
auto cat = m_catalogue.lock();
|
||||
bool with_rls = (cat && cat->serverVersion() >= 90500);
|
||||
for (auto row : res) {
|
||||
PgAuthId v;
|
||||
v.oid << row.get(0); // InvalidOid;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#ifndef PGAUTHIDCONTAINER_H
|
||||
#ifndef PGAUTHIDCONTAINER_H
|
||||
#define PGAUTHIDCONTAINER_H
|
||||
|
||||
#include <vector>
|
||||
|
|
@ -14,11 +14,10 @@ namespace Pgsql {
|
|||
|
||||
class PgAuthIdContainer: public PgContainer<PgAuthId> {
|
||||
public:
|
||||
explicit PgAuthIdContainer(PgDatabaseCatalogue *cat);
|
||||
using PgContainer<PgAuthId>::PgContainer;
|
||||
|
||||
std::string getLoadQuery() const;
|
||||
void load(const Pgsql::Result &res);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
#ifndef PGCONTAINER_H
|
||||
#ifndef PGCONTAINER_H
|
||||
#define PGCONTAINER_H
|
||||
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
|
|
@ -12,7 +13,7 @@ class PgContainer {
|
|||
public:
|
||||
using t_Container = std::vector<T>; ///< Do not assume it will stay a vector only expect bidirectional access
|
||||
|
||||
explicit PgContainer(PgDatabaseCatalogue *cat)
|
||||
explicit PgContainer(std::weak_ptr<PgDatabaseCatalogue> cat)
|
||||
: m_catalogue(cat)
|
||||
{}
|
||||
|
||||
|
|
@ -60,7 +61,7 @@ public:
|
|||
return m_container.at(idx);
|
||||
}
|
||||
protected:
|
||||
PgDatabaseCatalogue *m_catalogue;
|
||||
std::weak_ptr<PgDatabaseCatalogue> m_catalogue;
|
||||
t_Container m_container;
|
||||
private:
|
||||
T m_invalidInstance;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include "Pgsql_Connection.h"
|
||||
|
||||
|
||||
QString getRoleNameFromOid(const PgDatabaseCatalogue *cat, Oid oid)
|
||||
QString getRoleNameFromOid(std::shared_ptr<const PgDatabaseCatalogue> cat, Oid oid)
|
||||
{
|
||||
QString name;
|
||||
auto auth_ids = cat->authIds();
|
||||
|
|
@ -18,7 +18,7 @@ QString getRoleNameFromOid(const PgDatabaseCatalogue *cat, Oid oid)
|
|||
return name;
|
||||
}
|
||||
|
||||
QString getRoleDisplayString(const PgDatabaseCatalogue *cat, Oid oid)
|
||||
QString getRoleDisplayString(std::shared_ptr<const PgDatabaseCatalogue> cat, Oid oid)
|
||||
{
|
||||
QString name = getRoleNameFromOid(cat, oid);
|
||||
return QString("%1 (%2)").arg(name).arg(oid);
|
||||
|
|
@ -31,7 +31,6 @@ PgDatabaseCatalogue::PgDatabaseCatalogue()
|
|||
|
||||
PgDatabaseCatalogue::~PgDatabaseCatalogue()
|
||||
{
|
||||
delete m_types;
|
||||
}
|
||||
|
||||
void PgDatabaseCatalogue::loadAll(Pgsql::Connection &conn)
|
||||
|
|
@ -56,8 +55,8 @@ void PgDatabaseCatalogue::loadInfo(Pgsql::Connection &conn)
|
|||
|
||||
void PgDatabaseCatalogue::loadTypes(Pgsql::Connection &conn)
|
||||
{
|
||||
if (m_types == nullptr)
|
||||
m_types = new PgTypeContainer(this);
|
||||
if (!m_types)
|
||||
m_types = std::make_shared<PgTypeContainer>(shared_from_this());
|
||||
|
||||
std::string q = m_types->getLoadQuery();
|
||||
Pgsql::Result result = conn.query(q.c_str());
|
||||
|
|
@ -70,8 +69,8 @@ void PgDatabaseCatalogue::loadTypes(Pgsql::Connection &conn)
|
|||
|
||||
void PgDatabaseCatalogue::loadDatabases(Pgsql::Connection &conn)
|
||||
{
|
||||
if (m_databases == nullptr)
|
||||
m_databases = new PgDatabaseContainer(this);
|
||||
if (!m_databases)
|
||||
m_databases = std::make_shared<PgDatabaseContainer>(shared_from_this());
|
||||
|
||||
|
||||
std::string q = m_databases->getLoadQuery();
|
||||
|
|
@ -84,8 +83,8 @@ void PgDatabaseCatalogue::loadDatabases(Pgsql::Connection &conn)
|
|||
|
||||
void PgDatabaseCatalogue::loadAuthIds(Pgsql::Connection &conn)
|
||||
{
|
||||
if (m_authIds == nullptr)
|
||||
m_authIds = new PgAuthIdContainer(this);
|
||||
if (!m_authIds)
|
||||
m_authIds = std::make_shared<PgAuthIdContainer>(shared_from_this());
|
||||
|
||||
std::string q = m_authIds->getLoadQuery();
|
||||
Pgsql::Result result = conn.query(q.c_str());
|
||||
|
|
@ -105,17 +104,17 @@ int PgDatabaseCatalogue::serverVersion() const
|
|||
return m_serverVersion;
|
||||
}
|
||||
|
||||
const PgTypeContainer* PgDatabaseCatalogue::types() const
|
||||
std::shared_ptr<const PgTypeContainer> PgDatabaseCatalogue::types() const
|
||||
{
|
||||
return m_types;
|
||||
}
|
||||
|
||||
const PgDatabaseContainer *PgDatabaseCatalogue::databases() const
|
||||
std::shared_ptr<const PgDatabaseContainer> PgDatabaseCatalogue::databases() const
|
||||
{
|
||||
return m_databases;
|
||||
}
|
||||
|
||||
const PgAuthIdContainer *PgDatabaseCatalogue::authIds() const
|
||||
std::shared_ptr<const PgAuthIdContainer> PgDatabaseCatalogue::authIds() const
|
||||
{
|
||||
return m_authIds;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <libpq-fe.h>
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Pgsql {
|
||||
|
|
@ -15,7 +16,7 @@ class PgTypeContainer;
|
|||
class PgDatabaseContainer;
|
||||
class PgAuthIdContainer;
|
||||
|
||||
class PgDatabaseCatalogue {
|
||||
class PgDatabaseCatalogue: public std::enable_shared_from_this<PgDatabaseCatalogue> {
|
||||
public:
|
||||
PgDatabaseCatalogue();
|
||||
PgDatabaseCatalogue(const PgDatabaseCatalogue&) = delete;
|
||||
|
|
@ -33,19 +34,19 @@ public:
|
|||
const QString& serverVersionString() const;
|
||||
int serverVersion() const;
|
||||
|
||||
const PgTypeContainer* types() const;
|
||||
const PgDatabaseContainer *databases() const;
|
||||
const PgAuthIdContainer *authIds() const;
|
||||
std::shared_ptr<const PgTypeContainer> types() const;
|
||||
std::shared_ptr<const PgDatabaseContainer> databases() const;
|
||||
std::shared_ptr<const PgAuthIdContainer> authIds() const;
|
||||
private:
|
||||
QString m_serverVersionString;
|
||||
int m_serverVersion;
|
||||
PgTypeContainer *m_types = nullptr;
|
||||
PgDatabaseContainer *m_databases = nullptr;
|
||||
PgAuthIdContainer *m_authIds = nullptr;
|
||||
std::shared_ptr<PgTypeContainer> m_types;
|
||||
std::shared_ptr<PgDatabaseContainer> m_databases;
|
||||
std::shared_ptr<PgAuthIdContainer> m_authIds;
|
||||
};
|
||||
|
||||
QString getRoleNameFromOid(const PgDatabaseCatalogue *cat, Oid oid);
|
||||
QString getRoleDisplayString(const PgDatabaseCatalogue *cat, Oid oid);
|
||||
QString getRoleNameFromOid(std::shared_ptr<const PgDatabaseCatalogue> cat, Oid oid);
|
||||
QString getRoleDisplayString(std::shared_ptr<const PgDatabaseCatalogue> cat, Oid oid);
|
||||
|
||||
|
||||
#endif // PGSQLDATABASECATALOGUE_H
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
#include "Pgsql_Connection.h"
|
||||
#include "Pgsql_Col.h"
|
||||
|
||||
PgDatabaseContainer::PgDatabaseContainer(PgDatabaseCatalogue *cat)
|
||||
: PgContainer<PgDatabase>(cat)
|
||||
{}
|
||||
//PgDatabaseContainer::PgDatabaseContainer(PgDatabaseCatalogue *cat)
|
||||
// : PgContainer<PgDatabase>(cat)
|
||||
//{}
|
||||
|
||||
std::string PgDatabaseContainer::getLoadQuery() const
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#ifndef PGDATABASECONTAINER_H
|
||||
#ifndef PGDATABASECONTAINER_H
|
||||
#define PGDATABASECONTAINER_H
|
||||
|
||||
#include <vector>
|
||||
|
|
@ -14,7 +14,8 @@ namespace Pgsql {
|
|||
|
||||
class PgDatabaseContainer: public PgContainer<PgDatabase> {
|
||||
public:
|
||||
explicit PgDatabaseContainer(PgDatabaseCatalogue *cat);
|
||||
//explicit PgDatabaseContainer(PgDatabaseCatalogue *cat);
|
||||
using PgContainer<PgDatabase>::PgContainer;
|
||||
|
||||
std::string getLoadQuery() const;
|
||||
void load(const Pgsql::Result &res);
|
||||
|
|
|
|||
|
|
@ -1,12 +1,7 @@
|
|||
#include "PgTypeContainer.h"
|
||||
#include "PgTypeContainer.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
#include <algorithm>
|
||||
|
||||
PgTypeContainer::PgTypeContainer(PgDatabaseCatalogue *cat)
|
||||
: PgContainer<PgType>(cat)
|
||||
{}
|
||||
|
||||
|
||||
//const PgType& PgTypeContainer::getTypeByOid(Oid oid) const
|
||||
//{
|
||||
// auto lb_result = std::lower_bound(m_types.begin(), m_types.end(), oid);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef PGTYPECONTAINER_H
|
||||
#ifndef PGTYPECONTAINER_H
|
||||
#define PGTYPECONTAINER_H
|
||||
|
||||
#include <vector>
|
||||
#include "PgType.h"
|
||||
#include "PgContainer.h"
|
||||
#include <vector>
|
||||
|
||||
namespace Pgsql {
|
||||
|
||||
|
|
@ -13,15 +13,8 @@ namespace Pgsql {
|
|||
|
||||
class PgTypeContainer: public PgContainer<PgType> {
|
||||
public:
|
||||
// using t_Types = std::vector<PgType>; ///< Do not assume it will stay a vector only expect bidirectional access
|
||||
using PgContainer<PgType>::PgContainer;
|
||||
|
||||
explicit PgTypeContainer(PgDatabaseCatalogue *cat);
|
||||
|
||||
// t_Types::const_iterator begin() const { return m_types.begin(); }
|
||||
// t_Types::const_iterator end() const { return m_types.end(); }
|
||||
|
||||
// void clear();
|
||||
// int count() const { return (int)m_types.size(); }
|
||||
std::string getLoadQuery();
|
||||
void load(const Pgsql::Result &res);
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
|
|||
OpenDatabase* open_database = m_win->getDatabase();
|
||||
if (open_database) {
|
||||
auto cat = open_database->catalogue();
|
||||
highlighter->setTypes(cat->types());
|
||||
highlighter->setTypes(*cat->types());
|
||||
}
|
||||
|
||||
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTab::queryTextChanged);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ RolesTableModel::RolesTableModel(QObject *parent)
|
|||
{
|
||||
}
|
||||
|
||||
void RolesTableModel::setRoleList(const PgAuthIdContainer* roles)
|
||||
void RolesTableModel::setRoleList(std::shared_ptr<const PgAuthIdContainer> roles)
|
||||
{
|
||||
beginResetModel();
|
||||
m_roles = roles;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
|
||||
#include "BaseTableModel.h"
|
||||
#include <memory>
|
||||
|
||||
class PgAuthIdContainer;
|
||||
|
||||
|
|
@ -20,7 +21,7 @@ public:
|
|||
|
||||
explicit RolesTableModel(QObject *parent);
|
||||
|
||||
void setRoleList(const PgAuthIdContainer* roles);
|
||||
void setRoleList(std::shared_ptr<const PgAuthIdContainer> roles);
|
||||
|
||||
// Header:
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
|
|
@ -35,7 +36,7 @@ public:
|
|||
// QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
private:
|
||||
const PgAuthIdContainer *m_roles = nullptr;
|
||||
std::shared_ptr<const PgAuthIdContainer> m_roles;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -126,10 +126,10 @@ SqlSyntaxHighlighter::~SqlSyntaxHighlighter()
|
|||
{
|
||||
}
|
||||
|
||||
void SqlSyntaxHighlighter::setTypes(const PgTypeContainer *types)
|
||||
void SqlSyntaxHighlighter::setTypes(const PgTypeContainer& types)
|
||||
{
|
||||
m_typeNames.clear();
|
||||
for (auto &e : *types) {
|
||||
for (auto &e : types) {
|
||||
m_typeNames.insert(e.typname);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ public:
|
|||
SqlSyntaxHighlighter(QTextDocument *parent = nullptr);
|
||||
~SqlSyntaxHighlighter();
|
||||
|
||||
void setTypes(const PgTypeContainer *types);
|
||||
void setTypes(const PgTypeContainer& types);
|
||||
protected:
|
||||
void highlightBlock(const QString &text) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -56,13 +56,12 @@ QVariant TypeSelectionItemModel::data(const QModelIndex &index, int role) const
|
|||
return result;
|
||||
}
|
||||
|
||||
void TypeSelectionItemModel::setTypeList(const PgTypeContainer* types)
|
||||
void TypeSelectionItemModel::setTypeList(std::shared_ptr<const PgTypeContainer> types)
|
||||
{
|
||||
beginResetModel();
|
||||
m_types.clear();
|
||||
for (const auto &e : *types) {
|
||||
if (e.typcategory != "A"
|
||||
&& e.typtype != "c") {
|
||||
if (e.typcategory != "A" && e.typtype != "c") {
|
||||
m_types.push_back(e.typname);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define TYPESELECTIONITEMMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class PgTypeContainer;
|
||||
|
|
@ -11,7 +12,7 @@ class TypeSelectionItemModel : public QAbstractListModel {
|
|||
public:
|
||||
explicit TypeSelectionItemModel(QObject *parent = 0);
|
||||
|
||||
void setTypeList(const PgTypeContainer* types);
|
||||
void setTypeList(std::shared_ptr<const PgTypeContainer> types);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue