Browse Source

enhancement: treat NULL values as empty strings for CONCAT function

pull/2160/head
Wing-Kam Wong 3 years ago
parent
commit
1d94b7577a
  1. 33
      packages/nocodb/src/lib/dataMapper/lib/sql/formulav2/formulaQueryBuilderv2.ts

33
packages/nocodb/src/lib/dataMapper/lib/sql/formulav2/formulaQueryBuilderv2.ts

@ -612,7 +612,21 @@ export default async function formulaQueryBuilderv2(
return knex.raw( return knex.raw(
`${pt.callee.name}(${pt.arguments `${pt.callee.name}(${pt.arguments
.map(arg => fn(arg).toQuery()) .map(arg => {
const query = fn(arg).toQuery();
if (pt.callee.name === 'CONCAT') {
if (knex.clientType() === 'mysql2') {
// mysql2: CONCAT() returns NULL if any argument is NULL.
// adding IFNULL to convert NULL values to empty strings
return `IFNULL(${query}, '')`;
} else {
// do nothing
// pg / mssql: Concatenate all arguments. NULL arguments are ignored.
// sqlite3: special handling - See BinaryExpression
}
}
return query;
})
.join()})${colAlias}` .join()})${colAlias}`
); );
} else if (pt.type === 'Literal') { } else if (pt.type === 'Literal') {
@ -637,13 +651,16 @@ export default async function formulaQueryBuilderv2(
pt.left.fnName = pt.left.fnName || 'ARITH'; pt.left.fnName = pt.left.fnName || 'ARITH';
pt.right.fnName = pt.right.fnName || 'ARITH'; pt.right.fnName = pt.right.fnName || 'ARITH';
const query = knex.raw( const left = fn(pt.left, null, pt.operator).toQuery();
`${fn(pt.left, null, pt.operator).toQuery()} ${pt.operator} ${fn( const right = fn(pt.right, null, pt.operator).toQuery();
pt.right, let sql = `${left} ${pt.operator} ${right}${colAlias}`;
null,
pt.operator // handle NULL values when calling CONCAT for sqlite3
).toQuery()}${colAlias}` if (pt.left.fnName === 'CONCAT' && knex.clientType() === 'sqlite3') {
); sql = `COALESCE(${left}, '') ${pt.operator} COALESCE(${right},'')${colAlias}`;
}
const query = knex.raw(sql);
if (prevBinaryOp && pt.operator !== prevBinaryOp) { if (prevBinaryOp && pt.operator !== prevBinaryOp) {
query.wrap('(', ')'); query.wrap('(', ')');
} }

Loading…
Cancel
Save