Revision 59bf3900
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
example/main.cpp | ||
---|---|---|
CuteEntityManager::EntityManager *e = new
|
||
CuteEntityManager::EntityManager("QSQLITE",
|
||
QDir::currentPath() + "/db.sqlite");
|
||
QTime t;
|
||
t.start();
|
||
QThread *entityManager = new QThread();
|
||
e->moveToThread(entityManager);
|
||
|
||
|
||
QSharedPointer<Artikel> a = QSharedPointer<Artikel>(new Artikel(20.0,
|
||
"Müsli"));
|
||
auto ep = a.dynamicCast<CuteEntityManager::Entity>();
|
||
qDebug() << "Tabelle artikel erstellt:" << e->createTable(ep);
|
||
e->create(ep);
|
||
auto artikel = e->findById<Artikel *>(1);
|
||
qDebug() << "ArtikelID:" << artikel.data()->getId();
|
||
// QSharedPointer<CuteEntityManager::Entity> p = QSharedPointer<CuteEntityManager::Entity>(new Person("Max", "Mustermann", Person::MALE, "", "", "",
|
||
// QDate::currentDate()));
|
||
// auto pptr = p.dynamicCast<CuteEntityManager::Entity>();
|
||
... | ... | |
// e->create(pptr);
|
||
//e->createTable(grp.dynamicCast<CuteEntityManager::Entity>());
|
||
|
||
QTime t;
|
||
t.start();
|
||
// e->createTable(QSharedPointer<Artikel>(new Artikel()));
|
||
for (int var = 0; var < 10000; ++var) {
|
||
EntityInstanceFactory::createInstance<Artikel*>();
|
||
// QSharedPointer<Entity> a = QSharedPointer<Artikel>(new Artikel(var,QString("a")+QString::number(var)));
|
||
// e->save(a);
|
||
}
|
||
qDebug() << "Dauer:" << t.elapsed();
|
||
|
||
//QSharedPointer<Artikel> aPtr = QSharedPointer<Artikel>(entity);
|
||
Pupil *p = new Pupil();
|
||
//Pupil *p = new Pupil();
|
||
|
||
// auto iterator = hash.constBegin();
|
||
// while(iterator != hash.constEnd()) {
|
||
// qDebug() << iterator.key() << " Value:" << iterator.value().read(p);
|
||
// iterator++;
|
||
// }
|
||
|
||
qDebug() << "Duration:" << t.elapsed();
|
||
return 0;
|
||
}
|
src/entity.cpp | ||
---|---|---|
return this->property(name.toLatin1().constData());
|
||
}
|
||
|
||
bool Entity::setProperty(const QString &name, const QVariant &value) {
|
||
return this->setProperty(name.toLatin1().constData(), value);
|
||
}
|
||
|
||
qint64 Entity::getId() const {
|
||
return id;
|
||
}
|
src/entity.h | ||
---|---|---|
const QHash<Relation, QMetaProperty> getRelationProperties() const;
|
||
const char *getClassname() const;
|
||
QVariant property(const QString &name) const;
|
||
bool setProperty(const QString &name, const QVariant &value);
|
||
|
||
qint64 getId() const;
|
||
void setId(const qint64 &value);
|
src/entityinstancefactory.cpp | ||
---|---|---|
if (!s.contains("*")) {
|
||
s.append("*");
|
||
}
|
||
int type = QMetaType::type(
|
||
s.toUtf8().constData());
|
||
return EntityInstanceFactory::createInstance(type);
|
||
return EntityInstanceFactory::createInstance(QMetaType::type(
|
||
s.toUtf8().constData()));
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::createInstance(const QString &className) {
|
||
... | ... | |
}
|
||
return super;
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::createInstance(const QMetaObject *object) {
|
||
return qobject_cast<Entity *>(object->newInstance());
|
||
}
|
src/entityinstancefactory.h | ||
---|---|---|
const QHash<QString, QVariant> &attributes);
|
||
static const QString extractEntityType(const QString &s);
|
||
static Entity *newSuperClassInstance(const Entity *e);
|
||
static Entity *createInstance(const QMetaObject *object);
|
||
|
||
template<typename T>
|
||
static Entity *createInstance() {
|
src/entitymanager.cpp | ||
---|---|---|
bool first = true;
|
||
for (int var = 0; var < q.size(); ++var) {
|
||
auto query = q.at(var);
|
||
rc = query.exec();
|
||
rc = this->db.data()->exec(query);
|
||
if (!rc) {
|
||
qDebug() << "Query failed:" << query.lastError().text();
|
||
break;
|
||
}
|
||
if (first) {
|
||
entity.data()->setProperty(entity.data()->getPrimaryKey(),
|
||
query.lastInsertId().toLongLong(&rc));
|
||
entity.data()->setProperty(
|
||
entity.data()->getPrimaryKey().toLatin1().constData(), query.lastInsertId());
|
||
first = false;
|
||
}
|
||
}
|
||
... | ... | |
bool ok = true;
|
||
for (int var = 0; var < q.size(); ++var) {
|
||
auto query = q.at(var);
|
||
ok = query.exec();
|
||
ok = this->db.data()->exec(query);
|
||
if (!ok) {
|
||
qDebug() << query.lastError().text();
|
||
break;
|
src/querybuilder.cpp | ||
---|---|---|
const QSharedPointer<Entity> &e,
|
||
QHash<QString, QMetaProperty> &usedProperties) const {
|
||
auto properties = e.data()->getMetaProperties();
|
||
auto i = QMutableHashIterator<QString, QMetaProperty>(properties);
|
||
QMutableHashIterator<QString, QMetaProperty> i(properties);
|
||
while (i.hasNext()) {
|
||
i.next();
|
||
if (usedProperties.contains(i.key()) && i.key() != e.data()->getPrimaryKey()) {
|
||
properties.remove(i.key());
|
||
} else {
|
||
usedProperties.insert(i.key(), i.value());
|
||
}
|
||
i.next();
|
||
|
||
}
|
||
return properties;
|
||
}
|
||
... | ... | |
auto relations = e.data()->getRelations();
|
||
auto i = QMutableHashIterator<QString, Relation>(relations);
|
||
while (i.hasNext()) {
|
||
i.next();
|
||
if (usedRelations.contains(i.key())) {
|
||
relations.remove(i.key());
|
||
} else {
|
||
usedRelations.insert(i.key(), i.value());
|
||
}
|
||
i.next();
|
||
}
|
||
return relations;
|
||
}
|
||
... | ... | |
auto usedProperties = QHash<QString, QMetaProperty>();
|
||
auto usedRelations = QHash<QString, Relation>();
|
||
QSharedPointer<Entity> e;
|
||
for (int var = classes.size()-1; var >= 0; --var) {
|
||
for (int var = classes.size() - 1; var >= 0; --var) {
|
||
auto metaObj = classes.at(var);
|
||
e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
||
metaObj->className()));
|
||
metaObj));
|
||
if (e) {
|
||
list.append(QueryBuilder::ClassAttributes(e.data()->getTablename(),
|
||
this->saveAttributes(entity, this->processProperties(e, usedProperties),
|
Auch abrufbar als: Unified diff
bugfix