Search in sources :

Example 11 with TableReferenceFactory

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

the class PostgreSQLIndexing method createInsertStatementWithAutoGeneratedIDForEntitiesTable.

@Override
protected InsertStatement createInsertStatementWithAutoGeneratedIDForEntitiesTable(String schemaName, String tableName, SQLVendor vendor) {
    ModificationFactory m = vendor.getModificationFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    ColumnSourceByValuesBuilder columnBuilder = m.columnSourceByValues();
    columnBuilder.addValues(ValueSource.Default.INSTANCE);
    for (Integer x = 1; x < AMOUNT_OF_COLUMNS_IN_ENTITY_TABLE; ++x) {
        columnBuilder.addValues(l.param());
    }
    return ((PgSQLInsertStatementBuilder) m.insert()).setReturningClause(vendor.getQueryFactory().columnsBuilder().addUnnamedColumns(c.colName(DBNames.ENTITY_TABLE_PK_COLUMN_NAME)).createExpression()).setTableName(t.tableName(schemaName, tableName)).setColumnSource(columnBuilder.createExpression()).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) ColumnSourceByValuesBuilder(org.sql.generation.api.grammar.builders.modification.ColumnSourceByValuesBuilder) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 12 with TableReferenceFactory

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

the class AbstractSQLStartup method createInsertStatementForQNameInfo.

private InsertStatement createInsertStatementForQNameInfo(Connection connection, String schemaName, SQLVendor vendor) {
    ModificationFactory m = vendor.getModificationFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    return m.insert().setTableName(t.tableName(schemaName, USED_QNAMES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 13 with TableReferenceFactory

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

the class AbstractSQLQuerying method constructQuery.

@Override
public // 
String constructQuery(// 
Class<?> resultType, // 
Specification<Composite> whereClause, // 
OrderBy[] orderBySegments, // 
Integer firstResult, // 
Integer maxResults, // 
Map<String, Object> variables, // 
List<Object> values, // 
List<Integer> valueSQLTypes, // 
Boolean countOnly) 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(vendor, mainQuery, resultType, whereClause, orderBySegments, firstResult, maxResults, variables, values, valueSQLTypes, countOnly);
    String result = vendor.toString(finalMainQuery);
    _log.info("SQL query:\n" + result);
    return result;
}
Also used : TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QuerySpecificationBuilder(org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder) SQLVendor(org.sql.generation.api.vendor.SQLVendor) QueryBuilder(org.sql.generation.api.grammar.builders.query.QueryBuilder) QueryExpression(org.sql.generation.api.grammar.query.QueryExpression) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory) ColumnReference(org.sql.generation.api.grammar.query.ColumnReference)

Example 14 with TableReferenceFactory

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

the class AbstractSQLQuerying method traverseAssociationPath.

protected Integer traverseAssociationPath(TraversedAssoOrManyAssoRef reference, Integer lastTableIndex, Integer nextAvailableIndex, SQLVendor vendor, TableReferenceBuilder builder, JoinType joinStyle, Boolean includeLastTable) {
    Stack<QualifiedName> qNameStack = new Stack<QualifiedName>();
    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))));
            lastTableIndex = nextAvailableIndex;
            ++nextAvailableIndex;
        }
    // @formatter:on
    }
    return lastTableIndex;
}
Also used : TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QualifiedName(org.qi4j.api.common.QualifiedName) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) Stack(java.util.Stack)

Example 15 with TableReferenceFactory

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

the class AbstractSQLQuerying method constructQueryForPredicate.

protected // 
QuerySpecification constructQueryForPredicate(// 
Specification<Composite> predicate, // 
PropertyFunction<?> propRef, // 
TraversedAssoOrManyAssoRef assoRef, // 
Boolean includeLastAssoPathTable, // 
Boolean negationActive, // 
SQLVendor vendor, // 
org.sql.generation.api.grammar.booleans.BooleanExpression entityTypeCondition, // 
WhereClauseProcessor whereClauseGenerator) {
    Integer startingIndex = 0;
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    QuerySpecificationBuilder builder = this.getBuilderForPredicate(vendor, TABLE_NAME_PREFIX + startingIndex);
    TableReferenceBuilder from = t.tableBuilder(t.table(t.tableName(this._state.schemaName().get(), DBNames.ENTITY_TABLE_NAME), t.tableAlias(TABLE_NAME_PREFIX + startingIndex)));
    this.addTypeJoin(vendor, from, startingIndex);
    Integer lastTableIndex = null;
    JoinType joinStyle = this.getTableJoinStyle(predicate, negationActive);
    if (propRef == null && assoRef != null && assoRef._hasRefs) {
        lastTableIndex = this.traverseAssociationPath(assoRef, startingIndex, startingIndex + 1, vendor, from, joinStyle, includeLastAssoPathTable);
    } else if (assoRef == null || !assoRef._hasRefs) {
        lastTableIndex = this.traversePropertyPath(propRef, startingIndex, startingIndex + 1, vendor, from, joinStyle);
    } else {
        throw new InternalError("Can not have both property reference and association reference (non-)nulls [propRef=" + propRef + ", assoRef=" + assoRef + ", predicate=" + predicate + "].");
    }
    builder.getFrom().addTableReferences(from);
    BooleanBuilder afterWhere = vendor.getBooleanFactory().booleanBuilder();
    whereClauseGenerator.processWhereClause(builder, afterWhere, joinStyle, startingIndex, lastTableIndex);
    BooleanBuilder where = builder.getWhere();
    if (negationActive) {
        where.not();
    }
    where.and(afterWhere.createExpression());
    where.and(entityTypeCondition);
    builder.trimGroupBy();
    return builder.createExpression();
}
Also used : TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) BooleanBuilder(org.sql.generation.api.grammar.builders.booleans.BooleanBuilder) QuerySpecificationBuilder(org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder) JoinType(org.sql.generation.api.grammar.query.joins.JoinType) TableReferenceBuilder(org.sql.generation.api.grammar.builders.query.TableReferenceBuilder)

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