Skip to content

Commit

Permalink
fix: sqlite date string compatibility parsing only for valid dates (#410
Browse files Browse the repository at this point in the history
)

fixes: #409
  • Loading branch information
BobdenOs authored Jan 22, 2024
1 parent 5015a2c commit 2a8bb2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ class CQN2SQLRenderer {
case 'number': return `${val}` // REVISIT for HANA
case 'object':
if (val === null) return 'NULL'
if (val instanceof Date) return `'${val.toISOString()}'`
if (val instanceof Readable); // go on with default below
if (val instanceof Date) val = val.toJSON() // returns null if invalid
else if (val instanceof Readable); // go on with default below
else if (Buffer.isBuffer(val)); // go on with default below
else if (is_regexp(val)) val = val.source
else val = JSON.stringify(val)
Expand Down
7 changes: 6 additions & 1 deletion sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ class SQLiteService extends SQLService {
val(v) {
if (Buffer.isBuffer(v.val)) v.val = v.val.toString('base64')
// intercept DateTime values and convert to Date objects to compare ISO Strings
else if (/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[Z+-]/.test(v.val)) v.val = new Date(v.val)
else if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d{1,9})?(Z|[+-]\d{2}(:?\d{2})?)$/.test(v.val)) {
const date = new Date(v.val)
if (!Number.isNaN(date.getTime())) {
v.val = date
}
}
return super.val(v)
}

Expand Down
12 changes: 12 additions & 0 deletions test/compliance/SELECT.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ describe('SELECT', () => {
assert.strictEqual(res.length, 3, 'Ensure that all rows are coming back')
})

test('compare with DateTime column', async () => {
const entity = `basic.literals.dateTime`
const dateTime = '1970-02-02T10:09:34Z'
const timestamp = dateTime.slice(0,-1) + '.000Z'
await DELETE.from(entity)
await INSERT({ dateTime }).into(entity)
const dateTimeMatches = await SELECT('dateTime').from(entity).where(`dateTime = `, dateTime)
assert.strictEqual(dateTimeMatches.length, 1, 'Ensure that the dateTime column matches the dateTime value')
const timestampMatches = await SELECT('dateTime').from(entity).where(`dateTime = `, timestamp)
assert.strictEqual(timestampMatches.length, 1, 'Ensure that the dateTime column matches the timestamp value')
})

test.skip('ref select', async () => {
// Currently not possible as cqn4sql does not recognize where.ref.id: 'basic.projection.globals' as an external source
const cqn = {
Expand Down
12 changes: 12 additions & 0 deletions test/scenarios/bookshop/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ describe('Bookshop - Update', () => {
expect(res.data.author_ID).to.be.eq(201)
expect(res.data.descr).to.be.eq('UPDATED')
})

test('Update Book (with timestamp)', async () => {
const descr = `"${new Date().toISOString()}"`
const res = await PUT(
'/admin/Books(201)',
{ descr },
admin,
)
expect(res.status).to.be.eq(200)
expect(res.data.descr).to.be.eq(descr)
})

test('Update array of', async () => {
// create book
const insert = INSERT.into('sap.capire.bookshop.Books').columns(['ID']).values([150])
Expand Down

0 comments on commit 2a8bb2d

Please sign in to comment.