Browse Source

chore(nocodb): lint

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5392/head
Pranav C 2 years ago
parent
commit
2539aba5d9
  1. 54
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts
  2. 22
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/pg.ts

54
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts

@ -18,25 +18,27 @@ const mysql2 = {
INT: async (args: MapFnArgs) => { INT: async (args: MapFnArgs) => {
return { return {
builder: args.knex.raw( builder: args.knex.raw(
`CAST(${(await args.fn(args.pt.arguments[0])).builder} as SIGNED)${args.colAlias}`, `CAST(${(await args.fn(args.pt.arguments[0])).builder} as SIGNED)${
args.colAlias
}`
), ),
}; };
}, },
LEFT: async (args: MapFnArgs) => { LEFT: async (args: MapFnArgs) => {
return { return {
builder: args.knex.raw( builder: args.knex.raw(
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${(await args.fn( `SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${
args.pt.arguments[1], (await args.fn(args.pt.arguments[1])).builder
)).builder})${args.colAlias}`, })${args.colAlias}`
), ),
}; };
}, },
RIGHT: async (args: MapFnArgs) => { RIGHT: async (args: MapFnArgs) => {
return { return {
builder: args.knex.raw( builder: args.knex.raw(
`SUBSTR(${(await args.fn(args.pt.arguments[0])).builder}, -(${(await args.fn( `SUBSTR(${(await args.fn(args.pt.arguments[0])).builder}, -(${
args.pt.arguments[1], (await args.fn(args.pt.arguments[1])).builder
)).builder}))${args.colAlias}`, }))${args.colAlias}`
), ),
}; };
}, },
@ -45,9 +47,9 @@ const mysql2 = {
return { return {
builder: args.knex builder: args.knex
.raw( .raw(
`CAST(CAST(${(await args.fn(args.pt.arguments[0])).builder} as CHAR) AS DOUBLE)${ `CAST(CAST(${
args.colAlias (await args.fn(args.pt.arguments[0])).builder
}`, } as CHAR) AS DOUBLE)${args.colAlias}`
) )
.wrap('(', ')'), .wrap('(', ')'),
}; };
@ -58,17 +60,15 @@ const mysql2 = {
`CASE `CASE
WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN
DATE_FORMAT(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL DATE_FORMAT(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL
${(await fn(pt.arguments[1])).builder} ${String((await fn(pt.arguments[2])).builder).replace( ${(await fn(pt.arguments[1])).builder} ${String(
/["']/g, (await fn(pt.arguments[2])).builder
'', ).replace(/["']/g, '')}), '%Y-%m-%d %H:%i')
)}), '%Y-%m-%d %H:%i')
ELSE ELSE
DATE(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL DATE(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL
${(await fn(pt.arguments[1])).builder} ${String((await fn(pt.arguments[2])).builder).replace( ${(await fn(pt.arguments[1])).builder} ${String(
/["']/g, (await fn(pt.arguments[2])).builder
'', ).replace(/["']/g, '')}))
)})) END${colAlias}`
END${colAlias}`,
), ),
}; };
}, },
@ -77,7 +77,9 @@ const mysql2 = {
const datetime_expr2 = (await fn(pt.arguments[1])).builder; const datetime_expr2 = (await fn(pt.arguments[1])).builder;
const unit = convertUnits( const unit = convertUnits(
pt.arguments[2] ? (await fn(pt.arguments[2])).builder.bindings[0] : 'seconds', pt.arguments[2]
? (await fn(pt.arguments[2])).builder.bindings[0]
: 'seconds',
'mysql' 'mysql'
); );
@ -86,13 +88,13 @@ const mysql2 = {
// hence change from MICROSECOND to millisecond manually // hence change from MICROSECOND to millisecond manually
return { return {
builder: knex.raw( builder: knex.raw(
`TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) div 1000 ${colAlias}`, `TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) div 1000 ${colAlias}`
), ),
}; };
} }
return { return {
builder: knex.raw( builder: knex.raw(
`TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}`, `TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}`
), ),
}; };
}, },
@ -102,11 +104,13 @@ const mysql2 = {
builder: knex.raw( builder: knex.raw(
`(WEEKDAY(${ `(WEEKDAY(${
pt.arguments[0].type === 'Literal' pt.arguments[0].type === 'Literal'
? `'${dayjs((await fn(pt.arguments[0])).builder).format('YYYY-MM-DD')}'` ? `'${dayjs((await fn(pt.arguments[0])).builder).format(
'YYYY-MM-DD'
)}'`
: (await fn(pt.arguments[0])).builder : (await fn(pt.arguments[0])).builder
}) - ${getWeekdayByText( }) - ${getWeekdayByText(
pt?.arguments[1]?.value, pt?.arguments[1]?.value
)} % 7 + 7) % 7 ${colAlias}`, )} % 7 + 7) % 7 ${colAlias}`
), ),
}; };
}, },

22
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/pg.ts

@ -37,7 +37,11 @@ const pg = {
FLOAT: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { FLOAT: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return { return {
builder: knex builder: knex
.raw(`CAST(${(await fn(pt.arguments[0])).builder} as DOUBLE PRECISION)${colAlias}`) .raw(
`CAST(${
(await fn(pt.arguments[0])).builder
} as DOUBLE PRECISION)${colAlias}`
)
.wrap('(', ')'), .wrap('(', ')'),
}; };
}, },
@ -53,7 +57,9 @@ const pg = {
DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return { return {
builder: knex.raw( builder: knex.raw(
`${(await fn(pt.arguments[0])).builder} + (${(await fn(pt.arguments[1])).builder} || `${(await fn(pt.arguments[0])).builder} + (${
(await fn(pt.arguments[1])).builder
} ||
'${String((await fn(pt.arguments[2])).builder).replace( '${String((await fn(pt.arguments[2])).builder).replace(
/["']/g, /["']/g,
'' ''
@ -133,11 +139,13 @@ const pg = {
builder: args.knex.raw( builder: args.knex.raw(
`CASE WHEN ${args.knex `CASE WHEN ${args.knex
.raw( .raw(
`${(await Promise.all(args.pt.arguments `${(
.map(async (ar) => await Promise.all(
args.pt.arguments.map(async (ar) =>
(await args.fn(ar, '', 'AND')).builder.toQuery() (await args.fn(ar, '', 'AND')).builder.toQuery()
))) )
.join(' AND ')}` )
).join(' AND ')}`
) )
.wrap('(', ')') .wrap('(', ')')
.toQuery()} THEN TRUE ELSE FALSE END ${args.colAlias}` .toQuery()} THEN TRUE ELSE FALSE END ${args.colAlias}`
@ -160,7 +168,7 @@ const pg = {
), ),
}; };
}, },
SUBSTR:async ({ fn, knex, pt, colAlias }: MapFnArgs) => { SUBSTR: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const str = (await fn(pt.arguments[0])).builder; const str = (await fn(pt.arguments[0])).builder;
const positionFrom = (await fn(pt.arguments[1] ?? 1)).builder; const positionFrom = (await fn(pt.arguments[1] ?? 1)).builder;
const numberOfCharacters = (await fn(pt.arguments[2] ?? '')).builder; const numberOfCharacters = (await fn(pt.arguments[2] ?? '')).builder;

Loading…
Cancel
Save