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: make @cap-js/sqlite work with [email protected] #422

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 = `json${isRoot ? '' : 'b'}_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 : `jsonb_group_array(${obj})`} as _json_ FROM (${sql})`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering about the sqlite specific stuff in the generic db-service, could you elaborate why we need this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has always been in the generic db-service. The original reason for having it here is that there is no SQL specified uniform API to create json values. Therefor taking SQLite makes it easy for other database services to find their equivalent functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks!

}

/**
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
Loading