diff --git a/nut.pri b/nut.pri index 4932d79..169ecea 100644 --- a/nut.pri +++ b/nut.pri @@ -23,7 +23,9 @@ HEADERS += \ $$PWD/src/table.h \ $$PWD/src/database.h \ $$PWD/src/database_p.h \ - $$PWD/src/serializableobject.h + $$PWD/src/serializableobject.h \ + $$PWD/src/sqlmodel.h \ + $$PWD/src/sqlmodel_p.h SOURCES += \ $$PWD/src/generators/sqlgeneratorbase.cpp \ @@ -42,4 +44,5 @@ SOURCES += \ $$PWD/src/wherephrase.cpp \ $$PWD/src/table.cpp \ $$PWD/src/database.cpp \ - $$PWD/src/serializableobject.cpp + $$PWD/src/serializableobject.cpp \ + $$PWD/src/sqlmodel.cpp diff --git a/src/database.cpp b/src/database.cpp index 9ffdf45..948c06d 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -495,6 +495,12 @@ SqlGeneratorBase *Database::sqlGenertor() const return d->sqlGenertor; } +QSqlDatabase Database::database() +{ + Q_D(Database); + return d->db; +} + void Database::databaseUpdated(QString oldVersion, QString newVersion) { Q_UNUSED(oldVersion); diff --git a/src/database.h b/src/database.h index 455d491..9dbc08e 100644 --- a/src/database.h +++ b/src/database.h @@ -69,6 +69,7 @@ public: QString tableName(QString className); SqlGeneratorBase *sqlGenertor() const; + QSqlDatabase database(); protected: //remove minor version diff --git a/src/database_p.h b/src/database_p.h index 8265273..7c8709d 100644 --- a/src/database_p.h +++ b/src/database_p.h @@ -63,7 +63,7 @@ public: TableSet *changeLogs; QT_DEPRECATED - QHash tables; + QMap tables; static QMap allTableMaps; static qulonglong lastId; diff --git a/src/defines.h b/src/defines.h index b8f4f89..c36ca4e 100644 --- a/src/defines.h +++ b/src/defines.h @@ -100,6 +100,7 @@ public: \ #define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0) #define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \ NUT_AUTO_INCREMENT(x) +#define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name) #define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0) #define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len) #define NUT_DEFAULT_VALUE(x, n) NUT_INFO(__nut_DEFAULT_VALUE, x, n) diff --git a/src/defines_p.h b/src/defines_p.h index 4c9bea2..daab8be 100644 --- a/src/defines_p.h +++ b/src/defines_p.h @@ -34,6 +34,7 @@ #define __nut_TABLE "table" #define __nut_TABLE_NAME "table_name" +#define __nut_DISPLAY "display" #define __nut_LEN "len" #define __nut_DEFAULT_VALUE "def" #define __nut_NOT_NULL "notnull" diff --git a/src/generators/sqlgeneratorbase_p.h b/src/generators/sqlgeneratorbase_p.h index 011f6b1..74e2d6c 100644 --- a/src/generators/sqlgeneratorbase_p.h +++ b/src/generators/sqlgeneratorbase_p.h @@ -100,11 +100,11 @@ public: virtual QString phraseUpdate(const PhraseData *d) const; virtual QString operatorString(const PhraseData::Condition &cond) const; + QString phraseOrder(const PhraseData *d) const; private: QString agregateText(const AgregateType &t, const QString &arg = QString::null) const; QString fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const; QString createWhere(QList &wheres); - QString phraseOrder(const PhraseData *d) const; void replaceTableNames(QString &command); void removeTableNames(QString &command); }; diff --git a/src/query.h b/src/query.h index 1fb4796..490b16e 100644 --- a/src/query.h +++ b/src/query.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "query_p.h" #include "database.h" @@ -68,6 +69,7 @@ public: // Query *orderBy(QString fieldName, QString type); Query *skip(int n); Query *take(int n); + Query *fields(WherePhrase phrase); Query *orderBy(WherePhrase phrase); Query *include(TableSetBase *t); Query *include(Table *t); @@ -86,6 +88,8 @@ public: int update(WherePhrase phrase); int remove(); + QSqlQueryModel *toModal(); + //debug purpose QString sqlCommand() const; }; @@ -251,6 +255,9 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) = QMetaType::metaObjectForType(data.table->typeId()); table = qobject_cast(childMetaObject->newInstance()); + if (!table) + qFatal("Could not create instance of %s", + qPrintable(data.table->name())); } QStringList childFields = data.table->fieldsNames(); @@ -446,6 +453,14 @@ Q_OUTOFLINE_TEMPLATE Query *Query::take(int n) return this; } +template +Q_OUTOFLINE_TEMPLATE Query *Query::fields(WherePhrase phrase) +{ + Q_D(Query); + d->fields.append(phrase); + return this; +} + //template //Q_OUTOFLINE_TEMPLATE Query *Query::orderBy(QString fieldName, // QString type) @@ -504,6 +519,37 @@ Q_OUTOFLINE_TEMPLATE int Query::remove() return q.numRowsAffected(); } +template +Q_OUTOFLINE_TEMPLATE QSqlQueryModel *Query::toModal() +{ + Q_D(Query); + + + QString fff = ""; + + foreach (WherePhrase p, d->fields) { + if (fff != "") + fff.append(", "); + fff.append(d->database->sqlGenertor()->phraseOrder(p.data())); + } + + d->sql = d->database->sqlGenertor()->selectCommand( + SqlGeneratorBase::SelectAll, "", + d->tableName, + d->wheres, d->orderPhrases, d->relations, + d->skip, d->take); + qDebug()<<"Model to" << fff; + QSqlQueryModel *model = new QSqlQueryModel; + TableModel *tableModel = d->database->model().tableByName(d->tableName); + model->setQuery(d->sql, d->database->database()); + + int fieldIndex = 0; + foreach (FieldModel *f, tableModel->fields()) { + model->setHeaderData(fieldIndex++, Qt::Horizontal, f->displayName); + } + return model; +} + template Q_OUTOFLINE_TEMPLATE QString Query::sqlCommand() const { diff --git a/src/query_p.h b/src/query_p.h index 4b48859..bcf4180 100644 --- a/src/query_p.h +++ b/src/query_p.h @@ -50,6 +50,7 @@ public: QList relations; QList wheres; QList orderPhrases; + QList fields; QHash orders; int skip; int take; diff --git a/src/sqlmodel.cpp b/src/sqlmodel.cpp new file mode 100644 index 0000000..ff40fa3 --- /dev/null +++ b/src/sqlmodel.cpp @@ -0,0 +1,88 @@ +/************************************************************************** +** +** This file is part of Nut project. +** https://github.com/HamedMasafi/Nut +** +** Nut is free software: you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** Nut is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with Nut. If not, see . +** +**************************************************************************/ + +#include "database.h" +#include "tablesetbase_p.h" +#include "databasemodel.h" + +#include "sqlmodel_p.h" +#include "sqlmodel.h" + +NUT_BEGIN_NAMESPACE + +SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) : + QAbstractTableModel(parent) +{ + Q_D(SqlModel); + d->model = database->model() + .tableByClassName(tableSet->childClassName()); + d->tableName = d->model->name(); + + // setQuery("SELECT * FROM " + d->tableName, database->databaseName()); +} + +int SqlModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + Q_D(const SqlModel); + return d->rows.count(); +} + +int SqlModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + Q_D(const SqlModel); + return d->model->fields().count(); +} + +QVariant SqlModel::data(const QModelIndex &index, int role) const +{ + Q_D(const SqlModel); + if (!index.isValid()) + return QVariant(); + + if (index.row() >= d->rows.count() || index.row() < 0) + return QVariant("-"); + + if (role == Qt::DisplayRole) { + Table *t = d->rows.at(index.row()); + return t->property(d->model->field(index.column())->name.toLocal8Bit().data()); +// LogData *d = dataList.at(index.row()); + +// switch (index.column()) { +// case COL_ID: +// return index.row() + 1; +// case COL_Type: { +// return typeText(d->type); +// } +// case COL_TITLE: +// return d->title; +// case COL_File: +// return d->file; +// case COL_Function: +// return d->function; +// case COL_Line: +// return d->line; +// } + } + return QVariant(); +} + +NUT_END_NAMESPACE diff --git a/src/sqlmodel.h b/src/sqlmodel.h new file mode 100644 index 0000000..e7c5e60 --- /dev/null +++ b/src/sqlmodel.h @@ -0,0 +1,52 @@ +/************************************************************************** +** +** This file is part of Nut project. +** https://github.com/HamedMasafi/Nut +** +** Nut is free software: you can redistribute it and/or modify +** it under the terms of the GNU Lesser General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** Nut is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public License +** along with Nut. If not, see . +** +**************************************************************************/ + +#ifndef SQLMODEL_H +#define SQLMODEL_H + +#include +#include "defines.h" + +NUT_BEGIN_NAMESPACE + +class Database; +class TableSetBase; +class SqlModelPrivate; +class Table; +class TableModel; +class SqlModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + 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; + +private: + SqlModelPrivate *d_ptr; + Q_DECLARE_PRIVATE(SqlModel) +}; + +NUT_END_NAMESPACE + +#endif // SQLMODEL_H diff --git a/src/sqlmodel_p.h b/src/sqlmodel_p.h new file mode 100644 index 0000000..edd8dc8 --- /dev/null +++ b/src/sqlmodel_p.h @@ -0,0 +1,24 @@ +#ifndef SQLMODEL_P_H +#define SQLMODEL_P_H + +#include +#include "defines.h" + +NUT_BEGIN_NAMESPACE + +class SqlModel; +class Table; +class TableModel; +class SqlModelPrivate { + SqlModel *q_ptr; + Q_DECLARE_PUBLIC(SqlModel) +public: + QString tableName; + + QList rows; + TableModel *model; +}; + +NUT_END_NAMESPACE + +#endif // SQLMODEL_P_H diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 8c272a2..cf69778 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -66,6 +66,14 @@ void TableModel::setTypeId(const int &typeId) _typeId = typeId; } +FieldModel *TableModel::field(int n) const +{ + if (n < 0 || n >= _fields.count()) + return 0; + + return _fields.at(n); +} + FieldModel *TableModel::field(QString name) const { foreach (FieldModel *f, _fields) @@ -195,7 +203,7 @@ TableModel::TableModel(int typeId, QString tableName) if(type == __nut_FIELD){ FieldModel *f = new FieldModel; - f->name = name; + f->name = f->displayName = name; _fields.append(f); } } @@ -255,8 +263,8 @@ TableModel::TableModel(int typeId, QString tableName) f->isAutoIncrement = true; else if(type == __nut_UNIQUE) f->isUnique = true; - - + else if(type == __nut_DISPLAY) + f->displayName = value; } if(!findByTypeId(typeId) && !tableName.isNull()) diff --git a/src/tablemodel.h b/src/tablemodel.h index 42615e5..e089ed2 100644 --- a/src/tablemodel.h +++ b/src/tablemodel.h @@ -47,6 +47,7 @@ struct FieldModel{ bool isPrimaryKey; bool isAutoIncrement; bool isUnique; + QString displayName; bool operator ==(const FieldModel &f) const{ @@ -88,6 +89,7 @@ public: // static void createForegionKeys(); // static TableModel* model(QString className); + FieldModel *field(int n) const; FieldModel *field(QString name) const; RelationModel *foregionKey(QString otherTable) const; diff --git a/src/tableset.cpp b/src/tableset.cpp index 3bcce89..5621217 100644 --- a/src/tableset.cpp +++ b/src/tableset.cpp @@ -19,3 +19,4 @@ **************************************************************************/ #include "tableset.h" + diff --git a/src/tableset.h b/src/tableset.h index 1cb4762..d62fe73 100644 --- a/src/tableset.h +++ b/src/tableset.h @@ -28,7 +28,6 @@ #include #include "tablesetbase_p.h" -//#include "database.h" #include "table.h" NUT_BEGIN_NAMESPACE