initial
This commit is contained in:
parent
a9bcc76663
commit
e946a54753
|
|
@ -1,5 +1,7 @@
|
|||
|
||||
#include "../src/table.h"
|
||||
#include "../src/database.h"
|
||||
#include "../src/sqlmodel.h"
|
||||
#include "../src/tableset.h"
|
||||
#include "../src/tablemodel.h"
|
||||
#include "../src/query.h"
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
#include "../src/sqlmodel.h"
|
||||
|
|
@ -0,0 +1 @@
|
|||
#include "../src/tablemodel.h"
|
||||
|
|
@ -3,11 +3,10 @@
|
|||
src_dir="src"
|
||||
namespace_name="nut"
|
||||
|
||||
ns=$(echo $namespace_name|awk '{print tolower($0)}')
|
||||
#ns=$(echo $namespace_name|awk '{print tolower($0)}')
|
||||
Ns="Nut"
|
||||
NS=$(echo $namespace_name|awk '{print toupper($0)}')
|
||||
echo $NS
|
||||
exit
|
||||
|
||||
|
||||
create_sub_folder=true
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
#include "../src/sqlmodel.h"
|
||||
|
|
@ -0,0 +1 @@
|
|||
#include "../src/tablemodel.h"
|
||||
|
|
@ -278,7 +278,7 @@ bool DatabasePrivate::getCurrectScheema()
|
|||
|
||||
DatabaseModel DatabasePrivate::getLastScheema()
|
||||
{
|
||||
ChangeLogTable *u = changeLogs->query()
|
||||
typename TableType<ChangeLogTable>::Row u = changeLogs->query()
|
||||
->orderBy(!ChangeLogTable::idField())
|
||||
->first();
|
||||
|
||||
|
|
|
|||
|
|
@ -192,4 +192,32 @@ public: \
|
|||
#define NUT_NOT_NULL(x) NUT_INFO(__nut_NOT_NULL, x, 1)
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -834,6 +834,9 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v) const
|
|||
return QString();
|
||||
}
|
||||
|
||||
if (v.type() == QVariant::List)
|
||||
return serialized;
|
||||
|
||||
return "'" + serialized + "'";
|
||||
}
|
||||
|
||||
|
|
|
|||
76
src/query.h
76
src/query.h
|
|
@ -29,7 +29,11 @@
|
|||
#include <QtSql/QSqlResult>
|
||||
#include <QtSql/QSqlError>
|
||||
#include <QtSql/QSqlQueryModel>
|
||||
#include <QSqlQuery>
|
||||
#include <QtSql/QSqlQuery>
|
||||
|
||||
#ifdef NUT_SHARED_POINTER
|
||||
#include <QtCore/QSharedPointer>
|
||||
#endif
|
||||
|
||||
#include "table.h"
|
||||
#include "query_p.h"
|
||||
|
|
@ -40,6 +44,7 @@
|
|||
#include "querybase_p.h"
|
||||
#include "phrase.h"
|
||||
#include "tablemodel.h"
|
||||
#include "sqlmodel.h"
|
||||
|
||||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
|
|
@ -52,6 +57,14 @@ template <class T>
|
|||
bool m_autoDelete;
|
||||
|
||||
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);
|
||||
~Query();
|
||||
|
||||
|
|
@ -76,8 +89,8 @@ public:
|
|||
Query<T> *setWhere(const ConditionalPhrase &ph);
|
||||
|
||||
//data selecting
|
||||
T *first();
|
||||
QList<T*> toList(int count = -1);
|
||||
typename TableType<T>::Row first();
|
||||
typename TableType<T>::RowList toList(int count = -1);
|
||||
template <typename F>
|
||||
QList<F> select(const FieldPhrase<F> f);
|
||||
|
||||
|
|
@ -98,6 +111,7 @@ public:
|
|||
|
||||
QSqlQueryModel *toModel();
|
||||
void toModel(QSqlQueryModel *model);
|
||||
void toModel(SqlModel *model);
|
||||
|
||||
//debug purpose
|
||||
QString sqlCommand() const;
|
||||
|
|
@ -161,11 +175,11 @@ Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
|||
}
|
||||
|
||||
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_D(Query);
|
||||
QList<T*> returnList;
|
||||
typename TableType<T>::RowList returnList;
|
||||
d->select = "*";
|
||||
|
||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||
|
|
@ -285,13 +299,17 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
Table *table;
|
||||
if (data.table->className() == d->className) {
|
||||
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));
|
||||
#endif
|
||||
table->setParentTableSet(d->tableSet);
|
||||
} else {
|
||||
const QMetaObject *childMetaObject
|
||||
= QMetaType::metaObjectForType(data.table->typeId());
|
||||
table = qobject_cast<Table *>(childMetaObject->newInstance());
|
||||
|
||||
if (!table)
|
||||
qFatal("Could not create instance of %s",
|
||||
qPrintable(data.table->name()));
|
||||
|
|
@ -323,8 +341,11 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
|||
data.lastRow = table;
|
||||
} //while
|
||||
} // while
|
||||
|
||||
#ifndef NUT_SHARED_POINTER
|
||||
if (m_autoDelete)
|
||||
deleteLater();
|
||||
#endif
|
||||
|
||||
return returnList;
|
||||
}
|
||||
|
|
@ -357,11 +378,11 @@ Q_OUTOFLINE_TEMPLATE QList<F> Query<T>::select(const FieldPhrase<F> f)
|
|||
}
|
||||
|
||||
template <class T>
|
||||
Q_OUTOFLINE_TEMPLATE T *Query<T>::first()
|
||||
Q_OUTOFLINE_TEMPLATE typename TableType<T>::Row Query<T>::first()
|
||||
{
|
||||
skip(0);
|
||||
take(1);
|
||||
QList<T*> list = toList(1);
|
||||
typename TableType<T>::RowList list = toList(1);
|
||||
|
||||
if (list.count())
|
||||
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>
|
||||
Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const
|
||||
{
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@
|
|||
|
||||
#include "phrase.h"
|
||||
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <QtCore/QList>
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QSharedData>
|
||||
|
||||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
|
|
@ -32,7 +33,7 @@ class Database;
|
|||
class TableSetBase;
|
||||
class QueryBase;
|
||||
struct RelationModel;
|
||||
class QueryPrivate{
|
||||
class QueryPrivate : public QSharedData {
|
||||
QueryBase *q_ptr;
|
||||
Q_DECLARE_PUBLIC(QueryBase)
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,10 @@
|
|||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <QtCore/QExplicitlySharedDataPointer>
|
||||
|
||||
#include "defines.h"
|
||||
#include "query_p.h"
|
||||
|
||||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
|
|
@ -33,6 +36,10 @@ class TableSetBase;
|
|||
class QueryBase : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
QExplicitlySharedDataPointer<QueryPrivate> d;
|
||||
|
||||
public:
|
||||
explicit QueryBase(QObject *parent = 0);
|
||||
|
||||
|
|
|
|||
|
|
@ -25,17 +25,29 @@
|
|||
#include "table.h"
|
||||
#include "sqlmodel_p.h"
|
||||
#include "sqlmodel.h"
|
||||
#include "query.h"
|
||||
|
||||
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) :
|
||||
QAbstractTableModel(parent)
|
||||
QAbstractTableModel(parent), d_ptr(new SqlModelPrivate(this)), _renderer(nullptr)
|
||||
{
|
||||
Q_D(SqlModel);
|
||||
d->model = database->model()
|
||||
.tableByClassName(tableSet->childClassName());
|
||||
d->tableName = d->model->name();
|
||||
|
||||
|
||||
// setQuery("SELECT * FROM " + d->tableName, database->databaseName());
|
||||
}
|
||||
|
||||
|
|
@ -63,8 +75,12 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const
|
|||
return QVariant("-");
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
Table *t = d->rows.at(index.row());
|
||||
return t->property(d->model->field(index.column())->name.toLocal8Bit().data());
|
||||
TableType<Table>::Row t = d->rows.at(index.row());
|
||||
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());
|
||||
|
||||
// switch (index.column()) {
|
||||
|
|
@ -86,4 +102,49 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const
|
|||
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
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
#include <QtCore/QAbstractTableModel>
|
||||
#include "defines.h"
|
||||
#include <QList>
|
||||
#include <functional>
|
||||
|
||||
NUT_BEGIN_NAMESPACE
|
||||
|
||||
|
|
@ -31,22 +33,52 @@ class TableSetBase;
|
|||
class SqlModelPrivate;
|
||||
class Table;
|
||||
class TableModel;
|
||||
class SqlModel : public QAbstractTableModel
|
||||
|
||||
class NUT_EXPORT SqlModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
std::function <QVariant(int, QVariant)> _renderer;
|
||||
|
||||
public:
|
||||
// explicit SqlModel(Query *q);
|
||||
explicit SqlModel(Database *database, TableSetBase *tableSet, QObject *parent = Q_NULLPTR);
|
||||
|
||||
int rowCount(const QModelIndex &parent) const;
|
||||
int columnCount(const QModelIndex &parent) 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:
|
||||
SqlModelPrivate *d_ptr;
|
||||
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
|
||||
|
||||
#endif // SQLMODEL_H
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef SQLMODEL_P_H
|
||||
#define SQLMODEL_P_H
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
#include "defines.h"
|
||||
|
||||
|
|
@ -13,11 +14,11 @@ class SqlModelPrivate {
|
|||
SqlModel *q_ptr;
|
||||
Q_DECLARE_PUBLIC(SqlModel)
|
||||
public:
|
||||
explicit SqlModelPrivate() = default;
|
||||
explicit SqlModelPrivate(SqlModel *parent);
|
||||
|
||||
QString tableName;
|
||||
|
||||
QList<Table*> rows;
|
||||
TableType<Table>::RowList rows;
|
||||
TableModel *model;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue