Browse Source

feat: DATEADD formula for date arithmetics

Signed-off-by: Mert Ersoy <mertmit99@gmail.com>
pull/1415/head
Mert Ersoy 3 years ago
parent
commit
99d2c77c5e
  1. 1
      packages/nc-gui/helpers/formulaList.js
  2. 9
      packages/nocodb/src/lib/dataMapper/lib/sql/formulaQueryBuilderFromString.ts
  3. 24
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mssql.ts
  4. 26
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mysql.ts
  5. 24
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/pg.ts
  6. 24
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/sqlite.ts

1
packages/nc-gui/helpers/formulaList.js

@ -55,6 +55,7 @@ const validations = {
IF: { validation: { args: { min: 2, max: 3 } } }, IF: { validation: { args: { min: 2, max: 3 } } },
SWITCH: { validation: { args: { min: 3 } } }, SWITCH: { validation: { args: { min: 3 } } },
URL: { validation: { args: { rqd: 1 } } }, URL: { validation: { args: { rqd: 1 } } },
DATEADD: { validation: { args: { rqd: 3 } } },
}; };
export default Object.keys(validations); export default Object.keys(validations);

9
packages/nocodb/src/lib/dataMapper/lib/sql/formulaQueryBuilderFromString.ts

@ -90,6 +90,15 @@ export default function formulaQueryBuilder(
prevBinaryOp prevBinaryOp
); );
break; break;
case 'DATEADD':
if(pt.arguments[1].value) {
pt.callee.name = "DATE_ADD";
return fn(pt, a, prevBinaryOp);
} else if(pt.arguments[1].operator == '-') {
pt.callee.name = "DATE_SUB";
return fn(pt, a, prevBinaryOp);
}
break;
default: default:
{ {
const res = mapFunctionName({ const res = mapFunctionName({

24
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mssql.ts

@ -88,6 +88,30 @@ const mssql = {
return args.knex return args.knex
.raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`) .raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
},
DATE_ADD: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN ${args.fn(args.pt.arguments[0])} LIKE '%:%' THEN
FORMAT(DATEADD(${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")},
+${args.fn(args.pt.arguments[1])}, ${args.fn(args.pt.arguments[0])}), 'yyyy-MM-dd HH:mm')
ELSE
FORMAT(DATEADD(${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")},
+${args.fn(args.pt.arguments[1])}, ${args.fn(args.pt.arguments[0])}), 'yyyy-MM-dd')
END${args.colAlias}`
);
},
DATE_SUB: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN ${args.fn(args.pt.arguments[0])} LIKE '%:%' THEN
FORMAT(DATEADD(${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")},
-${args.fn(args.pt.arguments[1])}.argument.value, ${args.fn(args.pt.arguments[0])}), 'yyyy-MM-dd HH:mm')
ELSE
FORMAT(DATEADD(${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")},
-${args.fn(args.pt.arguments[1])}.argument.value, ${args.fn(args.pt.arguments[0])}), 'yyyy-MM-dd')
END${args.colAlias}`
);
} }
}; };

26
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mysql.ts

@ -36,6 +36,32 @@ const mysql2 = {
return args.knex return args.knex
.raw(`CAST(${args.fn(args.pt.arguments[0])} as DOUBLE)${args.colAlias}`) .raw(`CAST(${args.fn(args.pt.arguments[0])} as DOUBLE)${args.colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
},
DATE_ADD: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN ${args.fn(args.pt.arguments[0])} LIKE '%:%' THEN
DATE_FORMAT(DATE_ADD(${args.fn(args.pt.arguments[0])}, INTERVAL
${args.fn(args.pt.arguments[1])} ${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}), '%Y-%m-%d %H:%i')
ELSE
DATE(DATE_ADD(${args.fn(args.pt.arguments[0])}, INTERVAL
${args.fn(args.pt.arguments[1])} ${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}))
END${args.colAlias}`
);
},
DATE_SUB: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN ${args.fn(args.pt.arguments[0])} LIKE '%:%' THEN
DATE_FORMAT(DATE_ADD(${args.fn(args.pt.arguments[0])}, INTERVAL
${args.fn(args.pt.arguments[1])}.argument.value
${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}), '%Y-%m-%d %H:%i')
ELSE
DATE(DATE_ADD(${args.fn(args.pt.arguments[0])}, INTERVAL
${args.fn(args.pt.arguments[1])}.argument.value
${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}))
END${args.colAlias}`
);
} }
}; };

24
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/pg.ts

@ -36,6 +36,30 @@ const pg = {
}` }`
) )
.wrap('(', ')'); .wrap('(', ')');
},
DATE_ADD: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN CAST(${args.fn(args.pt.arguments[0])} AS text) LIKE '%:%' THEN
to_char(${args.fn(args.pt.arguments[0])} + INTERVAL '${args.fn(args.pt.arguments[1])}
${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}', 'YYYY-MM-DD HH24:MI')
ELSE
to_char(${args.fn(args.pt.arguments[0])} + INTERVAL '${args.fn(args.pt.arguments[1])}
${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}', 'YYYY-MM-DD')
END${args.colAlias}`
);
},
DATE_SUB: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN CAST(${args.fn(args.pt.arguments[0])} AS text) LIKE '%:%' THEN
to_char(${args.fn(args.pt.arguments[0])} - INTERVAL '${args.fn(args.pt.arguments[1]).argument.value}
${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}', 'YYYY-MM-DD HH24:MI')
ELSE
to_char(${args.fn(args.pt.arguments[0])} - INTERVAL '${args.fn(args.pt.arguments[1]).argument.value}
${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}', 'YYYY-MM-DD')
END${args.colAlias}`
);
} }
}; };

24
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/sqlite.ts

@ -55,6 +55,30 @@ const sqlite3 = {
return args.knex return args.knex
.raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`) .raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
},
DATE_ADD: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN ${args.fn(args.pt.arguments[0])} LIKE '%:%' THEN
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${args.fn(args.pt.arguments[0])}, 'localtime'),
'+${args.fn(args.pt.arguments[1])} ${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}'))
ELSE
DATE(DATETIME(${args.fn(args.pt.arguments[0])}, 'localtime'),
'+${args.fn(args.pt.arguments[1])} ${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}')
END${args.colAlias}`
);
},
DATE_SUB: (args: MapFnArgs) => {
return args.knex.raw(
`CASE
WHEN ${args.fn(args.pt.arguments[0])} LIKE '%:%' THEN
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${args.fn(args.pt.arguments[0])}, 'localtime'),
'-${args.fn(args.pt.arguments[1]).argument.value} ${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}'))
ELSE
DATE(DATETIME(${args.fn(args.pt.arguments[0])}, 'localtime'),
'-${args.fn(args.pt.arguments[1]).argument.value} ${String(args.fn(args.pt.arguments[2])).replace(/["']/g, "")}')
END${args.colAlias}`
);
} }
}; };

Loading…
Cancel
Save