Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

DBSchemaGenerator Class Reference

Class that is able to generate CREATE SCHEMA \ DROP SCHEMA \ EXPLAIN PLAN SQL statements for given class hierarchy. More...

#include <DBSchemaGenerator.h>

Inheritance diagram for DBSchemaGenerator:

Inheritance graph
[legend]
Collaboration diagram for DBSchemaGenerator:

Collaboration graph
[legend]
List of all members.

Public Types

typedef std::set< CStdString > TableColumnsSet

Public Member Functions

int generateColumnTypes (DatabaseSqlStatements *sqlStatements, CStdString &warningsForUser, CStdString &errorForUser)
int generateCreationScript (DatabaseSqlStatements *sqlStatements, bool generateComments, CStdString &creationSql, CStdString &warningsForUser, CStdString &errorForUser)
int generateDropScript (DatabaseSqlStatements *sqlStatements, CStdString &dropSql, CStdString &warningsForUser, CStdString &errorForUser)
int generateExplainScript (DatabaseSqlStatements *sqlStatements, CStdString &explainSql, CStdString &warningsForUser, CStdString &errorForUser)

Protected Member Functions

int generateViews (DatabaseSqlStatements *sqlStatements, bool generateComments, CStdString &viewsSql, CStdString &warningsForUser, CStdString &errorForUser)
int generateSimpleView (FullClassInfo *classInfo, DatabaseSqlStatements *sqlStatements, CStdString &createViewSql, CStdString &warningsForUser, CStdString &errorForUser)
int generateSimpleViewSelectHorizontal (FullClassInfo *classInfo, DatabaseSqlStatements *sqlStatements, CStdString &selectStmt, std::list< CStdString > &viewColumns, CStdString &warningsForUser, CStdString &errorForUser)
int generateSimpleViewSelectVertical (FullClassInfo *classInfo, DatabaseSqlStatements *sqlStatements, CStdString &selectStmt, std::list< CStdString > &viewColumns, CStdString &warningsForUser, CStdString &errorForUser)
int generateSimpleViewSelectFiltered (FullClassInfo *classInfo, DatabaseSqlStatements *sqlStatements, CStdString &selectStmt, std::list< CStdString > &viewColumns, CStdString &warningsForUser, CStdString &errorForUser)
void addParentsTables (FullClassInfo *classInfo, std::map< CStdString, TableColumnsSet > &tables)
int generatePolymorphicView (FullClassInfo *classInfo, DatabaseSqlStatements *sqlStatements, CStdString &createViewSql, CStdString &warningsForUser, CStdString &errorForUser)
void generatePolymorphicViewHelper (FullClassInfo *classInfo, std::list< FullClassInfo * > &horizontalAncestors, std::list< int > &classIDsForMainSelect, bool addClassIDs)
int generateTableComment (const TableInfo *tableInfo, DatabaseSqlStatements *sqlStatements, CStdString &tableComment, CStdString &warningsForUser, CStdString &errorForUser)
void generateClassIDs ()

Static Protected Member Functions

void qualifyColumnNames (const std::list< CStdString > &columnNames, const std::map< CStdString, TableColumnsSet > &tables, std::list< CStdString > &qualifiedColumnNames)

Detailed Description

Class that is able to generate CREATE SCHEMA \ DROP SCHEMA \ EXPLAIN PLAN SQL statements for given class hierarchy.

Inherited from ClassHierarchy it has the data to proceed.

All public functions of this class takes pointer to DatabaseSqlStatements as parameter. Pointed object is used to make it possible to generate different SQL for different databases / database vendors.


Member Typedef Documentation

typedef std::set<CStdString> DBSchemaGenerator::TableColumnsSet
 

Set of columns in one table. Used by addParentsTables().


Member Function Documentation

int DBSchemaGenerator::generateColumnTypes DatabaseSqlStatements sqlStatements,
CStdString &  warningsForUser,
CStdString &  errorForUser
 

Generates DB column type for every persistent class attribute, for which the user hasn't set it in XML. If the user has set it, checks whether the type is correct.

Parameters:
sqlStatements Object that generates database vendor specific SQL statements.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.
See also:
DatabaseSqlStatements::checkColumnTypeCompatibility(), DatabaseSqlStatements::getRecommendedColumnType()

int DBSchemaGenerator::generateCreationScript DatabaseSqlStatements sqlStatements,
bool  generateComments,
CStdString &  creationSql,
CStdString &  warningsForUser,
CStdString &  errorForUser
 

Generates SQL script that creates all tables, views, indexes etc. that are needed for the application to run.

Calls generateTableInfoMap() to acquire information about tables that should be generated, generates support for OID generation (DatabaseSqlStatements::generateOidGenerationSupportCreationScript()), project's class ID table, project's main table (+ index on it). Then generates all common class tables (+ indexes on class IDs if needed), fills in project's main table and calls generateViews() to get definitions of all views needed for application to run.

Note:
generateMissingValues() must be called before this function.
Parameters:
sqlStatements Object that generates database vendor specific SQL statements.
generateComments Should comments be generated into the script?
creationSql Result of this function - the script.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.
See also:
DatabaseSqlStatements::getRecommendedColumnType(), DatabaseSqlStatements::generateCreateTableSql(), DatabaseSqlStatements::generateCreateIndexSql(), DatabaseSqlStatements::generateInsertSqlSimple(), DatabaseSqlStatements::generateComment(), generateTableComment()

int DBSchemaGenerator::generateDropScript DatabaseSqlStatements sqlStatements,
CStdString &  dropSql,
CStdString &  warningsForUser,
CStdString &  errorForUser
 

Generates SQL script that drops all tables, views, indexes etc. created by script generated by generateCreationScript().

Note:
generateMissingValues() must be called before this function.
Parameters:
sqlStatements Object that generates database vendor specific SQL statements.
dropSql Result of this function - the script.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.
See also:
DatabaseSqlStatements::generateDropViewSql(), DatabaseSqlStatements::generateDropIndexSql(), DatabaseSqlStatements::generateDropTableSql(), DatabaseSqlStatements::generateOidGenerationSupportDropScript()

int DBSchemaGenerator::generateExplainScript DatabaseSqlStatements sqlStatements,
CStdString &  explainSql,
CStdString &  warningsForUser,
CStdString &  errorForUser
 

Generates SQL script that can be used to explain execution plans for getting data from classes' simple and polymorphic view (i.e. SELECT * FROM <view_name>).

Note:
generateMissingValues() must be called before this function.
Parameters:
sqlStatements Object that generates database vendor specific SQL statements.
explainSql Result of this function - the script.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.
See also:
DatabaseSqlStatements::generateExplainPlanScript(), generateSimpleView(), generatePolymorphicView(), DatabaseSqlStatements::generateSelectSql()

int DBSchemaGenerator::generateViews DatabaseSqlStatements sqlStatements,
bool  generateComments,
CStdString &  viewsSql,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates script that can be used to create simple and polymorphic views for all classes.

Traverses classMap and for each class calls generateSimpleView() and generatePolymorphicView().

Parameters:
sqlStatements Object that generates database vendor specific SQL statements.
generateComments Should comments be generated into the script?
viewsSql Result of this function - the script.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.

int DBSchemaGenerator::generateSimpleView FullClassInfo classInfo,
DatabaseSqlStatements sqlStatements,
CStdString &  createViewSql,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates SQL statement that creates simple view for given class (classInfo).

According to class's mappingType calls one of these functions: generateSimpleViewSelectHorizontal(), generateSimpleViewSelectVertical(), generateSimpleViewSelectFiltered(). Then takes acquired SELECT statement and generates CREATE VIEW statement from it using DatabaseSqlStatements::generateCreateViewSql().

Parameters:
classInfo Class whose simple view should be created.
sqlStatements Object that generates database vendor specific SQL statements.
createViewSql Result of this function - the SQL statement.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.

int DBSchemaGenerator::generateSimpleViewSelectHorizontal FullClassInfo classInfo,
DatabaseSqlStatements sqlStatements,
CStdString &  selectStmt,
std::list< CStdString > &  viewColumns,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates SELECT part of SQL statement that creates simple view for given class (classInfo). The class must use horizontal mapping.

The creation of the SELECT is trivial in this case - it's enough to SELECT all the columns from class's table (FullClassInfo::dbTable) that don't belong to any of class's filtered ancestors. WHERE clause selects only the rows with the right class ID.

Parameters:
classInfo Class whose simple view should be created.
sqlStatements Object that generates database vendor specific SQL statements.
selectStmt Result of this function - the SELECT part of the SQL statement.
viewColumns List of names of view columns. For now empty list is returned -> column names are not put into view definition. (This makes the view definition easier to be read.)
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
See also:
DatabaseSqlStatements::generateWhereClauseStmtPart(), DatabaseSqlStatements::generateSelectSql()

int DBSchemaGenerator::generateSimpleViewSelectVertical FullClassInfo classInfo,
DatabaseSqlStatements sqlStatements,
CStdString &  selectStmt,
std::list< CStdString > &  viewColumns,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates SELECT part of SQL statement that creates simple view for given class (classInfo). The class must use vertical mapping.

The SELECT is more complicated in this case. Firstly, map of tables that contain data from this class is created (using addParentsTables()), then all these tables are joined using OID. WHERE clause specifies only the rows with the right class ID and only columns coming from classInfo attributes (including inherited ones) are SELECTed.

Parameters:
classInfo Class whose simple view should be created.
sqlStatements Object that generates database vendor specific SQL statements.
selectStmt Result of this function - the SELECT part of the SQL statement.
viewColumns List of names of view columns.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
See also:
DatabaseSqlStatements::generateWhereClauseStmtPart(), DatabaseSqlStatements::generateSelectSql()

int DBSchemaGenerator::generateSimpleViewSelectFiltered FullClassInfo classInfo,
DatabaseSqlStatements sqlStatements,
CStdString &  selectStmt,
std::list< CStdString > &  viewColumns,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates SELECT part of SQL statement that creates simple view for given class (classInfo). The class must use filtered mapping.

Map of tables that contain data from this class is created (using addParentsTables()), then all these tables are joined using OID. WHERE clause specifies only the rows with the right class ID and only columns coming from classInfo attributes (including inherited ones) are SELECTed.

Parameters:
classInfo Class whose simple view should be created.
sqlStatements Object that generates database vendor specific SQL statements.
selectStmt Result of this function - the SELECT part of the SQL statement.
viewColumns List of names of view columns.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
See also:
DatabaseSqlStatements::generateWhereClauseStmtPart(), DatabaseSqlStatements::generateSelectSql()

void DBSchemaGenerator::addParentsTables FullClassInfo classInfo,
std::map< CStdString, TableColumnsSet > &  tables
[protected]
 

Walks through all classInfo's parents and adds to tables names of all tables, in which classInfo's instances store their data, together with sets of table columns. For horizontal mapping parent returns only name of its table, for filtered mapping one function calls itself on all its parents, for vertical one adds its table and calls itself on all its parents.

Parameters:
classInfo Class whose parents should be processed.
tables The resulting map - key: table name, value: map of table columns.

void DBSchemaGenerator::qualifyColumnNames const std::list< CStdString > &  columnNames,
const std::map< CStdString, TableColumnsSet > &  tables,
std::list< CStdString > &  qualifiedColumnNames
[static, protected]
 

Due to multiple inheritance it is sometimes necessary to specify column names fully qualified if creating views (if we have two horizontal mapping ancestors that inherit from one class and some descendant of the two classes uses filtered or vertical mapping).

This function fully qualifies given columnNames. It search for each column from columnNames in given tables and if the column is found, places table_name.column_name into qualifiedColumnNames. If it doesn't find the column in any of given tables, it leaves the column unqualified.

Note:
If more than one table from tables contains given column, it doesn't matter which of these tables we use - values in these columns are the same.
Parameters:
columnNames Columns names to be qualified.
tables Tables (as received from addParentsTables()) to be searched.
qualifiedColumnNames The result of the function - qualified column names.

int DBSchemaGenerator::generatePolymorphicView FullClassInfo classInfo,
DatabaseSqlStatements sqlStatements,
CStdString &  createViewSql,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates SQL statement that creates polymorphic view for given class (classInfo).

Columns of the view corresponds to persistent attributes of classInfo.

Gets all classInfo ancestors that uses horizontal mapping (using generatePolymorphicViewHelper()) - selecting columns corresponding to persistent attributes of classInfo gives us all but one SELECTs that are later united to create the view.

The last SELECT is the main one. It is similar to the select of classInfo simple view. So, it is generated in a similar way. If class doesn't use horizontal mapping, map of tables that contain data from this class is created (using addParentsTables()), these tables are put into FROM clause and joined using OIDs. WHERE clause resticts the selected rows only to ancestors of this class (list of appropriate class IDs is another result of generatePolymorphicViewHelper()). This restriction is needed if using filtered mapping.

Note:
Uses FullClassInfo::flags.
Parameters:
classInfo Class whose polymorphic view should be created.
sqlStatements Object that generates database vendor specific SQL statements.
createViewSql Result of this function - the SQL statement.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.

void DBSchemaGenerator::generatePolymorphicViewHelper FullClassInfo classInfo,
std::list< FullClassInfo * > &  horizontalAncestors,
std::list< int > &  classIDsForMainSelect,
bool  addClassIDs
[protected]
 

Helper function used by generatePolymorphicView()

Adds classInfo to horizontalAncestors if it uses horizontal mapping, calls itself recursively on classInfo children. If addClassIDs is set, adds to classIDsForMainSelect class IDs of ancestors stored in the same table as classInfo class instances.

Note:
Uses FullClassInfo::flags.
Parameters:
classInfo Class whose polymorphic view should be created.
horizontalAncestors List to that the horizontal ancestors of classInfo are added.
classIDsForMainSelect The list of class IDs.
addClassIDs Should classIDsForMainSelect be filled in?

int DBSchemaGenerator::generateTableComment const TableInfo tableInfo,
DatabaseSqlStatements sqlStatements,
CStdString &  tableComment,
CStdString &  warningsForUser,
CStdString &  errorForUser
[protected]
 

Generates comment that says which attributes of tableInfo were declared in which class.

Parameters:
tableInfo Information about the table whose comments should be created.
sqlStatements Object that generates database vendor specific SQL statements.
tableComment The result of the function - the table comment.
warningsForUser String used to send warning message(s) to user if needed.
errorForUser String used to send an error message to user if needed.
Returns:
0 if OK, non-zero in case of error.

void DBSchemaGenerator::generateClassIDs  )  [protected]
 

Traverses the class hierarchy and sets FullClassInfo::classID using a counter, starting from 1.


The documentation for this class was generated from the following files:
Generated on Wed Aug 11 22:35:26 2004 for IOPC by Doxygen 1.3.6