Herunterladen als
root/src/querybuilder.h @ 827458ed
c22391b2 | Christian Ehringfeld | /*
|
|
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|||
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
14f9beed | Christian Ehringfeld | #ifndef QUERYBUILDER_H
|
|
#define QUERYBUILDER_H
|
|||
b0bf458e | Christian Ehringfeld | #include <QString>
|
|
#include <QHash>
|
|||
#include <QSharedPointer>
|
|||
a47954c0 | Christian Ehringfeld | #include <QPointer>
|
|
dc6b13b4 | Christian Ehringfeld | #include <QSqlQuery>
|
|
f5087482 | Christian Ehringfeld | #include <QStringList>
|
|
99140fe9 | Christian Ehringfeld | #include <QMetaProperty>
|
|
5c53ac99 | Christian Ehringfeld | #include "relation.h"
|
|
506067a2 | Christian Ehringfeld | #include "expression.h"
|
|
82442988 | Christian Ehringfeld | #include "query.h"
|
|
b0bf458e | Christian Ehringfeld | namespace CuteEntityManager {
|
|
class Schema;
|
|||
class Entity;
|
|||
class Database;
|
|||
3938a37e | Christian Ehringfeld | class Logger;
|
|
ce6994c4 | Christian Ehringfeld | enum DbForeignKeyCascade {
|
|
RESTRICT,
|
|||
CASCADE,
|
|||
NO_ACTION,
|
|||
SET_DEFAULT,
|
|||
SET_NULL,
|
|||
};
|
|||
b0bf458e | Christian Ehringfeld | class QueryBuilder {
|
|
86e5c917 | Christian Ehringfeld | /**
|
|
* EntityManager is a friend class, cause we want a light public api.
|
|||
*/
|
|||
friend class EntityManager;
|
|||
3160499c | Christian Ehringfeld | friend class QueryInterpreter;
|
|
d27d606d | Christian Ehringfeld | friend class AttributeResolver;
|
|
b0bf458e | Christian Ehringfeld | public:
|
|
57d6da31 | Christian Ehringfeld | QueryBuilder(Schema *schema, QSharedPointer<Database> &database);
|
|
b0bf458e | Christian Ehringfeld | virtual ~QueryBuilder();
|
|
b7446f4c | Christian Ehringfeld | virtual bool createTable(const QSharedPointer<Entity> &entity,
|
|
bool createRelationTables = true) const;
|
|||
2e43da3f | Christian Ehringfeld | virtual bool createIndices(const QSharedPointer<Entity> &entity) const;
|
|
virtual QString createTable(const QString &tableName,
|
|||
47f9301a | Christian Ehringfeld | const QHash<QString, QString> &tableDefinition) const;
|
|
2fedfe76 | Christian Ehringfeld | virtual bool createRelationTables(const QSharedPointer<Entity> &entity) const;
|
|
e0e1ead8 | Christian Ehringfeld | virtual QString createTableQuery(const QString &tableName,
|
|
const QHash<QString, QString> &tableDefinition) const;
|
|||
d99101ae | Christian Ehringfeld | virtual QString renameTable(QString tableName, QString newName) const;
|
|
virtual QString dropTable(QString tableName) const;
|
|||
virtual QString truncateTable(QString tableName) const;
|
|||
e0e1ead8 | Christian Ehringfeld | virtual QString addColumn(QString tableName, QString columnName,
|
|
QString columnType) const;
|
|||
b0bf458e | Christian Ehringfeld | virtual QString dropColumn(QString tableName, QString columName)const;
|
|
e0e1ead8 | Christian Ehringfeld | virtual QString renameColumn(QString tableName, QString oldName,
|
|
QString newName) const;
|
|||
virtual QString alterColumn(QString tableName, QString columnName,
|
|||
QString newType)const;
|
|||
virtual QString addPrimaryKey(QString name, QString tableName,
|
|||
QStringList columns)const;
|
|||
b0bf458e | Christian Ehringfeld | virtual QString dropPrimaryKey(QString name, QString tableName) const;
|
|
e0e1ead8 | Christian Ehringfeld | virtual QString addForeignKey(QString name, QString tableName,
|
|
QStringList columns,
|
|||
QString refTableName,
|
|||
QStringList refColumns, QString deleteConstraint,
|
|||
QString updateConstraint) const;
|
|||
ce6994c4 | Christian Ehringfeld | virtual QString getForeignKeyCascade(DbForeignKeyCascade cascade) const;
|
|
b0bf458e | Christian Ehringfeld | virtual QString dropForeignKey(QString name, QString tableName) const;
|
|
e0e1ead8 | Christian Ehringfeld | virtual QString createIndex(QString name, QString tableName,
|
|
QStringList columns,
|
|||
bool unique)const;
|
|||
b0bf458e | Christian Ehringfeld | virtual QString dropIndex(QString name, QString tableName)const;
|
|
2e43da3f | Christian Ehringfeld | virtual QString createFkSuperClass(const Entity *e) const;
|
|
47f9301a | Christian Ehringfeld | virtual QStringList relationFks(const QSharedPointer<Entity> &entity) const;
|
|
b0e92bc6 | Christian Ehringfeld | virtual bool supportsForeignKeys() const;
|
|
47f9301a | Christian Ehringfeld | ||
virtual QString createForeignKeyManyToMany(const QString &tableName,
|
|||
const QSharedPointer<Entity> &entity, const QString &update,
|
|||
const QString &remove) const;
|
|||
ba800d6d | Christian Ehringfeld | ||
b0bf458e | Christian Ehringfeld | QSharedPointer<Database> getDatabase() const;
|
|
void setDatabase(const QSharedPointer<Database> &value);
|
|||
e0e1ead8 | Christian Ehringfeld | QHash<QString, QHash<QString, QString>> generateRelationTables(
|
|
const QSharedPointer<Entity> &entity)
|
|||
const;
|
|||
QHash<QString, QString> generateTableDefinition(const QSharedPointer<Entity>
|
|||
&entity)
|
|||
const;
|
|||
86e5c917 | Christian Ehringfeld | QSqlQuery getQuery() const;
|
|
6dc7d533 | Christian Ehringfeld | QString transformTypeToAbstractDbType(QString typeName) const;
|
|
QString transformAbstractTypeToRealDbType(QString typeName) const;
|
|||
a47954c0 | Christian Ehringfeld | QString getColumnType(const QString &type) const;
|
|
ea6e88f6 | Christian Ehringfeld | virtual QString placeHolder(QString key) const;
|
|
94bf67c7 | Christian Ehringfeld | void bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q,
|
|
c9f21778 | Christian Ehringfeld | bool ignoreID = false, const QString &primaryKey = QStringLiteral("id")) const;
|
|
94bf67c7 | Christian Ehringfeld | void bindValue(const QString &key, const QVariant &value, QSqlQuery &q) const;
|
|
358e1e04 | Christian Ehringfeld | /**
|
|
* @brief where
|
|||
* @param c if value is a single value than c is the column name if value is a QHash<QString, QVariant> than its a condition
|
|||
* @param value
|
|||
* @return
|
|||
*/
|
|||
Expression where(QString c, QVariant value);
|
|||
274b34e4 | Christian Ehringfeld | Join joinClasses(const QSharedPointer<Entity> &mainEntity,
|
|
const QSharedPointer<Entity> &foreignEntity,
|
|||
const QString &joinType = QStringLiteral("LEFT JOIN"))const;
|
|||
c9f21778 | Christian Ehringfeld | QList<Join> joinBaseClasses(const QSharedPointer<Entity> &entity);
|
|
584721e5 | Christian Ehringfeld | /**
|
|
* @brief where
|
|||
* @param query
|
|||
* @param conditions
|
|||
* @param conjunction its AND or OR
|
|||
*/
|
|||
506067a2 | Christian Ehringfeld | Expression where(QHash<QString, QVariant> conditions,
|
|
274b34e4 | Christian Ehringfeld | QString conjunction = QStringLiteral("AND")) const;
|
|
dbf92b46 | Christian Ehringfeld | Expression equal(QString &key, QVariant &value);
|
|
Expression notEqual(QString &key, QVariant &value);
|
|||
86e5c917 | Christian Ehringfeld | //void where(Query &query,QHash<QString, QList<QVariant>> conditions, QString concat="AND");
|
|
506067a2 | Christian Ehringfeld | Expression between(QString column, QVariant firstValue,
|
|
274b34e4 | Christian Ehringfeld | QVariant secondValue) const;
|
|
506067a2 | Christian Ehringfeld | Expression notBetween(QString column, QVariant firstValue,
|
|
274b34e4 | Christian Ehringfeld | QVariant secondValue) const;
|
|
Expression in(QString column, QList<QVariant> values) const;
|
|||
Expression notIn(QString column, QList<QVariant> values) const;
|
|||
Expression notOperator(QString op, QString column,
|
|||
QVariant value) const;
|
|||
506067a2 | Christian Ehringfeld | Expression orOperator(QHash<QString, QVariant> conditions,
|
|
274b34e4 | Christian Ehringfeld | bool like = false) const;
|
|
Expression orOperator() const;
|
|||
Expression norOperator() const;
|
|||
Expression andOperator(QHash<QString, QVariant> conditions) const;
|
|||
Expression andOperator() const;
|
|||
Expression nandOperator() const;
|
|||
584721e5 | Christian Ehringfeld | /**
|
|
* @brief arbitraryOperator
|
|||
* @param query
|
|||
* @param op can be = | != | <> | > | < | >= | <= | !< | !> @see http://www.tutorialspoint.com/sql/sql-operators.htm comparison operators
|
|||
* @param column
|
|||
* @param value
|
|||
*/
|
|||
506067a2 | Christian Ehringfeld | Expression arbitraryOperator(QString op, QString column,
|
|
274b34e4 | Christian Ehringfeld | QVariant value) const;
|
|
Expression isNull(QString column) const;
|
|||
Expression isNotNull(QString column) const;
|
|||
5d93390e | Christian Ehringfeld | ||
274b34e4 | Christian Ehringfeld | Expression plainOr() const; //adds a simple OR to condition
|
|
Expression plainNor() const;
|
|||
Expression plainAnd() const; //add a simple AND to condition
|
|||
Expression plainNand() const;
|
|||
5d93390e | Christian Ehringfeld | /**
|
|
* Generates 'foo' LIKE "%bar%"
|
|||
* @brief like
|
|||
* @param column
|
|||
* @param value
|
|||
*/
|
|||
506067a2 | Christian Ehringfeld | Expression like(QString column, QVariant value,
|
|
67aa0d3f | Christian Ehringfeld | JokerPosition jp = JokerPosition::BOTH, QChar wildcard = '%');
|
|
5d93390e | Christian Ehringfeld | /**
|
|
* @brief like
|
|||
* @param condition
|
|||
* @param concat
|
|||
*/
|
|||
506067a2 | Christian Ehringfeld | Expression like(QHash<QString, QVariant> conditions,
|
|
c9f21778 | Christian Ehringfeld | QString conjunction = QStringLiteral("AND"),
|
|
67aa0d3f | Christian Ehringfeld | JokerPosition jp = JokerPosition::BOTH, QChar wildcard = '%');
|
|
b0bf458e | Christian Ehringfeld | ||
protected:
|
|||
8275b945 | Christian Ehringfeld | class ClassAttributes {
|
|
99140fe9 | Christian Ehringfeld | public:
|
|
ClassAttributes() { }
|
|||
explicit ClassAttributes(const QString name,
|
|||
const QHash<QString, QVariant> attributes, QString pk = "id");
|
|||
8275b945 | Christian Ehringfeld | QString getName() const;
|
|
void setName(const QString &value);
|
|||
QHash<QString, QVariant> getAttributes() const;
|
|||
void setAttributes(const QHash<QString, QVariant> &value);
|
|||
99140fe9 | Christian Ehringfeld | QString getPk() const;
|
|
void setPk(const QString &value);
|
|||
private:
|
|||
8275b945 | Christian Ehringfeld | QString name;
|
|
99140fe9 | Christian Ehringfeld | QString pk;
|
|
8275b945 | Christian Ehringfeld | QHash<QString, QVariant> attributes;
|
|
};
|
|||
82442988 | Christian Ehringfeld | ||
QStringList quoteTableNames(const QStringList &tables);
|
|||
QString getSeparator() const;
|
|||
void setSeparator(const QString &value);
|
|||
86e5c917 | Christian Ehringfeld | 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;
|
|||
373a84e2 | Christian Ehringfeld | Query findByAttributes(const QHash<QString, QVariant> &m,
|
|
86e5c917 | Christian Ehringfeld | const QString &tableName,
|
|
const bool &ignoreID = true, const qint64 limit = 0,
|
|||
const qint64 offset = 0) const;
|
|||
373a84e2 | Christian Ehringfeld | Query findByAttributes(const QSharedPointer<Entity> &e,
|
|
86e5c917 | Christian Ehringfeld | bool ignoreID = true,
|
|
const qint64 limit = 0, const qint64 offset = 0);
|
|||
QSqlQuery findAll(const QString &tableName) const;
|
|||
QSqlQuery findAll(const QSharedPointer<Entity> &entity, const qint64 limit = 0,
|
|||
qint64 offset = 0);
|
|||
QList<QSqlQuery> remove(const QSharedPointer<Entity> &entity) const;
|
|||
QSqlQuery findId(const QSharedPointer<Entity> &entity) const;
|
|||
QSqlQuery count(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;
|
|||
373a84e2 | Christian Ehringfeld | Query oneToMany(const QString &tableName, const QString &attribute,
|
|
86e5c917 | Christian Ehringfeld | const qint64 &id,
|
|
const qint64 &limit = 0);
|
|||
QSqlQuery manyToMany(const QString &tableName, const QString &attribute,
|
|||
const qint64 &id);
|
|||
QSqlQuery manyToManyDelete(const QString &tableName, const QString &attribute,
|
|||
const qint64 &id);
|
|||
QSqlQuery manyToManyInsert(const QString &tableName, const QString &col1,
|
|||
const QString &col2) const;
|
|||
da3ce9cf | Christian Ehringfeld | QSqlQuery remove(const QString &tableName, const qint64 &id,
|
|
const QString &primaryKey = "id") const;
|
|||
QSqlQuery insert(const QString &tableName, QHash<QString, QVariant> &attributes,
|
|||
94bf67c7 | Christian Ehringfeld | const QString &primaryKey = "id", bool withId = false) const;
|
|
afde9013 | Christian Ehringfeld | QSqlQuery update(const QString &tableName, QHash<QString, QVariant> &attributes,
|
|
const QString &primaryKey = "id") const;
|
|||
992116ed | Christian Ehringfeld | QList<QSqlQuery> createOrMerge(const QSharedPointer<Entity> &entity,
|
|
bool insert) const;
|
|||
67aa0d3f | Christian Ehringfeld | virtual QString limit(const quint64 &limit, const quint64 &offset,
|
|
bool withSpace = true) const;
|
|||
86e5c917 | Christian Ehringfeld | QString generateIndexName(const QString &name, const QString &table,
|
|
const QString &refColumn, const QString &refTable, const bool fk) const;
|
|||
QString generateColumnNameID(QString name) const;
|
|||
554f7bc0 | Christian Ehringfeld | QString columnNameIDAppendix() const;
|
|
f5087482 | Christian Ehringfeld | virtual void createRelationFK(QStringList &queries,
|
|
const QSharedPointer<Entity> &entity, const Relation &relation,
|
|||
const QMetaProperty &metaProperty, const QString &update,
|
|||
const QString &remove) const;
|
|||
e0e1ead8 | Christian Ehringfeld | void insertRelationId(const Entity *e, QHash<QString, QVariant> &map,
|
|
QString relName) const;
|
|||
d99101ae | Christian Ehringfeld | QString buildColumns(const QStringList &columns) const;
|
|
99140fe9 | Christian Ehringfeld | QHash<QString, QVariant> getManyToOneAttributes(QHash<QString, QMetaProperty>
|
|
props,
|
|||
const QSharedPointer<Entity> &entity,
|
|||
QHash<QString, Relation> relations = QHash<QString, Relation>()) const;
|
|||
e0e1ead8 | Christian Ehringfeld | QHash<QString, QMetaProperty> getMetaProperties(const QSharedPointer<Entity>
|
|
&entity)
|
|||
const;
|
|||
QHash<QString, QVariant> getPropertyValues(const QHash<QString, QMetaProperty>
|
|||
&metaProps,
|
|||
dc6b13b4 | Christian Ehringfeld | const QSharedPointer<Entity> &entity) const;
|
|
86e5c917 | Christian Ehringfeld | QString generateManyToManyTableName(const QSharedPointer<Entity> &firstEntity,
|
|
const QSharedPointer<Entity> &secondEntity, const Relation &r) const;
|
|||
d27d606d | Christian Ehringfeld | QString generateManyToManyTableName(const QString &tableName,
|
|
const QString &attribute) const;
|
|||
QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity,
|
|||
QString attribute) const;
|
|||
e0e1ead8 | Christian Ehringfeld | QString buildCreateQuery(QHash<QString, QVariant>::const_iterator i,
|
|
QHash<QString, QVariant>::const_iterator end,
|
|||
dc6b13b4 | Christian Ehringfeld | QString &p1, QString &p2) const;
|
|
e0e1ead8 | Christian Ehringfeld | QString where(const QSharedPointer<Entity> &entity, QString conjunction = ",",
|
|
bool ignoreID = false) const;
|
|||
QString where(const QHash<QString, QVariant> &m,
|
|||
274b34e4 | Christian Ehringfeld | const QString &conjunction,
|
|
358e1e04 | Christian Ehringfeld | bool ignoreID, const QString &primaryKey = "id",
|
|
86e5c917 | Christian Ehringfeld | bool withKeyword = true) const;
|
|
358e1e04 | Christian Ehringfeld | QString where(const QString &key, const QVariant &var, bool withKeyword,
|
|
bool select = true, bool notEqual = false) const;
|
|||
87739ae0 | Christian Ehringfeld | QString attributes(const QHash<QString, QVariant> &m, bool select = true,
|
|
e0e1ead8 | Christian Ehringfeld | const QString &conjunction = ",",
|
|
da3ce9cf | Christian Ehringfeld | bool ignoreID = false, const QString &primaryKey = "id") const;
|
|
99140fe9 | Christian Ehringfeld | QHash<QString, QVariant> saveAttributes(const QSharedPointer<Entity> &entity,
|
|
QHash<QString, QMetaProperty> props = QHash<QString, QMetaProperty> (),
|
|||
QHash<QString, Relation> relations = QHash<QString, Relation>())
|
|||
e0e1ead8 | Christian Ehringfeld | const;
|
|
99140fe9 | Christian Ehringfeld | QHash<QString, QMetaProperty> processProperties(const QSharedPointer<Entity> &e,
|
|
QHash<QString, QMetaProperty> &usedProperties) const;
|
|||
QHash<QString, Relation> processRelations(const QSharedPointer<Entity> &e,
|
|||
QHash<QString, Relation> &usedRelations) const;
|
|||
8275b945 | Christian Ehringfeld | QList<ClassAttributes> inheritedAttributes(
|
|
99140fe9 | Christian Ehringfeld | const QSharedPointer<Entity> &entity) const;
|
|
8275b945 | Christian Ehringfeld | ||
e0e1ead8 | Christian Ehringfeld | QString leftJoin(const QString &foreignTable, const QString &tableName,
|
|
274b34e4 | Christian Ehringfeld | const QString &foreignKey = "id", const QString &primaryKey = "id",
|
|
bool onlyCondition = false) const;
|
|||
1a3b37ba | Christian Ehringfeld | QString superClassColumnName(const QMetaObject *&superMeta) const;
|
|
2ee5022f | Christian Ehringfeld | QString addWildcard(QVariant var, JokerPosition jp,
|
|
QChar jokerChar = '%') const;
|
|||
b0bf458e | Christian Ehringfeld | ||
f5087482 | Christian Ehringfeld | QString joinSuperClasses(const QSharedPointer<Entity> &entity) const;
|
|
virtual QString selectBase(const QStringList &tables,
|
|||
373a84e2 | Christian Ehringfeld | const QStringList &columns = QStringList(), bool withKeyword=true) const;
|
|
f5087482 | Christian Ehringfeld | virtual QString countFunction(const QString &distinctColumn = "") const;
|
|
virtual QString distinct() const;
|
|||
31916fa0 | Christian Ehringfeld | virtual QString notKeyword() const;
|
|
virtual QString between() const;
|
|||
virtual QString andKeyword() const;
|
|||
virtual QString orKeyword() const;
|
|||
virtual QString inKeyword() const;
|
|||
virtual QString whereKeyword() const;
|
|||
virtual QString countKeyword() const;
|
|||
dbf92b46 | Christian Ehringfeld | virtual QString notEqualOperator() const;
|
|
virtual QString equalOperator() const;
|
|||
virtual QString fromKeyword() const;
|
|||
virtual QString isNullKeywords() const;
|
|||
virtual QString isNotNullKeywords() const;
|
|||
506067a2 | Christian Ehringfeld | virtual Expression inFunction(QString column, QList<QVariant> values,
|
|
274b34e4 | Christian Ehringfeld | bool notOp = false) const;
|
|
86e5c917 | Christian Ehringfeld | virtual QString between(QString colName, QString valName1, QString valName2,
|
|
274b34e4 | Christian Ehringfeld | bool notOp = false) const;
|
|
584721e5 | Christian Ehringfeld | virtual QString likeKeyword() const;
|
|
virtual QString limitKeyword() const;
|
|||
virtual QString offsetKeyword() const;
|
|||
274b34e4 | Christian Ehringfeld | QString appendNot(bool notOp) const;
|
|
506067a2 | Christian Ehringfeld | virtual Expression appendCondition(QString ph1, QString ph2, QVariant val1,
|
|
274b34e4 | Christian Ehringfeld | QVariant val2, QString condition) const;
|
|
35cf13b7 | Christian Ehringfeld | QString entityClassname() const;
|
|
57d6da31 | Christian Ehringfeld | Schema *getSchema() const;
|
|
3160499c | Christian Ehringfeld | QString separator;
|
|
57d6da31 | Christian Ehringfeld | Schema *schema;
|
|
b0bf458e | Christian Ehringfeld | QSharedPointer<Database> database;
|
|
14f9beed | Christian Ehringfeld | };
|
|
b0bf458e | Christian Ehringfeld | }
|
|
14f9beed | Christian Ehringfeld | ||
#endif // QUERYBUILDER_H
|