|
|
@ -43,6 +43,26 @@ export default async function ({ |
|
|
|
dbDriver: knex, |
|
|
|
dbDriver: knex, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const applyFunction = (qb: any) => { |
|
|
|
|
|
|
|
// if postgres and rollup function is sum/sumDistinct/avgDistinct/avg, then cast the column to integer when type is boolean
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
baseModelSqlv2.isPg && |
|
|
|
|
|
|
|
['sum', 'sumDistinct', 'avgDistinct', 'avg'].includes( |
|
|
|
|
|
|
|
columnOptions.rollup_function, |
|
|
|
|
|
|
|
) && |
|
|
|
|
|
|
|
['bool', 'boolean'].includes(rollupColumn.dt) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
qb[columnOptions.rollup_function as string]?.( |
|
|
|
|
|
|
|
knex.raw('??.??::integer', [refTableAlias, rollupColumn.column_name]), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qb[columnOptions.rollup_function as string]?.( |
|
|
|
|
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
switch (relationColumnOption.type) { |
|
|
|
switch (relationColumnOption.type) { |
|
|
|
case RelationTypes.HAS_MANY: { |
|
|
|
case RelationTypes.HAS_MANY: { |
|
|
|
const queryBuilder: any = knex( |
|
|
|
const queryBuilder: any = knex( |
|
|
@ -50,19 +70,16 @@ export default async function ({ |
|
|
|
childBaseModel.getTnPath(childModel), |
|
|
|
childBaseModel.getTnPath(childModel), |
|
|
|
refTableAlias, |
|
|
|
refTableAlias, |
|
|
|
]), |
|
|
|
]), |
|
|
|
) |
|
|
|
).where( |
|
|
|
[columnOptions.rollup_function as string]?.( |
|
|
|
knex.ref( |
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), |
|
|
|
`${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${ |
|
|
|
) |
|
|
|
parentCol.column_name |
|
|
|
.where( |
|
|
|
}`,
|
|
|
|
knex.ref( |
|
|
|
), |
|
|
|
`${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${ |
|
|
|
'=', |
|
|
|
parentCol.column_name |
|
|
|
knex.ref(`${refTableAlias}.${childCol.column_name}`), |
|
|
|
}`,
|
|
|
|
); |
|
|
|
), |
|
|
|
applyFunction(queryBuilder); |
|
|
|
'=', |
|
|
|
|
|
|
|
knex.ref(`${refTableAlias}.${childCol.column_name}`), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
builder: queryBuilder, |
|
|
|
builder: queryBuilder, |
|
|
@ -75,20 +92,17 @@ export default async function ({ |
|
|
|
childBaseModel.getTnPath(childModel?.table_name), |
|
|
|
childBaseModel.getTnPath(childModel?.table_name), |
|
|
|
refTableAlias, |
|
|
|
refTableAlias, |
|
|
|
]), |
|
|
|
]), |
|
|
|
) |
|
|
|
).where( |
|
|
|
[columnOptions.rollup_function as string]?.( |
|
|
|
knex.ref( |
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), |
|
|
|
`${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${ |
|
|
|
) |
|
|
|
parentCol.column_name |
|
|
|
.where( |
|
|
|
}`,
|
|
|
|
knex.ref( |
|
|
|
), |
|
|
|
`${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${ |
|
|
|
'=', |
|
|
|
parentCol.column_name |
|
|
|
knex.ref(`${refTableAlias}.${childCol.column_name}`), |
|
|
|
}`,
|
|
|
|
); |
|
|
|
), |
|
|
|
|
|
|
|
'=', |
|
|
|
|
|
|
|
knex.ref(`${refTableAlias}.${childCol.column_name}`), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
applyFunction(qb); |
|
|
|
return { |
|
|
|
return { |
|
|
|
builder: qb, |
|
|
|
builder: qb, |
|
|
|
}; |
|
|
|
}; |
|
|
@ -114,18 +128,15 @@ export default async function ({ |
|
|
|
refTableAlias, |
|
|
|
refTableAlias, |
|
|
|
]), |
|
|
|
]), |
|
|
|
) |
|
|
|
) |
|
|
|
[columnOptions.rollup_function as string]?.( |
|
|
|
|
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.innerJoin( |
|
|
|
.innerJoin( |
|
|
|
assocBaseModel.getTnPath(mmModel.table_name), |
|
|
|
assocBaseModel.getTnPath(mmModel.table_name) as any, |
|
|
|
knex.ref( |
|
|
|
knex.ref( |
|
|
|
`${assocBaseModel.getTnPath(mmModel.table_name)}.${ |
|
|
|
`${assocBaseModel.getTnPath(mmModel.table_name)}.${ |
|
|
|
mmParentCol.column_name |
|
|
|
mmParentCol.column_name |
|
|
|
}`,
|
|
|
|
}`,
|
|
|
|
), |
|
|
|
) as any, |
|
|
|
'=', |
|
|
|
'=', |
|
|
|
knex.ref(`${refTableAlias}.${parentCol.column_name}`), |
|
|
|
knex.ref(`${refTableAlias}.${parentCol.column_name}`) as any, |
|
|
|
) |
|
|
|
) |
|
|
|
.where( |
|
|
|
.where( |
|
|
|
knex.ref( |
|
|
|
knex.ref( |
|
|
@ -141,6 +152,8 @@ export default async function ({ |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
applyFunction(qb); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
builder: qb, |
|
|
|
builder: qb, |
|
|
|
}; |
|
|
|
}; |
|
|
|