diff --git a/db-service/lib/cql-functions.js b/db-service/lib/cql-functions.js index 0165278e5..8596cfe83 100644 --- a/db-service/lib/cql-functions.js +++ b/db-service/lib/cql-functions.js @@ -271,6 +271,11 @@ const HANAFunctions = { // https://help.sap.com/docs/SAP_HANA_PLATFORM/4fe29514fd584807ac9f2a04f6754767/f12b86a6284c4aeeb449e57eb5dd3ebd.html // Time functions + /** + * Generates SQL statement that calculates the difference in 100nanoseconds between two timestamps + * @returns {string} + */ + current_utctimestamp: StandardFunctions.current_timestamp, /** * Generates SQL statement that calculates the difference in 100nanoseconds between two timestamps * @param {string} x left timestamp diff --git a/hana/lib/cql-functions.js b/hana/lib/cql-functions.js index b78516553..c86a433d4 100644 --- a/hana/lib/cql-functions.js +++ b/hana/lib/cql-functions.js @@ -45,6 +45,7 @@ const StandardFunctions = { current_date: () => 'current_utcdate', current_time: () => 'current_utctime', current_timestamp: () => 'current_utctimestamp', + current_utctimestamp: x => x ? `current_utctimestamp(${x})` : 'current_utctimestamp', fractionalseconds: x => `(TO_DECIMAL(SECOND(${x}),5,3) - TO_INTEGER(SECOND(${x})))`, } diff --git a/hana/test/hana-functions.test.js b/hana/test/hana-functions.test.js new file mode 100644 index 000000000..09ab09dd1 --- /dev/null +++ b/hana/test/hana-functions.test.js @@ -0,0 +1,49 @@ +const cds = require('../../test/cds') + +// HXE does not allow args +describe.skip('HANA native functions', () => { + const { expect } = cds.test(__dirname, 'fuzzy.cds') + + describe('current_timestamp', () => { + test('no arguments', async () => { + const cqn = { SELECT: { + one: true, + from: {ref: ['DUMMY']}, + columns: [{func: 'current_utctimestamp', as: 'NO'}] + }} + + const res = await cds.run(cqn) + + expect(res.NO.match(/\.(\d\d\d)0000/)).not.to.be.null // default 3 + }) + + test('0 skips ms precision', async () => { + const cqn = { SELECT: { + one: true, + from: {ref: ['DUMMY']}, + columns: [ + {func: 'current_utctimestamp', as: 'NO'}, + {func: 'current_utctimestamp', args: [{val: 0}], as: 'P0'}] + }} + + const res = await cds.run(cqn) + + expect(res.P0.match(/\.0000000/)).not.to.be.null + }) + + test('arbitrary values', async () => { + const cqn = { SELECT: { + one: true, + from: {ref: ['DUMMY']}, + columns: [ + {func: 'current_utctimestamp', args: [{val: 3}], as: 'P3'}, + {func: 'current_utctimestamp', args: [{val: 7}], as: 'P7'}] + }} + + const res = await cds.run(cqn) + + expect(res.P3.match(/\.(\d\d\d)0000/)).not.to.be.null + expect(res.P7.match(/\.(\d\d\d\d\d\d\d)/)).not.to.be.null + }) + }) +}) \ No newline at end of file diff --git a/test/compliance/functions.test.js b/test/compliance/functions.test.js index 09cee81de..8044a42c5 100644 --- a/test/compliance/functions.test.js +++ b/test/compliance/functions.test.js @@ -342,8 +342,26 @@ describe('functions', () => { }) }) describe('CURRENT_UTCTIMESTAMP', () => { - test.skip('missing', () => { - throw new Error('not supported') + test('no arguments', async () => { + const cqn = { SELECT: { + one: true, + from: {ref: ['basic.literals.string']}, + columns: [{func: 'current_utctimestamp', as: 'NO'}] + }} + + const res = await cds.run(cqn) + + const SQLiteService = require('../../sqlite') + const PgService = require('../../postgres') + + if (cds.db instanceof SQLiteService) { + expect(res.NO.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/)).to.exist + } else if (cds.db instanceof PgService) { + expect(res.NO.match(/\.(\d\d\d\d\d\d)\+00:00/)).not.to.be.null // precision 6 + } else { //HANA + expect(res.NO.match(/\.(\d\d\d)0000/)).not.to.be.null // default precision 3 + } + }) }) describe('DAYNAME', () => {