Revision 2e43da3f
Von Christian Ehringfeld vor mehr als 10 Jahren hinzugefügt
| src/entity.cpp | ||
|---|---|---|
|
this->id = -1;
|
||
|
}
|
||
|
|
||
|
QString Entity::toString() {
|
||
|
QString Entity::toString() const {
|
||
|
return this->getTablename() + ":" + QString::number(this->id);
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
|
||
|
}
|
||
|
|
||
|
QString Entity::getTablename() {
|
||
|
QString Entity::getTablename() const {
|
||
|
return QString(this->metaObject()->className()).toLower();
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
return QStringList();
|
||
|
}
|
||
|
|
||
|
const InheritanceStrategy Entity::getInheritanceStrategy() const {
|
||
|
InheritanceStrategy Entity::getInheritanceStrategy() const {
|
||
|
return JOINED_TABLE;
|
||
|
}
|
||
|
|
||
|
QString Entity::getPrimaryKey() {
|
||
|
QString Entity::getPrimaryKey() const {
|
||
|
return "id";
|
||
|
}
|
||
|
|
||
|
const QStack<const QMetaObject *> Entity::superClasses() const {
|
||
|
QStack<const QMetaObject *> classes = QStack<const QMetaObject *>();
|
||
|
auto superMetaObject = this->metaObject()->superClass();
|
||
|
if (this->getInheritanceStrategy() == JOINED_TABLE) {
|
||
|
while (QString(superMetaObject->className()) != QString("Entity")) {
|
||
|
classes.push(superMetaObject);
|
||
|
superMetaObject = superMetaObject->superClass();
|
||
|
}
|
||
|
}
|
||
|
return classes;
|
||
|
}
|
||
|
|
||
|
const QHash<QString, QMetaProperty> Entity::getMetaProperties() const {
|
||
|
return Entity::getMetaProperties(this->metaObject());
|
||
|
}
|
||
|
|
||
|
const QHash<QString, QMetaProperty> Entity::getMetaProperties(const QMetaObject *object) {
|
||
|
auto h = QHash<QString, QMetaProperty>();
|
||
|
for (int var = 0; var < this->metaObject()->propertyCount(); ++var) {
|
||
|
QMetaProperty m = this->metaObject()->property(var);
|
||
|
for (int var = 0; var < object->propertyCount(); ++var) {
|
||
|
QMetaProperty m = object->property(var);
|
||
|
if (m.isValid() && m.name() != QString("objectName")) {
|
||
|
h.insert(m.name(), m);
|
||
|
}
|
||
| ... | ... | |
|
return h;
|
||
|
}
|
||
|
|
||
|
const QHash<QString, QMetaProperty> Entity::getInheritedMetaProperties() const {
|
||
|
auto classes = this->superClasses();
|
||
|
auto wholeProperties = QHash<QString, QMetaProperty>();
|
||
|
while(!classes.isEmpty()) {
|
||
|
auto metaObject = classes.pop();
|
||
|
auto properties = Entity::getMetaProperties(metaObject);
|
||
|
auto iterator = properties.constBegin();
|
||
|
while (iterator != properties.constEnd()) {
|
||
|
wholeProperties.insert(iterator.key(),iterator.value());
|
||
|
++iterator;
|
||
|
}
|
||
|
}
|
||
|
return wholeProperties;
|
||
|
}
|
||
|
|
||
|
const QHash<Relation, QMetaProperty> Entity::getRelationProperties() const {
|
||
|
auto h = QHash<Relation, QMetaProperty>();
|
||
|
auto relations = this->getRelations();
|
||
| src/entity.h | ||
|---|---|---|
|
#include "relation.h"
|
||
|
#include <QStringList>
|
||
|
#include <QSharedPointer>
|
||
|
#include <QStack>
|
||
|
#include <QQueue>
|
||
|
namespace CuteEntityManager {
|
||
|
|
||
|
/**
|
||
| ... | ... | |
|
|
||
|
public:
|
||
|
Entity (QObject *parent = 0);
|
||
|
virtual QString toString();
|
||
|
virtual QString toString() const;
|
||
|
virtual ~Entity();
|
||
|
virtual QString getTablename();
|
||
|
virtual QString getTablename() const;
|
||
|
/**
|
||
|
* @brief getRelations
|
||
|
* @return
|
||
| ... | ... | |
|
virtual const QHash<QString, Relation> getRelations() const;
|
||
|
virtual const QStringList getTransientAttributes() const;
|
||
|
virtual const QStringList getBLOBColumns() const;
|
||
|
virtual const InheritanceStrategy getInheritanceStrategy() const;
|
||
|
virtual InheritanceStrategy getInheritanceStrategy() const;
|
||
|
|
||
|
//return value must be the exact name defined in Q_PROPERTY
|
||
|
virtual QString getPrimaryKey();
|
||
|
virtual QString getPrimaryKey() const;
|
||
|
const QStack<const QMetaObject *> superClasses() const;
|
||
|
const QHash<QString, QMetaProperty> getMetaProperties() const;
|
||
|
static const QHash<QString, QMetaProperty> getMetaProperties(const QMetaObject* object);
|
||
|
const QHash<QString, QMetaProperty> getInheritedMetaProperties() const;
|
||
|
const QHash<Relation, QMetaProperty> getRelationProperties() const;
|
||
|
const char *getClassname() const;
|
||
|
|
||
| src/querybuilder.cpp | ||
|---|---|---|
|
}
|
||
|
|
||
|
bool QueryBuilder::createTable(const QSharedPointer<Entity> &entity) const {
|
||
|
return this->createTable(entity.data()->getTablename(),
|
||
|
this->generateTableDefinition(entity));
|
||
|
}
|
||
|
|
||
|
bool QueryBuilder::createTable(const QString &tableName,
|
||
|
const QHash<QString, QString> &tableDefinition) const {
|
||
|
bool rc = false;
|
||
|
if(entity.data()) {
|
||
|
auto tableDefinition = this->generateTableDefinition(entity);
|
||
|
QString tableName = entity.data()->getTablename();
|
||
|
this->schema.data()->containsTable(tableName) ? rc = true : rc = false;
|
||
|
if (!rc) {
|
||
|
QSqlQuery q = this->database.data()->getQuery(this->createTableQuery(tableName,
|
||
|
tableDefinition));
|
||
|
QSqlQuery q = this->database.data()->getQuery(this->createTable(tableName,tableDefinition));
|
||
|
if (this->database.data()->transaction(q)) {
|
||
|
this->schema.data()->getTableSchema(tableName);
|
||
|
rc = true;
|
||
|
if(rc) {
|
||
|
rc = this->createIndices(entity);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return rc;
|
||
|
|
||
|
}
|
||
|
|
||
|
bool QueryBuilder::createIndices(const QSharedPointer<Entity> &entity) const {
|
||
|
Entity *e = entity.data();
|
||
|
bool ok = true;
|
||
|
QStringList queries = QStringList();
|
||
|
QString superIndex = this->createFkSuperClass(e);
|
||
|
if(!superIndex.isEmpty()) {
|
||
|
queries.append(superIndex);
|
||
|
}
|
||
|
/**
|
||
|
@todo Relations
|
||
|
*/
|
||
|
ok = this->database.data()->transaction(queries);
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::createTable(const QString &tableName, const QHash<QString, QString> &tableDefinition) const
|
||
|
{
|
||
|
return this->createTableQuery(tableName,
|
||
|
tableDefinition);
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::createFkSuperClass(const Entity *e) const
|
||
|
{
|
||
|
QString r = "";
|
||
|
auto superMetaObject = e->metaObject()->superClass();
|
||
|
|
||
|
if (e->getInheritanceStrategy() == JOINED_TABLE
|
||
|
&& QString(superMetaObject->className()) != QString("Entity")) {
|
||
|
Entity *superClass = EntityInstanceFactory::createInstance(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(),QStringList(e->getPrimaryKey()),refTable,QStringList(refColumn),"CASCADE","CASCADE");
|
||
|
delete superClass;
|
||
|
}
|
||
|
}
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::createTableQuery(const QString &tableName,
|
||
| ... | ... | |
|
this->schema.data()->quoteColumnName(name);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
|
||
|
*/
|
||
|
QString QueryBuilder::addForeignKey(QString name, QString tableName,
|
||
|
QStringList columns,
|
||
|
QString refTableName,
|
||
| ... | ... | |
|
return r;
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::generateIndexName(const QString &name,
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
QString QueryBuilder::dropForeignKey(QString name, QString tableName) const {
|
||
|
return "ALTER TABLE " + this->schema.data()->quoteTableName(
|
||
|
tableName) + " DROP CONSTRAINT " +
|
||
| ... | ... | |
|
QString QueryBuilder::dropIndex(QString name, QString tableName) const {
|
||
|
return "DROP INDEX " + this->schema.data()->quoteTableName(name) + " ON " +
|
||
|
this->schema.data()->quoteTableName(
|
||
|
tableName);
|
||
|
tableName);
|
||
|
}
|
||
|
|
||
|
QSharedPointer<Database> QueryBuilder::getDatabase() const {
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
}
|
||
|
QString pkType = map.value(entity.data()->getPrimaryKey());
|
||
|
if (entity.data()->getInheritanceStrategy() != JOINED_TABLE
|
||
|
|| QString(superMetaObject->className()) == QString("Entity")) {
|
||
|
if (pkType == this->schema.data()->TYPE_BIGINT) {
|
||
|
map.insert(entity.data()->getPrimaryKey(), this->schema.data()->TYPE_BIGPK);
|
||
|
} else {
|
||
|
map.insert(entity.data()->getPrimaryKey(), this->schema.data()->TYPE_PK);
|
||
|
}
|
||
|
if (QString(superMetaObject->className()) != QString("Entity")
|
||
|
&& entity.data()->getInheritanceStrategy() != JOINED_TABLE) {
|
||
|
map.insert(entity.data()->getPrimaryKey(), this->schema.data()->TYPE_BIGPK);
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
| src/querybuilder.h | ||
|---|---|---|
|
QueryBuilder(QSharedPointer<Schema> schema, QSharedPointer<Database> database);
|
||
|
virtual ~QueryBuilder();
|
||
|
virtual bool createTable(const QSharedPointer<Entity> &entity) const;
|
||
|
virtual bool createTable(const QString &tableName,
|
||
|
virtual bool createIndices(const QSharedPointer<Entity> &entity) const;
|
||
|
virtual QString createTable(const QString &tableName,
|
||
|
const QHash<QString, QString> &tableDefinition) const;
|
||
|
virtual QString createTableQuery(const QString &tableName,
|
||
|
const QHash<QString, QString> &tableDefinition) const;
|
||
| ... | ... | |
|
QString refTableName,
|
||
|
QStringList refColumns, QString deleteConstraint,
|
||
|
QString updateConstraint) const;
|
||
|
QString generateIndexName(const QString &name,const QString &table,const QString &refColumn,const QString &refTable,const bool fk) const;
|
||
|
virtual QString dropForeignKey(QString name, QString tableName) const;
|
||
|
virtual QString createIndex(QString name, QString tableName,
|
||
|
QStringList columns,
|
||
|
bool unique)const;
|
||
|
virtual QString dropIndex(QString name, QString tableName)const;
|
||
|
virtual QString createFkSuperClass(const Entity *e) const;
|
||
|
QHash<QString, QVariant> getEntityAttributes(const QHash<QString, QMetaProperty>
|
||
|
&props,
|
||
|
const QSharedPointer<Entity> &entity) const;
|
||
Auch abrufbar als: Unified diff
small update