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

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

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

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

@ -33,9 +33,10 @@ export default async function ({
case RelationTypes.HAS_MANY: case RelationTypes.HAS_MANY:
return { return {
builder: knex( builder: knex(
`${baseModelSqlv2.getTnPath( knex.raw(`?? as ??`, [
childModel?.table_name, baseModelSqlv2.getTnPath(childModel?.table_name),
)} as ${refTableAlias}`, refTableAlias,
]),
) )
[columnOptions.rollup_function as string]?.( [columnOptions.rollup_function as string]?.(
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), knex.ref(`${refTableAlias}.${rollupColumn.column_name}`),
@ -57,9 +58,10 @@ export default async function ({
return { return {
builder: knex( builder: knex(
`${baseModelSqlv2.getTnPath( knex.raw(`?? as ??`, [
parentModel?.table_name, baseModelSqlv2.getTnPath(parentModel?.table_name),
)} as ${refTableAlias}`, refTableAlias,
]),
) )
[columnOptions.rollup_function as string]?.( [columnOptions.rollup_function as string]?.(
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), 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(); await parentModel.getColumns();
selectQb = knex( selectQb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`, knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
).where( ).where(
`${alias}.${parentColumn.column_name}`, `${alias}.${parentColumn.column_name}`,
knex.raw(`??`, [ knex.raw(`??`, [
@ -102,7 +105,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns(); await parentModel.getColumns();
selectQb = knex( selectQb = knex(
`${baseModelSqlv2.getTnPath(childModel.table_name)} as ${alias}`, knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(childModel.table_name),
alias,
]),
).where( ).where(
`${alias}.${childColumn.column_name}`, `${alias}.${childColumn.column_name}`,
knex.raw(`??`, [ knex.raw(`??`, [
@ -124,7 +130,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns(); await parentModel.getColumns();
selectQb = knex( selectQb = knex(
`${baseModelSqlv2.getTnPath(parentModel.table_name)} as ${alias}`, knex.raw(`?? as ??`, [
baseModelSqlv2.getTnPath(parentModel.table_name),
alias,
]),
); );
const mmTableAlias = getAlias(); const mmTableAlias = getAlias();
@ -193,9 +202,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns(); await parentModel.getColumns();
selectQb.join( selectQb.join(
`${baseModelSqlv2.getTnPath( knex.raw(`?? as ??`, [
parentModel.table_name, baseModelSqlv2.getTnPath(parentModel.table_name),
)} as ${nestedAlias}`, nestedAlias,
]),
`${nestedAlias}.${parentColumn.column_name}`, `${nestedAlias}.${parentColumn.column_name}`,
`${prevAlias}.${childColumn.column_name}`, `${prevAlias}.${childColumn.column_name}`,
); );
@ -209,9 +219,10 @@ export default async function generateLookupSelectQuery({
await parentModel.getColumns(); await parentModel.getColumns();
selectQb.join( selectQb.join(
`${baseModelSqlv2.getTnPath( knex.raw(`?? as ??`, [
childModel.table_name, baseModelSqlv2.getTnPath(childModel.table_name),
)} as ${nestedAlias}`, nestedAlias,
]),
`${nestedAlias}.${childColumn.column_name}`, `${nestedAlias}.${childColumn.column_name}`,
`${prevAlias}.${parentColumn.column_name}`, `${prevAlias}.${parentColumn.column_name}`,
); );

Loading…
Cancel
Save