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

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


    {
        if( orderBy != null )
        {
            QNameInfo[] qNames = new QNameInfo[ orderBy.length ];

            QueryFactory q = vendor.getQueryFactory();
            ColumnsFactory c = vendor.getColumnsFactory();

            Integer tableIndex = 0;
            for( Integer idx = 0; idx < orderBy.length; ++idx )
            {
                if( orderBy[idx] != null )
                {
                    PropertyFunction<?> ref = orderBy[idx].property();
                    QualifiedName qName = QualifiedName.fromAccessor( ref.accessor() );
                    QNameInfo info = this._state.qNameInfos().get().get( qName );
                    qNames[idx] = info;
                    if( info == null )
                    {
                        throw new InternalError( "No qName info found for qName [" + qName + "]." );
                    }
                    tableIndex
                    = this.traversePropertyPath( ref, 0, tableIndex + 1, vendor, builder
                        .getFrom()
                        .getTableReferences().iterator().next(), JoinType.LEFT_OUTER );
                    Class<?> declaringType = ( (Member) ref.accessor() ).getDeclaringClass();
                    String colName;
                    Integer tableIdx;
                    if( Identity.class.equals( declaringType ) )
                    {
                        colName = DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME;
                        tableIdx = tableIndex - 1;
                    }
                    else
                    {
                        colName = DBNames.QNAME_TABLE_VALUE_COLUMN_NAME;
                        tableIdx = tableIndex;
                    }
                    Ordering ordering = Ordering.ASCENDING;
                    if( orderBy[idx].order() == Order.DESCENDING )
                    {
                        ordering = Ordering.DESCENDING;
                    }
                    builder.getOrderBy().addSortSpecs(
                        q.sortSpec( c.colName( TABLE_NAME_PREFIX + tableIdx, colName ), ordering ) );
                }
            }
        }

    }
View Full Code Here


        Integer result = 1;

        final BooleanFactory b = vendor.getBooleanFactory();
        final LiteralFactory l = vendor.getLiteralFactory();
        final ColumnsFactory c = vendor.getColumnsFactory();
        final QueryFactory q = vendor.getQueryFactory();
        final TableReferenceFactory t = vendor.getTableReferenceFactory();

        if( value instanceof Collection<?> )
        {
            // Collection
            Integer collectionIndex = 0;
            Boolean collectionIsSet = value instanceof Set<?>;
            Boolean topLevel = collectionPath.equals( DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME );
            String collTable = TABLE_NAME_PREFIX + currentTableIndex;
            String collCol = DBNames.QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME;
            ColumnReferenceByName collColExp = c.colName( collTable, collCol );

            BooleanBuilder collectionCondition = b.booleanBuilder();

            if( topLevel && negationActive )
            {
                afterWhere
                    .and( b
                        .booleanBuilder(
                            b.neq( collColExp,
                                   l.s( DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME ) ) )
                        .or( b.isNull( collColExp ) ).createExpression() );
            }

            Integer totalItemsProcessed = 0;
            for( Object item : (Collection<?>) value )
            {
                String path = collectionPath + DBNames.QNAME_TABLE_COLLECTION_PATH_SEPARATOR
                              + ( collectionIsSet ? "*{1,}" : collectionIndex );
                Boolean isCollection = ( item instanceof Collection<?> );
                BooleanBuilder newWhere = b.booleanBuilder();
                if( !isCollection )
                {
                    newWhere.reset( b.regexp( collColExp, l.s( path ) ) );
                }
                totalItemsProcessed
                = totalItemsProcessed
                  + modifyFromClauseAndWhereClauseToGetValue( qName, item, predicate,
                                                              negationActive,
                                                              currentTableIndex, maxTableIndex, columnName, path, vendor,
                                                              newWhere, afterWhere, fromClause,
                                                              groupBy, having, qNameJoins, variables, values, valueSQLTypes );

                ++collectionIndex;
                collectionCondition.or( newWhere.createExpression() );
            }
            result = totalItemsProcessed;

            if( topLevel )
            {
                if( totalItemsProcessed == 0 )
                {
                    collectionCondition.and( b.isNotNull( collColExp ) )
                        .and(
                            b.eq( collColExp,
                                  l.l( DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME ) ) );
                }
                else if( !negationActive )
                {
                    groupBy.addGroupingElements( q.groupingElement( c.colName( TABLE_NAME_PREFIX
                                                                               + currentTableIndex,
                                                                               DBNames.ENTITY_TABLE_PK_COLUMN_NAME ) ) );
                    having
                        .and( b.eq(
                                l.func( SQLFunctions.COUNT,
View Full Code Here

            // @formatter:on
        }

        protected SQLStatement createSelectAllEntitiesStatement( SQLVendor vendor )
        {
            QueryFactory q = vendor.getQueryFactory();
            TableReferenceFactory t = vendor.getTableReferenceFactory();

            // @formatter:off
            return q.simpleQueryBuilder()
                .select( SQLs.ENTITY_PK_COLUMN_NAME, SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, SQLs.ENTITY_STATE_COLUMN_NAME )
                .from( t.tableName( this.schemaName, SQLs.TABLE_NAME ) )
                .createExpression();
            // @formatter:on
        }
View Full Code Here

            // @formatter:on
        }

        protected SQLStatement createSelectEntityStatement( SQLVendor vendor )
        {
            QueryFactory q = vendor.getQueryFactory();
            TableReferenceFactory t = vendor.getTableReferenceFactory();
            BooleanFactory b = vendor.getBooleanFactory();
            ColumnsFactory c = vendor.getColumnsFactory();
            LiteralFactory l = vendor.getLiteralFactory();

            // @formatter:off
            return q.simpleQueryBuilder()
                .select( SQLs.ENTITY_PK_COLUMN_NAME, SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, SQLs.ENTITY_STATE_COLUMN_NAME )
                .from( t.tableName( this.schemaName, SQLs.TABLE_NAME ) )
                .where( b.eq( c.colName( SQLs.ENTITY_IDENTITY_COLUMN_NAME ), l.param() ) )
                .createExpression();
            // @formatter:on
View Full Code Here

    )
        throws EntityFinderException
    {
        SQLVendor vendor = this.descriptor.metaInfo( SQLVendor.class );

        QueryFactory q = vendor.getQueryFactory();
        TableReferenceFactory t = vendor.getTableReferenceFactory();
        LiteralFactory l = vendor.getLiteralFactory();
        ColumnsFactory c = vendor.getColumnsFactory();

        ColumnReference mainColumn = c.colName( TABLE_NAME_PREFIX + "0", DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME );
        if( countOnly )
        {
            mainColumn = c.colExp( l.func( SQLFunctions.COUNT, mainColumn ) );
        }

        QueryBuilder innerBuilder = this.processBooleanExpression(
            whereClause, false, vendor,
            this.createTypeCondition( resultType, vendor ), variables, values, valueSQLTypes );

        QuerySpecificationBuilder mainQuery = q.querySpecificationBuilder();
        mainQuery.getSelect().addUnnamedColumns( mainColumn );
        mainQuery.getFrom().addTableReferences(
            t.tableBuilder( t.table( q.createQuery( innerBuilder.createExpression() ),
                                     t.tableAlias( TABLE_NAME_PREFIX + "0" ) ) ) );

        this.processOrderBySegments( orderBySegments, vendor, mainQuery );

        QueryExpression finalMainQuery = this.finalizeQuery(
View Full Code Here

    )
    {
        QueryBuilder result = null;
        if( expression == null )
        {
            QueryFactory q = vendor.getQueryFactory();
            result = q.queryBuilder(
                this.selectAllEntitiesOfCorrectType( vendor, entityTypeCondition ).createExpression() );
        }
        else
        {
            if( EXPRESSION_PROCESSORS.containsKey( expression.getClass() ) )
View Full Code Here

    protected QueryBuilder finalizeContainsQuery(
        SQLVendor vendor, QuerySpecification contains,
        org.sql.generation.api.grammar.booleans.BooleanExpression entityTypeCondition,
        Boolean negationActive )
    {
        QueryFactory q = vendor.getQueryFactory();
        QueryBuilder result;

        if( negationActive )
        {
            result = q.queryBuilder(
                this.selectAllEntitiesOfCorrectType( vendor, entityTypeCondition )
                .createExpression() ).except(
                    contains );
        }
        else
        {
            result = q.queryBuilder( contains );
        }

        return result;
    }
View Full Code Here

    }

    protected QuerySpecificationBuilder
        getBuilderForPredicate( SQLVendor vendor, String tableAlias )
    {
        QueryFactory q = vendor.getQueryFactory();
        ColumnsFactory c = vendor.getColumnsFactory();
        QuerySpecificationBuilder result = q.querySpecificationBuilder();
        result
            .getSelect()
            .setSetQuantifier( SetQuantifier.DISTINCT )
            .addUnnamedColumns( c.colName( tableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME ),
                                c.colName( tableAlias, DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME ) );
View Full Code Here

        String schemaName = this._state.schemaName().get();
        Statement stmt = connection.createStatement();

        SQLVendor vendor = this._vendor;
        QueryFactory q = vendor.getQueryFactory();
        TableReferenceFactory t = vendor.getTableReferenceFactory();

        try
        {
            // @formatter:off

            q.simpleQueryBuilder()
                .select( ENTITY_TYPES_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME )
                .from( t.tableName( schemaName, ENTITY_TYPES_TABLE_NAME ) )
                .createExpression();

            ResultSet rs = stmt.executeQuery(
                vendor.toString(
                    q.simpleQueryBuilder()
                    .select( ENTITY_TYPES_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME )
                    .from( t.tableName( schemaName, ENTITY_TYPES_TABLE_NAME ) )
                    .createExpression()
                )
            );

            long pk;
            try
            {
                while( rs.next() )
                {
                    pk = rs.getInt( 1 );
                    String entityTypeName = rs.getString( 2 );
                    this._state.entityTypePKs().get().put( entityTypeName, (int) pk );
//                    this._state.entityTypeInfos().get()
//                        .put( entityTypeName, new EntityTypeInfo( entityDescriptors.get( entityTypeName ), (int) pk ) );
                }
            }
            finally
            {
                SQLUtil.closeQuietly( rs );
            }
            rs = stmt.executeQuery(
                vendor.toString(
                    q.simpleQueryBuilder()
                    .select( USED_CLASSES_TABLE_PK_COLUMN_NAME, USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME )
                    .from( t.tableName( schemaName, USED_CLASSES_TABLE_NAME ) )
                    .createExpression()
                )
            );

            try
            {
                while( rs.next() )
                {
                    pk = rs.getInt( 1 );
                    String descriptorTextualFormat = rs.getString( 2 );
                    this._state.usedClassesPKs().get().put(
                        stringToCompositeDescriptor( ValueDescriptor.class,
                                                     this._app.descriptor(),
                                                     descriptorTextualFormat ),
                        (int) pk );
                }
            }
            finally
            {
                SQLUtil.closeQuietly( rs );
            }

            rs = stmt.executeQuery(
                vendor.toString(
                    q.simpleQueryBuilder()
                    .select( ENUM_LOOKUP_TABLE_PK_COLUMN_NAME, ENUM_LOOKUP_TABLE_ENUM_VALUE_NAME )
                    .from( t.tableName( schemaName, ENUM_LOOKUP_TABLE_NAME ) )
                    .createExpression()
                )
            );

            try
            {
                while( rs.next() )
                {
                    pk = rs.getInt( 1 );
                    String enumName = rs.getString( 2 );
                    this._state.enumPKs().get().put( enumName, (int) pk );
                }
            }
            finally
            {
                SQLUtil.closeQuietly( rs );
            }

            rs = stmt.executeQuery(
                q.simpleQueryBuilder()
                .select( USED_QNAMES_TABLE_QNAME_COLUMN_NAME, USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME )
                .from( t.tableName( schemaName, USED_QNAMES_TABLE_NAME ) )
                .createExpression()
                .toString()
            );
View Full Code Here

        ResultSet rs = null;
        Long result = defaultPK;
        try
        {
            SQLVendor vendor = this._vendor;
            QueryFactory q = vendor.getQueryFactory();
            // Let's cheat a bit on SQL functions, so we won't need to use heavy query builder.
            // Also, currently there are no arithmetic statements
            rs
            = stmt.executeQuery(
                vendor
                .toString(
                    q.simpleQueryBuilder()
                    .select( "COUNT(" + columnName + ")", "MAX(" + columnName + ") + 1" )
                    .from(
                        vendor.getTableReferenceFactory().tableName( schemaName, tableName ) )
                    .createExpression()
                )
View Full Code Here

TOP

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

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.