Search in sources :

Example 21 with TableReferenceFactory

use of org.sql.generation.api.grammar.factories.TableReferenceFactory in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method createSchemaAndRequiredTables.

private void createSchemaAndRequiredTables(Connection connection, String schemaName, Map<String, Long> tablePKs) throws SQLException {
    boolean schemaFound = false;
    ResultSet rs = connection.getMetaData().getSchemas();
    try {
        while (rs.next() && !schemaFound) {
            schemaFound = rs.getString(1).equals(schemaName);
        }
    } finally {
        SQLUtil.closeQuietly(rs);
    }
    SQLVendor vendor = this._vendor;
    DefinitionFactory d = vendor.getDefinitionFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    Statement stmt = connection.createStatement();
    // @formatter:off
    try {
        if (!schemaFound) {
            stmt.execute(vendor.toString(d.createSchemaDefinitionBuilder().setSchemaName(schemaName).createExpression()));
            _log.debug("Database schema created");
        }
        this.testRequiredCapabilities(connection);
        _log.debug("Underlying database fullfill required capabilities");
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, USED_CLASSES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(USED_CLASSES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(USED_CLASSES_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.UNIQUE).addColumns(USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(USED_CLASSES_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENTITY_TYPES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_TYPE_PK_TYPE), false)).addTableElement(d.createColumnDefinition(ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENTITY_TYPES_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.UNIQUE).addColumns(ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(ENTITY_TYPES_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false, AutoGenerationPolicy.BY_DEFAULT)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_IDENTITY_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_MODIFIED_COLUMN_NAME, this._primitiveTypes.get(Date.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_VERSION_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_APPLICATION_VERSION_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENTITY_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.UNIQUE).addColumns(ENTITY_TABLE_IDENTITY_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(ENTITY_TABLE_NAME, 0L);
        stmt.execute(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENTITY_TYPES_JOIN_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createColumnDefinition(ENTITY_TYPES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_TYPE_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENTITY_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).addTargetColumns(ENTITY_TABLE_PK_COLUMN_NAME).setOnDelete(ReferentialAction.CASCADE).setOnUpdate(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE)).addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ENTITY_TYPES_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).addTargetColumns(ENTITY_TYPES_TABLE_PK_COLUMN_NAME).setOnDelete(ReferentialAction.RESTRICT).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.NOT_DEFERRABLE)).createExpression()).createExpression().toString());
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENUM_LOOKUP_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENUM_LOOKUP_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(ENUM_LOOKUP_TABLE_ENUM_VALUE_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENUM_LOOKUP_TABLE_PK_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(ENUM_LOOKUP_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, USED_QNAMES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(USED_QNAMES_TABLE_QNAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createColumnDefinition(USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(USED_QNAMES_TABLE_QNAME_COLUMN_NAME, USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ALL_QNAMES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).addTargetColumns(ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE)).createExpression()).createExpression()));
        tablePKs.put(ALL_QNAMES_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, APP_VERSION_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(APP_VERSION_PK_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(APP_VERSION_PK_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        ModificationFactory m = vendor.getModificationFactory();
        PreparedStatement ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, APP_VERSION_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(vendor.getLiteralFactory().param()).createExpression()).createExpression()));
        ps.setString(1, this._app.version());
        ps.execute();
    // TODO INDICES!!!!
    } finally {
        SQLUtil.closeQuietly(stmt);
    }
    // @formatter:on
    _log.debug("Indexing SQL database tables created");
}
Also used : BigInteger(java.math.BigInteger) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLVendor(org.sql.generation.api.vendor.SQLVendor) PreparedStatement(java.sql.PreparedStatement) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory) Date(java.util.Date)

Aggregations

TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)21 LiteralFactory (org.sql.generation.api.grammar.factories.LiteralFactory)11 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)10 ModificationFactory (org.sql.generation.api.grammar.factories.ModificationFactory)9 BooleanFactory (org.sql.generation.api.grammar.factories.BooleanFactory)8 QuerySpecificationBuilder (org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder)6 QueryFactory (org.sql.generation.api.grammar.factories.QueryFactory)6 SQLVendor (org.sql.generation.api.vendor.SQLVendor)5 Statement (java.sql.Statement)4 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)4 DefinitionFactory (org.sql.generation.api.grammar.factories.DefinitionFactory)4 BigInteger (java.math.BigInteger)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 ColumnSourceByValuesBuilder (org.sql.generation.api.grammar.builders.modification.ColumnSourceByValuesBuilder)3 InsertStatement (org.sql.generation.api.grammar.modification.InsertStatement)3 Stack (java.util.Stack)2 QualifiedName (org.qi4j.api.common.QualifiedName)2 QNameType (org.qi4j.index.sql.support.common.QNameInfo.QNameType)2 BooleanBuilder (org.sql.generation.api.grammar.builders.booleans.BooleanBuilder)2