Browse Source

fix(api): set sort order of nulls

re #4881

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4885/head
Pranav C 2 years ago
parent
commit
7ceccc130a
  1. 41
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/sortV2.ts

41
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/sortV2.ts

@ -29,6 +29,9 @@ export default async function sortV2(
const column = await sort.getColumn(); const column = await sort.getColumn();
if (!column) continue; if (!column) continue;
const model = await column.getModel(); const model = await column.getModel();
const nulls = sort.direction === 'desc' ? 'LAST' : 'FIRST';
switch (column.uidt) { switch (column.uidt) {
case UITypes.Rollup: case UITypes.Rollup:
{ {
@ -39,7 +42,7 @@ export default async function sortV2(
}) })
).builder; ).builder;
qb.orderBy(builder, sort.direction || 'asc'); qb.orderBy(builder, sort.direction || 'asc', nulls);
} }
break; break;
case UITypes.Formula: case UITypes.Formula:
@ -55,7 +58,7 @@ export default async function sortV2(
column column
) )
).builder; ).builder;
qb.orderBy(builder, sort.direction || 'asc'); qb.orderBy(builder, sort.direction || 'asc', nulls);
} }
break; break;
case UITypes.Lookup: case UITypes.Lookup:
@ -178,7 +181,7 @@ export default async function sortV2(
break; break;
} }
qb.orderBy(selectQb, sort.direction || 'asc'); qb.orderBy(selectQb, sort.direction || 'asc', nulls);
} }
} }
break; break;
@ -206,7 +209,7 @@ export default async function sortV2(
]) ])
); );
qb.orderBy(selectQb, sort.direction || 'asc'); qb.orderBy(selectQb, sort.direction || 'asc', nulls);
} }
break; break;
case UITypes.SingleSelect: { case UITypes.SingleSelect: {
@ -214,17 +217,23 @@ export default async function sortV2(
if (clientType === 'mysql' || clientType === 'mysql2') { if (clientType === 'mysql' || clientType === 'mysql2') {
qb.orderBy( qb.orderBy(
sanitize(knex.raw('CONCAT(??)', [column.column_name])), sanitize(knex.raw('CONCAT(??)', [column.column_name])),
sort.direction || 'asc' sort.direction || 'asc',
nulls
); );
} else if (clientType === 'mssql') { } else if (clientType === 'mssql') {
qb.orderBy( qb.orderBy(
sanitize( sanitize(
knex.raw('CAST(?? AS VARCHAR(MAX))', [column.column_name]) knex.raw('CAST(?? AS VARCHAR(MAX))', [column.column_name])
), ),
sort.direction || 'asc' sort.direction || 'asc',
nulls
); );
} else { } else {
qb.orderBy(sanitize(column.column_name), sort.direction || 'asc'); qb.orderBy(
sanitize(column.column_name),
sort.direction || 'asc',
nulls
);
} }
break; break;
} }
@ -233,22 +242,32 @@ export default async function sortV2(
if (clientType === 'mysql' || clientType === 'mysql2') { if (clientType === 'mysql' || clientType === 'mysql2') {
qb.orderBy( qb.orderBy(
sanitize(knex.raw('CONCAT(??)', [column.column_name])), sanitize(knex.raw('CONCAT(??)', [column.column_name])),
sort.direction || 'asc' sort.direction || 'asc',
nulls
); );
} else if (clientType === 'mssql') { } else if (clientType === 'mssql') {
qb.orderBy( qb.orderBy(
sanitize( sanitize(
knex.raw('CAST(?? AS VARCHAR(MAX))', [column.column_name]) knex.raw('CAST(?? AS VARCHAR(MAX))', [column.column_name])
), ),
sort.direction || 'asc' sort.direction || 'asc',
nulls
); );
} else { } else {
qb.orderBy(sanitize(column.column_name), sort.direction || 'asc'); qb.orderBy(
sanitize(column.column_name),
sort.direction || 'asc',
nulls
);
} }
break; break;
} }
default: default:
qb.orderBy(sanitize(column.column_name), sort.direction || 'asc'); qb.orderBy(
sanitize(column.column_name),
sort.direction || 'asc',
nulls
);
break; break;
} }
} }

Loading…
Cancel
Save