use of org.sql.generation.api.grammar.factories.ColumnsFactory in project qi4j-sdk by Qi4j.
the class AbstractSQLQuerying method createTypeCondition.
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));
}
return in.createExpression();
}
use of org.sql.generation.api.grammar.factories.ColumnsFactory 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;
}
use of org.sql.generation.api.grammar.factories.ColumnsFactory 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<>();
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;
}
Aggregations