Herunterladen als
root/samples/example/main.cpp @ 2ee5022f
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"
|
|||
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();
|
|
CuteEntityManager::EntityManager *e = new
|
|||
66704054 | Christian Ehringfeld | CuteEntityManager::EntityManager("QSQLITE",
|
|
86e5c917 | Christian Ehringfeld | QDir::currentPath() + "/db.sqlite","","","",0,true);
|
|
66704054 | Christian Ehringfeld | // CuteEntityManager::EntityManager("QSQLITE",
|
|
// ":memory:");
|
|||
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);
|
|||
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
|
|||
*/
|
|||
5c3d9487 | Christian Ehringfeld | e->startup("0.1", inits);
|
|
b7446f4c | Christian Ehringfeld | ||
5c3d9487 | Christian Ehringfeld | QSharedPointer<CuteEntityManager::Entity> p =
|
|
66704054 | Christian Ehringfeld | QSharedPointer<CuteEntityManager::Entity>(new Person("Max", "Mustermann",
|
|
Person::Gender::MALE, "", "", "",
|
|||
QDate::currentDate()));
|
|||
5c3d9487 | Christian Ehringfeld | /** ---------------------------------
|
|
* PERSIST
|
|||
* ---------------------------------
|
|||
*/
|
|||
da565582 | Christian Ehringfeld | ||
QSharedPointer<Group> gPtr = QSharedPointer<Group>(new Group());
|
|||
CreateFakeModelData::fillGroup(gPtr.data());
|
|||
gPtr->setName("9b");
|
|||
5c3d9487 | Christian Ehringfeld | QSharedPointer<Entity> groupPtr = gPtr.objectCast<Entity>();
|
|
df1e56bd | Christian Ehringfeld | QSharedPointer<Person> mainTeacher = QSharedPointer<Person>(new Person("Max",
|
|
66704054 | Christian Ehringfeld | "Mustermann", Person::Gender::MALE));
|
|
df1e56bd | Christian Ehringfeld | gPtr->setMainTeacher(mainTeacher);
|
|
5c3d9487 | Christian Ehringfeld | //Persons will also persisted
|
|
66704054 | Christian Ehringfeld | if (e->count(groupPtr->getTablename()) <= 0) {
|
|
c9e79b83 | Christian Ehringfeld | e->create(groupPtr, true, true);
|
|
}
|
|||
5c3d9487 | Christian Ehringfeld | ||
/** ---------------------------------
|
|||
67639b75 | Christian Ehringfeld | * FIND Group
|
|
5c3d9487 | Christian Ehringfeld | * ---------------------------------
|
|
*/
|
|||
42d32a68 | Christian Ehringfeld | qDebug() << "-----------------------------";
|
|
5c3d9487 | Christian Ehringfeld | QSharedPointer<Entity> groupFindPtr = e->findById<Group *>(1);
|
|
QSharedPointer<Group> grp = groupFindPtr.objectCast<Group>();
|
|||
f9cef58f | Christian Ehringfeld | qDebug() << "Group:" << groupFindPtr->toString();
|
|
5c3d9487 | Christian Ehringfeld | qDebug() << "PersonSize:" << grp->getPersons().size();
|
|
f9cef58f | Christian Ehringfeld | qDebug() << "PupilsSize:" << grp->getPupils().size();
|
|
df1e56bd | Christian Ehringfeld | qDebug() << "MainTeacher:" << grp->getMainTeacher()->toString();
|
|
67639b75 | Christian Ehringfeld | ||
/** ---------------------------------
|
|||
* FIND Person
|
|||
* ---------------------------------
|
|||
*/
|
|||
f9cef58f | Christian Ehringfeld | qDebug() << "-----------------------------";
|
|
QSharedPointer<Entity> personFindPtr = e->findById(1, QString("Person*"));
|
|||
67639b75 | Christian Ehringfeld | e->refresh(personFindPtr);
|
|
QSharedPointer<Person> pers = personFindPtr.objectCast<Person>();
|
|||
f9cef58f | Christian Ehringfeld | qDebug() << "MainTeacher:" << personFindPtr->toString();
|
|
9d62f4aa | Christian Ehringfeld | qDebug() << "MaintainedGroupSize:" << pers->getMaintainedGroups().size();
|
|
qDebug() << "GroupSize:" << pers->getGroups().size();
|
|||
67639b75 | Christian Ehringfeld | ||
f9cef58f | Christian Ehringfeld | /**
|
|
66704054 | Christian Ehringfeld | * or you can use this syntax:
|
|
f9cef58f | Christian Ehringfeld | */
|
|
qDebug() << "-----------------------------";
|
|||
QSharedPointer<Person> foundMainTeacher = e->findById<Person *>
|
|||
(1).objectCast<Person>();
|
|||
qDebug() << "FoundMainTeacher:" << foundMainTeacher->toString();
|
|||
qDebug() << "FoundMainTeacherGroupSize:" <<
|
|||
66704054 | Christian Ehringfeld | foundMainTeacher->getMaintainedGroups().size();
|
|
67639b75 | Christian Ehringfeld | ||
f9cef58f | Christian Ehringfeld | qDebug() << "-----------------------------";
|
|
67639b75 | Christian Ehringfeld | ||
b7446f4c | Christian Ehringfeld | qDebug() << "Duration:" << t.elapsed();
|
|
f9cef58f | Christian Ehringfeld | ||
b7446f4c | Christian Ehringfeld | return 0;
|
|
}
|