Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (5,75 KB) Statistiken
| Zweig: | Revision:
b7446f4c Christian Ehringfeld
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QThread>
#include "../src/entity.h"
#include "../src/entitymanager.h"
#include "../src/relation.h"
#include "entityinstancefactory.h"
#include "models/pupil.h"
#include "models/address.h"
#include "models/contact.h"
#include "models/group.h"
#include "models/faker/createfakemodeldata.h"
9fccde32 Christian Ehringfeld
#include "querybuilder.h"
#include "orderby.h"
bcd2b697 Christian Ehringfeld
#include "sqlitebackupprocessor.h"
5c3d9487 Christian Ehringfeld
b7446f4c Christian Ehringfeld
using namespace CuteEntityManager;
int main(int argc, char *argv[]) {
Q_UNUSED(argc) Q_UNUSED(argv)
26fb3804 Christian Ehringfeld
QTime t;
b7446f4c Christian Ehringfeld
t.start();
bcd2b697 Christian Ehringfeld
b7446f4c Christian Ehringfeld
CuteEntityManager::EntityManager *e = new
bcd2b697 Christian Ehringfeld
// CuteEntityManager::EntityManager("QSQLITE",
// QDir::currentPath() + "/db.sqlite", "", "", "", 0, true);
66704054 Christian Ehringfeld
CuteEntityManager::EntityManager("QSQLITE",
9971e7d2 Christian Ehringfeld
":memory:", "", "", "", "", true, "foreign_keys = ON");
bcd2b697 Christian Ehringfeld
SqliteBackupProcessor *sqliteproc = new SqliteBackupProcessor(e->getDb(),
QDir::currentPath());
qWarning() << "DB Loaded:" << sqliteproc->sqliteDBMemFile(false, "db.sqlite");
f9cef58f Christian Ehringfeld
/**
* @brief EntityInstanceFactory::registerClass<EntityClass>
* You must register every EntityClass, cause Qt is not creating all meta object informations for entity manager
*/
b7446f4c Christian Ehringfeld
EntityInstanceFactory::registerClass<Group>();
EntityInstanceFactory::registerClass<Person>();
EntityInstanceFactory::registerClass<Pupil>();
EntityInstanceFactory::registerClass<Contact>();
EntityInstanceFactory::registerClass<Address>();
QThread *entityManager = new QThread();
e->moveToThread(entityManager);
12a65aab Christian Ehringfeld
qWarning() << "-----------------------------";
qWarning() << "Create Contact, Address, Pupil and Group Tables";
qWarning() << "-----------------------------";
da565582 Christian Ehringfeld
QStringList inits = QStringList() << "Contact" << "Address" <<
66704054 Christian Ehringfeld
"Pupil" << "Group";
f9cef58f Christian Ehringfeld
/**
* Instead of startup(version,qstringlist) you can call method createTable of EntityManager (e->create(sharedptr))
* startup will create tables inclusive relation tables for classes in QStringList inits
*/
9971e7d2 Christian Ehringfeld
qWarning() << "Tables created or already set up:" << e->startup("0.1", inits,
true);
5c3d9487 Christian Ehringfeld
QSharedPointer<CuteEntityManager::Entity> p =
66704054 Christian Ehringfeld
QSharedPointer<CuteEntityManager::Entity>(new Person("Max", "Mustermann",
Person::Gender::MALE, "", "", "",
QDate::currentDate()));
975a7f96 Christian Ehringfeld
qWarning() << "-----------------------------";
qWarning() << "Persist Group with Relations";
qWarning() << "-----------------------------";
QSharedPointer<Group> gPtr = QSharedPointer<Group>(new Group());
CreateFakeModelData::fillGroup(gPtr.data());
gPtr->setName("9b");
QSharedPointer<Entity> groupPtr = gPtr.objectCast<Entity>();
QSharedPointer<Person> mainTeacher = QSharedPointer<Person>(new Person("Max",
"Mustermann", Person::Gender::MALE));
gPtr->setMainTeacher(mainTeacher);
//Persons will also persisted
if (e->count(groupPtr->getTablename()) <= 0) {
e->create(groupPtr, true, true);
}
qWarning() << "-----------------------------";
qWarning() << "Find All Groups";
qWarning() << "-----------------------------";
QList<QSharedPointer<Group>> groups = e->findAll<Group>();
QSharedPointer<Group> groupFindPtr = groups.at(0);
qWarning() << "Group:" << groupFindPtr->toString();
qWarning() << "MainTeacher:" << groupFindPtr->getMainTeacher()->toString();
5c3d9487 Christian Ehringfeld
975a7f96 Christian Ehringfeld
qWarning() << "-----------------------------";
qWarning() << "Find Person By Id Version 1";
qWarning() << "-----------------------------";
67639b75 Christian Ehringfeld
975a7f96 Christian Ehringfeld
QSharedPointer<Entity> personFindPtr = e->findById(1, QString("Person"));
e->refresh(personFindPtr);
QSharedPointer<Person> pers = personFindPtr.objectCast<Person>();
qWarning() << "MainTeacher:" << personFindPtr->toString();
qWarning() << "-----------------------------";
qWarning() << "Find Person By Id Version 2";
qWarning() << "-----------------------------";
67639b75 Christian Ehringfeld
975a7f96 Christian Ehringfeld
QSharedPointer<Person> foundMainTeacher = e->findById<Person>(1);
qWarning() << "FoundMainTeacher:" << foundMainTeacher->toString();
12a65aab Christian Ehringfeld
975a7f96 Christian Ehringfeld
qWarning() << "-----------------------------";
qWarning() << "Find Pupil with Query Class";
qWarning() << "-----------------------------";
67639b75 Christian Ehringfeld
975a7f96 Christian Ehringfeld
Query q = Query();
q.appendWhere(e->getQueryBuilder()->like(QString("firstname"), QString("Tim")));
q.appendJoin(Join("person", "pupil.id = person.id"));
q.setDistinct(true);
q.appendOrderBy(OrderBy(QString("birthday"), Direction::SORT_DESC));
q.setLimit(10);
QList<QSharedPointer<Pupil>> list = e->find<Pupil>(q);
for (int i = 0; i < list.size(); ++i) {
qWarning() << list.at(i)->toString();
}
qWarning() << "-----------------------------";
qWarning() << "Find Pupil by Attributes";
qWarning() << "-----------------------------";
67639b75 Christian Ehringfeld
975a7f96 Christian Ehringfeld
QHash<QString, QVariant> attributes;
attributes["familyName"] = QString("Dunst");
QSharedPointer<Pupil> pupil = e->findEntityByAttributes<Pupil>
(attributes, true);
qWarning() << pupil->toString();
qWarning() << "-----------------------------";
qWarning() << "Merge Group";
qWarning() << "-----------------------------";
groupFindPtr->setName("10b");
qWarning() << groupFindPtr->toString();
QSharedPointer<Entity> entityGroupFindPtr = groupFindPtr.objectCast<Entity>();
e->save(entityGroupFindPtr, false);
9e44a59b Christian Ehringfeld
975a7f96 Christian Ehringfeld
qWarning() << "-----------------------------";
qWarning() << "Remove Group";
qWarning() << "-----------------------------";
e->remove(entityGroupFindPtr);
bcd2b697 Christian Ehringfeld
sqliteproc->sqliteDBMemFile(true, "db.sqlite");
fa8b0f73 Christian Ehringfeld
qWarning() << "Duration:" << t.elapsed();
bcd2b697 Christian Ehringfeld
delete sqliteproc;
b7446f4c Christian Ehringfeld
return 0;
}