Browse Source

fix(nocodb): formula function builder return statement correction

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5392/head
Pranav C 2 years ago
parent
commit
7cac920274
  1. 88
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mssql.ts
  2. 74
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

88
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mssql.ts

@ -14,9 +14,7 @@ const mssql = {
for (const [i, arg] of Object.entries(args.pt.arguments)) {
if (+i === args.pt.arguments.length - 1) {
query += args.knex
.raw(
`\n\tElse ${(await await args.fn(arg)).builder.builder.toQuery()}`
)
.raw(`\n\tElse ${(await args.fn(arg)).builder.builder.toQuery()}`)
.toQuery();
} else {
query += args.knex
@ -37,7 +35,7 @@ const mssql = {
.toQuery();
}
}
return args.knex.raw(`Case ${query}\n End${args.colAlias}`);
return { builder: args.knex.raw(`Case ${query}\n End${args.colAlias}`) };
},
MAX: async (args: MapFnArgs) => {
if (args.pt.arguments.length === 1) {
@ -66,10 +64,11 @@ const mssql = {
}
}
return args.knex.raw(`Case ${query}\n End${args.colAlias}`);
return { builder: args.knex.raw(`Case ${query}\n End${args.colAlias}`) };
},
LOG: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`LOG(${(
await Promise.all(
args.pt.arguments
@ -77,7 +76,8 @@ const mssql = {
.map(async (ar) => (await args.fn(ar)).builder.toQuery())
)
).join(',')})${args.colAlias}`
);
),
};
},
MOD: (pt) => {
Object.assign(pt, {
@ -96,52 +96,71 @@ const mssql = {
args.pt.arguments[1] = temp;
},
INT: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`CASE WHEN ISNUMERIC(${(
await args.fn(args.pt.arguments[0])
).builder.toQuery()}) = 1 THEN FLOOR(${(
await args.fn(args.pt.arguments[0])
).builder.toQuery()}) ELSE 0 END${args.colAlias}`
);
),
};
},
MID: 'SUBSTR',
FLOAT: (args: MapFnArgs) => {
return args.knex
.raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`)
.wrap('(', ')');
FLOAT: async (args: MapFnArgs) => {
return {
builder: args.knex
.raw(
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as FLOAT)${
args.colAlias
}`
)
.wrap('(', ')'),
};
},
DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const dateIN = (await fn(pt.arguments[1])).builder;
return knex.raw(
return {
builder: knex.raw(
`CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])}, ${fn(
pt.arguments[0]
)}), 'yyyy-MM-dd HH:mm')
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN
FORMAT(DATEADD(${String((await fn(pt.arguments[2])).builder).replace(
/["']/g,
''
)},
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${
(await fn(pt.arguments[0])).builder
}), 'yyyy-MM-dd HH:mm')
ELSE
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])}, ${fn(
FORMAT(DATEADD(${String((await fn(pt.arguments[2])).builder).replace(
/["']/g,
''
)},
${dateIN > 0 ? '+' : ''}${(await fn(pt.arguments[1])).builder}, ${fn(
pt.arguments[0]
)}), 'yyyy-MM-dd')
END${colAlias}`
);
),
};
},
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const datetime_expr1 = fn(pt.arguments[0]);
const datetime_expr2 = fn(pt.arguments[1]);
const datetime_expr1 = (await fn(pt.arguments[0])).builder;
const datetime_expr2 = (await fn(pt.arguments[1])).builder;
const rawUnit = pt.arguments[2]
? (await fn(pt.arguments[2])).builder.bindings[0]
: 'seconds';
const unit = convertUnits(rawUnit, 'mssql');
return knex.raw(
return {
builder: knex.raw(
`DATEDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}`
);
),
};
},
WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// DATEPART(WEEKDAY, DATE): sunday = 1, monday = 2, ..., saturday = 7
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
return knex.raw(
return {
builder: knex.raw(
`(DATEPART(WEEKDAY, ${
pt.arguments[0].type === 'Literal'
? `'${dayjs((await fn(pt.arguments[0])).builder).format(
@ -151,10 +170,12 @@ const mssql = {
}) - 2 - ${getWeekdayByText(
pt?.arguments[1]?.value
)} % 7 + 7) % 7 ${colAlias}`
);
),
};
},
AND: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`CASE WHEN ${args.knex
.raw(
`${(
@ -167,10 +188,12 @@ const mssql = {
)
.wrap('(', ')')
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}`
);
),
};
},
OR: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`CASE WHEN ${args.knex
.raw(
`${(
@ -183,7 +206,8 @@ const mssql = {
)
.wrap('(', ')')
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}`
);
),
};
},
};

74
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

@ -12,74 +12,87 @@ const sqlite3 = {
...commonFns,
LEN: 'LENGTH',
async CEILING(args) {
return args.knex.raw(
return {
builder: args.knex.raw(
`round(${(await args.fn(args.pt.arguments[0])).builder} + 0.5)${
args.colAlias
}`
);
),
};
},
async FLOOR(args) {
return args.knex.raw(
return {
builder: args.knex.raw(
`round(${(await args.fn(args.pt.arguments[0])).builder} - 0.5)${
args.colAlias
}`
);
),
};
},
MOD: async (args: MapFnArgs) => {
return (
await args.fn({
return args.fn({
type: 'BinaryExpression',
operator: '%',
left: args.pt.arguments[0],
right: args.pt.arguments[1],
})
).builder;
});
},
async REPEAT(args: MapFnArgs) {
return args.knex.raw(
return {
builder: args.knex.raw(
`replace(printf('%.' || ${
(await args.fn(args.pt.arguments[1])).builder
} || 'c', '/'),'/',${(await args.fn(args.pt.arguments[0])).builder})${
args.colAlias
}`
);
),
};
},
NOW: 'DATE',
SEARCH: 'INSTR',
async INT(args: MapFnArgs) {
return args.knex.raw(
return {
builder: args.knex.raw(
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as INTEGER)${
args.colAlias
}`
);
),
};
},
LEFT: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${
(await args.fn(args.pt.arguments[1])).builder
})${args.colAlias}`
);
),
};
},
RIGHT: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},-(${
(await args.fn(args.pt.arguments[1])).builder
}))${args.colAlias}`
);
),
};
},
MID: 'SUBSTR',
FLOAT: async (args: MapFnArgs) => {
return args.knex
return {
builder: args.knex
.raw(
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as FLOAT)${
args.colAlias
}`
)
.wrap('(', ')');
.wrap('(', ')'),
};
},
DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const dateIN = (await fn(pt.arguments[1])).builder;
return knex.raw(
return {
builder: knex.raw(
`CASE
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn(
@ -100,7 +113,8 @@ const sqlite3 = {
''
)}')
END${colAlias}`
);
),
};
},
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
let datetime_expr1 = (await fn(pt.arguments[0])).builder;
@ -174,12 +188,13 @@ const sqlite3 = {
default:
sql = '';
}
return knex.raw(`${sql} ${colAlias}`);
return { builder: knex.raw(`${sql} ${colAlias}`) };
},
WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// strftime('%w', date) - day of week 0 - 6 with Sunday == 0
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
return knex.raw(
return {
builder: knex.raw(
`(strftime('%w', ${
pt.arguments[0].type === 'Literal'
? `'${dayjs((await fn(pt.arguments[0])).builder).format(
@ -189,10 +204,12 @@ const sqlite3 = {
}) - 1 - ${getWeekdayByText(
pt?.arguments[1]?.value
)} % 7 + 7) % 7 ${colAlias}`
);
),
};
},
AND: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`CASE WHEN ${args.knex
.raw(
`${(
@ -205,10 +222,12 @@ const sqlite3 = {
)
.wrap('(', ')')
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}`
);
),
};
},
OR: async (args: MapFnArgs) => {
return args.knex.raw(
return {
builder: args.knex.raw(
`CASE WHEN ${args.knex
.raw(
`${(
@ -221,7 +240,8 @@ const sqlite3 = {
)
.wrap('(', ')')
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}`
);
),
};
},
};

Loading…
Cancel
Save