1
0
Fork 0
mirror of synced 2024-09-24 21:31:17 +12:00

Updates to limit the response of JSON_ARRAYAGG in mysql/mariaDB - rather than using a limited sub-query which is dis-allowed in MySQL/MariaDB due to the nature of the correlated sub-query.

This commit is contained in:
mike12345567 2024-09-23 17:16:34 +01:00
parent b0252469ed
commit 676058bbbd

View file

@ -958,22 +958,9 @@ class InternalBuilder {
const primaryKey = `${toAlias}.${toPrimary || toKey}` const primaryKey = `${toAlias}.${toPrimary || toKey}`
let subQuery: Knex.QueryBuilder = knex let subQuery: Knex.QueryBuilder = knex
.from(toTableWithSchema) .from(toTableWithSchema)
.limit(getRelationshipLimit())
// add sorting to get consistent order // add sorting to get consistent order
.orderBy(primaryKey) .orderBy(primaryKey)
const addCorrelatedWhere = (
query: Knex.QueryBuilder,
column1: string,
column2: string
) => {
return query.where(
column1,
"=",
knex.raw(this.quotedIdentifier(column2))
)
}
const isManyToMany = throughTable && toPrimary && fromPrimary const isManyToMany = throughTable && toPrimary && fromPrimary
let correlatedTo = isManyToMany let correlatedTo = isManyToMany
? `${throughAlias}.${fromKey}` ? `${throughAlias}.${fromKey}`
@ -992,10 +979,15 @@ class InternalBuilder {
}) })
} }
subQuery = addCorrelatedWhere(subQuery, correlatedTo, correlatedFrom) // add the correlation to the overall query
subQuery = subQuery.where(
correlatedTo,
"=",
knex.raw(this.quotedIdentifier(correlatedFrom))
)
const standardWrap = (select: string): Knex.QueryBuilder => { const standardWrap = (select: string): Knex.QueryBuilder => {
subQuery = subQuery.select(`${toAlias}.*`) subQuery = subQuery.select(`${toAlias}.*`).limit(getRelationshipLimit())
// @ts-ignore - the from alias syntax isn't in Knex typing // @ts-ignore - the from alias syntax isn't in Knex typing
return knex.select(knex.raw(select)).from({ return knex.select(knex.raw(select)).from({
[toAlias]: subQuery, [toAlias]: subQuery,
@ -1016,7 +1008,12 @@ class InternalBuilder {
) )
break break
case SqlClient.MY_SQL: case SqlClient.MY_SQL:
wrapperQuery = knex.raw(`json_arrayagg(json_object(${fieldList}))`) // can't use the standard wrap due to correlated sub-query limitations in MariaDB
wrapperQuery = subQuery.select(
knex.raw(
`json_arrayagg(json_object(${fieldList}) LIMIT ${getRelationshipLimit()})`
)
)
break break
case SqlClient.ORACLE: case SqlClient.ORACLE:
wrapperQuery = standardWrap( wrapperQuery = standardWrap(