updating how counting disables sorting.

This commit is contained in:
mike12345567 2024-06-19 15:08:12 +01:00
parent 1056efdbf6
commit 2d74927177
1 changed files with 14 additions and 7 deletions

View File

@ -598,13 +598,16 @@ class InternalBuilder {
read( read(
knex: Knex, knex: Knex,
json: QueryJson, json: QueryJson,
limits?: { base?: number; query?: number } opts: {
limits?: { base: number; query: number }
disableSorting?: boolean
} = {}
): Knex.QueryBuilder { ): Knex.QueryBuilder {
let { endpoint, resource, filters, paginate, relationships, tableAliases } = let { endpoint, resource, filters, paginate, relationships, tableAliases } =
json json
const { limits, disableSorting } = opts
const tableName = endpoint.entityId const tableName = endpoint.entityId
const counting = endpoint.operation === Operation.COUNT
// select all if not specified // select all if not specified
if (!resource) { if (!resource) {
resource = { fields: [] } resource = { fields: [] }
@ -647,7 +650,7 @@ class InternalBuilder {
}) })
// add sorting to pre-query // add sorting to pre-query
// no point in sorting when counting // no point in sorting when counting
if (!counting) { if (!disableSorting) {
query = this.addSorting(query, json) query = this.addSorting(query, json)
} }
@ -661,7 +664,7 @@ class InternalBuilder {
}) })
preQuery = preQuery.select(selectStatement) preQuery = preQuery.select(selectStatement)
// have to add after as well (this breaks MS-SQL) // have to add after as well (this breaks MS-SQL)
if (this.client !== SqlClient.MS_SQL && !counting) { if (this.client !== SqlClient.MS_SQL && !disableSorting) {
preQuery = this.addSorting(preQuery, json) preQuery = this.addSorting(preQuery, json)
} }
// handle joins // handle joins
@ -686,7 +689,9 @@ class InternalBuilder {
} }
count(knex: Knex, json: QueryJson) { count(knex: Knex, json: QueryJson) {
const readQuery = this.read(knex, json) const readQuery = this.read(knex, json, {
disableSorting: true,
})
// have to alias the sub-query, this is a requirement for my-sql and ms-sql // have to alias the sub-query, this is a requirement for my-sql and ms-sql
// without this we get an error "Every derived table must have its own alias" // without this we get an error "Every derived table must have its own alias"
return knex({ return knex({
@ -769,8 +774,10 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
break break
case Operation.READ: case Operation.READ:
query = builder.read(client, json, { query = builder.read(client, json, {
query: this.limit, limits: {
base: BASE_LIMIT, query: this.limit,
base: BASE_LIMIT,
},
}) })
break break
case Operation.COUNT: case Operation.COUNT: