Search in sources :

Example 6 with QuerySpecificationBuilder

use of org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method getBuilderForPredicate.

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));
    return result;
}
Also used : 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)

Example 7 with QuerySpecificationBuilder

use of org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder 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);
    LOGGER.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)

Aggregations

QuerySpecificationBuilder (org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder)7 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)6 QueryFactory (org.sql.generation.api.grammar.factories.QueryFactory)5 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)4 LiteralFactory (org.sql.generation.api.grammar.factories.LiteralFactory)3 TableReferenceBuilder (org.sql.generation.api.grammar.builders.query.TableReferenceBuilder)2 BooleanFactory (org.sql.generation.api.grammar.factories.BooleanFactory)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BooleanBuilder (org.sql.generation.api.grammar.builders.booleans.BooleanBuilder)1 QueryBuilder (org.sql.generation.api.grammar.builders.query.QueryBuilder)1 ModificationFactory (org.sql.generation.api.grammar.factories.ModificationFactory)1 ColumnReference (org.sql.generation.api.grammar.query.ColumnReference)1 QueryExpression (org.sql.generation.api.grammar.query.QueryExpression)1 JoinType (org.sql.generation.api.grammar.query.joins.JoinType)1 SQLVendor (org.sql.generation.api.vendor.SQLVendor)1