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: sqlite date string compatibility parsing only for valid dates #410

Merged
merged 11 commits into from
Jan 22, 2024
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
danjoa marked this conversation as resolved.
Show resolved Hide resolved
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)) {
danjoa marked this conversation as resolved.
Show resolved Hide resolved
const date = new Date(v.val)
if (!Number.isNaN(date.getTime())) {
danjoa marked this conversation as resolved.
Show resolved Hide resolved
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