Browse Source

feat: group by - lookup support - mm/hm lookup - WIP

pull/6987/head
Pranav C 1 year ago
parent
commit
e4e99e01e1
  1. 109
      packages/nocodb/src/db/generateMMLookupSelectQuery.ts

109
packages/nocodb/src/db/generateMMLookupSelectQuery.ts

@ -11,6 +11,7 @@ import type {
import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2'; import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2';
import genRollupSelectv2 from '~/db/genRollupSelectv2'; import genRollupSelectv2 from '~/db/genRollupSelectv2';
import { getAliasGenerator } from '~/utils'; import { getAliasGenerator } from '~/utils';
import { NcError } from '~/helpers/catchError';
export default async function generateMMLookupSelectQuery({ export default async function generateMMLookupSelectQuery({
column, column,
@ -82,7 +83,7 @@ export default async function generateMMLookupSelectQuery({
} }
// if not belongs to then throw error as we don't support // if not belongs to then throw error as we don't support
if (relation.type === RelationTypes.MANY_TO_MANY) { else if (relation.type === RelationTypes.MANY_TO_MANY) {
const childColumn = await relation.getChildColumn(); const childColumn = await relation.getChildColumn();
const parentColumn = await relation.getParentColumn(); const parentColumn = await relation.getParentColumn();
const childModel = await childColumn.getModel(); const childModel = await childColumn.getModel();
@ -92,19 +93,9 @@ export default async function generateMMLookupSelectQuery({
selectQb = knex( selectQb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`, `${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`,
).where(
`${alias}.${parentColumn.column_name}`,
knex.raw(`??`, [
`${rootAlias || baseModelSqlv2.getTnPath(childModel.table_name)}.${
childColumn.column_name
}`,
]),
); );
const mmTableAlias = getAlias();
/*
const mmTableAlias = getAlias();
const mmModel = await relation.getMMModel(); const mmModel = await relation.getMMModel();
const mmChildCol = await relation.getMMChildColumn(); const mmChildCol = await relation.getMMChildColumn();
@ -117,29 +108,20 @@ export default async function generateMMLookupSelectQuery({
// ) // )
selectQb selectQb
.innerJoin( .innerJoin(
baseModelSqlv2.getTnPath(mmModel.table_name), baseModelSqlv2.getTnPath(mmModel.table_name, mmTableAlias),
knex.ref( knex.ref(`${mmTableAlias}.${mmParentCol.column_name}`),
`${baseModelSqlv2.getTnPath(mmModel.table_name)}.${
mmParentCol.column_name
}`,
),
'=', '=',
knex.ref(`${nestedAlias}.${parentColumn.column_name}`), knex.ref(`${alias}.${parentColumn.column_name}`),
) )
.where( .where(
knex.ref( knex.ref(`${mmTableAlias}.${mmChildCol.column_name}`),
`${baseModelSqlv2.getTnPath(mmModel.table_name)}.${
mmChildCol.column_name
}`,
),
'=', '=',
knex.ref( knex.ref(
`${alias || baseModelSqlv2.getTnPath(childModel.table_name)}.${ `${
childColumn.column_name rootAlias || baseModelSqlv2.getTnPath(childModel.table_name)
}`, }.${childColumn.column_name}`,
), ),
); );
* */
} }
} }
let lookupColumn = await lookup.getLookupColumn(); let lookupColumn = await lookup.getLookupColumn();
@ -204,21 +186,13 @@ export default async function generateMMLookupSelectQuery({
// ) // )
selectQb selectQb
.innerJoin( .innerJoin(
baseModelSqlv2.getTnPath(mmModel.table_name), baseModelSqlv2.getTnPath(mmModel.table_name, mmTableAlias),
knex.ref( knex.ref(`${mmTableAlias}.${mmParentCol.column_name}`),
`${baseModelSqlv2.getTnPath(mmModel.table_name)}.${
mmParentCol.column_name
}`,
),
'=', '=',
knex.ref(`${nestedAlias}.${parentColumn.column_name}`), knex.ref(`${nestedAlias}.${parentColumn.column_name}`),
) )
.where( .where(
knex.ref( knex.ref(`${mmTableAlias}.${mmChildCol.column_name}`),
`${baseModelSqlv2.getTnPath(mmModel.table_name)}.${
mmChildCol.column_name
}`,
),
'=', '=',
knex.ref( knex.ref(
`${alias || baseModelSqlv2.getTnPath(childModel.table_name)}.${ `${alias || baseModelSqlv2.getTnPath(childModel.table_name)}.${
@ -294,17 +268,62 @@ export default async function generateMMLookupSelectQuery({
break; break;
default: default:
{ {
selectQb.select(`${prevAlias}.${lookupColumn.column_name} as ${lookupColumn.title}`); selectQb.select(
`${prevAlias}.${lookupColumn.column_name} as ${lookupColumn.title}`,
);
} }
break; break;
} }
selectQb.orderBy(`${lookupColumn.title}`, 'asc'); const subQueryAlias = getAlias();
if (baseModelSqlv2.isPg) {
selectQb.orderBy(`${lookupColumn.title}`, 'asc');
// // alternate approach with array_agg
// return {
// builder: knex
// .select(knex.raw('array_agg(??)', [lookupColumn.title]))
// .from(selectQb),
// };
return {
builder: knex
.select(
knex.raw("STRING_AGG(DISTINCT ??::text, '___')", [
lookupColumn.title,
]),
)
.from(selectQb.as(subQueryAlias)),
};
} else if (baseModelSqlv2.isMySQL) {
// // alternate approach with JSON_ARRAYAGG
// return {
// builder: knex
// .select(
// knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.title]),
// )
// .from(selectQb.as(subQueryAlias)),
// };
return {
builder: knex
.select(
knex.raw(
"GROUP_CONCAT(DISTINCT ?? ORDER BY ?? ASC SEPARATOR '___')",
[lookupColumn.title, lookupColumn.title],
),
)
.from(selectQb.as(subQueryAlias)),
};
} else if (baseModelSqlv2.isSqlite) {
selectQb.orderBy(`${lookupColumn.title}`, 'asc');
return {
builder: knex
.select(knex.raw(`group_concat(??)`, [lookupColumn.title]))
.from(selectQb.as(subQueryAlias)),
};
}
// mysql : https://www.db-fiddle.com/f/qC4hrbSrSp7v3exb4moVCE/1 NcError.notImplemented('Database not supported Group by on Lookup');
const subQueryAlias = getAlias();
// return { builder: knex.select(knex.raw('array_agg(??)', [lookupColumn.title])).from(selectQb) };
return { builder: knex.select(knex.raw("cast(JSON_ARRAYAGG(??) as NCHAR)", [lookupColumn.title])).from(selectQb.as(subQueryAlias)) };
} }
} }

Loading…
Cancel
Save