Browse Source

fix: use knex raw to avoid special character issue

pull/7436/head
mertmit 10 months ago
parent
commit
26c42905b8
  1. 52
      packages/nocodb/src/db/conditionV2.ts
  2. 96
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  3. 14
      packages/nocodb/src/db/genRollupSelectv2.ts
  4. 29
      packages/nocodb/src/db/generateLookupSelectQuery.ts

52
packages/nocodb/src/db/conditionV2.ts

@ -1087,7 +1087,10 @@ async function generateLookupCondition(
if (relationColumnOptions.type === RelationTypes.HAS_MANY) {
qb = knex(
`${baseModelSqlv2.getTnPath(childModel.table_name)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
alias,
]),
);
qb.select(`${alias}.${childColumn.column_name}`);
@ -1115,7 +1118,10 @@ async function generateLookupCondition(
};
} else if (relationColumnOptions.type === RelationTypes.BELONGS_TO) {
qb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
);
qb.select(`${alias}.${parentColumn.column_name}`);
@ -1151,12 +1157,18 @@ async function generateLookupCondition(
const childAlias = `__nc${aliasCount.count++}`;
qb = knex(`${baseModelSqlv2.getTnPath(mmModel.table_name)} as ${alias}`)
qb = knex(
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(mmModel.table_name),
alias,
]),
)
.select(`${alias}.${mmChildColumn.column_name}`)
.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${childAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
childAlias,
]),
`${alias}.${mmParentColumn.column_name}`,
`${childAlias}.${parentColumn.column_name}`,
);
@ -1225,9 +1237,10 @@ async function nestedConditionJoin(
case RelationTypes.HAS_MANY:
{
qb.join(
`${baseModelSqlv2.getTnPath(
childModel.table_name,
)} as ${relAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
relAlias,
]),
`${alias}.${parentColumn.column_name}`,
`${relAlias}.${childColumn.column_name}`,
);
@ -1236,9 +1249,10 @@ async function nestedConditionJoin(
case RelationTypes.BELONGS_TO:
{
qb.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${relAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
relAlias,
]),
`${alias}.${childColumn.column_name}`,
`${relAlias}.${parentColumn.column_name}`,
);
@ -1253,15 +1267,17 @@ async function nestedConditionJoin(
const assocAlias = `__nc${aliasCount.count++}`;
qb.join(
`${baseModelSqlv2.getTnPath(
mmModel.table_name,
)} as ${assocAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(mmModel.table_name),
assocAlias,
]),
`${assocAlias}.${mmChildColumn.column_name}`,
`${alias}.${childColumn.column_name}`,
).join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${relAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
relAlias,
]),
`${relAlias}.${parentColumn.column_name}`,
`${assocAlias}.${mmParentColumn.column_name}`,
);

96
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -159,9 +159,10 @@ async function _formulaQueryBuilder(
switch (relation.type) {
case 'bt':
selectQb = knex(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
).where(
`${alias}.${parentColumn.column_name}`,
knex.raw(`??`, [
@ -175,9 +176,10 @@ async function _formulaQueryBuilder(
case 'hm':
isMany = true;
selectQb = knex(
`${baseModelSqlv2.getTnPath(
childModel.table_name,
)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
alias,
]),
).where(
`${alias}.${childColumn.column_name}`,
knex.raw(`??`, [
@ -197,14 +199,16 @@ async function _formulaQueryBuilder(
const assocAlias = `__nc${aliasCount++}`;
selectQb = knex(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
)
.join(
`${baseModelSqlv2.getTnPath(
mmModel.table_name,
)} as ${assocAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(mmModel.table_name),
assocAlias,
]),
`${assocAlias}.${mmParentColumn.column_name}`,
`${alias}.${parentColumn.column_name}`,
)
@ -245,9 +249,10 @@ async function _formulaQueryBuilder(
case 'bt':
{
selectQb.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
nestedAlias,
]),
`${prevAlias}.${childColumn.column_name}`,
`${nestedAlias}.${parentColumn.column_name}`,
);
@ -257,9 +262,10 @@ async function _formulaQueryBuilder(
{
isMany = true;
selectQb.join(
`${baseModelSqlv2.getTnPath(
childModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
nestedAlias,
]),
`${prevAlias}.${parentColumn.column_name}`,
`${nestedAlias}.${childColumn.column_name}`,
);
@ -275,16 +281,18 @@ async function _formulaQueryBuilder(
selectQb
.join(
`${baseModelSqlv2.getTnPath(
mmModel.table_name,
)} as ${assocAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(mmModel.table_name),
assocAlias,
]),
`${assocAlias}.${mmChildColumn.column_name}`,
`${prevAlias}.${childColumn.column_name}`,
)
.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
nestedAlias,
]),
`${nestedAlias}.${parentColumn.column_name}`,
`${assocAlias}.${mmParentColumn.column_name}`,
);
@ -353,9 +361,10 @@ async function _formulaQueryBuilder(
case 'bt':
{
selectQb.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
nestedAlias,
]),
`${alias}.${childColumn.column_name}`,
`${nestedAlias}.${parentColumn.column_name}`,
);
@ -369,9 +378,10 @@ async function _formulaQueryBuilder(
{
isMany = true;
selectQb.join(
`${baseModelSqlv2.getTnPath(
childModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
nestedAlias,
]),
`${alias}.${parentColumn.column_name}`,
`${nestedAlias}.${childColumn.column_name}`,
);
@ -393,16 +403,18 @@ async function _formulaQueryBuilder(
selectQb
.join(
`${baseModelSqlv2.getTnPath(
mmModel.table_name,
)} as ${assocAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(mmModel.table_name),
assocAlias,
]),
`${assocAlias}.${mmChildColumn.column_name}`,
`${alias}.${childColumn.column_name}`,
)
.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
nestedAlias,
]),
`${nestedAlias}.${parentColumn.column_name}`,
`${assocAlias}.${mmParentColumn.column_name}`,
);
@ -414,9 +426,10 @@ async function _formulaQueryBuilder(
}
selectQb.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
nestedAlias,
]),
`${nestedAlias}.${parentColumn.column_name}`,
`${prevAlias}.${childColumn.column_name}`,
);
@ -598,7 +611,10 @@ async function _formulaQueryBuilder(
const mmChildColumn = await relation.getMMChildColumn();
const qb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
)
.join(
`${baseModelSqlv2.getTnPath(mmModel.table_name)}`,

14
packages/nocodb/src/db/genRollupSelectv2.ts

@ -33,9 +33,10 @@ export default async function ({
case RelationTypes.HAS_MANY:
return {
builder: knex(
`${baseModelSqlv2.getTnPath(
childModel?.table_name,
)} as ${refTableAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel?.table_name),
refTableAlias,
]),
)
[columnOptions.rollup_function as string]?.(
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`),
@ -57,9 +58,10 @@ export default async function ({
return {
builder: knex(
`${baseModelSqlv2.getTnPath(
parentModel?.table_name,
)} as ${refTableAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel?.table_name),
refTableAlias,
]),
)
[columnOptions.rollup_function as string]?.(
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`),

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

@ -80,7 +80,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns();
selectQb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
).where(
`${alias}.${parentColumn.column_name}`,
knex.raw(`??`, [
@ -102,7 +105,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns();
selectQb = knex(
`${baseModelSqlv2.getTnPath(childModel.table_name)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
alias,
]),
).where(
`${alias}.${childColumn.column_name}`,
knex.raw(`??`, [
@ -124,7 +130,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns();
selectQb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
);
const mmTableAlias = getAlias();
@ -193,9 +202,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns();
selectQb.join(
`${baseModelSqlv2.getTnPath(
parentModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
nestedAlias,
]),
`${nestedAlias}.${parentColumn.column_name}`,
`${prevAlias}.${childColumn.column_name}`,
);
@ -209,9 +219,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns();
selectQb.join(
`${baseModelSqlv2.getTnPath(
childModel.table_name,
)} as ${nestedAlias}`,
knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
nestedAlias,
]),
`${nestedAlias}.${childColumn.column_name}`,
`${prevAlias}.${parentColumn.column_name}`,
);

Loading…
Cancel
Save