Revision c9f21778
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/entitymanager.cpp | ||
---|---|---|
}
|
||
|
||
bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
|
||
if (entity->getId() > -1 && this->count(entity) == 1) {
|
||
if (entity->getId() > -1) {
|
||
if (withRelations) {
|
||
this->savePrePersistedRelations(entity);
|
||
}
|
||
... | ... | |
|
||
bool EntityManager::save(QSharedPointer<Entity> &entity,
|
||
const bool persistRelations) {
|
||
if (entity->getProperty(entity->getPrimaryKey()) > -1) {
|
||
if (entity->getProperty(entity->getPrimaryKey()).toLongLong() > -1) {
|
||
return this->merge(entity, persistRelations);
|
||
} else {
|
||
return this->create(entity, persistRelations);
|
||
... | ... | |
|
||
QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
|
||
&map,
|
||
const char *classname, const bool refresh) {
|
||
const char *classname, const bool refresh, const bool resolveRelations) {
|
||
auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
|
||
classname, map));
|
||
this->cache.insert(ptr);
|
||
this->resolveRelations(ptr, map, refresh);
|
||
if (resolveRelations) {
|
||
this->resolveRelations(ptr, map, refresh);
|
||
}
|
||
return ptr;
|
||
}
|
||
|
||
QList<QSharedPointer<Entity> > EntityManager::convert(
|
||
QList<QHash<QString, QVariant> > maps,
|
||
const char *classname, const bool refresh) {
|
||
const char *classname, const bool refresh, const bool resolveRelations) {
|
||
auto list = QList<QSharedPointer<Entity> >();
|
||
for (int var = 0; var < maps.size(); ++var) {
|
||
auto ptr = this->convert(maps.at(var), classname, refresh);
|
||
auto ptr = this->convert(maps.at(var), classname, refresh, resolveRelations);
|
||
list.append(ptr);
|
||
this->cache.insert(ptr);
|
||
}
|
src/entitymanager.h | ||
---|---|---|
static QStringList getConnectionNames();
|
||
QSharedPointer<QueryBuilder> getQueryBuilder() const;
|
||
|
||
template<class T> QList<QSharedPointer<T>> find(Query &q) {
|
||
template<class T> QList<QSharedPointer<T>> find(Query &q,
|
||
const bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance<T *>());
|
||
if (ptr) {
|
||
if (q.getFrom().isEmpty()) {
|
||
q.setFrom(QStringList(ptr->getTablename()));
|
||
}
|
||
if (joinBaseClasses) {
|
||
q.appendJoins(this->schema->getQueryBuilder()->joinBaseClasses(ptr));
|
||
}
|
||
QSqlQuery query = this->queryInterpreter->build(q);
|
||
auto maps = this->convertQueryResult(query);
|
||
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()));
|
||
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
||
resolveRelations);
|
||
return this->convertList<T>(converted);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
}
|
||
|
||
template<class T> QList<QSharedPointer<T>> findAll() {
|
||
template<class T> QList<QSharedPointer<T>> findAll(const bool resolveRelations =
|
||
true) {
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance<T *>());
|
||
if (ptr) {
|
||
auto maps = this->findAll(ptr);
|
||
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()));
|
||
auto converted = this->convert(maps, EntityHelper::getClassname(ptr.data()),
|
||
resolveRelations);
|
||
return this->convertList<T>(converted);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
... | ... | |
return this->findById(id, ptr).objectCast<T>();
|
||
}
|
||
|
||
template<class T> QSharedPointer<T> findEntityByAttributes(
|
||
template<class T>
|
||
QSharedPointer<T> findEntityByAttributes(
|
||
const QHash<QString, QVariant>
|
||
&attributes) {
|
||
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0);
|
||
&attributes, const bool joinBaseClasses = false,
|
||
const bool resolveRelations = true) {
|
||
auto list = this->findAllEntitiesByAttributes<T>(attributes, 1, 0,
|
||
joinBaseClasses, resolveRelations);
|
||
if (list.isEmpty()) {
|
||
return QSharedPointer<T>();
|
||
}
|
||
... | ... | |
|
||
template<class T> QList<QSharedPointer<T>> findAllEntitiesByAttributes(
|
||
const QHash<QString, QVariant> &attributes =
|
||
QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0) {
|
||
QHash<QString, QString>(), quint64 limit = 0, quint64 offset = 0,
|
||
bool joinBaseClasses = false, const bool resolveRelations = true) {
|
||
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance<T *>());
|
||
(EntityInstanceFactory::createInstance<T *>());
|
||
if (e) {
|
||
Query query = Query(QStringList(e->getTablename()));
|
||
if (joinBaseClasses) {
|
||
query.appendJoins(this->schema->getQueryBuilder()->joinBaseClasses(e));
|
||
}
|
||
query.appendWhere(this->schema->getQueryBuilder()->where(attributes));
|
||
query.setLimit(limit);
|
||
query.setOffset(offset);
|
||
QSqlQuery q = this->queryInterpreter->build(query);
|
||
auto results = this->convertQueryResult(q);
|
||
auto list = this->convert(results, EntityHelper::getClassname(e.data()));
|
||
auto list = this->convert(results, EntityHelper::getClassname(e.data()),
|
||
resolveRelations);
|
||
return this->convertList<T>(list);
|
||
}
|
||
return QList<QSharedPointer<T>>();
|
||
... | ... | |
void setNullOneToManyRelation(QVariant &var, const Relation &r);
|
||
void setNullEntityPropertyRelation(QVariant &var, const Relation &r);
|
||
QSharedPointer<Entity> convert(const QHash<QString, QVariant> &map,
|
||
const char *classname, const bool refresh = false);
|
||
const char *classname, const bool refresh = false,
|
||
const bool resolveRelations = true);
|
||
QList<QSharedPointer<Entity>> convert(QList<QHash<QString, QVariant> > maps,
|
||
const char *classname, const bool refresh = false);
|
||
const char *classname, const bool refresh = false,
|
||
const bool resolveRelations = true);
|
||
void missingManyToManyTable(const QString &tblName,
|
||
const QSharedPointer<Entity> &e, const Relation &r);
|
||
bool isRelationPropertyValid(const QMetaProperty &prop, const Relation &r,
|
src/query.cpp | ||
---|---|---|
}
|
||
}
|
||
|
||
void Query::appendJoins(const QList<Join> &value) {
|
||
this->joins.append(value);
|
||
}
|
||
|
||
QStringList Query::getGroupBy() const {
|
||
return groupBy;
|
||
}
|
src/query.h | ||
---|---|---|
void setFrom(const QStringList &value);
|
||
|
||
void appendJoin(const Join &value);
|
||
void appendJoins(const QList<Join> &value);
|
||
QList<Join> getJoins() const;
|
||
void setJoins(const QList<Join> &value);
|
||
|
src/querybuilder.cpp | ||
---|---|---|
|
||
QSqlQuery QueryBuilder::update(const QString &tableName,
|
||
QHash<QString, QVariant> &attributes, const QString &primaryKey) const {
|
||
QVariant pk = attributes.value(primaryKey);
|
||
attributes.remove(primaryKey);
|
||
QSqlQuery q = this->database->getQuery("UPDATE " + this->schema->quoteTableName(
|
||
tableName) + " SET " + this->attributes(attributes) + " " + this->whereKeyword()
|
||
+ " " +
|
||
this->schema->quoteColumnName(primaryKey) + " = " + this->placeHolder(
|
||
primaryKey) + ";");
|
||
this->bindValues(attributes, q);
|
||
this->bindValue(primaryKey, pk, q);
|
||
return q;
|
||
}
|
||
|
||
... | ... | |
return val;
|
||
}
|
||
|
||
QString QueryBuilder::joinSuperClasses(const QSharedPointer<Entity> &entity)
|
||
const {
|
||
auto classes = EntityHelper::superClasses(entity.data(), true);
|
||
QString joined = "";
|
||
Entity *e = 0;
|
||
for (int var = 0; var < classes.size(); ++var) {
|
||
auto metaObject = classes.at(var);
|
||
e = EntityInstanceFactory::createInstance(metaObject->className());
|
||
if (e) {
|
||
joined.append(" ");
|
||
joined.append(this->leftJoin(e->getTablename(), entity->getTablename(),
|
||
e->getPrimaryKey(), entity->getPrimaryKey()));
|
||
}
|
||
delete e;
|
||
e = 0;
|
||
}
|
||
return joined;
|
||
}
|
||
|
||
QString QueryBuilder::countFunction(const QString &distinctColumn) const {
|
||
return QString(this->countKeyword() + "(" + (distinctColumn.isEmpty() ? "*" :
|
||
(this->distinct() +
|
||
... | ... | |
|
||
QString QueryBuilder::leftJoin(const QString &foreignTable,
|
||
const QString &tableName, const QString &foreignKey,
|
||
const QString &primaryKey) const {
|
||
return "LEFT JOIN " + this->schema->quoteTableName(
|
||
foreignTable) + " ON " +
|
||
const QString &primaryKey, bool onlyCondition) const {
|
||
return (!onlyCondition ? ("LEFT JOIN " + this->schema->quoteTableName(
|
||
foreignTable) + " ON ") : "") +
|
||
this->schema->quoteColumnName(foreignTable + "." + primaryKey) + "=" +
|
||
this->schema->quoteColumnName(
|
||
tableName + "." + foreignKey);
|
||
... | ... | |
return exp;
|
||
}
|
||
|
||
Join QueryBuilder::joinClasses(const QSharedPointer<Entity> &mainEntity,
|
||
const QSharedPointer<Entity> &foreignEntity, const QString &joinType) const {
|
||
Join j = Join(foreignEntity->getTablename(),
|
||
this->leftJoin(foreignEntity->getTablename(), mainEntity->getTablename(),
|
||
foreignEntity->getPrimaryKey(), mainEntity->getPrimaryKey(), true));
|
||
j.setType(joinType);
|
||
return j;
|
||
}
|
||
|
||
QList<Join> QueryBuilder::joinBaseClasses(const QSharedPointer<Entity>
|
||
&entity) {
|
||
auto classes = EntityHelper::superClasses(entity.data(), true);
|
||
QList<Join> joins = QList<Join>();
|
||
for (int var = 0; var < classes.size(); ++var) {
|
||
auto metaObject = classes.at(var);
|
||
QSharedPointer<Entity> e = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(metaObject->className()));
|
||
if (e) {
|
||
joins.append(this->joinClasses(entity, e));
|
||
}
|
||
}
|
||
return joins;
|
||
}
|
||
|
||
QString QueryBuilder::joinSuperClasses(const QSharedPointer<Entity> &entity)
|
||
const {
|
||
auto classes = EntityHelper::superClasses(entity.data(), true);
|
||
QString joined = "";
|
||
Entity *e = nullptr;
|
||
for (int var = 0; var < classes.size(); ++var) {
|
||
auto metaObject = classes.at(var);
|
||
e = EntityInstanceFactory::createInstance(metaObject->className());
|
||
if (e) {
|
||
joined.append(" ");
|
||
joined.append(this->leftJoin(e->getTablename(), entity->getTablename(),
|
||
e->getPrimaryKey(), entity->getPrimaryKey()));
|
||
}
|
||
delete e;
|
||
e = nullptr;
|
||
}
|
||
return joined;
|
||
}
|
||
|
||
Expression QueryBuilder::where(QHash<QString, QVariant> conditions,
|
||
QString conjunction) {
|
||
Expression exp = Expression(this->where(conditions, conjunction, false, "id",
|
src/querybuilder.h | ||
---|---|---|
QString getColumnType(const QString &type) const;
|
||
virtual QString placeHolder(const QString &key) const;
|
||
void bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q,
|
||
bool ignoreID = false, const QString &primaryKey = "id") const;
|
||
bool ignoreID = false, const QString &primaryKey = QStringLiteral("id")) const;
|
||
void bindValue(const QString &key, const QVariant &value, QSqlQuery &q) const;
|
||
Expression where(QString column, QVariant value);
|
||
Join joinClasses(const QSharedPointer<Entity> &mainEntity, const QSharedPointer<Entity> &foreignEntity, const QString &joinType=QStringLiteral("LEFT JOIN"))const;
|
||
QList<Join> joinBaseClasses(const QSharedPointer<Entity> &entity);
|
||
/**
|
||
* @brief where
|
||
* @param query
|
||
... | ... | |
* @param conjunction its AND or OR
|
||
*/
|
||
Expression where(QHash<QString, QVariant> conditions,
|
||
QString conjunction = "AND");
|
||
QString conjunction = QStringLiteral("AND"));
|
||
Expression where(QString condition,
|
||
QHash<QString, QVariant> values = QHash<QString, QVariant>());
|
||
//void where(Query &query,QHash<QString, QList<QVariant>> conditions, QString concat="AND");
|
||
... | ... | |
* @param concat
|
||
*/
|
||
Expression like(QHash<QString, QVariant> conditions,
|
||
QString conjunction = "AND",
|
||
QString conjunction = QStringLiteral("AND"),
|
||
JokerPosition jp = JokerPosition::BOTH, QChar wildcard = '%');
|
||
|
||
protected:
|
||
... | ... | |
const QSharedPointer<Entity> &entity) const;
|
||
|
||
QString leftJoin(const QString &foreignTable, const QString &tableName,
|
||
const QString &foreignKey = "id", const QString &primaryKey = "id") const;
|
||
const QString &foreignKey = "id", const QString &primaryKey = "id", bool onlyCondition=false) const;
|
||
QString superClassColumnName(const QMetaObject *&superMeta) const;
|
||
QString addWildcard(QVariant var, JokerPosition jp,
|
||
QChar jokerChar = '%') const;
|
src/queryinterpreter.cpp | ||
---|---|---|
QString QueryInterpreter::buildSelect(Query &q,
|
||
const QList<Expression> &columns,
|
||
const bool &distinct, const QString &selectOption) const {
|
||
QString sqlSelect = distinct ? "SELECT DISTINCT" : "SELECT";
|
||
QString sqlSelect = distinct ? ("SELECT " + this->builder->distinct()) :
|
||
"SELECT";
|
||
if (!selectOption.isEmpty()) {
|
||
sqlSelect += this->builder->getSeparator() + selectOption;
|
||
}
|
src/schema.cpp | ||
---|---|---|
}
|
||
|
||
QString Schema::quoteSimpleTableName(QString name) {
|
||
return name.indexOf("`") != -1 ? name : "`" + name + "`";
|
||
return name.indexOf("`") != -1 ? name : ("`" + name + "`");
|
||
}
|
||
|
||
QString Schema::quoteTableName(QString name) {
|
||
... | ... | |
}
|
||
|
||
QString Schema::quoteSimpleColumnName(QString name) {
|
||
return name.indexOf("`") != -1 || name == "*" ? name : "`" + name + "`";
|
||
return name.indexOf("`") != -1 || name == "*" ? name : ("`" + name + "`");
|
||
}
|
||
|
||
QHash<QString, QSharedPointer<TableSchema> > Schema::getTableSchemas(
|
Auch abrufbar als: Unified diff
fixes