Revision ce6994c4
Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt
src/entityinstancefactory.cpp | ||
---|---|---|
}
|
||
return r;
|
||
}
|
||
|
||
Entity *EntityInstanceFactory::newSuperClassInstance(Entity *e) {
|
||
Entity *super = 0;
|
||
if (e) {
|
||
auto metaObject = e->metaObject()->superClass();
|
||
if (QString(metaObject->className()) != QString("Entity")) {
|
||
super = EntityInstanceFactory::createInstance(metaObject->className());
|
||
}
|
||
}
|
||
return super;
|
||
}
|
src/entityinstancefactory.h | ||
---|---|---|
QHash<QString, QMetaProperty> metaprops);
|
||
static Entity *setAttributes(Entity *e, const QHash<QString, QVariant> &attributes);
|
||
static const QString extractEntityType(const QString &s);
|
||
static Entity *newSuperClassInstance(Entity *e);
|
||
|
||
template<typename T>
|
||
static Entity *createInstance() {
|
src/querybuilder.cpp | ||
---|---|---|
|
||
bool QueryBuilder::createTable(const QSharedPointer<Entity> &entity) const {
|
||
bool rc = false;
|
||
if(entity.data()) {
|
||
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->createTable(tableName,tableDefinition));
|
||
if (this->database.data()->transaction(q)) {
|
||
this->schema.data()->getTableSchema(tableName);
|
||
rc = true;
|
||
if(rc) {
|
||
rc = this->createIndices(entity);
|
||
this->schema.data()->containsTable(tableName) ? rc = true : rc = false;
|
||
if (!rc) {
|
||
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 ok = true;
|
||
QStringList queries = QStringList();
|
||
QString superIndex = this->createFkSuperClass(e);
|
||
if(!superIndex.isEmpty()) {
|
||
if (!superIndex.isEmpty()) {
|
||
queries.append(superIndex);
|
||
}
|
||
queries.append(this->relationIndices(e));
|
||
... | ... | |
}
|
||
|
||
|
||
QStringList QueryBuilder::relationIndices(const Entity *e) const
|
||
{
|
||
QStringList QueryBuilder::relationIndices(const Entity *e) const {
|
||
QStringList queries = QStringList();
|
||
auto relations = e->getRelations();
|
||
auto superObject = EntityInstanceFactory::newSuperClassInstance(e);
|
||
if(superObject) {
|
||
auto superRelations = superObject->getRelations();
|
||
auto iterator = superRelations.constBegin();
|
||
while(iterator != relations.constEnd()) {
|
||
if(relations.contains(iterator.key())) {
|
||
relations.remove(iterator.key());
|
||
}
|
||
++iterator;
|
||
}
|
||
delete superObject;
|
||
superObject = 0;
|
||
}
|
||
auto iterator = relations.constBegin();
|
||
while(iterator != relations.constEnd()) {
|
||
|
||
|
||
while (iterator != relations.constEnd()) {
|
||
auto relation = iterator.value();
|
||
if(!relation.getCascadeType().isEmpty()) {
|
||
switch (relation.getType()) {
|
||
case ONE_TO_MANY:
|
||
break;
|
||
case ONE_TO_ONE:
|
||
break;
|
||
case MANY_TO_MANY:
|
||
break;
|
||
case MANY_TO_ONE:
|
||
break;
|
||
}
|
||
}
|
||
++iterator;
|
||
}
|
||
return queries;
|
||
}
|
||
|
||
|
||
QString QueryBuilder::createTable(const QString &tableName, const QHash<QString, QString> &tableDefinition) const
|
||
{
|
||
QString QueryBuilder::createTable(const QString &tableName,
|
||
const QHash<QString, QString> &tableDefinition) const {
|
||
return this->createTableQuery(tableName,
|
||
tableDefinition);
|
||
tableDefinition);
|
||
}
|
||
|
||
QString QueryBuilder::createFkSuperClass(const Entity *e) const
|
||
{
|
||
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;
|
||
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), this->getForeignKeyCascade(CASCADE),
|
||
this->getForeignKeyCascade(CASCADE));
|
||
delete superClass;
|
||
}
|
||
}
|
||
return r;
|
||
... | ... | |
this->schema.data()->quoteColumnName(name);
|
||
}
|
||
|
||
|
||
/**
|
||
RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
|
||
*/
|
||
QString QueryBuilder::addForeignKey(QString name, QString tableName,
|
||
QStringList columns,
|
||
QString refTableName,
|
||
... | ... | |
refColumn).append(refTable);
|
||
}
|
||
|
||
QString QueryBuilder::getForeignKeyCascade(DbForeignKeyCascade cascade) const {
|
||
switch (cascade) {
|
||
case RESTRICT:
|
||
return "RESTRICT";
|
||
break;
|
||
case CASCADE:
|
||
return "CASCADE";
|
||
break;
|
||
case NO_ACTION:
|
||
return "NO ACTION";
|
||
break;
|
||
case SET_DEFAULT:
|
||
return "SET DEFAULT";
|
||
break;
|
||
case SET_NULL:
|
||
return "SET NULL";
|
||
break;
|
||
}
|
||
}
|
||
|
||
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 {
|
src/querybuilder.h | ||
---|---|---|
class Schema;
|
||
class Entity;
|
||
class Database;
|
||
/**
|
||
RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
|
||
*/
|
||
enum DbForeignKeyCascade {
|
||
RESTRICT,
|
||
CASCADE,
|
||
NO_ACTION,
|
||
SET_DEFAULT,
|
||
SET_NULL,
|
||
};
|
||
|
||
class QueryBuilder {
|
||
public:
|
||
QueryBuilder(QSharedPointer<Schema> schema, QSharedPointer<Database> database);
|
||
... | ... | |
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 getForeignKeyCascade(DbForeignKeyCascade cascade) const;
|
||
virtual QString dropForeignKey(QString name, QString tableName) const;
|
||
virtual QString createIndex(QString name, QString tableName,
|
||
QStringList columns,
|
Auch abrufbar als: Unified diff
...