Herunterladen als
root/src/querybuilder.h @ 3bfe1c1b
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"
|
|
b0bf458e | Christian Ehringfeld | namespace CuteEntityManager {
|
|
class Schema;
|
|||
class Entity;
|
|||
class Database;
|
|||
f5087482 | Christian Ehringfeld | ||
ce6994c4 | Christian Ehringfeld | enum DbForeignKeyCascade {
|
|
RESTRICT,
|
|||
CASCADE,
|
|||
NO_ACTION,
|
|||
SET_DEFAULT,
|
|||
SET_NULL,
|
|||
};
|
|||
b0bf458e | Christian Ehringfeld | class QueryBuilder {
|
|
public:
|
|||
QueryBuilder(QSharedPointer<Schema> schema, QSharedPointer<Database> database);
|
|||
virtual ~QueryBuilder();
|
|||
virtual bool createTable(const QSharedPointer<Entity> &entity) 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;
|
|
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;
|
|||
47f9301a | Christian Ehringfeld | QString generateIndexName(const QString &name, const QString &table,
|
|
const QString &refColumn, const QString &refTable, const bool fk) const;
|
|||
f62afb13 | Christian Ehringfeld | QString generateColumnNameID(QString name) 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;
|
|
e0e1ead8 | Christian Ehringfeld | QHash<QString, QVariant> getEntityAttributes(const QHash<QString, QMetaProperty>
|
|
&props,
|
|||
dc6b13b4 | Christian Ehringfeld | const QSharedPointer<Entity> &entity) 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<Schema> getSchema() const;
|
|
void setSchema(const QSharedPointer<Schema> &value);
|
|||
14f9beed | 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;
|
|||
ba800d6d | Christian Ehringfeld | QString generateManyToManyTableName(const QSharedPointer<Entity> &firstEntity,
|
|
const QSharedPointer<Entity> &secondEntity) const;
|
|||
b0bf458e | Christian Ehringfeld | ||
6dc7d533 | Christian Ehringfeld | QString transformTypeToAbstractDbType(QString typeName) const;
|
|
QString transformAbstractTypeToRealDbType(QString typeName) const;
|
|||
a47954c0 | Christian Ehringfeld | QString getColumnType(const QString &type) const;
|
|
dc6b13b4 | Christian Ehringfeld | QSqlQuery find(const qint64 &id, const QString &tableName) const;
|
|
6d91d381 | Christian Ehringfeld | QSqlQuery find(const qint64 &id, const QSharedPointer<Entity> &entity,
|
|
afde9013 | Christian Ehringfeld | qint64 offset = 0, QString pk = "id") const;
|
|
e0e1ead8 | Christian Ehringfeld | QSqlQuery 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,
|
|||
bool ignoreID = true,
|
|||
const qint64 limit = 0, const qint64 offset = 0);
|
|||
dc6b13b4 | Christian Ehringfeld | QSqlQuery findAll(const QString &tableName) const;
|
|
f5087482 | Christian Ehringfeld | QSqlQuery findAll(const QSharedPointer<Entity> &entity, const qint64 limit = 0,
|
|
qint64 offset = 0);
|
|||
95b60eb2 | Christian Ehringfeld | QList<QSqlQuery> remove(const QSharedPointer<Entity> &entity) const;
|
|
dc6b13b4 | Christian Ehringfeld | QSqlQuery findId(const QSharedPointer<Entity> &entity) const;
|
|
QSqlQuery count(const QSharedPointer<Entity> &entity, bool ignoreID) const;
|
|||
QSqlQuery count(const QString &tableName) const;
|
|||
afde9013 | Christian Ehringfeld | QList<QSqlQuery> merge(const QSharedPointer<Entity> &entity) const;
|
|
99140fe9 | Christian Ehringfeld | QList<QSqlQuery> create(const QSharedPointer<Entity> &entity) const;
|
|
8306a974 | Christian Ehringfeld | QSqlQuery removeAll(const QString &tableName) const;
|
|
e0e1ead8 | Christian Ehringfeld | QSqlQuery oneToMany(const QString &tableName, const QString &attribute,
|
|
const qint64 &id,
|
|||
const qint64 &limit = 0);
|
|||
QSqlQuery manyToMany(const QString &tableName, const QString &attribute,
|
|||
const qint64 &id,
|
|||
const QString &foreignKey,
|
|||
97846191 | Christian Ehringfeld | const QString &foreignTable);
|
|
98b5b08d | Christian Ehringfeld | QSqlQuery manyToManyDelete(const QString &tableName, const QString &attribute,
|
|
const qint64 &id);
|
|||
QSqlQuery manyToManyInsert(const QString &tableName, const QString &col1,
|
|||
const QString &col2) const;
|
|||
c599658a | Christian Ehringfeld | virtual QString limit(const qint64 &limit, const qint64 &offset) const;
|
|
e0e1ead8 | Christian Ehringfeld | QString generateManyToManyColumnName(const QSharedPointer<Entity> &entity)
|
|
const;
|
|||
11bbe9a6 | Christian Ehringfeld | QSqlQuery getQuery() const;
|
|
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;
|
|
};
|
|||
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,
|
|||
const QString &primaryKey = "id") 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;
|
|||
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;
|
|
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 | void bindValues(const QHash<QString, QVariant> &h, QSqlQuery &q,
|
|
da3ce9cf | Christian Ehringfeld | bool ignoreID = false, const QString &primaryKey = "id") const;
|
|
e0e1ead8 | Christian Ehringfeld | QString where(const QSharedPointer<Entity> &entity, QString conjunction = ",",
|
|
bool ignoreID = false) const;
|
|||
QString where(const QHash<QString, QVariant> &m,
|
|||
const QString &conjunction = ",",
|
|||
da3ce9cf | Christian Ehringfeld | bool ignoreID = false, const QString &primaryKey = "id") const;
|
|
e0e1ead8 | Christian Ehringfeld | QString attributes(const QHash<QString, QVariant> &m,
|
|
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,
|
|
f5087482 | Christian Ehringfeld | const QString &foreignKey = "id", const QString &primaryKey = "id") const;
|
|
1a3b37ba | Christian Ehringfeld | QString superClassColumnName(const QMetaObject *&superMeta) const;
|
|
b0bf458e | Christian Ehringfeld | ||
f5087482 | Christian Ehringfeld | QString joinSuperClasses(const QSharedPointer<Entity> &entity) const;
|
|
virtual QString selectBase(const QStringList &tables,
|
|||
const QStringList &columns = QStringList()) const;
|
|||
virtual QString countFunction(const QString &distinctColumn = "") const;
|
|||
virtual QString distinct() const;
|
|||
b0bf458e | Christian Ehringfeld | QSharedPointer<Schema> schema;
|
|
QSharedPointer<Database> database;
|
|||
14f9beed | Christian Ehringfeld | };
|
|
b0bf458e | Christian Ehringfeld | }
|
|
14f9beed | Christian Ehringfeld | ||
#endif // QUERYBUILDER_H
|