-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcqn2sql.js
1168 lines (1035 loc) · 39.5 KB
/
cqn2sql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const cds = require('@sap/cds')
const cds_infer = require('./infer')
const cqn4sql = require('./cqn4sql')
const _simple_queries = cds.env.features.sql_simple_queries
const _strict_booleans = _simple_queries < 2
const { Readable } = require('stream')
const DEBUG = cds.debug('sql|sqlite')
const LOG_SQL = cds.log('sql')
const LOG_SQLITE = cds.log('sqlite')
class CQN2SQLRenderer {
/**
* Creates a new CQN2SQL instance for processing a query
* @constructor
* @param {import('@sap/cds/apis/services').ContextProperties} context the cds.context of the request
*/
constructor(srv) {
this.context = srv?.context || cds.context // Using srv.context is required due to stakeholders doing unmanaged txs without cds.context being set
this.class = new.target // for IntelliSense
this.class._init() // is a noop for subsequent calls
this.model = srv?.model
// Overwrite smart quoting
if (cds.env.sql.names === 'quoted') {
this.class.prototype.name = (name, query) => {
const e = name.id || name
return (query?.target || this.model?.definitions[e])?.['@cds.persistence.name'] || e
}
this.class.prototype.quote = (s) => `"${String(s).replace(/"/g, '""')}"`
}
}
BINARY_TYPES = {
'cds.Binary': 1,
'cds.LargeBinary': 1,
'cds.hana.BINARY': 1,
}
static _add_mixins(aspect, mixins) {
const fqn = this.name + aspect
const types = cds.builtin.types
for (let each in mixins) {
const def = types[each]
if (!def) continue
const value = mixins[each]
if (value?.get) Object.defineProperty(def, fqn, { get: value.get })
else Object.defineProperty(def, fqn, { value })
}
return fqn
}
/**
* Initializes the class one first creation to link types to data converters
*/
static _init() {
this._localized = this._add_mixins(':localized', this.localized)
this._convertInput = this._add_mixins(':convertInput', this.InputConverters)
this._convertOutput = this._add_mixins(':convertOutput', this.OutputConverters)
this._sqlType = this._add_mixins(':sqlType', this.TypeMap)
// Have all-uppercase all-lowercase, and capitalized keywords to speed up lookups
for (let each in this.ReservedWords) {
// ORDER
this.ReservedWords[each[0] + each.slice(1).toLowerCase()] = 1 // Order
this.ReservedWords[each.toLowerCase()] = 1 // order
}
this._init = () => { } // makes this a noop for subsequent calls
}
/**
* Renders incoming query into SQL and generates binding values
* @param {import('./infer/cqn').Query} q CQN query to be rendered
* @param {unknown[]|undefined} vars Values to be used for params
* @returns {CQN2SQLRenderer|unknown}
*/
render(q, vars) {
const kind = q.kind || Object.keys(q)[0] // SELECT, INSERT, ...
/**
* @type {string} the rendered SQL string
*/
this.sql = '' // to have it as first property for debugging
/** @type {unknown[]} */
this.values = [] // prepare values, filled in by subroutines
this[kind]((this.cqn = q)) // actual sql rendering happens here
if (vars?.length && !this.values?.length) this.values = vars
if (vars && Object.keys(vars).length && !this.values?.length) this.values = vars
const sanitize_values = process.env.NODE_ENV === 'production' && cds.env.log.sanitize_values !== false
if (DEBUG && (LOG_SQL._debug || LOG_SQLITE._debug)) {
let values = sanitize_values && (this.entries || this.values?.length > 0) ? ['***'] : this.entries || this.values || []
if (values && !Array.isArray(values)) {
values = [values]
}
DEBUG(this.sql, ...values)
}
return this
}
/**
* Links the incoming query with the current service model
* @param {import('./infer/cqn').Query} q
* @returns {import('./infer/cqn').Query}
*/
infer(q) {
return q.target ? q : cds_infer(q)
}
cqn4sql(q) {
return cqn4sql(q, this.model)
}
// CREATE Statements ------------------------------------------------
/**
* Renders a CREATE query into generic SQL
* @param {import('./infer/cqn').CREATE} q
*/
CREATE(q) {
let { target } = q
let query = target?.query || q.CREATE.as
if (!target || target._unresolved) {
const entity = q.CREATE.entity
target = typeof entity === 'string' ? { name: entity } : q.CREATE.entity
}
const name = this.name(target.name, q)
// Don't allow place holders inside views
delete this.values
this.sql =
!query || target['@cds.persistence.table']
? `CREATE TABLE ${this.quote(name)} ( ${this.CREATE_elements(target.elements)} )`
: `CREATE VIEW ${this.quote(name)} AS ${this.SELECT(this.cqn4sql(query))}`
this.values = []
return
}
/**
* Renders a column clause for the given elements
* @param {import('./infer/cqn').elements} elements
* @returns {string} SQL
*/
CREATE_elements(elements) {
let sql = ''
let keys = ''
for (let e in elements) {
const definition = elements[e]
if (definition.isAssociation) continue
if (definition.key) keys = `${keys}, ${this.quote(definition.name)}`
const s = this.CREATE_element(definition)
if (s) sql += `, ${s}`
}
return `${sql.slice(2)}${keys && `, PRIMARY KEY(${keys.slice(2)})`}`
}
/**
* Renders a column definition for the given element
* @param {import('./infer/cqn').element} element
* @returns {string} SQL
*/
CREATE_element(element) {
const type = this.type4(element)
if (type) return this.quote(element.name) + ' ' + type
}
/**
* Renders the SQL type definition for the given element
* @param {import('./infer/cqn').element} element
* @returns {string}
*/
type4(element) {
if (!element._type) element = cds.builtin.types[element.type] || element
const fn = element[this.class._sqlType]
return (
fn?.(element) || element._type?.replace('cds.', '').toUpperCase() || cds.error`Unsupported type: ${element.type}`
)
}
/** @callback converter */
/** @type {Object<string,import('@sap/cds/apis/csn').Definition>} */
static TypeMap = {
// Utilizing cds.linked inheritance
UUID: () => `NVARCHAR(36)`,
String: e => `NVARCHAR(${e.length || 5000})`,
Binary: e => `VARBINARY(${e.length || 5000})`,
UInt8: () => 'TINYINT',
Int16: () => 'SMALLINT',
Int32: () => 'INT',
Int64: () => 'BIGINT',
Integer: () => 'INT',
Integer64: () => 'BIGINT',
LargeString: () => 'NCLOB',
LargeBinary: () => 'BLOB',
Association: () => false,
Composition: () => false,
array: () => 'NCLOB',
Map: () => 'NCLOB',
// HANA types
'cds.hana.TINYINT': () => 'TINYINT',
'cds.hana.REAL': () => 'REAL',
'cds.hana.CHAR': e => `CHAR(${e.length || 1})`,
'cds.hana.ST_POINT': () => 'ST_POINT',
'cds.hana.ST_GEOMETRY': () => 'ST_GEOMETRY',
}
// DROP Statements ------------------------------------------------
/**
* Renders a DROP query into generic SQL
* @param {import('./infer/cqn').DROP} q
*/
DROP(q) {
const { target } = q
const isView = target?.query || target?.projection || q.DROP.view
const name = target?.name || q.DROP.table?.ref?.[0] || q.DROP.view?.ref?.[0]
return (this.sql = `DROP ${isView ? 'VIEW' : 'TABLE'} IF EXISTS ${this.quote(this.name(name, q))}`)
}
// SELECT Statements ------------------------------------------------
/**
* Renders a SELECT statement into generic SQL
* @param {import('./infer/cqn').SELECT} q
*/
SELECT(q) {
let { from, expand, where, groupBy, having, orderBy, limit, one, distinct, localized, forUpdate, forShareLock } =
q.SELECT
if (from?.join && !q.SELECT.columns) {
throw new Error('CQN query using joins must specify the selected columns.')
}
// REVISIT: When selecting from an entity that is not in the model the from.where are not normalized (as cqn4sql is skipped)
if (!where && from?.ref?.length === 1 && from.ref[0]?.where) where = from.ref[0]?.where
const columns = this.SELECT_columns(q)
let sql = `SELECT`
if (distinct) sql += ` DISTINCT`
if (!_empty(columns)) sql += ` ${columns}`
if (!_empty(from)) sql += ` FROM ${this.from(from, q)}`
else sql += this.from_dummy()
if (!_empty(where)) sql += ` WHERE ${this.where(where)}`
if (!_empty(groupBy)) sql += ` GROUP BY ${this.groupBy(groupBy)}`
if (!_empty(having)) sql += ` HAVING ${this.having(having)}`
if (!_empty(orderBy)) sql += ` ORDER BY ${this.orderBy(orderBy, localized)}`
if (one) limit = Object.assign({}, limit, { rows: { val: 1 } })
if (limit) sql += ` LIMIT ${this.limit(limit)}`
if (forUpdate) sql += ` ${this.forUpdate(forUpdate)}`
else if (forShareLock) sql += ` ${this.forShareLock(forShareLock)}`
// Expand cannot work without an inferred query
if (expand) {
if ('elements' in q) sql = this.SELECT_expand(q, sql)
else cds.error`Query was not inferred and includes expand. For which the metadata is missing.`
}
return (this.sql = sql)
}
/**
* Renders a column clause into generic SQL
* @param {import('./infer/cqn').SELECT} param0
* @returns {string} SQL
*/
SELECT_columns(q) {
return (q.SELECT.columns ?? ['*']).map(x => this.column_expr(x, q))
}
/**
* Renders a JSON select around the provided SQL statement
* @param {import('./infer/cqn').SELECT} param0
* @param {string} sql
* @returns {string} SQL
*/
SELECT_expand(q, sql) {
if (!('elements' in q)) return sql
const SELECT = q.SELECT
if (!SELECT.columns) return sql
const isRoot = SELECT.expand === 'root'
const isSimple = _simple_queries &&
isRoot && // Simple queries are only allowed to have a root
!ObjectKeys(q.elements).some(e =>
_strict_booleans && q.elements[e].type === 'cds.Boolean' || // REVISIT: Booleans require json for sqlite
q.elements[e].isAssociation || // Indicates columns contains an expand
q.elements[e].$assocExpand || // REVISIT: sometimes associations are structs
q.elements[e].items // Array types require to be inlined with a json result
)
let cols = SELECT.columns.map(isSimple
? x => {
const name = this.column_name(x)
const escaped = `${name.replace(/"/g, '""')}`
let col = `${this.output_converter4(x.element, this.quote(name))} AS "${escaped}"`
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 })
return [col, `${this.expr(qc)} AS "${escaped}@odata.count"`]
}
return col
}
: x => {
const name = this.column_name(x)
const escaped = `${name.replace(/"/g, '""')}`
let col = `'$."${escaped}"',${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 })
return [col, `'$."${escaped}@odata.count"',${this.expr(qc)}`]
}
return col
}).flat()
if (isSimple) return `SELECT ${cols} FROM (${sql})`
// 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 ${isRoot || SELECT.one ? obj.replace('jsonb', 'json') : `jsonb_group_array(${obj})`} as _json_ FROM (${sql})`
}
/**
* Renders a SELECT column expression into generic SQL
* @param {import('./infer/cqn').col} x
* @returns {string} SQL
*/
column_expr(x, q) {
if (x === '*') return '*'
let sql = x.param !== true && typeof x.val === 'number' ? this.expr({ param: false, __proto__: x }) : this.expr(x)
let alias = this.column_alias4(x, q)
if (alias) sql += ' as ' + this.quote(alias)
return sql
}
/**
* Extracts the column alias from a SELECT column expression
* @param {import('./infer/cqn').col} x
* @returns {string}
*/
column_alias4(x) {
return typeof x.as === 'string' ? x.as : x.func || x.val
}
/**
* Renders a FROM clause into generic SQL
* @param {import('./infer/cqn').source} from
* @returns {string} SQL
*/
from(from, q) {
const { ref, as } = from
const _aliased = as ? s => s + ` as ${this.quote(as)}` : s => s
if (ref) {
let z = ref[0]
if (z.args) {
return _aliased(`${this.quote(this.name(z, q))}${this.from_args(z.args)}`)
}
return _aliased(this.quote(this.name(z, q)))
}
if (from.SELECT) return _aliased(`(${this.SELECT(from)})`)
if (from.join) return `${this.from(from.args[0])} ${from.join} JOIN ${this.from(from.args[1])}${from.on ? ` ON ${this.where(from.on)}` : ''}`
}
/**
* Renders a FROM clause for when the query does not have a target
* @returns {string} SQL
*/
from_dummy() {
return ''
}
/**
* Renders a FROM clause into generic SQL
* @param {import('./infer/cqn').ref['ref'][0]['args']} args
* @returns {string} SQL
*/
from_args(args) {
args
cds.error`Parameterized views are not supported by ${this.constructor.name}`
}
/**
* Renders a WHERE clause into generic SQL
* @param {import('./infer/cqn').predicate} xpr
* @returns {string} SQL
*/
where(xpr) {
return this.xpr({ xpr })
}
/**
* Renders a HAVING clause into generic SQL
* @param {import('./infer/cqn').predicate} xpr
* @returns {string} SQL
*/
having(xpr) {
return this.xpr({ xpr })
}
/**
* Renders a groupBy clause into generic SQL
* @param {import('./infer/cqn').expr[]} clause
* @returns {string[] | string} SQL
*/
groupBy(clause) {
return clause.map(c => this.expr(c))
}
/**
* Renders an orderBy clause into generic SQL
* @param {import('./infer/cqn').ordering_term[]} orderBy
* @param {boolean | undefined} localized
* @returns {string[] | string} SQL
*/
orderBy(orderBy, localized) {
return orderBy.map(
localized
? c =>
this.expr(c) +
(c.element?.[this.class._localized] ? ' COLLATE NOCASE' : '') +
(c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC' : ' ASC')
: c => this.expr(c) + (c.sort?.toLowerCase() === 'desc' || c.sort === -1 ? ' DESC' : ' ASC'),
)
}
/**
* Renders an limit clause into generic SQL
* @param {import('./infer/cqn').limit} param0
* @returns {string} SQL
* @throws {Error} When no rows are defined
*/
limit({ rows, offset }) {
if (!rows) throw new Error('Rows parameter is missing in SELECT.limit(rows, offset)')
return !offset ? this.val(rows) : `${this.val(rows)} OFFSET ${this.val(offset)}`
}
/**
* Renders an forUpdate clause into generic SQL
* @param {import('./infer/cqn').SELECT["SELECT"]["forUpdate"]} update
* @returns {string} SQL
*/
forUpdate(update) {
const { wait, of } = update
let sql = 'FOR UPDATE'
if (!_empty(of)) sql += ` OF ${of.map(x => this.expr(x)).join(', ')}`
if (typeof wait === 'number') sql += ` WAIT ${wait}`
return sql
}
/**
* Renders an forShareLock clause into generic SQL
* @param {import('./infer/cqn').SELECT["SELECT"]["forShareLock"]} update
* @returns {string} SQL
*/
forShareLock(lock) {
const { wait, of } = lock
let sql = 'FOR SHARE LOCK'
if (!_empty(of)) sql += ` OF ${of.map(x => this.expr(x)).join(', ')}`
if (typeof wait === 'number') sql += ` WAIT ${wait}`
return sql
}
// INSERT Statements ------------------------------------------------
/**
* Renders an INSERT query into generic SQL
* @param {import('./infer/cqn').INSERT} q
* @returns {string} SQL
*/
INSERT(q) {
const { INSERT } = q
return INSERT.entries
? this.INSERT_entries(q)
: INSERT.rows
? this.INSERT_rows(q)
: INSERT.values
? this.INSERT_values(q)
: INSERT.as
? this.INSERT_select(q)
: cds.error`Missing .entries, .rows, or .values in ${q}`
}
/**
* Renders an INSERT query with entries property
* @param {import('./infer/cqn').INSERT} q
* @returns {string} SQL
*/
INSERT_entries(q) {
const { INSERT } = q
const elements = q.elements || q.target?.elements
if (!elements && !INSERT.entries?.length) {
return // REVISIT: mtx sends an insert statement without entries and no reference entity
}
const columns = elements
? ObjectKeys(elements).filter(c => c in elements && !elements[c].virtual && !elements[c].value && !elements[c].isAssociation)
: ObjectKeys(INSERT.entries[0])
/** @type {string[]} */
this.columns = columns
const alias = INSERT.into.as
const entity = this.name(q.target?.name || INSERT.into.ref[0], q)
if (!elements) {
this.entries = INSERT.entries.map(e => columns.map(c => e[c]))
const param = this.param.bind(this, { ref: ['?'] })
return (this.sql = `INSERT INTO ${this.quote(entity)}${alias ? ' as ' + this.quote(alias) : ''} (${this.columns.map(c => this.quote(c))}) VALUES (${columns.map(param)})`)
}
// Include this.values for placeholders
/** @type {unknown[][]} */
this.entries = []
if (INSERT.entries[0] instanceof Readable) {
INSERT.entries[0].type = 'json'
this.entries = [[...this.values, INSERT.entries[0]]]
} else {
const stream = Readable.from(this.INSERT_entries_stream(INSERT.entries), { objectMode: false })
stream.type = 'json'
stream._raw = INSERT.entries
this.entries = [[...this.values, stream]]
}
const extractions = this._managed = this.managed(columns.map(c => ({ name: c })), elements)
return (this.sql = `INSERT INTO ${this.quote(entity)}${alias ? ' as ' + this.quote(alias) : ''} (${this.columns.map(c => this.quote(c))
}) SELECT ${extractions.map(c => c.insert)} FROM json_each(?)`)
}
async *INSERT_entries_stream(entries, binaryEncoding = 'base64') {
const elements = this.cqn.target?.elements || {}
const bufferLimit = 65536 // 1 << 16
let buffer = '['
let sep = ''
for (const row of entries) {
buffer += `${sep}{`
if (!sep) sep = ','
let sepsub = ''
for (const key in row) {
let val = row[key]
if (val === undefined) continue
const keyJSON = `${sepsub}${JSON.stringify(key)}:`
if (!sepsub) sepsub = ','
if (val instanceof Readable) {
buffer += `${keyJSON}"`
// TODO: double check that it works
val.setEncoding(binaryEncoding)
for await (const chunk of val) {
buffer += chunk
if (buffer.length > bufferLimit) {
yield buffer
buffer = ''
}
}
buffer += '"'
} else {
if (val != null && elements[key]?.type in this.BINARY_TYPES) {
val = Buffer.from(val, 'base64').toString(binaryEncoding)
}
buffer += `${keyJSON}${JSON.stringify(val)}`
}
}
buffer += '}'
if (buffer.length > bufferLimit) {
yield buffer
buffer = ''
}
}
buffer += ']'
yield buffer
}
async *INSERT_rows_stream(entries, binaryEncoding = 'base64') {
const elements = this.cqn.target?.elements || {}
const bufferLimit = 65536 // 1 << 16
let buffer = '['
let sep = ''
for (const row of entries) {
buffer += `${sep}[`
if (!sep) sep = ','
let sepsub = ''
for (let key = 0; key < row.length; key++) {
let val = row[key]
if (val instanceof Readable) {
buffer += `${sepsub}"`
// TODO: double check that it works
val.setEncoding(binaryEncoding)
for await (const chunk of val) {
buffer += chunk
if (buffer.length > bufferLimit) {
yield buffer
buffer = ''
}
}
buffer += '"'
} else {
if (val != null && elements[this.columns[key]]?.type in this.BINARY_TYPES) {
val = Buffer.from(val, 'base64').toString(binaryEncoding)
}
buffer += `${sepsub}${val === undefined ? 'null' : JSON.stringify(val)}`
}
if (!sepsub) sepsub = ','
}
buffer += ']'
if (buffer.length > bufferLimit) {
yield buffer
buffer = ''
}
}
buffer += ']'
yield buffer
}
/**
* Renders an INSERT query with rows property
* @param {import('./infer/cqn').INSERT} q
* @returns {string} SQL
*/
INSERT_rows(q) {
const { INSERT } = q
const entity = this.name(q.target?.name || INSERT.into.ref[0], q)
const alias = INSERT.into.as
const elements = q.elements || q.target?.elements
const columns = this.columns = INSERT.columns || cds.error`Cannot insert rows without columns or elements`
if (!elements) {
this.entries = INSERT.rows
const param = this.param.bind(this, { ref: ['?'] })
return (this.sql = `INSERT INTO ${this.quote(entity)}${alias ? ' as ' + this.quote(alias) : ''} (${this.columns.map(c => this.quote(c))}) VALUES (${columns.map(param)})`)
}
if (INSERT.rows[0] instanceof Readable) {
INSERT.rows[0].type = 'json'
this.entries = [[...this.values, INSERT.rows[0]]]
} else {
const stream = Readable.from(this.INSERT_rows_stream(INSERT.rows), { objectMode: false })
stream.type = 'json'
stream._raw = INSERT.rows
this.entries = [[...this.values, stream]]
}
const extraction = (this._managed = this.managed(columns.map(c => ({ name: c })), elements))
.slice(0, columns.length)
.map(c => c.converter(c.extract))
return (this.sql = `INSERT INTO ${this.quote(entity)}${alias ? ' as ' + this.quote(alias) : ''} (${this.columns.map(c => this.quote(c))
}) SELECT ${extraction} FROM json_each(?)`)
}
/**
* Renders an INSERT query with values property
* @param {import('./infer/cqn').INSERT} q
* @returns {string} SQL
*/
INSERT_values(q) {
let { columns, values } = q.INSERT
return this.render({ __proto__: q, INSERT: { __proto__: q.INSERT, columns, rows: [values] } })
}
/**
* Renders an INSERT query from SELECT query
* @param {import('./infer/cqn').INSERT} q
* @returns {string} SQL
*/
INSERT_select(q) {
const { INSERT } = q
const entity = this.name(q.target.name, q)
const alias = INSERT.into.as
const elements = q.elements || q.target?.elements || {}
const columns = (this.columns = (INSERT.columns || ObjectKeys(elements)).filter(
c => c in elements && !elements[c].virtual && !elements[c].isAssociation,
))
this.sql = `INSERT INTO ${this.quote(entity)}${alias ? ' as ' + this.quote(alias) : ''} (${columns.map(c => this.quote(c))}) ${this.SELECT(
this.cqn4sql(INSERT.as),
)}`
this.entries = [this.values]
return this.sql
}
/**
* Wraps the provided SQL expression for output processing
* @param {import('./infer/cqn').element} element
* @param {string} expr
* @returns {string} SQL
*/
output_converter4(element, expr) {
const fn = element?.[this.class._convertOutput]
return fn?.(expr, element) || expr
}
/** @type {import('./converters').Converters} */
static InputConverters = {} // subclasses to override
/** @type {import('./converters').Converters} */
static OutputConverters = {} // subclasses to override
static localized = { String: { get() { return this['@cds.collate'] !== false } }, UUID: false }
// UPSERT Statements ------------------------------------------------
/**
* Renders an UPSERT query into generic SQL
* @param {import('./infer/cqn').UPDATE} q
* @returns {string} SQL
*/
UPSERT(q) {
const { UPSERT } = q
let sql = this.INSERT({ __proto__: q, INSERT: UPSERT })
if (!q.target?.keys) return sql
const keys = []
for (const k of ObjectKeys(q.target?.keys)) {
const element = q.target.keys[k]
if (element.isAssociation || element.virtual) continue
keys.push(k)
}
const elements = q.target?.elements || {}
// temporal data
for (const k of ObjectKeys(elements)) {
if (elements[k]['@cds.valid.from']) keys.push(k)
}
const keyCompare = keys
.map(k => `NEW.${this.quote(k)}=OLD.${this.quote(k)}`)
.join(' AND ')
const columns = this.columns // this.columns is computed as part of this.INSERT
const managed = this._managed.slice(0, columns.length)
const extractkeys = managed
.filter(c => keys.includes(c.name))
.map(c => `${c.onInsert || c.sql} as ${this.quote(c.name)}`)
const entity = this.name(q.target?.name || UPSERT.into.ref[0], q)
sql = `SELECT ${managed.map(c => c.upsert
.replace(/value->/g, '"$$$$value$$$$"->')
.replace(/json_type\(value,/g, 'json_type("$$$$value$$$$",'))
} FROM (SELECT value as "$$value$$", ${extractkeys} from json_each(?)) as NEW LEFT JOIN ${this.quote(entity)} AS OLD ON ${keyCompare}`
const updateColumns = columns.filter(c => {
if (keys.includes(c)) return false //> keys go into ON CONFLICT clause
let e = elements[c]
if (!e) return true //> pass through to native SQL columns not in CDS model
if (e.virtual) return true //> skip virtual elements
if (e.value) return true //> skip calculated elements
// if (e.isAssociation) return true //> this breaks a a test in @sap/cds -> need to follow up how to correctly handle deep upserts
else return true
}).map(c => `${this.quote(c)} = excluded.${this.quote(c)}`)
return (this.sql = `INSERT INTO ${this.quote(entity)} (${columns.map(c => this.quote(c))}) ${sql
} WHERE TRUE ON CONFLICT(${keys.map(c => this.quote(c))}) DO ${updateColumns.length ? `UPDATE SET ${updateColumns}` : 'NOTHING'}`)
}
// UPDATE Statements ------------------------------------------------
/**
* Renders an UPDATE query into generic SQL
* @param {import('./infer/cqn').UPDATE} q
* @returns {string} SQL
*/
UPDATE(q) {
const { entity, with: _with, data, where } = q.UPDATE
const elements = q.target?.elements
let sql = `UPDATE ${this.quote(this.name(entity.ref?.[0] || entity, q))}`
if (entity.as) sql += ` AS ${this.quote(entity.as)}`
let columns = []
if (data) _add(data, val => this.val({ val }))
if (_with) _add(_with, x => this.expr(x))
function _add(data, sql4) {
for (let c in data) {
const columnExistsInDatabase =
elements && c in elements && !elements[c].virtual && !elements[c].isAssociation && !elements[c].value
if (!elements || columnExistsInDatabase) {
columns.push({ name: c, sql: sql4(data[c]) })
}
}
}
const extraction = this.managed(columns, elements)
.filter((c, i) => columns[i] || c.onUpdate)
.map((c, i) => `${this.quote(c.name)}=${!columns[i] ? c.onUpdate : c.sql}`)
sql += ` SET ${extraction}`
if (where) sql += ` WHERE ${this.where(where)}`
return (this.sql = sql)
}
// DELETE Statements ------------------------------------------------
/**
* Renders a DELETE query into generic SQL
* @param {import('./infer/cqn').DELETE} param0
* @returns {string} SQL
*/
DELETE(q) {
const { DELETE: { from, where } } = q
let sql = `DELETE FROM ${this.from(from, q)}`
if (where) sql += ` WHERE ${this.where(where)}`
return (this.sql = sql)
}
// Expression Clauses ---------------------------------------------
/**
* Renders an expression object into generic SQL
* @param {import('./infer/cqn').expr} x
* @returns {string} SQL
* @throws {Error} When an unknown un supported expression is provided
*/
expr(x) {
const wrap = x.cast ? sql => `cast(${sql} as ${this.type4(x.cast)})` : sql => sql
if (typeof x === 'string') throw cds.error`Unsupported expr: ${x}`
if (x.param) return wrap(this.param(x))
if ('ref' in x) return wrap(this.ref(x))
if ('val' in x) return wrap(this.val(x))
if ('func' in x) return wrap(this.func(x))
if ('xpr' in x) return wrap(this.xpr(x))
if ('list' in x) return wrap(this.list(x))
if ('SELECT' in x) return wrap(`(${this.SELECT(x)})`)
else throw cds.error`Unsupported expr: ${x}`
}
/**
* Renders an list of expression objects into generic SQL
* @param {import('./infer/cqn').xpr} param0
* @returns {string} SQL
*/
xpr({ xpr }) {
return xpr
.map((x, i) => {
if (x in { LIKE: 1, like: 1 } && is_regexp(xpr[i + 1]?.val)) return this.operator('regexp')
if (typeof x === 'string') return this.operator(x, i, xpr)
if (x.xpr) return `(${this.xpr(x)})`
else return this.expr(x)
})
.join(' ')
}
/**
* Renders an operation into generic SQL
* @param {string} x The current operator string
* @param {Number} i Current index of the operator inside the xpr
* @param {import('./infer/cqn').predicate[]} xpr The parent xpr in which the operator is used
* @returns {string} The correct operator string
*/
operator(x, i, xpr) {
// Translate = to IS NULL for rhs operand being NULL literal
if (x === '=') return xpr[i + 1]?.val === null
? _inline_null(xpr[i + 1]) || 'is'
: '='
// Translate == to IS NOT NULL for rhs operand being NULL literal, otherwise ...
// Translate == to IS NOT DISTINCT FROM, unless both operands cannot be NULL
if (x === '==') return xpr[i + 1]?.val === null
? _inline_null(xpr[i + 1]) || 'is'
: _not_null(i - 1) && _not_null(i + 1)
? '='
: this.is_not_distinct_from_
// Translate != to IS NULL for rhs operand being NULL literal, otherwise...
// Translate != to IS DISTINCT FROM, unless both operands cannot be NULL
if (x === '!=') return xpr[i + 1]?.val === null
? _inline_null(xpr[i + 1]) || 'is not'
: _not_null(i - 1) && _not_null(i + 1)
? '<>'
: this.is_distinct_from_
else return x
function _inline_null(n) {
n.param = false
}
/** Checks if the operand at xpr[i+-1] can be NULL. @returns true if not */
function _not_null(i) {
const operand = xpr[i]
if (!operand) return false
if (operand.val != null) return true // non-null values are not null
let element = operand.element
if (!element) return false
if (element.key) return true // primary keys usually should not be null
if (element.notNull) return true // not null elements cannot be null
}
}
get is_distinct_from_() { return 'is distinct from' }
get is_not_distinct_from_() { return 'is not distinct from' }
/**
* Renders an argument place holder into the SQL for prepared statements
* @param {import('./infer/cqn').ref} param0
* @returns {string} SQL
* @throws {Error} When an unsupported ref definition is provided
*/
param({ ref }) {
if (ref.length > 1) throw cds.error`Unsupported nested ref parameter: ${ref}`
return ref[0] === '?' ? '?' : `:${ref}`
}
/**
* Renders a ref into generic SQL
* @param {import('./infer/cqn').ref} param0
* @returns {string} SQL
*/
ref({ ref }) {
switch (ref[0]) {
case '$now': return this.func({ func: 'session_context', args: [{ val: '$now', param: false }] }) // REVISIT: why do we need param: false here?
case '$user': return this.func({ func: 'session_context', args: [{ val: '$user.' + ref[1] || 'id', param: false }] }) // REVISIT: same here?
default: return ref.map(r => this.quote(r)).join('.')
}
}
/**
* Renders a value into the correct SQL syntax or a placeholder for a prepared statement
* @param {import('./infer/cqn').val} param0
* @returns {string} SQL
*/
val({ val, param }) {
switch (typeof val) {
case 'function': throw new Error('Function values not supported.')
case 'undefined': val = null
break
case 'boolean': return `${val}`
case 'object':
if (val !== null) {
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)
}
}
if (!this.values || param === false) {
switch (typeof val) {
case 'string': return this.string(val)
case 'object': return 'NULL'
default:
return `${val}`
}
}
this.values.push(val)
return '?'
}
static Functions = require('./cql-functions')
/**
* Renders a function call into mapped SQL definitions from the Functions definition
* @param {import('./infer/cqn').func} param0
* @returns {string} SQL
*/
func({ func, args, xpr }) {
const wrap = e => (e === '*' ? e : { __proto__: e, toString: (x = e) => this.expr(x) })
args = args || []
if (Array.isArray(args)) {
args = args.map(wrap)
} else if (typeof args === 'object') {
const org = args
const wrapped = {
toString: () => {
const ret = []
for (const prop in org) {
ret.push(`${this.quote(prop)} => ${wrapped[prop]}`)
}
return ret.join(',')
}
}
for (const prop in args) {
wrapped[prop] = wrap(args[prop])
}
args = wrapped
} else {
cds.error`Invalid arguments provided for function '${func}' (${args})`
}
const fn = this.class.Functions[func]?.apply(this.class.Functions, args) || `${func}(${args})`
if (xpr) return `${fn} ${this.xpr({ xpr })}`
return fn
}
/**
* Renders a list into generic SQL
* @param {import('./infer/cqn').list} param0
* @returns {string} SQL
*/
list({ list }) {
return `(${list.map(e => this.expr(e))})`