|
|
|
@ -17,7 +17,7 @@ import Sort from '../../../../models/Sort';
|
|
|
|
|
import FormulaColumn from '../../../../models/FormulaColumn'; |
|
|
|
|
import genRollupSelectv2 from './genRollupSelectv2'; |
|
|
|
|
import formulaQueryBuilderv2 from './formulav2/formulaQueryBuilderv2'; |
|
|
|
|
import { QueryBuilder } from 'knex'; |
|
|
|
|
import { Knex } from 'knex'; |
|
|
|
|
import View from '../../../../models/View'; |
|
|
|
|
import { |
|
|
|
|
AuditOperationSubTypes, |
|
|
|
@ -391,7 +391,10 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(childTable.table_name); |
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(childTn); |
|
|
|
|
await childModel.selectObject({ qb }); |
|
|
|
|
await this.applySortAndFilter({ table: childTable, where, qb, sort }); |
|
|
|
|
|
|
|
|
@ -404,7 +407,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.select(this.dbDriver.raw('? as ??', [p, GROUP_COL])) |
|
|
|
|
.whereIn( |
|
|
|
|
chilCol.column_name, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(parentCol.column_name) |
|
|
|
|
// .where(parentTable.primaryKey.cn, p)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, p)) |
|
|
|
@ -477,13 +480,16 @@ class BaseModelSqlv2 {
|
|
|
|
|
const parentTable = await parentCol.getModel(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const children = await this.dbDriver.unionAll( |
|
|
|
|
ids.map((p) => { |
|
|
|
|
const query = this.dbDriver(childTable.table_name) |
|
|
|
|
const query = this.dbDriver(childTn) |
|
|
|
|
.count(`${chilCol?.column_name} as count`) |
|
|
|
|
.whereIn( |
|
|
|
|
chilCol.column_name, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(parentCol.column_name) |
|
|
|
|
// .where(parentTable.primaryKey.cn, p)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, p)) |
|
|
|
@ -525,12 +531,15 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(childTable.table_name); |
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(childTn); |
|
|
|
|
await this.applySortAndFilter({ table: childTable, where, qb, sort }); |
|
|
|
|
|
|
|
|
|
qb.whereIn( |
|
|
|
|
chilCol.column_name, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(parentCol.column_name) |
|
|
|
|
// .where(parentTable.primaryKey.cn, p)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, id)) |
|
|
|
@ -576,11 +585,14 @@ class BaseModelSqlv2 {
|
|
|
|
|
const parentTable = await parentCol.getModel(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const query = this.dbDriver(childTable.table_name) |
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const query = this.dbDriver(childTn) |
|
|
|
|
.count(`${chilCol?.column_name} as count`) |
|
|
|
|
.whereIn( |
|
|
|
|
chilCol.column_name, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(parentCol.column_name) |
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, id)) |
|
|
|
|
) |
|
|
|
@ -607,7 +619,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
// const tn = this.model.tn;
|
|
|
|
|
// const cn = (await relColOptions.getChildColumn()).title;
|
|
|
|
|
const vtn = (await relColOptions.getMMModel()).table_name; |
|
|
|
|
const mmTable = await relColOptions.getMMModel(); |
|
|
|
|
const vtn = this.getTnPath(mmTable); |
|
|
|
|
const vcn = (await relColOptions.getMMChildColumn()).column_name; |
|
|
|
|
const vrcn = (await relColOptions.getMMParentColumn()).column_name; |
|
|
|
|
const rcn = (await relColOptions.getParentColumn()).column_name; |
|
|
|
@ -619,7 +632,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
dbDriver: this.dbDriver, |
|
|
|
|
model: childTable, |
|
|
|
|
}); |
|
|
|
|
const rtn = childTable.table_name; |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = childTn; |
|
|
|
|
const rtnId = childTable.id; |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn).join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`); |
|
|
|
@ -634,7 +651,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.clone() |
|
|
|
|
.whereIn( |
|
|
|
|
`${vtn}.${vcn}`, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(cn) |
|
|
|
|
// .where(parentTable.primaryKey.cn, id)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, id)) |
|
|
|
@ -680,7 +697,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
// const tn = this.model.tn;
|
|
|
|
|
// const cn = (await relColOptions.getChildColumn()).title;
|
|
|
|
|
const vtn = (await relColOptions.getMMModel()).table_name; |
|
|
|
|
const mmTable = await relColOptions.getMMModel(); |
|
|
|
|
const vtn = this.getTnPath(mmTable); |
|
|
|
|
const vcn = (await relColOptions.getMMChildColumn()).column_name; |
|
|
|
|
const vrcn = (await relColOptions.getMMParentColumn()).column_name; |
|
|
|
|
const rcn = (await relColOptions.getParentColumn()).column_name; |
|
|
|
@ -692,14 +710,18 @@ class BaseModelSqlv2 {
|
|
|
|
|
dbDriver: this.dbDriver, |
|
|
|
|
model: childTable, |
|
|
|
|
}); |
|
|
|
|
const rtn = childTable.table_name; |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = childTn; |
|
|
|
|
const rtnId = childTable.id; |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn) |
|
|
|
|
.join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`) |
|
|
|
|
.whereIn( |
|
|
|
|
`${vtn}.${vcn}`, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(cn) |
|
|
|
|
// .where(parentTable.primaryKey.cn, id)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, parentId)) |
|
|
|
@ -731,16 +753,21 @@ class BaseModelSqlv2 {
|
|
|
|
|
const relColOptions = |
|
|
|
|
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
|
|
|
|
|
const vtn = (await relColOptions.getMMModel()).table_name; |
|
|
|
|
const mmTable = await relColOptions.getMMModel(); |
|
|
|
|
const vtn = this.getTnPath(mmTable); |
|
|
|
|
const vcn = (await relColOptions.getMMChildColumn()).column_name; |
|
|
|
|
const vrcn = (await relColOptions.getMMParentColumn()).column_name; |
|
|
|
|
const rcn = (await relColOptions.getParentColumn()).column_name; |
|
|
|
|
const cn = (await relColOptions.getChildColumn()).column_name; |
|
|
|
|
const childTable = await (await relColOptions.getParentColumn()).getModel(); |
|
|
|
|
const rtn = childTable.table_name; |
|
|
|
|
const parentTable = await (await relColOptions.getChildColumn()).getModel(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = childTn; |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn) |
|
|
|
|
.join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`) |
|
|
|
|
// .select({
|
|
|
|
@ -755,7 +782,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.clone() |
|
|
|
|
.whereIn( |
|
|
|
|
`${vtn}.${vcn}`, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(cn) |
|
|
|
|
// .where(parentTable.primaryKey.cn, id)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, id)) |
|
|
|
@ -778,16 +805,21 @@ class BaseModelSqlv2 {
|
|
|
|
|
const relColOptions = |
|
|
|
|
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
|
|
|
|
|
const vtn = (await relColOptions.getMMModel()).table_name; |
|
|
|
|
const mmTable = await relColOptions.getMMModel(); |
|
|
|
|
const vtn = this.getTnPath(mmTable); |
|
|
|
|
const vcn = (await relColOptions.getMMChildColumn()).column_name; |
|
|
|
|
const vrcn = (await relColOptions.getMMParentColumn()).column_name; |
|
|
|
|
const rcn = (await relColOptions.getParentColumn()).column_name; |
|
|
|
|
const cn = (await relColOptions.getChildColumn()).column_name; |
|
|
|
|
const childTable = await (await relColOptions.getParentColumn()).getModel(); |
|
|
|
|
const rtn = childTable.table_name; |
|
|
|
|
const parentTable = await (await relColOptions.getChildColumn()).getModel(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = childTn; |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn) |
|
|
|
|
.join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`) |
|
|
|
|
// .select({
|
|
|
|
@ -796,7 +828,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.count(`${vtn}.${vcn}`, { as: 'count' }) |
|
|
|
|
.whereIn( |
|
|
|
|
`${vtn}.${vcn}`, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(cn) |
|
|
|
|
// .where(parentTable.primaryKey.cn, id)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, parentId)) |
|
|
|
@ -820,7 +852,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
const relColOptions = |
|
|
|
|
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
|
|
|
|
|
const vtn = (await relColOptions.getMMModel()).table_name; |
|
|
|
|
const mmTable = await relColOptions.getMMModel(); |
|
|
|
|
const vtn = this.getTnPath(mmTable); |
|
|
|
|
const vcn = (await relColOptions.getMMChildColumn()).column_name; |
|
|
|
|
const vrcn = (await relColOptions.getMMParentColumn()).column_name; |
|
|
|
|
const rcn = (await relColOptions.getParentColumn()).column_name; |
|
|
|
@ -828,7 +861,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
const childTable = await (await relColOptions.getParentColumn()).getModel(); |
|
|
|
|
const parentTable = await (await relColOptions.getChildColumn()).getModel(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
const rtn = childTable.table_name; |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = childTn; |
|
|
|
|
const qb = this.dbDriver(rtn) |
|
|
|
|
.count(`*`, { as: 'count' }) |
|
|
|
|
.where((qb) => { |
|
|
|
@ -839,7 +876,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.join(vtn, `${rtn}.${rcn}`, `${vtn}.${vrcn}`) |
|
|
|
|
.whereIn( |
|
|
|
|
`${vtn}.${vcn}`, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(cn) |
|
|
|
|
// .where(parentTable.primaryKey.cn, pid)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, pid)) |
|
|
|
@ -866,7 +903,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
const relColOptions = |
|
|
|
|
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
|
|
|
|
|
const vtn = (await relColOptions.getMMModel()).table_name; |
|
|
|
|
const mmTable = await relColOptions.getMMModel(); |
|
|
|
|
const vtn = this.getTnPath(mmTable); |
|
|
|
|
const vcn = (await relColOptions.getMMChildColumn()).column_name; |
|
|
|
|
const vrcn = (await relColOptions.getMMParentColumn()).column_name; |
|
|
|
|
const rcn = (await relColOptions.getParentColumn()).column_name; |
|
|
|
@ -878,7 +916,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
const parentTable = await (await relColOptions.getChildColumn()).getModel(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
const rtn = childTable.table_name; |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = childTn; |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn).where((qb) => |
|
|
|
|
qb |
|
|
|
@ -889,7 +931,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.join(vtn, `${rtn}.${rcn}`, `${vtn}.${vrcn}`) |
|
|
|
|
.whereIn( |
|
|
|
|
`${vtn}.${vcn}`, |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(cn) |
|
|
|
|
// .where(parentTable.primaryKey.cn, pid)
|
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, pid)) |
|
|
|
@ -945,8 +987,12 @@ class BaseModelSqlv2 {
|
|
|
|
|
const parentTable = await ( |
|
|
|
|
await relColOptions.getParentColumn() |
|
|
|
|
).getModel(); |
|
|
|
|
const tn = childTable.table_name; |
|
|
|
|
const rtn = parentTable.table_name; |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const tn = childTn; |
|
|
|
|
const rtn = parentTn; |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(tn) |
|
|
|
@ -993,8 +1039,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const tn = childTable.table_name; |
|
|
|
|
const rtn = parentTable.table_name; |
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const tn = childTn; |
|
|
|
|
const rtn = parentTn; |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(tn).where((qb) => { |
|
|
|
|
qb.whereNotIn( |
|
|
|
@ -1054,8 +1103,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
const cn = (await relColOptions.getChildColumn()).column_name; |
|
|
|
|
const childTable = await (await relColOptions.getChildColumn()).getModel(); |
|
|
|
|
|
|
|
|
|
const rtn = parentTable.table_name; |
|
|
|
|
const tn = childTable.table_name; |
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = parentTn; |
|
|
|
|
const tn = childTn; |
|
|
|
|
await childTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn) |
|
|
|
@ -1100,8 +1152,12 @@ class BaseModelSqlv2 {
|
|
|
|
|
dbDriver: this.dbDriver, |
|
|
|
|
model: parentTable, |
|
|
|
|
}); |
|
|
|
|
const rtn = parentTable.table_name; |
|
|
|
|
const tn = childTable.table_name; |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
const rtn = parentTn; |
|
|
|
|
const tn = childTn; |
|
|
|
|
await childTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(rtn).where((qb) => { |
|
|
|
@ -1336,7 +1392,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
return obj; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async shuffle({ qb }: { qb: QueryBuilder }): Promise<void> { |
|
|
|
|
public async shuffle({ qb }: { qb: Knex.QueryBuilder }): Promise<void> { |
|
|
|
|
if (this.isMySQL) { |
|
|
|
|
qb.orderByRaw('RAND()'); |
|
|
|
|
} else if (this.isPg || this.isSqlite) { |
|
|
|
@ -1350,7 +1406,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
qb, |
|
|
|
|
columns: _columns, |
|
|
|
|
}: { |
|
|
|
|
qb: QueryBuilder; |
|
|
|
|
qb: Knex.QueryBuilder; |
|
|
|
|
columns?: Column[]; |
|
|
|
|
}): Promise<void> { |
|
|
|
|
const res = {}; |
|
|
|
@ -1569,15 +1625,19 @@ class BaseModelSqlv2 {
|
|
|
|
|
return _wherePk(this.model.primaryKeys, id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public get tnPath() { |
|
|
|
|
private getTnPath(tb: Model) { |
|
|
|
|
const schema = (this.dbDriver as any).searchPath?.(); |
|
|
|
|
const table = |
|
|
|
|
this.isMssql && schema |
|
|
|
|
? this.dbDriver.raw('??.??', [schema, this.model.table_name]) |
|
|
|
|
: this.model.table_name; |
|
|
|
|
this.isMssql && schema |
|
|
|
|
? this.dbDriver.raw('??.??', [schema, tb.table_name]) |
|
|
|
|
: tb.table_name; |
|
|
|
|
return table; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public get tnPath() { |
|
|
|
|
return this.getTnPath(this.model); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get isSqlite() { |
|
|
|
|
return this.clientType === 'sqlite3'; |
|
|
|
|
} |
|
|
|
@ -1766,9 +1826,12 @@ class BaseModelSqlv2 {
|
|
|
|
|
// refer : https://www.sqlite.org/limits.html
|
|
|
|
|
const chunkSize = this.isSqlite ? 10 : _chunkSize; |
|
|
|
|
|
|
|
|
|
const response = await this.dbDriver |
|
|
|
|
.batchInsert(this.model.table_name, insertDatas, chunkSize) |
|
|
|
|
.returning(this.model.primaryKey?.column_name); |
|
|
|
|
const response = (this.isPg || this.isMssql) ? |
|
|
|
|
await this.dbDriver |
|
|
|
|
.batchInsert(this.model.table_name, insertDatas, chunkSize) |
|
|
|
|
.returning(this.model.primaryKey?.column_name) : |
|
|
|
|
await this.dbDriver |
|
|
|
|
.batchInsert(this.model.table_name, insertDatas, chunkSize); |
|
|
|
|
|
|
|
|
|
await this.afterBulkInsert(insertDatas, this.dbDriver, cookie); |
|
|
|
|
|
|
|
|
@ -2230,6 +2293,9 @@ class BaseModelSqlv2 {
|
|
|
|
|
await childTable.getColumns(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
switch (colOptions.type) { |
|
|
|
|
case RelationTypes.MANY_TO_MANY: |
|
|
|
|
{ |
|
|
|
@ -2237,12 +2303,14 @@ class BaseModelSqlv2 {
|
|
|
|
|
const vParentCol = await colOptions.getMMParentColumn(); |
|
|
|
|
const vTable = await colOptions.getMMModel(); |
|
|
|
|
|
|
|
|
|
await this.dbDriver(vTable.table_name).insert({ |
|
|
|
|
[vParentCol.column_name]: this.dbDriver(parentTable.table_name) |
|
|
|
|
const vTn = this.getTnPath(vTable); |
|
|
|
|
|
|
|
|
|
await this.dbDriver(vTn).insert({ |
|
|
|
|
[vParentCol.column_name]: this.dbDriver(parentTn) |
|
|
|
|
.select(parentColumn.column_name) |
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, childId)) |
|
|
|
|
.first(), |
|
|
|
|
[vChildCol.column_name]: this.dbDriver(childTable.table_name) |
|
|
|
|
[vChildCol.column_name]: this.dbDriver(childTn) |
|
|
|
|
.select(childColumn.column_name) |
|
|
|
|
.where(_wherePk(childTable.primaryKeys, rowId)) |
|
|
|
|
.first(), |
|
|
|
@ -2251,10 +2319,10 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
case RelationTypes.HAS_MANY: |
|
|
|
|
{ |
|
|
|
|
await this.dbDriver(childTable.table_name) |
|
|
|
|
await this.dbDriver(childTn) |
|
|
|
|
.update({ |
|
|
|
|
[childColumn.column_name]: this.dbDriver.from( |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(parentColumn.column_name) |
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, rowId)) |
|
|
|
|
.first() |
|
|
|
@ -2266,10 +2334,10 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
case RelationTypes.BELONGS_TO: |
|
|
|
|
{ |
|
|
|
|
await this.dbDriver(childTable.table_name) |
|
|
|
|
await this.dbDriver(childTn) |
|
|
|
|
.update({ |
|
|
|
|
[childColumn.column_name]: this.dbDriver.from( |
|
|
|
|
this.dbDriver(parentTable.table_name) |
|
|
|
|
this.dbDriver(parentTn) |
|
|
|
|
.select(parentColumn.column_name) |
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, childId)) |
|
|
|
|
.first() |
|
|
|
@ -2325,6 +2393,9 @@ class BaseModelSqlv2 {
|
|
|
|
|
await childTable.getColumns(); |
|
|
|
|
await parentTable.getColumns(); |
|
|
|
|
|
|
|
|
|
const childTn = this.getTnPath(childTable); |
|
|
|
|
const parentTn = this.getTnPath(parentTable); |
|
|
|
|
|
|
|
|
|
switch (colOptions.type) { |
|
|
|
|
case RelationTypes.MANY_TO_MANY: |
|
|
|
|
{ |
|
|
|
@ -2332,13 +2403,15 @@ class BaseModelSqlv2 {
|
|
|
|
|
const vParentCol = await colOptions.getMMParentColumn(); |
|
|
|
|
const vTable = await colOptions.getMMModel(); |
|
|
|
|
|
|
|
|
|
await this.dbDriver(vTable.table_name) |
|
|
|
|
const vTn = this.getTnPath(vTable); |
|
|
|
|
|
|
|
|
|
await this.dbDriver(vTn) |
|
|
|
|
.where({ |
|
|
|
|
[vParentCol.column_name]: this.dbDriver(parentTable.table_name) |
|
|
|
|
[vParentCol.column_name]: this.dbDriver(parentTn) |
|
|
|
|
.select(parentColumn.column_name) |
|
|
|
|
.where(_wherePk(parentTable.primaryKeys, childId)) |
|
|
|
|
.first(), |
|
|
|
|
[vChildCol.column_name]: this.dbDriver(childTable.table_name) |
|
|
|
|
[vChildCol.column_name]: this.dbDriver(childTn) |
|
|
|
|
.select(childColumn.column_name) |
|
|
|
|
.where(_wherePk(childTable.primaryKeys, rowId)) |
|
|
|
|
.first(), |
|
|
|
@ -2348,7 +2421,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
case RelationTypes.HAS_MANY: |
|
|
|
|
{ |
|
|
|
|
await this.dbDriver(childTable.table_name) |
|
|
|
|
await this.dbDriver(childTn) |
|
|
|
|
// .where({
|
|
|
|
|
// [childColumn.cn]: this.dbDriver(parentTable.tn)
|
|
|
|
|
// .select(parentColumn.cn)
|
|
|
|
@ -2361,7 +2434,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
case RelationTypes.BELONGS_TO: |
|
|
|
|
{ |
|
|
|
|
await this.dbDriver(childTable.table_name) |
|
|
|
|
await this.dbDriver(childTn) |
|
|
|
|
// .where({
|
|
|
|
|
// [childColumn.cn]: this.dbDriver(parentTable.tn)
|
|
|
|
|
// .select(parentColumn.cn)
|
|
|
|
@ -2629,7 +2702,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
return await qb; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async extractRawQueryAndExec(qb: QueryBuilder) { |
|
|
|
|
private async extractRawQueryAndExec(qb: Knex.QueryBuilder) { |
|
|
|
|
let query = qb.toQuery(); |
|
|
|
|
if (!this.isPg && !this.isMssql) { |
|
|
|
|
query = unsanitize(qb.toQuery()); |
|
|
|
|