Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 373a84e2

Von Christian Ehringfeld vor mehr als 8 Jahren hinzugefügt

wip

Unterschiede anzeigen:

src/entitymanager.cpp
return schema;
}
void EntityManager::refresh(QSharedPointer<Entity> &entity) {
void EntityManager::refresh(QSharedPointer<Entity> &entity, const bool resolveRelations) {
if(entity) {
auto map = this->findByPk(entity->getId(), entity);
this->convert(map, entity, true);
this->convert(map, entity, true, resolveRelations);
}
}
......
}
QSharedPointer<Entity> EntityManager::findById(const qint64 &id,
const QString &classname) {
const QString &classname, const bool refresh) {
QSharedPointer<Entity> e = QSharedPointer<Entity>
(EntityInstanceFactory::createInstance(classname));
return this->findById(id, e);
return this->findById(id, e, refresh);
}
void EntityManager::manyToOne(const QSharedPointer<Entity> &entity,
......
QSharedPointer<Entity> ptr = QSharedPointer<Entity>();
if (refresh || !(this->cache.contains(convertedId, className)
&& (ptr = this->cache.get(convertedId, className)))) {
ptr = this->findById(convertedId, className);
ptr = this->findById(convertedId, className, refresh);
}
EntityHelper::setProperty(entity, ptr, attr->getMetaProperty());
}
......
if (entity.data() && entity->getId() > -1) {
auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(attr));
if (e) {
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(attr->getRelatedTable(),
attr->getRelatedColumnName(), entity->getId());
Query query = this->schema->getQueryBuilder()->oneToMany(
attr->getRelatedTable(),
attr->getRelatedColumnName(), entity->getId());
QSqlQuery q = this->queryInterpreter->build(query);
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
refresh);
......
} else {
auto e = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(attr));
if (e) {
QSqlQuery q = this->schema->getQueryBuilder()->oneToMany(
Query query = this->schema->getQueryBuilder()->oneToMany(
e->getTablename(),
this->schema->getQueryBuilder()->generateColumnNameID(
attr->getRelation().getMappedBy()),
entity->getProperty(entity->getPrimaryKey()).toLongLong(), 1);
QSqlQuery q = this->queryInterpreter->build(query);
auto listMap = this->convertQueryResult(q);
auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()),
refresh);
......
const QSharedPointer<Entity>
&entity,
bool ignoreID) {
QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(
Query query = this->schema->getQueryBuilder()->findByAttributes(
entity, ignoreID);
QSqlQuery q = this->queryInterpreter->build(query);
return this->convertQueryResult(q);
}
QList<QHash <QString, QVariant>> EntityManager::findAllByAttributes(
const QHash<QString, QVariant> &m, const QString &tblname, bool ignoreID) {
QSqlQuery q = this->schema->getQueryBuilder()->findByAttributes(m,
Query query = this->schema->getQueryBuilder()->findByAttributes(m,
tblname, ignoreID);
QSqlQuery q = this->queryInterpreter->build(query);
return this->convertQueryResult(q);
}
src/entitymanager.h
bool startup(QString version, QStringList toInitialize,
bool createIndices = false);
bool executeQuery(const QString &query);
QSharedPointer<Entity> findById(const qint64 &id, const QString &classname);
QSharedPointer<Entity> findById(const qint64 &id, const QString &classname, const bool refresh=false);
QList<QSharedPointer<Entity>> findEntityByAttributes(const
QSharedPointer<Entity> &entity,
bool ignoreID = false, const bool refresh = false, const bool resolveRelations = true);
......
* fetches an entity again from the database
* @param entity
*/
void refresh(QSharedPointer<Entity> &entity);
void refresh(QSharedPointer<Entity> &entity, const bool resolveRelations=true);
QList<QHash<QString, QVariant>> selectByQuery(Query &query);
QList<QHash<QString, QVariant>> selectBySql(const QString &sql);
quint32 count(Query &query);
......
&attributes, const bool joinBaseClasses = false,
const bool resolveRelations = true, const bool refresh=false) {
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0,
joinBaseClasses, resolveRelations, refresh);
joinBaseClasses, resolveRelations,refresh);
if (list.isEmpty()) {
return QSharedPointer<T>();
}
......
query.setOffset(offset);
QSqlQuery q = this->queryInterpreter->build(query);
auto results = this->convertQueryResult(q);
auto list = this->convert(results, EntityHelper::getClassname(e.data()), refresh,
resolveRelations);
auto list = this->convert(results, EntityHelper::getClassname(e.data()),refresh,resolveRelations);
return EntityManager::convertList<T>(list);
}
return QList<QSharedPointer<T>>();
src/expression.cpp
Expression::Expression(QString expression, QHash<QString, QVariant> params,
bool onlyColumn) {
for(auto i = params.begin(); i != params.end(); ++i) {
expression.replace(":" + i.key(),":" + this->generateParam());
QString ikey = i.key();
expression.replace(":" + ikey.replace('.','_'),":" + this->generateParam());
this->appendParam(i.key(),i.value());
}
this->expression = expression;
......
}
Expression::Expression(QString expression, QString key, QVariant value, bool onlyColumn) {
this->expression = expression.replace(":" + key, ":" + this->generateParam());
this->expression = expression.replace(":" + key.replace('.','_'), ":" + this->generateParam());
this->appendParam(key, value);
this->onlyColumn = onlyColumn;
}
......
void Expression::appendParam(QString key, const QVariant &value) {
this->params.insert(this->generateParam(), value);
/**
@todo remove
*/
this->params.insert(key.replace('.','_'), value);
}
src/querybuilder.cpp
}
QString QueryBuilder::selectBase(const QStringList &tables,
const QStringList &columns) const {
QString r = "SELECT ";
const QStringList &columns, bool withKeyword) const {
QString r = "";
if(withKeyword) {
r = "SELECT ";
}
if (columns.isEmpty()) {
r += "*";
} else {
......
entity) + this->limit(limit, offset) + ";");
}
QSqlQuery QueryBuilder::findByAttributes(const QHash<QString, QVariant> &m,
const QString &tableName,
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
tableName)) + this->where(m, this->andKeyword(), ignoreID) + this->limit(limit,
offset));
this->bindValues(m, q, ignoreID);
Query QueryBuilder::findByAttributes(const QHash<QString, QVariant> &m,
const QString &tableName,
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
Query q = Query();
q.setSelect(QStringList(this->selectBase(QStringList(
tableName),QStringList(),false)));
q.appendWhere(Expression(this->where(m, this->andKeyword(), ignoreID, "id", false), m));
q.setLimit(limit);
q.setOffset(offset);
// QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
// tableName)) + this->where(m, this->andKeyword(), ignoreID) + this->limit(limit,
// offset));
// this->bindValues(m, q, ignoreID);
return q;
}
QSqlQuery QueryBuilder::findByAttributes(const QSharedPointer<Entity> &e,
bool ignoreID,
const qint64 limit,
const qint64 offset) {
Query QueryBuilder::findByAttributes(const QSharedPointer<Entity> &e,
bool ignoreID,
const qint64 limit,
const qint64 offset) {
QHash<QString, QVariant> values = EntityHelper::getEntityAttributes(
EntityHelper::getMetaProperties(e.data()), e);
return this->findByAttributes(values, e->getTablename(), ignoreID, limit,
......
}
QSqlQuery QueryBuilder::oneToMany(const QString &tableName,
const QString &attribute,
const qint64 &id,
const qint64 &limit) {
Query QueryBuilder::oneToMany(const QString &tableName,
const QString &attribute,
const qint64 &id,
const qint64 &limit) {
QHash<QString, QVariant> values = QHash<QString, QVariant>();
values.insert(attribute, id);
return this->findByAttributes(values, tableName, false, limit);
......
}
QString QueryBuilder::placeHolder(QString key) const {
//return QString(":" + key.replace('.', '_'));
return QString(":" + key);
return QString(":" + key.replace('.', '_'));
}
QString QueryBuilder::where(const QHash<QString, QVariant> &m,
......
}
Expression QueryBuilder::equal(QString &key, QVariant &value) {
Expression exp = Expression(this->where(key, value, false, true, false),key,value);
Expression exp = Expression(this->where(key, value, false, true, false), key, value);
return exp;
}
src/querybuilder.h
QSqlQuery find(const qint64 &id, const QString &tableName) const;
QSqlQuery find(const qint64 &id, const QSharedPointer<Entity> &entity,
qint64 offset = 0, QString pk = "id") const;
QSqlQuery findByAttributes(const QHash<QString, QVariant> &m,
Query findByAttributes(const QHash<QString, QVariant> &m,
const QString &tableName,
const bool &ignoreID = true, const qint64 limit = 0,
const qint64 offset = 0) const;
QSqlQuery findByAttributes(const QSharedPointer<Entity> &e,
Query findByAttributes(const QSharedPointer<Entity> &e,
bool ignoreID = true,
const qint64 limit = 0, const qint64 offset = 0);
QSqlQuery findAll(const QString &tableName) const;
......
QList<QSqlQuery> merge(const QSharedPointer<Entity> &entity) const;
QList<QSqlQuery> create(const QSharedPointer<Entity> &entity) const;
QSqlQuery removeAll(const QString &tableName) const;
QSqlQuery oneToMany(const QString &tableName, const QString &attribute,
Query oneToMany(const QString &tableName, const QString &attribute,
const qint64 &id,
const qint64 &limit = 0);
QSqlQuery manyToMany(const QString &tableName, const QString &attribute,
......
QString joinSuperClasses(const QSharedPointer<Entity> &entity) const;
virtual QString selectBase(const QStringList &tables,
const QStringList &columns = QStringList()) const;
const QStringList &columns = QStringList(), bool withKeyword=true) const;
virtual QString countFunction(const QString &distinctColumn = "") const;
virtual QString distinct() const;
virtual QString notKeyword() const;
tests/em/tst_querybuilder.cpp
QCOMPARE(list.size(), 1);
QCOMPARE(list.at(0)->getNickName(), QString("Lotta"));
q = Query();
q.appendWhere(q.equal(qb, "leader", QVariant(list.at(0))));
QVariant var;
var.setValue<QSharedPointer<Employee>>(list.at(0));
q.appendWhere(q.equal(qb, "leader", var));
QList<QSharedPointer<Group>> groupList = e->find<Group>(q, false);
QCOMPARE(groupList.size(), 1);
QCOMPARE(groupList.at(0)->getName(), QString("Group Health"));
......
QCOMPARE(groupList.at(0)->getName(), QString("Group Psy"));
QCOMPARE(groupList.at(0)->getPersons().size(), 3);
}
void QuerybuilderTest::testRefresh() {
auto persons = e->findAll<Person>(false);
QCOMPARE(persons.first()->getGroups().size(), 0);
QHash<QString, QVariant> attributes;
attributes["name"] = QString("Group Health");
QSharedPointer<Group> g = this->e->findEntityByAttributes<Group>(attributes, false, false,
false);
QCOMPARE(g->getPersons().count(), 0);
auto entity = g.objectCast<Entity>();
e->refresh(entity, true);
QCOMPARE(g->getPersons().count(), 1);
}
tests/em/tst_querybuilder.h
void testQueryBuilderManyToOneRelationAttribute();
void testQueryBuilderManyToManyRelation();
void testQueryBuilderManyToManyRelationAttribute();
void testRefresh();
private:
CuteEntityManager::EntityManager *e;

Auch abrufbar als: Unified diff