knex 2.4.2 → 2.5.0
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 +54 -18
- package/CONTRIBUTING.md +194 -194
- package/LICENSE +22 -22
- package/README.md +147 -148
- package/UPGRADING.md +233 -233
- package/bin/cli.js +473 -473
- package/bin/utils/cli-config-utils.js +210 -210
- package/bin/utils/constants.js +7 -7
- package/bin/utils/migrationsLister.js +37 -37
- package/knex.js +23 -23
- package/knex.mjs +11 -0
- package/lib/builder-interface-augmenter.js +120 -120
- package/lib/client.js +495 -475
- package/lib/constants.js +61 -61
- package/lib/dialects/better-sqlite3/index.js +77 -72
- package/lib/dialects/cockroachdb/crdb-columncompiler.js +14 -14
- package/lib/dialects/cockroachdb/crdb-querybuilder.js +11 -11
- package/lib/dialects/cockroachdb/crdb-querycompiler.js +122 -122
- package/lib/dialects/cockroachdb/crdb-tablecompiler.js +37 -37
- package/lib/dialects/cockroachdb/crdb-viewcompiler.js +15 -15
- package/lib/dialects/cockroachdb/index.js +86 -86
- package/lib/dialects/index.js +33 -33
- package/lib/dialects/mssql/index.js +500 -495
- package/lib/dialects/mssql/mssql-formatter.js +34 -34
- package/lib/dialects/mssql/query/mssql-querycompiler.js +601 -600
- package/lib/dialects/mssql/schema/mssql-columncompiler.js +185 -185
- package/lib/dialects/mssql/schema/mssql-compiler.js +91 -91
- package/lib/dialects/mssql/schema/mssql-tablecompiler.js +378 -378
- package/lib/dialects/mssql/schema/mssql-viewcompiler.js +55 -55
- package/lib/dialects/mssql/transaction.js +176 -176
- package/lib/dialects/mysql/index.js +201 -201
- package/lib/dialects/mysql/query/mysql-querycompiler.js +274 -274
- package/lib/dialects/mysql/schema/mysql-columncompiler.js +193 -193
- package/lib/dialects/mysql/schema/mysql-compiler.js +60 -60
- package/lib/dialects/mysql/schema/mysql-tablecompiler.js +381 -381
- package/lib/dialects/mysql/schema/mysql-viewbuilder.js +21 -21
- package/lib/dialects/mysql/schema/mysql-viewcompiler.js +15 -15
- package/lib/dialects/mysql/transaction.js +46 -46
- package/lib/dialects/mysql2/index.js +53 -33
- package/lib/dialects/mysql2/transaction.js +44 -44
- package/lib/dialects/oracle/DEAD_CODE.md +5 -5
- package/lib/dialects/oracle/index.js +92 -92
- package/lib/dialects/oracle/query/oracle-querycompiler.js +343 -342
- package/lib/dialects/oracle/schema/internal/incrementUtils.js +20 -20
- package/lib/dialects/oracle/schema/internal/trigger.js +135 -135
- package/lib/dialects/oracle/schema/oracle-columnbuilder.js +17 -17
- package/lib/dialects/oracle/schema/oracle-columncompiler.js +126 -126
- package/lib/dialects/oracle/schema/oracle-compiler.js +122 -122
- package/lib/dialects/oracle/schema/oracle-tablecompiler.js +190 -190
- package/lib/dialects/oracle/utils.js +87 -87
- package/lib/dialects/oracledb/index.js +327 -327
- package/lib/dialects/oracledb/query/oracledb-querycompiler.js +481 -481
- package/lib/dialects/oracledb/schema/oracledb-columncompiler.js +61 -55
- package/lib/dialects/oracledb/schema/oracledb-tablecompiler.js +19 -19
- package/lib/dialects/oracledb/schema/oracledb-viewbuilder.js +13 -13
- package/lib/dialects/oracledb/schema/oracledb-viewcompiler.js +19 -19
- package/lib/dialects/oracledb/transaction.js +98 -98
- package/lib/dialects/oracledb/utils.js +208 -208
- package/lib/dialects/pgnative/index.js +60 -60
- package/lib/dialects/postgres/execution/pg-transaction.js +19 -12
- package/lib/dialects/postgres/index.js +358 -358
- package/lib/dialects/postgres/query/pg-querybuilder.js +43 -38
- package/lib/dialects/postgres/query/pg-querycompiler.js +400 -395
- package/lib/dialects/postgres/schema/pg-columncompiler.js +156 -156
- package/lib/dialects/postgres/schema/pg-compiler.js +138 -138
- package/lib/dialects/postgres/schema/pg-tablecompiler.js +304 -299
- package/lib/dialects/postgres/schema/pg-viewbuilder.js +21 -21
- package/lib/dialects/postgres/schema/pg-viewcompiler.js +35 -35
- package/lib/dialects/redshift/index.js +86 -86
- package/lib/dialects/redshift/query/redshift-querycompiler.js +163 -163
- package/lib/dialects/redshift/schema/redshift-columnbuilder.js +22 -22
- package/lib/dialects/redshift/schema/redshift-columncompiler.js +67 -67
- package/lib/dialects/redshift/schema/redshift-compiler.js +14 -14
- package/lib/dialects/redshift/schema/redshift-tablecompiler.js +122 -122
- package/lib/dialects/redshift/schema/redshift-viewcompiler.js +11 -11
- package/lib/dialects/redshift/transaction.js +32 -25
- package/lib/dialects/sqlite3/execution/sqlite-transaction.js +25 -18
- package/lib/dialects/sqlite3/index.js +250 -250
- package/lib/dialects/sqlite3/query/sqlite-querybuilder.js +33 -33
- package/lib/dialects/sqlite3/query/sqlite-querycompiler.js +334 -334
- package/lib/dialects/sqlite3/schema/ddl.js +400 -400
- package/lib/dialects/sqlite3/schema/internal/compiler.js +327 -327
- package/lib/dialects/sqlite3/schema/internal/parser-combinator.js +161 -161
- package/lib/dialects/sqlite3/schema/internal/parser.js +638 -638
- package/lib/dialects/sqlite3/schema/internal/sqlite-ddl-operations.js +41 -41
- package/lib/dialects/sqlite3/schema/internal/tokenizer.js +38 -38
- package/lib/dialects/sqlite3/schema/internal/utils.js +12 -12
- package/lib/dialects/sqlite3/schema/sqlite-columncompiler.js +50 -50
- package/lib/dialects/sqlite3/schema/sqlite-compiler.js +80 -80
- package/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js +347 -347
- package/lib/dialects/sqlite3/schema/sqlite-viewcompiler.js +40 -40
- package/lib/execution/batch-insert.js +51 -51
- package/lib/execution/internal/delay.js +6 -6
- package/lib/execution/internal/ensure-connection-callback.js +41 -41
- package/lib/execution/internal/query-executioner.js +62 -62
- package/lib/execution/runner.js +325 -307
- package/lib/execution/transaction.js +409 -401
- package/lib/formatter/formatterUtils.js +42 -42
- package/lib/formatter/rawFormatter.js +84 -84
- package/lib/formatter/wrappingFormatter.js +250 -250
- package/lib/formatter.js +25 -25
- package/lib/index.js +3 -3
- package/lib/knex-builder/FunctionHelper.js +80 -54
- package/lib/knex-builder/Knex.js +59 -59
- package/lib/knex-builder/internal/config-resolver.js +57 -57
- package/lib/knex-builder/internal/parse-connection.js +87 -87
- package/lib/knex-builder/make-knex.js +345 -340
- package/lib/logger.js +76 -76
- package/lib/migrations/common/MigrationsLoader.js +36 -36
- package/lib/migrations/migrate/MigrationGenerator.js +84 -84
- package/lib/migrations/migrate/Migrator.js +598 -598
- package/lib/migrations/migrate/migrate-stub.js +17 -17
- package/lib/migrations/migrate/migration-list-resolver.js +33 -33
- package/lib/migrations/migrate/migrator-configuration-merger.js +58 -58
- package/lib/migrations/migrate/sources/fs-migrations.js +74 -74
- package/lib/migrations/migrate/stub/cjs.stub +15 -15
- package/lib/migrations/migrate/stub/coffee.stub +13 -13
- package/lib/migrations/migrate/stub/eg.stub +14 -14
- package/lib/migrations/migrate/stub/js-schema.stub +22 -22
- package/lib/migrations/migrate/stub/js.stub +22 -22
- package/lib/migrations/migrate/stub/knexfile-coffee.stub +34 -34
- package/lib/migrations/migrate/stub/knexfile-eg.stub +43 -43
- package/lib/migrations/migrate/stub/knexfile-js.stub +47 -47
- package/lib/migrations/migrate/stub/knexfile-ls.stub +35 -35
- package/lib/migrations/migrate/stub/knexfile-ts.stub +47 -47
- package/lib/migrations/migrate/stub/ls.stub +14 -14
- package/lib/migrations/migrate/stub/mjs.stub +23 -23
- package/lib/migrations/migrate/stub/ts-schema.stub +21 -21
- package/lib/migrations/migrate/stub/ts.stub +21 -21
- package/lib/migrations/migrate/table-creator.js +77 -77
- package/lib/migrations/migrate/table-resolver.js +27 -27
- package/lib/migrations/seed/Seeder.js +137 -137
- package/lib/migrations/seed/seed-stub.js +13 -13
- package/lib/migrations/seed/seeder-configuration-merger.js +60 -60
- package/lib/migrations/seed/sources/fs-seeds.js +65 -65
- package/lib/migrations/seed/stub/coffee.stub +9 -9
- package/lib/migrations/seed/stub/eg.stub +11 -11
- package/lib/migrations/seed/stub/js.stub +13 -13
- package/lib/migrations/seed/stub/ls.stub +11 -11
- package/lib/migrations/seed/stub/mjs.stub +12 -12
- package/lib/migrations/seed/stub/ts.stub +13 -13
- package/lib/migrations/util/fs.js +86 -86
- package/lib/migrations/util/import-file.js +12 -12
- package/lib/migrations/util/is-module-type.js +9 -9
- package/lib/migrations/util/template.js +52 -52
- package/lib/migrations/util/timestamp.js +14 -14
- package/lib/query/analytic.js +52 -52
- package/lib/query/constants.js +15 -15
- package/lib/query/joinclause.js +270 -270
- package/lib/query/method-constants.js +136 -135
- package/lib/query/querybuilder.js +1793 -1794
- package/lib/query/querycompiler.js +1591 -1580
- package/lib/raw.js +139 -139
- package/lib/ref.js +39 -39
- package/lib/schema/builder.js +115 -115
- package/lib/schema/columnbuilder.js +146 -146
- package/lib/schema/columncompiler.js +307 -307
- package/lib/schema/compiler.js +187 -187
- package/lib/schema/internal/helpers.js +55 -55
- package/lib/schema/tablebuilder.js +376 -376
- package/lib/schema/tablecompiler.js +433 -433
- package/lib/schema/viewbuilder.js +92 -92
- package/lib/schema/viewcompiler.js +138 -138
- package/lib/util/finally-mixin.js +13 -13
- package/lib/util/helpers.js +95 -95
- package/lib/util/is.js +32 -32
- package/lib/util/nanoid.js +40 -40
- package/lib/util/noop.js +1 -1
- package/lib/util/save-async-stack.js +14 -14
- package/lib/util/security.js +26 -0
- package/lib/util/string.js +190 -190
- package/lib/util/timeout.js +29 -29
- package/package.json +12 -10
- package/scripts/build.js +125 -125
- package/scripts/clean.js +31 -31
- package/scripts/docker-compose.yml +152 -152
- package/scripts/next-release-howto.md +24 -24
- package/scripts/oracledb-install-driver-libs.sh +82 -82
- package/scripts/release.sh +34 -34
- package/scripts/runkit-example.js +34 -34
- package/scripts/stress-test/README.txt +18 -18
- package/scripts/stress-test/docker-compose.yml +57 -57
- package/scripts/stress-test/knex-stress-test.js +208 -208
- package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
- package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
- package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
- package/scripts/update_gitignore_for_tsc_output.js +90 -90
- package/types/index.d.ts +3273 -3233
- package/types/result.d.ts +27 -27
- package/types/tables.d.ts +4 -4
|
@@ -1,327 +1,327 @@
|
|
|
1
|
-
function compileCreateTable(ast, wrap = (v) => v) {
|
|
2
|
-
return createTable(ast, wrap);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
function compileCreateIndex(ast, wrap = (v) => v) {
|
|
6
|
-
return createIndex(ast, wrap);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function createTable(ast, wrap) {
|
|
10
|
-
return `CREATE${temporary(ast, wrap)} TABLE${exists(ast, wrap)} ${schema(
|
|
11
|
-
ast,
|
|
12
|
-
wrap
|
|
13
|
-
)}${table(ast, wrap)} (${columnDefinitionList(
|
|
14
|
-
ast,
|
|
15
|
-
wrap
|
|
16
|
-
)}${tableConstraintList(ast, wrap)})${rowid(ast, wrap)}`;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function temporary(ast, wrap) {
|
|
20
|
-
return ast.temporary ? ' TEMP' : '';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function rowid(ast, wrap) {
|
|
24
|
-
return ast.rowid ? ' WITHOUT ROWID' : '';
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function columnDefinitionList(ast, wrap) {
|
|
28
|
-
return ast.columns.map((column) => columnDefinition(column, wrap)).join(', ');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function columnDefinition(ast, wrap) {
|
|
32
|
-
return `${identifier(ast.name, wrap)}${typeName(
|
|
33
|
-
ast,
|
|
34
|
-
wrap
|
|
35
|
-
)}${columnConstraintList(ast.constraints, wrap)}`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function typeName(ast, wrap) {
|
|
39
|
-
return ast.type !== null ? ` ${ast.type}` : '';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function columnConstraintList(ast, wrap) {
|
|
43
|
-
return `${primaryColumnConstraint(ast, wrap)}${notnullColumnConstraint(
|
|
44
|
-
ast,
|
|
45
|
-
wrap
|
|
46
|
-
)}${nullColumnConstraint(ast, wrap)}${uniqueColumnConstraint(
|
|
47
|
-
ast,
|
|
48
|
-
wrap
|
|
49
|
-
)}${checkColumnConstraint(ast, wrap)}${defaultColumnConstraint(
|
|
50
|
-
ast,
|
|
51
|
-
wrap
|
|
52
|
-
)}${collateColumnConstraint(ast, wrap)}${referencesColumnConstraint(
|
|
53
|
-
ast,
|
|
54
|
-
wrap
|
|
55
|
-
)}${asColumnConstraint(ast, wrap)}`;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function primaryColumnConstraint(ast, wrap) {
|
|
59
|
-
return ast.primary !== null
|
|
60
|
-
? ` ${constraintName(ast.primary, wrap)}PRIMARY KEY${order(
|
|
61
|
-
ast.primary,
|
|
62
|
-
wrap
|
|
63
|
-
)}${conflictClause(ast.primary, wrap)}${autoincrement(ast.primary, wrap)}`
|
|
64
|
-
: '';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function autoincrement(ast, wrap) {
|
|
68
|
-
return ast.autoincrement ? ' AUTOINCREMENT' : '';
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function notnullColumnConstraint(ast, wrap) {
|
|
72
|
-
return ast.notnull !== null
|
|
73
|
-
? ` ${constraintName(ast.notnull, wrap)}NOT NULL${conflictClause(
|
|
74
|
-
ast.notnull,
|
|
75
|
-
wrap
|
|
76
|
-
)}`
|
|
77
|
-
: '';
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function nullColumnConstraint(ast, wrap) {
|
|
81
|
-
return ast.null !== null
|
|
82
|
-
? ` ${constraintName(ast.null, wrap)}NULL${conflictClause(ast.null, wrap)}`
|
|
83
|
-
: '';
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function uniqueColumnConstraint(ast, wrap) {
|
|
87
|
-
return ast.unique !== null
|
|
88
|
-
? ` ${constraintName(ast.unique, wrap)}UNIQUE${conflictClause(
|
|
89
|
-
ast.unique,
|
|
90
|
-
wrap
|
|
91
|
-
)}`
|
|
92
|
-
: '';
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function checkColumnConstraint(ast, wrap) {
|
|
96
|
-
return ast.check !== null
|
|
97
|
-
? ` ${constraintName(ast.check, wrap)}CHECK (${expression(
|
|
98
|
-
ast.check.expression,
|
|
99
|
-
wrap
|
|
100
|
-
)})`
|
|
101
|
-
: '';
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function defaultColumnConstraint(ast, wrap) {
|
|
105
|
-
return ast.default !== null
|
|
106
|
-
? ` ${constraintName(ast.default, wrap)}DEFAULT ${
|
|
107
|
-
!ast.default.expression
|
|
108
|
-
? ast.default.value
|
|
109
|
-
: `(${expression(ast.default.value, wrap)})`
|
|
110
|
-
}`
|
|
111
|
-
: '';
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function collateColumnConstraint(ast, wrap) {
|
|
115
|
-
return ast.collate !== null
|
|
116
|
-
? ` ${constraintName(ast.collate, wrap)}COLLATE ${ast.collate.collation}`
|
|
117
|
-
: '';
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function referencesColumnConstraint(ast, wrap) {
|
|
121
|
-
return ast.references !== null
|
|
122
|
-
? ` ${constraintName(ast.references, wrap)}${foreignKeyClause(
|
|
123
|
-
ast.references,
|
|
124
|
-
wrap
|
|
125
|
-
)}`
|
|
126
|
-
: '';
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function asColumnConstraint(ast, wrap) {
|
|
130
|
-
return ast.as !== null
|
|
131
|
-
? ` ${constraintName(ast.as, wrap)}${
|
|
132
|
-
ast.as.generated ? 'GENERATED ALWAYS ' : ''
|
|
133
|
-
}AS (${expression(ast.as.expression, wrap)})${
|
|
134
|
-
ast.as.mode !== null ? ` ${ast.as.mode}` : ''
|
|
135
|
-
}`
|
|
136
|
-
: '';
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function tableConstraintList(ast, wrap) {
|
|
140
|
-
return ast.constraints.reduce(
|
|
141
|
-
(constraintList, constraint) =>
|
|
142
|
-
`${constraintList}, ${tableConstraint(constraint, wrap)}`,
|
|
143
|
-
''
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function tableConstraint(ast, wrap) {
|
|
148
|
-
switch (ast.type) {
|
|
149
|
-
case 'PRIMARY KEY':
|
|
150
|
-
return primaryTableConstraint(ast, wrap);
|
|
151
|
-
case 'UNIQUE':
|
|
152
|
-
return uniqueTableConstraint(ast, wrap);
|
|
153
|
-
case 'CHECK':
|
|
154
|
-
return checkTableConstraint(ast, wrap);
|
|
155
|
-
case 'FOREIGN KEY':
|
|
156
|
-
return foreignTableConstraint(ast, wrap);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function primaryTableConstraint(ast, wrap) {
|
|
161
|
-
return `${constraintName(ast, wrap)}PRIMARY KEY (${indexedColumnList(
|
|
162
|
-
ast,
|
|
163
|
-
wrap
|
|
164
|
-
)})${conflictClause(ast, wrap)}`;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function uniqueTableConstraint(ast, wrap) {
|
|
168
|
-
return `${constraintName(ast, wrap)}UNIQUE (${indexedColumnList(
|
|
169
|
-
ast,
|
|
170
|
-
wrap
|
|
171
|
-
)})${conflictClause(ast, wrap)}`;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function conflictClause(ast, wrap) {
|
|
175
|
-
return ast.conflict !== null ? ` ON CONFLICT ${ast.conflict}` : '';
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function checkTableConstraint(ast, wrap) {
|
|
179
|
-
return `${constraintName(ast, wrap)}CHECK (${expression(
|
|
180
|
-
ast.expression,
|
|
181
|
-
wrap
|
|
182
|
-
)})`;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function foreignTableConstraint(ast, wrap) {
|
|
186
|
-
return `${constraintName(ast, wrap)}FOREIGN KEY (${columnNameList(
|
|
187
|
-
ast,
|
|
188
|
-
wrap
|
|
189
|
-
)}) ${foreignKeyClause(ast.references, wrap)}`;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function foreignKeyClause(ast, wrap) {
|
|
193
|
-
return `REFERENCES ${table(ast, wrap)}${columnNameListOptional(
|
|
194
|
-
ast,
|
|
195
|
-
wrap
|
|
196
|
-
)}${deleteUpdateMatchList(ast, wrap)}${deferrable(ast.deferrable, wrap)}`;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function columnNameListOptional(ast, wrap) {
|
|
200
|
-
return ast.columns.length > 0 ? ` (${columnNameList(ast, wrap)})` : '';
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function columnNameList(ast, wrap) {
|
|
204
|
-
return ast.columns.map((column) => identifier(column, wrap)).join(', ');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function deleteUpdateMatchList(ast, wrap) {
|
|
208
|
-
return `${deleteReference(ast, wrap)}${updateReference(
|
|
209
|
-
ast,
|
|
210
|
-
wrap
|
|
211
|
-
)}${matchReference(ast, wrap)}`;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function deleteReference(ast, wrap) {
|
|
215
|
-
return ast.delete !== null ? ` ON DELETE ${ast.delete}` : '';
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function updateReference(ast, wrap) {
|
|
219
|
-
return ast.update !== null ? ` ON UPDATE ${ast.update}` : '';
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function matchReference(ast, wrap) {
|
|
223
|
-
return ast.match !== null ? ` MATCH ${ast.match}` : '';
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function deferrable(ast, wrap) {
|
|
227
|
-
return ast !== null
|
|
228
|
-
? ` ${ast.not ? 'NOT ' : ''}DEFERRABLE${
|
|
229
|
-
ast.initially !== null ? ` INITIALLY ${ast.initially}` : ''
|
|
230
|
-
}`
|
|
231
|
-
: '';
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function constraintName(ast, wrap) {
|
|
235
|
-
return ast.name !== null ? `CONSTRAINT ${identifier(ast.name, wrap)} ` : '';
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function createIndex(ast, wrap) {
|
|
239
|
-
return `CREATE${unique(ast, wrap)} INDEX${exists(ast, wrap)} ${schema(
|
|
240
|
-
ast,
|
|
241
|
-
wrap
|
|
242
|
-
)}${index(ast, wrap)} on ${table(ast, wrap)} (${indexedColumnList(
|
|
243
|
-
ast,
|
|
244
|
-
wrap
|
|
245
|
-
)})${where(ast, wrap)}`;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function unique(ast, wrap) {
|
|
249
|
-
return ast.unique ? ' UNIQUE' : '';
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function exists(ast, wrap) {
|
|
253
|
-
return ast.exists ? ' IF NOT EXISTS' : '';
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function schema(ast, wrap) {
|
|
257
|
-
return ast.schema !== null ? `${identifier(ast.schema, wrap)}.` : '';
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function index(ast, wrap) {
|
|
261
|
-
return identifier(ast.index, wrap);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function table(ast, wrap) {
|
|
265
|
-
return identifier(ast.table, wrap);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function where(ast, wrap) {
|
|
269
|
-
return ast.where !== null ? ` where ${expression(ast.where)}` : '';
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function indexedColumnList(ast, wrap) {
|
|
273
|
-
return ast.columns
|
|
274
|
-
.map((column) =>
|
|
275
|
-
!column.expression
|
|
276
|
-
? indexedColumn(column, wrap)
|
|
277
|
-
: indexedColumnExpression(column, wrap)
|
|
278
|
-
)
|
|
279
|
-
.join(', ');
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function indexedColumn(ast, wrap) {
|
|
283
|
-
return `${identifier(ast.name, wrap)}${collation(ast, wrap)}${order(
|
|
284
|
-
ast,
|
|
285
|
-
wrap
|
|
286
|
-
)}`;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function indexedColumnExpression(ast, wrap) {
|
|
290
|
-
return `${indexedExpression(ast.name, wrap)}${collation(ast, wrap)}${order(
|
|
291
|
-
ast,
|
|
292
|
-
wrap
|
|
293
|
-
)}`;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function collation(ast, wrap) {
|
|
297
|
-
return ast.collation !== null ? ` COLLATE ${ast.collation}` : '';
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
function order(ast, wrap) {
|
|
301
|
-
return ast.order !== null ? ` ${ast.order}` : '';
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function indexedExpression(ast, wrap) {
|
|
305
|
-
return expression(ast, wrap);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
function expression(ast, wrap) {
|
|
309
|
-
return ast.reduce(
|
|
310
|
-
(expr, e) =>
|
|
311
|
-
Array.isArray(e)
|
|
312
|
-
? `${expr}(${expression(e)})`
|
|
313
|
-
: !expr
|
|
314
|
-
? e
|
|
315
|
-
: `${expr} ${e}`,
|
|
316
|
-
''
|
|
317
|
-
);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
function identifier(ast, wrap) {
|
|
321
|
-
return wrap(ast);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
module.exports = {
|
|
325
|
-
compileCreateTable,
|
|
326
|
-
compileCreateIndex,
|
|
327
|
-
};
|
|
1
|
+
function compileCreateTable(ast, wrap = (v) => v) {
|
|
2
|
+
return createTable(ast, wrap);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function compileCreateIndex(ast, wrap = (v) => v) {
|
|
6
|
+
return createIndex(ast, wrap);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function createTable(ast, wrap) {
|
|
10
|
+
return `CREATE${temporary(ast, wrap)} TABLE${exists(ast, wrap)} ${schema(
|
|
11
|
+
ast,
|
|
12
|
+
wrap
|
|
13
|
+
)}${table(ast, wrap)} (${columnDefinitionList(
|
|
14
|
+
ast,
|
|
15
|
+
wrap
|
|
16
|
+
)}${tableConstraintList(ast, wrap)})${rowid(ast, wrap)}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function temporary(ast, wrap) {
|
|
20
|
+
return ast.temporary ? ' TEMP' : '';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function rowid(ast, wrap) {
|
|
24
|
+
return ast.rowid ? ' WITHOUT ROWID' : '';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function columnDefinitionList(ast, wrap) {
|
|
28
|
+
return ast.columns.map((column) => columnDefinition(column, wrap)).join(', ');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function columnDefinition(ast, wrap) {
|
|
32
|
+
return `${identifier(ast.name, wrap)}${typeName(
|
|
33
|
+
ast,
|
|
34
|
+
wrap
|
|
35
|
+
)}${columnConstraintList(ast.constraints, wrap)}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function typeName(ast, wrap) {
|
|
39
|
+
return ast.type !== null ? ` ${ast.type}` : '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function columnConstraintList(ast, wrap) {
|
|
43
|
+
return `${primaryColumnConstraint(ast, wrap)}${notnullColumnConstraint(
|
|
44
|
+
ast,
|
|
45
|
+
wrap
|
|
46
|
+
)}${nullColumnConstraint(ast, wrap)}${uniqueColumnConstraint(
|
|
47
|
+
ast,
|
|
48
|
+
wrap
|
|
49
|
+
)}${checkColumnConstraint(ast, wrap)}${defaultColumnConstraint(
|
|
50
|
+
ast,
|
|
51
|
+
wrap
|
|
52
|
+
)}${collateColumnConstraint(ast, wrap)}${referencesColumnConstraint(
|
|
53
|
+
ast,
|
|
54
|
+
wrap
|
|
55
|
+
)}${asColumnConstraint(ast, wrap)}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function primaryColumnConstraint(ast, wrap) {
|
|
59
|
+
return ast.primary !== null
|
|
60
|
+
? ` ${constraintName(ast.primary, wrap)}PRIMARY KEY${order(
|
|
61
|
+
ast.primary,
|
|
62
|
+
wrap
|
|
63
|
+
)}${conflictClause(ast.primary, wrap)}${autoincrement(ast.primary, wrap)}`
|
|
64
|
+
: '';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function autoincrement(ast, wrap) {
|
|
68
|
+
return ast.autoincrement ? ' AUTOINCREMENT' : '';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function notnullColumnConstraint(ast, wrap) {
|
|
72
|
+
return ast.notnull !== null
|
|
73
|
+
? ` ${constraintName(ast.notnull, wrap)}NOT NULL${conflictClause(
|
|
74
|
+
ast.notnull,
|
|
75
|
+
wrap
|
|
76
|
+
)}`
|
|
77
|
+
: '';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function nullColumnConstraint(ast, wrap) {
|
|
81
|
+
return ast.null !== null
|
|
82
|
+
? ` ${constraintName(ast.null, wrap)}NULL${conflictClause(ast.null, wrap)}`
|
|
83
|
+
: '';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function uniqueColumnConstraint(ast, wrap) {
|
|
87
|
+
return ast.unique !== null
|
|
88
|
+
? ` ${constraintName(ast.unique, wrap)}UNIQUE${conflictClause(
|
|
89
|
+
ast.unique,
|
|
90
|
+
wrap
|
|
91
|
+
)}`
|
|
92
|
+
: '';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function checkColumnConstraint(ast, wrap) {
|
|
96
|
+
return ast.check !== null
|
|
97
|
+
? ` ${constraintName(ast.check, wrap)}CHECK (${expression(
|
|
98
|
+
ast.check.expression,
|
|
99
|
+
wrap
|
|
100
|
+
)})`
|
|
101
|
+
: '';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function defaultColumnConstraint(ast, wrap) {
|
|
105
|
+
return ast.default !== null
|
|
106
|
+
? ` ${constraintName(ast.default, wrap)}DEFAULT ${
|
|
107
|
+
!ast.default.expression
|
|
108
|
+
? ast.default.value
|
|
109
|
+
: `(${expression(ast.default.value, wrap)})`
|
|
110
|
+
}`
|
|
111
|
+
: '';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function collateColumnConstraint(ast, wrap) {
|
|
115
|
+
return ast.collate !== null
|
|
116
|
+
? ` ${constraintName(ast.collate, wrap)}COLLATE ${ast.collate.collation}`
|
|
117
|
+
: '';
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function referencesColumnConstraint(ast, wrap) {
|
|
121
|
+
return ast.references !== null
|
|
122
|
+
? ` ${constraintName(ast.references, wrap)}${foreignKeyClause(
|
|
123
|
+
ast.references,
|
|
124
|
+
wrap
|
|
125
|
+
)}`
|
|
126
|
+
: '';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function asColumnConstraint(ast, wrap) {
|
|
130
|
+
return ast.as !== null
|
|
131
|
+
? ` ${constraintName(ast.as, wrap)}${
|
|
132
|
+
ast.as.generated ? 'GENERATED ALWAYS ' : ''
|
|
133
|
+
}AS (${expression(ast.as.expression, wrap)})${
|
|
134
|
+
ast.as.mode !== null ? ` ${ast.as.mode}` : ''
|
|
135
|
+
}`
|
|
136
|
+
: '';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function tableConstraintList(ast, wrap) {
|
|
140
|
+
return ast.constraints.reduce(
|
|
141
|
+
(constraintList, constraint) =>
|
|
142
|
+
`${constraintList}, ${tableConstraint(constraint, wrap)}`,
|
|
143
|
+
''
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function tableConstraint(ast, wrap) {
|
|
148
|
+
switch (ast.type) {
|
|
149
|
+
case 'PRIMARY KEY':
|
|
150
|
+
return primaryTableConstraint(ast, wrap);
|
|
151
|
+
case 'UNIQUE':
|
|
152
|
+
return uniqueTableConstraint(ast, wrap);
|
|
153
|
+
case 'CHECK':
|
|
154
|
+
return checkTableConstraint(ast, wrap);
|
|
155
|
+
case 'FOREIGN KEY':
|
|
156
|
+
return foreignTableConstraint(ast, wrap);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function primaryTableConstraint(ast, wrap) {
|
|
161
|
+
return `${constraintName(ast, wrap)}PRIMARY KEY (${indexedColumnList(
|
|
162
|
+
ast,
|
|
163
|
+
wrap
|
|
164
|
+
)})${conflictClause(ast, wrap)}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function uniqueTableConstraint(ast, wrap) {
|
|
168
|
+
return `${constraintName(ast, wrap)}UNIQUE (${indexedColumnList(
|
|
169
|
+
ast,
|
|
170
|
+
wrap
|
|
171
|
+
)})${conflictClause(ast, wrap)}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function conflictClause(ast, wrap) {
|
|
175
|
+
return ast.conflict !== null ? ` ON CONFLICT ${ast.conflict}` : '';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function checkTableConstraint(ast, wrap) {
|
|
179
|
+
return `${constraintName(ast, wrap)}CHECK (${expression(
|
|
180
|
+
ast.expression,
|
|
181
|
+
wrap
|
|
182
|
+
)})`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function foreignTableConstraint(ast, wrap) {
|
|
186
|
+
return `${constraintName(ast, wrap)}FOREIGN KEY (${columnNameList(
|
|
187
|
+
ast,
|
|
188
|
+
wrap
|
|
189
|
+
)}) ${foreignKeyClause(ast.references, wrap)}`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function foreignKeyClause(ast, wrap) {
|
|
193
|
+
return `REFERENCES ${table(ast, wrap)}${columnNameListOptional(
|
|
194
|
+
ast,
|
|
195
|
+
wrap
|
|
196
|
+
)}${deleteUpdateMatchList(ast, wrap)}${deferrable(ast.deferrable, wrap)}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function columnNameListOptional(ast, wrap) {
|
|
200
|
+
return ast.columns.length > 0 ? ` (${columnNameList(ast, wrap)})` : '';
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function columnNameList(ast, wrap) {
|
|
204
|
+
return ast.columns.map((column) => identifier(column, wrap)).join(', ');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function deleteUpdateMatchList(ast, wrap) {
|
|
208
|
+
return `${deleteReference(ast, wrap)}${updateReference(
|
|
209
|
+
ast,
|
|
210
|
+
wrap
|
|
211
|
+
)}${matchReference(ast, wrap)}`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function deleteReference(ast, wrap) {
|
|
215
|
+
return ast.delete !== null ? ` ON DELETE ${ast.delete}` : '';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function updateReference(ast, wrap) {
|
|
219
|
+
return ast.update !== null ? ` ON UPDATE ${ast.update}` : '';
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function matchReference(ast, wrap) {
|
|
223
|
+
return ast.match !== null ? ` MATCH ${ast.match}` : '';
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function deferrable(ast, wrap) {
|
|
227
|
+
return ast !== null
|
|
228
|
+
? ` ${ast.not ? 'NOT ' : ''}DEFERRABLE${
|
|
229
|
+
ast.initially !== null ? ` INITIALLY ${ast.initially}` : ''
|
|
230
|
+
}`
|
|
231
|
+
: '';
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function constraintName(ast, wrap) {
|
|
235
|
+
return ast.name !== null ? `CONSTRAINT ${identifier(ast.name, wrap)} ` : '';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function createIndex(ast, wrap) {
|
|
239
|
+
return `CREATE${unique(ast, wrap)} INDEX${exists(ast, wrap)} ${schema(
|
|
240
|
+
ast,
|
|
241
|
+
wrap
|
|
242
|
+
)}${index(ast, wrap)} on ${table(ast, wrap)} (${indexedColumnList(
|
|
243
|
+
ast,
|
|
244
|
+
wrap
|
|
245
|
+
)})${where(ast, wrap)}`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function unique(ast, wrap) {
|
|
249
|
+
return ast.unique ? ' UNIQUE' : '';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function exists(ast, wrap) {
|
|
253
|
+
return ast.exists ? ' IF NOT EXISTS' : '';
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function schema(ast, wrap) {
|
|
257
|
+
return ast.schema !== null ? `${identifier(ast.schema, wrap)}.` : '';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function index(ast, wrap) {
|
|
261
|
+
return identifier(ast.index, wrap);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function table(ast, wrap) {
|
|
265
|
+
return identifier(ast.table, wrap);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function where(ast, wrap) {
|
|
269
|
+
return ast.where !== null ? ` where ${expression(ast.where)}` : '';
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function indexedColumnList(ast, wrap) {
|
|
273
|
+
return ast.columns
|
|
274
|
+
.map((column) =>
|
|
275
|
+
!column.expression
|
|
276
|
+
? indexedColumn(column, wrap)
|
|
277
|
+
: indexedColumnExpression(column, wrap)
|
|
278
|
+
)
|
|
279
|
+
.join(', ');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function indexedColumn(ast, wrap) {
|
|
283
|
+
return `${identifier(ast.name, wrap)}${collation(ast, wrap)}${order(
|
|
284
|
+
ast,
|
|
285
|
+
wrap
|
|
286
|
+
)}`;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function indexedColumnExpression(ast, wrap) {
|
|
290
|
+
return `${indexedExpression(ast.name, wrap)}${collation(ast, wrap)}${order(
|
|
291
|
+
ast,
|
|
292
|
+
wrap
|
|
293
|
+
)}`;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function collation(ast, wrap) {
|
|
297
|
+
return ast.collation !== null ? ` COLLATE ${ast.collation}` : '';
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function order(ast, wrap) {
|
|
301
|
+
return ast.order !== null ? ` ${ast.order}` : '';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function indexedExpression(ast, wrap) {
|
|
305
|
+
return expression(ast, wrap);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function expression(ast, wrap) {
|
|
309
|
+
return ast.reduce(
|
|
310
|
+
(expr, e) =>
|
|
311
|
+
Array.isArray(e)
|
|
312
|
+
? `${expr}(${expression(e)})`
|
|
313
|
+
: !expr
|
|
314
|
+
? e
|
|
315
|
+
: `${expr} ${e}`,
|
|
316
|
+
''
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function identifier(ast, wrap) {
|
|
321
|
+
return wrap(ast);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
module.exports = {
|
|
325
|
+
compileCreateTable,
|
|
326
|
+
compileCreateIndex,
|
|
327
|
+
};
|