181 lines
6.7 KiB
C++
Executable File
181 lines
6.7 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Generate SQL statements from Xml service files
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
#pragma once
|
|
|
|
#include <QStringList>
|
|
#include <QSharedPointer>
|
|
#include "StructBuilder.h"
|
|
#include "codegen_export.h"
|
|
|
|
namespace NGL
|
|
{
|
|
class INGLLogger;
|
|
}
|
|
|
|
namespace CodeGen
|
|
{
|
|
struct ColumnInformation;
|
|
struct ConstraintInformation;
|
|
struct EnumInformation;
|
|
struct ForeignKeyInformation;
|
|
struct TableInformation;
|
|
class XmlReader;
|
|
|
|
class CODEGEN_BASE_EXPORT SqlGenerator final
|
|
{
|
|
public:
|
|
struct SqlStatements
|
|
{
|
|
QStringList dropViews;
|
|
QStringList dropTables;
|
|
QStringList dropSequences;
|
|
QStringList dropConstraints;
|
|
|
|
QStringList createTables;
|
|
QStringList createSequences;
|
|
QStringList createPrimaryKeys;
|
|
QStringList createForeignKeys;
|
|
QStringList createViews;
|
|
QStringList createConstraints;
|
|
|
|
QStringList enumerationValues; // tables are created within createTables
|
|
QStringList dbFunctions;
|
|
QStringList dbFunctionTrigger;
|
|
|
|
bool isEmpty() const
|
|
{
|
|
return dropViews.isEmpty() && dropTables.isEmpty() && dropSequences.isEmpty() && dropConstraints.isEmpty();
|
|
}
|
|
};
|
|
|
|
struct SqlItems
|
|
{
|
|
QStringList tables; // all tables (just names)
|
|
QStringList sequences; // all sequences (just names)
|
|
QStringList primaryKeys;
|
|
QStringList foreignKeys;
|
|
QStringList constraints;
|
|
};
|
|
|
|
public:
|
|
Q_DISABLE_COPY(SqlGenerator)
|
|
|
|
explicit SqlGenerator(XmlReader *xmlReader, StructBuilder *structBuilder);
|
|
explicit SqlGenerator(const QString &xmlFile);
|
|
|
|
~SqlGenerator();
|
|
|
|
bool isValid() const { return m_isValid; }
|
|
// exerciseId -> use exerciseId if ExerciseDependent tables are found
|
|
bool generateData(int exerciseId);
|
|
|
|
bool createStatementsForTable(const QString &tableName, const QString &tableNameOut);
|
|
|
|
SqlStatements getStatements(ExerciseDependency ed) const
|
|
{
|
|
return (ed == ExerciseDependency::Exercise ? m_sqlExercise : m_sqlGlobal);
|
|
}
|
|
|
|
SqlItems getSqlItems(ExerciseDependency ed) const
|
|
{
|
|
return (ed == ExerciseDependency::Exercise ? m_sqlItemsExercise : m_sqlItemsGlobal);
|
|
}
|
|
|
|
QStringList getEnumerationInsertValues(const QString &table, const QString &dbSchema) const;
|
|
QStringList getReferencedServices() const // returns the abbreviations
|
|
{
|
|
return m_referencedSevices;
|
|
}
|
|
|
|
QStringList getDbSettings(const QString &dbSchema) const;
|
|
|
|
// Writes
|
|
bool writeSql(const QString &filePath, ExerciseDependency ed);
|
|
|
|
bool writeSqlToFile(const QString &dbSchema, const QString &sqlFile, ExerciseDependency ed) const;
|
|
|
|
// Methods from XmlReader
|
|
QString getServiceAbbr() const { return m_xmlReader->getServiceAbbr(); }
|
|
bool isServiceExerciseIndependent() const { return m_xmlReader->isServiceExerciseIndependent(); }
|
|
bool isServiceGlobal() const { return (m_xmlReader->defaultExerciseDependency() == CodeGen::ExerciseDependency::Global || m_xmlReader->defaultExerciseDependency() == CodeGen::ExerciseDependency::GlobalAndExercise); }
|
|
CodeGen::ExerciseDependency getExerciseDependency() const { return m_xmlReader->defaultExerciseDependency();}
|
|
QStringList getGroupNames() const { return m_xmlReader->getGroupNames(); }
|
|
|
|
QStringList getAllSqlStmts(const QString &dbSchema, ExerciseDependency ed) const;
|
|
|
|
|
|
protected:
|
|
|
|
private:
|
|
bool handleTables();
|
|
bool handleViews();
|
|
bool handleTable(const CompleteTableInformation &cti);
|
|
bool handleView(const ViewInformation &vi);
|
|
QString getTableColumnCreateStatements(const QString &tableName,
|
|
const QVector<MemberInformation> &columns,
|
|
QStringList *tableComments, ExerciseDependency ed);
|
|
QString generateSequence(const QString &tableName, const FullQualifiedColumnInformation &ci, ExerciseDependency ed);
|
|
void generatePrimaryKey(const QString &tableName, const FullQualifiedColumnInformation &ci, ExerciseDependency ed);
|
|
void generateForeignKeys(const QVector<QSharedPointer<ForeignKeyInformation> > &foreignKeys, ExerciseDependency ed);
|
|
void generateForeignKey(const QSharedPointer<ForeignKeyInformation> &foreignKey, ExerciseDependency ed);
|
|
void generateConstraints(const QString &tableName, const QVector<ConstraintInformation> &constraints, ExerciseDependency ed);
|
|
void generateConstraint(const QString &tableName, const ConstraintInformation &constraint, ExerciseDependency ed);
|
|
bool handleEnumerations();
|
|
bool handleEnumeration(const EnumInformation &ei);
|
|
QStringList handleEnumeration(const EnumInformation &ei, const QString &dbSchema) const;
|
|
bool generateDbNotifications(const QString &tableName, const QVector<DbNotifcationInformation> ¬ifications, ExerciseDependency ed);
|
|
void generateDbNotification(const DbNotifcationInformation ¬ification, ExerciseDependency ed);
|
|
bool generateDbNotificationTrigger(const QString &tableName, const DbNotifcationInformation ¬ification, ExerciseDependency ed);
|
|
QString getNotificationFunctionName(const DbNotifcationInformation ¬ification) const;
|
|
static QString fixupComment(const QString &comment);
|
|
|
|
private:
|
|
XmlReader *m_xmlReader;
|
|
StructBuilder *m_structBuilder;
|
|
bool m_createdByXmlFile;
|
|
|
|
void addToDropSequences(const QString &stmt, ExerciseDependency ed);
|
|
void addToDropTables(const QString &stmt, ExerciseDependency ed);
|
|
void addToDropViews(const QString &stmt, ExerciseDependency ed);
|
|
void addToDropConstraints(const QString &stmt, ExerciseDependency ed);
|
|
|
|
void addToCreateConstraints(const QString &stmt, ExerciseDependency ed);
|
|
void addToCreateFks(const QString &stmt, ExerciseDependency ed);
|
|
void addToCreatePks(const QString &stmt, ExerciseDependency ed);
|
|
void addToCreateSequences(const QString &stmt, ExerciseDependency ed);
|
|
void addToCreateTables(const QString &stmt, ExerciseDependency ed);
|
|
void addToCreateTables(const QStringList &stmts, ExerciseDependency ed);
|
|
void addToCreateViews(const QString &stmt, ExerciseDependency ed);
|
|
|
|
void addToDbFunctions(const QString &stmt, ExerciseDependency ed);
|
|
void addToDbFunctionTrigger(const QString &stmt, ExerciseDependency ed);
|
|
|
|
void addToConstraints(const QString &name, ExerciseDependency ed);
|
|
void addToFks(const QString &name, ExerciseDependency ed);
|
|
void addToPks(const QString &name, ExerciseDependency ed);
|
|
void addToSequences(const QString &name, ExerciseDependency ed);
|
|
void addToTables(const QString &name, ExerciseDependency ed);
|
|
|
|
bool writeFile(const QString &fn, const QString &content) const;
|
|
|
|
SqlStatements m_sqlGlobal;
|
|
SqlStatements m_sqlExercise;
|
|
|
|
SqlItems m_sqlItemsGlobal;
|
|
SqlItems m_sqlItemsExercise;
|
|
|
|
QStringList m_referencedSevices;
|
|
|
|
bool m_isValid;
|
|
int m_exerciseId;
|
|
|
|
NGL::INGLLogger &m_logger;
|
|
};
|
|
|
|
} // namespace CodeGen
|