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

Fixing an issue that was stopping the limit from being applied to MySQL, it needs to wrap the query the same as all other DBs, however it needs to apply the where statement in a slightly different manner.

This commit is contained in:
mike12345567 2024-09-20 17:58:31 +01:00
parent 67b5d54d37
commit efdfbe7229

View file

@ -930,7 +930,8 @@ class InternalBuilder {
} }
const relatedTable = meta.tables?.[toTable] const relatedTable = meta.tables?.[toTable]
const toAlias = aliases?.[toTable] || toTable, const toAlias = aliases?.[toTable] || toTable,
fromAlias = aliases?.[fromTable] || fromTable fromAlias = aliases?.[fromTable] || fromTable,
throughAlias = (throughTable && aliases?.[throughTable]) || throughTable
let toTableWithSchema = this.tableNameWithSchema(toTable, { let toTableWithSchema = this.tableNameWithSchema(toTable, {
alias: toAlias, alias: toAlias,
schema: endpoint.schema, schema: endpoint.schema,
@ -961,30 +962,38 @@ class InternalBuilder {
// add sorting to get consistent order // add sorting to get consistent order
.orderBy(primaryKey) .orderBy(primaryKey)
// many-to-many relationship with junction table const addCorrelatedWhere = (
if (throughTable && toPrimary && fromPrimary) { query: Knex.QueryBuilder,
const throughAlias = aliases?.[throughTable] || throughTable column1: string,
column2: string
) => {
return query.where(
column1,
"=",
knex.raw(this.quotedIdentifier(column2))
)
}
const isManyToMany = throughTable && toPrimary && fromPrimary
let correlatedTo = isManyToMany
? `${throughAlias}.${fromKey}`
: `${toAlias}.${toKey}`,
correlatedFrom = isManyToMany
? `${fromAlias}.${fromPrimary}`
: `${fromAlias}.${fromKey}`
// many-to-many relationship needs junction table join
if (isManyToMany) {
let throughTableWithSchema = this.tableNameWithSchema(throughTable, { let throughTableWithSchema = this.tableNameWithSchema(throughTable, {
alias: throughAlias, alias: throughAlias,
schema: endpoint.schema, schema: endpoint.schema,
}) })
subQuery = subQuery subQuery = subQuery.join(throughTableWithSchema, function () {
.join(throughTableWithSchema, function () {
this.on(`${toAlias}.${toPrimary}`, "=", `${throughAlias}.${toKey}`) this.on(`${toAlias}.${toPrimary}`, "=", `${throughAlias}.${toKey}`)
}) })
.where(
`${throughAlias}.${fromKey}`,
"=",
knex.raw(this.quotedIdentifier(`${fromAlias}.${fromPrimary}`))
)
} }
// one-to-many relationship with foreign key // my-sql needs the where statement to be part of main query, not sub-query
else { if (sqlClient !== SqlClient.MY_SQL) {
subQuery = subQuery.where( subQuery = addCorrelatedWhere(subQuery, correlatedTo, correlatedFrom)
`${toAlias}.${toKey}`,
"=",
knex.raw(this.quotedIdentifier(`${fromAlias}.${fromKey}`))
)
} }
const standardWrap = (select: string): Knex.QueryBuilder => { const standardWrap = (select: string): Knex.QueryBuilder => {
@ -1009,8 +1018,10 @@ class InternalBuilder {
) )
break break
case SqlClient.MY_SQL: case SqlClient.MY_SQL:
wrapperQuery = subQuery.select( wrapperQuery = addCorrelatedWhere(
knex.raw(`json_arrayagg(json_object(${fieldList}))`) standardWrap(`json_arrayagg(json_object(${fieldList}))`),
isManyToMany ? fromKey! : toKey!,
correlatedFrom
) )
break break
case SqlClient.ORACLE: case SqlClient.ORACLE: