Package org.sql.generation.api.grammar.factories

Examples of org.sql.generation.api.grammar.factories.DefinitionFactory


            return new SQLStatement[] {};
        }

        protected SQLStatement[] createTableStatements( SQLVendor vendor )
        {
            DefinitionFactory d = vendor.getDefinitionFactory();
            TableReferenceFactory t = vendor.getTableReferenceFactory();


            // @formatter:off
            return new SQLStatement[]
            {
                d.createTableDefinitionBuilder()
                    .setTableName( t.tableName( this.getSchemaName(), SQLs.TABLE_NAME ) )
                    .setTableContentsSource( d.createTableElementListBuilder()
                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_PK_COLUMN_NAME, this.getPKType(), false, AutoGenerationPolicy.BY_DEFAULT ) )
                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, this.getOptimisticLockType(), false ) )
                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_IDENTITY_COLUMN_NAME, this.getIDType(), false ) )
                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_STATE_COLUMN_NAME, this.getStateType(), false ) )
                        .addTableElement( d.createColumnDefinition( SQLs.ENTITY_LAST_MODIFIED_COLUMN_NAME, this.getLastModifiedType(), false ) )
                        .addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
                            .setUniqueness( UniqueSpecification.PRIMARY_KEY )
                            .addColumns( SQLs.ENTITY_PK_COLUMN_NAME )
                            .createExpression() ) )
                        .addTableElement( d.createTableConstraintDefinition( d.createUniqueConstraintBuilder()
                            .setUniqueness( UniqueSpecification.UNIQUE )
                            .addColumns( SQLs.ENTITY_IDENTITY_COLUMN_NAME )
                            .createExpression() ) )
                        .createExpression()
                        )
View Full Code Here


        {
            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()
                    )
                );
                LOGGER.debug( "Database schema created" );
            }

            this.testRequiredCapabilities( connection );
            LOGGER.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()
View Full Code Here

            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()
                ) );
View Full Code Here

            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 ) )
View Full Code Here

        // 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 "
View Full Code Here

TOP

Related Classes of org.sql.generation.api.grammar.factories.DefinitionFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.