Search in sources :

Example 1 with DefinitionFactory

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

the class AbstractSQLStartup method appendColumnDefinitionsForProperty.

private void appendColumnDefinitionsForProperty(TableElementListBuilder builder, QNameInfo qNameInfo) {
    Type finalType = qNameInfo.getFinalType();
    if (finalType instanceof ParameterizedType) {
        finalType = ((ParameterizedType) finalType).getRawType();
    }
    Class<?> finalClass = (Class<?>) finalType;
    SQLDataType sqlType = null;
    String valueRefTableName = null;
    String valueRefTablePKColumnName = null;
    if (qNameInfo.isFinalTypePrimitive()) {
        if (this._customizableTypes.keySet().contains(finalClass) && qNameInfo.getPropertyDescriptor().accessor().isAnnotationPresent(SQLTypeInfo.class)) {
            sqlType = this._customizableTypes.get(finalClass).customizeType(finalClass, qNameInfo.getPropertyDescriptor().accessor().getAnnotation(SQLTypeInfo.class));
        } else if (Enum.class.isAssignableFrom(finalClass)) {
            // Enum - reference the lookup table
            sqlType = this._primitiveTypes.get(Integer.class);
            valueRefTableName = ENUM_LOOKUP_TABLE_NAME;
            valueRefTablePKColumnName = ENUM_LOOKUP_TABLE_PK_COLUMN_NAME;
        } else {
            // Primitive type, default sqlType
            sqlType = this._primitiveTypes.get(finalClass);
        }
        if (sqlType == null) {
            throw new InternalError("Could not find sql type for java type [" + finalType + "]");
        }
    } else {
        // Value composite - just need used class
        sqlType = this._primitiveTypes.get(Integer.class);
        valueRefTableName = USED_CLASSES_TABLE_NAME;
        valueRefTablePKColumnName = USED_CLASSES_TABLE_PK_COLUMN_NAME;
    }
    SQLVendor vendor = this._vendor;
    DefinitionFactory d = vendor.getDefinitionFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, sqlType, qNameInfo.getCollectionDepth() > 0)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
    if (valueRefTableName != null && valueRefTablePKColumnName != null) {
        builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_VALUE_COLUMN_NAME).setTargetTableName(t.tableName(this._state.schemaName().get(), valueRefTableName)).addTargetColumns(valueRefTablePKColumnName).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.RESTRICT).createExpression(), ConstraintCharacteristics.NOT_DEFERRABLE));
    }
}
Also used : SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) SQLTypeInfo(org.qi4j.index.sql.support.api.SQLTypeInfo) ParameterizedType(java.lang.reflect.ParameterizedType) BigInteger(java.math.BigInteger) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) Type(java.lang.reflect.Type) ObjectType(org.sql.generation.api.grammar.manipulation.ObjectType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) ParameterizedType(java.lang.reflect.ParameterizedType) SQLVendor(org.sql.generation.api.vendor.SQLVendor) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory)

Example 2 with DefinitionFactory

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

the class AbstractSQLStartup method writeAppMetadataToDB.

private void writeAppMetadataToDB(Connection connection, ApplicationInfo appInfo, Map<String, Long> tablePKs) throws SQLException {
    String schemaName = this._state.schemaName().get();
    SQLVendor vendor = this._vendor;
    ModificationFactory m = vendor.getModificationFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    // @formatter:off
    PreparedStatement ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
    try {
        Set<String> insertedTypeNames = new HashSet<String>();
        for (EntityDescriptor descriptor : appInfo.entityDescriptors.values()) {
            for (Class<?> entityType : descriptor.types()) {
                String entityTypeName = entityType.getName();
                if (!insertedTypeNames.contains(entityTypeName)) {
                    long pk = tablePKs.get(ENTITY_TYPES_TABLE_NAME);
                    ps.setInt(1, (int) pk);
                    ps.setString(2, entityTypeName);
                    ps.executeUpdate();
                    this._state.entityTypePKs().get().put(entityTypeName, (int) pk);
                    // this._state.entityTypeInfos().get().put( entityTypeName, new EntityTypeInfo( descriptor, (int) pk ) );
                    tablePKs.put(ENTITY_TYPES_TABLE_NAME, pk + 1);
                }
            }
        }
    } finally {
        SQLUtil.closeQuietly(ps);
    }
    ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, USED_CLASSES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
    try {
        for (CompositeDescriptorInfo descInfo : appInfo.usedValueComposites) {
            String vDescStr = compositeDescriptorToString(descInfo.layer, descInfo.module, descInfo.composite);
            long pk = tablePKs.get(USED_CLASSES_TABLE_NAME);
            ps.setInt(1, (int) pk);
            ps.setString(2, vDescStr);
            ps.executeUpdate();
            this._state.usedClassesPKs().get().put(descInfo.composite, (int) pk);
            tablePKs.put(USED_CLASSES_TABLE_NAME, pk + 1);
        }
    } finally {
        SQLUtil.closeQuietly(ps);
    }
    ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, ENUM_LOOKUP_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
    try {
        for (String enumValue : appInfo.enumValues) {
            long pk = tablePKs.get(ENUM_LOOKUP_TABLE_NAME);
            ps.setInt(1, (int) pk);
            ps.setString(2, enumValue);
            ps.executeUpdate();
            this._state.enumPKs().get().put(enumValue, (int) pk);
            tablePKs.put(ENUM_LOOKUP_TABLE_NAME, pk + 1);
        }
    } finally {
        SQLUtil.closeQuietly(ps);
    }
    Statement stmt = connection.createStatement();
    ps = connection.prepareStatement(this.createInsertStatementForQNameInfo(connection, schemaName, vendor).toString());
    try {
        DefinitionFactory d = vendor.getDefinitionFactory();
        for (QNameInfo qNameInfo : this._state.qNameInfos().get().values()) {
            QNameType type = qNameInfo.getQNameType();
            TableElementListBuilder builder = d.createTableElementListBuilder();
            builder.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));
            if (type.equals(QNameType.PROPERTY)) {
                builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, this._primitiveTypes.get(Integer.class), true));
                if (qNameInfo.getCollectionDepth() > 0) {
                    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME, this.getCollectionPathDataType(), false));
                }
                this.appendColumnDefinitionsForProperty(builder, qNameInfo);
                builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).addTargetColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
            } else {
                if (type.equals(QNameType.ASSOCIATION)) {
                    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_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()));
                } else if (type.equals(QNameType.MANY_ASSOCIATION)) {
                    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_ASSOCIATION_INDEX_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_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()));
                } else {
                    throw new IllegalArgumentException("Did not how to create table for qName type: " + type + ".");
                }
                builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_VALUE_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));
                tablePKs.put(qNameInfo.getTableName(), 0L);
            }
            builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).addTargetColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
            stmt.execute(this._vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, qNameInfo.getTableName())).setTableContentsSource(builder.createExpression()).createExpression()));
            // stmt.execute( "COMMENT ON TABLE " + schemaName + "." + qNameInfo.getTableName() + " IS '"
            // + qNameInfo.getQName() + "'" );
            ps.setString(1, qNameInfo.getQName().toString());
            ps.setString(2, qNameInfo.getTableName());
            ps.execute();
        }
    } finally {
        SQLUtil.closeQuietly(stmt);
        SQLUtil.closeQuietly(ps);
    }
// @formatter:off
}
Also used : QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) TableElementListBuilder(org.sql.generation.api.grammar.builders.definition.TableElementListBuilder) PreparedStatement(java.sql.PreparedStatement) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory) BigInteger(java.math.BigInteger) ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) SQLVendor(org.sql.generation.api.vendor.SQLVendor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory) HashSet(java.util.HashSet)

Example 3 with DefinitionFactory

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

the class PostgreSQLAppStartup method testRequiredCapabilities.

@Override
protected void testRequiredCapabilities(Connection connection) throws SQLException {
    // If collection structure matching will ever be needed, using ltree as path to each leaf
    // item in
    // collection-generated tree will be very useful
    // ltree module provides specific datatype for such path, which may be indexed in order to
    // greatly improve
    // performance
    Statement stmt = connection.createStatement();
    try {
        DefinitionFactory d = this._vendor.getDefinitionFactory();
        TableReferenceFactory t = this._vendor.getTableReferenceFactory();
        DataTypeFactory dt = this._vendor.getDataTypeFactory();
        stmt.execute(this._vendor.toString(d.createTableDefinitionBuilder().setTableScope(TableScope.LOCAL_TEMPORARY).setTableName(t.tableName("ltree_test")).setCommitAction(PgSQLTableCommitAction.DROP).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition("test_column", dt.userDefined("ltree"))).createExpression()).createExpression()));
    } catch (SQLException sqle) {
        throw new InternalError("It seems that your database doesn't have ltree as type. It is needed to store collections. Please refer to hopefully supplied instructions on how to add ltree type (hint: run <pg_install_dir>/share/contrib/ltree.sql script or command 'CREATE EXTENSION ltree;').");
    } finally {
        SQLUtil.closeQuietly(stmt);
    }
}
Also used : TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DataTypeFactory(org.sql.generation.api.grammar.factories.DataTypeFactory) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory)

Example 4 with DefinitionFactory

use of org.sql.generation.api.grammar.factories.DefinitionFactory 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

DefinitionFactory (org.sql.generation.api.grammar.factories.DefinitionFactory)4 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)4 BigInteger (java.math.BigInteger)3 Statement (java.sql.Statement)3 SQLVendor (org.sql.generation.api.vendor.SQLVendor)3 PreparedStatement (java.sql.PreparedStatement)2 QNameType (org.qi4j.index.sql.support.common.QNameInfo.QNameType)2 ModificationFactory (org.sql.generation.api.grammar.factories.ModificationFactory)2 InsertStatement (org.sql.generation.api.grammar.modification.InsertStatement)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 SQLTypeInfo (org.qi4j.index.sql.support.api.SQLTypeInfo)1 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)1 TableElementListBuilder (org.sql.generation.api.grammar.builders.definition.TableElementListBuilder)1 SQLDataType (org.sql.generation.api.grammar.common.datatypes.SQLDataType)1