From 625c59e2f54ab4b0b33fb81a7f4d1d402be4cb02 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 1 Feb 2023 18:07:20 +0800 Subject: [PATCH] fix(nocodb): condition nlike logic --- .../db/sql-data-mapper/lib/sql/conditionV2.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts index 54ad7faaea..5c96d1a36a 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/conditionV2.ts @@ -297,20 +297,34 @@ const parseConditionV2 = async ( } break; case 'nlike': - if (column.uidt === UITypes.Formula) { - [field, val] = [val, field]; - val = `%${val}%`.replace(/^%'([\s\S]*)'%$/, '%$1%'); + if (!val) { + // val is empty -> include all values but empty strings + qb.whereNot(field, ''); + qb.orWhereNull(field); } else { - val = val.startsWith('%') || val.endsWith('%') ? val : `%${val}%`; - } - qb.where((nestedQb) => { - if (qb?.client?.config?.client === 'pg') { - nestedQb.whereRaw('??::text not ilike ?', [field, val]); + if (column.uidt === UITypes.Formula) { + [field, val] = [val, field]; + val = `%${val}%`.replace(/^%'([\s\S]*)'%$/, '%$1%'); } else { - nestedQb.whereNot(field, 'like', val); + val = + val.startsWith('%') || val.endsWith('%') ? val : `%${val}%`; } - nestedQb.orWhereNull(field); - }); + qb.where((nestedQb) => { + if (qb?.client?.config?.client === 'pg') { + nestedQb.whereRaw('??::text not ilike ?', [field, val]); + } else { + nestedQb.whereNot(field, 'like', val); + } + if (val !== '%%') { + // if value is not empty, empty or null should be included + nestedQb.orWhere(field, ''); + nestedQb.orWhereNull(field); + } else { + // if value is empty, then only null is included + nestedQb.orWhereNull(field); + } + }); + } break; case 'allof': case 'anyof':