commit
cc6ed112b6
|
|
@ -209,6 +209,11 @@ inline Row<T> create() {
|
||||||
return QSharedPointer<T>(new T);
|
return QSharedPointer<T>(new T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline Row<T> create(QObject *parent) {
|
||||||
|
return QSharedPointer<T>(new T(parent));
|
||||||
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline T *get(T *row) {
|
inline T *get(T *row) {
|
||||||
return row;
|
return row;
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ QString PostgreSqlGenerator::fieldType(FieldModel *field)
|
||||||
case QMetaType::QJsonValue:
|
case QMetaType::QJsonValue:
|
||||||
case QMetaType::QJsonObject:
|
case QMetaType::QJsonObject:
|
||||||
case QMetaType::QJsonDocument:
|
case QMetaType::QJsonDocument:
|
||||||
return "JSON";
|
return "JSONB";
|
||||||
|
|
||||||
case QMetaType::QStringList:
|
case QMetaType::QStringList:
|
||||||
return "TEXT[]";
|
return "TEXT[]";
|
||||||
|
|
|
||||||
|
|
@ -159,6 +159,9 @@ protected:
|
||||||
QString agregateText(const AgregateType &t, const QString &arg = QString()) const;
|
QString agregateText(const AgregateType &t, const QString &arg = QString()) const;
|
||||||
QString fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const;
|
QString fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const;
|
||||||
// QString createWhere(QList<WherePhrase> &wheres);
|
// QString createWhere(QList<WherePhrase> &wheres);
|
||||||
|
|
||||||
|
void replaceTableNames(QString &command);
|
||||||
|
void removeTableNames(QString &command);
|
||||||
};
|
};
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -186,11 +186,4 @@ void SqlServerGenerator::appendSkipTake(QString &sql, int skip, int take)
|
||||||
.arg(skip).arg(take));
|
.arg(skip).arg(take));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SqlServerGenerator::replaceTableNames(QString &command)
|
|
||||||
{
|
|
||||||
foreach (TableModel *m, TableModel::allModels())
|
|
||||||
command = command
|
|
||||||
.replace("[" + m->className() + "]", m->name() );
|
|
||||||
}
|
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
10
src/query.h
10
src/query.h
|
|
@ -57,14 +57,6 @@ template <class T>
|
||||||
bool m_autoDelete;
|
bool m_autoDelete;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//#ifdef NUT_SHARED_POINTER
|
|
||||||
// typedef QList<QSharedPointer<T>> RowList;
|
|
||||||
// typedef QSharedPointer<T> Row;
|
|
||||||
//#else
|
|
||||||
// typedef QList<T*> RowList;
|
|
||||||
// typedef T* Row;
|
|
||||||
//#endif
|
|
||||||
|
|
||||||
explicit Query(Database *database, TableSetBase *tableSet, bool autoDelete);
|
explicit Query(Database *database, TableSetBase *tableSet, bool autoDelete);
|
||||||
~Query();
|
~Query();
|
||||||
|
|
||||||
|
|
@ -185,7 +177,7 @@ Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
d->tableName, d->fieldPhrase, d->wherePhrase, d->orderPhrase,
|
d->tableName, d->fieldPhrase, d->wherePhrase, d->orderPhrase,
|
||||||
d->relations, d->skip, d->take);
|
d->relations, d->skip, d->take);
|
||||||
qDebug()<<d->sql;
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
if (q.lastError().isValid()) {
|
if (q.lastError().isValid()) {
|
||||||
qDebug() << q.lastError().text();
|
qDebug() << q.lastError().text();
|
||||||
|
|
|
||||||
|
|
@ -40,9 +40,8 @@ void SqlModel::setRenderer(const std::function<QVariant (int, QVariant)> &render
|
||||||
}
|
}
|
||||||
|
|
||||||
SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) :
|
SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) :
|
||||||
QAbstractTableModel(parent), d_ptr(new SqlModelPrivate(this)), _renderer(nullptr)
|
QAbstractTableModel(parent), d(new SqlModelPrivate(this)), _renderer(nullptr)
|
||||||
{
|
{
|
||||||
Q_D(SqlModel);
|
|
||||||
d->model = database->model()
|
d->model = database->model()
|
||||||
.tableByClassName(tableSet->childClassName());
|
.tableByClassName(tableSet->childClassName());
|
||||||
d->tableName = d->model->name();
|
d->tableName = d->model->name();
|
||||||
|
|
@ -54,20 +53,17 @@ SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent)
|
||||||
int SqlModel::rowCount(const QModelIndex &parent) const
|
int SqlModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
Q_UNUSED(parent);
|
Q_UNUSED(parent);
|
||||||
Q_D(const SqlModel);
|
|
||||||
return d->rows.count();
|
return d->rows.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
int SqlModel::columnCount(const QModelIndex &parent) const
|
int SqlModel::columnCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
Q_UNUSED(parent);
|
Q_UNUSED(parent);
|
||||||
Q_D(const SqlModel);
|
|
||||||
return d->model->fields().count();
|
return d->model->fields().count();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant SqlModel::data(const QModelIndex &index, int role) const
|
QVariant SqlModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
Q_D(const SqlModel);
|
|
||||||
if (!index.isValid())
|
if (!index.isValid())
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
|
|
@ -104,7 +100,7 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const
|
||||||
|
|
||||||
void SqlModel::setRows(RowList<Table> rows)
|
void SqlModel::setRows(RowList<Table> rows)
|
||||||
{
|
{
|
||||||
Q_D(SqlModel);
|
d.detach();
|
||||||
beginRemoveRows(QModelIndex(), 0, d->rows.count());
|
beginRemoveRows(QModelIndex(), 0, d->rows.count());
|
||||||
d->rows.clear();
|
d->rows.clear();
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
|
|
@ -115,7 +111,7 @@ void SqlModel::setRows(RowList<Table> rows)
|
||||||
|
|
||||||
void SqlModel::append(Row<Table> table)
|
void SqlModel::append(Row<Table> table)
|
||||||
{
|
{
|
||||||
Q_D(SqlModel);
|
d.detach();
|
||||||
beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count());
|
beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count());
|
||||||
d->rows.append(table);
|
d->rows.append(table);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
|
|
@ -129,7 +125,6 @@ void SqlModel::append(Row<Table> table)
|
||||||
QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
{
|
{
|
||||||
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
||||||
Q_D(const SqlModel);
|
|
||||||
return d->model->field(section)->displayName;
|
return d->model->field(section)->displayName;
|
||||||
}
|
}
|
||||||
return QAbstractItemModel::headerData(section, orientation, role);
|
return QAbstractItemModel::headerData(section, orientation, role);
|
||||||
|
|
@ -137,11 +132,10 @@ QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role
|
||||||
|
|
||||||
Row<Table> SqlModel::at(const int &i) const
|
Row<Table> SqlModel::at(const int &i) const
|
||||||
{
|
{
|
||||||
Q_D(const SqlModel);
|
|
||||||
return d->rows.at(i);
|
return d->rows.at(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
SqlModelPrivate::SqlModelPrivate(SqlModel *parent) : q_ptr(parent)
|
SqlModelPrivate::SqlModelPrivate(SqlModel *parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,8 @@
|
||||||
|
|
||||||
#include <QtCore/QAbstractTableModel>
|
#include <QtCore/QAbstractTableModel>
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
#include "sqlmodel_p.h"
|
||||||
|
#include <QExplicitlySharedDataPointer>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
|
@ -30,7 +32,6 @@ NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
class Database;
|
class Database;
|
||||||
class TableSetBase;
|
class TableSetBase;
|
||||||
class SqlModelPrivate;
|
|
||||||
class Table;
|
class Table;
|
||||||
class TableModel;
|
class TableModel;
|
||||||
|
|
||||||
|
|
@ -60,8 +61,7 @@ public:
|
||||||
void setRenderer(const std::function<QVariant (int, QVariant)> &renderer);
|
void setRenderer(const std::function<QVariant (int, QVariant)> &renderer);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SqlModelPrivate *d_ptr;
|
QExplicitlySharedDataPointer<SqlModelPrivate> d;
|
||||||
Q_DECLARE_PRIVATE(SqlModel)
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void beforeShowText(int col, QVariant &value);
|
void beforeShowText(int col, QVariant &value);
|
||||||
|
|
@ -70,8 +70,6 @@ signals:
|
||||||
template<class T>
|
template<class T>
|
||||||
Q_OUTOFLINE_TEMPLATE void SqlModel::setTable(RowList<T> rows)
|
Q_OUTOFLINE_TEMPLATE void SqlModel::setTable(RowList<T> rows)
|
||||||
{
|
{
|
||||||
Q_D(SqlModel);
|
|
||||||
|
|
||||||
RowList<Table> tab;
|
RowList<Table> tab;
|
||||||
foreach (auto t, rows)
|
foreach (auto t, rows)
|
||||||
tab.append(t);
|
tab.append(t);
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,7 @@ NUT_BEGIN_NAMESPACE
|
||||||
class SqlModel;
|
class SqlModel;
|
||||||
class Table;
|
class Table;
|
||||||
class TableModel;
|
class TableModel;
|
||||||
class SqlModelPrivate {
|
class SqlModelPrivate : public QSharedData {
|
||||||
SqlModel *q_ptr;
|
|
||||||
Q_DECLARE_PUBLIC(SqlModel)
|
|
||||||
public:
|
public:
|
||||||
explicit SqlModelPrivate(SqlModel *parent);
|
explicit SqlModelPrivate(SqlModel *parent);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,8 @@ public:
|
||||||
|
|
||||||
void append(Row<T> t);
|
void append(Row<T> t);
|
||||||
void append(RowList<T> t);
|
void append(RowList<T> t);
|
||||||
void remove(T *t);
|
void remove(Row<T> t);
|
||||||
void remove(QList<T *> t);
|
void remove(RowList<T> t);
|
||||||
|
|
||||||
int length() const;
|
int length() const;
|
||||||
T *at(int i) const;
|
T *at(int i) const;
|
||||||
|
|
@ -135,18 +135,19 @@ Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(RowList<T> t)
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(T *t)
|
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(Row<T> t)
|
||||||
{
|
{
|
||||||
data.detach();
|
data.detach();
|
||||||
|
data->childs.removeOne(t.data());
|
||||||
|
data->tables.remove(t.data());
|
||||||
data->childs.removeOne(t);
|
data->childs.removeOne(t);
|
||||||
data->tables.remove(t);
|
|
||||||
t->setStatus(Table::Deleted);
|
t->setStatus(Table::Deleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(QList<T *> t)
|
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(RowList<T> t)
|
||||||
{
|
{
|
||||||
foreach (T* i, t)
|
foreach (Row<T> i, t)
|
||||||
remove(i);
|
remove(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue