Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private SqlQuerySet createQuerySet(
boolean skipMetaQuery) {
List<SortKey> sortKeys =
transformSortKeys(typeQuery.getSortKeys(), queryTemplates.getMapping());
boolean useMinMaxKeys = queryTemplates.getMapping().getMainTable().isSortKeyUnique();

BiFunction<Long, Long, Optional<String>> metaQuery =
(maxLimit, skipped) ->
Expand Down Expand Up @@ -212,9 +213,10 @@ private SqlQuerySet createQuerySet(
sortKeys,
typeQuery.getFilter(),
typeQuery.forceSimpleFeatureGeometry(),
((Objects.nonNull(metaResult.getMinKey())
&& Objects.nonNull(metaResult.getMaxKey()))
|| metaResult.getNumberReturned() == 0)
(useMinMaxKeys
&& ((Objects.nonNull(metaResult.getMinKey())
&& Objects.nonNull(metaResult.getMaxKey()))
|| metaResult.getNumberReturned() == 0))
? Optional.of(
Tuple.of(metaResult.getMinKey(), metaResult.getMaxKey()))
: Optional.empty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ private List<SchemaSql> createTableParents(
newParentPath.get(newParentPath.size() - 1)
+ "{sortKey="
+ entry.getValue().get(0).getSortKey().get()
+ "}"
+ "{sortKeyUnique="
+ entry.getValue().get(0).getSortKeyUnique()
+ "}")
: pathParser.parseTablePath(newParentPath.get(newParentPath.size() - 1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ private SqlQuerySchema derive(
.name(sqlPath.getName())
.pathSegment(sqlPath.asPath())
.sortKey(sqlPath.getSortKey())
.sortKeyUnique(sqlPath.getSortKeyUnique())
.primaryKey(sqlPath.getPrimaryKey())
.filter(sqlPath.getFilter().map(expr -> (Operation<?>) expr))
.columns(columns.stream().map(column -> getColumn(schema, column)).toList())
Expand Down Expand Up @@ -426,6 +427,7 @@ private static List<SqlQueryJoin> getJoins(SqlPath path, List<List<String>> prev
.pathSegment(parentTable.asPath())
.sourceField(childTable.getJoin().get().first())
.sortKey(parentTable.getSortKey())
.sortKeyUnique(parentTable.getSortKeyUnique())
.filter(parentTable.getFilter().map(expr -> (Operation<?>) expr))
.target(childTable.getName())
.targetField(childTable.getJoin().get().second())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ MetaQueryTemplate createMetaQueryTemplate(SqlQuerySchema schema, SqlQueryMapping

columns += getSortColumn("A", sortKey, i) + ", ";
}
columns += String.format("A.%s AS " + SKEY, schema.getSortKey());
columns += getSkeyColumn("A", schema, "", false);
String orderBy = getOrderBy(additionalSortKeys);
String minMaxColumns = getMinMaxColumns(additionalSortKeys);

Expand Down Expand Up @@ -180,8 +180,9 @@ ValueQueryTemplate createValueQueryTemplate(SqlQuerySchema schema, SqlQueryMappi
? sqlFilter
: toWhereClause(
aliases.get(0), main.getSortKey(), additionalSortKeys, minMaxKeys, sqlFilter);
boolean useRangePaging = additionalSortKeys.isEmpty() && minMaxKeys.isPresent();
Optional<String> pagingClause =
additionalSortKeys.isEmpty() || (limit == 0 && offset == 0)
useRangePaging || (limit == 0 && offset == 0)
? Optional.empty()
: Optional.of(
String.format(
Expand Down Expand Up @@ -348,11 +349,29 @@ private String formatLiteral(Object literal) {
return String.format("'%s'", sqlDialect.escapeString(literalString));
}

private String getSkeyExpression(
String alias, SqlQueryTable table, boolean useRowNumberForNonUnique) {
if (table.isSortKeyUnique()) {
return String.format("%s.%s", alias, table.getSortKey());
}

if (!useRowNumberForNonUnique) {
return String.format("%s.%s", alias, table.getSortKey());
}

return String.format("ROW_NUMBER() OVER (ORDER BY %s.%s)", alias, table.getSortKey());
}

private String getSkeyColumn(
String alias, SqlQueryTable table, String suffix, boolean useRowNumberForNonUnique) {
return String.format(
"%s AS SKEY%s", getSkeyExpression(alias, table, useRowNumberForNonUnique), suffix);
}

private List<String> getSortFields(
SqlQuerySchema schema, List<String> aliases, List<SortKey> additionalSortKeys) {

final int[] i = {0};
final int[] j = {0};
Stream<String> customSortKeys =
additionalSortKeys.stream().map(sortKey -> getSortColumn(aliases.get(0), sortKey, i[0]++));

Expand All @@ -370,14 +389,7 @@ private List<String> getSortFields(
.collect(Collectors.toList());
} else {
return Stream.concat(
customSortKeys,
Stream.of(
String.format(
schema.isSortKeyUnique()
? "%s.%s AS SKEY"
: "ROW_NUMBER() OVER (ORDER BY %s.%s) AS SKEY",
aliases.get(0),
schema.getSortKey())))
customSortKeys, Stream.of(getSkeyColumn(aliases.get(0), schema, "", true)))
.collect(Collectors.toList());
}
}
Expand Down Expand Up @@ -445,17 +457,10 @@ private List<String> getSortKeys(
ImmutableList.Builder<String> keys = ImmutableList.builder();

int keyIndex = keyIndexStart;
SqlQueryTable previousRelation = null;

if (!onlyRelations) {
// add key for value table
keys.add(
String.format(
tables.get(0).isSortKeyUnique()
? "%s.%s AS SKEY"
: "ROW_NUMBER() OVER (ORDER BY %s.%s) AS SKEY",
aliasesIterator.next(),
tables.get(0).getSortKey()));
keys.add(getSkeyColumn(aliasesIterator.next(), tables.get(0), "", true));
keyIndex++;
}

Expand All @@ -465,11 +470,9 @@ private List<String> getSortKeys(

if (!(relation instanceof SqlQueryJoin) || !((SqlQueryJoin) relation).isJunction()) {
String suffix = keyIndex > 0 ? "_" + keyIndex : "";
keys.add(String.format("%s.%s AS SKEY%s", alias, relation.getSortKey(), suffix));
keys.add(getSkeyColumn(alias, relation, suffix, true));
keyIndex++;
}

previousRelation = relation;
}

return keys.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,12 @@ private SqlRelation toRelation(SqlPath source, SqlPath target) {
.sourceField(sourceField)
.sourcePrimaryKey(source.getPrimaryKey())
.sourceSortKey(source.getSortKey())
.sourceSortKeyUnique(source.getSortKeyUnique())
.sourceFilter(source.getFilterString())
.targetContainer(target.getName())
.targetField(targetField)
.targetSortKey(target.getSortKey())
.targetSortKeyUnique(target.getSortKeyUnique())
.targetFilter(target.getFilterString())
.joinType(target.getJoinType().orElse(JoinType.INNER))
.build();
Expand All @@ -690,13 +693,16 @@ private SqlRelation toRelation(SqlPath source, SqlPath link, SqlPath target) {
.sourceField(sourceField)
.sourcePrimaryKey(source.getPrimaryKey())
.sourceSortKey(source.getSortKey())
.sourceSortKeyUnique(source.getSortKeyUnique())
.sourceFilter(source.getFilterString())
.junctionSource(junctionSourceField)
.junction(link.getName())
.junctionTarget(junctionTargetField)
.junctionFilter(link.getFilterString())
.targetContainer(target.getName())
.targetField(targetField)
.targetSortKey(target.getSortKey())
.targetSortKeyUnique(target.getSortKeyUnique())
.targetFilter(target.getFilterString())
.joinType(target.getJoinType().orElse(JoinType.INNER))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ enum CARDINALITY {

Optional<String> getSourceSortKey();

Optional<Boolean> getSourceSortKeyUnique();

Optional<String> getSourceFilter();

String getTargetContainer();

String getTargetField();

Optional<String> getTargetSortKey();

Optional<Boolean> getTargetSortKeyUnique();

Optional<String> getTargetFilter();

Optional<String> getJunction();
Expand Down Expand Up @@ -96,19 +102,27 @@ default List<String> asPath() {
getJunction().get(),
getJunctionFilter().map(filter -> "{filter=" + filter + "}").orElse("")),
String.format(
"[%s=%s]%s%s",
"[%s=%s]%s%s%s%s",
getJunctionTarget().get(),
getTargetField(),
getTargetContainer(),
getTargetSortKey().map(sortKey -> "{sortKey=" + sortKey + "}").orElse(""),
getTargetSortKeyUnique()
.map(sortKeyUnique -> "{sortKeyUnique=" + sortKeyUnique + "}")
.orElse(""),
getTargetFilter().map(filter -> "{filter=" + filter + "}").orElse("")));
}

return ImmutableList.of(
String.format(
"[%s=%s]%s%s",
"[%s=%s]%s%s%s%s",
getSourceField(),
getTargetField(),
getTargetContainer(),
getTargetSortKey().map(sortKey -> "{sortKey=" + sortKey + "}").orElse(""),
getTargetSortKeyUnique()
.map(sortKeyUnique -> "{sortKeyUnique=" + sortKeyUnique + "}")
.orElse(""),
getTargetFilter().map(filter -> "{filter=" + filter + "}").orElse("")));
}
}
Loading