Merge pull request #14515 from Budibase/sql-server-aggregation-backwards-compat

Test against SQL Server 2017, get JSON aggregation of relationships working under 2017.
This commit is contained in:
Michael Drury 2024-09-04 17:01:13 +01:00 committed by GitHub
commit fa2611b7a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 19 deletions

View File

@ -913,26 +913,21 @@ class InternalBuilder {
const fieldList: string = relationshipFields
.map(field => jsonField(field))
.join(",")
let rawJsonArray: Knex.Raw
let select: Knex.Raw
switch (sqlClient) {
case SqlClient.SQL_LITE:
rawJsonArray = this.knex.raw(
`json_group_array(json_object(${fieldList}))`
)
select = this.knex.raw(`json_group_array(json_object(${fieldList}))`)
break
case SqlClient.POSTGRES:
rawJsonArray = this.knex.raw(
`json_agg(json_build_object(${fieldList}))`
)
select = this.knex.raw(`json_agg(json_build_object(${fieldList}))`)
break
case SqlClient.MY_SQL:
case SqlClient.ORACLE:
rawJsonArray = this.knex.raw(
`json_arrayagg(json_object(${fieldList}))`
)
select = this.knex.raw(`json_arrayagg(json_object(${fieldList}))`)
break
case SqlClient.MS_SQL:
rawJsonArray = this.knex.raw(`json_array(json_object(${fieldList}))`)
// Cursed, needs some code later instead
select = this.knex.raw(`*`)
break
default:
throw new Error(`JSON relationships not implement for ${this.client}`)
@ -940,7 +935,7 @@ class InternalBuilder {
// SQL Server uses TOP - which performs a little differently to the normal LIMIT syntax
// it reduces the result set rather than limiting how much data it filters over
const primaryKey = `${toAlias}.${toPrimary || toKey}`
let subQuery = this.knex
let subQuery: Knex.QueryBuilder | Knex.Raw = this.knex
.select(`${toAlias}.*`)
.from(toTableWithSchema)
.limit(getBaseLimit())
@ -977,10 +972,19 @@ class InternalBuilder {
if (this.client === SqlClient.SQL_LITE) {
subQuery = this.addJoinFieldCheck(subQuery, relationship)
}
// @ts-ignore - the from alias syntax isn't in Knex typing
const wrapperQuery = this.knex.select(rawJsonArray).from({
[toAlias]: subQuery,
})
let wrapperQuery: Knex.QueryBuilder | Knex.Raw
if (this.client === SqlClient.MS_SQL) {
wrapperQuery = this.knex.raw(
`(SELECT [${toAlias}] = (SELECT a.* FROM (${subQuery}) AS a FOR JSON PATH))`
)
} else {
// @ts-ignore - the from alias syntax isn't in Knex typing
wrapperQuery = this.knex.select(select).from({
[toAlias]: subQuery,
})
}
query = query.select({ [relationship.column]: wrapperQuery })
}
return query

View File

@ -1,4 +1,4 @@
MSSQL_SHA=sha256:c4369c38385eba011c10906dc8892425831275bb035d5ce69656da8e29de50d8
MSSQL_SHA=sha256:3b913841850a4d57fcfcb798be06acc88ea0f2acc5418bc0c140a43e91c4a545
MYSQL_SHA=sha256:9de9d54fecee6253130e65154b930978b1fcc336bcc86dfd06e89b72a2588ebe
POSTGRES_SHA=sha256:bd0d8e485d1aca439d39e5ea99b931160bd28d862e74c786f7508e9d0053090e
MONGODB_SHA=sha256:afa36bca12295b5f9dae68a493c706113922bdab520e901bd5d6c9d7247a1d8d

View File

@ -343,9 +343,9 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
err.number
)
if (readableMessage) {
throw new Error(readableMessage)
throw new Error(readableMessage, { cause: err })
} else {
throw new Error(err.message as string)
throw new Error(err.message as string, { cause: err })
}
}
}