Revision d568923d
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| example/main.cpp | ||
|---|---|---|
|
auto ep = a.dynamicCast<CuteEntityManager::Entity>();
|
||
|
qDebug() << e;
|
||
|
qDebug() << "Tabelle artikel erstellt:" << e->createTable(ep);
|
||
|
e->create(ep);
|
||
|
// OpenTeacherTool::Artikel *b= new OpenTeacherTool::Artikel(30,"Peter123");
|
||
|
// OpenTeacherTool::Entity *entity = b->getEntity();
|
||
|
// qDebug() << "findByAttributes:" << e->findByAttributes(entity,true);
|
||
| example/models/group.cpp | ||
|---|---|---|
|
QHash<QString, CuteEntityManager::Relation> Group::getRelations()
|
||
|
{
|
||
|
QHash<QString, CuteEntityManager::Relation> h = QHash<QString, CuteEntityManager::Relation>();
|
||
|
CuteEntityManager::Relation r = CuteEntityManager::Relation("artikel",CuteEntityManager::BELONGS_TO);
|
||
|
CuteEntityManager::Relation r = CuteEntityManager::Relation("artikel",CuteEntityManager::MANY_TO_ONE);
|
||
|
h.insert("artikel", r);
|
||
|
return h;
|
||
|
}
|
||
| src/database.cpp | ||
|---|---|---|
|
} else {
|
||
|
rc = this->exec(query);
|
||
|
}
|
||
|
qDebug() << "Executed Query:" << query;
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
bool Database::transaction(QSqlQuery &query) {
|
||
|
this->database.transaction();
|
||
|
query.exec();
|
||
|
this->debugQuery(query);
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
return false;
|
||
| ... | ... | |
|
for (int var = 0; var < queries.size(); ++var) {
|
||
|
q = queries.at(var);
|
||
|
q.exec();
|
||
|
this->debugQuery(q);
|
||
|
}
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
| ... | ... | |
|
return true;
|
||
|
}
|
||
|
|
||
|
bool Database::exec(QString query) {
|
||
|
bool Database::exec(const QString &query) {
|
||
|
this->database.transaction();
|
||
|
QSqlQuery q = QSqlQuery(this->database);
|
||
|
q.exec(query);
|
||
|
this->debugQuery(q);
|
||
|
if (!this->database.commit()) {
|
||
|
this->database.rollback();
|
||
|
return false;
|
||
| ... | ... | |
|
bool ok = true;
|
||
|
for (int var = 0; var < queries.size() && ok; ++var) {
|
||
|
ok = q.exec(queries.at(var));
|
||
|
this->debugQuery(q);
|
||
|
if (!ok) {
|
||
|
break;
|
||
|
}
|
||
| ... | ... | |
|
return ok;
|
||
|
}
|
||
|
|
||
|
bool Database::exec(QSqlQuery query) {
|
||
|
return query.exec();
|
||
|
bool Database::exec(QSqlQuery &query) {
|
||
|
bool ok = query.exec();
|
||
|
this->debugQuery(query);
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
bool Database::exec(QList<QSqlQuery> queries) {
|
||
| ... | ... | |
|
for (int var = 0; var < queries.size() && ok; ++var) {
|
||
|
q = queries.at(var);
|
||
|
ok = q.exec();
|
||
|
this->debugQuery(q);
|
||
|
if (!ok) {
|
||
|
break;
|
||
|
}
|
||
| ... | ... | |
|
return ok;
|
||
|
}
|
||
|
|
||
|
void Database::debugQuery(const QSqlQuery &query) const {
|
||
|
qDebug() << query.executedQuery();
|
||
|
}
|
||
|
|
||
|
bool Database::select(QSqlQuery &query) {
|
||
|
query.setForwardOnly(true);
|
||
|
return query.exec();
|
||
|
bool ok = query.exec();
|
||
|
this->debugQuery(query);
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
QSqlQuery Database::select(const QString &query) {
|
||
|
QSqlQuery q = QSqlQuery(this->database);
|
||
|
q.exec(query);
|
||
|
this->debugQuery(q);
|
||
|
return q;
|
||
|
}
|
||
|
|
||
| src/database.h | ||
|---|---|---|
|
bool transaction(const QStringList &queries);
|
||
|
bool transaction(QSqlQuery &query);
|
||
|
bool transaction(QList<QSqlQuery> &queries);
|
||
|
bool exec(QString query);
|
||
|
bool exec(const QString &query);
|
||
|
bool exec(QStringList queries);
|
||
|
bool exec(QSqlQuery query);
|
||
|
bool exec(QSqlQuery &query);
|
||
|
bool exec(QList<QSqlQuery> queries);
|
||
|
void debugQuery(const QSqlQuery &query) const;
|
||
|
bool select(QSqlQuery &query);
|
||
|
QSqlQuery select(const QString &query);
|
||
|
|
||
| src/databasemigration.cpp | ||
|---|---|---|
|
|
||
|
}
|
||
|
|
||
|
DatabaseMigration::DatabaseMigration(QString version, QDateTime applyTime) {
|
||
|
DatabaseMigration::DatabaseMigration(QString version, QDateTime applyTime) : Entity() {
|
||
|
this->version = version;
|
||
|
this->applyTime = applyTime;
|
||
|
}
|
||
| src/entityinstancefactory.cpp | ||
|---|---|---|
|
return EntityInstanceFactory::createInstance(QMetaType::type(className));
|
||
|
}
|
||
|
|
||
|
Entity *EntityInstanceFactory::createInstance(const QString &className) {
|
||
|
return EntityInstanceFactory::createInstance(className.toUtf8().constData());
|
||
|
}
|
||
|
|
||
|
Entity *EntityInstanceFactory::createInstance(int id) {
|
||
|
Entity *e = 0;
|
||
|
if (id != -1) {
|
||
| src/entityinstancefactory.h | ||
|---|---|---|
|
class EntityInstanceFactory {
|
||
|
public:
|
||
|
static Entity *createInstance(const char *className);
|
||
|
static Entity *createInstance(const QString &className);
|
||
|
static Entity *createInstance(int id);
|
||
|
static Entity *createInstance(const char *className, const QHash<QString, QVariant> &attributes);
|
||
|
static Entity *setAttributes(Entity *e, const QHash<QString, QVariant> &attributes, QHash<QString, QMetaProperty> metaprops);
|
||
| src/entitymanager.cpp | ||
|---|---|---|
|
}
|
||
|
|
||
|
bool EntityManager::startup(QString version, QStringList toInitialize) {
|
||
|
DatabaseMigration dbm = DatabaseMigration();
|
||
|
DatabaseMigration *dbm = new DatabaseMigration();
|
||
|
QSharedPointer<Entity> ptrDbm = QSharedPointer<Entity>(dbm);
|
||
|
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
||
|
map.insert("version", version);
|
||
|
this->findAllByAttributes(map, dbm.getTablename());
|
||
|
if (this->findAllByAttributes(map, dbm->getTablename()).isEmpty()) {
|
||
|
for (int var = 0; var < toInitialize.size(); ++var) {
|
||
|
QString c = toInitialize.at(var);
|
||
|
this->createTable(QSharedPointer<Entity>(EntityInstanceFactory::createInstance(c)));
|
||
|
}
|
||
|
dbm->setVersion(version);
|
||
|
dbm->setApplyTime(QDateTime::currentDateTime());
|
||
|
this->create(ptrDbm);
|
||
|
}
|
||
|
delete dbm;
|
||
|
}
|
||
|
|
||
|
EntityManager::EntityManager(const QString &databaseType, QString databasename , QString hostname, QString username,
|
||
| src/querybuilder.cpp | ||
|---|---|---|
|
#include <QMetaProperty>
|
||
|
#include "entity.h"
|
||
|
#include <QRegularExpression>
|
||
|
#include "entityinstancefactory.h"
|
||
|
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
| ... | ... | |
|
h.insert("id", this->schema.data()->TYPE_BIGPK);
|
||
|
h.insert(QString(entity.data()->metaObject()->className()) + QString("_id"), this->schema.data()->TYPE_BIGINT);
|
||
|
auto m = props.value(r.getPropertyName());
|
||
|
Entity *e = this->createInstance(m.type());
|
||
|
Entity *e = EntityInstanceFactory::createInstance(m.type());
|
||
|
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(e);
|
||
|
h.insert(QString(ptr.data()->metaObject()->className()) + QString("_id"),
|
||
|
this->schema.data()->TYPE_BIGINT);
|
||
| src/querybuilder.h | ||
|---|---|---|
|
QString where(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false) const;
|
||
|
QString attributes(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false) const;
|
||
|
QHash<QString, QVariant> saveAttributes(const QSharedPointer<Entity> &entity) const;
|
||
|
Entity* createInstance(const char *className) const;
|
||
|
Entity* createInstance(int id) const;
|
||
|
|
||
|
QSharedPointer<Schema> schema;
|
||
|
QSharedPointer<Database> database;
|
||
Auch abrufbar als: Unified diff
example compiles, persisting of entities without relations works, added
debug output for queries