Revision 7e233492
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| EntityManager.pro | ||
|---|---|---|
|
src/tableschema.h \
|
||
|
src/schema/pgsqlschema.h \
|
||
|
src/schema/mysqlschema.h \
|
||
|
src/querybuilder.h
|
||
|
src/querybuilder.h \
|
||
|
src/databasemigration.h
|
||
|
|
||
|
SOURCES += \
|
||
|
src/entity.cpp \
|
||
| ... | ... | |
|
src/tableschema.cpp \
|
||
|
src/schema/pgsqlschema.cpp \
|
||
|
src/schema/mysqlschema.cpp \
|
||
|
src/querybuilder.cpp
|
||
|
src/querybuilder.cpp \
|
||
|
src/databasemigration.cpp
|
||
|
|
||
|
unix {
|
||
|
target.path = /usr/lib
|
||
|
INSTALLS += target
|
||
|
}
|
||
|
CONFIG += c++11
|
||
|
CONFIG += c++14
|
||
| src/databasemigration.cpp | ||
|---|---|---|
|
#include "databasemigration.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
|
||
|
DatabaseMigration::DatabaseMigration() : Entity() {
|
||
|
|
||
|
}
|
||
|
|
||
|
DatabaseMigration::~DatabaseMigration() {
|
||
|
|
||
|
}
|
||
|
QString DatabaseMigration::getVersion() const
|
||
|
{
|
||
|
return version;
|
||
|
}
|
||
|
|
||
|
void DatabaseMigration::setVersion(const QString &value)
|
||
|
{
|
||
|
version = value;
|
||
|
}
|
||
|
QDateTime DatabaseMigration::getApplyTime() const
|
||
|
{
|
||
|
return applyTime;
|
||
|
}
|
||
|
|
||
|
void DatabaseMigration::setApplyTime(const QDateTime &value)
|
||
|
{
|
||
|
applyTime = value;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
| src/databasemigration.h | ||
|---|---|---|
|
#ifndef DATABASEMIGRATION_H
|
||
|
#define DATABASEMIGRATION_H
|
||
|
#include "entity.h"
|
||
|
#include <QString>
|
||
|
#include <QDateTime>
|
||
|
namespace CuteEntityManager {
|
||
|
class DatabaseMigration : public Entity {
|
||
|
|
||
|
Q_PROPERTY(QString version READ getVersion WRITE setVersion)
|
||
|
Q_PROPERTY(QDateTime applyTime READ getApplyTime WRITE setApplyTime)
|
||
|
|
||
|
public:
|
||
|
DatabaseMigration();
|
||
|
~DatabaseMigration();
|
||
|
QString getVersion() const;
|
||
|
void setVersion(const QString &value);
|
||
|
|
||
|
QDateTime getApplyTime() const;
|
||
|
void setApplyTime(const QDateTime &value);
|
||
|
|
||
|
private:
|
||
|
QString version;
|
||
|
QDateTime applyTime;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif // DATABASEMIGRATION_H
|
||
| src/entity.cpp | ||
|---|---|---|
|
}
|
||
|
|
||
|
QString Entity::toString() {
|
||
|
return this->getTablename()+ ":" + QString::number(this->id);
|
||
|
return this->getTablename() + ":" + QString::number(this->id);
|
||
|
}
|
||
|
|
||
|
Entity::~Entity() {
|
||
| ... | ... | |
|
QString Entity::getTablename() {
|
||
|
return QString(this->metaObject()->className());
|
||
|
}
|
||
|
|
||
|
QStringList Entity::getTransientAttributes() {
|
||
|
return QStringList();
|
||
|
}
|
||
| src/entity.h | ||
|---|---|---|
|
#include <QDebug>
|
||
|
#include <QObject>
|
||
|
#include "enums/databasetype.h"
|
||
|
#include <QStringList>
|
||
|
namespace CuteEntityManager {
|
||
|
|
||
|
class Entity : public QObject {
|
||
| ... | ... | |
|
virtual QString toString();
|
||
|
virtual ~Entity();
|
||
|
virtual QString getTablename();
|
||
|
/**
|
||
|
* You should return the names of properties which should not persisted e.g. Properties which are only exposed to qml
|
||
|
* @brief getTransientAttributes
|
||
|
* @return
|
||
|
*/
|
||
|
virtual QStringList getTransientAttributes();
|
||
|
// virtual QMap<QString, QString> getManyToManyRelations() = 0; //Key = Table, Value = joined Table Column
|
||
|
protected:
|
||
|
qint64 id;
|
||
| src/entitymanager.cpp | ||
|---|---|---|
|
*/
|
||
|
|
||
|
#include "entitymanager.h"
|
||
|
#include "enums/databasetype.h"
|
||
|
using namespace CuteEntityManager;
|
||
|
/**
|
||
|
* Relationen fehlen noch
|
||
| ... | ... | |
|
|
||
|
QStringList EntityManager::connectionNames = QStringList();
|
||
|
|
||
|
//EntityManager::EntityManager(QSqlDatabase database) {
|
||
|
// auto db = new Database(database);
|
||
|
// this->db = QSharedPointer<Database>(db);
|
||
|
|
||
|
// 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);
|
||
|
//}
|
||
|
void EntityManager::init() {
|
||
|
auto schema = CuteEntityManager::getSchema(CuteEntityManager::getDatabaseType(
|
||
|
this->db.data()->getDatabase().driverName()), this->db);
|
||
|
this->schema = QSharedPointer<Schema>(schema);
|
||
|
}
|
||
|
|
||
|
EntityManager::EntityManager(QSqlDatabase database) {
|
||
|
auto db = new Database(database);
|
||
|
this->db = QSharedPointer<Database>(db);
|
||
|
this->init();
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
this->init();
|
||
|
}
|
||
|
|
||
|
//inline bool EntityManager::checkTable(Entity *entity) {
|
||
|
// bool rc = true;
|
||
| ... | ... | |
|
// return rc;
|
||
|
//}
|
||
|
|
||
|
//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;
|
||
|
// }
|
||
|
//}
|
||
|
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());
|
||
|
}
|
||
|
|
||
|
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) {
|
||
|
// qint8 rc = 0;
|
||
| ... | ... | |
|
// 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;
|
||
|
//}
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
//bool EntityManager::create(Entity *entity) {
|
||
|
// bool rc = false;
|
||
| ... | ... | |
|
// return rc;
|
||
|
//}
|
||
|
|
||
|
//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);
|
||
|
// 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
|
||
|
// }
|
||
|
// }
|
||
|
// return map;
|
||
|
//}
|
||
|
|
||
|
//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);
|
||
|
// return this->convertQueryResult(q);
|
||
|
//}
|
||
|
|
||
|
//QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(QSqlQuery &q) {
|
||
|
// QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
||
|
// 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));
|
||
|
// }
|
||
|
// QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
||
|
// while (q.next()) {
|
||
|
// for (int var = 0; var < field_count; ++var) {
|
||
|
// map.insert(l.at(var), q.value(rec.indexOf(l.at(var))));
|
||
|
// }
|
||
|
// listmap.append(map);
|
||
|
// }
|
||
|
// return listmap;
|
||
|
//}
|
||
|
|
||
|
//QList<QHash <QString, QVariant> > EntityManager::findAll(QString tblname) {
|
||
|
// QSqlQuery q = this->db->getQuery("SELECT * FROM " + tblname + ";");
|
||
|
// return this->convertQueryResult(q);
|
||
|
//}
|
||
|
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);
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
return this->convertQueryResult(q);
|
||
|
}
|
||
|
|
||
|
QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(QSqlQuery &q) {
|
||
|
QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
||
|
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));
|
||
|
}
|
||
|
QHash<QString, QVariant> map = QHash<QString, QVariant>();
|
||
|
while (q.next()) {
|
||
|
for (int var = 0; var < field_count; ++var) {
|
||
|
map.insert(l.at(var), q.value(rec.indexOf(l.at(var))));
|
||
|
}
|
||
|
listmap.append(map);
|
||
|
}
|
||
|
return listmap;
|
||
|
}
|
||
|
|
||
|
QList<QHash <QString, QVariant> > EntityManager::findAll(QString tblname) {
|
||
|
QSqlQuery q = this->db->getQuery("SELECT * FROM " + tblname + ";");
|
||
|
return this->convertQueryResult(q);
|
||
|
}
|
||
|
|
||
|
//bool EntityManager::merge(Entity *entity) {
|
||
|
// if (this->count(entity) == 0 && entity->getId() != -1) {
|
||
| ... | ... | |
|
// return r;
|
||
|
//}
|
||
|
|
||
|
//QString EntityManager::attributes(QHash<QString, QVariant> *m, 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;
|
||
|
//}
|
||
|
|
||
|
|
||
|
//QString EntityManager::where(QHash<QString, QVariant> *m, 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::attributes(QHash<QString, QVariant> *m, 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;
|
||
|
}
|
||
|
|
||
|
|
||
|
QString EntityManager::where(QHash<QString, QVariant> *m, 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);
|
||
| ... | ... | |
|
// return this->findByAttributes(entity->getAttributeValues(), entity->getTablename(), ignoreID);
|
||
|
//}
|
||
|
|
||
|
//bool EntityManager::save(Entity *entity) {
|
||
|
// if (entity->getId() > -1) {
|
||
|
// 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;");
|
||
|
// q.bindValue(":id", entity->getId());
|
||
|
// if (this->db->transaction(q)) {
|
||
|
// delete entity;
|
||
|
// entity = 0;
|
||
|
// rc = true;
|
||
|
// }
|
||
|
// return rc;
|
||
|
//}
|
||
|
bool EntityManager::save(Entity *entity) {
|
||
|
if (entity->getId() > -1) {
|
||
|
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;");
|
||
|
q.bindValue(":id", entity->getId());
|
||
|
if (this->db->transaction(q)) {
|
||
|
delete entity;
|
||
|
entity = 0;
|
||
|
rc = true;
|
||
|
}
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
|
//QString EntityManager::createTableQuery(Entity *entity) {
|
||
|
// QChar c = this->db->escapeChar();
|
||
| src/entitymanager.h | ||
|---|---|---|
|
void bindValues(const QHash<QString, QVariant> *h, QSqlQuery &q, bool ignoreID = false);
|
||
|
|
||
|
protected:
|
||
|
void init();
|
||
|
QString where(Entity *entity, QString conjunction = ",", bool ignoreID = false);
|
||
|
QString where(QHash<QString, QVariant> *m, QString conjunction = ",", bool ignoreID = false);
|
||
|
|
||
Auch abrufbar als: Unified diff
not really productive...shit