Browse Source

wip: nested filter empty/not empty

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/2535/head
Pranav C 2 years ago
parent
commit
8dec49015e
  1. 96
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts

96
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts

@ -50,11 +50,11 @@ const parseConditionV2 = async (
}
if (Array.isArray(_filter)) {
const qbs = await Promise.all(
_filter.map((child) => parseConditionV2(child, knex, aliasCount))
_filter.map(child => parseConditionV2(child, knex, aliasCount))
);
return (qbP) => {
qbP.where((qb) => {
return qbP => {
qbP.where(qb => {
for (const [i, qb1] of Object.entries(qbs)) {
qb[getLogicalOpMethod(_filter[i])](qb1);
}
@ -64,11 +64,11 @@ const parseConditionV2 = async (
const children = await filter.getChildren();
const qbs = await Promise.all(
(children || []).map((child) => parseConditionV2(child, knex, aliasCount))
(children || []).map(child => parseConditionV2(child, knex, aliasCount))
);
return (qbP) => {
qbP[getLogicalOpMethod(filter)]((qb) => {
return qbP => {
qbP[getLogicalOpMethod(filter)](qb => {
for (const [i, qb1] of Object.entries(qbs)) {
qb[getLogicalOpMethod(children[i])](qb1);
}
@ -78,8 +78,7 @@ const parseConditionV2 = async (
const column = await filter.getColumn();
if (!column) return () => {};
if (column.uidt === UITypes.LinkToAnotherRecord) {
const colOptions =
(await column.getColOptions()) as LinkToAnotherRecordColumn;
const colOptions = (await column.getColOptions()) as LinkToAnotherRecordColumn;
const childColumn = await colOptions.getChildColumn();
const parentColumn = await colOptions.getParentColumn();
const childModel = await childColumn.getModel();
@ -96,21 +95,32 @@ const parseConditionV2 = async (
console.log(selectParentQb.toQuery());
const selectHmCount = knex(childModel.table_name)
.select(childColumn.column_name)
.count(childColumn.column_name)
.whereNotNull(childColumn.column_name)
.groupBy(childColumn.column_name);
console.log('HERE2');
console.log(selectHmCount.toQuery());
const q = selectParentQb.leftJoin(
selectHmCount.as('t2'),
parentModel.table_name + '.' + parentColumn.column_name,
't2.' + childColumn.column_name
);
console.log('HERE3');
console.log(q.toQuery());
return q;
.where(
childColumn.column_name,
knex.raw('??.??', [
alias || parentModel.table_name,
parentColumn.column_name
])
);
// console.log('HERE2');
// console.log(selectHmCount.toQuery());
//
// const q = selectParentQb.leftJoin(
// selectHmCount.as('t2'),
// parentModel.table_name + '.' + parentColumn.column_name,
// 't2.' + childColumn.column_name
// );
// console.log('HERE3');
// console.log(q.toQuery());
return qb => {
if (filter.comparison_op === 'empty') {
qb.where(knex.raw('0'), selectHmCount);
} else {
qb.whereNot(knex.raw('0'), selectHmCount);
}
};
}
const selectQb = knex(childModel.table_name).select(
childColumn.column_name
@ -123,7 +133,7 @@ const parseConditionV2 = async (
? negatedMapping[filter.comparison_op]
: {}),
fk_model_id: childModel.id,
fk_column_id: childModel?.primaryValue?.id,
fk_column_id: childModel?.primaryValue?.id
}),
knex,
aliasCount
@ -137,12 +147,12 @@ const parseConditionV2 = async (
};
} else if (colOptions.type === RelationTypes.BELONGS_TO) {
if (filter.comparison_op === 'null') {
return (qb) => {
return qb => {
qb.whereNull(childColumn.column_name);
};
}
if (filter.comparison_op === 'notnull') {
return (qb) => {
return qb => {
qb.whereNotNull(childColumn.column_name);
};
}
@ -158,7 +168,7 @@ const parseConditionV2 = async (
? negatedMapping[filter.comparison_op]
: {}),
fk_model_id: parentModel.id,
fk_column_id: parentModel?.primaryValue?.id,
fk_column_id: parentModel?.primaryValue?.id
}),
knex,
aliasCount
@ -190,7 +200,7 @@ const parseConditionV2 = async (
? negatedMapping[filter.comparison_op]
: {}),
fk_model_id: parentModel.id,
fk_column_id: parentModel?.primaryValue?.id,
fk_column_id: parentModel?.primaryValue?.id
}),
knex,
aliasCount
@ -204,7 +214,7 @@ const parseConditionV2 = async (
};
}
return (_qb) => {};
return _qb => {};
} else if (column.uidt === UITypes.Lookup) {
return await generateLookupCondition(column, filter, knex, aliasCount);
} else if (column.uidt === UITypes.Rollup && !customWhereClause) {
@ -212,7 +222,7 @@ const parseConditionV2 = async (
await genRollupSelectv2({
knex,
alias,
columnOptions: (await column.getColOptions()) as RollupColumn,
columnOptions: (await column.getColOptions()) as RollupColumn
})
).builder;
return parseConditionV2(
@ -250,7 +260,7 @@ const parseConditionV2 = async (
);
let val = customWhereClause ? customWhereClause : filter.value;
return (qb) => {
return qb => {
switch (filter.comparison_op) {
case 'eq':
qb = qb.where(field, val);
@ -358,7 +368,7 @@ const parseConditionV2 = async (
const negatedMapping = {
nlike: { comparison_op: 'like' },
neq: { comparison_op: 'eq' },
neq: { comparison_op: 'eq' }
};
function getAlias(aliasCount: { count: number }) {
@ -374,8 +384,9 @@ async function generateLookupCondition(
): Promise<any> {
const colOptions = await col.getColOptions<LookupColumn>();
const relationColumn = await colOptions.getRelationColumn();
const relationColumnOptions =
await relationColumn.getColOptions<LinkToAnotherRecordColumn>();
const relationColumnOptions = await relationColumn.getColOptions<
LinkToAnotherRecordColumn
>();
// const relationModel = await relationColumn.getModel();
const lookupColumn = await colOptions.getLookupColumn();
const alias = getAlias(aliasCount);
@ -398,7 +409,7 @@ async function generateLookupCondition(
...filter,
...(filter.comparison_op in negatedMapping
? negatedMapping[filter.comparison_op]
: {}),
: {})
},
lookupColumn,
qb,
@ -421,7 +432,7 @@ async function generateLookupCondition(
...filter,
...(filter.comparison_op in negatedMapping
? negatedMapping[filter.comparison_op]
: {}),
: {})
},
lookupColumn,
qb,
@ -455,7 +466,7 @@ async function generateLookupCondition(
...filter,
...(filter.comparison_op in negatedMapping
? negatedMapping[filter.comparison_op]
: {}),
: {})
},
lookupColumn,
qb,
@ -491,8 +502,9 @@ async function nestedConditionJoin(
await lookupColumn.getColOptions<LookupColumn>()
).getRelationColumn()
: lookupColumn;
const relationColOptions =
await relationColumn.getColOptions<LinkToAnotherRecordColumn>();
const relationColOptions = await relationColumn.getColOptions<
LinkToAnotherRecordColumn
>();
const relAlias = `__nc${aliasCount.count++}`;
const childColumn = await relationColOptions.getChildColumn();
@ -563,7 +575,7 @@ async function nestedConditionJoin(
new Filter({
...filter,
fk_model_id: childModel.id,
fk_column_id: childModel.primaryValue?.id,
fk_column_id: childModel.primaryValue?.id
}),
knex,
aliasCount,
@ -579,7 +591,7 @@ async function nestedConditionJoin(
new Filter({
...filter,
fk_model_id: parentModel.id,
fk_column_id: parentModel?.primaryValue?.id,
fk_column_id: parentModel?.primaryValue?.id
}),
knex,
aliasCount,
@ -595,7 +607,7 @@ async function nestedConditionJoin(
new Filter({
...filter,
fk_model_id: parentModel.id,
fk_column_id: parentModel.primaryValue?.id,
fk_column_id: parentModel.primaryValue?.id
}),
knex,
aliasCount,
@ -612,7 +624,7 @@ async function nestedConditionJoin(
new Filter({
...filter,
fk_model_id: (await lookupColumn.getModel()).id,
fk_column_id: lookupColumn?.id,
fk_column_id: lookupColumn?.id
}),
knex,
aliasCount,

Loading…
Cancel
Save