remove back pointer from template :-|

This commit is contained in:
Hamed Masafi 2018-01-09 13:03:54 +03:30
commit 403d24ae23
13 changed files with 93 additions and 72 deletions

View File

@ -184,7 +184,7 @@ bool DatabasePrivate::getCurrectScheema()
tables.insert(ChangeLogTable::staticMetaObject.className(), tables.insert(ChangeLogTable::staticMetaObject.className(),
__CHANGE_LOG_TABLE_NAME); __CHANGE_LOG_TABLE_NAME);
changeLogs = new TableSet<ChangeLogTable*>(q); changeLogs = new TableSet<ChangeLogTable>(q);
for (int i = 0; i < q->metaObject()->classInfoCount(); i++) { for (int i = 0; i < q->metaObject()->classInfoCount(); i++) {
QMetaClassInfo ci = q->metaObject()->classInfo(i); QMetaClassInfo ci = q->metaObject()->classInfo(i);

View File

@ -58,7 +58,7 @@ public:
SqlGeneratorBase *sqlGenertor; SqlGeneratorBase *sqlGenertor;
DatabaseModel currentModel; DatabaseModel currentModel;
TableSet<ChangeLogTable*> *changeLogs; TableSet<ChangeLogTable> *changeLogs;
QT_DEPRECATED QT_DEPRECATED
QHash<QString, QString> tables; QHash<QString, QString> tables;

View File

@ -40,13 +40,13 @@
#define NUT_DECLARE_TABLE(type, name) \ #define NUT_DECLARE_TABLE(type, name) \
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #name) \ Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #name) \
Q_PROPERTY(type* name READ name) \ Q_PROPERTY(type* name READ name) \
Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type*>) name##s READ name##s) \ Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet<type>) name##s READ name##s) \
type* m_##name; \ type* m_##name; \
NUT_WRAP_NAMESPACE(TableSet<type*>) *m_##name##s; \ NUT_WRAP_NAMESPACE(TableSet<type>) *m_##name##s; \
public: \ public: \
static const type _##name; \ static const type _##name; \
type* name() const{ return m_##name; } \ type* name() const{ return m_##name; } \
NUT_WRAP_NAMESPACE(TableSet<type*>) *name##s() const { return m_##name##s; } NUT_WRAP_NAMESPACE(TableSet<type>) *name##s() const { return m_##name##s; }
//Table //Table
#define NUT_DECLARE_FIELD(type, name, read, write) \ #define NUT_DECLARE_FIELD(type, name, read, write) \
@ -79,30 +79,35 @@ public: \
#define NUT_DECLARE_CHILD_TABLE(type, n) \ #define NUT_DECLARE_CHILD_TABLE(type, n) \
private: \ private: \
NUT_WRAP_NAMESPACE(TableSet)<type*> *m_##n; \ NUT_WRAP_NAMESPACE(TableSet)<type> *m_##n; \
public: \ public: \
NUT_WRAP_NAMESPACE(TableSet)<type*> *n(){ \ static type *n##Table(); \
return m_##n; \ NUT_WRAP_NAMESPACE(TableSet)<type> *n();
}
//static type *n##Table(){ \ #define NUT_IMPLEMENT_CHILD_TABLE(class, type, n) \
// /*static type *f = new type();*/ \ type *class::n##Table(){ \
// /*return f;*/ \ static type *f = new type(); \
//} \ return f; \
} \
NUT_WRAP_NAMESPACE(TableSet)<type> *class::n(){ \
return m_##n; \
}
#define NUT_INDEX(name, field, order)
#define NUT_PRIMARY_KEY(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_PRIMARY_KEY), #x) #define NUT_PRIMARY_KEY(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_PRIMARY_KEY), #x)
#define NUT_AUTO_INCREMENT(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_AUTO_INCREMENT), #x) #define NUT_AUTO_INCREMENT(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_AUTO_INCREMENT), #x)
#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \ #define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \
NUT_AUTO_INCREMENT(x) NUT_AUTO_INCREMENT(x)
#define NUT_UNIQUE(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_UNIQUE), #x) #define NUT_UNIQUE(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_UNIQUE), #x)
#define NUT_LEN(field, len) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #field " " __nut_LEN), #len) #define NUT_LEN(field, len) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #field " " __nut_LEN), #len)
#define NUT_DEFAULT_VALUE(x, n) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_DEFAULT_VALUE), #n) #define NUT_DEFAULT_VALUE(x, n) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_DEFAULT_VALUE), #n)
#define NUT_NOT_NULL(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_NOT_NULL), "1") #define NUT_NOT_NULL(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_NOT_NULL), "1")
#define NUT_INDEX(name, field, order)
#ifndef NUT_NO_KEYWORDS #ifndef NUT_NO_KEYWORDS
# define FROM(x) (x->query()) # define FROM(x) (x->query())
# define WHERE(x) ->setWhere(x) # define WHERE(x) ->setWhere(x)
# define JOIN(x) ->join(#x) # define JOIN(x) ->join<x>()
# define ORDERBY(x) ->orderBy(#x); # define ORDERBY(x) ->orderBy(#x);
# define ORDERBY_DESC(x) ->orderBy(!#x); # define ORDERBY_DESC(x) ->orderBy(!#x);

View File

@ -44,13 +44,13 @@
#define __nut_CHANGE "change" #define __nut_CHANGE "change"
#ifdef NUT_NAMESPACE #ifdef NUT_NAMESPACE
# define NUT_BEGIN_NAMESPACE namespace NUT_NAMESPACE{ # define NUT_BEGIN_NAMESPACE namespace NUT_NAMESPACE{
# define NUT_END_NAMESPACE } # define NUT_END_NAMESPACE }
# define NUT_WRAP_NAMESPACE(x) NUT_NAMESPACE::x # define NUT_WRAP_NAMESPACE(x) NUT_NAMESPACE::x
#else #else
# define NUT_BEGIN_NAMESPACE # define NUT_BEGIN_NAMESPACE
# define NUT_END_NAMESPACE # define NUT_END_NAMESPACE
# define NUT_WRAP_NAMESPACE(x) x # define NUT_WRAP_NAMESPACE(x) x
#endif #endif
#endif // DEFINES_P_H #endif // DEFINES_P_H

View File

@ -48,17 +48,13 @@ class NUT_EXPORT Query : public QueryBase
public: public:
Query(Database *database, TableSetBase *tableSet, bool autoDelete); Query(Database *database, TableSetBase *tableSet, bool autoDelete);
~Query(); ~Query();
Query<T> *join(const QString &tableName); //ddl
Query<T> *setWhere(WherePhrase where); Query<T> *setWhere(WherePhrase where);
Query<T> *join(Table *c) Query<T> *join(const QString &tableName);
{ Query<T> *join(Table *c);
join(c->metaObject()->className());
return this;
}
template<class TABLE> template<class TABLE>
Query<T> *join() Query<T> *join()
@ -68,24 +64,27 @@ public:
} }
// Query<T> *orderBy(QString fieldName, QString type); // Query<T> *orderBy(QString fieldName, QString type);
Query<T> *skip(int &n); Query<T> *skip(int n);
Query<T> *take(int &n); Query<T> *take(int n);
Query<T> *orderBy(WherePhrase phrase); Query<T> *orderBy(WherePhrase phrase);
Query<T> *include(TableSetBase *t); Query<T> *include(TableSetBase *t);
Query<T> *include(Table *t); Query<T> *include(Table *t);
//data selecting
T *first();
QList<T*> toList(int count = -1);
template <typename F>
QList<F> select(const FieldPhrase<F> f);
int count(); int count();
QVariant max(FieldPhrase<int> &f); QVariant max(FieldPhrase<int> &f);
QVariant min(FieldPhrase<int> &f); QVariant min(FieldPhrase<int> &f);
QVariant average(FieldPhrase<int> &f); QVariant average(FieldPhrase<int> &f);
T first();
QList<T> toList(int count = -1);
template <typename F>
QList<F> select(const FieldPhrase<F> f);
//data mailpulation
int update(WherePhrase phrase); int update(WherePhrase phrase);
int remove(); int remove();
//debug purpose
QString sqlCommand() const; QString sqlCommand() const;
}; };
@ -120,10 +119,10 @@ 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 QList<T *> Query<T>::toList(int count)
{ {
Q_D(Query); Q_D(Query);
QList<T> result; QList<T*> result;
d->select = "*"; d->select = "*";
// QSqlQuery q = // QSqlQuery q =
@ -138,7 +137,7 @@ Q_OUTOFLINE_TEMPLATE QList<T> Query<T>::toList(int count)
QString pk = d->database->model().tableByName(d->tableName)->primaryKey(); QString pk = d->database->model().tableByName(d->tableName)->primaryKey();
QVariant lastPkValue = QVariant(); QVariant lastPkValue = QVariant();
int childTypeId = 0; int childTypeId = 0;
T lastRow = 0; T *lastRow = 0;
TableSetBase *childTableSet = Q_NULLPTR; TableSetBase *childTableSet = Q_NULLPTR;
// FIXME: getting table error // FIXME: getting table error
@ -164,7 +163,7 @@ Q_OUTOFLINE_TEMPLATE QList<T> Query<T>::toList(int count)
while (q.next()) { while (q.next()) {
if (lastPkValue != q.value(pk)) { if (lastPkValue != q.value(pk)) {
T t = T();//new std::remove_pointer<T>::type(); T *t = new T();//new std::remove_pointer<T>::type();
foreach (QString field, masterFields) foreach (QString field, masterFields)
t->setProperty(field.toLatin1().data(), q.value(field)); t->setProperty(field.toLatin1().data(), q.value(field));
// for (int i = 0; i < t->metaObject()->propertyCount(); // for (int i = 0; i < t->metaObject()->propertyCount();
@ -244,9 +243,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 T *Query<T>::first()
{ {
QList<T> list = toList(1); skip(0);
take(1);
QList<T*> list = toList(1);
if (list.count()) if (list.count())
return list.first(); return list.first();
@ -322,6 +323,13 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &tableName)
return this; return this;
} }
template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(Table *c)
{
join(c->metaObject()->className());
return this;
}
template <class T> template <class T>
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where) Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
{ {
@ -331,7 +339,7 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::setWhere(WherePhrase where)
} }
template<class T> template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::skip(int &n) Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::skip(int n)
{ {
Q_D(Query); Q_D(Query);
d->skip = n; d->skip = n;
@ -339,7 +347,7 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::skip(int &n)
} }
template<class T> template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::take(int &n) Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::take(int n)
{ {
Q_D(Query); Q_D(Query);
d->take = n; d->take = n;

View File

@ -45,15 +45,15 @@ public:
TableSet(Database *parent); TableSet(Database *parent);
TableSet(Table *parent); TableSet(Table *parent);
void append(T t); void append(T *t);
void append(QList<T> t); void append(QList<T *> t);
void remove(T t); void remove(T *t);
void remove(QList<T> t); void remove(QList<T *> t);
inline T type() const {} inline T *type() const {}
int length() const; int length() const;
T at(int i) const; T *at(int i) const;
const T &operator[](int i) const; const T &operator[](int i) const;
Query<T> *query(); Query<T> *query();
Query<T> *query(bool autoDelete); Query<T> *query(bool autoDelete);
@ -96,9 +96,9 @@ Q_OUTOFLINE_TEMPLATE int TableSet<T>::length() const
} }
template<class T> template<class T>
Q_OUTOFLINE_TEMPLATE T TableSet<T>::at(int i) const Q_OUTOFLINE_TEMPLATE T *TableSet<T >::at(int i) const
{ {
return (T)_tablesList.at(i); return (T*)_tablesList.at(i);
} }
template<class T> template<class T>
@ -108,7 +108,7 @@ Q_OUTOFLINE_TEMPLATE const T &TableSet<T>::operator[](int i) const
} }
template<class T> template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(T t) Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(T *t)
{ {
_tables.insert(t); _tables.insert(t);
_tablesList.append(t); _tablesList.append(t);
@ -119,21 +119,21 @@ Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(T t)
} }
template<class T> template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(QList<T> t) Q_OUTOFLINE_TEMPLATE void TableSet<T>::append(QList<T *> t)
{ {
foreach (T* i, t) foreach (T* i, t)
append(i); append(i);
} }
template<class T> template<class T>
Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(T t) Q_OUTOFLINE_TEMPLATE void TableSet<T>::remove(T *t)
{ {
_tables.remove(t); _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(QList<T *> t)
{ {
foreach (T* i, t) foreach (T* i, t)
remove(i); remove(i);

View File

@ -10,9 +10,9 @@
#include "tablemodel.h" #include "tablemodel.h"
#include "databasemodel.h" #include "databasemodel.h"
#include "user.h"
#include "post.h" #include "post.h"
#include "comment.h" #include "comment.h"
#include "user.h"
MainTest::MainTest(QObject *parent) : QObject(parent) MainTest::MainTest(QObject *parent) : QObject(parent)
{ {
@ -95,13 +95,15 @@ void MainTest::createPost2()
void MainTest::selectPosts() void MainTest::selectPosts()
{ {
auto q = db.posts()->query(); auto q = db.posts()->query()
// q->join(Post::commentsTable()); // q->join(Post::commentsTable());
q->orderBy(!Post::saveDateField() & Post::bodyField()); // q->join(Post::commentsTable());
q->setWhere(Post::idField() == postId); // ->join<Comment>()
->orderBy(!Post::saveDateField() & Post::bodyField())
->setWhere(Post::idField() == postId);
auto posts = q->toList(); auto posts = q->toList();
qDebug() << "SQL="<<q->sqlCommand();
post = posts.at(0); post = posts.at(0);
post->setBody(""); post->setBody("");

View File

@ -12,6 +12,7 @@ class MainTest : public QObject
WeblogDatabase db; WeblogDatabase db;
int postId; int postId;
Post *post; Post *post;
public: public:
explicit MainTest(QObject *parent = 0); explicit MainTest(QObject *parent = 0);

View File

@ -3,7 +3,9 @@
#include "tableset.h" #include "tableset.h"
Post::Post(QObject *parent) : Table(parent), Post::Post(QObject *parent) : Table(parent),
m_comments(new TableSet<Comment*>(this)), m_id(0), m_title("") m_comments(new TableSet<Comment>(this)), m_id(0), m_title("")
{ {
} }
NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments)

View File

@ -1,10 +1,9 @@
#ifndef USER_H #ifndef POST_H
#define USER_H #define POST_H
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#include "table.h" #include "table.h"
#include "database.h" #include "database.h"
//#include "comment.h"
#include "databasemodel.h" #include "databasemodel.h"
#ifdef NUT_NAMESPACE #ifdef NUT_NAMESPACE
@ -39,4 +38,4 @@ public slots:
Q_DECLARE_METATYPE(Post*) Q_DECLARE_METATYPE(Post*)
#endif // USER_H #endif // POST_H

View File

@ -3,7 +3,9 @@
#include "comment.h" #include "comment.h"
User::User(QObject *tableSet) : Table(tableSet), User::User(QObject *tableSet) : Table(tableSet),
m_comments(new TableSet<Comment*>(this)) m_comments(new TableSet<Comment>(this))
{ {
} }
NUT_IMPLEMENT_CHILD_TABLE(User, Comment, comments)

View File

@ -1,17 +1,18 @@
#ifndef USER_H #ifndef USER_H
#define USER_H #define USER_H
#include <QtCore/QUuid>
#include "table.h" #include "table.h"
#include "tableset.h" #include "tableset.h"
//#include "comment.h"
#include <QtCore/QUuid>
#include <QtCore/QString>
#ifdef NUT_NAMESPACE #ifdef NUT_NAMESPACE
using namespace NUT_NAMESPACE; using namespace NUT_NAMESPACE;
#endif #endif
class Comment; class Comment;
class User : public Table class User : public Nut::Table
{ {
Q_OBJECT Q_OBJECT
@ -29,7 +30,7 @@ class User : public Table
NUT_DECLARE_CHILD_TABLE(Comment, comments) NUT_DECLARE_CHILD_TABLE(Comment, comments)
public: public:
User(QObject *tableSet = 0); Q_INVOKABLE User(QObject *tableSet = 0);
}; };
Q_DECLARE_METATYPE(User*) Q_DECLARE_METATYPE(User*)

View File

@ -3,11 +3,12 @@
#include "user.h" #include "user.h"
#include "post.h" #include "post.h"
#include "comment.h" #include "comment.h"
#include "user.h"
#include "weblogdatabase.h" #include "weblogdatabase.h"
WeblogDatabase::WeblogDatabase() : Database(), WeblogDatabase::WeblogDatabase() : Database(),
m_posts(new TableSet<Post*>(this)), m_posts(new TableSet<Post>(this)),
m_comments(new TableSet<Comment*>(this)), m_comments(new TableSet<Comment>(this)),
m_users(new TableSet<User*>(this)) m_users(new TableSet<User>(this))
{ {
} }