Skip to content

Commit

Permalink
fix: make @cap-js/sqlite work with [email protected] (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Jan 22, 2024
1 parent 9bbac6e commit 44c0a59
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
23 changes: 8 additions & 15 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class CQN2SQLRenderer {

let cols = SELECT.columns.map(x => {
const name = this.column_name(x)
let col = `'${name}',${this.output_converter4(x.element, this.quote(name))}`
let col = `'$."${name}"',${this.output_converter4(x.element, this.quote(name))}`
if (x.SELECT?.count) {
// Return both the sub select and the count for @odata.count
const qc = cds.ql.clone(x, { columns: [{ func: 'count' }], one: 1, limit: 0, orderBy: 0 })
Expand All @@ -252,21 +252,14 @@ class CQN2SQLRenderer {
return col
}).flat()

// Prevent SQLite from hitting function argument limit of 100
let obj = ''
const isRoot = SELECT.expand === 'root'

if (cols.length < 50) obj = `json_object(${cols.slice(0, 50)})`
else {
const chunks = []
for (let i = 0; i < cols.length; i += 50) {
chunks.push(`json_object(${cols.slice(i, i + 50)})`)
}
// REVISIT: json_merge is a user defined function, bad performance!
obj = `json_merge(${chunks})`
// Prevent SQLite from hitting function argument limit of 100
let obj = "'{}'"
for (let i = 0; i < cols.length; i += 48) {
obj = `jsonb_insert(${obj},${cols.slice(i, i + 48)})`
}


return `SELECT ${SELECT.one || SELECT.expand === 'root' ? obj : `json_group_array(${obj.includes('json_merge') ? `json_insert(${obj})` : obj})`} as _json_ FROM (${sql})`
return `SELECT ${isRoot || SELECT.one ? obj.replace('jsonb', 'json') : `jsonb_group_array(${obj})`} as _json_ FROM (${sql})`
}

/**
Expand Down Expand Up @@ -695,7 +688,7 @@ class CQN2SQLRenderer {
columns = columns.map(c => {
if (q.elements?.[c.name]?.['@cds.extension']) return {
name: 'extensions__',
sql: `json_set(extensions__,${this.string('$."' + c.name + '"')},${c.sql})`,
sql: `jsonb_set(extensions__,${this.string('$."' + c.name + '"')},${c.sql})`,
}
return c
})
Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions sqlite/lib/SQLiteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ class SQLiteService extends SQLService {
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, ','),
)
if (!dbc.memory) dbc.pragma('journal_mode = WAL')
return dbc
},
Expand Down Expand Up @@ -84,13 +81,11 @@ class SQLiteService extends SQLService {
async _run(stmt, binding_params) {
for (let i = 0; i < binding_params.length; i++) {
const val = binding_params[i]
if (val instanceof Readable) {
binding_params[i] = await convStrm[val.type === 'json' ? 'text' : 'buffer'](val)
}
if (Buffer.isBuffer(val)) {
binding_params[i] = Buffer.from(val.base64Slice())
} else if (typeof val === 'object' && val && val.pipe) {
// REVISIT: stream.setEncoding('base64') sometimes misses the last bytes
// if (val.type === 'binary') val.setEncoding('base64')
binding_params[i] = await convStrm.buffer(val)
if (val.type === 'binary') binding_params[i] = Buffer.from(binding_params[i].toString('base64'))
binding_params[i] = Buffer.from(val.toString('base64'))
}
}
return stmt.run(binding_params)
Expand Down Expand Up @@ -254,7 +249,7 @@ class SQLiteService extends SQLService {
} catch (err) {
throw _not_unique(err, 'UNIQUE_CONSTRAINT_VIOLATION') || err
}
}
}
}

// function _not_null (err) {
Expand Down
2 changes: 1 addition & 1 deletion sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"dependencies": {
"@cap-js/db-service": "^1.3.1",
"better-sqlite3": "^9"
"better-sqlite3": "^9.3.0"
},
"peerDependencies": {
"@sap/cds": ">=7"
Expand Down

0 comments on commit 44c0a59

Please sign in to comment.