Browse Source

fix(nocodb): rating filter logic

pull/5106/head
Wing-Kam Wong 2 years ago
parent
commit
ffa379d12d
  1. 57
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts

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

@ -280,6 +280,12 @@ const parseConditionV2 = async (
// val cannot be empty for non-is & non-isnot filters // val cannot be empty for non-is & non-isnot filters
return; return;
} }
if (column.uidt === UITypes.Rating) {
// convert to number
val = +val;
}
switch (filter.comparison_op) { switch (filter.comparison_op) {
case 'eq': case 'eq':
if (qb?.client?.config?.client === 'mysql2') { if (qb?.client?.config?.client === 'mysql2') {
@ -302,6 +308,10 @@ const parseConditionV2 = async (
} else { } else {
qb = qb.where(field, val); qb = qb.where(field, val);
} }
if (column.uidt === UITypes.Rating && val === 0) {
// unset rating is considered as NULL
qb = qb.orWhereNull(field);
}
break; break;
case 'neq': case 'neq':
case 'not': case 'not':
@ -313,7 +323,6 @@ const parseConditionV2 = async (
UITypes.Percent, UITypes.Percent,
UITypes.Number, UITypes.Number,
UITypes.Decimal, UITypes.Decimal,
UITypes.Rating,
UITypes.Rollup, UITypes.Rollup,
].includes(column.uidt) ].includes(column.uidt)
) { ) {
@ -322,12 +331,20 @@ const parseConditionV2 = async (
.whereNot(field, val) .whereNot(field, val)
.orWhereNull(customWhereClause ? _val : _field); .orWhereNull(customWhereClause ? _val : _field);
}); });
} else if (column.uidt === UITypes.Rating) {
// unset rating is considered as NULL
if (val === 0) {
qb = qb.whereNot(field, val).whereNotNull(field);
} else {
qb = qb.whereNot(field, val).orWhereNull(field);
}
} else { } else {
// mysql is case-insensitive for strings, turn to case-sensitive // mysql is case-insensitive for strings, turn to case-sensitive
qb = qb.where((nestedQb) => { qb = qb.where((nestedQb) => {
nestedQb nestedQb.whereRaw('BINARY ?? != ?', [field, val]);
.whereRaw('BINARY ?? != ?', [field, val]) if (column.uidt !== UITypes.Rating) {
.orWhereNull(customWhereClause ? _val : _field); nestedQb.orWhereNull(customWhereClause ? _val : _field);
}
}); });
} }
} else { } else {
@ -431,11 +448,23 @@ const parseConditionV2 = async (
} }
break; break;
case 'gt': case 'gt':
qb = qb.where(field, customWhereClause ? '<' : '>', val); const gt_op = customWhereClause ? '<' : '>';
qb = qb.where(field, gt_op, val);
if (column.uidt === UITypes.Rating) {
if (gt_op === '<' && val > 0) {
qb = qb.orWhereNull(field);
}
}
break; break;
case 'ge': case 'ge':
case 'gte': case 'gte':
qb = qb.where(field, customWhereClause ? '<=' : '>=', val); const ge_op = customWhereClause ? '<=' : '>=';
qb = qb.where(field, ge_op, val);
if (column.uidt === UITypes.Rating) {
if (ge_op === '<=' || (ge_op === '>=' && val === 0)) {
qb = qb.orWhereNull(field);
}
}
break; break;
case 'in': case 'in':
qb = qb.whereIn( qb = qb.whereIn(
@ -474,11 +503,23 @@ const parseConditionV2 = async (
qb = qb.whereNot(customWhereClause || field, false); qb = qb.whereNot(customWhereClause || field, false);
break; break;
case 'lt': case 'lt':
qb = qb.where(field, customWhereClause ? '>' : '<', val); const lt_op = customWhereClause ? '>' : '<';
qb = qb.where(field, lt_op, val);
if (column.uidt === UITypes.Rating) {
if (lt_op === '<' && val > 0) {
qb = qb.orWhereNull(field);
}
}
break; break;
case 'le': case 'le':
case 'lte': case 'lte':
qb = qb.where(field, customWhereClause ? '>=' : '<=', val); const le_op = customWhereClause ? '>=' : '<=';
qb = qb.where(field, le_op, val);
if (column.uidt === UITypes.Rating) {
if (le_op === '<=' || (le_op === '>=' && val === 0)) {
qb = qb.orWhereNull(field);
}
}
break; break;
case 'empty': case 'empty':
if (column.uidt === UITypes.Formula) { if (column.uidt === UITypes.Formula) {

Loading…
Cancel
Save