Herunterladen als
root/src/entitymanager.cpp @ 9d05e414
81c23b56 | Christian Ehringfeld | /*
|
|
6899f814 | Christian Ehringfeld | * Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
81c23b56 | Christian Ehringfeld | ||
#include "entitymanager.h"
|
|||
4d58ef6a | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
81c23b56 | Christian Ehringfeld | /**
|
|
* Relationen fehlen noch
|
|||
* Fehlermeldungen erstellen am besten eine Exception Klasse diesbzgl. erstellen
|
|||
*/
|
|||
QStringList EntityManager::connectionNames = QStringList();
|
|||
9d05e414 | Christian Ehringfeld | //EntityManager::EntityManager(QSqlDatabase database) {
|
|
// auto db = new Database(database);
|
|||
// this->db = QSharedPointer<Database>(db);
|
|||
81c23b56 | Christian Ehringfeld | ||
9d05e414 | Christian Ehringfeld | // auto schema = DatabaseType::getSchema(DatabaseType::getDatabaseType(database.driver()),this->db);
|
|
// this->schema = QSharedPointer<Schema>(db);
|
|||
//}
|
|||
//EntityManager::EntityManager(const QString &databaseType, QString databasename , QString hostname, QString username,
|
|||
// QString password, QString port) {
|
|||
// auto db = new Database(databaseType, this->createConnection(), hostname, databasename, username, password,
|
|||
// port.toInt());
|
|||
// this->db = QSharedPointer<Database>(db);
|
|||
// auto schema = new Schema(this->db);
|
|||
// this->schema = QSharedPointer<Schema>(db);
|
|||
//}
|
|||
81c23b56 | Christian Ehringfeld | ||
9d05e414 | Christian Ehringfeld | //inline bool EntityManager::checkTable(Entity *entity) {
|
|
14f9beed | Christian Ehringfeld | // bool rc = true;
|
|
// if (!this->db->containsTable(entity->getTablename())) {
|
|||
// qDebug() << "Tabelle" << entity->getTablename() << "existiert noch nicht.";
|
|||
// if (this->createTable(entity)) {
|
|||
// this->db->refreshTableList();
|
|||
// rc = this->db->containsTable(entity->getTablename());
|
|||
// }
|
|||
// }
|
|||
// return rc;
|
|||
9d05e414 | Christian Ehringfeld | //}
|
|
//QSharedPointer<Database> EntityManager::getDb() const {
|
|||
// return db;
|
|||
//}
|
|||
//void EntityManager::setDb(const QSharedPointer<Database> &value) {
|
|||
// db = value;
|
|||
//}
|
|||
//QSharedPointer<Schema> EntityManager::getSchema() const {
|
|||
// return schema;
|
|||
//}
|
|||
//void EntityManager::setSchema(const QSharedPointer<Schema> &value) {
|
|||
// schema = value;
|
|||
//}
|
|||
//QString EntityManager::createConnection() {
|
|||
// QStringList l = EntityManager::getConnectionNames();
|
|||
// QString conName = "";
|
|||
// bool ok = false;
|
|||
// qint16 i = 0;
|
|||
// while (!ok) {
|
|||
// if (l.contains("con" + QString::number(i))) {
|
|||
// ++i;
|
|||
// } else {
|
|||
// l.append("con" + QString::number(i));
|
|||
// ok = true;
|
|||
// conName = "con" + QString::number(i);
|
|||
// EntityManager::setConnectionNames(l);
|
|||
// }
|
|||
// }
|
|||
// return conName;
|
|||
//}
|
|||
//void EntityManager::removeConnectionName(const QString &name) {
|
|||
// EntityManager::connectionNames.removeOne(name);
|
|||
//}
|
|||
//EntityManager::~EntityManager() {
|
|||
// EntityManager::removeConnectionName(this->db->getConnectionName());
|
|||
// delete db;
|
|||
//}
|
|||
//QStringList EntityManager::getConnectionNames() {
|
|||
// return EntityManager::connectionNames;
|
|||
//}
|
|||
//void EntityManager::bindValues(const QHash<QString, QVariant> *h, QSqlQuery &q, bool ignoreID) {
|
|||
// QHash<QString, QVariant>::const_iterator i = h->constBegin();
|
|||
// while (i != h->constEnd()) {
|
|||
// if (!ignoreID || (ignoreID && !(i.key() == "id"))) {
|
|||
// q.bindValue(":" + i.key(), i.value());
|
|||
// }
|
|||
// ++i;
|
|||
// }
|
|||
//}
|
|||
//qint8 EntityManager::count(Entity *entity, bool ignoreID) {
|
|||
b0b8dac3 | Christian Ehringfeld | // qint8 rc = 0;
|
|
9d05e414 | Christian Ehringfeld | // QSqlQuery q = this->db->getQuery("SELECT COUNT(*) FROM " + entity->getTablename() + this->where(
|
|
// entity->getAttributeValues(), "AND", ignoreID));
|
|||
// this->bindValues(entity->getAttributeValues(), q);
|
|||
b0b8dac3 | Christian Ehringfeld | // this->db->select(q);
|
|
9d05e414 | Christian Ehringfeld | // if (q.next()) {
|
|
b0b8dac3 | Christian Ehringfeld | // rc = q.value(0).toInt();
|
|
// }
|
|||
// return rc;
|
|||
//}
|
|||
9d05e414 | Christian Ehringfeld | //QString EntityManager::buildCreateQuery(QHash<QString, QVariant>::const_iterator i,
|
|
// QHash<QString, QVariant>::const_iterator end, QString &p1, QString &p2) {
|
|||
b0b8dac3 | Christian Ehringfeld | // bool first = true;
|
|
9d05e414 | Christian Ehringfeld | // while (i != end) {
|
|
// if (!first) {
|
|||
b0b8dac3 | Christian Ehringfeld | // p1 += ",";
|
|
// p2 += ",";
|
|||
// } else {
|
|||
// first = false;
|
|||
// }
|
|||
// p1 += i.key();
|
|||
9d05e414 | Christian Ehringfeld | // p2 += ":" + i.key();
|
|
b0b8dac3 | Christian Ehringfeld | // ++i;
|
|
// }
|
|||
// p1 += ")";
|
|||
// p2 += ");";
|
|||
// return p1 + p2;
|
|||
//}
|
|||
//bool EntityManager::create(Entity *entity) {
|
|||
// bool rc = false;
|
|||
9d05e414 | Christian Ehringfeld | // if (this->checkTable(entity) && this->count(entity) == 0) {
|
|
b0b8dac3 | Christian Ehringfeld | // QSqlQuery q = this->db->getQuery();
|
|
// QString p1 = "INSERT INTO " + entity->getTablename() + "(";
|
|||
// QString p2 = "VALUES(";
|
|||
// entity->setId(this->db->getLastId());
|
|||
9d05e414 | Christian Ehringfeld | // if (!entity->getAttributeValues()->isEmpty()) {
|
|
// q.prepare(this->buildCreateQuery(entity->getAttributeValues()->constBegin(), entity->getAttributeValues()->constEnd(),
|
|||
// p1, p2));
|
|||
b0b8dac3 | Christian Ehringfeld | // }
|
|
9d05e414 | Christian Ehringfeld | // this->bindValues(entity->getAttributeValues(), q);
|
|
// if (this->db->updateSequenceCounter(q)) {
|
|||
b0b8dac3 | Christian Ehringfeld | // entity->setId(this->findId(entity));
|
|
// rc = true;
|
|||
// }
|
|||
// }
|
|||
// return rc;
|
|||
//}
|
|||
9d05e414 | Christian Ehringfeld | //QHash<QString, QVariant> EntityManager::find(qint64 id, QString tblname) {
|
|
// QSqlQuery q = this->db->getQuery("SELECT * FROM " + tblname + " WHERE id= :id LIMIT 1;");
|
|||
// q.bindValue(":id", id);
|
|||
b0b8dac3 | Christian Ehringfeld | // this->db->select(q);
|
|
// QSqlRecord rec = q.record();
|
|||
9d05e414 | Christian Ehringfeld | // QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|
// if (q.next()) {
|
|||
b0b8dac3 | Christian Ehringfeld | // for (int var = 0; var < rec.count(); ++var) {
|
|
9d05e414 | Christian Ehringfeld | // map.insert(rec.fieldName(var), q.value(rec.indexOf(rec.fieldName(
|
|
// var)))); // parameters of q.value can maybe replaced with var
|
|||
b0b8dac3 | Christian Ehringfeld | // }
|
|
// }
|
|||
// return map;
|
|||
//}
|
|||
9d05e414 | Christian Ehringfeld | //QList<QHash <QString, QVariant> > EntityManager::findByAttributes(QHash<QString, QVariant> *m, const QString &tblname,
|
|
// bool ignoreID) {
|
|||
// QSqlQuery q = this->db->getQuery("SELECT * FROM " + tblname + this->where(m, "AND", ignoreID));
|
|||
// this->bindValues(m, q, true);
|
|||
b0b8dac3 | Christian Ehringfeld | // return this->convertQueryResult(q);
|
|
//}
|
|||
//QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(QSqlQuery &q) {
|
|||
9d05e414 | Christian Ehringfeld | // QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
|
b0b8dac3 | Christian Ehringfeld | // this->db->select(q);
|
|
// QSqlRecord rec = q.record();
|
|||
// QStringList l = QStringList();
|
|||
// qint16 field_count = rec.count();
|
|||
// for (int var = 0; var < field_count; ++var) {
|
|||
// l.append(rec.fieldName(var));
|
|||
// }
|
|||
9d05e414 | Christian Ehringfeld | // QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
|
// while (q.next()) {
|
|||
b0b8dac3 | Christian Ehringfeld | // for (int var = 0; var < field_count; ++var) {
|
|
9d05e414 | Christian Ehringfeld | // map.insert(l.at(var), q.value(rec.indexOf(l.at(var))));
|
|
b0b8dac3 | Christian Ehringfeld | // }
|
|
// listmap.append(map);
|
|||
// }
|
|||
// return listmap;
|
|||
//}
|
|||
9d05e414 | Christian Ehringfeld | //QList<QHash <QString, QVariant> > EntityManager::findAll(QString tblname) {
|
|
// QSqlQuery q = this->db->getQuery("SELECT * FROM " + tblname + ";");
|
|||
b0b8dac3 | Christian Ehringfeld | // return this->convertQueryResult(q);
|
|
//}
|
|||
//bool EntityManager::merge(Entity *entity) {
|
|||
9d05e414 | Christian Ehringfeld | // if (this->count(entity) == 0 && entity->getId() != -1) {
|
|
// QSqlQuery q = this->db->getQuery("UPDATE " + entity->getTablename() + " SET " + this->attributes(
|
|||
// entity->getAttributeValues()) + " WHERE id=:idM;");
|
|||
// this->bindValues(entity->getAttributeValues(), q);
|
|||
// q.bindValue(":idM", entity->getId());
|
|||
b0b8dac3 | Christian Ehringfeld | // return this->db->transaction(q);
|
|
// } else {
|
|||
// return false;
|
|||
// }
|
|||
//}
|
|||
//qint64 EntityManager::findId(Entity *entity) {
|
|||
// qint64 r = -1;
|
|||
9d05e414 | Christian Ehringfeld | // QSqlQuery q = this->db->getQuery("SELECT id FROM " + entity->getTablename() + this->where(entity->getAttributeValues(),
|
|
// "AND", true) + " LIMIT 1");
|
|||
// this->bindValues(entity->getAttributeValues(), q);
|
|||
b0b8dac3 | Christian Ehringfeld | // this->db->select(q);
|
|
9d05e414 | Christian Ehringfeld | // if (q.next()) {
|
|
b0b8dac3 | Christian Ehringfeld | // r = q.value(0).toInt();
|
|
// }
|
|||
// return r;
|
|||
//}
|
|||
//QString EntityManager::attributes(QHash<QString, QVariant> *m, QString conjunction, bool ignoreID) {
|
|||
// QString rc = "";
|
|||
9d05e414 | Christian Ehringfeld | // if (!m->isEmpty()) {
|
|
b0b8dac3 | Christian Ehringfeld | // QHash<QString, QVariant>::const_iterator i = m->constBegin();
|
|
9d05e414 | Christian Ehringfeld | // while (i != m->constEnd()) {
|
|
// if (!ignoreID || (ignoreID && !(i.key() == "id"))) {
|
|||
// if (!(rc == "")) {
|
|||
b0b8dac3 | Christian Ehringfeld | // rc += " " + conjunction + " ";
|
|
// }
|
|||
9d05e414 | Christian Ehringfeld | // rc += i.key() + "= :" + i.key();
|
|
b0b8dac3 | Christian Ehringfeld | // }
|
|
// ++i;
|
|||
// }
|
|||
// }
|
|||
// return rc;
|
|||
//}
|
|||
//QString EntityManager::where(QHash<QString, QVariant> *m, QString conjunction, bool ignoreID) {
|
|||
9d05e414 | Christian Ehringfeld | // if (m->size() == 0 || (ignoreID && m->contains("id") && m->size() == 1)) {
|
|
b0b8dac3 | Christian Ehringfeld | // return "";
|
|
// }
|
|||
9d05e414 | Christian Ehringfeld | // return " WHERE " + this->attributes(m, conjunction, ignoreID);
|
|
b0b8dac3 | Christian Ehringfeld | //}
|
|
//QString EntityManager::where(Entity *entity, QString conjunction, bool ignoreID) {
|
|||
9d05e414 | Christian Ehringfeld | // return this->where(entity->getAttributeValues(), conjunction, ignoreID);
|
|
b0b8dac3 | Christian Ehringfeld | //}
|
|
9d05e414 | Christian Ehringfeld | //QList<QHash <QString, QVariant> > EntityManager::findByAttributes(Entity *entity, bool ignoreID) {
|
|
// return this->findByAttributes(entity->getAttributeValues(), entity->getTablename(), ignoreID);
|
|||
b0b8dac3 | Christian Ehringfeld | //}
|
|
//bool EntityManager::save(Entity *entity) {
|
|||
9d05e414 | Christian Ehringfeld | // if (entity->getId() > -1) {
|
|
b0b8dac3 | Christian Ehringfeld | // return this->merge(entity);
|
|
// } else {
|
|||
// return this->create(entity);
|
|||
// }
|
|||
//}
|
|||
//bool EntityManager::remove(Entity *&entity) {
|
|||
// bool rc = false;
|
|||
// QSqlQuery q = this->db->getQuery("DELETE FROM " + entity->getTablename() + " WHERE id= :id;");
|
|||
9d05e414 | Christian Ehringfeld | // q.bindValue(":id", entity->getId());
|
|
// if (this->db->transaction(q)) {
|
|||
b0b8dac3 | Christian Ehringfeld | // delete entity;
|
|
// entity = 0;
|
|||
// rc = true;
|
|||
// }
|
|||
// return rc;
|
|||
//}
|
|||
//QString EntityManager::createTableQuery(Entity *entity) {
|
|||
// QChar c = this->db->escapeChar();
|
|||
// QHash<QString, QString> m = entity->getProperties(this->db->getDatabaseType());
|
|||
// bool first = true;
|
|||
// QString s = "CREATE TABLE IF NOT EXISTS ";
|
|||
// s.append(c).append(entity->getTablename()).append(c).append("(");
|
|||
// QHash<QString, QString>::const_iterator i = m.constBegin();
|
|||
// while (i != m.constEnd()) {
|
|||
9d05e414 | Christian Ehringfeld | // if (first) {
|
|
b0b8dac3 | Christian Ehringfeld | // first = false;
|
|
// } else {
|
|||
// s.append(',');
|
|||
// }
|
|||
// s.append(c).append(i.key()).append(c).append(" " + i.value());
|
|||
// ++i;
|
|||
// }
|
|||
// s.append(");");
|
|||
// return s;
|
|||
//}
|
|||
//bool EntityManager::createTable(Entity *entity) {
|
|||
// bool rc = false;
|
|||
// this->db->containsTable(entity->getTablename()) ? rc = true : rc = false;
|
|||
9d05e414 | Christian Ehringfeld | // if (!rc) {
|
|
b0b8dac3 | Christian Ehringfeld | // QSqlQuery q = this->db->getQuery(this->createTableQuery(entity));
|
|
9d05e414 | Christian Ehringfeld | // if (this->db->transaction(q)) {
|
|
b0b8dac3 | Christian Ehringfeld | // this->db->refreshTableList();
|
|
// rc = true;
|
|||
// }
|
|||
// }
|
|||
// return rc;
|
|||
//}
|