This commit is contained in:
Hamed Masafi 2019-06-18 18:10:40 +04:30
parent a9bcc76663
commit e946a54753
16 changed files with 218 additions and 23 deletions

View File

@ -1,5 +1,7 @@
#include "../src/table.h" #include "../src/table.h"
#include "../src/database.h" #include "../src/database.h"
#include "../src/sqlmodel.h"
#include "../src/tableset.h" #include "../src/tableset.h"
#include "../src/tablemodel.h"
#include "../src/query.h" #include "../src/query.h"

1
include/SqlModel Normal file
View File

@ -0,0 +1 @@
#include "../src/sqlmodel.h"

1
include/TableModel Normal file
View File

@ -0,0 +1 @@
#include "../src/tablemodel.h"

5
include/header_copier Normal file → Executable file
View File

@ -3,11 +3,10 @@
src_dir="src" src_dir="src"
namespace_name="nut" namespace_name="nut"
ns=$(echo $namespace_name|awk '{print tolower($0)}') #ns=$(echo $namespace_name|awk '{print tolower($0)}')
Ns="Nut" Ns="Nut"
NS=$(echo $namespace_name|awk '{print toupper($0)}') NS=$(echo $namespace_name|awk '{print toupper($0)}')
echo $NS
exit
create_sub_folder=true create_sub_folder=true

1
include/sqlmodel.h Normal file
View File

@ -0,0 +1 @@
#include "../src/sqlmodel.h"

1
include/tablemodel.h Normal file
View File

@ -0,0 +1 @@
#include "../src/tablemodel.h"

View File

@ -4,4 +4,3 @@ SUBDIRS += \
src \ src \
test test

View File

@ -278,7 +278,7 @@ bool DatabasePrivate::getCurrectScheema()
DatabaseModel DatabasePrivate::getLastScheema() DatabaseModel DatabasePrivate::getLastScheema()
{ {
ChangeLogTable *u = changeLogs->query() typename TableType<ChangeLogTable>::Row u = changeLogs->query()
->orderBy(!ChangeLogTable::idField()) ->orderBy(!ChangeLogTable::idField())
->first(); ->first();

View File

@ -192,4 +192,32 @@ public: \
#define NUT_NOT_NULL(x) NUT_INFO(__nut_NOT_NULL, x, 1) #define NUT_NOT_NULL(x) NUT_INFO(__nut_NOT_NULL, x, 1)
#define NUT_INDEX(name, field, order) #define NUT_INDEX(name, field, order)
template <class T>
struct TableType
{
#ifdef NUT_SHARED_POINTER
typedef QList<QSharedPointer<T>> RowList;
typedef QSet<QSharedPointer<T>> RowSet;
typedef QSharedPointer<T> Row;
#else
typedef QList<T*> RowList;
typedef QSet<T*> RowSet;
typedef T* Row;
#endif
};
//#ifdef NUT_SHARED_POINTER
// template <class T>
// using RowList = typename QList<QSharedPointer<T>>;
// template <typename T>
// using Row = typename QSharedPointer<T>;
//#else
// template <typename T>
// using RowList = typename QList<T*>;
// template <typename T>
// using Row = typename T*
//#endif
#endif // SYNTAX_DEFINES_H #endif // SYNTAX_DEFINES_H

View File

@ -834,6 +834,9 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v) const
return QString(); return QString();
} }
if (v.type() == QVariant::List)
return serialized;
return "'" + serialized + "'"; return "'" + serialized + "'";
} }

View File

@ -29,7 +29,11 @@
#include <QtSql/QSqlResult> #include <QtSql/QSqlResult>
#include <QtSql/QSqlError> #include <QtSql/QSqlError>
#include <QtSql/QSqlQueryModel> #include <QtSql/QSqlQueryModel>
#include <QSqlQuery> #include <QtSql/QSqlQuery>
#ifdef NUT_SHARED_POINTER
#include <QtCore/QSharedPointer>
#endif
#include "table.h" #include "table.h"
#include "query_p.h" #include "query_p.h"
@ -40,6 +44,7 @@
#include "querybase_p.h" #include "querybase_p.h"
#include "phrase.h" #include "phrase.h"
#include "tablemodel.h" #include "tablemodel.h"
#include "sqlmodel.h"
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
@ -52,6 +57,14 @@ 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();
@ -76,8 +89,8 @@ public:
Query<T> *setWhere(const ConditionalPhrase &ph); Query<T> *setWhere(const ConditionalPhrase &ph);
//data selecting //data selecting
T *first(); typename TableType<T>::Row first();
QList<T*> toList(int count = -1); typename TableType<T>::RowList toList(int count = -1);
template <typename F> template <typename F>
QList<F> select(const FieldPhrase<F> f); QList<F> select(const FieldPhrase<F> f);
@ -98,6 +111,7 @@ public:
QSqlQueryModel *toModel(); QSqlQueryModel *toModel();
void toModel(QSqlQueryModel *model); void toModel(QSqlQueryModel *model);
void toModel(SqlModel *model);
//debug purpose //debug purpose
QString sqlCommand() const; QString sqlCommand() const;
@ -161,11 +175,11 @@ Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
} }
template <class T> template <class T>
Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count) Q_OUTOFLINE_TEMPLATE typename TableType<T>::RowList Query<T>::toList(int count)
{ {
Q_UNUSED(count); Q_UNUSED(count);
Q_D(Query); Q_D(Query);
QList<T*> returnList; typename TableType<T>::RowList returnList;
d->select = "*"; d->select = "*";
d->sql = d->database->sqlGenertor()->selectCommand( d->sql = d->database->sqlGenertor()->selectCommand(
@ -285,13 +299,17 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
Table *table; Table *table;
if (data.table->className() == d->className) { if (data.table->className() == d->className) {
table = new T(); table = new T();
table->setParentTableSet(d->tableSet); #ifdef NUT_SHARED_POINTER
auto shp = QSharedPointer<T>(qobject_cast<T*>(table));
returnList.append(shp);
#else
returnList.append(dynamic_cast<T*>(table)); returnList.append(dynamic_cast<T*>(table));
#endif
table->setParentTableSet(d->tableSet);
} else { } else {
const QMetaObject *childMetaObject const QMetaObject *childMetaObject
= QMetaType::metaObjectForType(data.table->typeId()); = QMetaType::metaObjectForType(data.table->typeId());
table = qobject_cast<Table *>(childMetaObject->newInstance()); table = qobject_cast<Table *>(childMetaObject->newInstance());
if (!table) if (!table)
qFatal("Could not create instance of %s", qFatal("Could not create instance of %s",
qPrintable(data.table->name())); qPrintable(data.table->name()));
@ -323,8 +341,11 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
data.lastRow = table; data.lastRow = table;
} //while } //while
} // while } // while
#ifndef NUT_SHARED_POINTER
if (m_autoDelete) if (m_autoDelete)
deleteLater(); deleteLater();
#endif
return returnList; return returnList;
} }
@ -357,11 +378,11 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
} }
template <class T> template <class T>
Q_OUTOFLINE_TEMPLATE T *Query<T>::first() Q_OUTOFLINE_TEMPLATE typename TableType<T>::Row Query<T>::first()
{ {
skip(0); skip(0);
take(1); take(1);
QList<T*> list = toList(1); typename TableType<T>::RowList list = toList(1);
if (list.count()) if (list.count())
return list.first(); return list.first();
@ -617,6 +638,43 @@ Q_OUTOFLINE_TEMPLATE void Query<T>::toModel(QSqlQueryModel *model)
} }
} }
template<class T>
Q_OUTOFLINE_TEMPLATE void Query<T>::toModel(SqlModel *model)
{
Q_D(Query);
d->sql = d->database->sqlGenertor()->selectCommand(
d->tableName,
d->fieldPhrase,
d->wherePhrase, d->orderPhrase, d->relations,
d->skip, d->take);
model->setTable(toList());
/*
DatabaseModel dbModel = d->database->model();
model->setQuery(d->sql, d->database->database());
int fieldIndex = 0;
if (d->fieldPhrase.data.count()) {
foreach (const PhraseData *pd, d->fieldPhrase.data) {
QString displayName = dbModel.tableByClassName(pd->className)
->field(pd->fieldName)->displayName;
model->setHeaderData(fieldIndex++,
Qt::Horizontal,
displayName);
}
} else {
TableModel *tbl = d->database->model().tableByName(d->tableName);
foreach (FieldModel *f, tbl->fields()) {
model->setHeaderData(fieldIndex++,
Qt::Horizontal,
f->displayName);
}
}*/
}
template <class T> template <class T>
Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const
{ {

View File

@ -23,8 +23,9 @@
#include "phrase.h" #include "phrase.h"
#include <QList> #include <QtCore/QList>
#include <QString> #include <QtCore/QString>
#include <QtCore/QSharedData>
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
@ -32,7 +33,7 @@ class Database;
class TableSetBase; class TableSetBase;
class QueryBase; class QueryBase;
struct RelationModel; struct RelationModel;
class QueryPrivate{ class QueryPrivate : public QSharedData {
QueryBase *q_ptr; QueryBase *q_ptr;
Q_DECLARE_PUBLIC(QueryBase) Q_DECLARE_PUBLIC(QueryBase)

View File

@ -23,7 +23,10 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#include <QtCore/QExplicitlySharedDataPointer>
#include "defines.h" #include "defines.h"
#include "query_p.h"
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
@ -33,6 +36,10 @@ class TableSetBase;
class QueryBase : public QObject class QueryBase : public QObject
{ {
Q_OBJECT Q_OBJECT
protected:
QExplicitlySharedDataPointer<QueryPrivate> d;
public: public:
explicit QueryBase(QObject *parent = 0); explicit QueryBase(QObject *parent = 0);

View File

@ -25,17 +25,29 @@
#include "table.h" #include "table.h"
#include "sqlmodel_p.h" #include "sqlmodel_p.h"
#include "sqlmodel.h" #include "sqlmodel.h"
#include "query.h"
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
//SqlModel::SqlModel(Query *q) : QAbstractItemModel(q.)
//{
//}
void SqlModel::setRenderer(const std::function<QVariant (int, QVariant)> &renderer)
{
_renderer = renderer;
}
SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) : SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) :
QAbstractTableModel(parent) QAbstractTableModel(parent), d_ptr(new SqlModelPrivate(this)), _renderer(nullptr)
{ {
Q_D(SqlModel); 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();
// setQuery("SELECT * FROM " + d->tableName, database->databaseName()); // setQuery("SELECT * FROM " + d->tableName, database->databaseName());
} }
@ -63,8 +75,12 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const
return QVariant("-"); return QVariant("-");
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
Table *t = d->rows.at(index.row()); TableType<Table>::Row t = d->rows.at(index.row());
return t->property(d->model->field(index.column())->name.toLocal8Bit().data()); QVariant v = t->property(d->model->field(index.column())->name.toLocal8Bit().data());
// emit beforeShowText(index.column(), v);
if (_renderer != nullptr)
v = _renderer(index.column(), v);
return v;
// LogData *d = dataList.at(index.row()); // LogData *d = dataList.at(index.row());
// switch (index.column()) { // switch (index.column()) {
@ -86,4 +102,49 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const
return QVariant(); return QVariant();
} }
void SqlModel::setRows(TableType<Table>::RowList rows)
{
Q_D(SqlModel);
beginRemoveRows(QModelIndex(), 0, d->rows.count());
d->rows.clear();
endRemoveRows();
beginInsertRows(QModelIndex(), 0, rows.count());
d->rows = rows;
endInsertRows();
}
void SqlModel::append(TableType<Table>::Row table)
{
Q_D(SqlModel);
beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count());
d->rows.append(table);
endInsertRows();
}
//void SqlModel::append(Table *table)
//{
// append(TableType<Table>::Row(table));
//}
QVariant SqlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
Q_D(const SqlModel);
return d->model->field(section)->displayName;
}
return QAbstractItemModel::headerData(section, orientation, role);
}
Table *SqlModel::at(const int &i) const
{
Q_D(const SqlModel);
return d->rows.at(i).data();
}
SqlModelPrivate::SqlModelPrivate(SqlModel *parent) : q_ptr(parent)
{
}
NUT_END_NAMESPACE NUT_END_NAMESPACE

View File

@ -23,6 +23,8 @@
#include <QtCore/QAbstractTableModel> #include <QtCore/QAbstractTableModel>
#include "defines.h" #include "defines.h"
#include <QList>
#include <functional>
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
@ -31,22 +33,52 @@ class TableSetBase;
class SqlModelPrivate; class SqlModelPrivate;
class Table; class Table;
class TableModel; class TableModel;
class SqlModel : public QAbstractTableModel
class NUT_EXPORT SqlModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
std::function <QVariant(int, QVariant)> _renderer;
public: public:
// explicit SqlModel(Query *q);
explicit SqlModel(Database *database, TableSetBase *tableSet, QObject *parent = Q_NULLPTR); explicit SqlModel(Database *database, TableSetBase *tableSet, QObject *parent = Q_NULLPTR);
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
template<class T>
void setTable(QList<QSharedPointer<T>> rows);
void setRows(TableType<Table>::RowList rows);
void append(TableType<Table>::Row table);
// void append(Table *table);
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
Table *at(const int &i) const;
void setRenderer(const std::function<QVariant (int, QVariant)> &renderer);
private: private:
SqlModelPrivate *d_ptr; SqlModelPrivate *d_ptr;
Q_DECLARE_PRIVATE(SqlModel) Q_DECLARE_PRIVATE(SqlModel)
signals:
void beforeShowText(int col, QVariant &value);
}; };
template<class T>
Q_OUTOFLINE_TEMPLATE void SqlModel::setTable(QList<QSharedPointer<T> > rows)
{
Q_D(SqlModel);
TableType<Table>::RowList tab;
foreach (auto t, rows)
tab.append(t);
setRows(tab);
}
NUT_END_NAMESPACE NUT_END_NAMESPACE
#endif // SQLMODEL_H #endif // SQLMODEL_H

View File

@ -1,6 +1,7 @@
#ifndef SQLMODEL_P_H #ifndef SQLMODEL_P_H
#define SQLMODEL_P_H #define SQLMODEL_P_H
#include <QSharedPointer>
#include <QString> #include <QString>
#include "defines.h" #include "defines.h"
@ -13,11 +14,11 @@ class SqlModelPrivate {
SqlModel *q_ptr; SqlModel *q_ptr;
Q_DECLARE_PUBLIC(SqlModel) Q_DECLARE_PUBLIC(SqlModel)
public: public:
explicit SqlModelPrivate() = default; explicit SqlModelPrivate(SqlModel *parent);
QString tableName; QString tableName;
QList<Table*> rows; TableType<Table>::RowList rows;
TableModel *model; TableModel *model;
}; };