|
|
|
@ -339,6 +339,12 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
let data; |
|
|
|
|
|
|
|
|
|
console.log(` |
|
|
|
|
|
|
|
|
|
${qb.toQuery()} |
|
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
data = await this.execAndParse(qb); |
|
|
|
|
} catch (e) { |
|
|
|
@ -499,6 +505,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
args.column_name = args.column_name || ''; |
|
|
|
|
|
|
|
|
|
const cols = await this.model.getColumns(); |
|
|
|
|
const groupByColumns: Record<string, Column> = {}; |
|
|
|
|
|
|
|
|
|
const selectors = []; |
|
|
|
|
const groupBySelectors = []; |
|
|
|
@ -508,11 +515,13 @@ class BaseModelSqlv2 {
|
|
|
|
|
const column = cols.find( |
|
|
|
|
(c) => c.column_name === col || c.title === col, |
|
|
|
|
); |
|
|
|
|
groupByColumns[column.id] = column; |
|
|
|
|
if (!column) { |
|
|
|
|
throw NcError.notFound('Column not found'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (column.uidt) { |
|
|
|
|
case UITypes.Links: |
|
|
|
|
case UITypes.Rollup: |
|
|
|
|
selectors.push( |
|
|
|
|
( |
|
|
|
@ -554,8 +563,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
case UITypes.Lookup: |
|
|
|
|
{ |
|
|
|
|
let selectQb; |
|
|
|
|
try { |
|
|
|
|
const _selectQb = await generateBTLookupSelectQuery({ |
|
|
|
|
baseModelSqlv2: this, |
|
|
|
|
column, |
|
|
|
@ -563,25 +570,18 @@ class BaseModelSqlv2 {
|
|
|
|
|
model: this.model, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
selectQb = this.dbDriver.raw(`?? as ??`, [ |
|
|
|
|
const selectQb = this.dbDriver.raw(`?? as ??`, [ |
|
|
|
|
this.dbDriver.raw(_selectQb.builder).wrap('(', ')'), |
|
|
|
|
sanitize(column.title), |
|
|
|
|
]); |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e); |
|
|
|
|
// return dummy select
|
|
|
|
|
selectQb = this.dbDriver.raw(`'ERR' as ??`, [ |
|
|
|
|
sanitize(column.title), |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
selectors.push(selectQb); |
|
|
|
|
groupBySelectors.push(column.title); |
|
|
|
|
groupBySelectors.push(sanitize(column.title)); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
selectors.push(column.column_name); |
|
|
|
|
groupBySelectors.push(sanitize(column.title)); |
|
|
|
|
groupBySelectors.push(sanitize(column.column_name)); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
}), |
|
|
|
@ -634,6 +634,17 @@ class BaseModelSqlv2 {
|
|
|
|
|
? args.sortArr |
|
|
|
|
: await Sort.list({ viewId: this.viewId }); |
|
|
|
|
|
|
|
|
|
sorts = sorts.filter((sort) => { |
|
|
|
|
if (!groupByColumns[sort.fk_column_id]) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
qb.orderBy( |
|
|
|
|
groupByColumns[sort.fk_column_id].column_name || |
|
|
|
|
groupByColumns[sort.fk_column_id].title, |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (sorts) await sortV2(this, sorts, qb); |
|
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
return await qb; |
|
|
|
@ -668,6 +679,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
switch (column.uidt) { |
|
|
|
|
case UITypes.Rollup: |
|
|
|
|
case UITypes.Links: |
|
|
|
|
selectors.push( |
|
|
|
|
( |
|
|
|
|
await genRollupSelectv2({ |
|
|
|
@ -725,7 +737,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
selectors.push(column.column_name); |
|
|
|
|
groupBySelectors.push(sanitize(column.title)); |
|
|
|
|
groupBySelectors.push(sanitize(column.column_name)); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
}), |
|
|
|
@ -4894,7 +4906,7 @@ async function generateBTLookupSelectQuery({
|
|
|
|
|
{ |
|
|
|
|
let aliasCount = 0, |
|
|
|
|
selectQb; |
|
|
|
|
const alias = `__nc_lookup${aliasCount++}`; |
|
|
|
|
const alias = `__nc_lk_${aliasCount++}`; |
|
|
|
|
const lookup = await column.getColOptions<LookupColumn>(); |
|
|
|
|
{ |
|
|
|
|
const relationCol = await lookup.getRelationColumn(); |
|
|
|
@ -4925,7 +4937,7 @@ async function generateBTLookupSelectQuery({
|
|
|
|
|
let lookupColumn = await lookup.getLookupColumn(); |
|
|
|
|
let prevAlias = alias; |
|
|
|
|
while (lookupColumn.uidt === UITypes.Lookup) { |
|
|
|
|
const nestedAlias = `__nc_sort${aliasCount++}`; |
|
|
|
|
const nestedAlias = `__nc_lk_nested_${aliasCount++}`; |
|
|
|
|
const nestedLookup = await lookupColumn.getColOptions<LookupColumn>(); |
|
|
|
|
const relationCol = await nestedLookup.getRelationColumn(); |
|
|
|
|
const relation = |
|
|
|
|