Browse Source

refactor(nocodb): remove parseDateOrDateTimeCondition

pull/5185/head
Wing-Kam Wong 2 years ago
parent
commit
0a67a812e3
  1. 189
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts

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

@ -35,15 +35,6 @@ function getLogicalOpMethod(filter: Filter) {
} }
} }
function parseDateOrDateTimeCondition(
qb: Knex.QueryBuilder,
op: '>' | '<' | '>=' | '<=' | '=' | '!=',
val: any
) {
// TODO:
console.log(qb, op, val);
}
const parseConditionV2 = async ( const parseConditionV2 = async (
_filter: Filter | Filter[], _filter: Filter | Filter[],
knex: XKnex, knex: XKnex,
@ -280,9 +271,13 @@ const parseConditionV2 = async (
return (qb: Knex.QueryBuilder) => { return (qb: Knex.QueryBuilder) => {
let [field, val] = [_field, _val]; let [field, val] = [_field, _val];
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt) && !val) { if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) {
// for date & datetime, val cannot be empty for all filters if (!val) {
return; // for date & datetime, val cannot be empty for all filters
return;
}
// handle sub operation
// TODO:
} }
if (isNumericCol(column.uidt) && typeof val === 'string') { if (isNumericCol(column.uidt) && typeof val === 'string') {
@ -292,79 +287,71 @@ const parseConditionV2 = async (
switch (filter.comparison_op) { switch (filter.comparison_op) {
case 'eq': case 'eq':
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) { if (qb?.client?.config?.client === 'mysql2') {
parseDateOrDateTimeCondition(qb, '=', val); if (
} else { [
if (qb?.client?.config?.client === 'mysql2') { UITypes.Duration,
if ( UITypes.Currency,
[ UITypes.Percent,
UITypes.Duration, UITypes.Number,
UITypes.Currency, UITypes.Decimal,
UITypes.Percent, UITypes.Rating,
UITypes.Number, UITypes.Rollup,
UITypes.Decimal, ].includes(column.uidt)
UITypes.Rating, ) {
UITypes.Rollup,
].includes(column.uidt)
) {
qb = qb.where(field, val);
} else {
// mysql is case-insensitive for strings, turn to case-sensitive
qb = qb.whereRaw('BINARY ?? = ?', [field, val]);
}
} else {
qb = qb.where(field, val); qb = qb.where(field, val);
} else {
// mysql is case-insensitive for strings, turn to case-sensitive
qb = qb.whereRaw('BINARY ?? = ?', [field, val]);
} }
if (column.uidt === UITypes.Rating && val === 0) { } else {
// unset rating is considered as NULL qb = qb.where(field, val);
qb = qb.orWhereNull(field); }
} 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':
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) { if (qb?.client?.config?.client === 'mysql2') {
parseDateOrDateTimeCondition(qb, '!=', val); if (
} else { [
if (qb?.client?.config?.client === 'mysql2') { UITypes.Duration,
if ( UITypes.Currency,
[ UITypes.Percent,
UITypes.Duration, UITypes.Number,
UITypes.Currency, UITypes.Decimal,
UITypes.Percent, UITypes.Rollup,
UITypes.Number, ].includes(column.uidt)
UITypes.Decimal, ) {
UITypes.Rollup,
].includes(column.uidt)
) {
qb = qb.where((nestedQb) => {
nestedQb
.whereNot(field, val)
.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 {
// mysql is case-insensitive for strings, turn to case-sensitive
qb = qb.where((nestedQb) => {
nestedQb.whereRaw('BINARY ?? != ?', [field, val]);
if (column.uidt !== UITypes.Rating) {
nestedQb.orWhereNull(customWhereClause ? _val : _field);
}
});
}
} else {
qb = qb.where((nestedQb) => { qb = qb.where((nestedQb) => {
nestedQb nestedQb
.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 {
// mysql is case-insensitive for strings, turn to case-sensitive
qb = qb.where((nestedQb) => {
nestedQb.whereRaw('BINARY ?? != ?', [field, val]);
if (column.uidt !== UITypes.Rating) {
nestedQb.orWhereNull(customWhereClause ? _val : _field);
}
});
} }
} else {
qb = qb.where((nestedQb) => {
nestedQb
.whereNot(field, val)
.orWhereNull(customWhereClause ? _val : _field);
});
} }
break; break;
case 'like': case 'like':
@ -474,59 +461,43 @@ const parseConditionV2 = async (
break; break;
case 'gt': case 'gt':
const gt_op = customWhereClause ? '<' : '>'; const gt_op = customWhereClause ? '<' : '>';
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) { qb = qb.where(field, gt_op, val);
parseDateOrDateTimeCondition(qb, gt_op, val); if (column.uidt === UITypes.Rating) {
} else { // unset rating is considered as NULL
qb = qb.where(field, gt_op, val); if (gt_op === '<' && val > 0) {
if (column.uidt === UITypes.Rating) { qb = qb.orWhereNull(field);
// unset rating is considered as NULL
if (gt_op === '<' && val > 0) {
qb = qb.orWhereNull(field);
}
} }
} }
break; break;
case 'ge': case 'ge':
case 'gte': case 'gte':
const ge_op = customWhereClause ? '<=' : '>='; const ge_op = customWhereClause ? '<=' : '>=';
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) { qb = qb.where(field, ge_op, val);
parseDateOrDateTimeCondition(qb, ge_op, val); if (column.uidt === UITypes.Rating) {
} else { // unset rating is considered as NULL
qb = qb.where(field, ge_op, val); if (ge_op === '<=' || (ge_op === '>=' && val === 0)) {
if (column.uidt === UITypes.Rating) { qb = qb.orWhereNull(field);
// unset rating is considered as NULL
if (ge_op === '<=' || (ge_op === '>=' && val === 0)) {
qb = qb.orWhereNull(field);
}
} }
} }
break; break;
case 'lt': case 'lt':
const lt_op = customWhereClause ? '>' : '<'; const lt_op = customWhereClause ? '>' : '<';
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) { qb = qb.where(field, lt_op, val);
parseDateOrDateTimeCondition(qb, lt_op, val); if (column.uidt === UITypes.Rating) {
} else { // unset number is considered as NULL
qb = qb.where(field, lt_op, val); if (lt_op === '<' && val > 0) {
if (column.uidt === UITypes.Rating) { qb = qb.orWhereNull(field);
// unset number is considered as NULL
if (lt_op === '<' && val > 0) {
qb = qb.orWhereNull(field);
}
} }
} }
break; break;
case 'le': case 'le':
case 'lte': case 'lte':
const le_op = customWhereClause ? '>=' : '<='; const le_op = customWhereClause ? '>=' : '<=';
if ([UITypes.Date, UITypes.DateTime].includes(column.uidt)) { qb = qb.where(field, le_op, val);
parseDateOrDateTimeCondition(qb, le_op, val); if (column.uidt === UITypes.Rating) {
} else { // unset number is considered as NULL
qb = qb.where(field, le_op, val); if (le_op === '<=' || (le_op === '>=' && val === 0)) {
if (column.uidt === UITypes.Rating) { qb = qb.orWhereNull(field);
// unset number is considered as NULL
if (le_op === '<=' || (le_op === '>=' && val === 0)) {
qb = qb.orWhereNull(field);
}
} }
} }
break; break;

Loading…
Cancel
Save