Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (8,55 KB) Statistiken
| Zweig: | Revision:
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>
5c53ac99 Christian Ehringfeld
#include "relation.h"
b0bf458e Christian Ehringfeld
namespace CuteEntityManager {
class Schema;
class Entity;
class Database;
ce6994c4 Christian Ehringfeld
/**
RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
*/
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;
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;
QSqlQuery remove(const QSharedPointer<Entity> &entity) const;
QSqlQuery findId(const QSharedPointer<Entity> &entity) const;
QSqlQuery count(const QSharedPointer<Entity> &entity, bool ignoreID) const;
QSqlQuery count(const QString &tableName) const;
QSqlQuery merge(const QSharedPointer<Entity> &entity) const;
QSqlQuery create(const QSharedPointer<Entity> &entity) 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:
47f9301a 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;
e0e1ead8 Christian Ehringfeld
QHash<QString, QVariant> getManyToOneAttributes(const
QHash<QString, QMetaProperty>
&props,
dc6b13b4 Christian Ehringfeld
const QSharedPointer<Entity> &entity) 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,
bool ignoreID = false) const;
QString where(const QSharedPointer<Entity> &entity, QString conjunction = ",",
bool ignoreID = false) const;
QString where(const QHash<QString, QVariant> &m,
const QString &conjunction = ",",
bool ignoreID = false) const;
QString attributes(const QHash<QString, QVariant> &m,
const QString &conjunction = ",",
bool ignoreID = false) const;
QHash<QString, QVariant> saveAttributes(const QSharedPointer<Entity> &entity)
const;
QString leftJoin(const QString &foreignTable, const QString &tableName,
const QString &foreignKey);
1a3b37ba Christian Ehringfeld
QString superClassColumnName(const QMetaObject *&superMeta) const;
b0bf458e Christian Ehringfeld
QSharedPointer<Schema> schema;
QSharedPointer<Database> database;
14f9beed Christian Ehringfeld
};
b0bf458e Christian Ehringfeld
}
14f9beed Christian Ehringfeld
#endif // QUERYBUILDER_H