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

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


                                        JoinType joinStyle, Integer firstTableIndex, Integer lastTableIndex )
        {
            if( !this.negationActive )
            {
                ColumnsFactory c = this.vendor.getColumnsFactory();
                BooleanFactory b = this.vendor.getBooleanFactory();

                QNameInfo info = this.state
                    .qNameInfos()
                    .get()
                    .get(
                        QualifiedName.fromAccessor( this.propFunction.accessor() ) );
                String colName;
                if( info.getCollectionDepth() > 0 )
                {
                    colName = DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME;
                }
                else
                {
                    colName = DBNames.QNAME_TABLE_VALUE_COLUMN_NAME;
                }
                // Last table column might be null because of left joins
                builder.getWhere().reset(
                    b.isNull( c.colName( TABLE_NAME_PREFIX + lastTableIndex, colName ) ) );
            }
        }
View Full Code Here


                                        JoinType joinStyle, Integer firstTableIndex, Integer lastTableIndex )
        {
            if( !negationActive )
            {
                ColumnsFactory c = vendor.getColumnsFactory();
                BooleanFactory b = vendor.getBooleanFactory();

                // Last table column might be null because of left joins
                builder.getWhere().reset(
                    b.isNull( c.colName( TABLE_NAME_PREFIX + lastTableIndex,
                                         DBNames.QNAME_TABLE_VALUE_COLUMN_NAME ) ) );
            }
        }
View Full Code Here

        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

        protected SQLStatement createUpdateEntityStatement( SQLVendor vendor )
        {
            ModificationFactory m = vendor.getModificationFactory();
            TableReferenceFactory t = vendor.getTableReferenceFactory();
            LiteralFactory l = vendor.getLiteralFactory();
            BooleanFactory b = vendor.getBooleanFactory();
            ColumnsFactory c = vendor.getColumnsFactory();

            // @formatter:off
            UpdateBySearchBuilder builder = m.updateBySearch()
                .setTargetTable( m.createTargetTable( t.tableName( this.schemaName, SQLs.TABLE_NAME ) ) )
                .addSetClauses(
                    m.setClause( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME, m.updateSourceByExp( l.param() ) ),
                    m.setClause( SQLs.ENTITY_STATE_COLUMN_NAME, m.updateSourceByExp( l.param() ) ),
                    m.setClause( SQLs.ENTITY_LAST_MODIFIED_COLUMN_NAME, m.updateSourceByExp( l.param() ) )
                    );
            builder
                .getWhereBuilder()
                    .reset( b.eq( c.colName( SQLs.ENTITY_PK_COLUMN_NAME ), l.param() ) )
                    .and( b.eq( c.colName( SQLs.ENTITY_OPTIMISTIC_LOCK_COLUMN_NAME ), l.param() ) );
            return builder.createExpression();
            // @formatter:on
        }
View Full Code Here

        protected SQLStatement createRemoveEntityStatement( SQLVendor vendor )
        {
            ModificationFactory m = vendor.getModificationFactory();
            TableReferenceFactory t = vendor.getTableReferenceFactory();
            LiteralFactory l = vendor.getLiteralFactory();
            BooleanFactory b = vendor.getBooleanFactory();
            ColumnsFactory c = vendor.getColumnsFactory();

            // @formatter:off
            DeleteBySearchBuilder builder = m.deleteBySearch()
                .setTargetTable( m.createTargetTable( t.tableName( this.schemaName, SQLs.TABLE_NAME ) ) );
            builder.getWhere()
                .reset( b.eq( c.colName( SQLs.ENTITY_PK_COLUMN_NAME ), l.param() ) );
            return builder.createExpression();
            // @formatter:on
        }
View Full Code Here

                                               JoinType joinStyle,
                                               Boolean includeLastTable )
    {
        Stack<QualifiedName> qNameStack = new Stack<>();
        TableReferenceFactory t = vendor.getTableReferenceFactory();
        BooleanFactory b = vendor.getBooleanFactory();
        ColumnsFactory c = vendor.getColumnsFactory();
        String schemaName = this._state.schemaName().get();

        while( reference._hasRefs )
        {
            qNameStack.add( QualifiedName.fromAccessor( reference.getAccessor() ) );
            reference = reference.getTraversedAssociation();
        }
        while( !qNameStack.isEmpty() )
        {
            QualifiedName qName = qNameStack.pop();
            QNameInfo info = this._state.qNameInfos().get().get( qName );
            if( info == null )
            {
                throw new InternalError( "No qName info found for qName [" + qName + "]." );
            }
            // @formatter:off
            builder.addQualifiedJoin(
                joinStyle,
                t.table( t.tableName( schemaName, info.getTableName() ), t.tableAlias( TABLE_NAME_PREFIX
                                                                                       + nextAvailableIndex ) ),
                t.jc(
                b.eq(
                    c.colName( TABLE_NAME_PREFIX + lastTableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME ),
                    c.colName( TABLE_NAME_PREFIX + nextAvailableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME ) )
            ) );
            lastTableIndex = nextAvailableIndex;
            ++nextAvailableIndex;
            if( includeLastTable || !qNameStack.isEmpty() )
            {
                builder.addQualifiedJoin(
                    joinStyle,
                    t.table( t.tableName( schemaName, DBNames.ENTITY_TABLE_NAME ), t.tableAlias( TABLE_NAME_PREFIX + nextAvailableIndex ) ),
                    t.jc(
                    b.eq(
                        c.colName( TABLE_NAME_PREFIX + lastTableIndex, DBNames.QNAME_TABLE_VALUE_COLUMN_NAME ),
                        c.colName( TABLE_NAME_PREFIX + nextAvailableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME )
                    )
                )
                );
View Full Code Here

        }

        final String schemaName = this._state.schemaName().get();
        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,
                                        c.colName( TABLE_NAME_PREFIX + currentTableIndex,
                                                   DBNames.QNAME_TABLE_VALUE_COLUMN_NAME ) ),
                                l.n( totalItemsProcessed ) ) );

                }
            }

            whereClause.and( collectionCondition.createExpression() );

        }
        else if( value instanceof ValueComposite )
        {
            // Visit all properties with recursion and make joins as necessary
            // @formatter:off
            for( Property<?> property
                 : Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map( (ValueComposite) value ).state().properties() )
            {
                Boolean qNameJoinDone = false;
                Integer sourceIndex = maxTableIndex.getInt();
                Integer targetIndex = sourceIndex + 1;
                for( QNameJoin join : qNameJoins )
                {
                    if( join.getSourceQName().equals( qName ) )
                    {
                        sourceIndex = join.getSourceTableIndex();
                        if( join.getTargetQName().equals( spi.propertyDescriptorFor( property ).qualifiedName() ) )
                        {
                            // This join has already been done once
                            qNameJoinDone = true;
                            targetIndex = join.getTargetTableIndex();
                            break;
                        }
                    }
                }

                if( !qNameJoinDone )
                {
                    // @formatter:off
                    QNameInfo info = _state.qNameInfos().get().get( spi.propertyDescriptorFor( property ).qualifiedName() );
                    String prevTableName = TABLE_NAME_PREFIX + sourceIndex;
                    String nextTableName = TABLE_NAME_PREFIX + targetIndex;
                    fromClause.addQualifiedJoin(
                        JoinType.LEFT_OUTER,
                        t.table( t.tableName( schemaName, info.getTableName() ), t.tableAlias( TABLE_NAME_PREFIX + targetIndex ) ),
                        t.jc(
                        b.booleanBuilder( b.eq(
                                c.colName( prevTableName, DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME ),
                                c.colName( nextTableName, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME )
                            ) )
                        .and( b.eq(
                                c.colName( prevTableName, DBNames.ENTITY_TABLE_PK_COLUMN_NAME ),
                                c.colName( nextTableName, DBNames.ENTITY_TABLE_PK_COLUMN_NAME )
                            ) ).createExpression()
                    )
                    );
                    // @formatter:on

                    qNameJoins.add( new QNameJoin( qName, spi.propertyDescriptorFor( property )
                        .qualifiedName(), sourceIndex, targetIndex ) );
                    maxTableIndex.setInt( maxTableIndex.getInt() + 1 );
                }
                modifyFromClauseAndWhereClauseToGetValue( spi.propertyDescriptorFor( property )
                    .qualifiedName(), property.get(), predicate, negationActive,
                                                          targetIndex, maxTableIndex, columnName, collectionPath, vendor, whereClause,
                                                          afterWhere,
                                                          fromClause, groupBy, having, qNameJoins, variables, values, valueSQLTypes );
            }

            // @formatter:on
        }
        else
        {
            // Primitive
            ColumnReferenceByName valueCol = c.colName( TABLE_NAME_PREFIX + currentTableIndex, columnName );
            if( value == null )
            {
                whereClause.and( b.isNull( valueCol ) );
            }
            else
            {
                Object dbValue = value;
                if( Enum.class.isAssignableFrom( value.getClass() ) )
                {
                    dbValue = this._state.enumPKs().get().get( value.getClass().getName() );
                }
                whereClause.and( b.and( b.isNotNull( valueCol ),
                                        this.getOperator( predicate ).getExpression( b, valueCol, l.param() ) ) );
                values.add( dbValue );
                valueSQLTypes.add( _typeHelper.getSQLType( value ) );
                LOGGER.info( TABLE_NAME_PREFIX + currentTableIndex + "." + columnName + " is " + dbValue );
            }
View Full Code Here

    protected org.sql.generation.api.grammar.booleans.BooleanExpression createTypeCondition(
        Class<?> resultType,
        SQLVendor vendor )
    {
        BooleanFactory b = vendor.getBooleanFactory();
        LiteralFactory l = vendor.getLiteralFactory();
        ColumnsFactory c = vendor.getColumnsFactory();

        List<Integer> typeIDs = this.getEntityTypeIDs( resultType );
        InBuilder in = b.inBuilder( c.colName( TABLE_NAME_PREFIX + TYPE_TABLE_SUFFIX,
                                               DBNames.ENTITY_TYPES_TABLE_PK_COLUMN_NAME ) );
        for( Integer i : typeIDs )
        {
            in.addValues( l.n( i ) );
        }
View Full Code Here

                                            BooleanBuilder afterWhere,
                                            JoinType joinStyle, Integer firstTableIndex, Integer lastTableIndex )
            {
                LiteralFactory l = vendor.getLiteralFactory();
                ColumnsFactory c = vendor.getColumnsFactory();
                BooleanFactory b = vendor.getBooleanFactory();

                builder.getWhere().reset(
                    getOperator( predicate ).getExpression(
                        b,
                        c.colName( TABLE_NAME_PREFIX + lastTableIndex,
View Full Code Here

            @Override
            public void processWhereClause( QuerySpecificationBuilder builder,
                                            BooleanBuilder afterWhere,
                                            JoinType joinStyle, Integer firstTableIndex, Integer lastTableIndex )
            {
                BooleanFactory b = vendor.getBooleanFactory();
                LiteralFactory l = vendor.getLiteralFactory();
                ColumnsFactory c = vendor.getColumnsFactory();

                builder.getWhere().reset(
                    b.regexp( c.colName( TABLE_NAME_PREFIX + lastTableIndex,
                                         DBNames.QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME ), l
                        .s( DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME + ".*{1,}" ) ) );

                Object value = predicate.value();
                if( value instanceof Collection<?> )
                {
                    throw new IllegalArgumentException(
                        "ContainsPredicate may have only either primitive or value composite as value." );
                }
                BooleanBuilder condition = b.booleanBuilder();
                modifyFromClauseAndWhereClauseToGetValue(
                    QualifiedName.fromAccessor( predicate.collectionProperty().accessor() ), value, predicate,
                    false, lastTableIndex, new ModifiableInt( lastTableIndex ),
                    DBNames.QNAME_TABLE_VALUE_COLUMN_NAME,
                    DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME,
View Full Code Here

TOP

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

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.