Revision dc6b13b4
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/entity.cpp | ||
---|---|---|
return "id";
|
||
}
|
||
|
||
QHash<QString, QMetaProperty> Entity::getMetaProperties() {
|
||
QHash<QString, QMetaProperty> h = QHash<QString, QMetaProperty>();
|
||
for (int var = 0; var < this->metaObject()->propertyCount(); ++var) {
|
||
QMetaProperty m = this->metaObject()->property(var);
|
||
if (m.name() != QString("objectName") && m.isValid()) {
|
||
h.insert(m.name(), m);
|
||
}
|
||
}
|
||
return h;
|
||
}
|
||
|
||
qint64 Entity::getId() const {
|
||
return id;
|
||
}
|
src/entity.h | ||
---|---|---|
virtual QStringList getBLOBColumns();
|
||
//return value must be the exact name defined in Q_PROPERTY
|
||
virtual QString getPrimaryKey();
|
||
QHash<QString, QMetaProperty> getMetaProperties();
|
||
|
||
qint64 getId() const;
|
||
void setId(const qint64 &value);
|
src/entitymanager.cpp | ||
---|---|---|
EntityManager::connectionNames.removeOne(name);
|
||
}
|
||
|
||
bool EntityManager::create(QSharedPointer<Entity> &entity) {
|
||
QList<QSharedPointer<Entity> > EntityManager::findAllEntities(QSharedPointer<Entity> entity) {
|
||
|
||
}
|
||
|
||
EntityManager::~EntityManager() {
|
||
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
QSharedPointer<Entity> EntityManager::findEntity(QSharedPointer<Entity> entity) {
|
||
|
||
}
|
||
|
||
QStringList EntityManager::getConnectionNames() {
|
||
return EntityManager::connectionNames;
|
||
QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(const QSharedPointer<Entity> &entity,
|
||
bool ignoreID) {
|
||
|
||
}
|
||
|
||
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());
|
||
/**
|
||
* @TODO insert Relations
|
||
* @brief EntityManager::create
|
||
* @param entity
|
||
* @return
|
||
*/
|
||
bool EntityManager::create(QSharedPointer<Entity> &entity) {
|
||
bool rc = false;
|
||
if (this->checkTable(entity) && this->count(entity) == 0) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->create(entity);
|
||
rc = this->db->transaction(q);
|
||
if (rc) {
|
||
entity.data()->setId(this->schema.data()->getLastInsertID().toLongLong(&rc));
|
||
}
|
||
++i;
|
||
}
|
||
return rc;
|
||
}
|
||
|
||
//qint8 EntityManager::count(Entity *entity, bool ignoreID) {
|
||
// qint8 rc = 0;
|
||
// QSqlQuery q = this->db->getQuery("SELECT COUNT(*) FROM " + entity->getTablename() + this->where(
|
||
// entity->getAttributeValues(), "AND", ignoreID));
|
||
// this->bindValues(entity->getAttributeValues(), q);
|
||
// this->db->select(q);
|
||
// if (q.next()) {
|
||
// rc = q.value(0).toInt();
|
||
// }
|
||
// return rc;
|
||
//}
|
||
|
||
QString EntityManager::buildCreateQuery(QHash<QString, QVariant>::const_iterator i,
|
||
QHash<QString, QVariant>::const_iterator end, QString &p1, QString &p2) {
|
||
bool first = true;
|
||
while (i != end) {
|
||
if (!first) {
|
||
p1 += ",";
|
||
p2 += ",";
|
||
} else {
|
||
first = false;
|
||
}
|
||
p1 += i.key();
|
||
p2 += ":" + i.key();
|
||
++i;
|
||
}
|
||
p1 += ")";
|
||
p2 += ");";
|
||
return p1 + p2;
|
||
EntityManager::~EntityManager() {
|
||
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
}
|
||
|
||
//bool EntityManager::create(QSharedPointer<Entity> &entity) {
|
||
// bool rc = false;
|
||
// if (this->checkTable(entity) && this->count(entity) == 0) {
|
||
// QSqlQuery q = this->db->getQuery();
|
||
// QString p1 = "INSERT INTO " + entity->getTablename() + "(";
|
||
// QString p2 = "VALUES(";
|
||
// entity->setId(this->db->getLastId());
|
||
// if (!entity->getAttributeValues()->isEmpty()) {
|
||
// q.prepare(this->buildCreateQuery(entity->getAttributeValues()->constBegin(), entity->getAttributeValues()->constEnd(),
|
||
// p1, p2));
|
||
// }
|
||
// this->bindValues(entity->getAttributeValues(), q);
|
||
// }
|
||
// return rc;
|
||
//}
|
||
QStringList EntityManager::getConnectionNames() {
|
||
return EntityManager::connectionNames;
|
||
}
|
||
|
||
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);
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(id, tblname);
|
||
this->db->select(q);
|
||
QSqlRecord rec = q.record();
|
||
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
||
if (q.next()) {
|
||
for (int var = 0; var < rec.count(); ++var) {
|
||
map.insert(rec.fieldName(var), q.value(rec.indexOf(rec.fieldName(
|
||
var)))); // parameters of q.value can maybe replaced with var
|
||
QVariant variant = rec.value(var);
|
||
if (variant.isValid()) {
|
||
map.insert(rec.fieldName(var), variant);
|
||
}
|
||
}
|
||
}
|
||
return map;
|
||
}
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::findByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(entity, ignoreID);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
|
||
QList<QHash <QString, QVariant> > EntityManager::findByAttributes(const 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);
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findByAttributes(m,
|
||
tblname, ignoreID);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
|
||
bool EntityManager::merge(QSharedPointer<Entity> &entity) {
|
||
|
||
/**
|
||
* @TODO insert Relations
|
||
* @brief EntityManager::merge
|
||
* @param entity
|
||
* @param withRelations
|
||
* @return
|
||
*/
|
||
bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
|
||
if (this->count(entity) == 0 && entity->getId() != -1) {
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->merge(entity);
|
||
return this->db->transaction(q);
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(QSqlQuery &q) {
|
||
QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
||
this->db->select(q);
|
||
... | ... | |
}
|
||
|
||
QList<QHash <QString, QVariant> > EntityManager::findAll(QString tblname) {
|
||
QSqlQuery q = this->db->getQuery("SELECT * FROM " + tblname + ";");
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findAll(tblname);
|
||
return this->convertQueryResult(q);
|
||
}
|
||
|
||
//bool EntityManager::merge(Entity *entity) {
|
||
// 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());
|
||
// return this->db->transaction(q);
|
||
// } else {
|
||
// return false;
|
||
// }
|
||
//}
|
||
|
||
|
||
//qint64 EntityManager::findId(Entity *entity) {
|
||
// qint64 r = -1;
|
||
// QSqlQuery q = this->db->getQuery("SELECT id FROM " + entity->getTablename() + this->where(entity->getAttributeValues(),
|
||
// "AND", true) + " LIMIT 1");
|
||
// this->bindValues(entity->getAttributeValues(), q);
|
||
// this->db->select(q);
|
||
// if (q.next()) {
|
||
// r = q.value(0).toInt();
|
||
// }
|
||
// return r;
|
||
//}
|
||
|
||
QString EntityManager::attributes(const QHash<QString, QVariant> &m, const QString &conjunction, bool ignoreID) {
|
||
QString rc = "";
|
||
if (!m.isEmpty()) {
|
||
QHash<QString, QVariant>::const_iterator i = m.constBegin();
|
||
while (i != m.constEnd()) {
|
||
if (!ignoreID || (ignoreID && !(i.key() == "id"))) {
|
||
if (!(rc == "")) {
|
||
rc += " " + conjunction + " ";
|
||
}
|
||
rc += i.key() + "= :" + i.key();
|
||
}
|
||
++i;
|
||
}
|
||
}
|
||
return rc;
|
||
QHash<QString, QVariant> EntityManager::find(QSharedPointer<Entity> entity) {
|
||
return this->find(entity.data()->getId(), entity.data()->getTablename());
|
||
}
|
||
|
||
|
||
QString EntityManager::where(const QHash<QString, QVariant> &m, const QString &conjunction, bool ignoreID) {
|
||
if (m.size() == 0 || (ignoreID && m.contains("id") && m.size() == 1)) {
|
||
return "";
|
||
}
|
||
return " WHERE " + this->attributes(m, conjunction, ignoreID);
|
||
}
|
||
|
||
//QString EntityManager::where(Entity *entity, QString conjunction, bool ignoreID) {
|
||
// return this->where(entity->getAttributeValues(), conjunction, ignoreID);
|
||
//}
|
||
|
||
//QList<QHash <QString, QVariant> > EntityManager::findByAttributes(Entity *entity, bool ignoreID) {
|
||
// return this->findByAttributes(entity->getAttributeValues(), entity->getTablename(), ignoreID);
|
||
//}
|
||
|
||
bool EntityManager::save(QSharedPointer<Entity> &entity) {
|
||
if (entity.data()->getId() > -1) {
|
||
return this->merge(entity);
|
||
... | ... | |
}
|
||
}
|
||
|
||
qint64 EntityManager::findId(QSharedPointer<Entity> &entity) {
|
||
qint64 r = -1;
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->findId(entity);
|
||
this->db->select(q);
|
||
if (q.next()) {
|
||
r = q.value(0).toLongLong();
|
||
}
|
||
return r;
|
||
}
|
||
|
||
|
||
bool EntityManager::remove(QSharedPointer<Entity> &entity) {
|
||
bool rc = false;
|
||
QSqlQuery q = this->db->getQuery("DELETE FROM " + entity.data()->getTablename() + " WHERE id= :id;");
|
||
q.bindValue(":id", entity.data()->getId());
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->remove(entity);
|
||
if (this->db->transaction(q)) {
|
||
entity.clear();
|
||
rc = true;
|
||
... | ... | |
return this->schema.data()->getQueryBuilder().data()->createTable(entity);
|
||
}
|
||
|
||
qint8 EntityManager::count(const QSharedPointer<Entity> &entity, bool ignoreID) {
|
||
qint8 rc = -1;
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->count(entity, ignoreID);
|
||
this->db->select(q);
|
||
if (q.next()) {
|
||
rc = q.value(0).toInt();
|
||
}
|
||
return rc;
|
||
}
|
||
|
||
qint8 EntityManager::count(const QString &tableName) {
|
||
qint8 rc = -1;
|
||
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->count(tableName);
|
||
this->db->select(q);
|
||
if (q.next()) {
|
||
rc = q.value(0).toInt();
|
||
}
|
||
return rc;
|
||
}
|
||
|
||
void EntityManager::setConnectionNames(QStringList list) {
|
||
EntityManager::connectionNames = list;
|
||
}
|
src/entitymanager.h | ||
---|---|---|
QSharedPointer<Database> db;
|
||
QString createConnection();
|
||
QString createTableQuery(const QSharedPointer<Entity> &entity);
|
||
QString attributes(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > convertQueryResult(QSqlQuery &q);
|
||
bool checkTable(const QSharedPointer<Entity> &entity);
|
||
QString buildCreateQuery(QHash<QString, QVariant>::const_iterator i, QHash<QString, QVariant>::const_iterator end,
|
||
QString &p1, QString &p2);
|
||
void bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q, bool ignoreID = false);
|
||
|
||
protected:
|
||
void init();
|
||
QString where(const QSharedPointer<Entity> &entity, QString conjunction = ",", bool ignoreID = false);
|
||
QString where(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > findAll(QString tblname);
|
||
QHash<QString, QVariant> find(QSharedPointer<Entity> entity);
|
||
QHash<QString, QVariant> find(qint64 id, QString tblname);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QHash<QString, QVariant> &m, const QString &tblname,
|
||
bool ignoreID = false);
|
||
|
||
public:
|
||
EntityManager(QSqlDatabase database);
|
||
... | ... | |
~EntityManager();
|
||
static QStringList getConnectionNames();
|
||
static void removeConnectionName(const QString &name);
|
||
QList<QSharedPointer<Entity>> findAllEntities(QSharedPointer<Entity> entity);
|
||
QSharedPointer<Entity> findEntity(QSharedPointer<Entity> entity);
|
||
QList<QSharedPointer<Entity>> findEntityByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
|
||
|
||
bool create(QSharedPointer<Entity> &entity);
|
||
bool save(QSharedPointer<Entity> &entity);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
QList<QHash<QString, QVariant> > findAll(QString tblname);
|
||
QHash<QString, QVariant> find(qint64 id, QString tblname);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QHash<QString, QVariant> &m, const QString &tblname,
|
||
bool ignoreID = false);
|
||
bool merge(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withManyToOne = true);
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(const QSharedPointer<Entity> &entity);
|
||
qint8 count(Entity *entity, bool ignoreID = true);
|
||
qint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
|
||
qint8 count(const QString &tableName);
|
||
QSharedPointer<Database> getDb() const;
|
||
void setDb(const QSharedPointer<Database> &value);
|
||
QSharedPointer<Schema> getSchema() const;
|
src/entitymanager.h.autosave | ||
---|---|---|
/*
|
||
* 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/>.
|
||
*/
|
||
|
||
#ifndef ENTITYMANAGER_H
|
||
#define ENTITYMANAGER_H
|
||
#include <QtSql/QSqlDatabase>
|
||
#include <QtSql/QSqlRecord>
|
||
#include <QtSql/QSqlField>
|
||
#include <QString>
|
||
#include <QStringList>
|
||
#include <QSharedPointer>
|
||
#include <QDebug>
|
||
#include "schema.h"
|
||
#include <QtSql/QSqlError>
|
||
#include "entity.h"
|
||
#include "database.h"
|
||
|
||
namespace CuteEntityManager {
|
||
|
||
class EntityManager {
|
||
signals:
|
||
void actionFinished(qint64 id);
|
||
|
||
private:
|
||
static QStringList connectionNames;
|
||
QSharedPointer<Schema> schema;
|
||
static void setConnectionNames(QStringList list);
|
||
QSharedPointer<Database> db;
|
||
QString createConnection();
|
||
QString createTableQuery(const QSharedPointer<Entity> &entity);
|
||
QList<QHash<QString, QVariant> > convertQueryResult(QSqlQuery &q);
|
||
bool checkTable(const QSharedPointer<Entity> &entity);
|
||
|
||
protected:
|
||
void init();
|
||
QList<QHash<QString, QVariant> > findAll(QString tblname);
|
||
QHash<QString, QVariant> find(QSharedPointer<Entity> entity);
|
||
QHash<QString, QVariant> find(qint64 id, QString tblname);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
QList<QHash<QString, QVariant> > findByAttributes(const QHash<QString, QVariant> &m, const QString &tblname,
|
||
bool ignoreID = false);
|
||
|
||
public:
|
||
EntityManager(QSqlDatabase database);
|
||
EntityManager(const QString &databaseType, QString databasename = "" , QString hostname = "", QString username = "",
|
||
QString password = "", QString port = "");
|
||
~EntityManager();
|
||
static QStringList getConnectionNames();
|
||
static void removeConnectionName(const QString &name);
|
||
QList<QSharedPointer<Entity>> findAllEntities(QSharedPointer<Entity> entity);
|
||
QSharedPointer<Entity> findEntity(QSharedPointer<Entity> entity);
|
||
QList<QSharedPointer<Entity>> findEntityByAttributes(const QSharedPointer<Entity> &entity, bool ignoreID = false);
|
||
|
||
|
||
bool create(QSharedPointer<Entity> &entity);
|
||
bool save(QSharedPointer<Entity> &entity);
|
||
qint64 findId(QSharedPointer<Entity> &entity);
|
||
bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
|
||
bool remove(QSharedPointer<Entity> &entity);
|
||
bool removeAll(QString tblname);
|
||
bool createTable(const QSharedPointer<Entity> &entity);
|
||
qint8 count(const QSharedPointer<Entity> &entity, bool ignoreID = true);
|
||
qint8 count(const QString &tableName);
|
||
QSharedPointer<Database> getDb() const;
|
||
void setDb(const QSharedPointer<Database> &value);
|
||
QSharedPointer<Schema> getSchema() const;
|
||
void setSchema(const QSharedPointer<Schema> &value);
|
||
|
||
};
|
||
}
|
||
#endif // ENTITYMANAGER_H
|
src/querybuilder.cpp | ||
---|---|---|
database = value;
|
||
}
|
||
|
||
|
||
QHash<QString, QMetaProperty> QueryBuilder::getMetaProperties(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QMetaProperty> h = QHash<QString, QMetaProperty>();
|
||
for (int var = 0; var < entity.data()->metaObject()->propertyCount(); ++var) {
|
||
QMetaProperty m = entity.data()->metaObject()->property(var);
|
||
if (m.name() != QString("objectName") && m.isValid()) {
|
||
h.insert(m.name(), m);
|
||
}
|
||
}
|
||
return h;
|
||
}
|
||
|
||
QString QueryBuilder::buildCreateQuery(QHash<QString, QVariant>::const_iterator i,
|
||
QHash<QString, QVariant>::const_iterator end,
|
||
QString &p1, QString &p2) const {
|
||
bool first = true;
|
||
while (i != end) {
|
||
if (!first) {
|
||
p1 += ",";
|
||
p2 += ",";
|
||
} else {
|
||
first = false;
|
||
}
|
||
p1 += this->schema.data()->quoteColumnName(i.key());
|
||
p2 += ":" + i.key();
|
||
++i;
|
||
}
|
||
p1 += ")";
|
||
p2 += ");";
|
||
return p1 + p2;
|
||
}
|
||
|
||
QHash<QString, QString> QueryBuilder::generateTableDefinition(const QSharedPointer<Entity> &entity) const {
|
||
auto map = QHash<QString, QString>();
|
||
auto o = entity.data()->metaObject();
|
||
QHash<QString, Relation> relations = entity.data()->getRelations();
|
||
QHash<QString, QSharedPointer<Entity>> relationObjects = entity.data()->getRelationObjects();
|
||
for (int var = 0; var < o->propertyCount(); ++var) {
|
||
o->property(var);
|
||
auto m = o->property(var);
|
||
... | ... | |
return type;
|
||
}
|
||
|
||
/**
|
||
* @brief QueryBuilder::find
|
||
* @param id
|
||
* @param tableName
|
||
* @return
|
||
*/
|
||
QSqlQuery QueryBuilder::find(const qint64 &id, const QString &tableName) const {
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT * FROM " + this->schema.data()->quoteTableName(
|
||
tableName) + " WHERE id= :id LIMIT 1;");
|
||
q.bindValue(":id", id);
|
||
return q;
|
||
}
|
||
|
||
/**
|
||
* @brief QueryBuilder::findByAttributes
|
||
* @param m
|
||
* @param tableName
|
||
* @param ignoreID
|
||
* @return
|
||
*/
|
||
QSqlQuery QueryBuilder::findByAttributes(const QHash<QString, QVariant> &m, const QString &tableName,
|
||
const bool &ignoreID) const {
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT * FROM " + this->schema.data()->quoteTableName(
|
||
tableName) + this->where(m, "AND", ignoreID));
|
||
this->bindValues(m, q, ignoreID);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::findByAttributes(const QSharedPointer<Entity> &e, bool ignoreID) {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(this->getMetaProperties(e), e);
|
||
return this->findByAttributes(values, e.data()->getTablename(), ignoreID);
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::findAll(const QString &tableName) const {
|
||
return this->database->getQuery("SELECT * FROM " + this->schema.data()->quoteTableName(
|
||
tableName) + ";");
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::remove(const QSharedPointer<Entity> &entity) const {
|
||
QSqlQuery q = this->database->getQuery("DELETE FROM " + this->schema.data()->quoteTableName(
|
||
entity.data()->getTablename()) + " WHERE " + this->schema.data()->quoteColumnName("id") + "=:id;");
|
||
q.bindValue(":id", entity.data()->getId());
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::findId(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(this->getMetaProperties(entity), entity);
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT " + this->schema.data()->quoteColumnName("id") + " FROM " +
|
||
this->schema.data()->quoteTableName(
|
||
entity.data()->getTablename()) + this->where(values,
|
||
"AND", true) + " LIMIT 1");
|
||
this->bindValues(values, q);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::count(const QSharedPointer<Entity> &entity, bool ignoreID) const {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(this->getMetaProperties(entity), entity);
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT COUNT(*) FROM " + this->schema.data()->quoteTableName(
|
||
entity.data()->getTablename()) + this->where(
|
||
values, "AND", ignoreID));
|
||
this->bindValues(values, q, ignoreID);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::count(const QString &tableName) const {
|
||
QSqlQuery q = this->database.data()->getQuery("SELECT COUNT(*) FROM " + this->schema.data()->quoteTableName(
|
||
tableName) + ";");
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::merge(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QVariant> values = this->saveAttributes(entity);
|
||
QSqlQuery q = this->database.data()->getQuery("UPDATE " + this->schema.data()->quoteTableName(
|
||
entity->getTablename()) + " SET " + this->attributes(
|
||
values) + " WHERE " + this->schema.data()->quoteColumnName("id") + "=:id;");
|
||
this->bindValues(values, q);
|
||
//q.bindValue(":idM", entity->getId());
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::create(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QVariant> values = this->saveAttributes(entity);
|
||
values.remove("id");
|
||
QSqlQuery q = this->database.data()->getQuery();
|
||
QString p1 = "INSERT INTO " + this->schema.data()->quoteTableName(entity->getTablename()) + "(";
|
||
QString p2 = "VALUES(";
|
||
if (!values.isEmpty()) {
|
||
q.prepare(this->buildCreateQuery(values.constBegin(), values.constEnd(),
|
||
p1, p2));
|
||
}
|
||
this->bindValues(values, q);
|
||
return q;
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::saveAttributes(const QSharedPointer<Entity> &entity) const {
|
||
auto props = this->getMetaProperties(entity);
|
||
auto values = this->getEntityAttributes(props, entity);
|
||
auto relValues = this->getManyToOneAttributes(props, entity);
|
||
auto iterator = relValues.constBegin();
|
||
while (iterator != relValues.constEnd()) {
|
||
values.insert(iterator.key(), iterator.value());
|
||
++iterator;
|
||
}
|
||
return values;
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity) {
|
||
const QSharedPointer<Entity> &entity) const {
|
||
Entity *e = entity.data();
|
||
auto map = QHash<QString, QVariant>();
|
||
auto transientAttrs = e->getTransientAttributes();
|
||
... | ... | |
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getManyToOneAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity) {
|
||
const QSharedPointer<Entity> &entity) const {
|
||
Entity *e = entity.data();
|
||
auto map = QHash<QString, QVariant>();
|
||
auto relations = e->getRelations();
|
||
... | ... | |
return map;
|
||
}
|
||
|
||
|
||
void QueryBuilder::insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName) {
|
||
void QueryBuilder::insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName) const {
|
||
if (e && e->getId() > -1) {
|
||
map.insert(relName + "_id", e->getId());
|
||
}
|
||
... | ... | |
return r;
|
||
}
|
||
|
||
void QueryBuilder::bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q, bool ignoreID) const {
|
||
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;
|
||
}
|
||
}
|
||
|
||
QString QueryBuilder::where(const QSharedPointer<Entity> &entity, QString conjunction, bool ignoreID) const {
|
||
return this->where(this->getEntityAttributes(this->getMetaProperties(entity), entity), conjunction, ignoreID);
|
||
}
|
||
|
||
QString QueryBuilder::where(const QHash<QString, QVariant> &m, const QString &conjunction, bool ignoreID) const {
|
||
if (m.size() == 0 || (ignoreID && m.contains("id") && m.size() == 1)) {
|
||
return "";
|
||
}
|
||
return " WHERE " + this->attributes(m, conjunction, ignoreID);
|
||
}
|
||
|
||
QString QueryBuilder::attributes(const QHash<QString, QVariant> &m, const QString &conjunction, bool ignoreID) const {
|
||
QString rc = "";
|
||
if (!m.isEmpty()) {
|
||
QHash<QString, QVariant>::const_iterator i = m.constBegin();
|
||
while (i != m.constEnd()) {
|
||
if (!ignoreID || (ignoreID && !(i.key() == "id"))) {
|
||
if (!(rc == "")) {
|
||
rc += " " + conjunction + " ";
|
||
}
|
||
rc += this->schema.data()->quoteColumnName(i.key()) + "= :" + i.key();
|
||
}
|
||
++i;
|
||
}
|
||
}
|
||
return rc;
|
||
}
|
||
|
||
|
||
QSharedPointer<Schema> QueryBuilder::getSchema() const {
|
src/querybuilder.h | ||
---|---|---|
#include <QHash>
|
||
#include <QSharedPointer>
|
||
#include <QPointer>
|
||
#include <QSqlQuery>
|
||
namespace CuteEntityManager {
|
||
class Schema;
|
||
class Entity;
|
||
... | ... | |
virtual QString createIndex(QString name, QString tableName, QStringList columns, bool unique)const;
|
||
virtual QString dropIndex(QString name, QString tableName)const;
|
||
QHash<QString, QVariant> getEntityAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity);
|
||
const QSharedPointer<Entity> &entity) const;
|
||
|
||
QSharedPointer<Schema> getSchema() const;
|
||
void setSchema(const QSharedPointer<Schema> &value);
|
||
... | ... | |
QString transformTypeToAbstractDbType(QString typeName) const;
|
||
QString transformAbstractTypeToRealDbType(QString typeName) const;
|
||
QString getColumnType(const QString &type) const;
|
||
|
||
QSqlQuery find(const qint64 &id, const QString &tableName) const;
|
||
QSqlQuery findByAttributes(const QHash<QString, QVariant> &m, const QString &tableName, const bool &ignoreID) const;
|
||
QSqlQuery findByAttributes(const QSharedPointer<Entity> &e,bool ignoreID = true);
|
||
QSqlQuery findAll(const QString &tableName) const;
|
||
QSqlQuery remove(const QSharedPointer<Entity> &entity) const;
|
||
QSqlQuery findId(const QSharedPointer<Entity> &entity) const;
|
||
QSqlQuery count(const QSharedPointer<Entity> &entity, bool ignoreID) const;
|
||
QSqlQuery count(const QString &tableName) const;
|
||
QSqlQuery merge(const QSharedPointer<Entity> &entity) const;
|
||
QSqlQuery create(const QSharedPointer<Entity> &entity) const;
|
||
|
||
protected:
|
||
void insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName);
|
||
void insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName) const;
|
||
QString buildColumns(const QStringList &columns) const;
|
||
QHash<QString, QVariant> getManyToOneAttributes(const QHash<QString, QMetaProperty> &props,
|
||
const QSharedPointer<Entity> &entity);
|
||
const QSharedPointer<Entity> &entity) const;
|
||
QHash<QString, QMetaProperty> getMetaProperties(const QSharedPointer<Entity> &entity) const;
|
||
QHash<QString, QVariant> getPropertyValues(const QHash<QString, QMetaProperty> &metaProps,
|
||
const QSharedPointer<Entity> &entity) const;
|
||
QString buildCreateQuery(QHash<QString, QVariant>::const_iterator i, QHash<QString, QVariant>::const_iterator end,
|
||
QString &p1, QString &p2) const;
|
||
void bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q, bool ignoreID = false) const;
|
||
QString where(const QSharedPointer<Entity> &entity, QString conjunction = ",", bool ignoreID = false) const;
|
||
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;
|
||
|
||
QSharedPointer<Schema> schema;
|
||
QSharedPointer<Database> database;
|
Auch abrufbar als: Unified diff
not tested