|
|
|
@ -11,23 +11,21 @@ import type {
|
|
|
|
|
import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2'; |
|
|
|
|
import genRollupSelectv2 from '~/db/genRollupSelectv2'; |
|
|
|
|
import { NcError } from '~/helpers/catchError'; |
|
|
|
|
import {getAliasGenerator} from "~/utils"; |
|
|
|
|
import { getAliasGenerator } from '~/utils'; |
|
|
|
|
|
|
|
|
|
export default async function generateBTLookupSelectQuery({ |
|
|
|
|
export default async function generateMMLookupSelectQuery({ |
|
|
|
|
column, |
|
|
|
|
baseModelSqlv2, |
|
|
|
|
alias, |
|
|
|
|
model, |
|
|
|
|
getAlias = getAliasGenerator('__lk_slt_') |
|
|
|
|
getAlias = getAliasGenerator('__lk_slt_'), |
|
|
|
|
}: { |
|
|
|
|
column: Column; |
|
|
|
|
baseModelSqlv2: BaseModelSqlv2; |
|
|
|
|
alias: string; |
|
|
|
|
model: Model; |
|
|
|
|
getAlias: ReturnType<typeof getAliasGenerator> |
|
|
|
|
getAlias: ReturnType<typeof getAliasGenerator>; |
|
|
|
|
}): Promise<any> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const knex = baseModelSqlv2.dbDriver; |
|
|
|
|
|
|
|
|
|
const rootAlias = alias; |
|
|
|
@ -53,9 +51,9 @@ export default async function generateBTLookupSelectQuery({
|
|
|
|
|
const parentModel = await parentColumn.getModel(); |
|
|
|
|
await parentModel.getColumns(); |
|
|
|
|
|
|
|
|
|
selectQb = knex( |
|
|
|
|
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`, |
|
|
|
|
).where( |
|
|
|
|
selectQb = knex( |
|
|
|
|
`${alias}.${parentColumn.column_name}`, |
|
|
|
|
knex.raw(`??`, [ |
|
|
|
|
`${rootAlias || baseModelSqlv2.getTnPath(childModel.table_name)}.${ |
|
|
|
@ -76,7 +74,6 @@ export default async function generateBTLookupSelectQuery({
|
|
|
|
|
// if any of the relation in nested lookup is
|
|
|
|
|
// not belongs to then throw error as we don't support
|
|
|
|
|
if (relation.type === RelationTypes.BELONGS_TO) { |
|
|
|
|
|
|
|
|
|
const childColumn = await relation.getChildColumn(); |
|
|
|
|
const parentColumn = await relation.getParentColumn(); |
|
|
|
|
const childModel = await childColumn.getModel(); |
|
|
|
@ -85,15 +82,36 @@ export default async function generateBTLookupSelectQuery({
|
|
|
|
|
await parentModel.getColumns(); |
|
|
|
|
|
|
|
|
|
selectQb.join( |
|
|
|
|
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${nestedAlias}`, |
|
|
|
|
`${baseModelSqlv2.getTnPath( |
|
|
|
|
parentModel.table_name, |
|
|
|
|
)} as ${nestedAlias}`,
|
|
|
|
|
`${nestedAlias}.${parentColumn.column_name}`, |
|
|
|
|
`${prevAlias}.${childColumn.column_name}`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
} else if (relation.type === RelationTypes.HAS_MANY) { |
|
|
|
|
const childColumn = await relation.getChildColumn(); |
|
|
|
|
const parentColumn = await relation.getParentColumn(); |
|
|
|
|
const childModel = await childColumn.getModel(); |
|
|
|
|
await childModel.getColumns(); |
|
|
|
|
const parentModel = await parentColumn.getModel(); |
|
|
|
|
await parentModel.getColumns(); |
|
|
|
|
|
|
|
|
|
selectQb.join( |
|
|
|
|
`${baseModelSqlv2.getTnPath( |
|
|
|
|
childModel.table_name, |
|
|
|
|
)} as ${nestedAlias}`,
|
|
|
|
|
`${nestedAlias}.${parentColumn.column_name}`, |
|
|
|
|
`${prevAlias}.${childColumn.column_name}`, |
|
|
|
|
); |
|
|
|
|
} else if (relation.type === RelationTypes.MANY_TO_MANY) { |
|
|
|
|
const childColumn = await relation.getChildColumn(); |
|
|
|
|
const parentColumn = await relation.getParentColumn(); |
|
|
|
|
const childModel = await childColumn.getModel(); |
|
|
|
|
await childModel.getColumns(); |
|
|
|
|
const parentModel = await parentColumn.getModel(); |
|
|
|
|
await parentModel.getColumns(); |
|
|
|
|
|
|
|
|
|
}else if (relation.type === RelationTypes.MANY_TO_MANY) { |
|
|
|
|
const mmTableAlias = getAlias() |
|
|
|
|
const mmTableAlias = getAlias(); |
|
|
|
|
|
|
|
|
|
const mmModel = await relation.getMMModel(); |
|
|
|
|
const mmChildCol = await relation.getMMChildColumn(); |
|
|
|
@ -102,10 +120,7 @@ export default async function generateBTLookupSelectQuery({
|
|
|
|
|
knex( |
|
|
|
|
`${baseModelSqlv2.getTnPath( |
|
|
|
|
parentModel?.table_name, |
|
|
|
|
)} as ${refTableAlias}`,
|
|
|
|
|
) |
|
|
|
|
[columnOptions.rollup_function as string]?.( |
|
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), |
|
|
|
|
)} as ${nestedAlias}`,
|
|
|
|
|
) |
|
|
|
|
.innerJoin( |
|
|
|
|
baseModelSqlv2.getTnPath(mmModel.table_name), |
|
|
|
@ -115,7 +130,7 @@ export default async function generateBTLookupSelectQuery({
|
|
|
|
|
}`,
|
|
|
|
|
), |
|
|
|
|
'=', |
|
|
|
|
knex.ref(`${refTableAlias}.${parentCol.column_name}`), |
|
|
|
|
knex.ref(`${nestedAlias}.${parentColumn.column_name}`), |
|
|
|
|
) |
|
|
|
|
.where( |
|
|
|
|
knex.ref( |
|
|
|
@ -126,15 +141,12 @@ export default async function generateBTLookupSelectQuery({
|
|
|
|
|
'=', |
|
|
|
|
knex.ref( |
|
|
|
|
`${alias || baseModelSqlv2.getTnPath(childModel.table_name)}.${ |
|
|
|
|
childCol.column_name |
|
|
|
|
childColumn.column_name |
|
|
|
|
}`,
|
|
|
|
|
), |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lookupColumn = await nestedLookup.getLookupColumn(); |
|
|
|
|
prevAlias = nestedAlias; |
|
|
|
|
} |
|
|
|
|