Changing how counting is processed.
This commit is contained in:
parent
223df424fc
commit
aab100b130
|
@ -34,7 +34,10 @@ import {
|
||||||
isManyToMany,
|
isManyToMany,
|
||||||
sqlOutputProcessing,
|
sqlOutputProcessing,
|
||||||
} from "./utils"
|
} from "./utils"
|
||||||
import { getDatasourceAndQuery } from "../../../sdk/app/rows/utils"
|
import {
|
||||||
|
getDatasourceAndQuery,
|
||||||
|
processRowCountResponse,
|
||||||
|
} from "../../../sdk/app/rows/utils"
|
||||||
import { processObjectSync } from "@budibase/string-templates"
|
import { processObjectSync } from "@budibase/string-templates"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { db as dbCore } from "@budibase/backend-core"
|
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
|
// aliasing can be disabled fully if desired
|
||||||
let response
|
|
||||||
const aliasing = new sdk.rows.AliasTables(Object.keys(this.tables))
|
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
|
let response = env.SQL_ALIASING_DISABLE
|
||||||
if (this.operation === Operation.COUNT) {
|
|
||||||
return (await aliasing.countWithAliasing(
|
|
||||||
json,
|
|
||||||
makeExternalQuery
|
|
||||||
)) as ExternalRequestReturnType<T>
|
|
||||||
} else {
|
|
||||||
response = env.SQL_ALIASING_DISABLE
|
|
||||||
? await getDatasourceAndQuery(json)
|
? await getDatasourceAndQuery(json)
|
||||||
: await aliasing.queryWithAliasing(json, makeExternalQuery)
|
: 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 : []
|
const responseRows = Array.isArray(response) ? response : []
|
||||||
|
|
|
@ -29,6 +29,7 @@ import { CONSTANT_INTERNAL_ROW_COLS } from "../../../../db/utils"
|
||||||
import AliasTables from "../sqlAlias"
|
import AliasTables from "../sqlAlias"
|
||||||
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
||||||
import pick from "lodash/pick"
|
import pick from "lodash/pick"
|
||||||
|
import { processRowCountResponse } from "../utils"
|
||||||
|
|
||||||
const builder = new sql.Sql(SqlClient.SQL_LITE)
|
const builder = new sql.Sql(SqlClient.SQL_LITE)
|
||||||
|
|
||||||
|
@ -141,10 +142,11 @@ async function runSqlQuery(
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
return await db.sql<Row>(sql, bindings)
|
return await db.sql<Row>(sql, bindings)
|
||||||
}
|
}
|
||||||
|
const response = await alias.queryWithAliasing(json, processSQLQuery)
|
||||||
if (opts?.countTotalRows) {
|
if (opts?.countTotalRows) {
|
||||||
return await alias.countWithAliasing(json, processSQLQuery)
|
return processRowCountResponse(response)
|
||||||
} else {
|
} else {
|
||||||
return await alias.queryWithAliasing(json, processSQLQuery)
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -242,19 +242,4 @@ export default class AliasTables {
|
||||||
return response
|
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,17 @@ export function getSQLClient(datasource: Datasource): SqlClient {
|
||||||
throw new Error("Unable to determine client for SQL datasource")
|
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(
|
export async function getDatasourceAndQuery(
|
||||||
json: QueryJson
|
json: QueryJson
|
||||||
): Promise<DatasourcePlusQueryResponse> {
|
): Promise<DatasourcePlusQueryResponse> {
|
||||||
|
|
Loading…
Reference in New Issue