Browse Source

fix: get aggregated value as json string array to avoid issues with separator

pull/6987/head
Pranav C 1 year ago
parent
commit
2b4eb320ed
  1. 54
      packages/nocodb/src/db/generateLookupSelectQuery.ts

54
packages/nocodb/src/db/generateLookupSelectQuery.ts

@ -304,44 +304,52 @@ export default async function generateLookupSelectQuery({
const subQueryAlias = getAlias(); const subQueryAlias = getAlias();
if (baseModelSqlv2.isPg) { if (baseModelSqlv2.isPg) {
// selectQb.orderBy(`${lookupColumn.title}`, 'asc'); selectQb.orderBy(`${lookupColumn.title}`, 'asc');
// // alternate approach with array_agg // alternate approach with array_agg
// return {
// builder: knex
// .select(knex.raw('array_agg(??)', [lookupColumn.title]))
// .from(selectQb),
// };
return { return {
builder: knex builder: knex
.select( .select(knex.raw('json_agg(??)::text', [lookupColumn.title]))
knex.raw('STRING_AGG(??::text, ?)', [ .from(selectQb),
lookupColumn.title,
LOOKUP_VAL_SEPARATOR,
]),
)
.from(selectQb.as(subQueryAlias)),
}; };
} else if (baseModelSqlv2.isMySQL) { /*
// // alternate approach with JSON_ARRAYAGG // alternate approach with array_agg
return {
builder: knex
.select(knex.raw('array_agg(??)', [lookupColumn.title]))
.from(selectQb),
};*/
// alternate approach with string aggregation
// return { // return {
// builder: knex // builder: knex
// .select( // .select(
// knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.title]), // knex.raw('STRING_AGG(??::text, ?)', [
// lookupColumn.title,
// LOOKUP_VAL_SEPARATOR,
// ]),
// ) // )
// .from(selectQb.as(subQueryAlias)), // .from(selectQb.as(subQueryAlias)),
// }; // };
} else if (baseModelSqlv2.isMySQL) {
// alternate approach with JSON_ARRAYAGG
return { return {
builder: knex builder: knex
.select( .select(
knex.raw('GROUP_CONCAT(?? ORDER BY ?? ASC SEPARATOR ?)', [ knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.title]),
lookupColumn.title,
lookupColumn.title,
LOOKUP_VAL_SEPARATOR,
]),
) )
.from(selectQb.as(subQueryAlias)), .from(selectQb.as(subQueryAlias)),
}; };
// return {
// builder: knex
// .select(
// knex.raw('GROUP_CONCAT(?? ORDER BY ?? ASC SEPARATOR ?)', [
// lookupColumn.title,
// lookupColumn.title,
// LOOKUP_VAL_SEPARATOR,
// ]),
// )
// .from(selectQb.as(subQueryAlias)),
// };
} else if (baseModelSqlv2.isSqlite) { } else if (baseModelSqlv2.isSqlite) {
// ref: https://stackoverflow.com/questions/13382856/sqlite3-join-group-concat-using-distinct-with-custom-separator // ref: https://stackoverflow.com/questions/13382856/sqlite3-join-group-concat-using-distinct-with-custom-separator
// selectQb.orderBy(`${lookupColumn.title}`, 'asc'); // selectQb.orderBy(`${lookupColumn.title}`, 'asc');

Loading…
Cancel
Save