Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6d91d381

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID 6d91d3817e9a022261ec6fb5935c055967ca91ac
  • Vorgänger f5087482
  • Nachfolger ac8aede7

some bug fixes, caching works

Unterschiede anzeigen:

example/main.cpp
*/
using namespace CuteEntityManager;
int main(int argc, char *argv[]) {
// Q_UNUSED(argc) Q_UNUSED(argv)
Q_UNUSED(argc) Q_UNUSED(argv)
CuteEntityManager::EntityManager *e = new
CuteEntityManager::EntityManager("QSQLITE",
QDir::currentPath() + "/db.sqlite");
QSharedPointer<Artikel> a = QSharedPointer<Artikel>(new Artikel(20.0,
"Müsli"));
auto ep = a.dynamicCast<CuteEntityManager::Entity>();
qDebug() << e;
qDebug() << "Tabelle artikel erstellt:" << e->createTable(ep);
e->create(ep);
//qDebug() << e->findById<Artikel *>(1); //not working grml!
auto artikel = e->findById<Artikel *>(1);
// QSharedPointer<CuteEntityManager::Entity> p = QSharedPointer<CuteEntityManager::Entity>(new Person("Max", "Mustermann", Person::MALE, "", "", "",
// QDate::currentDate()));
// auto pptr = p.dynamicCast<CuteEntityManager::Entity>();
......
// e->save(a);
// }
// qDebug() << "Dauer:" << t.elapsed();
//QSharedPointer<Artikel> aPtr = QSharedPointer<Artikel>(entity);
Pupil *p = new Pupil();
auto hash = p->getMetaProperties();
// auto iterator = hash.constBegin();
// while(iterator != hash.constEnd()) {
// qDebug() << iterator.key() << " Value:" << iterator.value().read(p);
// iterator++;
// }
qDebug() << p->metaObject()->superClass()->className();
return 0;
}
example/models/artikel.cpp
}
Artikel::Artikel(QObject *parent) : Entity(parent)
{
Artikel::Artikel() : Entity() {
this->preis = 0.0;
this->name = "";
}
Artikel::Artikel(double preis, QString name) : Entity(){
Artikel::Artikel(double preis, QString name) : Entity() {
this->preis = preis;
this->name = name;
}
example/models/artikel.h
#include <QHash>
#include <QVariant>
class Artikel : virtual public CuteEntityManager::Entity {
class Artikel : public CuteEntityManager::Entity {
Q_OBJECT
Q_PROPERTY(double preis READ getPreis WRITE setPreis)
Q_PROPERTY(QString name READ getName WRITE setName)
......
QString name;
public:
~Artikel();
explicit Artikel(QObject *parent = 0);
virtual ~Artikel();
Q_INVOKABLE Artikel();
Artikel(double preis, QString name);
double getPreis() const;
void setPreis(double value);
src/entity.h
void idChanged();
public:
Entity (QObject *parent = 0);
explicit Entity (QObject *parent = 0);
virtual QString toString() const;
virtual ~Entity();
virtual QString getTablename() const;
......
//return value must be the exact name defined in Q_PROPERTY
virtual QString getPrimaryKey() const;
const QStack<const QMetaObject *> superClasses(bool stopAtSingleTableInheritance = false) const;
const QStack<const QMetaObject *> superClasses(bool stopAtSingleTableInheritance
= false) const;
const QHash<QString, QMetaProperty> getMetaProperties() const;
const QHash<QString, QMetaProperty> getSuperMetaProperties() const;
static const QHash<QString, QMetaProperty> getMetaProperties(const QMetaObject* object);
static const QHash<QString, QMetaProperty> getMetaProperties(
const QMetaObject *object);
const QHash<QString, QMetaProperty> getInheritedMetaProperties() const;
const QHash<Relation, QMetaProperty> getRelationProperties() const;
const char *getClassname() const;
src/entityinstancefactory.cpp
}
Entity *EntityInstanceFactory::createInstance(const char *className) {
return EntityInstanceFactory::createInstance(QMetaType::type(className));
QString s = QString(className);
if (!s.contains("*")) {
s.append("*");
}
return EntityInstanceFactory::createInstance(QMetaType::type(
s.toUtf8().constData()));
}
Entity *EntityInstanceFactory::createInstance(const QString &className) {
......
Entity *EntityInstanceFactory::createInstance(int metaTypeId) {
Entity *e = 0;
if (metaTypeId != QMetaType::UnknownType) {
e = static_cast<Entity *>(QMetaType::create(metaTypeId));
if(!e) {
auto metaObject = QMetaType::metaObjectForType(metaTypeId);
e = static_cast<Entity *>(metaObject->newInstance());
if (!e) {
qDebug() << "Entity instance could not created!";
throw -2; //testing
}
} else {
qDebug() << metaTypeId <<" is NOT registered! Please register it!";
throw -1; //testing
throw - 2; //testing
}
} else {
qDebug() << metaTypeId << "EntityClass NOT registered! Please register it!";
throw - 1; //testing
}
return e;
}
Entity *EntityInstanceFactory::createInstance(const char *className,
const QHash<QString, QVariant> &attributes) {
Entity *e = EntityInstanceFactory::createInstance(className);
EntityInstanceFactory::setAttributes(e, attributes);
e = EntityInstanceFactory::setAttributes(e, attributes);
return e;
}
Entity *EntityInstanceFactory::setAttributes(Entity *e,
Entity *EntityInstanceFactory::setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes,
QHash<QString, QMetaProperty> metaprops) {
if (e) {
......
while (iterator != attributes.constEnd()) {
if (metaprops.contains(iterator.key())) {
QMetaProperty prop = metaprops.value(iterator.key());
if (!prop.isWritable() && !prop.write(e, iterator.value())) {
if (!(prop.isWritable() && prop.write(e, iterator.value()))) {
qDebug() << prop.name() << "on Entity" << e->getClassname() << "not writeable!";
}
}
......
return e;
}
Entity *EntityInstanceFactory::setAttributes(Entity *e,
Entity *EntityInstanceFactory::setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes) {
auto metaprops = e->getMetaProperties();
return EntityInstanceFactory::setAttributes(e, attributes, metaprops);
src/entityinstancefactory.h
static Entity *createInstance(int metaTypeId);
static Entity *createInstance(const char *className,
const QHash<QString, QVariant> &attributes);
static Entity *setAttributes(Entity *e,
static Entity *setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes,
QHash<QString, QMetaProperty> metaprops);
static Entity *setAttributes(Entity *e,
static Entity *setAttributes(Entity *&e,
const QHash<QString, QVariant> &attributes);
static const QString extractEntityType(const QString &s);
static Entity *newSuperClassInstance(const Entity *e);
src/entitymanager.cpp
const bool refresh) {
QSharedPointer<Entity> r;
if (!e.isNull()) {
if (refresh || ((r = this->cache.get(id, QString(e.data()->getClassname())))
&& !r.data())) {
auto map = this->findByPk(e);
if (refresh || !(r = this->cache.get(id, QString(e.data()->getClassname())))) {
e.data()->setId(id);
auto map = this->findByPk(id, e);
r = this->convert(map, e->getClassname(), refresh);
}
}
......
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));
entity.data()->setId(q.lastInsertId().toLongLong(&rc));
if (persistRelations) {
this->saveRelations(entity);
}
......
return EntityManager::connectionNames;
}
QHash<QString, QVariant> EntityManager::findByPk(const QSharedPointer<Entity>
QHash<QString, QVariant> EntityManager::findByPk(qint64 id,
const QSharedPointer<Entity>
&e) {
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(e);
QSqlQuery q = this->schema.data()->getQueryBuilder().data()->find(id, e);
auto listMap = this->convertQueryResult(q);
if (!listMap.isEmpty()) {
return listMap.at(0);
src/entitymanager.h
QList<QHash<QString, QVariant> > findAll(const QSharedPointer<Entity> &e);
void resolveRelations(const QSharedPointer<Entity> &entity,
const QHash<QString, QVariant> &map, const bool refresh = false);
QHash<QString, QVariant> findByPk(const QSharedPointer<Entity> &e);
QHash<QString, QVariant> findByPk(qint64 id, const QSharedPointer<Entity> &e);
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
const char *classname, const bool refresh = false);
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
......
}
template<class T> QList<QSharedPointer<Entity>> findAll() {
QSharedPointer<Entity> ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance<T>());
if (!ptr.isNull()) {
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance<T>());
if (ptr) {
auto maps = this->findAll(ptr);
const char *className = ptr.data()->getClassname();
return this->convert(maps, className);
src/querybuilder.cpp
QString r = "";
auto superMetaObject = e->metaObject()->superClass();
if (e->getInheritanceStrategy() == JOINED_TABLE
&& QString(superMetaObject->className()) != QString("CuteEntityManager::Entity")) {
qDebug() << superMetaObject->className();
&& QString(superMetaObject->className()) !=
QString("CuteEntityManager::Entity")) {
Entity *superClass = EntityInstanceFactory::createInstance(
superMetaObject->className());
if (superClass) {
......
}
}
}
if (QString(superMetaObject->className()) != QString("CuteEntityManager::Entity")
&& entity.data()->getInheritanceStrategy() != JOINED_TABLE) {
if (!(QString(superMetaObject->className()) !=
QString("CuteEntityManager::Entity")
&& entity.data()->getInheritanceStrategy() == JOINED_TABLE)) {
map.insert(entity.data()->getPrimaryKey(), this->schema.data()->TYPE_BIGPK);
}
return map;
......
return r;
}
QSqlQuery QueryBuilder::find(const QSharedPointer<Entity> &entity,
qint64 offset) const {
QSqlQuery QueryBuilder::find(const qint64 &id, const QSharedPointer<Entity> &entity, qint64 offset) const
{
QSqlQuery q = this->database.data()->getQuery(this->selectBase(QStringList(
entity.data()->getTablename())) + " " + this->joinSuperClasses(
entity) + " WHERE id= :id " + this->limit(1, offset));
q.bindValue(":id", entity.data()->getId());
entity.data()->getTablename())) + this->joinSuperClasses(
entity) + " WHERE id= :id" + this->limit(1, offset));
q.bindValue(":id", id);
return q;
}
......
const qint64 limit, qint64 offset) {
return this->database->getQuery(this->selectBase(QStringList(
entity.data()->getTablename())) + " " + this->joinSuperClasses(
entity) + " " + this->limit(limit, offset) + ";");
entity) + this->limit(limit, offset) + ";");
}
/**
......
const {
auto stack = entity.data()->superClasses();
QString joined = "";
bool first = true;
Entity *e = 0;
while (!stack.isEmpty()) {
auto metaObject = stack.pop();
e = EntityInstanceFactory::createInstance(metaObject->className());
if (first) {
first = false;
} else {
if (e) {
joined.append(" ");
}
}
if (e) {
joined.append(" ");
joined.append(this->leftJoin(e->getTablename(), entity.data()->getTablename(),
e->getPrimaryKey(), entity.data()->getPrimaryKey()));
}
src/querybuilder.h
QString transformAbstractTypeToRealDbType(QString typeName) const;
QString getColumnType(const QString &type) const;
QSqlQuery find(const qint64 &id, const QString &tableName) const;
QSqlQuery find(const QSharedPointer<Entity> &entity,
QSqlQuery find(const qint64 &id, const QSharedPointer<Entity> &entity,
qint64 offset = 0) const;
QSqlQuery findByAttributes(const QHash<QString, QVariant> &m,
const QString &tableName,
src/schema.cpp
QVariant Schema::getLastInsertID() {
QSqlQuery q(this->database.data()->getDatabase());
return q.lastInsertId();
auto lastId = q.lastInsertId();
return lastId;
}
void Schema::refresh() {

Auch abrufbar als: Unified diff