Changing how counting is processed.

This commit is contained in:
mike12345567 2024-06-19 14:28:22 +01:00
parent 223df424fc
commit aab100b130
4 changed files with 24 additions and 27 deletions

View File

@ -34,7 +34,10 @@ import {
isManyToMany,
sqlOutputProcessing,
} from "./utils"
import { getDatasourceAndQuery } from "../../../sdk/app/rows/utils"
import {
getDatasourceAndQuery,
processRowCountResponse,
} from "../../../sdk/app/rows/utils"
import { processObjectSync } from "@budibase/string-templates"
import { cloneDeep } from "lodash/fp"
import { db as dbCore } from "@budibase/backend-core"
@ -670,18 +673,14 @@ export class ExternalRequest<T extends Operation> {
}
// aliasing can be disabled fully if desired
let response
const aliasing = new sdk.rows.AliasTables(Object.keys(this.tables))
// if it's a counting operation there will be no more processing, just return the number
if (this.operation === Operation.COUNT) {
return (await aliasing.countWithAliasing(
json,
makeExternalQuery
)) as ExternalRequestReturnType<T>
} else {
response = env.SQL_ALIASING_DISABLE
let response = env.SQL_ALIASING_DISABLE
? await getDatasourceAndQuery(json)
: await aliasing.queryWithAliasing(json, makeExternalQuery)
// if it's a counting operation there will be no more processing, just return the number
if (this.operation === Operation.COUNT) {
return processRowCountResponse(response) as ExternalRequestReturnType<T>
}
const responseRows = Array.isArray(response) ? response : []

View File

@ -29,6 +29,7 @@ import { CONSTANT_INTERNAL_ROW_COLS } from "../../../../db/utils"
import AliasTables from "../sqlAlias"
import { outputProcessing } from "../../../../utilities/rowProcessor"
import pick from "lodash/pick"
import { processRowCountResponse } from "../utils"
const builder = new sql.Sql(SqlClient.SQL_LITE)
@ -141,10 +142,11 @@ async function runSqlQuery(
const db = context.getAppDB()
return await db.sql<Row>(sql, bindings)
}
const response = await alias.queryWithAliasing(json, processSQLQuery)
if (opts?.countTotalRows) {
return await alias.countWithAliasing(json, processSQLQuery)
return processRowCountResponse(response)
} else {
return await alias.queryWithAliasing(json, processSQLQuery)
return response
}
}

View File

@ -242,19 +242,4 @@ export default class AliasTables {
return response
}
}
// handles getting the count out of the query
async countWithAliasing(
json: QueryJson,
queryFn: PerformQueryFunction
): Promise<number> {
json.endpoint.operation = Operation.COUNT
let response = await this.queryWithAliasing(json, queryFn)
if (response && response.length === 1 && "total" in response[0]) {
const total = response[0].total
return typeof total === "number" ? total : parseInt(total)
} else {
throw new Error("Unable to count rows in query - no count response")
}
}
}

View File

@ -50,6 +50,17 @@ export function getSQLClient(datasource: Datasource): SqlClient {
throw new Error("Unable to determine client for SQL datasource")
}
export function processRowCountResponse(
response: DatasourcePlusQueryResponse
): number {
if (response && response.length === 1 && "total" in response[0]) {
const total = response[0].total
return typeof total === "number" ? total : parseInt(total)
} else {
throw new Error("Unable to count rows in query - no count response")
}
}
export async function getDatasourceAndQuery(
json: QueryJson
): Promise<DatasourcePlusQueryResponse> {