/
query-generator.js
1278 lines (1131 loc) · 45.7 KB
/
query-generator.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
// Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved
'use strict';
const Utils = require('../../utils');
const DataTypes = require('../../data-types');
const AbstractQueryGenerator = require('../abstract/query-generator');
const _ = require('lodash');
const util = require('util');
const Transaction = require('../../transaction');
/**
* list of reserved words in Oracle DB 21c
* source: https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-7B72E154-677A-4342-A1EA-C74C1EA928E6
*
* @private
*/
const ORACLE_RESERVED_WORDS = ['ACCESS', 'ADD', 'ALL', 'ALTER', 'AND', 'ANY', 'ARRAYLEN', 'AS', 'ASC', 'AUDIT', 'BETWEEN', 'BY', 'CHAR', 'CHECK', 'CLUSTER', 'COLUMN', 'COMMENT', 'COMPRESS', 'CONNECT', 'CREATE', 'CURRENT', 'DATE', 'DECIMAL', 'DEFAULT', 'DELETE', 'DESC', 'DISTINCT', 'DROP', 'ELSE', 'EXCLUSIVE', 'EXISTS', 'FILE', 'FLOAT', 'FOR', 'FROM', 'GRANT', 'GROUP', 'HAVING', 'IDENTIFIED', 'IMMEDIATE', 'IN', 'INCREMENT', 'INDEX', 'INITIAL', 'INSERT', 'INTEGER', 'INTERSECT', 'INTO', 'IS', 'LEVEL', 'LIKE', 'LOCK', 'LONG', 'MAXEXTENTS', 'MINUS', 'MODE', 'MODIFY', 'NOAUDIT', 'NOCOMPRESS', 'NOT', 'NOTFOUND', 'NOWAIT', 'NULL', 'NUMBER', 'OF', 'OFFLINE', 'ON', 'ONLINE', 'OPTION', 'OR', 'ORDER', 'PCTFREE', 'PRIOR', 'PRIVILEGES', 'PUBLIC', 'RAW', 'RENAME', 'RESOURCE', 'REVOKE', 'ROW', 'ROWID', 'ROWLABEL', 'ROWNUM', 'ROWS', 'SELECT', 'SESSION', 'SET', 'SHARE', 'SIZE', 'SMALLINT', 'SQLBUF', 'START', 'SUCCESSFUL', 'SYNONYM', 'SYSDATE', 'TABLE', 'THEN', 'TO', 'TRIGGER', 'UID', 'UNION', 'UNIQUE', 'UPDATE', 'USER', 'VALIDATE', 'VALUES', 'VARCHAR', 'VARCHAR2', 'VIEW', 'WHENEVER', 'WHERE', 'WITH'];
const JSON_FUNCTION_REGEX = /^\s*((?:[a-z]+_){0,2}jsonb?(?:_[a-z]+){0,2})\([^)]*\)/i;
const JSON_OPERATOR_REGEX = /^\s*(->>?|@>|<@|\?[|&]?|\|{2}|#-)/i;
const TOKEN_CAPTURE_REGEX = /^\s*((?:([`"'])(?:(?!\2).|\2{2})*\2)|[\w\d\s]+|[().,;+-])/i;
export class OracleQueryGenerator extends AbstractQueryGenerator {
constructor(options) {
super(options);
}
/**
* Returns the value as it is stored in the Oracle DB
*
* @param {string} value
*/
getCatalogName(value) {
if (value) {
if (this.options.quoteIdentifiers === false) {
const quotedValue = this.quoteIdentifier(value);
if (quotedValue === value) {
value = value.toUpperCase();
}
}
}
return value;
}
/**
* Returns the tableName and schemaName as it is stored the Oracle DB
*
* @param {object|string} table
*/
getSchemaNameAndTableName(table) {
const tableName = this.getCatalogName(table.tableName || table);
const schemaName = this.getCatalogName(table.schema);
return [tableName, schemaName];
}
createSchema(schema) {
const quotedSchema = this.quoteIdentifier(schema);
return [
'DECLARE',
'USER_FOUND BOOLEAN := FALSE;',
'BEGIN',
' BEGIN',
' EXECUTE IMMEDIATE ',
this.escape(`CREATE USER ${quotedSchema} IDENTIFIED BY 12345 DEFAULT TABLESPACE USERS`),
';',
' EXCEPTION WHEN OTHERS THEN',
' IF SQLCODE != -1920 THEN',
' RAISE;',
' ELSE',
' USER_FOUND := TRUE;',
' END IF;',
' END;',
' IF NOT USER_FOUND THEN',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT "CONNECT" TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT CREATE TABLE TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT CREATE VIEW TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT CREATE ANY TRIGGER TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT CREATE ANY PROCEDURE TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT CREATE SEQUENCE TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`GRANT CREATE SYNONYM TO ${quotedSchema}`),
';',
' EXECUTE IMMEDIATE ',
this.escape(`ALTER USER ${quotedSchema} QUOTA UNLIMITED ON USERS`),
';',
' END IF;',
'END;'
].join(' ');
}
showSchemasQuery() {
return 'SELECT USERNAME AS "schema_name" FROM ALL_USERS WHERE COMMON = (\'NO\') AND USERNAME != user';
}
dropSchema(schema) {
return [
'BEGIN',
'EXECUTE IMMEDIATE ',
this.escape(`DROP USER ${this.quoteTable(schema)} CASCADE`),
';',
'EXCEPTION WHEN OTHERS THEN',
' IF SQLCODE != -1918 THEN',
' RAISE;',
' END IF;',
'END;'
].join(' ');
}
versionQuery() {
return "SELECT VERSION_FULL FROM PRODUCT_COMPONENT_VERSION WHERE PRODUCT LIKE 'Oracle%'";
}
createTableQuery(tableName, attributes, options) {
const primaryKeys = [],
foreignKeys = Object.create(null),
attrStr = [],
checkStr = [];
const values = {
table: this.quoteTable(tableName)
};
// Starting by dealing with all attributes
for (let attr in attributes) {
if (!Object.prototype.hasOwnProperty.call(attributes, attr)) continue;
const dataType = attributes[attr];
attr = this.quoteIdentifier(attr);
// ORACLE doesn't support inline REFERENCES declarations: move to the end
if (dataType.includes('PRIMARY KEY')) {
// Primary key
primaryKeys.push(attr);
if (dataType.includes('REFERENCES')) {
const match = dataType.match(/^(.+) (REFERENCES.*)$/);
attrStr.push(`${attr} ${match[1].replace(/PRIMARY KEY/, '')}`);
// match[2] already has foreignKeys in correct format so we don't need to replace
foreignKeys[attr] = match[2];
} else {
attrStr.push(`${attr} ${dataType.replace(/PRIMARY KEY/, '').trim()}`);
}
} else if (dataType.includes('REFERENCES')) {
// Foreign key
const match = dataType.match(/^(.+) (REFERENCES.*)$/);
attrStr.push(`${attr} ${match[1]}`);
// match[2] already has foreignKeys in correct format so we don't need to replace
foreignKeys[attr] = match[2];
} else {
attrStr.push(`${attr} ${dataType}`);
}
}
values['attributes'] = attrStr.join(', ');
const pkString = primaryKeys.map(pk => this.quoteIdentifier(pk)).join(', ');
if (pkString.length > 0) {
values.attributes += `,PRIMARY KEY (${pkString})`;
}
// Dealing with FKs
for (const fkey in foreignKeys) {
if (!Object.prototype.hasOwnProperty.call(foreignKeys, fkey)) continue;
// Oracle default response for FK, doesn't support if defined
if (foreignKeys[fkey].indexOf('ON DELETE NO ACTION') > -1) {
foreignKeys[fkey] = foreignKeys[fkey].replace('ON DELETE NO ACTION', '');
}
values.attributes += `,FOREIGN KEY (${this.quoteIdentifier(fkey)}) ${foreignKeys[fkey]}`;
}
if (checkStr.length > 0) {
values.attributes += `, ${checkStr.join(', ')}`;
}
// Specific case for unique indexes with Oracle, we have to set the constraint on the column, if not, no FK will be possible (ORA-02270: no matching unique or primary key for this column-list)
if (options && options.indexes && options.indexes.length > 0) {
const idxToDelete = [];
options.indexes.forEach((index, idx) => {
if ('unique' in index && (index.unique === true || index.unique.length > 0 && index.unique !== false)) {
// If unique index, transform to unique constraint on column
const fields = index.fields.map(field => {
if (typeof field === 'string') {
return field;
}
return field.attribute;
});
// Now we have to be sure that the constraint isn't already declared in uniqueKeys
let canContinue = true;
if (options.uniqueKeys) {
const keys = Object.keys(options.uniqueKeys);
for (let fieldIdx = 0; fieldIdx < keys.length; fieldIdx++) {
const currUnique = options.uniqueKeys[keys[fieldIdx]];
if (currUnique.fields.length === fields.length) {
// lengths are the same, possible same constraint
for (let i = 0; i < currUnique.fields.length; i++) {
const field = currUnique.fields[i];
if (_.includes(fields, field)) {
canContinue = false;
} else {
// We have at least one different column, even if we found the same columns previously, we let the constraint be created
canContinue = true;
break;
}
}
}
}
if (canContinue) {
const indexName = 'name' in index ? index.name : '';
const constraintToAdd = {
name: indexName,
fields
};
if (!('uniqueKeys' in options)) {
options.uniqueKeys = {};
}
options.uniqueKeys[indexName] = constraintToAdd;
idxToDelete.push(idx);
} else {
// The constraint already exists, we remove it from the list
idxToDelete.push(idx);
}
}
}
});
idxToDelete.forEach(idx => {
options.indexes.splice(idx, 1);
});
}
if (options && !!options.uniqueKeys) {
_.each(options.uniqueKeys, (columns, indexName) => {
let canBeUniq = false;
// Check if we can create the unique key
primaryKeys.forEach(primaryKey => {
// We can create an unique constraint if it's not on the primary key AND if it doesn't have unique in its definition
// We replace quotes in primary key with ''
// Primary key would be a list with double quotes in it so we remove the double quotes
primaryKey = primaryKey.replace(/"/g, '');
// We check if the unique indexes are already a part of primary key or not
// If it is not then we set canbeuniq to true and add a unique constraint to these fields.
// Else we can ignore unique constraint on these
if (!_.includes(columns.fields, primaryKey)) {
canBeUniq = true;
}
});
columns.fields.forEach(field => {
let currField = '';
if (!_.isString(field)) {
currField = field.attribute.replace(/[.,"\s]/g, '');
} else {
currField = field.replace(/[.,"\s]/g, '');
}
if (currField in attributes) {
// If canBeUniq is false we need not replace the UNIQUE for the attribute
// So we replace UNIQUE with '' only if there exists a primary key
if (attributes[currField].toUpperCase().indexOf('UNIQUE') > -1 && canBeUniq) {
// We generate the attribute without UNIQUE
const attrToReplace = attributes[currField].replace('UNIQUE', '');
// We replace in the final string
values.attributes = values.attributes.replace(attributes[currField], attrToReplace);
}
}
});
// Oracle cannot have an unique AND a primary key on the same fields, prior to the primary key
if (canBeUniq) {
const index = options.uniqueKeys[columns.name];
delete options.uniqueKeys[columns.name];
indexName = indexName.replace(/[.,\s]/g, '');
columns.name = indexName;
options.uniqueKeys[indexName] = index;
// Autogenerate Constraint name, if no indexName is given
if (indexName.length === 0) {
values.attributes += `,UNIQUE (${columns.fields.map(field => this.quoteIdentifier(field)).join(', ') })`;
} else {
values.attributes +=
`, CONSTRAINT ${this.quoteIdentifier(indexName)} UNIQUE (${columns.fields.map(field => this.quoteIdentifier(field)).join(', ') })`;
}
}
});
}
// we replace single quotes by two quotes in order for the execute statement to work
const query = Utils.joinSQLFragments([
'CREATE TABLE',
values.table,
`(${values.attributes})`
]);
return Utils.joinSQLFragments([
'BEGIN',
'EXECUTE IMMEDIATE',
`${this.escape(query)};`,
'EXCEPTION WHEN OTHERS THEN',
'IF SQLCODE != -955 THEN',
'RAISE;',
'END IF;',
'END;'
]);
}
tableExistsQuery(table) {
const [tableName, schemaName] = this.getSchemaNameAndTableName(table);
return `SELECT TABLE_NAME FROM ALL_TABLES WHERE TABLE_NAME = ${this.escape(tableName)} AND OWNER = ${table.schema ? this.escape(schemaName) : 'USER'}`;
}
describeTableQuery(tableName, schema) {
const currTableName = this.getCatalogName(tableName.tableName || tableName);
schema = this.getCatalogName(schema);
// name, type, datalength (except number / nvarchar), datalength varchar, datalength number, nullable, default value, primary ?
return [
'SELECT atc.COLUMN_NAME, atc.DATA_TYPE, atc.DATA_LENGTH, atc.CHAR_LENGTH, atc.DEFAULT_LENGTH, atc.NULLABLE, ucc.constraint_type ',
'FROM all_tab_columns atc ',
'LEFT OUTER JOIN ',
'(SELECT acc.column_name, acc.table_name, ac.constraint_type FROM all_cons_columns acc INNER JOIN all_constraints ac ON acc.constraint_name = ac.constraint_name) ucc ',
'ON (atc.table_name = ucc.table_name AND atc.COLUMN_NAME = ucc.COLUMN_NAME) ',
schema
? `WHERE (atc.OWNER = ${this.escape(schema)}) `
: 'WHERE atc.OWNER = USER ',
`AND (atc.TABLE_NAME = ${this.escape(currTableName)})`,
'ORDER BY atc.COLUMN_NAME, CONSTRAINT_TYPE DESC'
].join('');
}
renameTableQuery(before, after) {
return Utils.joinSQLFragments([
'ALTER TABLE',
this.quoteTable(before),
'RENAME TO',
this.quoteTable(after)
]);
}
showConstraintsQuery(table) {
const tableName = this.getCatalogName(table.tableName || table);
return `SELECT CONSTRAINT_NAME constraint_name FROM user_cons_columns WHERE table_name = ${this.escape(tableName)}`;
}
showTablesQuery() {
return 'SELECT owner as table_schema, table_name, 0 as lvl FROM all_tables where OWNER IN(SELECT USERNAME AS "schema_name" FROM ALL_USERS WHERE ORACLE_MAINTAINED = \'N\')';
}
dropTableQuery(tableName) {
return Utils.joinSQLFragments([
'BEGIN ',
'EXECUTE IMMEDIATE \'DROP TABLE',
this.quoteTable(tableName),
'CASCADE CONSTRAINTS PURGE\';',
'EXCEPTION WHEN OTHERS THEN',
' IF SQLCODE != -942 THEN',
' RAISE;',
' END IF;',
'END;'
]);
}
/*
Modifying the indexname so that it is prefixed with the schema name
otherwise Oracle tries to add the index to the USER schema
@overide
*/
addIndexQuery(tableName, attributes, options, rawTablename) {
if (typeof tableName !== 'string' && attributes.name) {
attributes.name = `${tableName.schema}.${attributes.name}`;
}
return super.addIndexQuery(tableName, attributes, options, rawTablename);
}
addConstraintQuery(tableName, options) {
options = options || {};
const constraintSnippet = this.getConstraintSnippet(tableName, options);
tableName = this.quoteTable(tableName);
return `ALTER TABLE ${tableName} ADD ${constraintSnippet};`;
}
addColumnQuery(table, key, dataType) {
dataType.field = key;
const attribute = Utils.joinSQLFragments([
this.quoteIdentifier(key),
this.attributeToSQL(dataType, {
attributeName: key,
context: 'addColumn'
})
]);
return Utils.joinSQLFragments([
'ALTER TABLE',
this.quoteTable(table),
'ADD',
attribute
]);
}
removeColumnQuery(tableName, attributeName) {
return Utils.joinSQLFragments([
'ALTER TABLE',
this.quoteTable(tableName),
'DROP COLUMN',
this.quoteIdentifier(attributeName),
';'
]);
}
/**
* Function to add new foreign key to the attribute
* Block for add and drop foreign key constraint query
* taking the assumption that there is a single column foreign key reference always
* i.e. we always do - FOREIGN KEY (a) reference B(a) during createTable queryGenerator
* so there would be one and only one match for a constraint name for each column
* and every foreign keyed column would have a different constraint name
* Since sequelize doesn't support multiple column foreign key, added complexity to
* add the feature isn't needed
*
* @param {string} definition The operation that needs to be performed on the attribute
* @param {string|object} table The table that needs to be altered
* @param {string} attributeName The name of the attribute which would get altered
*/
_alterForeignKeyConstraint(definition, table, attributeName) {
const [tableName, schemaName] = this.getSchemaNameAndTableName(table);
const attributeNameConstant = this.escape(this.getCatalogName(attributeName));
const schemaNameConstant = table.schema ? this.escape(this.getCatalogName(schemaName)) : 'USER';
const tableNameConstant = this.escape(this.getCatalogName(tableName));
const getConsNameQuery = [
'SELECT constraint_name INTO cons_name',
'FROM (',
' SELECT DISTINCT cc.owner, cc.table_name, cc.constraint_name, cc.column_name AS cons_columns',
' FROM all_cons_columns cc, all_constraints c',
' WHERE cc.owner = c.owner',
' AND cc.table_name = c.table_name',
' AND cc.constraint_name = c.constraint_name',
' AND c.constraint_type = \'R\'',
' GROUP BY cc.owner, cc.table_name, cc.constraint_name, cc.column_name',
')',
'WHERE owner =',
schemaNameConstant,
'AND table_name =',
tableNameConstant,
'AND cons_columns =',
attributeNameConstant,
';'
].join(' ');
const secondQuery = Utils.joinSQLFragments([
`ALTER TABLE ${this.quoteIdentifier(tableName)}`,
'ADD FOREIGN KEY',
`(${this.quoteIdentifier(attributeName)})`,
definition.replace(/.+?(?=REFERENCES)/, '')
]);
return [
'BEGIN',
getConsNameQuery,
'EXCEPTION',
'WHEN NO_DATA_FOUND THEN',
' CONS_NAME := NULL;',
'END;',
'IF CONS_NAME IS NOT NULL THEN',
` EXECUTE IMMEDIATE 'ALTER TABLE ${this.quoteTable(table)} DROP CONSTRAINT "'||CONS_NAME||'"';`,
'END IF;',
`EXECUTE IMMEDIATE ${this.escape(secondQuery)};`
].join(' ');
}
/**
* Function to alter table modify
*
* @param {string} definition The operation that needs to be performed on the attribute
* @param {object|string} table The table that needs to be altered
* @param {string} attributeName The name of the attribute which would get altered
*/
_modifyQuery(definition, table, attributeName) {
const query = Utils.joinSQLFragments([
'ALTER TABLE',
this.quoteTable(table),
'MODIFY',
this.quoteIdentifier(attributeName),
definition
]);
const secondQuery = query.replace('NOT NULL', '').replace('NULL', '');
return [
'BEGIN',
`EXECUTE IMMEDIATE ${this.escape(query)};`,
'EXCEPTION',
'WHEN OTHERS THEN',
' IF SQLCODE = -1442 OR SQLCODE = -1451 THEN',
// We execute the statement without the NULL / NOT NULL clause if the first statement failed due to this
` EXECUTE IMMEDIATE ${this.escape(secondQuery)};`,
' ELSE',
' RAISE;',
' END IF;',
'END;'
].join(' ');
}
changeColumnQuery(table, attributes) {
const sql = [
'DECLARE',
'CONS_NAME VARCHAR2(200);',
'BEGIN'
];
for (const attributeName in attributes) {
if (!Object.prototype.hasOwnProperty.call(attributes, attributeName)) continue;
const definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) {
sql.push(this._alterForeignKeyConstraint(definition, table, attributeName));
} else {
// Building the modify query
sql.push(this._modifyQuery(definition, table, attributeName));
}
}
sql.push('END;');
return sql.join(' ');
}
renameColumnQuery(tableName, attrBefore, attributes) {
const newName = Object.keys(attributes)[0];
return `ALTER TABLE ${this.quoteTable(tableName)} RENAME COLUMN ${this.quoteIdentifier(attrBefore)} TO ${this.quoteIdentifier(newName)}`;
}
/**
* Populates the returnAttributes array with outbind bindByPosition values
* and also the options.outBindAttributes map with bindDef for outbind of InsertQuery
*
* @param {Array} returningModelAttributes
* @param {Array} returnTypes
* @param {number} inbindLength
* @param {object} returnAttributes
* @param {object} options
*
* @private
*/
populateInsertQueryReturnIntoBinds(returningModelAttributes, returnTypes, inbindLength, returnAttributes, options) {
const oracledb = this.sequelize.connectionManager.lib;
const outBindAttributes = Object.create(null);
const outbind = [];
const outbindParam = this.bindParam(outbind, inbindLength);
returningModelAttributes.forEach((element, index) => {
// generateReturnValues function quotes identifier based on the quoteIdentifier option
// If the identifier starts with a quote we remove it else we use it as is
if (element.startsWith('"')) {
element = element.substring(1, element.length - 1);
}
outBindAttributes[element] = Object.assign(returnTypes[index]._getBindDef(oracledb), { dir: oracledb.BIND_OUT });
const returnAttribute = `${this.format(undefined, undefined, { context: 'INSERT' }, outbindParam)}`;
returnAttributes.push(returnAttribute);
});
options.outBindAttributes = outBindAttributes;
}
/**
* Override of upsertQuery, Oracle specific
* Using PL/SQL for finding the row
*
* @param {object|string} tableName
* @param {Array} insertValues
* @param {Array} updateValues
* @param {Array} where
* @param {object} model
* @param {object} options
*/
upsertQuery(tableName, insertValues, updateValues, where, model, options) {
const rawAttributes = model.rawAttributes;
const updateQuery = this.updateQuery(tableName, updateValues, where, options, rawAttributes);
// This bind is passed so that the insert query starts appending to this same bind array
options.bind = updateQuery.bind;
const insertQuery = this.insertQuery(tableName, insertValues, rawAttributes, options);
const sql = [
'DECLARE ',
'BEGIN ',
updateQuery.query ? [
updateQuery.query,
'; ',
' IF ( SQL%ROWCOUNT = 0 ) THEN ',
insertQuery.query,
' :isUpdate := 0; ',
'ELSE ',
' :isUpdate := 1; ',
' END IF; '
].join('') : [
insertQuery.query,
' :isUpdate := 0; ',
// If there is a conflict on insert we ignore
'EXCEPTION WHEN OTHERS THEN',
' IF SQLCODE != -1 THEN',
' RAISE;',
' END IF;'
].join(''),
'END;'
];
const query = sql.join('');
const result = { query };
if (options.bindParam !== false) {
result.bind = updateQuery.bind || insertQuery.bind;
}
return result;
}
/**
* Returns an insert into command for multiple values.
*
* @param {string} tableName
* @param {object} fieldValueHashes
* @param {object} options
* @param {object} fieldMappedAttributes
*
* @private
*/
bulkInsertQuery(tableName, fieldValueHashes, options, fieldMappedAttributes) {
options = options || {};
options.executeMany = true;
fieldMappedAttributes = fieldMappedAttributes || {};
const tuples = [];
const allColumns = {};
const inBindBindDefMap = {};
const outBindBindDefMap = {};
const oracledb = this.sequelize.connectionManager.lib;
// Generating the allColumns map
// The data is provided as an array of objects.
// Each object may contain differing numbers of attributes.
// A set of the attribute names that are used in all objects must be determined.
// The allColumns map contains the column names and indicates whether the value is generated or not
// We set allColumns[key] to true if the field is an
// auto-increment field and the value given is null and fieldMappedAttributes[key]
// is valid for the specific column else it is set to false
for (const fieldValueHash of fieldValueHashes) {
_.forOwn(fieldValueHash, (value, key) => {
allColumns[key] = fieldMappedAttributes[key] && fieldMappedAttributes[key].autoIncrement === true && value === null;
});
}
// Building the inbind parameter
// A list that would have inbind positions like [:1, :2, :3...] to be used in generating sql string
let inBindPosition;
// Iterating over each row of the fieldValueHashes
for (const fieldValueHash of fieldValueHashes) {
// Has each column for a row after coverting it to appropriate format using this.format function
// like ['Mick', 'Broadstone', 2022-02-16T05:24:18.949Z, 2022-02-16T05:24:18.949Z],
const tuple = [];
// A function expression for this.bindParam/options.bindparam function
// This function is passed to this.format function which inserts column values to the tuple list
// using _bindParam/_stringify function in data-type.js file
const inbindParam = options.bindParam === undefined ? this.bindParam(tuple) : options.bindParam;
// We are iterating over each col
// and pushing the given values to tuple list using this.format function
// and also simultaneously generating the bindPosition
// tempBindPostions has the inbind positions
const tempBindPositions = Object.keys(allColumns).map(key => {
if (allColumns[key] === true) {
// We had set allAttributes[key] to true since at least one row for an auto increment column was null
// If we get any other row that has this specific column as non-null we must raise an error
// Since for an auto-increment column, either all row has to be null or all row has to be a non-null
if (fieldValueHash[key] !== null) {
throw Error('For an auto-increment column either all row must be null or non-null, a mix of null and non-null is not allowed!');
}
// Return DEFAULT for auto-increment column and if all values for the column is null in each row
return 'DEFAULT';
}
// Sanitizes the values given by the user and pushes it to the tuple list using inBindParam function and
// also generates the inbind position for the sql string for example (:1, :2, :3.....) which is a by product of the push
return this.format(fieldValueHash[key], fieldMappedAttributes[key], { context: 'INSERT' }, inbindParam);
});
// Even though the bind variable positions are calculated for each row we only retain the values for the first row
// since the values will be identical
if (!inBindPosition) {
inBindPosition = tempBindPositions;
}
// Adding the row to the array of rows that will be supplied to executeMany()
tuples.push(tuple);
}
// The columns that we are expecting to be returned from the DB like ["id1", "id2"...]
const returnColumn = [];
// The outbind positions for the returning columns like [:3, :4, :5....]
const returnColumnBindPositions = [];
// Has the columns name in which data would be inserted like ["id", "name".....]
const insertColumns = [];
// Iterating over the allColumns keys to get the bindDef for inbind and outbinds
// and also to get the list of insert and return column after applying this.quoteIdentifier
for (const key of Object.keys(allColumns)) {
// If fieldMappenAttributes[attr] is defined we generate the bindDef
// and return clause else we can skip it
if (fieldMappedAttributes[key]) {
// BindDef for the specific column
const bindDef = fieldMappedAttributes[key].type._getBindDef(oracledb);
if (allColumns[key]) {
// Binddef for outbinds
bindDef.dir = oracledb.BIND_OUT;
outBindBindDefMap[key] = bindDef;
// Building the outbind parameter list
// ReturnColumn has the column name for example "id", "usedId", quoting depends on quoteIdentifier option
returnColumn.push(this.quoteIdentifier(key));
// Pushing the outbind index to the returnColumnPositions to generate (:3, :4, :5)
// The start offset depend on the tuple length (bind array size of a particular row)
// the outbind position starts after the position where inbind position ends
returnColumnBindPositions.push(`:${tuples[0].length + returnColumn.length}`);
} else {
// Binddef for inbinds
bindDef.dir = oracledb.BIND_IN;
inBindBindDefMap[key] = bindDef;
}
}
// Quoting and pushing each insert column based on quoteIdentifier option
insertColumns.push(this.quoteIdentifier(key));
}
// Generating the sql query
let query = Utils.joinSQLFragments([
'INSERT',
'INTO',
// Table name for the table in which data needs to inserted
this.quoteTable(tableName),
// Columns names for the columns of the table (example "a", "b", "c" - quoting depends on the quoteidentifier option)
`(${insertColumns.join(',')})`,
'VALUES',
// InBind position for the insert query (for example :1, :2, :3....)
`(${inBindPosition})`
]);
// If returnColumn.length is > 0
// then the returning into clause is needed
if (returnColumn.length > 0) {
options.outBindAttributes = outBindBindDefMap;
query = Utils.joinSQLFragments([
query,
'RETURNING',
// List of return column (for example "id", "userId"....)
`${returnColumn.join(',')}`,
'INTO',
// List of outbindPosition (for example :4, :5, :6....)
// Start offset depends on where inbindPosition end
`${returnColumnBindPositions}`
]);
}
// Binding the bind variable to result
const result = { query };
// Binding the bindParam to result
// Tuple has each row for the insert query
result.bind = tuples;
// Setting options.inbindAttribute
options.inbindAttributes = inBindBindDefMap;
return result;
}
truncateTableQuery(tableName) {
return `TRUNCATE TABLE ${this.quoteTable(tableName)}`;
}
deleteQuery(tableName, where, options, model) {
options = options || {};
const table = tableName;
where = this.getWhereConditions(where, null, model, options);
let queryTmpl;
// delete with limit <l> and optional condition <e> on Oracle: DELETE FROM <t> WHERE rowid in (SELECT rowid FROM <t> WHERE <e> AND rownum <= <l>)
// Note that the condition <e> has to be in the subquery; otherwise, the subquery would select <l> arbitrary rows.
if (options.limit) {
const whereTmpl = where ? ` AND ${where}` : '';
queryTmpl =
`DELETE FROM ${this.quoteTable(table)} WHERE rowid IN (SELECT rowid FROM ${this.quoteTable(table)} WHERE rownum <= ${this.escape(options.limit)}${
whereTmpl
})`;
} else {
const whereTmpl = where ? ` WHERE ${where}` : '';
queryTmpl = `DELETE FROM ${this.quoteTable(table)}${whereTmpl}`;
}
return queryTmpl;
}
showIndexesQuery(table) {
const [tableName, owner] = this.getSchemaNameAndTableName(table);
const sql = [
'SELECT i.index_name,i.table_name, i.column_name, u.uniqueness, i.descend, c.constraint_type ',
'FROM all_ind_columns i ',
'INNER JOIN all_indexes u ',
'ON (u.table_name = i.table_name AND u.index_name = i.index_name) ',
'LEFT OUTER JOIN all_constraints c ',
'ON (c.table_name = i.table_name AND c.index_name = i.index_name) ',
`WHERE i.table_name = ${this.escape(tableName)}`,
' AND u.table_owner = ',
owner ? this.escape(owner) : 'USER',
' ORDER BY index_name, column_position'
];
return sql.join('');
}
removeIndexQuery(tableName, indexNameOrAttributes) {
let indexName = indexNameOrAttributes;
if (typeof indexName !== 'string') {
indexName = Utils.underscore(`${tableName }_${indexNameOrAttributes.join('_')}`);
}
return `DROP INDEX ${this.quoteIdentifier(indexName)}`;
}
attributeToSQL(attribute, options) {
if (!_.isPlainObject(attribute)) {
attribute = {
type: attribute
};
}
// TODO: Address on update cascade issue whether to throw error or ignore.
// Add this to documentation when merging to sequelize-main
// ON UPDATE CASCADE IS NOT SUPPORTED BY ORACLE.
attribute.onUpdate = '';
// handle self referential constraints
if (attribute.references) {
if (attribute.Model && attribute.Model.tableName === attribute.references.model) {
this.sequelize.log(
'Oracle does not support self referencial constraints, ' +
'we will remove it but we recommend restructuring your query'
);
attribute.onDelete = '';
}
}
let template;
if (attribute.type instanceof DataTypes.ENUM) {
if (attribute.type.values && !attribute.values) attribute.values = attribute.type.values;
// enums are a special case
template = attribute.type.toSql();
template +=
` CHECK (${this.quoteIdentifier(options.attributeName)} IN(${
_.map(attribute.values, value => {
return this.escape(value);
}).join(', ')
}))`;
return template;
}
if (attribute.type instanceof DataTypes.JSON) {
template = attribute.type.toSql();
template += ` CHECK (${this.quoteIdentifier(options.attributeName)} IS JSON)`;
return template;
}
if (attribute.type instanceof DataTypes.BOOLEAN) {
template = attribute.type.toSql();
template +=
` CHECK (${this.quoteIdentifier(options.attributeName)} IN('1', '0'))`;
return template;
}
if (attribute.autoIncrement) {
template = ' NUMBER(*,0) GENERATED BY DEFAULT ON NULL AS IDENTITY';
} else if (attribute.type && attribute.type.key === DataTypes.DOUBLE.key) {
template = attribute.type.toSql();
} else if (attribute.type) {
// setting it to false because oracle doesn't support unsigned int so put a check to make it behave like unsigned int
let unsignedTemplate = '';
if (attribute.type._unsigned) {
attribute.type._unsigned = false;
unsignedTemplate += ` check(${this.quoteIdentifier(options.attributeName)} >= 0)`;
}
template = attribute.type.toString();
// Blobs/texts cannot have a defaultValue
if (
attribute.type &&
attribute.type !== 'TEXT' &&
attribute.type._binary !== true &&
Utils.defaultValueSchemable(attribute.defaultValue)
) {
template += ` DEFAULT ${this.escape(attribute.defaultValue)}`;
}
if (!attribute.autoIncrement) {
// If autoincrement, not null is set automatically
if (attribute.allowNull === false) {
template += ' NOT NULL';
} else if (!attribute.primaryKey && !Utils.defaultValueSchemable(attribute.defaultValue)) {
template += ' NULL';
}
}
template += unsignedTemplate;
} else {
template = '';
}
if (attribute.unique === true && !attribute.primaryKey) {
template += ' UNIQUE';
}
if (attribute.primaryKey) {
template += ' PRIMARY KEY';
}
if ((!options || !options.withoutForeignKeyConstraints) && attribute.references) {
template += ` REFERENCES ${this.quoteTable(attribute.references.model)}`;
if (attribute.references.key) {
template += ` (${this.quoteIdentifier(attribute.references.key) })`;
} else {
template += ` (${this.quoteIdentifier('id') })`;
}
if (attribute.onDelete && attribute.onDelete.toUpperCase() !== 'NO ACTION') {
template += ` ON DELETE ${attribute.onDelete.toUpperCase()}`;
}
}
return template;
}
attributesToSQL(attributes, options) {
const result = {};
for (const key in attributes) {
const attribute = attributes[key];
const attributeName = attribute.field || key;
result[attributeName] = this.attributeToSQL(attribute, { attributeName, ...options });
}
return result;
}
createTrigger() {
throwMethodUndefined('createTrigger');
}
dropTrigger() {
throwMethodUndefined('dropTrigger');
}
renameTrigger() {
throwMethodUndefined('renameTrigger');
}
createFunction() {
throwMethodUndefined('createFunction');
}
dropFunction() {
throwMethodUndefined('dropFunction');
}
renameFunction() {
throwMethodUndefined('renameFunction');
}
getConstraintsOnColumn(table, column) {
const [tableName, schemaName] = this.getSchemaNameAndTableName(table);
column = this.getCatalogName(column);
const sql = [
'SELECT CONSTRAINT_NAME FROM user_cons_columns WHERE TABLE_NAME = ',
this.escape(tableName),
' and OWNER = ',
table.schema ? this.escape(schemaName) : 'USER',
' and COLUMN_NAME = ',
this.escape(column),
' AND POSITION IS NOT NULL ORDER BY POSITION'
].join('');
return sql;
}
getForeignKeysQuery(table) {
// We don't call quoteTable as we don't want the schema in the table name, Oracle seperates it on another field
const [tableName, schemaName] = this.getSchemaNameAndTableName(table);
const sql = [
'SELECT DISTINCT a.table_name "tableName", a.constraint_name "constraintName", a.owner "owner", a.column_name "columnName",',
' b.table_name "referencedTableName", b.column_name "referencedColumnName"',
' FROM all_cons_columns a',
' JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name',