Revision a47954c0
Von Christian Ehringfeld vor fast 10 Jahren hinzugefügt
example/main.cpp | ||
---|---|---|
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();
|
||
qDebug() << "Name:" << g->metaObject()->property(var).name();
|
||
// qDebug() << "Type:" << g->metaObject()->property(var).typeName();
|
||
auto p = g->metaObject()->property(var).read(g);
|
||
qDebug() << "Value:" << p;
|
||
qDebug() << p.canConvert<CuteEntityManager::Entity*>();
|
||
qDebug() << qvariant_cast<CuteEntityManager::Entity*>(p);
|
||
// qDebug() << "Value:" << p;
|
||
// qDebug() << p.canConvert<CuteEntityManager::Entity*>();
|
||
// qDebug() << qvariant_cast<CuteEntityManager::Entity*>(p);
|
||
//p.type().canConvert(1);
|
||
|
||
|
example/models/artikel.h | ||
---|---|---|
|
||
class Artikel : public CuteEntityManager::Entity {
|
||
Q_OBJECT
|
||
private:
|
||
Q_PROPERTY(double preis READ getPreis WRITE setPreis)
|
||
Q_PROPERTY(QString name READ getName WRITE setName)
|
||
private:
|
||
double preis;
|
||
QString name;
|
||
|
||
public:
|
||
public:
|
||
~Artikel();
|
||
// QHash<QString, QString> getProperties(DatabaseType type);
|
||
// PersistenceType getPersistenceType();
|
||
... | ... | |
// void setAttributes(QHash<QString, QVariant> h);
|
||
explicit Artikel(QObject *parent = 0);
|
||
Artikel(double preis, QString name);
|
||
Q_INVOKABLE double getPreis() const;
|
||
Q_INVOKABLE void setPreis(double value);
|
||
Q_INVOKABLE QString getName() const;
|
||
Q_INVOKABLE void setName(const QString &value);
|
||
double getPreis() const;
|
||
void setPreis(double value);
|
||
QString getName() const;
|
||
void setName(const QString &value);
|
||
};
|
||
#endif // ARTIKEL_H
|
example/models/group.cpp | ||
---|---|---|
|
||
#include "models/person.h"
|
||
#include "models/group.h"
|
||
#include "relation.h"
|
||
//#include <QQmlListProperty>
|
||
#include <QDebug>
|
||
|
||
... | ... | |
{
|
||
teacher = value;
|
||
}
|
||
QSharedPointer<Artikel> Group::getArtikel() const
|
||
{
|
||
return artikel;
|
||
}
|
||
|
||
void Group::setArtikel(const QSharedPointer<Artikel> &value)
|
||
{
|
||
artikel = value;
|
||
}
|
||
|
||
|
||
|
||
|
||
... | ... | |
qDebug() << "changed!";
|
||
}
|
||
|
||
QHash<QString, CuteEntityManager::Relation> Group::getRelations()
|
||
{
|
||
QHash<QString, CuteEntityManager::Relation> h = QHash<QString, CuteEntityManager::Relation>();
|
||
CuteEntityManager::Relation r = CuteEntityManager::Relation("artikel",CuteEntityManager::BELONGS_TO);
|
||
h.insert("artikel", r);
|
||
return h;
|
||
}
|
||
|
||
//void Group::appendPerson(QQmlListProperty<Person> *list, Person *p) {
|
||
// Group *group = qobject_cast<Group*>(list->object);
|
||
// if(group && p) {
|
example/models/group.h | ||
---|---|---|
#ifndef GROUP_H
|
||
#define GROUP_H
|
||
|
||
#define BR_PROPERTY(TYPE,NAME,DEFAULT) \
|
||
TYPE NAME; \
|
||
TYPE get_##NAME() const { return NAME; } \
|
||
void set_##NAME(TYPE the_##NAME) { NAME = the_##NAME; } \
|
||
void reset_##NAME() { NAME = DEFAULT; }
|
||
|
||
|
||
#include "models/person.h"
|
||
//#include <QQmlListProperty>
|
||
#include <QDebug>
|
||
#include <QList>
|
||
#include <QVariantList>
|
||
//#include <QQuickView>
|
||
#include "artikel.h"
|
||
|
||
|
||
|
||
class SeatingPlan;
|
||
class Teacher;
|
||
class Person;
|
||
|
||
class Relation;
|
||
|
||
class Group: public CuteEntityManager::Entity {
|
||
Q_OBJECT
|
||
... | ... | |
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)
|
||
Q_PROPERTY(QSharedPointer<Artikel> artikel READ getArtikel WRITE setArtikel)
|
||
|
||
signals:
|
||
void personsChanged();
|
||
... | ... | |
void personChangedSlot();
|
||
|
||
public:
|
||
virtual QHash<QString, CuteEntityManager::Relation> getRelations();
|
||
// constructor
|
||
Group();
|
||
|
||
... | ... | |
Person *getTeacher() const;
|
||
void setTeacher(Person *value);
|
||
|
||
QSharedPointer<Artikel> getArtikel() const;
|
||
void setArtikel(const QSharedPointer<Artikel> &value);
|
||
|
||
protected:
|
||
// members
|
||
Person* teacher;
|
||
QSharedPointer<Person> teacherP;
|
||
QList<Person*> persons;
|
||
QSharedPointer<Artikel> artikel;
|
||
QList <Person *> m_classPrefects;
|
||
QList <Person *> m_parentSpeakers;
|
||
|
src/entity.h | ||
---|---|---|
virtual QString toString();
|
||
virtual ~Entity();
|
||
virtual QString getTablename();
|
||
/**
|
||
* Relation with BELONGS_TO should use qint32 as primary key
|
||
* @brief getRelations
|
||
* @return
|
||
*/
|
||
virtual QHash<QString, Relation> getRelations();
|
||
/**
|
||
* You should return the names of properties which should not persisted e.g. Properties which are only exposed to qml
|
src/querybuilder.cpp | ||
---|---|---|
#include <QMetaObject>
|
||
#include <QMetaProperty>
|
||
#include "entity.h"
|
||
#include <QRegularExpression>
|
||
|
||
using namespace CuteEntityManager;
|
||
|
||
//bool QueryBuilder::createTable(QString tablename, QHash<QString, QString> tableDefinition) {
|
||
... | ... | |
} else if (relations.contains(m.name())) {
|
||
Relation r = relations.value(m.name());
|
||
if (r.getType() == RelationType::BELONGS_TO) {
|
||
//@TODO detect if id is BIGINT or only Integer
|
||
map.insert(QString(m.name()) + "_id", this->schema.data()->TYPE_INTEGER);
|
||
}
|
||
} else if (entity.data()->getBLOBColumns().contains(m.name())) {
|
||
... | ... | |
return this->schema.data()->getTypeMap().data()->value(typeName);
|
||
}
|
||
|
||
QString QueryBuilder::getColumnType(QString type) const {
|
||
QString QueryBuilder::getColumnType(const QString &type) const {
|
||
/**
|
||
* @TODO
|
||
* @WARNING
|
||
*/
|
||
return this->transformAbstractTypeToRealDbType(type);
|
||
QHash<QString, QString> *tMap = this->schema.data()->getTypeMap().data();
|
||
if (tMap->contains(type)) {
|
||
return this->transformAbstractTypeToRealDbType(type);
|
||
}
|
||
//cant believe that this could work in Qt
|
||
//https://github.com/yiisoft/yii2/blob/master/framework/db/QueryBuilder.php
|
||
QRegularExpression reg = QRegularExpression(QRegularExpression::escape("/^(\w+)\((.+?)\)(.*)$/"));
|
||
reg.optimize();
|
||
QRegularExpressionMatchIterator i = reg.globalMatch(type, 0, QRegularExpression::PartialPreferFirstMatch);
|
||
short s = 0;
|
||
bool ok = false;
|
||
QString before = "";
|
||
while (i.hasNext() && s < 2) {
|
||
before = i.next().captured();
|
||
if (tMap->contains(before)) {
|
||
ok = true;
|
||
}
|
||
if (ok) {
|
||
return before.replace(QRegularExpression::escape("/\(.+\)/"), "(" + i.next().captured() + ")");
|
||
}
|
||
s++;
|
||
}
|
||
reg = QRegularExpression(QRegularExpression::escape("/^(\w+)\s+/"));
|
||
QRegularExpressionMatchIterator i = reg.globalMatch(type, 0, QRegularExpression::PartialPreferFirstMatch);
|
||
if (i.hasNext()) {
|
||
return before.replace(QRegularExpression::escape("/^w+/"), i.next().captured());
|
||
}
|
||
return type;
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(const QSharedPointer<Entity> &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 (v.canConvert<QPointer<Entity>>()) {
|
||
this->insertRelationId(qvariant_cast<QPointer<Entity>>(v).data(), map, name);
|
||
} else if (QString(p.typeName()).contains("QList")) {
|
||
/**
|
||
@TODO
|
src/querybuilder.h | ||
---|---|---|
#include <QString>
|
||
#include <QHash>
|
||
#include <QSharedPointer>
|
||
#include <QPointer>
|
||
namespace CuteEntityManager {
|
||
class Schema;
|
||
class Entity;
|
||
... | ... | |
|
||
QString transformTypeToAbstractDbType(QString typeName) const;
|
||
QString transformAbstractTypeToRealDbType(QString typeName) const;
|
||
QString getColumnType(QString type) const;
|
||
QString getColumnType(const QString &type) const;
|
||
|
||
|
||
protected:
|
src/schema.cpp | ||
---|---|---|
|
||
QString Schema::getRawTable(QString name) {
|
||
if (name.indexOf("{{")) {
|
||
QRegularExpression re("/\\{\\{(.*?)\\}\\}/");
|
||
return name.replace(re, "\\1");
|
||
QRegularExpression re(QRegularExpression::escape("/\{\{(.*?)\}\}/"));
|
||
return name.replace(re, QRegularExpression::escape("\1"));
|
||
}
|
||
return name;
|
||
}
|
Auch abrufbar als: Unified diff
some wip