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(),
__CHANGE_LOG_TABLE_NAME);
changeLogs = new TableSet<ChangeLogTable*>(q);
changeLogs = new TableSet<ChangeLogTable>(q);
for (int i = 0; i < q->metaObject()->classInfoCount(); i++) {
QMetaClassInfo ci = q->metaObject()->classInfo(i);

View File

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

View File

@ -40,13 +40,13 @@
#define NUT_DECLARE_TABLE(type, name) \
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #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; \
NUT_WRAP_NAMESPACE(TableSet<type*>) *m_##name##s; \
NUT_WRAP_NAMESPACE(TableSet<type>) *m_##name##s; \
public: \
static const type _##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
#define NUT_DECLARE_FIELD(type, name, read, write) \
@ -79,17 +79,21 @@ public: \
#define NUT_DECLARE_CHILD_TABLE(type, n) \
private: \
NUT_WRAP_NAMESPACE(TableSet)<type*> *m_##n; \
NUT_WRAP_NAMESPACE(TableSet)<type> *m_##n; \
public: \
NUT_WRAP_NAMESPACE(TableSet)<type*> *n(){ \
static type *n##Table(); \
NUT_WRAP_NAMESPACE(TableSet)<type> *n();
#define NUT_IMPLEMENT_CHILD_TABLE(class, type, n) \
type *class::n##Table(){ \
static type *f = new type(); \
return f; \
} \
NUT_WRAP_NAMESPACE(TableSet)<type> *class::n(){ \
return m_##n; \
}
//static type *n##Table(){ \
// /*static type *f = new type();*/ \
// /*return f;*/ \
//} \
#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_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) \
@ -98,11 +102,12 @@ public: \
#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_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
# define FROM(x) (x->query())
# define WHERE(x) ->setWhere(x)
# define JOIN(x) ->join(#x)
# define JOIN(x) ->join<x>()
# define ORDERBY(x) ->orderBy(#x);
# define ORDERBY_DESC(x) ->orderBy(!#x);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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