Revision 586bb527
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| example/main.cpp | ||
|---|---|---|
|
// qDebug() << b->metaObject()->property(var).read(b);
|
||
|
// }
|
||
|
Group *g = new Group();
|
||
|
g->setTeacher(new Person("Test","Test12345"));
|
||
|
g->setTeacherP(QSharedPointer<Person>(new Person("Max","Mustermann")));
|
||
|
for (int var = 0; var < g->metaObject()->propertyCount(); ++var) {
|
||
|
qDebug() << "Name:" << g->metaObject()->property(var).name();
|
||
|
qDebug() << "Type:" << g->metaObject()->property(var).typeName();
|
||
|
auto p = g->metaObject()->property(var).read(g);
|
||
|
if (QString(p.typeName()).contains("QList")) {
|
||
|
auto n = static_cast<QList<CuteEntityManager::Entity *>*>(p.data());
|
||
|
qDebug() << "Size:" << n->size();
|
||
|
for (int var = 0; var < n->size(); ++var) {
|
||
|
CuteEntityManager::Entity *entity = n->at(var);
|
||
|
qDebug() << entity->toString();
|
||
|
}
|
||
|
}
|
||
|
qDebug() << "Value:" << p;
|
||
|
qDebug() << p.canConvert<CuteEntityManager::Entity*>();
|
||
|
qDebug() << qvariant_cast<CuteEntityManager::Entity*>(p);
|
||
|
//p.type().canConvert(1);
|
||
|
|
||
|
|
||
|
// if (QString(p.typeName()).contains("QList")) {
|
||
|
// auto n = static_cast<QList<CuteEntityManager::Entity *>*>(p.data());
|
||
|
// qDebug() << "Size:" << n->size();
|
||
|
// for (int var = 0; var < n->size(); ++var) {
|
||
|
// CuteEntityManager::Entity *entity = n->at(var);
|
||
|
// qDebug() << entity->toString();
|
||
|
// }
|
||
|
// }
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
| example/models/group.cpp | ||
|---|---|---|
|
qDebug() << "set!!!";
|
||
|
persons = value;
|
||
|
}
|
||
|
QSharedPointer<Person> Group::getTeacherP() const
|
||
|
{
|
||
|
return teacherP;
|
||
|
}
|
||
|
|
||
|
void Group::setTeacherP(const QSharedPointer<Person> &value)
|
||
|
{
|
||
|
teacherP = value;
|
||
|
}
|
||
|
Person *Group::getTeacher() const
|
||
|
{
|
||
|
return teacher;
|
||
|
}
|
||
|
|
||
|
void Group::setTeacher(Person *value)
|
||
|
{
|
||
|
teacher = value;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
void Group::personChangedSlot() {
|
||
| example/models/group.h | ||
|---|---|---|
|
// Q_PROPERTY(QList<Person*> persons READ get_persons WRITE set_persons RESET reset_persons)
|
||
|
// BR_PROPERTY(QList<Person*>, persons, QList<Person*>())
|
||
|
Q_PROPERTY(QList<Person*> persons READ getPersons WRITE setPersons NOTIFY personsChanged)
|
||
|
Q_PROPERTY(Person* teacher READ getTeacher WRITE setTeacher)
|
||
|
Q_PROPERTY(QSharedPointer<Person> teacherP READ getTeacherP WRITE setTeacherP)
|
||
|
|
||
|
signals:
|
||
|
void personsChanged();
|
||
| ... | ... | |
|
QList<Person *> getPersons() const;
|
||
|
void setPersons(const QList<Person *> &value);
|
||
|
|
||
|
QSharedPointer<Person> getTeacherP() const;
|
||
|
void setTeacherP(const QSharedPointer<Person> &value);
|
||
|
|
||
|
Person *getTeacher() const;
|
||
|
void setTeacher(Person *value);
|
||
|
|
||
|
protected:
|
||
|
// members
|
||
|
Person* teacher;
|
||
|
QSharedPointer<Person> teacherP;
|
||
|
QList<Person*> persons;
|
||
|
QList <Person *> m_classPrefects;
|
||
|
QList <Person *> m_parentSpeakers;
|
||
| src/databasemigration.cpp | ||
|---|---|---|
|
DatabaseMigration::~DatabaseMigration() {
|
||
|
|
||
|
}
|
||
|
QString DatabaseMigration::getVersion() const
|
||
|
{
|
||
|
|
||
|
QString DatabaseMigration::getVersion() const {
|
||
|
return version;
|
||
|
}
|
||
|
|
||
|
void DatabaseMigration::setVersion(const QString &value)
|
||
|
{
|
||
|
void DatabaseMigration::setVersion(const QString &value) {
|
||
|
version = value;
|
||
|
}
|
||
|
QDateTime DatabaseMigration::getApplyTime() const
|
||
|
{
|
||
|
|
||
|
QDateTime DatabaseMigration::getApplyTime() const {
|
||
|
return applyTime;
|
||
|
}
|
||
|
|
||
|
void DatabaseMigration::setApplyTime(const QDateTime &value)
|
||
|
{
|
||
|
void DatabaseMigration::setApplyTime(const QDateTime &value) {
|
||
|
applyTime = value;
|
||
|
}
|
||
|
|
||
| src/entity.cpp | ||
|---|---|---|
|
this->id = -1;
|
||
|
}
|
||
|
|
||
|
qint64 Entity::getId() {
|
||
|
return this->id;
|
||
|
}
|
||
|
|
||
|
void Entity::setId(qint64 id) {
|
||
|
this->id = id;
|
||
|
}
|
||
|
|
||
|
QString Entity::toString() {
|
||
|
return this->getTablename() + ":" + QString::number(this->id);
|
||
|
}
|
||
| ... | ... | |
|
QStringList Entity::getTransientAttributes() {
|
||
|
return QStringList();
|
||
|
}
|
||
|
qint64 Entity::getId() const
|
||
|
{
|
||
|
return id;
|
||
|
}
|
||
|
|
||
|
void Entity::setId(const qint64 &value)
|
||
|
{
|
||
|
id = value;
|
||
|
}
|
||
|
|
||
| src/entity.h | ||
|---|---|---|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
#ifndef MODEL_H
|
||
|
#define MODEL_H
|
||
|
#ifndef ENTITY_H
|
||
|
#define ENTITY_H
|
||
|
#include <QtGlobal>
|
||
|
#include <QMap>
|
||
|
#include <QDebug>
|
||
| ... | ... | |
|
Q_OBJECT
|
||
|
Q_PROPERTY(qint64 firstName READ getId WRITE setId NOTIFY idChanged)
|
||
|
|
||
|
signals:
|
||
|
void idChanged();
|
||
|
signals:
|
||
|
void idChanged();
|
||
|
|
||
|
public:
|
||
|
Entity (QObject *parent = 0);
|
||
|
virtual qint64 getId();
|
||
|
virtual void setId(qint64 id);
|
||
|
virtual QString toString();
|
||
|
virtual ~Entity();
|
||
|
virtual QString getTablename();
|
||
| ... | ... | |
|
*/
|
||
|
virtual QStringList getTransientAttributes();
|
||
|
// virtual QMap<QString, QString> getManyToManyRelations() = 0; //Key = Table, Value = joined Table Column
|
||
|
protected:
|
||
|
virtual qint64 getId() const;
|
||
|
virtual void setId(const qint64 &value);
|
||
|
|
||
|
protected:
|
||
|
qint64 id;
|
||
|
};
|
||
|
}
|
||
|
#endif // MODEL_H
|
||
|
//Q_DECLARE_METATYPE(CuteEntityManager::Entity)
|
||
|
//Q_DECLARE_METATYPE(CuteEntityManager::Entity*)
|
||
|
#endif // ENTITY_H
|
||
| src/entitymanager.cpp | ||
|---|---|---|
|
|
||
|
#include "entitymanager.h"
|
||
|
#include "enums/databasetype.h"
|
||
|
#include <QMetaObject>
|
||
|
#include <QMetaProperty>
|
||
|
using namespace CuteEntityManager;
|
||
|
/**
|
||
|
* Relationen fehlen noch
|
||
| ... | ... | |
|
schema = value;
|
||
|
}
|
||
|
|
||
|
QHash<QString, QVariant> EntityManager::getEntityAttributes(const QSharedPointer<Entity> &entity) {
|
||
|
Entity *e = entity.data();
|
||
|
auto map = QHash<QString, QVariant>();
|
||
|
auto metaObject = e->metaObject();
|
||
|
auto transientAttrs = e->getTransientAttributes();
|
||
|
for (int var = 0; var < metaObject->propertyCount(); ++var) {
|
||
|
auto p = metaObject->property(var);
|
||
|
QString name = QString(p.name());
|
||
|
if (p.isValid() && !transientAttrs.contains(name)) {
|
||
|
QVariant v = p.read(e);
|
||
|
//Relation
|
||
|
if (v.canConvert<Entity *>()) {
|
||
|
this->insertRelationId(qvariant_cast<Entity *>(v),map,name);
|
||
|
} else if (v.canConvert<QSharedPointer<Entity>>()) {
|
||
|
this->insertRelationId(qvariant_cast<QSharedPointer<Entity>>(v).data(),map,name);
|
||
|
} else if (QString(p.typeName()).contains("QList")) {
|
||
|
/**
|
||
|
@TODO
|
||
|
//List and/or ManyToManyRelation
|
||
|
*/
|
||
|
auto n = static_cast<QList<CuteEntityManager::Entity *>*>(v.data());
|
||
|
for (int var = 0; var < n->size(); ++var) {
|
||
|
CuteEntityManager::Entity *entity = n->at(var);
|
||
|
qDebug() << entity->toString();
|
||
|
}
|
||
|
} else {
|
||
|
map.insert(name, v);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
void EntityManager::insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName) {
|
||
|
if (e && e->getId() > -1) {
|
||
|
map.insert(relName + "_id", e->getId());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
QString EntityManager::createConnection() {
|
||
|
QStringList l = EntityManager::getConnectionNames();
|
||
| ... | ... | |
|
EntityManager::connectionNames.removeOne(name);
|
||
|
}
|
||
|
|
||
|
bool EntityManager::create(QSharedPointer<Entity> &entity) {
|
||
|
|
||
|
}
|
||
|
|
||
|
EntityManager::~EntityManager() {
|
||
|
EntityManager::removeConnectionName(this->db->getConnectionName());
|
||
|
}
|
||
| ... | ... | |
|
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()) {
|
||
|
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());
|
||
|
}
|
||
| ... | ... | |
|
return p1 + p2;
|
||
|
}
|
||
|
|
||
|
//bool EntityManager::create(Entity *entity) {
|
||
|
//bool EntityManager::create(QSharedPointer<Entity> &entity) {
|
||
|
// bool rc = false;
|
||
|
// if (this->checkTable(entity) && this->count(entity) == 0) {
|
||
|
// QSqlQuery q = this->db->getQuery();
|
||
| ... | ... | |
|
// p1, p2));
|
||
|
// }
|
||
|
// this->bindValues(entity->getAttributeValues(), q);
|
||
|
// if (this->db->updateSequenceCounter(q)) {
|
||
|
// entity->setId(this->findId(entity));
|
||
|
// rc = true;
|
||
|
// }
|
||
|
// }
|
||
|
// return rc;
|
||
|
//}
|
||
| ... | ... | |
|
return map;
|
||
|
}
|
||
|
|
||
|
QList<QHash <QString, QVariant> > EntityManager::findByAttributes(QHash<QString, QVariant> *m, const QString &tblname,
|
||
|
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);
|
||
|
return this->convertQueryResult(q);
|
||
|
}
|
||
|
|
||
|
bool EntityManager::merge(QSharedPointer<Entity> &entity) {
|
||
|
|
||
|
}
|
||
|
|
||
|
QList<QHash<QString, QVariant> > EntityManager::convertQueryResult(QSqlQuery &q) {
|
||
|
QList<QHash <QString, QVariant> > listmap = QList<QHash <QString, QVariant> >();
|
||
|
this->db->select(q);
|
||
| ... | ... | |
|
// return r;
|
||
|
//}
|
||
|
|
||
|
QString EntityManager::attributes(QHash<QString, QVariant> *m, QString conjunction, bool ignoreID) {
|
||
|
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 (!m.isEmpty()) {
|
||
|
QHash<QString, QVariant>::const_iterator i = m.constBegin();
|
||
|
while (i != m.constEnd()) {
|
||
|
if (!ignoreID || (ignoreID && !(i.key() == "id"))) {
|
||
|
if (!(rc == "")) {
|
||
|
rc += " " + conjunction + " ";
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
|
||
|
QString EntityManager::where(QHash<QString, QVariant> *m, QString conjunction, bool ignoreID) {
|
||
|
if (m->size() == 0 || (ignoreID && m->contains("id") && m->size() == 1)) {
|
||
|
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);
|
||
| ... | ... | |
|
// return this->findByAttributes(entity->getAttributeValues(), entity->getTablename(), ignoreID);
|
||
|
//}
|
||
|
|
||
|
bool EntityManager::save(Entity *entity) {
|
||
|
if (entity->getId() > -1) {
|
||
|
bool EntityManager::save(QSharedPointer<Entity> &entity) {
|
||
|
if (entity.data()->getId() > -1) {
|
||
|
return this->merge(entity);
|
||
|
} else {
|
||
|
return this->create(entity);
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
|
||
|
bool EntityManager::remove(Entity *&entity) {
|
||
|
bool EntityManager::remove(QSharedPointer<Entity> &entity) {
|
||
|
bool rc = false;
|
||
|
QSqlQuery q = this->db->getQuery("DELETE FROM " + entity->getTablename() + " WHERE id= :id;");
|
||
|
q.bindValue(":id", entity->getId());
|
||
|
QSqlQuery q = this->db->getQuery("DELETE FROM " + entity.data()->getTablename() + " WHERE id= :id;");
|
||
|
q.bindValue(":id", entity.data()->getId());
|
||
|
if (this->db->transaction(q)) {
|
||
|
delete entity;
|
||
|
entity = 0;
|
||
|
entity.clear();
|
||
|
rc = true;
|
||
|
}
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
|
//QString EntityManager::createTableQuery(Entity *entity) {
|
||
|
//QString EntityManager::createTableQuery(QSharedPointer<Entity> entity) {
|
||
|
// QChar c = this->db->escapeChar();
|
||
|
// QHash<QString, QString> m = entity->getProperties(this->db->getDatabaseType());
|
||
|
// bool first = true;
|
||
| ... | ... | |
|
// return rc;
|
||
|
|
||
|
//}
|
||
|
|
||
|
void EntityManager::setConnectionNames(QStringList list) {
|
||
|
EntityManager::connectionNames = list;
|
||
|
}
|
||
| src/entitymanager.h | ||
|---|---|---|
|
#include <QtSql/QSqlField>
|
||
|
#include <QString>
|
||
|
#include <QStringList>
|
||
|
#include <QSharedPointer>
|
||
|
#include <QDebug>
|
||
|
#include "schema.h"
|
||
|
#include <QtSql/QSqlError>
|
||
| ... | ... | |
|
static void setConnectionNames(QStringList list);
|
||
|
QSharedPointer<Database> db;
|
||
|
QString createConnection();
|
||
|
QString createTableQuery(Entity *entity);
|
||
|
QString attributes(QHash<QString, QVariant> *m, QString conjunction = ",", bool ignoreID = false);
|
||
|
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(Entity *entity);
|
||
|
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);
|
||
|
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);
|
||
|
QString where(const QSharedPointer<Entity> &entity, QString conjunction = ",", bool ignoreID = false);
|
||
|
QString where(const QHash<QString, QVariant> &m, const QString &conjunction = ",", bool ignoreID = false);
|
||
|
void insertRelationId(const Entity *e, QHash<QString, QVariant> &map, QString relName);
|
||
|
|
||
|
public:
|
||
|
EntityManager(QSqlDatabase database);
|
||
| ... | ... | |
|
~EntityManager();
|
||
|
static QStringList getConnectionNames();
|
||
|
static void removeConnectionName(const QString &name);
|
||
|
bool create(Entity *entity);
|
||
|
bool save(Entity *entity);
|
||
|
qint64 findId(Entity *entity);
|
||
|
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(Entity *entity, bool ignoreID = false);
|
||
|
QList<QHash<QString, QVariant> > findByAttributes(QHash<QString, QVariant> *m, const 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(Entity *entity);
|
||
|
bool remove(Entity *&entity);
|
||
|
bool merge(QSharedPointer<Entity> &entity);
|
||
|
bool remove(QSharedPointer<Entity> &entity);
|
||
|
bool removeAll(QString tblname);
|
||
|
bool createTable(Entity *entity);
|
||
|
bool createTable(QSharedPointer<Entity> &entity);
|
||
|
qint8 count(Entity *entity, bool ignoreID = true);
|
||
|
QSharedPointer<Database> getDb() const;
|
||
|
void setDb(const QSharedPointer<Database> &value);
|
||
|
QSharedPointer<Schema> getSchema() const;
|
||
|
void setSchema(const QSharedPointer<Schema> &value);
|
||
|
QHash<QString, QVariant> getEntityAttributes(const QSharedPointer<Entity> &entity);
|
||
|
|
||
|
};
|
||
|
}
|
||
|
#endif // ENTITYMANAGER_H
|
||
Auch abrufbar als: Unified diff
update