From 19d0825303bf1ef3e357ff27e8f0a629c49e5c16 Mon Sep 17 00:00:00 2001 From: Pranav C <61551451+pranavxc@users.noreply.github.com> Date: Wed, 5 Oct 2022 11:40:00 +0530 Subject: [PATCH] feat(api): oracledb - add basic formula support --- .../lib/sql/functionMappings/oracle.ts | 61 +++++++++++++++++++ .../lib/sql/mapFunctionName.ts | 4 ++ 2 files changed, 65 insertions(+) create mode 100644 packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/oracle.ts diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/oracle.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/oracle.ts new file mode 100644 index 0000000000..31c3bd0ae7 --- /dev/null +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/oracle.ts @@ -0,0 +1,61 @@ +import commonFns from './commonFns'; +import { MapFnArgs } from '../mapFunctionName'; + +const pg = { + ...commonFns, + LEN: 'LENGTH', + MIN: 'LEAST', + MAX: 'GREATEST', + CEILING: 'CEIL', + POWER: 'POWER', + SQRT: 'SQRT', + SEARCH: 'INSTR', + NOW: (args: MapFnArgs) => { + return args.knex.raw(`CURRENT_DATE${args.colAlias}`); + }, + REPEAT: (args: MapFnArgs) => { + const arg1 = args.fn(args.pt.arguments[0]); + const arg2 = args.fn(args.pt.arguments[1]); + return args.knex.raw( + `RPAD(${arg1}, ${arg2}*LENGTH(${arg1}), ${arg1})${args.colAlias}` + ); + }, + // INT(args: MapFnArgs) { + // // todo: correction + // return args.knex.raw( + // `REGEXP_REPLACE(COALESCE(${args.fn( + // args.pt.arguments[0] + // )}::character varying, '0'), '[^0-9]+|\\.[0-9]+' ,'')${args.colAlias}` + // ); + // }, + // MID: 'SUBSTR', + // FLOAT: ({ fn, knex, pt, colAlias }: MapFnArgs) => { + // return knex + // .raw(`CAST(${fn(pt.arguments[0])} as DOUBLE PRECISION)${colAlias}`) + // .wrap('(', ')'); + // }, + // DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { + // return knex.raw( + // `${fn(pt.arguments[0])} + (${fn(pt.arguments[1])} || + // '${String(fn(pt.arguments[2])).replace( + // /["']/g, + // '' + // )}')::interval${colAlias}` + // ); + // }, + // WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { + // // isodow: the day of the week as Monday (1) to Sunday (7) + // // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday + // return knex.raw( + // `(EXTRACT(ISODOW FROM ${ + // pt.arguments[0].type === 'Literal' + // ? `date '${dayjs(fn(pt.arguments[0])).format('YYYY-MM-DD')}'` + // : fn(pt.arguments[0]) + // }) - 1 - ${getWeekdayByText( + // pt?.arguments[1]?.value + // )} % 7 + 7) ::INTEGER % 7 ${colAlias}` + // ); + // }, +}; + +export default pg; diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/mapFunctionName.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/mapFunctionName.ts index 8227e273ec..4989cd5352 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/mapFunctionName.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/mapFunctionName.ts @@ -3,6 +3,7 @@ import mssql from './functionMappings/mssql'; import mysql from './functionMappings/mysql'; import pg from './functionMappings/pg'; import sqlite from './functionMappings/sqlite'; +import oracle from './functionMappings/oracle'; import { Knex } from 'knex'; export interface MapFnArgs { @@ -37,6 +38,9 @@ const mapFunctionName = (args: MapFnArgs): any => { case 'sqlite3': val = sqlite[name] || name; break; + case 'oracledb': + val = oracle[name] || name; + break; } if (typeof val === 'function') {