Revision 9971e7d2
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
samples/example/main.cpp | ||
---|---|---|
// CuteEntityManager::EntityManager("QSQLITE",
|
||
// QDir::currentPath() + "/db.sqlite", "", "", "", 0, true);
|
||
CuteEntityManager::EntityManager("QSQLITE",
|
||
":memory:", "", "", "", "", true);
|
||
":memory:", "", "", "", "", true, "foreign_keys = ON");
|
||
SqliteBackupProcessor *sqliteproc = new SqliteBackupProcessor(e->getDb(),
|
||
QDir::currentPath());
|
||
qWarning() << "DB Loaded:" << sqliteproc->sqliteDBMemFile(false, "db.sqlite");
|
||
... | ... | |
* 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
|
||
*/
|
||
qWarning() << "Tables created or already set up:" << e->startup("0.1", inits);
|
||
qWarning() << "Tables created or already set up:" << e->startup("0.1", inits,
|
||
true);
|
||
QSharedPointer<CuteEntityManager::Entity> p =
|
||
QSharedPointer<CuteEntityManager::Entity>(new Person("Max", "Mustermann",
|
||
Person::Gender::MALE, "", "", "",
|
||
QDate::currentDate()));
|
||
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() << "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() << "-----------------------------";
|
||
// qWarning() << "-----------------------------";
|
||
// qWarning() << "Find Person By Id Version 1";
|
||
// qWarning() << "-----------------------------";
|
||
|
||
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() << "-----------------------------";
|
||
// 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() << "-----------------------------";
|
||
|
||
QSharedPointer<Person> foundMainTeacher = e->findById<Person>(1);
|
||
qWarning() << "FoundMainTeacher:" << foundMainTeacher->toString();
|
||
// QSharedPointer<Person> foundMainTeacher = e->findById<Person>(1);
|
||
// qWarning() << "FoundMainTeacher:" << foundMainTeacher->toString();
|
||
|
||
qWarning() << "-----------------------------";
|
||
qWarning() << "Find Pupil with Query Class";
|
||
qWarning() << "-----------------------------";
|
||
// qWarning() << "-----------------------------";
|
||
// qWarning() << "Find Pupil with Query Class";
|
||
// qWarning() << "-----------------------------";
|
||
|
||
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() << "-----------------------------";
|
||
// 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() << "-----------------------------";
|
||
|
||
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);
|
||
// 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);
|
||
|
||
qWarning() << "-----------------------------";
|
||
qWarning() << "Remove Group";
|
||
qWarning() << "-----------------------------";
|
||
e->remove(entityGroupFindPtr);
|
||
// qWarning() << "-----------------------------";
|
||
// qWarning() << "Remove Group";
|
||
// qWarning() << "-----------------------------";
|
||
// e->remove(entityGroupFindPtr);
|
||
sqliteproc->sqliteDBMemFile(true, "db.sqlite");
|
||
qWarning() << "Duration:" << t.elapsed();
|
||
delete sqliteproc;
|
src/database.cpp | ||
---|---|---|
QString hostname,
|
||
QString databasename,
|
||
QString username, QString password, qint64 port, bool loggerActivated,
|
||
bool logQueries, bool logErrors) {
|
||
bool logQueries, bool logErrors, QString databaseOptions) {
|
||
this->database = QSqlDatabase::addDatabase(databaseType, connectionName);
|
||
this->connectionName = connectionName;
|
||
if (hostname != QString("")) {
|
||
if (!hostname.isEmpty()) {
|
||
this->database.setHostName(hostname);
|
||
}
|
||
if (databasename != QString("")) {
|
||
if (!databasename.isEmpty()) {
|
||
this->database.setDatabaseName(databasename);
|
||
}
|
||
if (username != QString("")) {
|
||
if (!username.isEmpty()) {
|
||
this->database.setUserName(username);
|
||
}
|
||
if (password != QString("")) {
|
||
if (!password.isEmpty()) {
|
||
this->database.setPassword(password);
|
||
}
|
||
if (port != 0) {
|
||
this->database.setPort(port);
|
||
}
|
||
if(!databaseOptions.isEmpty()) {
|
||
this->database.setConnectOptions(databaseOptions);
|
||
}
|
||
this->init();
|
||
this->initLogger(loggerActivated, logQueries, logErrors);
|
||
}
|
src/database.h | ||
---|---|---|
QString databasename = QString("") ,
|
||
QString username = QString(""), QString password = QString(""),
|
||
qint64 port = 0, bool loggerActivated = true, bool logQueries = false,
|
||
bool logErrors = true);
|
||
bool logErrors = true, QString databaseOptions = "");
|
||
QSqlDatabase getDatabase();
|
||
QString getConnectionName();
|
||
QSqlQuery getQuery();
|
src/entitymanager.cpp | ||
---|---|---|
|
||
EntityManager::EntityManager(const QString &databaseType, QString databasename ,
|
||
QString hostname, QString username, QString password, QString port,
|
||
bool logQueries) : QObject() {
|
||
bool logQueries, QString databaseOptions) : QObject() {
|
||
auto db = new Database(databaseType, this->createConnection(), hostname,
|
||
databasename, username,
|
||
password,
|
||
port.toInt(), true, logQueries);
|
||
port.toInt(), true, logQueries, true, databaseOptions);
|
||
this->db = QSharedPointer<Database>(db);
|
||
this->init();
|
||
}
|
||
... | ... | |
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
}
|
||
|
||
bool EntityManager::startup(QString version, QStringList toInitialize) {
|
||
bool EntityManager::startup(QString version, QStringList toInitialize,
|
||
bool createIndices) {
|
||
QSharedPointer<Entity> dbm = QSharedPointer<DatabaseMigration>
|
||
(new DatabaseMigration());
|
||
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
||
... | ... | |
this->createTable(dbm, true);
|
||
}
|
||
if (this->findAllByAttributes(map, dbm->getTablename()).isEmpty()) {
|
||
QList<QSharedPointer<Entity>> entities = QList<QSharedPointer<Entity>>();
|
||
for (int var = 0; var < toInitialize.size(); ++var) {
|
||
if (ok) {
|
||
QString c = toInitialize.at(var);
|
||
ok = this->createTable(QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(c)));
|
||
auto entity = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(c));
|
||
ok = this->createTable(entity);
|
||
entities.append(entity);
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
if (createIndices) {
|
||
for (int i = 0; i < entities.size(); ++i) {
|
||
ok = this->schema->getQueryBuilder()->createIndices(entities.at(i));
|
||
}
|
||
}
|
||
if (ok) {
|
||
auto dbmPtr = dbm.objectCast<DatabaseMigration>();
|
||
dbmPtr->setVersion(version);
|
src/entitymanager.h | ||
---|---|---|
class QueryInterpreter;
|
||
class EntityManager : public QObject {
|
||
Q_OBJECT
|
||
signals:
|
||
void actionFinished(qint64 id);
|
||
public slots:
|
||
/**
|
||
* @brief startup
|
||
... | ... | |
* @param toInitialize list of entity classnames which database tables should be created
|
||
* @return
|
||
*/
|
||
bool startup(QString version, QStringList toInitialize);
|
||
bool startup(QString version, QStringList toInitialize, bool createIndices = false);
|
||
bool executeQuery(const QString &query);
|
||
static void removeConnectionName(const QString &name);
|
||
QSharedPointer<Entity> findById(const qint64 &id, const QString &classname);
|
||
... | ... | |
EntityManager(const QString &databaseType, QString databasename = "" ,
|
||
QString hostname = "",
|
||
QString username = "",
|
||
QString password = "", QString port = "", bool logQueries = false);
|
||
QString password = "", QString port = "", bool logQueries = false, QString databaseOptions = "");
|
||
~EntityManager();
|
||
static QStringList getConnectionNames();
|
||
QSharedPointer<QueryBuilder> getQueryBuilder() const;
|
src/querybuilder.cpp | ||
---|---|---|
}
|
||
this->schema->getTableSchema(tableName);
|
||
rc = true;
|
||
if (rc) {
|
||
rc = this->createIndices(entity);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
queries.append(superIndex);
|
||
}
|
||
queries.append(this->relationFks(entity));
|
||
qDebug() << "create index:" << entity->getTablename() << queries.size();
|
||
ok = this->database->transaction(queries);
|
||
return ok;
|
||
}
|
||
... | ... | |
|
||
QString QueryBuilder::createFkSuperClass(const Entity *e) const {
|
||
QString r = "";
|
||
auto superMetaObject = e->metaObject()->superClass();
|
||
if (e->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE
|
||
&& QString(superMetaObject->className()) !=
|
||
this->entityClassname()) {
|
||
Entity *superClass = EntityInstanceFactory::createInstance(
|
||
superMetaObject->className());
|
||
if (superClass) {
|
||
QString refColumn = superClass->getPrimaryKey();
|
||
QString refTable = superClass->getTablename();
|
||
r = this->addForeignKey(this->generateIndexName(e->getPrimaryKey(),
|
||
e->getTablename(), refColumn, refTable, true), e->getTablename(),
|
||
QStringList(e->getPrimaryKey()), refTable, QStringList(refColumn),
|
||
this->getForeignKeyCascade(CASCADE),
|
||
this->getForeignKeyCascade(CASCADE));
|
||
delete superClass;
|
||
if (supportsForeignKeys()) {
|
||
auto superMetaObject = e->metaObject()->superClass();
|
||
if (e->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE
|
||
&& QString(superMetaObject->className()) !=
|
||
this->entityClassname()) {
|
||
Entity *superClass = EntityInstanceFactory::createInstance(
|
||
superMetaObject->className());
|
||
if (superClass) {
|
||
QString refColumn = superClass->getPrimaryKey();
|
||
QString refTable = superClass->getTablename();
|
||
r = this->addForeignKey(this->generateIndexName(e->getPrimaryKey(),
|
||
e->getTablename(), refColumn, refTable, this->supportsForeignKeys()),
|
||
e->getTablename(),
|
||
QStringList(e->getPrimaryKey()), refTable, QStringList(refColumn),
|
||
this->getForeignKeyCascade(CASCADE),
|
||
this->getForeignKeyCascade(CASCADE));
|
||
delete superClass;
|
||
}
|
||
}
|
||
}
|
||
return r;
|
src/schema/sqlitequerybuilder.cpp | ||
---|---|---|
refTableName)Q_UNUSED(refColumns)Q_UNUSED(deleteConstraint)Q_UNUSED(
|
||
updateConstraint)
|
||
//not supported
|
||
if (this->supportsForeignKeys()) {
|
||
return QueryBuilder::addForeignKey(name, tableName, columns, refTableName,
|
||
refColumns, deleteConstraint, updateConstraint);
|
||
}
|
||
return "";
|
||
}
|
||
|
||
QString CuteEntityManager::SqliteQueryBuilder::dropForeignKey(QString name,
|
||
QString tableName) const {
|
||
Q_UNUSED(name)Q_UNUSED(tableName)
|
||
if (this->supportsForeignKeys()) {
|
||
return QueryBuilder::dropForeignKey(name, tableName);
|
||
}
|
||
//not supported
|
||
return "";
|
||
}
|
Auch abrufbar als: Unified diff
update