Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: date functions with null value #347

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ class SQLiteService extends SQLService {
const isTime = /^\d{1,2}:\d{1,2}:\d{1,2}$/
const hasTimezone = /([+-]\d{1,2}:?\d{0,2}|Z)$/
const toDate = (d, allowTime = false) => {
if (d === null) return null
const date = new Date(allowTime && isTime.test(d) ? `1970-01-01T${d}Z` : hasTimezone.test(d) ? d : d + 'Z')
if (Number.isNaN(date.getTime())) throw new Error(`Value does not contain a valid ${allowTime ? 'time' : 'date'} "${d}"`)
return date
}
dbc.function('year', deterministic, d => toDate(d).getUTCFullYear())
dbc.function('month', deterministic, d => toDate(d).getUTCMonth() + 1)
dbc.function('day', deterministic, d => toDate(d).getUTCDate())
dbc.function('hour', deterministic, d => toDate(d, true).getUTCHours())
dbc.function('minute', deterministic, d => toDate(d, true).getUTCMinutes())
dbc.function('second', deterministic, d => toDate(d, true).getUTCSeconds())
dbc.function('year', deterministic, d => d === null ? null : toDate(d).getUTCFullYear())
dbc.function('month', deterministic, d => d === null ? null : toDate(d).getUTCMonth() + 1)
dbc.function('day', deterministic, d => d === null ? null : toDate(d).getUTCDate())
dbc.function('hour', deterministic, d => d === null ? null : toDate(d, true).getUTCHours())
dbc.function('minute', deterministic, d => d === null ? null : toDate(d, true).getUTCMinutes())
dbc.function('second', deterministic, d => d === null ? null : toDate(d, true).getUTCSeconds())

dbc.function('json_merge', { varargs: true, deterministic: true }, (...args) =>
args.join('').replace(/}{/g, ','),
Expand Down
8 changes: 8 additions & 0 deletions test/scenarios/bookshop/funcs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ describe('Bookshop - Functions', () => {
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(1)
})

test('date function with null value', async () => {
const { result } = await SELECT.one(`day(null) as result`)
.from('sap.capire.bookshop.Books')

expect(result).to.be.null
})

test.skip('fractionalseconds', async () => {
// REVISIT: ERROR: Feature is not supported: Method "fractionalseconds" in $filter or $orderby query options
const res = await GET(
Expand Down