knex 0.95.13 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +74 -3
- package/README.md +1 -1
- package/UPGRADING.md +7 -0
- package/lib/client.js +14 -1
- package/lib/constants.js +2 -0
- package/lib/dialects/better-sqlite3/index.js +72 -0
- package/lib/dialects/cockroachdb/crdb-querycompiler.js +87 -33
- package/lib/dialects/cockroachdb/crdb-tablecompiler.js +19 -0
- package/lib/dialects/cockroachdb/index.js +13 -0
- package/lib/dialects/mssql/index.js +0 -11
- package/lib/dialects/mssql/query/mssql-querycompiler.js +122 -64
- package/lib/dialects/mssql/schema/mssql-columncompiler.js +41 -6
- package/lib/dialects/mssql/schema/mssql-tablecompiler.js +24 -9
- package/lib/dialects/mssql/schema/mssql-viewcompiler.js +15 -1
- package/lib/dialects/mysql/index.js +3 -7
- package/lib/dialects/mysql/query/mysql-querycompiler.js +91 -5
- package/lib/dialects/mysql/schema/mysql-columncompiler.js +32 -5
- package/lib/dialects/mysql/schema/mysql-tablecompiler.js +28 -4
- package/lib/dialects/mysql2/index.js +7 -4
- package/lib/dialects/oracle/query/oracle-querycompiler.js +7 -6
- package/lib/dialects/oracle/schema/internal/trigger.js +1 -1
- package/lib/dialects/oracle/schema/oracle-columncompiler.js +10 -4
- package/lib/dialects/oracle/schema/oracle-tablecompiler.js +17 -6
- package/lib/dialects/oracledb/index.js +0 -4
- package/lib/dialects/oracledb/query/oracledb-querycompiler.js +104 -0
- package/lib/dialects/oracledb/schema/oracledb-columncompiler.js +23 -0
- package/lib/dialects/postgres/index.js +21 -6
- package/lib/dialects/postgres/query/pg-querybuilder.js +8 -0
- package/lib/dialects/postgres/query/pg-querycompiler.js +166 -5
- package/lib/dialects/postgres/schema/pg-columncompiler.js +24 -4
- package/lib/dialects/postgres/schema/pg-tablecompiler.js +55 -47
- package/lib/dialects/redshift/index.js +12 -0
- package/lib/dialects/redshift/query/redshift-querycompiler.js +62 -26
- package/lib/dialects/redshift/schema/redshift-columncompiler.js +2 -1
- package/lib/dialects/redshift/schema/redshift-tablecompiler.js +4 -1
- package/lib/dialects/sqlite3/index.js +18 -4
- package/lib/dialects/sqlite3/query/sqlite-querycompiler.js +85 -18
- package/lib/dialects/sqlite3/schema/ddl.js +274 -282
- package/lib/dialects/sqlite3/schema/internal/sqlite-ddl-operations.js +18 -8
- package/lib/dialects/sqlite3/schema/sqlite-columncompiler.js +20 -0
- package/lib/dialects/sqlite3/schema/sqlite-compiler.js +16 -12
- package/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js +15 -5
- package/lib/dialects/sqlite3/schema/sqlite-viewcompiler.js +31 -2
- package/lib/execution/runner.js +37 -2
- package/lib/knex-builder/FunctionHelper.js +21 -0
- package/lib/migrations/common/MigrationsLoader.js +36 -0
- package/lib/migrations/migrate/MigrationGenerator.js +1 -1
- package/lib/migrations/migrate/Migrator.js +20 -23
- package/lib/migrations/migrate/migration-list-resolver.js +2 -5
- package/lib/migrations/migrate/{configuration-merger.js → migrator-configuration-merger.js} +2 -4
- package/lib/migrations/migrate/sources/fs-migrations.js +4 -29
- package/lib/migrations/migrate/stub/js.stub +8 -1
- package/lib/migrations/migrate/stub/knexfile-js.stub +3 -0
- package/lib/migrations/migrate/stub/knexfile-ts.stub +5 -2
- package/lib/migrations/migrate/table-creator.js +6 -5
- package/lib/migrations/seed/Seeder.js +25 -92
- package/lib/migrations/seed/seeder-configuration-merger.js +60 -0
- package/lib/migrations/seed/sources/fs-seeds.js +65 -0
- package/lib/migrations/seed/stub/js.stub +4 -1
- package/lib/migrations/util/import-file.js +0 -1
- package/lib/query/joinclause.js +24 -5
- package/lib/query/method-constants.js +37 -0
- package/lib/query/querybuilder.js +230 -5
- package/lib/query/querycompiler.js +269 -84
- package/lib/schema/columnbuilder.js +8 -0
- package/lib/schema/columncompiler.js +132 -5
- package/lib/schema/compiler.js +1 -0
- package/lib/schema/tablebuilder.js +41 -8
- package/lib/schema/tablecompiler.js +57 -0
- package/lib/schema/viewcompiler.js +13 -10
- package/package.json +35 -22
- package/scripts/docker-compose.yml +7 -7
- package/scripts/oracledb-install-driver-libs.sh +82 -0
- package/scripts/runkit-example.js +1 -1
- package/scripts/stress-test/docker-compose.yml +3 -3
- package/scripts/stress-test/knex-stress-test.js +1 -1
- package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +1 -1
- package/types/index.d.ts +124 -20
|
@@ -144,7 +144,28 @@ class QueryCompiler {
|
|
|
144
144
|
// inserts using a single query statement.
|
|
145
145
|
insert() {
|
|
146
146
|
const insertValues = this.single.insert || [];
|
|
147
|
-
|
|
147
|
+
const sql = this.with() + `insert into ${this.tableName} `;
|
|
148
|
+
const body = this._insertBody(insertValues);
|
|
149
|
+
return body === '' ? '' : sql + body;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_buildInsertValues(insertData) {
|
|
153
|
+
let sql = '';
|
|
154
|
+
let i = -1;
|
|
155
|
+
while (++i < insertData.values.length) {
|
|
156
|
+
if (i !== 0) sql += '), (';
|
|
157
|
+
sql += this.client.parameterize(
|
|
158
|
+
insertData.values[i],
|
|
159
|
+
this.client.valueForUndefined,
|
|
160
|
+
this.builder,
|
|
161
|
+
this.bindingsHolder
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
return sql;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
_insertBody(insertValues) {
|
|
168
|
+
let sql = '';
|
|
148
169
|
if (Array.isArray(insertValues)) {
|
|
149
170
|
if (insertValues.length === 0) {
|
|
150
171
|
return '';
|
|
@@ -164,18 +185,7 @@ class QueryCompiler {
|
|
|
164
185
|
this.client,
|
|
165
186
|
this.bindingsHolder
|
|
166
187
|
)}`;
|
|
167
|
-
sql += ') values (';
|
|
168
|
-
let i = -1;
|
|
169
|
-
while (++i < insertData.values.length) {
|
|
170
|
-
if (i !== 0) sql += '), (';
|
|
171
|
-
sql += this.client.parameterize(
|
|
172
|
-
insertData.values[i],
|
|
173
|
-
this.client.valueForUndefined,
|
|
174
|
-
this.builder,
|
|
175
|
-
this.bindingsHolder
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
sql += ')';
|
|
188
|
+
sql += ') values (' + this._buildInsertValues(insertData) + ')';
|
|
179
189
|
} else if (insertValues.length === 1 && insertValues[0]) {
|
|
180
190
|
sql += this._emptyInsertValue;
|
|
181
191
|
} else {
|
|
@@ -230,6 +240,8 @@ class QueryCompiler {
|
|
|
230
240
|
sql.push(this.aggregateRaw(stmt));
|
|
231
241
|
} else if (stmt.type === 'analytic') {
|
|
232
242
|
sql.push(this.analytic(stmt));
|
|
243
|
+
} else if (stmt.type === 'json') {
|
|
244
|
+
sql.push(this.json(stmt));
|
|
233
245
|
} else if (stmt.value && stmt.value.length > 0) {
|
|
234
246
|
sql.push(
|
|
235
247
|
columnize_(
|
|
@@ -243,8 +255,9 @@ class QueryCompiler {
|
|
|
243
255
|
}
|
|
244
256
|
}
|
|
245
257
|
if (sql.length === 0) sql = ['*'];
|
|
258
|
+
const select = this.onlyJson() ? '' : 'select ';
|
|
246
259
|
return (
|
|
247
|
-
|
|
260
|
+
`${select}${hints}${distinctClause}` +
|
|
248
261
|
sql.join(', ') +
|
|
249
262
|
(this.tableName
|
|
250
263
|
? ` from ${this.single.only ? 'only ' : ''}${this.tableName}`
|
|
@@ -333,6 +346,12 @@ class QueryCompiler {
|
|
|
333
346
|
})`;
|
|
334
347
|
}
|
|
335
348
|
|
|
349
|
+
_joinTable(join) {
|
|
350
|
+
return join.schema && !(join.table instanceof Raw)
|
|
351
|
+
? `${join.schema}.${join.table}`
|
|
352
|
+
: join.table;
|
|
353
|
+
}
|
|
354
|
+
|
|
336
355
|
// Compiles all each of the `join` clauses on the query,
|
|
337
356
|
// including any nested join queries.
|
|
338
357
|
join() {
|
|
@@ -342,10 +361,7 @@ class QueryCompiler {
|
|
|
342
361
|
if (!joins) return '';
|
|
343
362
|
while (++i < joins.length) {
|
|
344
363
|
const join = joins[i];
|
|
345
|
-
const table =
|
|
346
|
-
join.schema && !(join.table instanceof Raw)
|
|
347
|
-
? `${join.schema}.${join.table}`
|
|
348
|
-
: join.table;
|
|
364
|
+
const table = this._joinTable(join);
|
|
349
365
|
if (i > 0) sql += ' ';
|
|
350
366
|
if (join.joinType === 'raw') {
|
|
351
367
|
sql += unwrapRaw_(
|
|
@@ -435,6 +451,23 @@ class QueryCompiler {
|
|
|
435
451
|
|
|
436
452
|
onIn(statement) {
|
|
437
453
|
if (Array.isArray(statement.column)) return this.multiOnIn(statement);
|
|
454
|
+
|
|
455
|
+
let values;
|
|
456
|
+
if (statement.value instanceof Raw) {
|
|
457
|
+
values = this.client.parameter(
|
|
458
|
+
statement.value,
|
|
459
|
+
this.builder,
|
|
460
|
+
this.formatter
|
|
461
|
+
);
|
|
462
|
+
} else {
|
|
463
|
+
values = this.client.parameterize(
|
|
464
|
+
statement.value,
|
|
465
|
+
undefined,
|
|
466
|
+
this.builder,
|
|
467
|
+
this.bindingsHolder
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
438
471
|
return (
|
|
439
472
|
wrap_(
|
|
440
473
|
statement.column,
|
|
@@ -445,14 +478,7 @@ class QueryCompiler {
|
|
|
445
478
|
) +
|
|
446
479
|
' ' +
|
|
447
480
|
this._not(statement, 'in ') +
|
|
448
|
-
this.wrap(
|
|
449
|
-
this.client.parameterize(
|
|
450
|
-
statement.value,
|
|
451
|
-
undefined,
|
|
452
|
-
this.builder,
|
|
453
|
-
this.bindingsHolder
|
|
454
|
-
)
|
|
455
|
-
)
|
|
481
|
+
this.wrap(values)
|
|
456
482
|
);
|
|
457
483
|
}
|
|
458
484
|
|
|
@@ -653,24 +679,7 @@ class QueryCompiler {
|
|
|
653
679
|
}
|
|
654
680
|
|
|
655
681
|
multiHavingIn(statement) {
|
|
656
|
-
|
|
657
|
-
sql = `(${columnize_(
|
|
658
|
-
statement.column,
|
|
659
|
-
this.builder,
|
|
660
|
-
this.client,
|
|
661
|
-
this.bindingsHolder
|
|
662
|
-
)}) `;
|
|
663
|
-
sql += this._not(statement, 'in ') + '((';
|
|
664
|
-
while (++i < statement.value.length) {
|
|
665
|
-
if (i !== 0) sql += '),(';
|
|
666
|
-
sql += this.client.parameterize(
|
|
667
|
-
statement.value[i],
|
|
668
|
-
undefined,
|
|
669
|
-
this.builder,
|
|
670
|
-
this.bindingsHolder
|
|
671
|
-
);
|
|
672
|
-
}
|
|
673
|
-
return sql + '))';
|
|
682
|
+
return this.multiOnIn(statement);
|
|
674
683
|
}
|
|
675
684
|
|
|
676
685
|
// Compile the "union" queries attached to the main query.
|
|
@@ -702,26 +711,44 @@ class QueryCompiler {
|
|
|
702
711
|
// If we haven't specified any columns or a `tableName`, we're assuming this
|
|
703
712
|
// is only being used for unions.
|
|
704
713
|
onlyUnions() {
|
|
705
|
-
return
|
|
714
|
+
return (
|
|
715
|
+
(!this.grouped.columns || !!this.grouped.columns[0].value) &&
|
|
716
|
+
this.grouped.union &&
|
|
717
|
+
!this.tableName
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
_getValueOrParameterFromAttribute(attribute, rawValue) {
|
|
722
|
+
if (this.single.skipBinding[attribute] === true) {
|
|
723
|
+
return rawValue !== undefined && rawValue !== null
|
|
724
|
+
? rawValue
|
|
725
|
+
: this.single[attribute];
|
|
726
|
+
}
|
|
727
|
+
return this.client.parameter(
|
|
728
|
+
this.single[attribute],
|
|
729
|
+
this.builder,
|
|
730
|
+
this.bindingsHolder
|
|
731
|
+
);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
onlyJson() {
|
|
735
|
+
return (
|
|
736
|
+
!this.tableName &&
|
|
737
|
+
this.grouped.columns &&
|
|
738
|
+
this.grouped.columns.length === 1 &&
|
|
739
|
+
this.grouped.columns[0].type === 'json'
|
|
740
|
+
);
|
|
706
741
|
}
|
|
707
742
|
|
|
708
743
|
limit() {
|
|
709
744
|
const noLimit = !this.single.limit && this.single.limit !== 0;
|
|
710
745
|
if (noLimit) return '';
|
|
711
|
-
return `limit ${this.
|
|
712
|
-
this.single.limit,
|
|
713
|
-
this.builder,
|
|
714
|
-
this.bindingsHolder
|
|
715
|
-
)}`;
|
|
746
|
+
return `limit ${this._getValueOrParameterFromAttribute('limit')}`;
|
|
716
747
|
}
|
|
717
748
|
|
|
718
749
|
offset() {
|
|
719
750
|
if (!this.single.offset) return '';
|
|
720
|
-
return `offset ${this.
|
|
721
|
-
this.single.offset,
|
|
722
|
-
this.builder,
|
|
723
|
-
this.bindingsHolder
|
|
724
|
-
)}`;
|
|
751
|
+
return `offset ${this._getValueOrParameterFromAttribute('offset')}`;
|
|
725
752
|
}
|
|
726
753
|
|
|
727
754
|
// Compiles a `delete` query.
|
|
@@ -882,7 +909,23 @@ class QueryCompiler {
|
|
|
882
909
|
// Where Clause
|
|
883
910
|
// ------
|
|
884
911
|
|
|
885
|
-
|
|
912
|
+
_valueClause(statement) {
|
|
913
|
+
return statement.asColumn
|
|
914
|
+
? wrap_(
|
|
915
|
+
statement.value,
|
|
916
|
+
undefined,
|
|
917
|
+
this.builder,
|
|
918
|
+
this.client,
|
|
919
|
+
this.bindingsHolder
|
|
920
|
+
)
|
|
921
|
+
: this.client.parameter(
|
|
922
|
+
statement.value,
|
|
923
|
+
this.builder,
|
|
924
|
+
this.bindingsHolder
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
_columnClause(statement) {
|
|
886
929
|
let columns;
|
|
887
930
|
if (Array.isArray(statement.column)) {
|
|
888
931
|
columns = `(${columnize_(
|
|
@@ -900,13 +943,33 @@ class QueryCompiler {
|
|
|
900
943
|
this.bindingsHolder
|
|
901
944
|
);
|
|
902
945
|
}
|
|
946
|
+
return columns;
|
|
947
|
+
}
|
|
903
948
|
|
|
949
|
+
whereIn(statement) {
|
|
904
950
|
const values = this.client.values(
|
|
905
951
|
statement.value,
|
|
906
952
|
this.builder,
|
|
907
953
|
this.bindingsHolder
|
|
908
954
|
);
|
|
909
|
-
return `${
|
|
955
|
+
return `${this._columnClause(statement)} ${this._not(
|
|
956
|
+
statement,
|
|
957
|
+
'in '
|
|
958
|
+
)}${values}`;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
whereLike(statement) {
|
|
962
|
+
return `${this._columnClause(statement)} ${this._not(
|
|
963
|
+
statement,
|
|
964
|
+
'like '
|
|
965
|
+
)}${this._valueClause(statement)}`;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
whereILike(statement) {
|
|
969
|
+
return `${this._columnClause(statement)} ${this._not(
|
|
970
|
+
statement,
|
|
971
|
+
'ilike '
|
|
972
|
+
)}${this._valueClause(statement)}`;
|
|
910
973
|
}
|
|
911
974
|
|
|
912
975
|
whereNull(statement) {
|
|
@@ -942,19 +1005,7 @@ class QueryCompiler {
|
|
|
942
1005
|
this.bindingsHolder
|
|
943
1006
|
) +
|
|
944
1007
|
' ' +
|
|
945
|
-
(statement
|
|
946
|
-
? wrap_(
|
|
947
|
-
statement.value,
|
|
948
|
-
undefined,
|
|
949
|
-
this.builder,
|
|
950
|
-
this.client,
|
|
951
|
-
this.bindingsHolder
|
|
952
|
-
)
|
|
953
|
-
: this.client.parameter(
|
|
954
|
-
statement.value,
|
|
955
|
-
this.builder,
|
|
956
|
-
this.bindingsHolder
|
|
957
|
-
))
|
|
1008
|
+
this._valueClause(statement)
|
|
958
1009
|
);
|
|
959
1010
|
}
|
|
960
1011
|
|
|
@@ -1018,11 +1069,33 @@ class QueryCompiler {
|
|
|
1018
1069
|
);
|
|
1019
1070
|
}
|
|
1020
1071
|
|
|
1072
|
+
_jsonWrapValue(jsonValue) {
|
|
1073
|
+
if (this.builder._isJsonObject(jsonValue)) {
|
|
1074
|
+
return JSON.stringify(jsonValue);
|
|
1075
|
+
}
|
|
1076
|
+
return jsonValue;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
_jsonValueClause(statement) {
|
|
1080
|
+
statement.value = this._jsonWrapValue(statement.value);
|
|
1081
|
+
return this._valueClause(statement);
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
whereJsonObject(statement) {
|
|
1085
|
+
return `${this._columnClause(statement)} ${
|
|
1086
|
+
statement.not ? '!=' : '='
|
|
1087
|
+
} ${this._jsonValueClause(statement)}`;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1021
1090
|
wrap(str) {
|
|
1022
1091
|
if (str.charAt(0) !== '(') return `(${str})`;
|
|
1023
1092
|
return str;
|
|
1024
1093
|
}
|
|
1025
1094
|
|
|
1095
|
+
json(stmt) {
|
|
1096
|
+
return this[stmt.method](stmt.params);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1026
1099
|
analytic(stmt) {
|
|
1027
1100
|
let sql = '';
|
|
1028
1101
|
const self = this;
|
|
@@ -1235,8 +1308,9 @@ class QueryCompiler {
|
|
|
1235
1308
|
nullOrder = ' is not null';
|
|
1236
1309
|
}
|
|
1237
1310
|
|
|
1311
|
+
let groupOrder;
|
|
1238
1312
|
if (value instanceof Raw) {
|
|
1239
|
-
|
|
1313
|
+
groupOrder = unwrapRaw_(
|
|
1240
1314
|
value,
|
|
1241
1315
|
undefined,
|
|
1242
1316
|
this.builder,
|
|
@@ -1244,10 +1318,25 @@ class QueryCompiler {
|
|
|
1244
1318
|
this.bindingsHolder
|
|
1245
1319
|
);
|
|
1246
1320
|
} else if (value instanceof QueryBuilder || nulls) {
|
|
1247
|
-
|
|
1321
|
+
groupOrder = '(' + formatter.columnize(value) + nullOrder + ')';
|
|
1248
1322
|
} else {
|
|
1249
|
-
|
|
1323
|
+
groupOrder = formatter.columnize(value);
|
|
1250
1324
|
}
|
|
1325
|
+
return groupOrder;
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
_groupOrder(item, type) {
|
|
1329
|
+
const column = this._formatGroupsItemValue(item.value, item.nulls);
|
|
1330
|
+
const direction =
|
|
1331
|
+
type === 'order' && item.type !== 'orderByRaw'
|
|
1332
|
+
? ` ${direction_(
|
|
1333
|
+
item.direction,
|
|
1334
|
+
this.builder,
|
|
1335
|
+
this.client,
|
|
1336
|
+
this.bindingsHolder
|
|
1337
|
+
)}`
|
|
1338
|
+
: '';
|
|
1339
|
+
return column + direction;
|
|
1251
1340
|
}
|
|
1252
1341
|
|
|
1253
1342
|
// Compiles the `order by` statements.
|
|
@@ -1255,17 +1344,7 @@ class QueryCompiler {
|
|
|
1255
1344
|
const items = this.grouped[type];
|
|
1256
1345
|
if (!items) return '';
|
|
1257
1346
|
const sql = items.map((item) => {
|
|
1258
|
-
|
|
1259
|
-
const direction =
|
|
1260
|
-
type === 'order' && item.type !== 'orderByRaw'
|
|
1261
|
-
? ` ${direction_(
|
|
1262
|
-
item.direction,
|
|
1263
|
-
this.builder,
|
|
1264
|
-
this.client,
|
|
1265
|
-
this.bindingsHolder
|
|
1266
|
-
)}`
|
|
1267
|
-
: '';
|
|
1268
|
-
return column + direction;
|
|
1347
|
+
return this._groupOrder(item, type);
|
|
1269
1348
|
});
|
|
1270
1349
|
return sql.length ? type + ' by ' + sql.join(', ') : '';
|
|
1271
1350
|
}
|
|
@@ -1301,6 +1380,112 @@ class QueryCompiler {
|
|
|
1301
1380
|
}
|
|
1302
1381
|
return this._tableName;
|
|
1303
1382
|
}
|
|
1383
|
+
|
|
1384
|
+
_jsonPathWrap(extraction) {
|
|
1385
|
+
return this.client.parameter(
|
|
1386
|
+
extraction.path || extraction[1],
|
|
1387
|
+
this.builder,
|
|
1388
|
+
this.bindingsHolder
|
|
1389
|
+
);
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
// Json common functions
|
|
1393
|
+
_jsonExtract(nameFunction, params) {
|
|
1394
|
+
let extractions;
|
|
1395
|
+
if (Array.isArray(params.column)) {
|
|
1396
|
+
extractions = params.column;
|
|
1397
|
+
} else {
|
|
1398
|
+
extractions = [params];
|
|
1399
|
+
}
|
|
1400
|
+
if (!Array.isArray(nameFunction)) {
|
|
1401
|
+
nameFunction = [nameFunction];
|
|
1402
|
+
}
|
|
1403
|
+
return extractions
|
|
1404
|
+
.map((extraction) => {
|
|
1405
|
+
let jsonCol = `${columnize_(
|
|
1406
|
+
extraction.column || extraction[0],
|
|
1407
|
+
this.builder,
|
|
1408
|
+
this.client,
|
|
1409
|
+
this.bindingsHolder
|
|
1410
|
+
)}, ${this._jsonPathWrap(extraction)}`;
|
|
1411
|
+
nameFunction.forEach((f) => {
|
|
1412
|
+
jsonCol = f + '(' + jsonCol + ')';
|
|
1413
|
+
});
|
|
1414
|
+
const alias = extraction.alias || extraction[2];
|
|
1415
|
+
return alias
|
|
1416
|
+
? this.client.alias(jsonCol, this.formatter.wrap(alias))
|
|
1417
|
+
: jsonCol;
|
|
1418
|
+
})
|
|
1419
|
+
.join(', ');
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
_jsonSet(nameFunction, params) {
|
|
1423
|
+
const jsonSet = `${nameFunction}(${columnize_(
|
|
1424
|
+
params.column,
|
|
1425
|
+
this.builder,
|
|
1426
|
+
this.client,
|
|
1427
|
+
this.bindingsHolder
|
|
1428
|
+
)}, ${this.client.parameter(
|
|
1429
|
+
params.path,
|
|
1430
|
+
this.builder,
|
|
1431
|
+
this.bindingsHolder
|
|
1432
|
+
)}, ${this.client.parameter(
|
|
1433
|
+
params.value,
|
|
1434
|
+
this.builder,
|
|
1435
|
+
this.bindingsHolder
|
|
1436
|
+
)})`;
|
|
1437
|
+
return params.alias
|
|
1438
|
+
? this.client.alias(jsonSet, this.formatter.wrap(params.alias))
|
|
1439
|
+
: jsonSet;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
_whereJsonPath(nameFunction, statement) {
|
|
1443
|
+
return `${nameFunction}(${this._columnClause(
|
|
1444
|
+
statement
|
|
1445
|
+
)}, ${this._jsonPathWrap({ path: statement.jsonPath })}) ${operator_(
|
|
1446
|
+
statement.operator,
|
|
1447
|
+
this.builder,
|
|
1448
|
+
this.client,
|
|
1449
|
+
this.bindingsHolder
|
|
1450
|
+
)} ${this._jsonValueClause(statement)}`;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
_onJsonPathEquals(nameJoinFunction, clause) {
|
|
1454
|
+
return (
|
|
1455
|
+
nameJoinFunction +
|
|
1456
|
+
'(' +
|
|
1457
|
+
wrap_(
|
|
1458
|
+
clause.columnFirst,
|
|
1459
|
+
undefined,
|
|
1460
|
+
this.builder,
|
|
1461
|
+
this.client,
|
|
1462
|
+
this.bindingsHolder
|
|
1463
|
+
) +
|
|
1464
|
+
', ' +
|
|
1465
|
+
this.client.parameter(
|
|
1466
|
+
clause.jsonPathFirst,
|
|
1467
|
+
this.builder,
|
|
1468
|
+
this.bindingsHolder
|
|
1469
|
+
) +
|
|
1470
|
+
') = ' +
|
|
1471
|
+
nameJoinFunction +
|
|
1472
|
+
'(' +
|
|
1473
|
+
wrap_(
|
|
1474
|
+
clause.columnSecond,
|
|
1475
|
+
undefined,
|
|
1476
|
+
this.builder,
|
|
1477
|
+
this.client,
|
|
1478
|
+
this.bindingsHolder
|
|
1479
|
+
) +
|
|
1480
|
+
', ' +
|
|
1481
|
+
this.client.parameter(
|
|
1482
|
+
clause.jsonPathSecond,
|
|
1483
|
+
this.builder,
|
|
1484
|
+
this.bindingsHolder
|
|
1485
|
+
) +
|
|
1486
|
+
')'
|
|
1487
|
+
);
|
|
1488
|
+
}
|
|
1304
1489
|
}
|
|
1305
1490
|
|
|
1306
1491
|
module.exports = QueryCompiler;
|
|
@@ -9,6 +9,7 @@ const has = require('lodash/has');
|
|
|
9
9
|
const tail = require('lodash/tail');
|
|
10
10
|
const { toNumber } = require('../util/helpers');
|
|
11
11
|
const { formatDefault } = require('../formatter/formatterUtils');
|
|
12
|
+
const { operator: operator_ } = require('../formatter/wrappingFormatter');
|
|
12
13
|
|
|
13
14
|
class ColumnCompiler {
|
|
14
15
|
constructor(client, tableCompiler, columnBuilder) {
|
|
@@ -29,6 +30,21 @@ class ColumnCompiler {
|
|
|
29
30
|
|
|
30
31
|
this.sequence = [];
|
|
31
32
|
this.modifiers = [];
|
|
33
|
+
|
|
34
|
+
this.checksCount = 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_addCheckModifiers() {
|
|
38
|
+
this.modifiers.push(
|
|
39
|
+
'check',
|
|
40
|
+
'checkPositive',
|
|
41
|
+
'checkNegative',
|
|
42
|
+
'checkIn',
|
|
43
|
+
'checkNotIn',
|
|
44
|
+
'checkBetween',
|
|
45
|
+
'checkLength',
|
|
46
|
+
'checkRegex'
|
|
47
|
+
);
|
|
32
48
|
}
|
|
33
49
|
|
|
34
50
|
defaults(label) {
|
|
@@ -134,6 +150,120 @@ class ColumnCompiler {
|
|
|
134
150
|
defaultTo(value) {
|
|
135
151
|
return `default ${formatDefault(value, this.type, this.client)}`;
|
|
136
152
|
}
|
|
153
|
+
|
|
154
|
+
increments(options = { primaryKey: true }) {
|
|
155
|
+
return (
|
|
156
|
+
'integer not null' +
|
|
157
|
+
(this.tableCompiler._canBeAddPrimaryKey(options) ? ' primary key' : '') +
|
|
158
|
+
' autoincrement'
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
bigincrements(options = { primaryKey: true }) {
|
|
163
|
+
return this.increments(options);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
_pushAlterCheckQuery(checkPredicate, constraintName) {
|
|
167
|
+
let checkName = constraintName;
|
|
168
|
+
if (!checkName) {
|
|
169
|
+
this.checksCount++;
|
|
170
|
+
checkName =
|
|
171
|
+
this.tableCompiler.tableNameRaw +
|
|
172
|
+
'_' +
|
|
173
|
+
this.getColumnName() +
|
|
174
|
+
'_' +
|
|
175
|
+
this.checksCount;
|
|
176
|
+
}
|
|
177
|
+
this.pushAdditional(function () {
|
|
178
|
+
this.pushQuery(
|
|
179
|
+
`alter table ${this.tableCompiler.tableName()} add constraint ${checkName} check(${checkPredicate})`
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
_checkConstraintName(constraintName) {
|
|
185
|
+
return constraintName ? `constraint ${constraintName} ` : '';
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
_check(checkPredicate, constraintName) {
|
|
189
|
+
if (this.columnBuilder._method === 'alter') {
|
|
190
|
+
this._pushAlterCheckQuery(checkPredicate, constraintName);
|
|
191
|
+
return '';
|
|
192
|
+
}
|
|
193
|
+
return `${this._checkConstraintName(
|
|
194
|
+
constraintName
|
|
195
|
+
)}check (${checkPredicate})`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
checkPositive(constraintName) {
|
|
199
|
+
return this._check(
|
|
200
|
+
`${this.formatter.wrap(this.getColumnName())} ${operator_(
|
|
201
|
+
'>',
|
|
202
|
+
this.columnBuilder,
|
|
203
|
+
this.bindingsHolder
|
|
204
|
+
)} 0`,
|
|
205
|
+
constraintName
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
checkNegative(constraintName) {
|
|
210
|
+
return this._check(
|
|
211
|
+
`${this.formatter.wrap(this.getColumnName())} ${operator_(
|
|
212
|
+
'<',
|
|
213
|
+
this.columnBuilder,
|
|
214
|
+
this.bindingsHolder
|
|
215
|
+
)} 0`,
|
|
216
|
+
constraintName
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
_checkIn(values, constraintName, not) {
|
|
221
|
+
return this._check(
|
|
222
|
+
`${this.formatter.wrap(this.getColumnName())} ${
|
|
223
|
+
not ? 'not ' : ''
|
|
224
|
+
}in (${values.map((v) => this.client._escapeBinding(v)).join(',')})`,
|
|
225
|
+
constraintName
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
checkIn(values, constraintName) {
|
|
230
|
+
return this._checkIn(values, constraintName);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
checkNotIn(values, constraintName) {
|
|
234
|
+
return this._checkIn(values, constraintName, true);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
checkBetween(intervals, constraintName) {
|
|
238
|
+
if (
|
|
239
|
+
intervals.length === 2 &&
|
|
240
|
+
!Array.isArray(intervals[0]) &&
|
|
241
|
+
!Array.isArray(intervals[1])
|
|
242
|
+
) {
|
|
243
|
+
intervals = [intervals];
|
|
244
|
+
}
|
|
245
|
+
const intervalChecks = intervals
|
|
246
|
+
.map((interval) => {
|
|
247
|
+
return `${this.formatter.wrap(
|
|
248
|
+
this.getColumnName()
|
|
249
|
+
)} between ${this.client._escapeBinding(
|
|
250
|
+
interval[0]
|
|
251
|
+
)} and ${this.client._escapeBinding(interval[1])}`;
|
|
252
|
+
})
|
|
253
|
+
.join(' or ');
|
|
254
|
+
return this._check(intervalChecks, constraintName);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
checkLength(operator, length, constraintName) {
|
|
258
|
+
return this._check(
|
|
259
|
+
`length(${this.formatter.wrap(this.getColumnName())}) ${operator_(
|
|
260
|
+
operator,
|
|
261
|
+
this.columnBuilder,
|
|
262
|
+
this.bindingsHolder
|
|
263
|
+
)} ${toNumber(length)}`,
|
|
264
|
+
constraintName
|
|
265
|
+
);
|
|
266
|
+
}
|
|
137
267
|
}
|
|
138
268
|
|
|
139
269
|
ColumnCompiler.prototype.binary = 'blob';
|
|
@@ -147,11 +277,8 @@ ColumnCompiler.prototype.geography = 'geography';
|
|
|
147
277
|
ColumnCompiler.prototype.point = 'point';
|
|
148
278
|
ColumnCompiler.prototype.enu = 'varchar';
|
|
149
279
|
ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
|
|
150
|
-
ColumnCompiler.prototype.uuid =
|
|
151
|
-
|
|
152
|
-
'integer not null' + (primaryKey ? ' primary key' : '') + ' autoincrement';
|
|
153
|
-
ColumnCompiler.prototype.bigincrements = ({ primaryKey = true } = {}) =>
|
|
154
|
-
'integer not null' + (primaryKey ? ' primary key' : '') + ' autoincrement';
|
|
280
|
+
ColumnCompiler.prototype.uuid = ({ useBinaryUuid = false } = {}) =>
|
|
281
|
+
useBinaryUuid ? 'binary(16)' : 'char(36)';
|
|
155
282
|
ColumnCompiler.prototype.integer =
|
|
156
283
|
ColumnCompiler.prototype.smallint =
|
|
157
284
|
ColumnCompiler.prototype.mediumint =
|