minor changes and some TODO comments
This commit is contained in:
parent
2a18e75d95
commit
f2a0ebab2d
12
src/query.h
12
src/query.h
|
|
@ -78,9 +78,9 @@ public:
|
||||||
template <typename F>
|
template <typename F>
|
||||||
QList<F> select(const FieldPhrase<F> f);
|
QList<F> select(const FieldPhrase<F> f);
|
||||||
int count();
|
int count();
|
||||||
QVariant max(FieldPhrase<int> &f);
|
QVariant max(const FieldPhrase<int> &f);
|
||||||
QVariant min(FieldPhrase<int> &f);
|
QVariant min(const FieldPhrase<int> &f);
|
||||||
QVariant average(FieldPhrase<int> &f);
|
QVariant average(const FieldPhrase<int> &f);
|
||||||
|
|
||||||
//data mailpulation
|
//data mailpulation
|
||||||
int update(WherePhrase phrase);
|
int update(WherePhrase phrase);
|
||||||
|
|
@ -343,7 +343,7 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::count()
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f)
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(const FieldPhrase<int> &f)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
|
|
@ -360,7 +360,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f)
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f)
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(const FieldPhrase<int> &f)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
|
|
@ -377,7 +377,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f)
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(FieldPhrase<int> &f)
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(const FieldPhrase<int> &f)
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,18 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
/*
|
||||||
|
* FIXME:
|
||||||
|
* Qt can not access metaObject inside of constructor
|
||||||
|
* so, if we can't initalize myModel inside of ctor. in
|
||||||
|
* other side myModel inited in propertyChanged signal, so
|
||||||
|
* any method that uses myModel (like: primaryKey, ...) can't
|
||||||
|
* be accessed before any property set. So ugly, but there are
|
||||||
|
* no other way for now.
|
||||||
|
*
|
||||||
|
* This should be fixed to v1.2
|
||||||
|
*/
|
||||||
|
|
||||||
Table::Table(QObject *parent) : QObject(parent)
|
Table::Table(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
setStatus(NewCreated);
|
setStatus(NewCreated);
|
||||||
|
|
@ -40,38 +52,12 @@ void Table::add(TableSetBase *t)
|
||||||
|
|
||||||
QString Table::primaryKey() const
|
QString Table::primaryKey() const
|
||||||
{
|
{
|
||||||
// static QString ret = QString::null;
|
return myModel->primaryKey();
|
||||||
|
|
||||||
// if(ret == QString::null){
|
|
||||||
// for(int i = 0; i < metaObject()->classInfoCount(); i++){
|
|
||||||
// QMetaClassInfo ci = metaObject()->classInfo(i);
|
|
||||||
// QString ciName = ci.name();
|
|
||||||
|
|
||||||
// if(ciName.startsWith(__nut_NAME_PERFIX))
|
|
||||||
// ciName.remove(__nut_NAME_PERFIX);
|
|
||||||
|
|
||||||
// if(ciName.contains(" ")){
|
|
||||||
// QStringList parts = ciName.split(" ");
|
|
||||||
// QString propName = parts.at(1);
|
|
||||||
// if(propName == __nut_PRIMARY_KEY)
|
|
||||||
// ret = parts.at(0);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if(ret == QString::null)
|
|
||||||
// ret = "";
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return ret;
|
|
||||||
return TableModel::findByClassName(metaObject()->className())->primaryKey();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Table::isPrimaryKeyAutoIncrement() const
|
bool Table::isPrimaryKeyAutoIncrement() const
|
||||||
{
|
{
|
||||||
auto m = TableModel::findByClassName(metaObject()->className());
|
return myModel->field(myModel->primaryKey())->isAutoIncrement;
|
||||||
auto pk = m->primaryKey();
|
|
||||||
auto f = m->field(pk);
|
|
||||||
return f->isAutoIncrement;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -82,14 +68,17 @@ QVariant Table::primaryValue() const
|
||||||
|
|
||||||
void Table::propertyChanged(QString propName)
|
void Table::propertyChanged(QString propName)
|
||||||
{
|
{
|
||||||
if(propName == primaryKey())
|
if (!myModel)
|
||||||
|
myModel = TableModel::findByClassName(metaObject()->className());
|
||||||
|
|
||||||
|
if (propName == primaryKey())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_changedProperties.insert(propName);
|
_changedProperties.insert(propName);
|
||||||
if(_status == FeatchedFromDB)
|
if (_status == FeatchedFromDB)
|
||||||
_status = Modified;
|
_status = Modified;
|
||||||
|
|
||||||
if(_status == NewCreated)
|
if (_status == NewCreated)
|
||||||
_status = Added;
|
_status = Added;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,12 +95,12 @@ QSet<QString> Table::changedProperties() const
|
||||||
bool Table::setParentTable(Table *master)
|
bool Table::setParentTable(Table *master)
|
||||||
{
|
{
|
||||||
QString masterClassName = master->metaObject()->className();
|
QString masterClassName = master->metaObject()->className();
|
||||||
TableModel *myModel = TableModel::findByClassName(metaObject()->className());
|
|
||||||
|
|
||||||
foreach (RelationModel *r, myModel->foregionKeys())
|
foreach (RelationModel *r, myModel->foregionKeys())
|
||||||
if(r->masterClassName == masterClassName)
|
if(r->masterClassName == masterClassName)
|
||||||
{
|
{
|
||||||
setProperty(QString(r->localColumn).toLatin1().data(), master->primaryValue());
|
setProperty(QString(r->localColumn).toLatin1().data(),
|
||||||
|
master->primaryValue());
|
||||||
_changedProperties.insert(r->localColumn);
|
_changedProperties.insert(r->localColumn);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
class Database;
|
class Database;
|
||||||
class TableSetBase;
|
class TableSetBase;
|
||||||
|
class TableModel;
|
||||||
class NUT_EXPORT Table : public QObject
|
class NUT_EXPORT Table : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
@ -72,6 +73,7 @@ protected:
|
||||||
void propertyChanged(QString propName);
|
void propertyChanged(QString propName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
TableModel *myModel;
|
||||||
Status _status;
|
Status _status;
|
||||||
QSet<QString> _changedProperties;
|
QSet<QString> _changedProperties;
|
||||||
//TODO: is this removable?
|
//TODO: is this removable?
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,11 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: It may be good idea if we replace this QSet with two QHash!
|
||||||
|
* one for className search and another for typeId.
|
||||||
|
*/
|
||||||
QSet<TableModel*> TableModel::_allModels;
|
QSet<TableModel*> TableModel::_allModels;
|
||||||
//QMap<int, TableScheema*> TableScheema::scheemas;
|
|
||||||
|
|
||||||
QString TableModel::name() const
|
QString TableModel::name() const
|
||||||
{
|
{
|
||||||
|
|
@ -95,6 +98,9 @@ QSet<TableModel *> TableModel::allModels()
|
||||||
return _allModels;
|
return _allModels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is not used anywhere
|
||||||
|
*/
|
||||||
TableModel *TableModel::findByTypeId(int typeId)
|
TableModel *TableModel::findByTypeId(int typeId)
|
||||||
{
|
{
|
||||||
foreach (TableModel *model, _allModels)
|
foreach (TableModel *model, _allModels)
|
||||||
|
|
@ -103,20 +109,18 @@ TableModel *TableModel::findByTypeId(int typeId)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TableModel *TableModel::findByName(QString name)
|
/**
|
||||||
//{
|
* @brief TableModel::findByClassName
|
||||||
// foreach (TableModel *model, _allModels)
|
* Find a table model by class name
|
||||||
// if(model->name() == name)
|
* @param className
|
||||||
// return model;
|
* @return
|
||||||
// return 0;
|
*/
|
||||||
//}
|
|
||||||
|
|
||||||
TableModel *TableModel::findByClassName(QString className)
|
TableModel *TableModel::findByClassName(QString className)
|
||||||
{
|
{
|
||||||
foreach (TableModel *model, _allModels){
|
foreach (TableModel *model, _allModels)
|
||||||
if(model->className() == className)
|
if(model->className() == className)
|
||||||
return model;
|
return model;
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -338,37 +342,6 @@ QJsonObject TableModel::toJson() const
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TableScheema *TableScheema::registerTable(int typeId, QString tableName)
|
|
||||||
//{
|
|
||||||
// TableScheema *scheema = new TableScheema(typeId, tableName);
|
|
||||||
// scheemas.insert(typeId, scheema);
|
|
||||||
// return scheema;
|
|
||||||
//}
|
|
||||||
|
|
||||||
//void TableScheema::createForegionKeys()
|
|
||||||
//{
|
|
||||||
// foreach (TableScheema *sch, scheemas) {
|
|
||||||
// foreach (ForegionKey *fk, sch->_foregionKeys) {
|
|
||||||
// fk->table = scheema(fk->tableName);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//TableModel *TableModel::model(QString className)
|
|
||||||
//{
|
|
||||||
// qFatal("");
|
|
||||||
//#ifdef NUT_NAMESPACE
|
|
||||||
// if(className.startsWith(QT_STRINGIFY(NUT_NAMESPACE) "::"))
|
|
||||||
// className = className.replace(QT_STRINGIFY(NUT_NAMESPACE) "::", "");
|
|
||||||
//#endif
|
|
||||||
|
|
||||||
// foreach (TableModel *s, _allModels)
|
|
||||||
// if(s->_className == className){
|
|
||||||
// return s;
|
|
||||||
// }
|
|
||||||
// return 0;
|
|
||||||
//}
|
|
||||||
|
|
||||||
RelationModel *TableModel::foregionKey(QString otherTable) const
|
RelationModel *TableModel::foregionKey(QString otherTable) const
|
||||||
{
|
{
|
||||||
foreach (RelationModel *fk, _foregionKeys)
|
foreach (RelationModel *fk, _foregionKeys)
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
#include "post.h"
|
#include "post.h"
|
||||||
#include "comment.h"
|
#include "comment.h"
|
||||||
|
#include "score.h"
|
||||||
|
|
||||||
#define PRINT(x) qDebug() << #x "=" << x;
|
#define PRINT(x) qDebug() << #x "=" << x;
|
||||||
#define TIC() QElapsedTimer timer; timer.start()
|
#define TIC() QElapsedTimer timer; timer.start()
|
||||||
|
|
@ -29,6 +30,7 @@ void MainTest::initTestCase()
|
||||||
{
|
{
|
||||||
qDebug() << "User type id:" << qRegisterMetaType<User*>();
|
qDebug() << "User type id:" << qRegisterMetaType<User*>();
|
||||||
qDebug() << "Post type id:" << qRegisterMetaType<Post*>();
|
qDebug() << "Post type id:" << qRegisterMetaType<Post*>();
|
||||||
|
qDebug() << "Score type id:" << qRegisterMetaType<Score*>();
|
||||||
qDebug() << "Comment type id:" << qRegisterMetaType<Comment*>();
|
qDebug() << "Comment type id:" << qRegisterMetaType<Comment*>();
|
||||||
qDebug() << "DB type id:" << qRegisterMetaType<WeblogDatabase*>();
|
qDebug() << "DB type id:" << qRegisterMetaType<WeblogDatabase*>();
|
||||||
|
|
||||||
|
|
@ -80,6 +82,12 @@ void MainTest::createPost()
|
||||||
comment->setAuthorId(user->id());
|
comment->setAuthorId(user->id());
|
||||||
newPost->comments()->append(comment);
|
newPost->comments()->append(comment);
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
Score *score = new Score;
|
||||||
|
score->setScore(i % 5);
|
||||||
|
newPost->scores()->append(score);
|
||||||
|
}
|
||||||
|
|
||||||
db.saveChanges();
|
db.saveChanges();
|
||||||
|
|
||||||
postId = newPost->id();
|
postId = newPost->id();
|
||||||
|
|
@ -99,7 +107,7 @@ void MainTest::createPost2()
|
||||||
|
|
||||||
for(int i = 0 ; i < 3; i++){
|
for(int i = 0 ; i < 3; i++){
|
||||||
Comment *comment = new Comment;
|
Comment *comment = new Comment;
|
||||||
comment->setMessage("comment #" + QString::number(i));
|
comment->setMessage("comment #" + QString::number(i + 2));
|
||||||
comment->setSaveDate(QDateTime::currentDateTime());
|
comment->setSaveDate(QDateTime::currentDateTime());
|
||||||
comment->setAuthor(user);
|
comment->setAuthor(user);
|
||||||
comment->setPostId(newPost->id());
|
comment->setPostId(newPost->id());
|
||||||
|
|
@ -134,6 +142,15 @@ void MainTest::selectPosts()
|
||||||
db.cleanUp();
|
db.cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainTest::selectScoreAverage()
|
||||||
|
{
|
||||||
|
auto a = db.scores()->query()
|
||||||
|
->join<Post>()
|
||||||
|
->setWhere(Post::idField() == 1)
|
||||||
|
->average(Score::scoreField());
|
||||||
|
qDebug() << a;
|
||||||
|
}
|
||||||
|
|
||||||
void MainTest::selectFirst()
|
void MainTest::selectFirst()
|
||||||
{
|
{
|
||||||
auto posts = db.posts()->query()
|
auto posts = db.posts()->query()
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ private slots:
|
||||||
void createPost2();
|
void createPost2();
|
||||||
void join();
|
void join();
|
||||||
void selectPosts();
|
void selectPosts();
|
||||||
|
void selectScoreAverage();
|
||||||
void selectFirst();
|
void selectFirst();
|
||||||
void selectPostsWithoutTitle();
|
void selectPostsWithoutTitle();
|
||||||
void selectPostIds();
|
void selectPostIds();
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,4 @@ Post::Post(QObject *parent) : Table(parent),
|
||||||
}
|
}
|
||||||
|
|
||||||
NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments)
|
NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments)
|
||||||
|
NUT_IMPLEMENT_CHILD_TABLE(Post, Score, scores)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue