Herunterladen als
root/samples/example/main.cpp @ 846ba44a
b7446f4c | Christian Ehringfeld | #include <QCoreApplication>
|
|
#include <QDir>
|
|||
#include <QDebug>
|
|||
#include <QThread>
|
|||
886bdb22 | Christian Ehringfeld | #include "entity.h"
|
|
#include "entitymanager.h"
|
|||
#include "relation.h"
|
|||
b7446f4c | Christian Ehringfeld | #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();
|
|
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>();
|
|||
79bcb404 | Christian Ehringfeld | CuteEntityManager::EntityManager *e = new
|
|
CuteEntityManager::EntityManager("QSQLITE",
|
|||
bf16a581 | Christian Ehringfeld | QDir::currentPath() + "/db.sqlite", "", "", "", 0, true);
|
|
// CuteEntityManager::EntityManager("QSQLITE",
|
|||
// ":memory:", "", "", "", "", true, "foreign_keys = ON");
|
|||
// SqliteBackupProcessor *sqliteproc = new SqliteBackupProcessor(e->getDb(),
|
|||
// QDir::currentPath());
|
|||
// qWarning() << "DB Loaded:" << sqliteproc->sqliteDBMemFile(false, "db.sqlite");
|
|||
01fe6db3 | Christian Ehringfeld | qDebug() << "EntityManagerObjectName:" << e->objectName();
|
|
b7446f4c | Christian Ehringfeld | QThread *entityManager = new QThread();
|
|
e->moveToThread(entityManager);
|
|||
12a65aab | Christian Ehringfeld | qWarning() << "-----------------------------";
|
|
qWarning() << "Create Contact, Address, Pupil and Group Tables";
|
|||
qWarning() << "-----------------------------";
|
|||
409ca288 | Christian Ehringfeld | QStringList inits = QStringList() << "Contact" << "Address" << "Person" <<
|
|
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();
|
|||
qWarning() << "-----------------------------";
|
|||
qWarning() << "Find Person By Id Version 1";
|
|||
qWarning() << "-----------------------------";
|
|||
QSharedPointer<Entity> personFindPtr = e->findById(1, QString("Person"));
|
|||
554f7bc0 | Christian Ehringfeld | qDebug() << "HASCHANGED:" << e->hasChanged(personFindPtr);
|
|
975a7f96 | Christian Ehringfeld | e->refresh(personFindPtr);
|
|
QSharedPointer<Person> pers = personFindPtr.objectCast<Person>();
|
|||
qWarning() << "MainTeacher:" << personFindPtr->toString();
|
|||
qWarning() << "-----------------------------";
|
|||
qWarning() << "Find Person By Id Version 2";
|
|||
qWarning() << "-----------------------------";
|
|||
QSharedPointer<Person> foundMainTeacher = e->findById<Person>(1);
|
|||
qWarning() << "FoundMainTeacher:" << foundMainTeacher->toString();
|
|||
qWarning() << "-----------------------------";
|
|||
qWarning() << "Find Pupil with Query Class";
|
|||
qWarning() << "-----------------------------";
|
|||
Query q = Query();
|
|||
274b34e4 | Christian Ehringfeld | q.appendWhere(e->getQueryBuilder()->like(QString("firstname"), QString("Tim"),
|
|
JokerPosition::BEHIND));
|
|||
q.appendWhere(e->getQueryBuilder()->andOperator());
|
|||
q.appendWhere(e->getQueryBuilder()->arbitraryOperator("<", "birthday",
|
|||
QDate(2000, 10, 10)));
|
|||
975a7f96 | Christian Ehringfeld | q.setDistinct(true);
|
|
q.appendOrderBy(OrderBy(QString("birthday"), Direction::SORT_DESC));
|
|||
q.setLimit(10);
|
|||
fc14f551 | Christian Ehringfeld | QList<QSharedPointer<Pupil>> list = e->find<Pupil>(q, true);
|
|
975a7f96 | Christian Ehringfeld | for (int i = 0; i < list.size(); ++i) {
|
|
d2c33642 | Christian Ehringfeld | qWarning() << "-----------------------------";
|
|
qWarning() << "Merge Pupil";
|
|||
qWarning() << "-----------------------------";
|
|||
e111d9ab | Christian Ehringfeld | list.at(i)->setBirthday(QDate(2222, 12, 22));
|
|
d2c33642 | Christian Ehringfeld | QSharedPointer<Entity> pupilE = list.at(i).objectCast<Entity>();
|
|
e111d9ab | Christian Ehringfeld | e->merge(pupilE, true);
|
|
975a7f96 | Christian Ehringfeld | qWarning() << list.at(i)->toString();
|
|
}
|
|||
qWarning() << "-----------------------------";
|
|||
qWarning() << "Find Pupil by Attributes";
|
|||
qWarning() << "-----------------------------";
|
|||
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>();
|
|||
a473cd61 | Christian Ehringfeld | e->save(entityGroupFindPtr, true);
|
|
975a7f96 | Christian Ehringfeld | qWarning() << "-----------------------------";
|
|
qWarning() << "Remove Group";
|
|||
qWarning() << "-----------------------------";
|
|||
e111d9ab | Christian Ehringfeld | e->remove(entityGroupFindPtr);
|
|
bf16a581 | Christian Ehringfeld | //sqliteproc->sqliteDBMemFile(true, "db.sqlite");
|
|
fa8b0f73 | Christian Ehringfeld | qWarning() << "Duration:" << t.elapsed();
|
|
85946434 | Christian Ehringfeld | // delete sqliteproc;
|
|
5ca225ed | Christian Ehringfeld | delete e;
|
|
b7446f4c | Christian Ehringfeld | return 0;
|
|
}
|