add donate button in readme [skip ci]
This commit is contained in:
parent
5014016e4d
commit
8654b6a1f9
|
|
@ -61,4 +61,8 @@ if(post) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Donate
|
||||||
|
Butcoin address: 1Dn1WHKkaxanXe4cTGDk4cFRRABxLUpEVj
|
||||||
|

|
||||||
|
|
||||||
For more information read [Wiki](wiki).
|
For more information read [Wiki](wiki).
|
||||||
|
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 403 B |
|
|
@ -214,7 +214,10 @@ bool DatabasePrivate::getCurrectScheema()
|
||||||
tables.insert(name, value);
|
tables.insert(name, value);
|
||||||
|
|
||||||
int typeId = QMetaType::type(name.toLocal8Bit() + "*");
|
int typeId = QMetaType::type(name.toLocal8Bit() + "*");
|
||||||
qDebug() << type << name << value << typeId;
|
|
||||||
|
if (!typeId)
|
||||||
|
qFatal("The class %s is not registered with qt meta object", qPrintable(name));
|
||||||
|
|
||||||
TableModel *sch = new TableModel(typeId, value);
|
TableModel *sch = new TableModel(typeId, value);
|
||||||
currentModel.append(sch);
|
currentModel.append(sch);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -229,13 +229,13 @@ DatabaseModel *DatabaseModel::modelByName(const QString &name)
|
||||||
|
|
||||||
void DatabaseModel::deleteAllModels()
|
void DatabaseModel::deleteAllModels()
|
||||||
{
|
{
|
||||||
// QMapIterator<QString, DatabaseModel*> i(_models);
|
QMapIterator<QString, DatabaseModel*> i(_models);
|
||||||
// while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
// i.next();
|
i.next();
|
||||||
//// cout << i.key() << ": " << i.value() << endl;
|
// cout << i.key() << ": " << i.value() << endl;
|
||||||
// delete i.value();
|
// qDeleteAll(i.value());
|
||||||
// }
|
}
|
||||||
qDeleteAll(_models.values());
|
// qDeleteAll(_models.values());
|
||||||
_models.clear();
|
_models.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,7 @@ inline bool nutClassInfoInt(const QMetaClassInfo &classInfo,
|
||||||
NUT_INFO(__nut_FIELD, name, 0) \
|
NUT_INFO(__nut_FIELD, name, 0) \
|
||||||
type m_##name; \
|
type m_##name; \
|
||||||
public: \
|
public: \
|
||||||
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) name ## Field(){ \
|
static NUT_WRAP_NAMESPACE(FieldPhrase<type>)& name ## Field(){ \
|
||||||
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) f = \
|
static NUT_WRAP_NAMESPACE(FieldPhrase<type>) f = \
|
||||||
NUT_WRAP_NAMESPACE(FieldPhrase<type>) \
|
NUT_WRAP_NAMESPACE(FieldPhrase<type>) \
|
||||||
(staticMetaObject.className(), #name); \
|
(staticMetaObject.className(), #name); \
|
||||||
|
|
|
||||||
|
|
@ -534,7 +534,7 @@ QString SqlGeneratorBase::selectCommand(const QString &tableName,
|
||||||
Q_UNUSED(take);
|
Q_UNUSED(take);
|
||||||
QString selectText;
|
QString selectText;
|
||||||
|
|
||||||
if (!fields.isValid) {
|
if (fields.data.count() == 0) {
|
||||||
QSet<TableModel*> tables;
|
QSet<TableModel*> tables;
|
||||||
tables.insert(_database->model().tableByName(tableName));
|
tables.insert(_database->model().tableByName(tableName));
|
||||||
foreach (RelationModel *rel, joins)
|
foreach (RelationModel *rel, joins)
|
||||||
|
|
@ -648,6 +648,27 @@ QString SqlGeneratorBase::updateCommand(const QString &tableName,
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString SqlGeneratorBase::insertCommand(const QString &tableName, const AssignmentPhraseList &assigments)
|
||||||
|
{
|
||||||
|
|
||||||
|
QString fieldNames;
|
||||||
|
QString values;
|
||||||
|
foreach (PhraseData *d, assigments.data) {
|
||||||
|
if (fieldNames != "")
|
||||||
|
fieldNames.append(", ");
|
||||||
|
|
||||||
|
if (values != "")
|
||||||
|
values.append(", ");
|
||||||
|
|
||||||
|
fieldNames.append(d->left->fieldName);
|
||||||
|
values.append(escapeValue(d->operand));
|
||||||
|
}
|
||||||
|
return QString("INSERT INTO %1 (%2) VALUES (%3);")
|
||||||
|
.arg(tableName)
|
||||||
|
.arg(fieldNames)
|
||||||
|
.arg(values);
|
||||||
|
}
|
||||||
|
|
||||||
//QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
//QString SqlGeneratorBase::selectCommand(SqlGeneratorBase::AgregateType t,
|
||||||
// QString agregateArg,
|
// QString agregateArg,
|
||||||
// QString tableName,
|
// QString tableName,
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,9 @@ public:
|
||||||
virtual QString updateCommand(const QString &tableName,
|
virtual QString updateCommand(const QString &tableName,
|
||||||
const AssignmentPhraseList &assigments,
|
const AssignmentPhraseList &assigments,
|
||||||
const ConditionalPhrase &where);
|
const ConditionalPhrase &where);
|
||||||
|
|
||||||
|
virtual QString insertCommand(const QString &tableName,
|
||||||
|
const AssignmentPhraseList &assigments);
|
||||||
// virtual QString selectCommand(AgregateType t,
|
// virtual QString selectCommand(AgregateType t,
|
||||||
// QString agregateArg, QString tableName,
|
// QString agregateArg, QString tableName,
|
||||||
// QList<WherePhrase> &wheres,
|
// QList<WherePhrase> &wheres,
|
||||||
|
|
|
||||||
58
src/query.h
58
src/query.h
|
|
@ -83,12 +83,14 @@ public:
|
||||||
QVariant min(const FieldPhrase<int> &f);
|
QVariant min(const FieldPhrase<int> &f);
|
||||||
QVariant average(const FieldPhrase<int> &f);
|
QVariant average(const FieldPhrase<int> &f);
|
||||||
|
|
||||||
|
QVariant insert(AssignmentPhraseList p);
|
||||||
|
|
||||||
//data mailpulation
|
//data mailpulation
|
||||||
int update(const AssignmentPhraseList &ph);
|
int update(const AssignmentPhraseList &ph);
|
||||||
// int insert(const AssignmentPhraseList &ph);
|
// int insert(const AssignmentPhraseList &ph);
|
||||||
int remove();
|
int remove();
|
||||||
|
|
||||||
QSqlQueryModel *toModal();
|
QSqlQueryModel *toModel();
|
||||||
|
|
||||||
//debug purpose
|
//debug purpose
|
||||||
QString sqlCommand() const;
|
QString sqlCommand() const;
|
||||||
|
|
@ -121,7 +123,6 @@ template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
qDebug() << "~Query";// << d->sql;
|
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -223,7 +224,8 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
// if (p == lastP)
|
// if (p == lastP)
|
||||||
// qFatal("NULL Loop detected");
|
// qFatal("NULL Loop detected");
|
||||||
|
|
||||||
n = (++n) % levels.count();
|
++n;
|
||||||
|
n = n % levels.count();
|
||||||
if (checked[n])
|
if (checked[n])
|
||||||
continue;
|
continue;
|
||||||
LevelData &data = levels[n];
|
LevelData &data = levels[n];
|
||||||
|
|
@ -231,13 +233,16 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
// check if key value is changed
|
// check if key value is changed
|
||||||
if (data.lastKeyValue == q.value(data.keyFiledname)) {
|
if (data.lastKeyValue == q.value(data.keyFiledname)) {
|
||||||
--p;
|
--p;
|
||||||
|
qDebug() << "key os not changed for" << data.keyFiledname;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if master if current table has processed
|
// check if master if current table has processed
|
||||||
foreach (int m, data.masters)
|
foreach (int m, data.masters)
|
||||||
if (!checked[m])
|
if (!checked[m]) {
|
||||||
|
qDebug() << "row is checked";
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
checked[n] = true;
|
checked[n] = true;
|
||||||
--p;
|
--p;
|
||||||
|
|
@ -257,6 +262,8 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
if (!table)
|
if (!table)
|
||||||
qFatal("Could not create instance of %s",
|
qFatal("Could not create instance of %s",
|
||||||
qPrintable(data.table->name()));
|
qPrintable(data.table->name()));
|
||||||
|
|
||||||
|
qDebug() << data.table->name() << "created";
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList childFields = data.table->fieldsNames();
|
QStringList childFields = data.table->fieldsNames();
|
||||||
|
|
@ -327,7 +334,7 @@ Q_OUTOFLINE_TEMPLATE T *Query<T>::first()
|
||||||
if (list.count())
|
if (list.count())
|
||||||
return list.first();
|
return list.first();
|
||||||
else
|
else
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|
@ -404,6 +411,17 @@ Q_OUTOFLINE_TEMPLATE QVariant Query<T>::average(const FieldPhrase<int> &f)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
Q_OUTOFLINE_TEMPLATE QVariant Query<T>::insert(AssignmentPhraseList p)
|
||||||
|
{
|
||||||
|
Q_D(Query);
|
||||||
|
d->sql = d->database->sqlGenertor()
|
||||||
|
->insertCommand(d->tableName, p);
|
||||||
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
|
return q.lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &className)
|
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::join(const QString &className)
|
||||||
{
|
{
|
||||||
|
|
@ -524,7 +542,7 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QSqlQueryModel *Query<T>::toModal()
|
Q_OUTOFLINE_TEMPLATE QSqlQueryModel *Query<T>::toModel()
|
||||||
{
|
{
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
|
|
||||||
|
|
@ -533,21 +551,31 @@ Q_OUTOFLINE_TEMPLATE QSqlQueryModel *Query<T>::toModal()
|
||||||
d->fieldPhrase,
|
d->fieldPhrase,
|
||||||
d->wherePhrase, d->orderPhrase, d->relations,
|
d->wherePhrase, d->orderPhrase, d->relations,
|
||||||
d->skip, d->take);
|
d->skip, d->take);
|
||||||
|
qDebug() << d->sql;
|
||||||
DatabaseModel dbModel = d->database->model();
|
DatabaseModel dbModel = d->database->model();
|
||||||
QSqlQueryModel *model = new QSqlQueryModel;
|
QSqlQueryModel *model = new QSqlQueryModel;
|
||||||
model->setQuery(d->sql, d->database->database());
|
model->setQuery(d->sql, d->database->database());
|
||||||
|
|
||||||
int fieldIndex = 0;
|
int fieldIndex = 0;
|
||||||
foreach (const PhraseData *pd, d->fieldPhrase.data) {
|
|
||||||
QString displayName = dbModel.tableByClassName(pd->className)
|
|
||||||
->field(pd->fieldName)->displayName;
|
|
||||||
|
|
||||||
qDebug() << "Display name for"<<pd->className<<pd->fieldName
|
if (d->fieldPhrase.data.count()) {
|
||||||
<<"="<<displayName;
|
foreach (const PhraseData *pd, d->fieldPhrase.data) {
|
||||||
model->setHeaderData(fieldIndex++,
|
QString displayName = dbModel.tableByClassName(pd->className)
|
||||||
Qt::Horizontal,
|
->field(pd->fieldName)->displayName;
|
||||||
displayName);
|
|
||||||
|
qDebug() << "Display name for"<<pd->className<<pd->fieldName
|
||||||
|
<<"="<<displayName;
|
||||||
|
model->setHeaderData(fieldIndex++,
|
||||||
|
Qt::Horizontal,
|
||||||
|
displayName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TableModel *tbl = d->database->model().tableByName(d->tableName);
|
||||||
|
foreach (FieldModel *f, tbl->fields()) {
|
||||||
|
model->setHeaderData(fieldIndex++,
|
||||||
|
Qt::Horizontal,
|
||||||
|
f->displayName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ class NUT_EXPORT Table : public QObject
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Table(QObject *parentTableSet = 0);
|
explicit Table(QObject *parentTableSet = nullptr);
|
||||||
|
|
||||||
enum Status{
|
enum Status{
|
||||||
NewCreated,
|
NewCreated,
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,14 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
TableSetBase::TableSetBase(Database *parent) : QObject(parent), _database(parent), _table(0)
|
TableSetBase::TableSetBase(Database *parent) : QObject(parent), _database(parent), _table(0),
|
||||||
|
_tableName(QString())
|
||||||
{
|
{
|
||||||
parent->add(this);
|
parent->add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
TableSetBase::TableSetBase(Table *parent) : QObject(parent), _database(0), _table(parent)
|
TableSetBase::TableSetBase(Table *parent) : QObject(parent), _database(0), _table(parent),
|
||||||
|
_tableName(QString())
|
||||||
{
|
{
|
||||||
parent->add(this);
|
parent->add(this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,17 +22,20 @@
|
||||||
.arg(timer.elapsed() / 1000.) \
|
.arg(timer.elapsed() / 1000.) \
|
||||||
.arg(__func__)
|
.arg(__func__)
|
||||||
|
|
||||||
|
#define REGISTER(x) qDebug() << #x << "type id:" << qRegisterMetaType<x*>()
|
||||||
|
|
||||||
MainTest::MainTest(QObject *parent) : QObject(parent)
|
MainTest::MainTest(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainTest::initTestCase()
|
void MainTest::initTestCase()
|
||||||
{
|
{
|
||||||
qDebug() << "User type id:" << qRegisterMetaType<User*>();
|
//register all entities with Qt-MetaType mechanism
|
||||||
qDebug() << "Post type id:" << qRegisterMetaType<Post*>();
|
REGISTER(User);
|
||||||
qDebug() << "Score type id:" << qRegisterMetaType<Score*>();
|
REGISTER(Post);
|
||||||
qDebug() << "Comment type id:" << qRegisterMetaType<Comment*>();
|
REGISTER(Score);
|
||||||
qDebug() << "DB type id:" << qRegisterMetaType<WeblogDatabase*>();
|
REGISTER(Comment);
|
||||||
|
REGISTER(WeblogDatabase);
|
||||||
|
|
||||||
db.setDriver(DRIVER);
|
db.setDriver(DRIVER);
|
||||||
db.setHostName(HOST);
|
db.setHostName(HOST);
|
||||||
|
|
@ -41,11 +44,12 @@ void MainTest::initTestCase()
|
||||||
db.setPassword(PASSWORD);
|
db.setPassword(PASSWORD);
|
||||||
|
|
||||||
bool ok = db.open();
|
bool ok = db.open();
|
||||||
|
QTEST_ASSERT(ok);
|
||||||
|
|
||||||
db.comments()->query()->remove();
|
db.comments()->query()->remove();
|
||||||
db.posts()->query()->remove();
|
db.posts()->query()->remove();
|
||||||
|
db.users()->query()->remove();
|
||||||
QTEST_ASSERT(ok);
|
db.scores()->query()->remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainTest::dataScheema()
|
void MainTest::dataScheema()
|
||||||
|
|
@ -61,6 +65,7 @@ void MainTest::dataScheema()
|
||||||
void MainTest::createUser()
|
void MainTest::createUser()
|
||||||
{
|
{
|
||||||
user = new User;
|
user = new User;
|
||||||
|
user->setId(QUuid::createUuid());
|
||||||
user->setUsername("admin");
|
user->setUsername("admin");
|
||||||
user->setPassword("123456");
|
user->setPassword("123456");
|
||||||
db.users()->append(user);
|
db.users()->append(user);
|
||||||
|
|
@ -73,11 +78,13 @@ void MainTest::createPost()
|
||||||
Post *newPost = new Post;
|
Post *newPost = new Post;
|
||||||
newPost->setTitle("post title");
|
newPost->setTitle("post title");
|
||||||
newPost->setSaveDate(QDateTime::currentDateTime());
|
newPost->setSaveDate(QDateTime::currentDateTime());
|
||||||
|
newPost->setPublic(false);
|
||||||
|
|
||||||
db.posts()->append(newPost);
|
db.posts()->append(newPost);
|
||||||
|
|
||||||
for(int i = 0 ; i < 3; i++){
|
for(int i = 0 ; i < 3; i++){
|
||||||
Comment *comment = new Comment;
|
Comment *comment = new Comment;
|
||||||
|
comment->setId(QUuid::createUuid());
|
||||||
comment->setMessage("comment #" + QString::number(i));
|
comment->setMessage("comment #" + QString::number(i));
|
||||||
comment->setSaveDate(QDateTime::currentDateTime());
|
comment->setSaveDate(QDateTime::currentDateTime());
|
||||||
comment->setAuthorId(user->id());
|
comment->setAuthorId(user->id());
|
||||||
|
|
@ -100,25 +107,27 @@ void MainTest::createPost()
|
||||||
|
|
||||||
void MainTest::createPost2()
|
void MainTest::createPost2()
|
||||||
{
|
{
|
||||||
Post *newPost = new Post;
|
//create post on the fly
|
||||||
newPost->setTitle("post title");
|
QVariant postIdVar = db.posts()->query()->insert(
|
||||||
newPost->setSaveDate(QDateTime::currentDateTime());
|
(Post::titleField() = "This is a sample")
|
||||||
|
& (Post::isPublicField() = true));
|
||||||
|
|
||||||
db.posts()->append(newPost);
|
QTEST_ASSERT(postIdVar.type() == QVariant::LongLong);
|
||||||
db.saveChanges();
|
int postId = postIdVar.toInt();
|
||||||
|
|
||||||
for(int i = 0 ; i < 3; i++){
|
for(int i = 0 ; i < 3; i++){
|
||||||
Comment *comment = new Comment;
|
Comment *comment = new Comment;
|
||||||
|
comment->setId(QUuid::createUuid());
|
||||||
comment->setMessage("comment #" + QString::number(i + 2));
|
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());
|
//join child to master by id
|
||||||
|
comment->setPostId(postId);
|
||||||
db.comments()->append(comment);
|
db.comments()->append(comment);
|
||||||
}
|
}
|
||||||
db.saveChanges();
|
db.saveChanges();
|
||||||
|
|
||||||
QTEST_ASSERT(newPost->id() != 0);
|
QTEST_ASSERT(postId != 0);
|
||||||
qDebug() << "New post2 inserted with id:" << newPost->id();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainTest::updatePostOnTheFly()
|
void MainTest::updatePostOnTheFly()
|
||||||
|
|
@ -134,17 +143,20 @@ void MainTest::selectPublicts()
|
||||||
{
|
{
|
||||||
auto q = db.posts()->query()
|
auto q = db.posts()->query()
|
||||||
->where(Post::isPublicField())
|
->where(Post::isPublicField())
|
||||||
->toList();
|
->count();
|
||||||
|
|
||||||
auto q2 = db.posts()->query()
|
auto q2 = db.posts()->query()
|
||||||
->where(!Post::isPublicField())
|
->where(!Post::isPublicField())
|
||||||
->toList();
|
->count();
|
||||||
|
|
||||||
|
QTEST_ASSERT(q == 1);
|
||||||
|
QTEST_ASSERT(q2 == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainTest::selectPosts()
|
void MainTest::selectPosts()
|
||||||
{
|
{
|
||||||
auto q = db.posts()->query()
|
auto q = db.posts()->query()
|
||||||
->join<Comment>()//Comment::authorIdField() == Post::idField())
|
->join<Comment>()
|
||||||
->orderBy(!Post::saveDateField() | Post::bodyField())
|
->orderBy(!Post::saveDateField() | Post::bodyField())
|
||||||
->setWhere(Post::idField() == postId);
|
->setWhere(Post::idField() == postId);
|
||||||
|
|
||||||
|
|
@ -155,6 +167,7 @@ void MainTest::selectPosts()
|
||||||
PRINT(posts.length());
|
PRINT(posts.length());
|
||||||
PRINT(posts.at(0)->comments()->length());
|
PRINT(posts.at(0)->comments()->length());
|
||||||
QTEST_ASSERT(posts.length() == 1);
|
QTEST_ASSERT(posts.length() == 1);
|
||||||
|
qDebug() << posts.at(0)->comments()->length();
|
||||||
QTEST_ASSERT(posts.at(0)->comments()->length() == 3);
|
QTEST_ASSERT(posts.at(0)->comments()->length() == 3);
|
||||||
QTEST_ASSERT(posts.at(0)->title() == "post title");
|
QTEST_ASSERT(posts.at(0)->title() == "post title");
|
||||||
|
|
||||||
|
|
@ -193,7 +206,7 @@ void MainTest::selectPostIds()
|
||||||
{
|
{
|
||||||
auto q = db.posts()->query();
|
auto q = db.posts()->query();
|
||||||
auto ids = q->select(Post::idField());
|
auto ids = q->select(Post::idField());
|
||||||
qDebug() << q->sqlCommand();
|
qDebug() << ids.count();
|
||||||
QTEST_ASSERT(ids.count() == 2);
|
QTEST_ASSERT(ids.count() == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -248,7 +261,7 @@ void MainTest::modifyPost()
|
||||||
|
|
||||||
Post *post = q->first();
|
Post *post = q->first();
|
||||||
|
|
||||||
QTEST_ASSERT(post != 0);
|
QTEST_ASSERT(post != nullptr);
|
||||||
|
|
||||||
post->setTitle("new name");
|
post->setTitle("new name");
|
||||||
db.saveChanges();
|
db.saveChanges();
|
||||||
|
|
@ -263,10 +276,10 @@ void MainTest::modifyPost()
|
||||||
|
|
||||||
void MainTest::emptyDatabase()
|
void MainTest::emptyDatabase()
|
||||||
{
|
{
|
||||||
auto commentsCount = db.comments()->query()->remove();
|
// auto commentsCount = db.comments()->query()->remove();
|
||||||
auto postsCount = db.posts()->query()->remove();
|
// auto postsCount = db.posts()->query()->remove();
|
||||||
QTEST_ASSERT(postsCount == 3);
|
// QTEST_ASSERT(postsCount == 3);
|
||||||
QTEST_ASSERT(commentsCount == 6);
|
// QTEST_ASSERT(commentsCount == 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainTest::cleanupTestCase()
|
void MainTest::cleanupTestCase()
|
||||||
|
|
@ -274,8 +287,10 @@ void MainTest::cleanupTestCase()
|
||||||
post->deleteLater();
|
post->deleteLater();
|
||||||
user->deleteLater();
|
user->deleteLater();
|
||||||
|
|
||||||
|
//release models before exiting
|
||||||
qDeleteAll(TableModel::allModels());
|
qDeleteAll(TableModel::allModels());
|
||||||
DatabaseModel::deleteAllModels();
|
|
||||||
|
PRINT_FORM(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
QTEST_MAIN(MainTest)
|
QTEST_MAIN(MainTest)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class Comment : public Table
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
NUT_PRIMARY_AUTO_INCREMENT(id)
|
NUT_PRIMARY_KEY(id)
|
||||||
NUT_DECLARE_FIELD(QUuid, id, id, setId)
|
NUT_DECLARE_FIELD(QUuid, id, id, setId)
|
||||||
NUT_DECLARE_FIELD(QString, message, message, setMessage)
|
NUT_DECLARE_FIELD(QString, message, message, setMessage)
|
||||||
NUT_DECLARE_FIELD(QDateTime, saveDate, saveDate, setSaveDate)
|
NUT_DECLARE_FIELD(QDateTime, saveDate, saveDate, setSaveDate)
|
||||||
|
|
@ -26,7 +26,7 @@ class Comment : public Table
|
||||||
NUT_FOREGION_KEY(User, QUuid, author, author, setAuthor)
|
NUT_FOREGION_KEY(User, QUuid, author, author, setAuthor)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Q_INVOKABLE explicit Comment(QObject *parentTableSet = 0);
|
Q_INVOKABLE explicit Comment(QObject *parentTableSet = nullptr);
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(Comment*)
|
Q_DECLARE_METATYPE(Comment*)
|
||||||
|
|
|
||||||
|
|
@ -25,4 +25,25 @@
|
||||||
//#define USERNAME "sa"
|
//#define USERNAME "sa"
|
||||||
//#define PASSWORD "qwe123!@#"
|
//#define PASSWORD "qwe123!@#"
|
||||||
|
|
||||||
|
#ifdef Q_OS_LINUX
|
||||||
|
# define OS "Linux"
|
||||||
|
#elif defined(Q_OS_WIN)
|
||||||
|
# define OS "Windows"
|
||||||
|
#elif defined(Q_OS_OSX)
|
||||||
|
# define OS "macOS"
|
||||||
|
#else
|
||||||
|
# define OS "Unknown"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define PRINT_FORM(db) \
|
||||||
|
qDebug() << "\n\n****************************" \
|
||||||
|
<< "\nAll tests passed," \
|
||||||
|
<< "please fill in bellow form and email it to me at" \
|
||||||
|
<< "hamed.masafi@gmail.com" \
|
||||||
|
<< "\n\tDriver:" << db.driver() \
|
||||||
|
<< "\n\tOS: " OS " (version: ________)" \
|
||||||
|
<< "\n\tQt version: " QT_VERSION_STR \
|
||||||
|
<< "\n\tTest:" << metaObject()->className() \
|
||||||
|
<< "\n****************************\n";
|
||||||
|
|
||||||
#endif // CONSTS_H
|
#endif // CONSTS_H
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ class User : public Nut::Table
|
||||||
NUT_DECLARE_CHILD_TABLE(Score, scores)
|
NUT_DECLARE_CHILD_TABLE(Score, scores)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Q_INVOKABLE User(QObject *parentTableSet = 0);
|
Q_INVOKABLE User(QObject *parentTableSet = nullptr);
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(User*)
|
Q_DECLARE_METATYPE(User*)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
#include <QtTest>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QSqlError>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QElapsedTimer>
|
||||||
|
|
||||||
|
#include "consts.h"
|
||||||
|
|
||||||
|
#include "maintest.h"
|
||||||
|
#include "query.h"
|
||||||
|
#include "tableset.h"
|
||||||
|
#include "tablemodel.h"
|
||||||
|
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
MainTest::MainTest(QObject *parent) : QObject(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainTest::initTestCase()
|
||||||
|
{
|
||||||
|
qDebug() << "Test type id:" << qRegisterMetaType<Test*>();
|
||||||
|
qDebug() << "DB type id:" << qRegisterMetaType<TestDatabase*>();
|
||||||
|
|
||||||
|
db.setDriver(DRIVER);
|
||||||
|
db.setHostName(HOST);
|
||||||
|
db.setDatabaseName("nut_tst_quuid");
|
||||||
|
db.setUserName(USERNAME);
|
||||||
|
db.setPassword(PASSWORD);
|
||||||
|
|
||||||
|
bool ok = db.open();
|
||||||
|
|
||||||
|
db.tests()->query()->remove();
|
||||||
|
|
||||||
|
QTEST_ASSERT(ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainTest::add()
|
||||||
|
{
|
||||||
|
TIC();
|
||||||
|
QUuid uuid = QUuid::createUuid();
|
||||||
|
Test t;
|
||||||
|
t.setId(uuid);
|
||||||
|
t.setUsername("test username");
|
||||||
|
db.tests()->append(&t);
|
||||||
|
db.saveChanges();
|
||||||
|
TOC();
|
||||||
|
|
||||||
|
Test *t2 = db.tests()->query()
|
||||||
|
->where(Test::idField() == uuid)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
TOC();
|
||||||
|
QTEST_ASSERT(t2->id() == uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainTest::cleanupTestCase()
|
||||||
|
{
|
||||||
|
qDeleteAll(Nut::TableModel::allModels());
|
||||||
|
// Nut::DatabaseModel::deleteAllModels();
|
||||||
|
}
|
||||||
|
|
||||||
|
QTEST_MAIN(MainTest)
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
#ifndef MAINTEST_H
|
||||||
|
#define MAINTEST_H
|
||||||
|
|
||||||
|
#include <QtCore/QObject>
|
||||||
|
#include <QtCore/qglobal.h>
|
||||||
|
|
||||||
|
#include "testdatabase.h"
|
||||||
|
class Test;
|
||||||
|
class MainTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
TestDatabase db;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit MainTest(QObject *parent = 0);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void initTestCase();
|
||||||
|
void add();
|
||||||
|
|
||||||
|
void cleanupTestCase();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MAINTEST_H
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
QT += qml quick testlib sql
|
||||||
|
QT -= gui
|
||||||
|
|
||||||
|
TARGET = tst_nut
|
||||||
|
TEMPLATE = app
|
||||||
|
|
||||||
|
CONFIG += warn_on qmltestcase c++11
|
||||||
|
INCLUDEPATH += $$PWD/../../src $$PWD/../common
|
||||||
|
include(../../nut.pri)
|
||||||
|
IMPORTPATH += $$OUT_PWD/../src/imports
|
||||||
|
SOURCES += \
|
||||||
|
maintest.cpp \
|
||||||
|
testdatabase.cpp \
|
||||||
|
test.cpp
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
maintest.h \
|
||||||
|
../common/consts.h \
|
||||||
|
testdatabase.h \
|
||||||
|
test.h
|
||||||
Loading…
Reference in New Issue