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

feat: Query level SQL caching #977

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion db-service/lib/InsertResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = class InsertResult {
get affectedRows() {
const { INSERT: _ } = this.query
if (_.as) return (super.affectedRows = this.affectedRows4(this.results[0] || this.results))
else return (super.affectedRows = _.entries?.length || _.rows?.length || this.results.length || 1)
else return (super.affectedRows = _.entries?.length || _.rows?.length || this.results?.reduce((l, c) => l + this.affectedRows4(c), 0) || 1)
}

/**
Expand Down
29 changes: 17 additions & 12 deletions db-service/lib/SQLService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const cds = require('@sap/cds'),
DEBUG = cds.debug('sql|db')
const { Readable } = require('stream')
const { resolveView, getDBTable, getTransition } = require('@sap/cds/libx/_runtime/common/utils/resolveView')
const { getDBTable, getTransition } = require('@sap/cds/libx/_runtime/common/utils/resolveView')
const DatabaseService = require('./common/DatabaseService')
const cqn4sql = require('./cqn4sql')
const cacheSymbol = Symbol('sql cache')

const BINARY_TYPES = {
'cds.Binary': 1,
Expand Down Expand Up @@ -117,7 +118,7 @@ class SQLService extends DatabaseService {
* Handler for SELECT
* @type {Handler}
*/
async onSELECT({ query, data }) {
async onSELECT({ query, data = query.params }) {
// REVISIT: for custom joins, infer is called twice, which is bad
// --> make cds.infer properly work with custom joins and remove this
if (!query.target) {
Expand Down Expand Up @@ -166,7 +167,7 @@ class SQLService extends DatabaseService {
* Handler for INSERT
* @type {Handler}
*/
async onINSERT({ query, data }) {
async onINSERT({ query, data = query.params }) {
const { sql, entries, cqn } = this.cqn2sql(query, data)
if (!sql) return // Do nothing when there is nothing to be done // REVISIT: fix within mtxs
const ps = await this.prepare(sql)
Expand All @@ -178,7 +179,7 @@ class SQLService extends DatabaseService {
* Handler for UPSERT
* @type {Handler}
*/
async onUPSERT({ query, data }) {
async onUPSERT({ query, data = query.params }) {
const { sql, entries } = this.cqn2sql(query, data)
if (!sql) return // Do nothing when there is nothing to be done // REVISIT: When does this happen?
const ps = await this.prepare(sql)
Expand Down Expand Up @@ -206,7 +207,7 @@ class SQLService extends DatabaseService {
* Handler for CREATE, DROP, UPDATE, DELETE, with simple CQN
* @type {Handler}
*/
async onSIMPLE({ query, data }) {
async onSIMPLE({ query, data = query.params }) {
const { sql, values } = this.cqn2sql(query, data)
let ps = await this.prepare(sql)
return (await ps.run(values)).changes
Expand Down Expand Up @@ -377,15 +378,19 @@ class SQLService extends DatabaseService {
* @returns {typeof SQLService.CQN2SQL}
*/
cqn2sql(query, values) {
let q = this.cqn4sql(query)
let kind = q.kind || Object.keys(q)[0]
if (kind in { INSERT: 1, DELETE: 1, UPSERT: 1, UPDATE: 1 }) {
q = resolveView(q, this.model, this) // REVISIT: before resolveView was called on flat cqn obtained from cqn4sql -> is it correct to call on original q instead?
let target = q[kind]._transitions?.[0].target
if (target) q.target = target // REVISIT: Why isn't that done in resolveView?
if (Object.hasOwn(query, cacheSymbol)) {
const cache = query[cacheSymbol]
if (!cache.params) return cache
const ret = { __proto__: cache }
ret.updateParams(values)
return ret
}

let q = this.cqn4sql(query)
let cqn2sql = new this.class.CQN2SQL(this)
return cqn2sql.render(q, values)
const ret = cqn2sql.render(q, values)
Object.defineProperty(query, cacheSymbol, { value: ret })
return ret
}

/**
Expand Down
Loading
Loading