minor changes and some TODO comments

This commit is contained in:
Hamed Masafi 2018-01-15 23:34:42 +03:30
parent 2a18e75d95
commit f2a0ebab2d
7 changed files with 65 additions and 82 deletions

View File

@ -78,9 +78,9 @@ public:
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);
QVariant max(const FieldPhrase<int> &f);
QVariant min(const FieldPhrase<int> &f);
QVariant average(const FieldPhrase<int> &f);
//data mailpulation
int update(WherePhrase phrase);
@ -343,7 +343,7 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::count()
}
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);
@ -360,7 +360,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::max(FieldPhrase<int> &f)
}
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);
@ -377,7 +377,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::min(FieldPhrase<int> &f)
}
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);

View File

@ -27,6 +27,18 @@
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)
{
setStatus(NewCreated);
@ -40,38 +52,12 @@ void Table::add(TableSetBase *t)
QString Table::primaryKey() const
{
// static QString ret = QString::null;
// 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();
return myModel->primaryKey();
}
bool Table::isPrimaryKeyAutoIncrement() const
{
auto m = TableModel::findByClassName(metaObject()->className());
auto pk = m->primaryKey();
auto f = m->field(pk);
return f->isAutoIncrement;
return myModel->field(myModel->primaryKey())->isAutoIncrement;
}
@ -82,14 +68,17 @@ QVariant Table::primaryValue() const
void Table::propertyChanged(QString propName)
{
if(propName == primaryKey())
if (!myModel)
myModel = TableModel::findByClassName(metaObject()->className());
if (propName == primaryKey())
return;
_changedProperties.insert(propName);
if(_status == FeatchedFromDB)
if (_status == FeatchedFromDB)
_status = Modified;
if(_status == NewCreated)
if (_status == NewCreated)
_status = Added;
}
@ -106,12 +95,12 @@ QSet<QString> Table::changedProperties() const
bool Table::setParentTable(Table *master)
{
QString masterClassName = master->metaObject()->className();
TableModel *myModel = TableModel::findByClassName(metaObject()->className());
foreach (RelationModel *r, myModel->foregionKeys())
if(r->masterClassName == masterClassName)
{
setProperty(QString(r->localColumn).toLatin1().data(), master->primaryValue());
setProperty(QString(r->localColumn).toLatin1().data(),
master->primaryValue());
_changedProperties.insert(r->localColumn);
return true;
}

View File

@ -33,6 +33,7 @@ NUT_BEGIN_NAMESPACE
class Database;
class TableSetBase;
class TableModel;
class NUT_EXPORT Table : public QObject
{
Q_OBJECT
@ -72,6 +73,7 @@ protected:
void propertyChanged(QString propName);
private:
TableModel *myModel;
Status _status;
QSet<QString> _changedProperties;
//TODO: is this removable?

View File

@ -30,8 +30,11 @@
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;
//QMap<int, TableScheema*> TableScheema::scheemas;
QString TableModel::name() const
{
@ -95,6 +98,9 @@ QSet<TableModel *> TableModel::allModels()
return _allModels;
}
/*
* This is not used anywhere
*/
TableModel *TableModel::findByTypeId(int typeId)
{
foreach (TableModel *model, _allModels)
@ -103,20 +109,18 @@ TableModel *TableModel::findByTypeId(int typeId)
return 0;
}
//TableModel *TableModel::findByName(QString name)
//{
// foreach (TableModel *model, _allModels)
// if(model->name() == name)
// return model;
// return 0;
//}
/**
* @brief TableModel::findByClassName
* Find a table model by class name
* @param className
* @return
*/
TableModel *TableModel::findByClassName(QString className)
{
foreach (TableModel *model, _allModels){
foreach (TableModel *model, _allModels)
if(model->className() == className)
return model;
}
return 0;
}
@ -338,37 +342,6 @@ QJsonObject TableModel::toJson() const
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
{
foreach (RelationModel *fk, _foregionKeys)

View File

@ -14,6 +14,7 @@
#include "user.h"
#include "post.h"
#include "comment.h"
#include "score.h"
#define PRINT(x) qDebug() << #x "=" << x;
#define TIC() QElapsedTimer timer; timer.start()
@ -29,6 +30,7 @@ void MainTest::initTestCase()
{
qDebug() << "User type id:" << qRegisterMetaType<User*>();
qDebug() << "Post type id:" << qRegisterMetaType<Post*>();
qDebug() << "Score type id:" << qRegisterMetaType<Score*>();
qDebug() << "Comment type id:" << qRegisterMetaType<Comment*>();
qDebug() << "DB type id:" << qRegisterMetaType<WeblogDatabase*>();
@ -80,6 +82,12 @@ void MainTest::createPost()
comment->setAuthorId(user->id());
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();
postId = newPost->id();
@ -99,7 +107,7 @@ void MainTest::createPost2()
for(int i = 0 ; i < 3; i++){
Comment *comment = new Comment;
comment->setMessage("comment #" + QString::number(i));
comment->setMessage("comment #" + QString::number(i + 2));
comment->setSaveDate(QDateTime::currentDateTime());
comment->setAuthor(user);
comment->setPostId(newPost->id());
@ -134,6 +142,15 @@ void MainTest::selectPosts()
db.cleanUp();
}
void MainTest::selectScoreAverage()
{
auto a = db.scores()->query()
->join<Post>()
->setWhere(Post::idField() == 1)
->average(Score::scoreField());
qDebug() << a;
}
void MainTest::selectFirst()
{
auto posts = db.posts()->query()

View File

@ -29,6 +29,7 @@ private slots:
void createPost2();
void join();
void selectPosts();
void selectScoreAverage();
void selectFirst();
void selectPostsWithoutTitle();
void selectPostIds();

View File

@ -12,3 +12,4 @@ Post::Post(QObject *parent) : Table(parent),
}
NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments)
NUT_IMPLEMENT_CHILD_TABLE(Post, Score, scores)