Revision 5d93390e
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
EntityManager.pro | ||
---|---|---|
src/entityinstancefactory.h \
|
||
src/cache.h \
|
||
src/entityhelper.h \
|
||
src/logger.h
|
||
src/logger.h \
|
||
src/query.h \
|
||
src/join.h
|
||
|
||
SOURCES += \
|
||
src/entity.cpp \
|
||
... | ... | |
src/entityinstancefactory.cpp \
|
||
src/cache.cpp \
|
||
src/entityhelper.cpp \
|
||
src/logger.cpp
|
||
src/logger.cpp \
|
||
src/query.cpp \
|
||
src/join.cpp
|
||
|
||
unix {
|
||
target.path = /usr/lib
|
src/join.cpp | ||
---|---|---|
#include "join.h"
|
||
using namespace CuteEntityManager;
|
||
Join::Join() {
|
||
|
||
}
|
||
|
||
Join::Join(QString foreignTable, QString condition, QString type) {
|
||
this->foreignTable = foreignTable;
|
||
this->condition = condition;
|
||
this->type = type;
|
||
}
|
||
|
||
QString Join::getType() const {
|
||
return type;
|
||
}
|
||
|
||
void Join::setType(const QString &value) {
|
||
type = value;
|
||
}
|
||
|
||
QString Join::getForeignTable() const {
|
||
return foreignTable;
|
||
}
|
||
|
||
void Join::setForeignTable(const QString &value) {
|
||
foreignTable = value;
|
||
}
|
||
|
||
QString Join::getCondition() const {
|
||
return condition;
|
||
}
|
||
|
||
void Join::setCondition(const QString &value) {
|
||
condition = value;
|
||
}
|
||
|
||
|
src/join.h | ||
---|---|---|
#ifndef JOIN_H
|
||
#define JOIN_H
|
||
#include <QString>
|
||
|
||
namespace CuteEntityManager {
|
||
class Join {
|
||
|
||
public:
|
||
Join();
|
||
Join(QString foreignTable, QString condition,
|
||
QString type = QStringLiteral("LEFT JOIN"));
|
||
|
||
QString getType() const;
|
||
void setType(const QString &value);
|
||
|
||
QString getForeignTable() const;
|
||
void setForeignTable(const QString &value);
|
||
|
||
QString getCondition() const;
|
||
void setCondition(const QString &value);
|
||
|
||
private:
|
||
QString type = QStringLiteral("LEFT JOIN");
|
||
QString foreignTable;
|
||
QString condition;
|
||
|
||
};
|
||
}
|
||
#endif // JOIN_H
|
src/query.cpp | ||
---|---|---|
#include "query.h"
|
||
using namespace CuteEntityManager;
|
||
Query::Query() {
|
||
this->select << "*";
|
||
}
|
||
|
||
QStringList Query::getSelect() const {
|
||
return select;
|
||
}
|
||
|
||
void Query::appendCondition(const QString &condition) {
|
||
this->conditions.append(condition);
|
||
}
|
||
|
||
void Query::setSelect(const QStringList &value) {
|
||
select = value;
|
||
}
|
||
QString Query::getSelectOption() const {
|
||
return selectOption;
|
||
}
|
||
|
||
void Query::setSelectOption(const QString &value) {
|
||
selectOption = value;
|
||
}
|
||
bool Query::getDistinct() const {
|
||
return distinct;
|
||
}
|
||
|
||
void Query::setDistinct(bool value) {
|
||
distinct = value;
|
||
}
|
||
QStringList Query::getFrom() const {
|
||
return from;
|
||
}
|
||
|
||
void Query::setFrom(const QStringList &value) {
|
||
from = value;
|
||
}
|
||
QStringList Query::getGroupBy() const {
|
||
return groupBy;
|
||
}
|
||
|
||
void Query::setGroupBy(const QStringList &value) {
|
||
groupBy = value;
|
||
}
|
||
QStringList Query::getOrderBy() const {
|
||
return orderBy;
|
||
}
|
||
|
||
void Query::setOrderBy(const QStringList &value) {
|
||
orderBy = value;
|
||
}
|
||
QList<Join> Query::getJoins() const {
|
||
return joins;
|
||
}
|
||
|
||
void Query::setJoins(const QList<Join> &value) {
|
||
joins = value;
|
||
}
|
||
|
||
void Query::appendParam(const QString &column, QVariant value) {
|
||
this->params.insert(column, value);
|
||
}
|
||
|
||
QHash<QString, QVariant> Query::getParams() const {
|
||
return params;
|
||
}
|
||
|
||
void Query::setParams(const QHash<QString, QVariant> &value) {
|
||
params = value;
|
||
}
|
||
|
||
uint Query::getLimit() const {
|
||
return limit;
|
||
}
|
||
|
||
void Query::setLimit(const uint &value) {
|
||
limit = value;
|
||
}
|
||
|
||
uint Query::getOffset() const {
|
||
return offset;
|
||
}
|
||
|
||
void Query::setOffset(const uint &value) {
|
||
offset = value;
|
||
}
|
||
|
||
QLinkedList<QString> Query::getConditions() const {
|
||
return conditions;
|
||
}
|
||
|
||
void Query::setConditions(const QLinkedList<QString> &value) {
|
||
conditions = value;
|
||
}
|
src/query.h | ||
---|---|---|
#ifndef QUERY_H
|
||
#define QUERY_H
|
||
#include <QString>
|
||
#include <QHash>
|
||
#include <QVariant>
|
||
#include <QLinkedList>
|
||
#include "join.h"
|
||
|
||
namespace CuteEntityManager {
|
||
class Query
|
||
{
|
||
public:
|
||
Query();
|
||
QStringList getSelect() const;
|
||
void setSelect(const QStringList &value);
|
||
|
||
QString getSelectOption() const;
|
||
void setSelectOption(const QString &value);
|
||
|
||
bool getDistinct() const;
|
||
void setDistinct(bool value);
|
||
|
||
QStringList getFrom() const;
|
||
void setFrom(const QStringList &value);
|
||
|
||
QStringList getGroupBy() const;
|
||
void setGroupBy(const QStringList &value);
|
||
|
||
QStringList getOrderBy() const;
|
||
void setOrderBy(const QStringList &value);
|
||
|
||
QList<Join> getJoins() const;
|
||
void setJoins(const QList<Join> &value);
|
||
|
||
void appendParam(const QString &column, QVariant value);
|
||
QHash<QString, QVariant> getParams() const;
|
||
void setParams(const QHash<QString, QVariant> &value);
|
||
|
||
uint getLimit() const;
|
||
void setLimit(const uint &value);
|
||
|
||
uint getOffset() const;
|
||
void setOffset(const uint &value);
|
||
|
||
QLinkedList<QString> getConditions() const;
|
||
void appendCondition(const QString &condition);
|
||
void setConditions(const QLinkedList<QString> &value);
|
||
|
||
private:
|
||
QStringList select;
|
||
QString selectOption = QStringLiteral("");
|
||
bool distinct = false;
|
||
QStringList from;
|
||
QStringList groupBy;
|
||
QStringList orderBy;
|
||
QLinkedList<QString> conditions;
|
||
QList<Join> joins;
|
||
QHash<QString, QVariant> params;
|
||
uint limit = 0;
|
||
uint offset = 0;
|
||
};
|
||
|
||
enum class JokerPosition {
|
||
FRONT, // e.g. "%foo"
|
||
BEHIND, // e.g. "foo%"
|
||
BOTH // e.g. "%foo%"
|
||
};
|
||
|
||
|
||
}
|
||
|
||
#endif // QUERY_H
|
src/querybuilder.cpp | ||
---|---|---|
this->schema->containsTable(tableName) ? rc = true : rc = false;
|
||
if (!rc) {
|
||
QSqlQuery q = this->database->getQuery(this->createTable(tableName,
|
||
tableDefinition));
|
||
tableDefinition));
|
||
if (this->database->exec(q)) {
|
||
if (createRelationTables) {
|
||
auto relTables = this->generateRelationTables(entity);
|
||
auto i = relTables.constBegin();
|
||
while (i != relTables.constEnd()) {
|
||
auto query = this->database->getQuery(this->createTable(i.key(),
|
||
i.value()));
|
||
i.value()));
|
||
this->database->exec(query);
|
||
++i;
|
||
}
|
||
... | ... | |
auto relation = iterator.value();
|
||
if (relation.getMappedBy().isEmpty() && !relation.getCascadeType().isEmpty()) {
|
||
QString update = relation.getCascadeType().contains(CascadeType::MERGE)
|
||
|| relation.getCascadeType().contains(CascadeType::ALL) ?
|
||
this->getForeignKeyCascade(
|
||
CASCADE) : this->getForeignKeyCascade(NO_ACTION);
|
||
|| relation.getCascadeType().contains(CascadeType::ALL) ?
|
||
this->getForeignKeyCascade(
|
||
CASCADE) : this->getForeignKeyCascade(NO_ACTION);
|
||
QString remove = relation.getCascadeType().contains(CascadeType::REMOVE)
|
||
|| relation.getCascadeType().contains(CascadeType::ALL) ?
|
||
this->getForeignKeyCascade(
|
||
CASCADE) : this->getForeignKeyCascade(DbForeignKeyCascade::SET_NULL);
|
||
|| relation.getCascadeType().contains(CascadeType::ALL) ?
|
||
this->getForeignKeyCascade(
|
||
CASCADE) : this->getForeignKeyCascade(DbForeignKeyCascade::SET_NULL);
|
||
this->createRelationFK(queries, entity, relation,
|
||
props.value(relation.getPropertyName()), update, remove);
|
||
}
|
||
... | ... | |
const QMetaProperty &metaProperty, const QString &update,
|
||
const QString &remove) const {
|
||
auto ptr = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(metaProperty.type()));
|
||
(EntityInstanceFactory::createInstance(metaProperty.type()));
|
||
if (ptr) {
|
||
if (relation.getType() == RelationType::ONE_TO_ONE
|
||
|| relation.getType() == RelationType::MANY_TO_ONE) {
|
||
QString indexName = this->generateIndexName(relation.getPropertyName(),
|
||
entity->getTablename(),
|
||
this->generateColumnNameID(relation.getPropertyName()),
|
||
ptr->getTablename(), true);
|
||
entity->getTablename(),
|
||
this->generateColumnNameID(relation.getPropertyName()),
|
||
ptr->getTablename(), true);
|
||
queries.append(this->addForeignKey(indexName, entity->getTablename(),
|
||
QStringList(this->generateColumnNameID(relation.getPropertyName())),
|
||
ptr->getTablename(),
|
||
... | ... | |
} else if (relation.getType() == RelationType::MANY_TO_MANY) {
|
||
QString tableName = this->generateManyToManyTableName(entity, ptr, relation);
|
||
queries.append(this->createForeignKeyManyToMany(tableName, entity, update,
|
||
remove));
|
||
remove));
|
||
queries.append(this->createForeignKeyManyToMany(tableName, ptr, update,
|
||
remove));
|
||
remove));
|
||
}
|
||
}
|
||
}
|
||
|
||
QString QueryBuilder::createForeignKeyManyToMany(const QString &tableName,
|
||
const QSharedPointer<Entity> &entity, const QString &update,
|
||
const QString &remove) const {
|
||
const QSharedPointer<Entity> &entity, const QString &update,
|
||
const QString &remove) const {
|
||
QString fkColumn = this->generateManyToManyColumnName(entity);
|
||
QString indexName = this->generateIndexName(fkColumn,
|
||
tableName, fkColumn,
|
||
entity->getTablename(), true);
|
||
tableName, fkColumn,
|
||
entity->getTablename(), true);
|
||
return this->addForeignKey(indexName, tableName, QStringList(fkColumn),
|
||
entity->getTablename(), QStringList(entity->getPrimaryKey()),
|
||
remove, update);
|
||
... | ... | |
&& QString(superMetaObject->className()) !=
|
||
this->entityClassname()) {
|
||
Entity *superClass = EntityInstanceFactory::createInstance(
|
||
superMetaObject->className());
|
||
superMetaObject->className());
|
||
if (superClass) {
|
||
QString refColumn = superClass->getPrimaryKey();
|
||
QString refTable = superClass->getTablename();
|
||
r = this->addForeignKey(this->generateIndexName(e->getPrimaryKey(),
|
||
e->getTablename(), refColumn, refTable, true), e->getTablename(),
|
||
e->getTablename(), refColumn, refTable, true), e->getTablename(),
|
||
QStringList(e->getPrimaryKey()), refTable, QStringList(refColumn),
|
||
this->getForeignKeyCascade(CASCADE),
|
||
this->getForeignKeyCascade(CASCADE));
|
||
... | ... | |
s.append(", ");
|
||
}
|
||
s.append(this->schema->quoteColumnName(i.key())).append(" " +
|
||
this->getColumnType(
|
||
i.value()));
|
||
this->getColumnType(
|
||
i.value()));
|
||
++i;
|
||
}
|
||
s.append(");");
|
||
... | ... | |
|
||
QString QueryBuilder::renameTable(QString tableName, QString newName) const {
|
||
return "RENAME TABLE " + this->schema->quoteTableName(
|
||
tableName) + " TO " +
|
||
this->schema->quoteTableName(
|
||
newName);
|
||
tableName) + " TO " +
|
||
this->schema->quoteTableName(
|
||
newName);
|
||
}
|
||
|
||
QString QueryBuilder::dropTable(QString tableName) const {
|
||
... | ... | |
QString QueryBuilder::addColumn(QString tableName, QString columnName,
|
||
QString columnType) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " ADD " +
|
||
this->schema->quoteColumnName(
|
||
columnName) + " " + this->getColumnType(columnType);
|
||
tableName) + " ADD " +
|
||
this->schema->quoteColumnName(
|
||
columnName) + " " + this->getColumnType(columnType);
|
||
}
|
||
|
||
QString QueryBuilder::dropColumn(QString tableName, QString columName) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " DROP COLUMN " +
|
||
this->schema->quoteColumnName(columName);
|
||
tableName) + " DROP COLUMN " +
|
||
this->schema->quoteColumnName(columName);
|
||
}
|
||
|
||
QString QueryBuilder::renameColumn(QString tableName, QString oldName,
|
||
QString newName) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " RENAME COLUMN " +
|
||
this->schema->quoteColumnName(oldName) + " TO " +
|
||
this->schema->quoteColumnName(
|
||
newName);
|
||
tableName) + " RENAME COLUMN " +
|
||
this->schema->quoteColumnName(oldName) + " TO " +
|
||
this->schema->quoteColumnName(
|
||
newName);
|
||
}
|
||
|
||
QString QueryBuilder::alterColumn(QString tableName, QString columnName,
|
||
QString newType) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " CHANGE " +
|
||
this->schema->quoteColumnName(columnName) + " " +
|
||
this->schema->quoteColumnName(columnName) + this->getColumnType(newType);
|
||
tableName) + " CHANGE " +
|
||
this->schema->quoteColumnName(columnName) + " " +
|
||
this->schema->quoteColumnName(columnName) + this->getColumnType(newType);
|
||
}
|
||
|
||
QString QueryBuilder::addPrimaryKey(QString name, QString tableName,
|
||
QStringList columns) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " ADD CONSTRAINT " +
|
||
this->schema->quoteColumnName(name) + "PRIMARY KEY (" +
|
||
this->buildColumns(columns) + " )";
|
||
tableName) + " ADD CONSTRAINT " +
|
||
this->schema->quoteColumnName(name) + "PRIMARY KEY (" +
|
||
this->buildColumns(columns) + " )";
|
||
}
|
||
|
||
QString QueryBuilder::dropPrimaryKey(QString name, QString tableName) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " DROP CONSTRAINT " +
|
||
this->schema->quoteColumnName(name);
|
||
tableName) + " DROP CONSTRAINT " +
|
||
this->schema->quoteColumnName(name);
|
||
}
|
||
|
||
QString QueryBuilder::addForeignKey(QString name, QString tableName,
|
||
... | ... | |
QStringList refColumns, QString deleteConstraint,
|
||
QString updateConstraint) const {
|
||
QString r = "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + "ADD CONSTRAINT " +
|
||
this->schema->quoteColumnName(name)
|
||
+ " FOREIGN KEY (" + this->buildColumns(columns) + ")" + " REFERENCES " +
|
||
this->schema->quoteTableName(
|
||
refTableName) +
|
||
" (" + this->buildColumns(refColumns) + ")";
|
||
tableName) + "ADD CONSTRAINT " +
|
||
this->schema->quoteColumnName(name)
|
||
+ " FOREIGN KEY (" + this->buildColumns(columns) + ")" + " REFERENCES " +
|
||
this->schema->quoteTableName(
|
||
refTableName) +
|
||
" (" + this->buildColumns(refColumns) + ")";
|
||
if (!deleteConstraint.isEmpty()) {
|
||
r.append(" ON DELETE " + deleteConstraint);
|
||
}
|
||
... | ... | |
const QString &table, const QString &refColumn, const QString &refTable,
|
||
const bool fk) const {
|
||
return QString(fk ? "fk" : "idx").append("_").append(name).append(table).append(
|
||
refColumn).append(refTable);
|
||
refColumn).append(refTable);
|
||
}
|
||
|
||
QString QueryBuilder::generateColumnNameID(QString name) const {
|
||
... | ... | |
|
||
QString QueryBuilder::dropForeignKey(QString name, QString tableName) const {
|
||
return "ALTER TABLE " + this->schema->quoteTableName(
|
||
tableName) + " DROP CONSTRAINT " +
|
||
this->schema->quoteColumnName(name);
|
||
tableName) + " DROP CONSTRAINT " +
|
||
this->schema->quoteColumnName(name);
|
||
}
|
||
|
||
QString QueryBuilder::createIndex(QString name, QString tableName,
|
||
QStringList columns,
|
||
bool unique) const {
|
||
QString s = (unique ? "CREATE UNIQUE INDEX " : "CREATE INDEX ") +
|
||
this->schema->quoteTableName(
|
||
name) + " ON " + this->schema->quoteTableName(tableName) + " (";
|
||
this->schema->quoteTableName(
|
||
name) + " ON " + this->schema->quoteTableName(tableName) + " (";
|
||
s.append(this->buildColumns(columns));
|
||
s.append(");");
|
||
return s;
|
||
... | ... | |
|
||
QString QueryBuilder::dropIndex(QString name, QString tableName) const {
|
||
return "DROP INDEX " + this->schema->quoteTableName(name) + " ON " +
|
||
this->schema->quoteTableName(
|
||
tableName);
|
||
this->schema->quoteTableName(
|
||
tableName);
|
||
}
|
||
|
||
QSharedPointer<Database> QueryBuilder::getDatabase() const {
|
||
... | ... | |
}
|
||
|
||
QHash<QString, QString> QueryBuilder::generateTableDefinition(
|
||
const QSharedPointer<Entity> &entity)
|
||
const QSharedPointer<Entity> &entity)
|
||
const {
|
||
auto map = QHash<QString, QString>();
|
||
auto o = entity->metaObject();
|
||
auto superMetaObject = entity->metaObject()->superClass();
|
||
auto superMetaObjectPropertyMap = EntityHelper::getSuperMetaProperties(entity.data());
|
||
auto superMetaObjectPropertyMap = EntityHelper::getSuperMetaProperties(
|
||
entity.data());
|
||
QHash<QString, Relation> relations = entity->getRelations();
|
||
for (int var = 0; var < o->propertyCount(); ++var) {
|
||
auto m = o->property(var);
|
||
if ((!superMetaObjectPropertyMap.contains(QString(m.name()))
|
||
|| entity->getInheritanceStrategy() == InheritanceStrategy::PER_CLASS_TABLE)
|
||
|| entity->getInheritanceStrategy() == InheritanceStrategy::PER_CLASS_TABLE)
|
||
&& m.name() != QString("objectName") && m.isReadable()
|
||
&& !entity->getTransientAttributes().contains(m.name())) {
|
||
if (m.isEnumType()) {
|
||
... | ... | |
}
|
||
|
||
QString QueryBuilder::generateManyToManyTableName(const QSharedPointer<Entity>
|
||
&firstEntity,
|
||
const QSharedPointer<Entity> &secondEntity, const Relation &r) const {
|
||
&firstEntity,
|
||
const QSharedPointer<Entity> &secondEntity, const Relation &r) const {
|
||
if (r.getMappedBy().isEmpty()) {
|
||
return firstEntity->getTablename() + "_" + r.getPropertyName();
|
||
} else {
|
||
... | ... | |
this->schema->TYPE_BIGINT);
|
||
auto meta = props.value(r.getPropertyName());
|
||
QSharedPointer<Entity> ptr = QSharedPointer<Entity>
|
||
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
|
||
QMetaType::typeName(meta.userType()))));
|
||
(EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(
|
||
QMetaType::typeName(meta.userType()))));
|
||
h.insert(this->generateManyToManyColumnName(ptr),
|
||
this->schema->TYPE_BIGINT);
|
||
relations.insert(this->generateManyToManyTableName(entity, ptr, r), h);
|
||
... | ... | |
}
|
||
|
||
QString QueryBuilder::transformAbstractTypeToRealDbType(
|
||
QString typeName) const {
|
||
QString typeName) const {
|
||
return this->schema->getTypeMap()->value(typeName);
|
||
}
|
||
|
||
... | ... | |
//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+)\\((.+?)\\)(.*)$/"));
|
||
QRegularExpression::escape("/^(\\w+)\\((.+?)\\)(.*)$/"));
|
||
reg.optimize();
|
||
QRegularExpressionMatchIterator i = reg.globalMatch(type, 0,
|
||
QRegularExpression::PartialPreferFirstMatch);
|
||
QRegularExpression::PartialPreferFirstMatch);
|
||
short s = 0;
|
||
bool ok = false;
|
||
QString before = "";
|
||
... | ... | |
QSqlQuery QueryBuilder::find(const qint64 &id, const QString &tableName) const {
|
||
QString pk = "id";
|
||
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
tableName)) + " WHERE " + this->schema->quoteColumnName(pk) + " = " + this->placeHolder(pk) + " LIMIT 1;");
|
||
tableName)) + " WHERE " + this->schema->quoteColumnName(
|
||
pk) + " = " + this->placeHolder(pk) + " LIMIT 1;");
|
||
this->bindValue(pk, id, q);
|
||
return q;
|
||
}
|
||
... | ... | |
QSqlQuery QueryBuilder::find(const qint64 &id,
|
||
const QSharedPointer<Entity> &entity, qint64 offset, QString pk) const {
|
||
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
entity->getTablename())) + this->joinSuperClasses(
|
||
entity) + " WHERE " + this->schema->quoteColumnName(entity->getTablename() + "." + pk) + "= " + this->placeHolder(pk) + this->limit(1, offset));
|
||
entity->getTablename())) + this->joinSuperClasses(
|
||
entity) + " WHERE " + this->schema->quoteColumnName(entity->getTablename() + "."
|
||
+ pk) + "= " + this->placeHolder(pk) + this->limit(1, offset));
|
||
this->bindValue(pk, id, q);
|
||
return q;
|
||
}
|
||
... | ... | |
QSqlQuery QueryBuilder::findAll(const QSharedPointer<Entity> &entity,
|
||
const qint64 limit, qint64 offset) {
|
||
return this->database->getQuery(this->selectBase(QStringList(
|
||
entity->getTablename())) + " " + this->joinSuperClasses(
|
||
entity->getTablename())) + " " + this->joinSuperClasses(
|
||
entity) + this->limit(limit, offset) + ";");
|
||
}
|
||
|
||
... | ... | |
* @return
|
||
*/
|
||
QSqlQuery QueryBuilder::findByAttributes(const QHash<QString, QVariant> &m,
|
||
const QString &tableName,
|
||
const bool &ignoreID, const qint64 limit, const qint64 offset) const {
|
||
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, "AND", ignoreID) + this->limit(limit, offset));
|
||
tableName)) + this->where(m, "AND", 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) {
|
||
bool ignoreID,
|
||
const qint64 limit,
|
||
const qint64 offset) {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(
|
||
EntityHelper::getMetaProperties(e.data()), e);
|
||
EntityHelper::getMetaProperties(e.data()), e);
|
||
return this->findByAttributes(values, e->getTablename(), ignoreID, limit,
|
||
offset);
|
||
}
|
||
... | ... | |
entity->getProperty(entity->getPrimaryKey()).toLongLong()));
|
||
if (entity->getInheritanceStrategy() != InheritanceStrategy::PER_CLASS_TABLE
|
||
&& entity->isInheritanceCascaded()) {
|
||
auto classes = EntityHelper::superClasses(entity.data(),true);
|
||
auto classes = EntityHelper::superClasses(entity.data(), true);
|
||
for (int var = 0; var < classes.size(); ++var) {
|
||
auto item = classes.at(var);
|
||
auto instance = EntityInstanceFactory::createInstance(item->className());
|
||
... | ... | |
const qint64 &id, const QString &primaryKey) const {
|
||
QSqlQuery q = this->database->getQuery("DELETE FROM " +
|
||
this->schema->quoteTableName(
|
||
tableName) + " WHERE " +
|
||
tableName) + " WHERE " +
|
||
this->schema->quoteColumnName(primaryKey) + "=" + this->placeHolder(
|
||
primaryKey) + ";");
|
||
primaryKey) + ";");
|
||
this->bindValue(primaryKey, id, q);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::findId(const QSharedPointer<Entity> &entity) const {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(
|
||
EntityHelper::getMetaProperties(entity.data()),
|
||
entity);
|
||
EntityHelper::getMetaProperties(entity.data()),
|
||
entity);
|
||
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
entity->getTablename()),
|
||
QStringList(entity->getPrimaryKey())) + this->where(values,
|
||
"AND", true) + " LIMIT 1");
|
||
entity->getTablename()),
|
||
QStringList(entity->getPrimaryKey())) + this->where(values,
|
||
"AND", true) + " LIMIT 1");
|
||
this->bindValues(values, q);
|
||
return q;
|
||
}
|
||
... | ... | |
QSqlQuery QueryBuilder::count(const QSharedPointer<Entity> &entity,
|
||
bool ignoreID) const {
|
||
QHash<QString, QVariant> values = this->getEntityAttributes(
|
||
EntityHelper::getMetaProperties(entity.data()),
|
||
entity);
|
||
EntityHelper::getMetaProperties(entity.data()),
|
||
entity);
|
||
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
entity->getTablename()),
|
||
QStringList(this->countFunction())) + this->where(
|
||
values, "AND", ignoreID));
|
||
entity->getTablename()),
|
||
QStringList(this->countFunction())) + this->where(
|
||
values, "AND", ignoreID));
|
||
this->bindValues(values, q, ignoreID);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::count(const QString &tableName) const {
|
||
QSqlQuery q = this->database->getQuery(this->selectBase(QStringList(
|
||
tableName), QStringList(this->countFunction())) + ";");
|
||
tableName), QStringList(this->countFunction())) + ";");
|
||
return q;
|
||
}
|
||
|
||
... | ... | |
|
||
|
||
QList<QSqlQuery> QueryBuilder::createOrMerge(const QSharedPointer<Entity>
|
||
&entity, bool insert) const {
|
||
&entity, bool insert) const {
|
||
const QList<ClassAttributes> attrs = this->inheritedAttributes(entity);
|
||
auto queries = QList<QSqlQuery>();
|
||
bool first = true;
|
||
... | ... | |
}
|
||
QSqlQuery q = this->database->getQuery();
|
||
QString p1 = "INSERT INTO " + this->schema->quoteTableName(
|
||
tableName) + "(";
|
||
tableName) + "(";
|
||
QString p2 = "VALUES(";
|
||
if (!attributes.isEmpty()) {
|
||
q.prepare(this->buildCreateQuery(attributes.constBegin(), attributes.constEnd(),
|
||
... | ... | |
QSqlQuery QueryBuilder::update(const QString &tableName,
|
||
QHash<QString, QVariant> &attributes, const QString &primaryKey) const {
|
||
QSqlQuery q = this->database->getQuery("UPDATE " + this->schema->quoteTableName(
|
||
tableName) + " SET " + this->attributes(attributes) + " WHERE " +
|
||
tableName) + " SET " + this->attributes(attributes) + " WHERE " +
|
||
this->schema->quoteColumnName(primaryKey) + " = " + this->placeHolder(
|
||
primaryKey) + ";");
|
||
primaryKey) + ";");
|
||
this->bindValues(attributes, q);
|
||
return q;
|
||
}
|
||
... | ... | |
QString pk = "id";
|
||
sql += " WHERE ";
|
||
sql += this->schema->quoteColumnName(
|
||
attribute);
|
||
attribute);
|
||
sql += " = " + this->placeHolder(pk) + ";";
|
||
q.prepare(sql);
|
||
this->bindValue(pk, id, q);
|
||
... | ... | |
}
|
||
|
||
QSqlQuery QueryBuilder::manyToManyDelete(const QString &tableName,
|
||
const QString &attribute, const qint64 &id) {
|
||
const QString &attribute, const qint64 &id) {
|
||
QSqlQuery q = this->database->getQuery();
|
||
QString pkCol = "id";
|
||
QString sql = "DELETE FROM " + this->schema->quoteTableName(
|
||
tableName) + " WHERE " + this->schema->quoteColumnName(
|
||
attribute) + "=" + this->placeHolder(pkCol);
|
||
tableName) + " WHERE " + this->schema->quoteColumnName(
|
||
attribute) + "=" + this->placeHolder(pkCol);
|
||
q.prepare(sql);
|
||
this->bindValue(pkCol, id, q);
|
||
return q;
|
||
}
|
||
|
||
QSqlQuery QueryBuilder::manyToManyInsert(const QString &tableName,
|
||
const QString &col1, const QString &col2) const {
|
||
const QString &col1, const QString &col2) const {
|
||
QSqlQuery q = this->database->getQuery();
|
||
QString sql = "INSERT INTO " + this->schema->quoteTableName(
|
||
tableName) + "(" + col1 + "," + col2 + ")"
|
||
+ "VALUES(?, ?);";
|
||
tableName) + "(" + col1 + "," + col2 + ")"
|
||
+ "VALUES(?, ?);";
|
||
q.prepare(sql);
|
||
return q;
|
||
}
|
||
... | ... | |
|
||
QString QueryBuilder::joinSuperClasses(const QSharedPointer<Entity> &entity)
|
||
const {
|
||
auto classes = EntityHelper::superClasses(entity.data(),true);
|
||
auto classes = EntityHelper::superClasses(entity.data(), true);
|
||
QString joined = "";
|
||
Entity *e = 0;
|
||
for (int var = 0; var < classes.size(); ++var) {
|
||
... | ... | |
|
||
QString QueryBuilder::countFunction(const QString &distinctColumn) const {
|
||
return QString("COUNT(" + distinctColumn.isEmpty() ? "*" : (this->distinct() +
|
||
this->schema->quoteColumnName(distinctColumn)) + ")");
|
||
this->schema->quoteColumnName(distinctColumn)) + ")");
|
||
}
|
||
|
||
QString QueryBuilder::distinct() const {
|
||
... | ... | |
}
|
||
|
||
QString QueryBuilder::generateManyToManyColumnName(const QSharedPointer<Entity>
|
||
&entity) const {
|
||
&entity) const {
|
||
if (entity) {
|
||
return this->generateColumnNameID(entity->getTablename());
|
||
}
|
||
... | ... | |
|
||
|
||
QHash<QString, QVariant> QueryBuilder::saveAttributes(const
|
||
QSharedPointer<Entity> &entity, QHash<QString, QMetaProperty> props,
|
||
QHash<QString, Relation> relations) const {
|
||
QSharedPointer<Entity> &entity, QHash<QString, QMetaProperty> props,
|
||
QHash<QString, Relation> relations) const {
|
||
if (props.isEmpty()) {
|
||
props = EntityHelper::getMetaProperties(entity.data());
|
||
}
|
||
... | ... | |
}
|
||
|
||
QHash<QString, QMetaProperty> QueryBuilder::processProperties(
|
||
const QSharedPointer<Entity> &e,
|
||
QHash<QString, QMetaProperty> &usedProperties) const {
|
||
const QSharedPointer<Entity> &e,
|
||
QHash<QString, QMetaProperty> &usedProperties) const {
|
||
auto properties = EntityHelper::getMetaProperties(e.data());
|
||
QMutableHashIterator<QString, QMetaProperty> i(properties);
|
||
while (i.hasNext()) {
|
||
... | ... | |
}
|
||
|
||
QHash<QString, Relation> QueryBuilder::processRelations(
|
||
const QSharedPointer<Entity> &e,
|
||
QHash<QString, Relation> &usedRelations) const {
|
||
const QSharedPointer<Entity> &e,
|
||
QHash<QString, Relation> &usedRelations) const {
|
||
auto relations = e->getRelations();
|
||
auto i = QMutableHashIterator<QString, Relation>(relations);
|
||
while (i.hasNext()) {
|
||
... | ... | |
}
|
||
|
||
QList<QueryBuilder::ClassAttributes> QueryBuilder::inheritedAttributes(
|
||
const QSharedPointer<Entity> &entity) const {
|
||
const QSharedPointer<Entity> &entity) const {
|
||
auto list = QList<QueryBuilder::ClassAttributes>();
|
||
if (entity->getInheritanceStrategy() == InheritanceStrategy::JOINED_TABLE) {
|
||
auto classes = QList<const QMetaObject *>();
|
||
classes.append(entity->metaObject());
|
||
classes.append(EntityHelper::superClasses(entity.data(),true));
|
||
classes.append(EntityHelper::superClasses(entity.data(), true));
|
||
auto usedProperties = QHash<QString, QMetaProperty>();
|
||
auto usedRelations = QHash<QString, Relation>();
|
||
QSharedPointer<Entity> e;
|
||
... | ... | |
metaObj));
|
||
if (e) {
|
||
list.append(QueryBuilder::ClassAttributes(e->getTablename(),
|
||
this->saveAttributes(entity, this->processProperties(e, usedProperties),
|
||
this->processRelations(e, usedRelations)), e->getPrimaryKey()));
|
||
this->saveAttributes(entity, this->processProperties(e, usedProperties),
|
||
this->processRelations(e, usedRelations)), e->getPrimaryKey()));
|
||
} else {
|
||
qDebug() << "Instance of " << metaObj->className() << " could not be created";
|
||
break;
|
||
... | ... | |
}
|
||
} else {
|
||
list.append(QueryBuilder::ClassAttributes(entity->getTablename(),
|
||
this->saveAttributes(entity), entity->getPrimaryKey()));
|
||
this->saveAttributes(entity), entity->getPrimaryKey()));
|
||
}
|
||
return list;
|
||
}
|
||
... | ... | |
const QString &tableName, const QString &foreignKey,
|
||
const QString &primaryKey) const {
|
||
return "LEFT JOIN " + this->schema->quoteTableName(
|
||
foreignTable) + " ON " +
|
||
this->schema->quoteColumnName(foreignTable + "." + primaryKey) + "=" +
|
||
this->schema->quoteColumnName(
|
||
tableName + "." + foreignKey);
|
||
foreignTable) + " ON " +
|
||
this->schema->quoteColumnName(foreignTable + "." + primaryKey) + "=" +
|
||
this->schema->quoteColumnName(
|
||
tableName + "." + foreignKey);
|
||
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getEntityAttributes(
|
||
const QHash<QString, QMetaProperty>
|
||
&props,
|
||
const QSharedPointer<Entity> &entity) const {
|
||
const QHash<QString, QMetaProperty>
|
||
&props,
|
||
const QSharedPointer<Entity> &entity) const {
|
||
auto map = QHash<QString, QVariant>();
|
||
auto transientAttrs = entity->getTransientAttributes();
|
||
auto relations = entity->getRelations();
|
||
... | ... | |
}
|
||
|
||
QHash<QString, QVariant> QueryBuilder::getManyToOneAttributes(
|
||
QHash<QString, QMetaProperty>
|
||
props,
|
||
const QSharedPointer<Entity> &entity,
|
||
QHash<QString, Relation> relations) const {
|
||
QHash<QString, QMetaProperty>
|
||
props,
|
||
const QSharedPointer<Entity> &entity,
|
||
QHash<QString, Relation> relations) const {
|
||
auto map = QHash<QString, QVariant>();
|
||
if (relations.isEmpty()) {
|
||
relations = entity->getRelations();
|
||
... | ... | |
bool ignoreID, const QString &primaryKey) const {
|
||
QHash<QString, QVariant>::const_iterator i = h.constBegin();
|
||
while (i != h.constEnd()) {
|
||
if ((!ignoreID || (ignoreID && !(i.key() == primaryKey))) && !i.value().isNull()) {
|
||
if ((!ignoreID || (ignoreID && !(i.key() == primaryKey)))
|
||
&& !i.value().isNull()) {
|
||
this->bindValue(i.key(), i.value(), q);
|
||
}
|
||
++i;
|
||
... | ... | |
return QString(":" + key);
|
||
}
|
||
|
||
QString QueryBuilder::where(const QSharedPointer<Entity> &entity,
|
||
QString conjunction,
|
||
bool ignoreID) const {
|
||
return this->where(this->getEntityAttributes(EntityHelper::getMetaProperties(entity.data()),
|
||
entity),
|
||
conjunction, ignoreID, entity->getPrimaryKey());
|
||
}
|
||
|
||
QString QueryBuilder::where(const QHash<QString, QVariant> &m,
|
||
const QString &conjunction,
|
||
bool ignoreID, const QString &primaryKey) const {
|
||
... | ... | |
if (!m.isEmpty()) {
|
||
QHash<QString, QVariant>::const_iterator i = m.constBegin();
|
||
while (i != m.constEnd()) {
|
||
if ((!ignoreID || (ignoreID && !(i.key() == primaryKey))) && !i.value().isNull()) {
|
||
if ((!ignoreID || (ignoreID && !(i.key() == primaryKey)))
|
||
&& !i.value().isNull()) {
|
||
if (!(rc == "")) {
|
||
rc += " " + conjunction + " ";
|
||
}
|
||
... | ... | |
}
|
||
|
||
QueryBuilder::ClassAttributes::ClassAttributes(const QString name,
|
||
const QHash<QString, QVariant> attributes, QString pk) {
|
||
const QHash<QString, QVariant> attributes, QString pk) {
|
||
this->name = name;
|
||
this->attributes = attributes;
|
||
this->pk = pk;
|
||
... | ... | |
}
|
||
|
||
void QueryBuilder::ClassAttributes::setAttributes(const QHash<QString, QVariant>
|
||
&value) {
|
||
&value) {
|
||
attributes = value;
|
||
}
|
||
|
||
... | ... | |
void QueryBuilder::ClassAttributes::setPk(const QString &value) {
|
||
pk = value;
|
||
}
|
||
|
||
void QueryBuilder::andOperator(Query &query,
|
||
QHash<QString, QVariant> conditions) {
|
||
bool first = true;
|
||
QString condition = "";
|
||
for (auto var = conditions.constBegin(); var != conditions.constEnd(); ++var) {
|
||
if (first) {
|
||
first = false;
|
||
} else {
|
||
condition += " AND ";
|
||
}
|
||
condition += this->schema->quoteColumnName(var.key()) + " = " +
|
||
this->placeHolder(var.key());
|
||
query.appendParam(var.key(), var.value());
|
||
}
|
||
query.appendCondition(condition);
|
||
}
|
||
|
||
void QueryBuilder::arbitraryOperator(Query &query, QString op, QString column,
|
||
QVariant value) {
|
||
query.appendCondition(column + " " + op + this->placeHolder(column));
|
||
query.appendParam(column, value);
|
||
}
|
||
|
||
void QueryBuilder::plainOr(Query &query) {
|
||
query.appendCondition("OR");
|
||
}
|
||
|
||
void QueryBuilder::plainAnd(Query &query) {
|
||
query.appendCondition("AND");
|
||
}
|
||
|
||
|
||
void QueryBuilder::where(Query &query, QString, QVariant) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::where(Query &query, QHash<QString, QVariant> conditions,
|
||
QString concat) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::where(Query &query,
|
||
QHash<QString, QList<QVariant> > conditions, QString concat) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::between(Query &query, QString column, QVariant firstValue,
|
||
QVariant secondValue) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::in(Query &query, QString column, QList<QVariant> values) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::notIn(Query &query, QString column, QList<QVariant> values) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::notOperator(Query &query, QString column, QVariant value) {
|
||
|
||
}
|
||
|
||
void QueryBuilder::orOperator(Query &query,
|
||
QHash<QString, QVariant> conditions) {
|
||
|
||
}
|
||
|
||
QString QueryBuilder::where(const QSharedPointer<Entity> &entity,
|
||
QString conjunction,
|
||
bool ignoreID) const {
|
||
return this->where(this->getEntityAttributes(EntityHelper::getMetaProperties(
|
||
entity.data()),
|
||
entity),
|
||
conjunction, ignoreID, entity->getPrimaryKey());
|
||
}
|
src/querybuilder.h | ||
---|---|---|
#include <QStringList>
|
||
#include <QMetaProperty>
|
||
#include "relation.h"
|
||
#include "query.h"
|
||
namespace CuteEntityManager {
|
||
class Schema;
|
||
class Entity;
|
||
... | ... | |
bool ignoreID = false, const QString &primaryKey = "id") const;
|
||
void bindValue(const QString &key, const QVariant &value, QSqlQuery &q) const;
|
||
virtual QString placeHolder(const QString &key) const;
|
||
void where(Query &query,QString, QVariant);
|
||
void where(Query &query,QHash<QString, QVariant> conditions, QString concat="AND");
|
||
void where(Query &query,QHash<QString, QList<QVariant>> conditions, QString concat="AND");
|
||
void between(Query &query,QString column, QVariant firstValue, QVariant secondValue);
|
||
void in(Query &query,QString column, QList<QVariant> values);
|
||
void notIn(Query &query,QString column, QList<QVariant> values);
|
||
void notOperator(Query &query,QString column, QVariant value);
|
||
void orOperator(Query &query,QHash<QString, QVariant> conditions);
|
||
void andOperator(Query &query,QHash<QString, QVariant> conditions);
|
||
void arbitraryOperator(Query &query,QString op, QString column, QVariant value);
|
||
|
||
void plainOr(Query &query); //adds a simple OR to condition
|
||
void plainAnd(Query &query); //add a simple AND to condition
|
||
/**
|
||
* Generates 'foo' LIKE "%bar%"
|
||
* @brief like
|
||
* @param column
|
||
* @param value
|
||
*/
|
||
void like(QString column, QString value, JokerPosition = JokerPosition::BOTH);
|
||
/**
|
||
* @brief like
|
||
* @param condition
|
||
* @param concat
|
||
*/
|
||
void like(QHash<QString, QVariant> conditions, QString concat ="AND", JokerPosition = JokerPosition::BOTH);
|
||
|
||
|
||
|
||
|
||
|
||
protected:
|
Auch abrufbar als: Unified diff
started implementationof for queryconditions