knex 3.2.3 → 3.2.5
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 +2447 -2441
- package/CONTRIBUTING.md +190 -190
- package/LICENSE +22 -22
- package/README.md +177 -156
- package/UPGRADING.md +245 -245
- package/bin/cli.js +516 -516
- package/bin/knexfile-runtime-error.js +27 -27
- package/bin/utils/cli-config-utils.js +217 -217
- package/bin/utils/constants.js +7 -7
- package/bin/utils/migrationsLister.js +37 -37
- package/knex.js +23 -23
- package/knex.mjs +11 -11
- package/lib/builder-interface-augmenter.js +120 -120
- package/lib/client.js +585 -585
- package/lib/constants.js +61 -61
- package/lib/dialects/better-sqlite3/index.js +101 -101
- 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 +46 -46
- package/lib/dialects/cockroachdb/crdb-viewcompiler.js +15 -15
- package/lib/dialects/cockroachdb/index.js +86 -86
- package/lib/dialects/index.js +34 -34
- package/lib/dialects/mssql/index.js +498 -498
- package/lib/dialects/mssql/mssql-formatter.js +34 -34
- package/lib/dialects/mssql/query/mssql-querycompiler.js +601 -601
- 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 +393 -393
- package/lib/dialects/mssql/schema/mssql-viewcompiler.js +55 -55
- package/lib/dialects/mssql/transaction.js +176 -176
- package/lib/dialects/mysql/index.js +317 -317
- package/lib/dialects/mysql/query/mysql-querybuilder.js +14 -14
- package/lib/dialects/mysql/query/mysql-querycompiler.js +292 -292
- 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 +426 -426
- 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 -53
- 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 -343
- package/lib/dialects/oracle/schema/internal/incrementUtils.js +22 -22
- package/lib/dialects/oracle/schema/internal/trigger.js +155 -155
- 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 +124 -124
- package/lib/dialects/oracle/schema/oracle-tablecompiler.js +210 -210
- package/lib/dialects/oracle/utils.js +107 -107
- package/lib/dialects/oracledb/index.js +381 -381
- package/lib/dialects/oracledb/query/oracledb-querycompiler.js +481 -481
- package/lib/dialects/oracledb/schema/oracledb-columncompiler.js +61 -61
- 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 -19
- package/lib/dialects/postgres/index.js +373 -373
- package/lib/dialects/postgres/query/pg-querybuilder.js +43 -43
- package/lib/dialects/postgres/query/pg-querycompiler.js +400 -400
- package/lib/dialects/postgres/schema/pg-columncompiler.js +162 -162
- package/lib/dialects/postgres/schema/pg-compiler.js +138 -138
- package/lib/dialects/postgres/schema/pg-tablecompiler.js +331 -331
- 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 +134 -134
- package/lib/dialects/redshift/schema/redshift-viewcompiler.js +11 -11
- package/lib/dialects/redshift/transaction.js +32 -32
- package/lib/dialects/sqlite3/execution/sqlite-transaction.js +172 -172
- package/lib/dialects/sqlite3/index.js +263 -263
- package/lib/dialects/sqlite3/query/sqlite-querybuilder.js +33 -33
- package/lib/dialects/sqlite3/query/sqlite-querycompiler.js +341 -341
- package/lib/dialects/sqlite3/schema/ddl.js +380 -380
- 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 +364 -364
- 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 -325
- package/lib/execution/transaction.js +417 -417
- package/lib/formatter/formatterUtils.js +42 -42
- package/lib/formatter/rawFormatter.js +84 -84
- package/lib/formatter/wrappingFormatter.js +253 -253
- package/lib/formatter.js +25 -25
- package/lib/index.js +3 -3
- package/lib/knex-builder/FunctionHelper.js +80 -80
- 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 -345
- 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 +632 -632
- 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 -136
- package/lib/query/querybuilder.js +1793 -1793
- package/lib/query/querycompiler.js +1634 -1634
- 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 +379 -379
- package/lib/schema/tablecompiler.js +450 -450
- 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 +32 -32
- package/lib/util/string.js +190 -190
- package/lib/util/timeout.js +29 -29
- package/package.json +295 -293
- package/scripts/act-testing/act.sh +19 -19
- package/scripts/act-testing/merged-no-label.json +11 -11
- package/scripts/act-testing/merged-patch-labeled.json +12 -12
- package/scripts/act-testing/merged-skip-labeled.json +12 -12
- package/scripts/act-testing/not-merged-patch-labeled.json +12 -12
- package/scripts/build-for-release.sh +121 -121
- package/scripts/build.js +125 -125
- package/scripts/clean.js +31 -31
- package/scripts/docker-compose.yml +150 -150
- package/scripts/format-changelog.js +55 -55
- package/scripts/next-release-howto.md +24 -24
- package/scripts/oracledb-install-driver-libs.sh +82 -82
- package/scripts/release.sh +36 -36
- package/scripts/runkit-example.js +35 -35
- package/scripts/stress-test/README.txt +18 -18
- package/scripts/stress-test/docker-compose.yml +55 -55
- package/scripts/stress-test/knex-stress-test.js +212 -212
- package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +149 -149
- package/scripts/stress-test/mysql2-sudden-exit-without-error.js +101 -101
- package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +188 -188
- package/types/index.d.mts +14 -0
- package/types/index.d.ts +3321 -3321
- package/types/result.d.ts +27 -27
- package/types/tables.d.ts +4 -4
|
@@ -1,638 +1,638 @@
|
|
|
1
|
-
const { tokenize } = require('./tokenizer');
|
|
2
|
-
const { s, a, m, o, l, n, t, e, f } = require('./parser-combinator');
|
|
3
|
-
|
|
4
|
-
const TOKENS = {
|
|
5
|
-
keyword:
|
|
6
|
-
/(?:ABORT|ACTION|ADD|AFTER|ALL|ALTER|ALWAYS|ANALYZE|AND|AS|ASC|ATTACH|AUTOINCREMENT|BEFORE|BEGIN|BETWEEN|BY|CASCADE|CASE|CAST|CHECK|COLLATE|COLUMN|COMMIT|CONFLICT|CONSTRAINT|CREATE|CROSS|CURRENT|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|DATABASE|DEFAULT|DEFERRED|DEFERRABLE|DELETE|DESC|DETACH|DISTINCT|DO|DROP|END|EACH|ELSE|ESCAPE|EXCEPT|EXCLUSIVE|EXCLUDE|EXISTS|EXPLAIN|FAIL|FILTER|FIRST|FOLLOWING|FOR|FOREIGN|FROM|FULL|GENERATED|GLOB|GROUP|GROUPS|HAVING|IF|IGNORE|IMMEDIATE|IN|INDEX|INDEXED|INITIALLY|INNER|INSERT|INSTEAD|INTERSECT|INTO|IS|ISNULL|JOIN|KEY|LAST|LEFT|LIKE|LIMIT|MATCH|MATERIALIZED|NATURAL|NO|NOT|NOTHING|NOTNULL|NULL|NULLS|OF|OFFSET|ON|OR|ORDER|OTHERS|OUTER|OVER|PARTITION|PLAN|PRAGMA|PRECEDING|PRIMARY|QUERY|RAISE|RANGE|RECURSIVE|REFERENCES|REGEXP|REINDEX|RELEASE|RENAME|REPLACE|RESTRICT|RETURNING|RIGHT|ROLLBACK|ROW|ROWS|SAVEPOINT|SELECT|SET|TABLE|TEMP|TEMPORARY|THEN|TIES|TO|TRANSACTION|TRIGGER|UNBOUNDED|UNION|UNIQUE|UPDATE|USING|VACUUM|VALUES|VIEW|VIRTUAL|WHEN|WHERE|WINDOW|WITH|WITHOUT)(?=\s+|-|\(|\)|;|\+|\*|\/|%|==|=|<=|<>|<<|<|>=|>>|>|!=|,|&|~|\|\||\||\.)/,
|
|
7
|
-
id: /"[^"]*(?:""[^"]*)*"|`[^`]*(?:``[^`]*)*`|\[[^[\]]*\]|[a-z_][a-z0-9_$]*/,
|
|
8
|
-
string: /'[^']*(?:''[^']*)*'/,
|
|
9
|
-
blob: /x'(?:[0-9a-f][0-9a-f])+'/,
|
|
10
|
-
numeric: /(?:\d+(?:\.\d*)?|\.\d+)(?:e(?:\+|-)?\d+)?|0x[0-9a-f]+/,
|
|
11
|
-
variable: /\?\d*|[@$:][a-z0-9_$]+/,
|
|
12
|
-
operator: /-|\(|\)|;|\+|\*|\/|%|==|=|<=|<>|<<|<|>=|>>|>|!=|,|&|~|\|\||\||\./,
|
|
13
|
-
_ws: /\s+/,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
function parseCreateTable(sql) {
|
|
17
|
-
const result = createTable({ input: tokenize(sql, TOKENS) });
|
|
18
|
-
|
|
19
|
-
if (!result.success) {
|
|
20
|
-
throw new Error(
|
|
21
|
-
`Parsing CREATE TABLE failed at [${result.input
|
|
22
|
-
.slice(result.index)
|
|
23
|
-
.map((t) => t.text)
|
|
24
|
-
.join(' ')}] of "${sql}"`
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return result.ast;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function parseCreateIndex(sql) {
|
|
32
|
-
const result = createIndex({ input: tokenize(sql, TOKENS) });
|
|
33
|
-
|
|
34
|
-
if (!result.success) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
`Parsing CREATE INDEX failed at [${result.input
|
|
37
|
-
.slice(result.index)
|
|
38
|
-
.map((t) => t.text)
|
|
39
|
-
.join(' ')}] of "${sql}"`
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return result.ast;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function createTable(ctx) {
|
|
47
|
-
return s(
|
|
48
|
-
[
|
|
49
|
-
t({ text: 'CREATE' }, (v) => null),
|
|
50
|
-
temporary,
|
|
51
|
-
t({ text: 'TABLE' }, (v) => null),
|
|
52
|
-
exists,
|
|
53
|
-
schema,
|
|
54
|
-
table,
|
|
55
|
-
t({ text: '(' }, (v) => null),
|
|
56
|
-
columnDefinitionList,
|
|
57
|
-
tableConstraintList,
|
|
58
|
-
t({ text: ')' }, (v) => null),
|
|
59
|
-
rowid,
|
|
60
|
-
f,
|
|
61
|
-
],
|
|
62
|
-
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
63
|
-
)(ctx);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function temporary(ctx) {
|
|
67
|
-
return a([t({ text: 'TEMP' }), t({ text: 'TEMPORARY' }), e], (v) => ({
|
|
68
|
-
temporary: v !== null,
|
|
69
|
-
}))(ctx);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function rowid(ctx) {
|
|
73
|
-
return o(s([t({ text: 'WITHOUT' }), t({ text: 'ROWID' })]), (v) => ({
|
|
74
|
-
rowid: v !== null,
|
|
75
|
-
}))(ctx);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function columnDefinitionList(ctx) {
|
|
79
|
-
return a([
|
|
80
|
-
s([columnDefinition, t({ text: ',' }), columnDefinitionList], (v) => ({
|
|
81
|
-
columns: [v[0]].concat(v[2].columns),
|
|
82
|
-
})),
|
|
83
|
-
s([columnDefinition], (v) => ({ columns: [v[0]] })),
|
|
84
|
-
])(ctx);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function columnDefinition(ctx) {
|
|
88
|
-
return s(
|
|
89
|
-
[s([identifier], (v) => ({ name: v[0] })), typeName, columnConstraintList],
|
|
90
|
-
(v) => Object.assign({}, ...v)
|
|
91
|
-
)(ctx);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function typeName(ctx) {
|
|
95
|
-
return o(
|
|
96
|
-
s(
|
|
97
|
-
[
|
|
98
|
-
m(t({ type: 'id' })),
|
|
99
|
-
a([
|
|
100
|
-
s(
|
|
101
|
-
[
|
|
102
|
-
t({ text: '(' }),
|
|
103
|
-
signedNumber,
|
|
104
|
-
t({ text: ',' }),
|
|
105
|
-
signedNumber,
|
|
106
|
-
t({ text: ')' }),
|
|
107
|
-
],
|
|
108
|
-
(v) => `(${v[1]}, ${v[3]})`
|
|
109
|
-
),
|
|
110
|
-
s(
|
|
111
|
-
[t({ text: '(' }), signedNumber, t({ text: ')' })],
|
|
112
|
-
(v) => `(${v[1]})`
|
|
113
|
-
),
|
|
114
|
-
e,
|
|
115
|
-
]),
|
|
116
|
-
],
|
|
117
|
-
(v) => `${v[0].join(' ')}${v[1] || ''}`
|
|
118
|
-
),
|
|
119
|
-
(v) => ({ type: v })
|
|
120
|
-
)(ctx);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function columnConstraintList(ctx) {
|
|
124
|
-
return o(m(columnConstraint), (v) => ({
|
|
125
|
-
constraints: Object.assign(
|
|
126
|
-
{
|
|
127
|
-
primary: null,
|
|
128
|
-
notnull: null,
|
|
129
|
-
null: null,
|
|
130
|
-
unique: null,
|
|
131
|
-
check: null,
|
|
132
|
-
default: null,
|
|
133
|
-
collate: null,
|
|
134
|
-
references: null,
|
|
135
|
-
as: null,
|
|
136
|
-
},
|
|
137
|
-
...(v || [])
|
|
138
|
-
),
|
|
139
|
-
}))(ctx);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function columnConstraint(ctx) {
|
|
143
|
-
return a([
|
|
144
|
-
primaryColumnConstraint,
|
|
145
|
-
notnullColumnConstraint,
|
|
146
|
-
nullColumnConstraint,
|
|
147
|
-
uniqueColumnConstraint,
|
|
148
|
-
checkColumnConstraint,
|
|
149
|
-
defaultColumnConstraint,
|
|
150
|
-
collateColumnConstraint,
|
|
151
|
-
referencesColumnConstraint,
|
|
152
|
-
asColumnConstraint,
|
|
153
|
-
])(ctx);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function primaryColumnConstraint(ctx) {
|
|
157
|
-
return s(
|
|
158
|
-
[
|
|
159
|
-
constraintName,
|
|
160
|
-
t({ text: 'PRIMARY' }, (v) => null),
|
|
161
|
-
t({ text: 'KEY' }, (v) => null),
|
|
162
|
-
order,
|
|
163
|
-
conflictClause,
|
|
164
|
-
autoincrement,
|
|
165
|
-
],
|
|
166
|
-
(v) => ({ primary: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
167
|
-
)(ctx);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function autoincrement(ctx) {
|
|
171
|
-
return o(t({ text: 'AUTOINCREMENT' }), (v) => ({
|
|
172
|
-
autoincrement: v !== null,
|
|
173
|
-
}))(ctx);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function notnullColumnConstraint(ctx) {
|
|
177
|
-
return s(
|
|
178
|
-
[
|
|
179
|
-
constraintName,
|
|
180
|
-
t({ text: 'NOT' }, (v) => null),
|
|
181
|
-
t({ text: 'NULL' }, (v) => null),
|
|
182
|
-
conflictClause,
|
|
183
|
-
],
|
|
184
|
-
(v) => ({ notnull: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
185
|
-
)(ctx);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function nullColumnConstraint(ctx) {
|
|
189
|
-
return s(
|
|
190
|
-
[constraintName, t({ text: 'NULL' }, (v) => null), conflictClause],
|
|
191
|
-
(v) => ({ null: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
192
|
-
)(ctx);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function uniqueColumnConstraint(ctx) {
|
|
196
|
-
return s(
|
|
197
|
-
[constraintName, t({ text: 'UNIQUE' }, (v) => null), conflictClause],
|
|
198
|
-
(v) => ({ unique: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
199
|
-
)(ctx);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function checkColumnConstraint(ctx) {
|
|
203
|
-
return s(
|
|
204
|
-
[
|
|
205
|
-
constraintName,
|
|
206
|
-
t({ text: 'CHECK' }, (v) => null),
|
|
207
|
-
t({ text: '(' }, (v) => null),
|
|
208
|
-
s([expression], (v) => ({ expression: v[0] })),
|
|
209
|
-
t({ text: ')' }, (v) => null),
|
|
210
|
-
],
|
|
211
|
-
(v) => ({ check: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
212
|
-
)(ctx);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function defaultColumnConstraint(ctx) {
|
|
216
|
-
return s(
|
|
217
|
-
[
|
|
218
|
-
constraintName,
|
|
219
|
-
t({ text: 'DEFAULT' }, (v) => null),
|
|
220
|
-
a([
|
|
221
|
-
s([t({ text: '(' }), expression, t({ text: ')' })], (v) => ({
|
|
222
|
-
value: v[1],
|
|
223
|
-
expression: true,
|
|
224
|
-
})),
|
|
225
|
-
s([literalValue], (v) => ({ value: v[0], expression: false })),
|
|
226
|
-
s([signedNumber], (v) => ({ value: v[0], expression: false })),
|
|
227
|
-
]),
|
|
228
|
-
],
|
|
229
|
-
(v) => ({ default: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
230
|
-
)(ctx);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
function collateColumnConstraint(ctx) {
|
|
234
|
-
return s(
|
|
235
|
-
[
|
|
236
|
-
constraintName,
|
|
237
|
-
t({ text: 'COLLATE' }, (v) => null),
|
|
238
|
-
t({ type: 'id' }, (v) => ({ collation: v.text })),
|
|
239
|
-
],
|
|
240
|
-
(v) => ({ collate: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
241
|
-
)(ctx);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function referencesColumnConstraint(ctx) {
|
|
245
|
-
return s(
|
|
246
|
-
[constraintName, s([foreignKeyClause], (v) => v[0].references)],
|
|
247
|
-
(v) => ({
|
|
248
|
-
references: Object.assign({}, ...v.filter((x) => x !== null)),
|
|
249
|
-
})
|
|
250
|
-
)(ctx);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function asColumnConstraint(ctx) {
|
|
254
|
-
return s(
|
|
255
|
-
[
|
|
256
|
-
constraintName,
|
|
257
|
-
o(s([t({ text: 'GENERATED' }), t({ text: 'ALWAYS' })]), (v) => ({
|
|
258
|
-
generated: v !== null,
|
|
259
|
-
})),
|
|
260
|
-
t({ text: 'AS' }, (v) => null),
|
|
261
|
-
t({ text: '(' }, (v) => null),
|
|
262
|
-
s([expression], (v) => ({ expression: v[0] })),
|
|
263
|
-
t({ text: ')' }, (v) => null),
|
|
264
|
-
a([t({ text: 'STORED' }), t({ text: 'VIRTUAL' }), e], (v) => ({
|
|
265
|
-
mode: v ? v.toUpperCase() : null,
|
|
266
|
-
})),
|
|
267
|
-
],
|
|
268
|
-
(v) => ({ as: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
269
|
-
)(ctx);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function tableConstraintList(ctx) {
|
|
273
|
-
return o(m(s([t({ text: ',' }), tableConstraint], (v) => v[1])), (v) => ({
|
|
274
|
-
constraints: v || [],
|
|
275
|
-
}))(ctx);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function tableConstraint(ctx) {
|
|
279
|
-
return a([
|
|
280
|
-
primaryTableConstraint,
|
|
281
|
-
uniqueTableConstraint,
|
|
282
|
-
checkTableConstraint,
|
|
283
|
-
foreignTableConstraint,
|
|
284
|
-
])(ctx);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function primaryTableConstraint(ctx) {
|
|
288
|
-
return s(
|
|
289
|
-
[
|
|
290
|
-
constraintName,
|
|
291
|
-
t({ text: 'PRIMARY' }, (v) => null),
|
|
292
|
-
t({ text: 'KEY' }, (v) => null),
|
|
293
|
-
t({ text: '(' }, (v) => null),
|
|
294
|
-
indexedColumnList,
|
|
295
|
-
t({ text: ')' }, (v) => null),
|
|
296
|
-
conflictClause,
|
|
297
|
-
],
|
|
298
|
-
(v) =>
|
|
299
|
-
Object.assign({ type: 'PRIMARY KEY' }, ...v.filter((x) => x !== null))
|
|
300
|
-
)(ctx);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function uniqueTableConstraint(ctx) {
|
|
304
|
-
return s(
|
|
305
|
-
[
|
|
306
|
-
constraintName,
|
|
307
|
-
t({ text: 'UNIQUE' }, (v) => null),
|
|
308
|
-
t({ text: '(' }, (v) => null),
|
|
309
|
-
indexedColumnList,
|
|
310
|
-
t({ text: ')' }, (v) => null),
|
|
311
|
-
conflictClause,
|
|
312
|
-
],
|
|
313
|
-
(v) => Object.assign({ type: 'UNIQUE' }, ...v.filter((x) => x !== null))
|
|
314
|
-
)(ctx);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function conflictClause(ctx) {
|
|
318
|
-
return o(
|
|
319
|
-
s(
|
|
320
|
-
[
|
|
321
|
-
t({ text: 'ON' }),
|
|
322
|
-
t({ text: 'CONFLICT' }),
|
|
323
|
-
a([
|
|
324
|
-
t({ text: 'ROLLBACK' }),
|
|
325
|
-
t({ text: 'ABORT' }),
|
|
326
|
-
t({ text: 'FAIL' }),
|
|
327
|
-
t({ text: 'IGNORE' }),
|
|
328
|
-
t({ text: 'REPLACE' }),
|
|
329
|
-
]),
|
|
330
|
-
],
|
|
331
|
-
(v) => v[2]
|
|
332
|
-
),
|
|
333
|
-
(v) => ({ conflict: v ? v.toUpperCase() : null })
|
|
334
|
-
)(ctx);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
function checkTableConstraint(ctx) {
|
|
338
|
-
return s(
|
|
339
|
-
[
|
|
340
|
-
constraintName,
|
|
341
|
-
t({ text: 'CHECK' }, (v) => null),
|
|
342
|
-
t({ text: '(' }, (v) => null),
|
|
343
|
-
s([expression], (v) => ({ expression: v[0] })),
|
|
344
|
-
t({ text: ')' }, (v) => null),
|
|
345
|
-
],
|
|
346
|
-
(v) => Object.assign({ type: 'CHECK' }, ...v.filter((x) => x !== null))
|
|
347
|
-
)(ctx);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
function foreignTableConstraint(ctx) {
|
|
351
|
-
return s(
|
|
352
|
-
[
|
|
353
|
-
constraintName,
|
|
354
|
-
t({ text: 'FOREIGN' }, (v) => null),
|
|
355
|
-
t({ text: 'KEY' }, (v) => null),
|
|
356
|
-
t({ text: '(' }, (v) => null),
|
|
357
|
-
columnNameList,
|
|
358
|
-
t({ text: ')' }, (v) => null),
|
|
359
|
-
foreignKeyClause,
|
|
360
|
-
],
|
|
361
|
-
(v) =>
|
|
362
|
-
Object.assign({ type: 'FOREIGN KEY' }, ...v.filter((x) => x !== null))
|
|
363
|
-
)(ctx);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function foreignKeyClause(ctx) {
|
|
367
|
-
return s(
|
|
368
|
-
[
|
|
369
|
-
t({ text: 'REFERENCES' }, (v) => null),
|
|
370
|
-
table,
|
|
371
|
-
columnNameListOptional,
|
|
372
|
-
o(m(a([deleteReference, updateReference, matchReference])), (v) =>
|
|
373
|
-
Object.assign({ delete: null, update: null, match: null }, ...(v || []))
|
|
374
|
-
),
|
|
375
|
-
deferrable,
|
|
376
|
-
],
|
|
377
|
-
(v) => ({ references: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
378
|
-
)(ctx);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
function columnNameListOptional(ctx) {
|
|
382
|
-
return o(
|
|
383
|
-
s([t({ text: '(' }), columnNameList, t({ text: ')' })], (v) => v[1]),
|
|
384
|
-
(v) => ({ columns: v ? v.columns : [] })
|
|
385
|
-
)(ctx);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
function columnNameList(ctx) {
|
|
389
|
-
return s(
|
|
390
|
-
[
|
|
391
|
-
o(m(s([identifier, t({ text: ',' })], (v) => v[0])), (v) =>
|
|
392
|
-
v !== null ? v : []
|
|
393
|
-
),
|
|
394
|
-
identifier,
|
|
395
|
-
],
|
|
396
|
-
(v) => ({ columns: v[0].concat([v[1]]) })
|
|
397
|
-
)(ctx);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
function deleteReference(ctx) {
|
|
401
|
-
return s([t({ text: 'ON' }), t({ text: 'DELETE' }), onAction], (v) => ({
|
|
402
|
-
delete: v[2],
|
|
403
|
-
}))(ctx);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function updateReference(ctx) {
|
|
407
|
-
return s([t({ text: 'ON' }), t({ text: 'UPDATE' }), onAction], (v) => ({
|
|
408
|
-
update: v[2],
|
|
409
|
-
}))(ctx);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
function matchReference(ctx) {
|
|
413
|
-
return s(
|
|
414
|
-
[t({ text: 'MATCH' }), a([t({ type: 'keyword' }), t({ type: 'id' })])],
|
|
415
|
-
(v) => ({ match: v[1] })
|
|
416
|
-
)(ctx);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
function deferrable(ctx) {
|
|
420
|
-
return o(
|
|
421
|
-
s([
|
|
422
|
-
o(t({ text: 'NOT' })),
|
|
423
|
-
t({ text: 'DEFERRABLE' }),
|
|
424
|
-
o(
|
|
425
|
-
s(
|
|
426
|
-
[
|
|
427
|
-
t({ text: 'INITIALLY' }),
|
|
428
|
-
a([t({ text: 'DEFERRED' }), t({ text: 'IMMEDIATE' })]),
|
|
429
|
-
],
|
|
430
|
-
(v) => v[1].toUpperCase()
|
|
431
|
-
)
|
|
432
|
-
),
|
|
433
|
-
]),
|
|
434
|
-
(v) => ({ deferrable: v ? { not: v[0] !== null, initially: v[2] } : null })
|
|
435
|
-
)(ctx);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
function constraintName(ctx) {
|
|
439
|
-
return o(
|
|
440
|
-
s([t({ text: 'CONSTRAINT' }), identifier], (v) => v[1]),
|
|
441
|
-
(v) => ({ name: v })
|
|
442
|
-
)(ctx);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
function createIndex(ctx) {
|
|
446
|
-
return s(
|
|
447
|
-
[
|
|
448
|
-
t({ text: 'CREATE' }, (v) => null),
|
|
449
|
-
unique,
|
|
450
|
-
t({ text: 'INDEX' }, (v) => null),
|
|
451
|
-
exists,
|
|
452
|
-
schema,
|
|
453
|
-
index,
|
|
454
|
-
t({ text: 'ON' }, (v) => null),
|
|
455
|
-
table,
|
|
456
|
-
t({ text: '(' }, (v) => null),
|
|
457
|
-
indexedColumnList,
|
|
458
|
-
t({ text: ')' }, (v) => null),
|
|
459
|
-
where,
|
|
460
|
-
f,
|
|
461
|
-
],
|
|
462
|
-
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
463
|
-
)(ctx);
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
function unique(ctx) {
|
|
467
|
-
return o(t({ text: 'UNIQUE' }), (v) => ({ unique: v !== null }))(ctx);
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
function exists(ctx) {
|
|
471
|
-
return o(
|
|
472
|
-
s([t({ text: 'IF' }), t({ text: 'NOT' }), t({ text: 'EXISTS' })]),
|
|
473
|
-
(v) => ({ exists: v !== null })
|
|
474
|
-
)(ctx);
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function schema(ctx) {
|
|
478
|
-
return o(
|
|
479
|
-
s([identifier, t({ text: '.' })], (v) => v[0]),
|
|
480
|
-
(v) => ({ schema: v })
|
|
481
|
-
)(ctx);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function index(ctx) {
|
|
485
|
-
return s([identifier], (v) => ({ index: v[0] }))(ctx);
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
function table(ctx) {
|
|
489
|
-
return s([identifier], (v) => ({ table: v[0] }))(ctx);
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
function where(ctx) {
|
|
493
|
-
return o(
|
|
494
|
-
s([t({ text: 'WHERE' }), expression], (v) => v[1]),
|
|
495
|
-
(v) => ({ where: v })
|
|
496
|
-
)(ctx);
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
function indexedColumnList(ctx) {
|
|
500
|
-
return a([
|
|
501
|
-
s([indexedColumn, t({ text: ',' }), indexedColumnList], (v) => ({
|
|
502
|
-
columns: [v[0]].concat(v[2].columns),
|
|
503
|
-
})),
|
|
504
|
-
s([indexedColumnExpression, t({ text: ',' }), indexedColumnList], (v) => ({
|
|
505
|
-
columns: [v[0]].concat(v[2].columns),
|
|
506
|
-
})),
|
|
507
|
-
l({ do: indexedColumn, next: t({ text: ')' }) }, (v) => ({
|
|
508
|
-
columns: [v],
|
|
509
|
-
})),
|
|
510
|
-
l({ do: indexedColumnExpression, next: t({ text: ')' }) }, (v) => ({
|
|
511
|
-
columns: [v],
|
|
512
|
-
})),
|
|
513
|
-
])(ctx);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
function indexedColumn(ctx) {
|
|
517
|
-
return s(
|
|
518
|
-
[
|
|
519
|
-
s([identifier], (v) => ({ name: v[0], expression: false })),
|
|
520
|
-
collation,
|
|
521
|
-
order,
|
|
522
|
-
],
|
|
523
|
-
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
524
|
-
)(ctx);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
function indexedColumnExpression(ctx) {
|
|
528
|
-
return s(
|
|
529
|
-
[
|
|
530
|
-
s([indexedExpression], (v) => ({ name: v[0], expression: true })),
|
|
531
|
-
collation,
|
|
532
|
-
order,
|
|
533
|
-
],
|
|
534
|
-
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
535
|
-
)(ctx);
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
function collation(ctx) {
|
|
539
|
-
return o(
|
|
540
|
-
s([t({ text: 'COLLATE' }), t({ type: 'id' })], (v) => v[1]),
|
|
541
|
-
(v) => ({ collation: v })
|
|
542
|
-
)(ctx);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
function order(ctx) {
|
|
546
|
-
return a([t({ text: 'ASC' }), t({ text: 'DESC' }), e], (v) => ({
|
|
547
|
-
order: v ? v.toUpperCase() : null,
|
|
548
|
-
}))(ctx);
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
function indexedExpression(ctx) {
|
|
552
|
-
return m(
|
|
553
|
-
a([
|
|
554
|
-
n({
|
|
555
|
-
do: t({ type: 'keyword' }),
|
|
556
|
-
not: a([
|
|
557
|
-
t({ text: 'COLLATE' }),
|
|
558
|
-
t({ text: 'ASC' }),
|
|
559
|
-
t({ text: 'DESC' }),
|
|
560
|
-
]),
|
|
561
|
-
}),
|
|
562
|
-
t({ type: 'id' }),
|
|
563
|
-
t({ type: 'string' }),
|
|
564
|
-
t({ type: 'blob' }),
|
|
565
|
-
t({ type: 'numeric' }),
|
|
566
|
-
t({ type: 'variable' }),
|
|
567
|
-
n({
|
|
568
|
-
do: t({ type: 'operator' }),
|
|
569
|
-
not: a([t({ text: '(' }), t({ text: ')' }), t({ text: ',' })]),
|
|
570
|
-
}),
|
|
571
|
-
s([t({ text: '(' }), o(expression), t({ text: ')' })], (v) => v[1] || []),
|
|
572
|
-
])
|
|
573
|
-
)(ctx);
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
function expression(ctx) {
|
|
577
|
-
return m(
|
|
578
|
-
a([
|
|
579
|
-
t({ type: 'keyword' }),
|
|
580
|
-
t({ type: 'id' }),
|
|
581
|
-
t({ type: 'string' }),
|
|
582
|
-
t({ type: 'blob' }),
|
|
583
|
-
t({ type: 'numeric' }),
|
|
584
|
-
t({ type: 'variable' }),
|
|
585
|
-
n({
|
|
586
|
-
do: t({ type: 'operator' }),
|
|
587
|
-
not: a([t({ text: '(' }), t({ text: ')' })]),
|
|
588
|
-
}),
|
|
589
|
-
s([t({ text: '(' }), o(expression), t({ text: ')' })], (v) => v[1] || []),
|
|
590
|
-
])
|
|
591
|
-
)(ctx);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
function identifier(ctx) {
|
|
595
|
-
return a([t({ type: 'id' }), t({ type: 'string' })], (v) =>
|
|
596
|
-
/^["`['][^]*["`\]']$/.test(v) ? v.substring(1, v.length - 1) : v
|
|
597
|
-
)(ctx);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
function onAction(ctx) {
|
|
601
|
-
return a(
|
|
602
|
-
[
|
|
603
|
-
s([t({ text: 'SET' }), t({ text: 'NULL' })], (v) => `${v[0]} ${v[1]}`),
|
|
604
|
-
s([t({ text: 'SET' }), t({ text: 'DEFAULT' })], (v) => `${v[0]} ${v[1]}`),
|
|
605
|
-
t({ text: 'CASCADE' }),
|
|
606
|
-
t({ text: 'RESTRICT' }),
|
|
607
|
-
s([t({ text: 'NO' }), t({ text: 'ACTION' })], (v) => `${v[0]} ${v[1]}`),
|
|
608
|
-
],
|
|
609
|
-
(v) => v.toUpperCase()
|
|
610
|
-
)(ctx);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
function literalValue(ctx) {
|
|
614
|
-
return a([
|
|
615
|
-
t({ type: 'numeric' }),
|
|
616
|
-
t({ type: 'string' }),
|
|
617
|
-
t({ type: 'id' }),
|
|
618
|
-
t({ type: 'blob' }),
|
|
619
|
-
t({ text: 'NULL' }),
|
|
620
|
-
t({ text: 'TRUE' }),
|
|
621
|
-
t({ text: 'FALSE' }),
|
|
622
|
-
t({ text: 'CURRENT_TIME' }),
|
|
623
|
-
t({ text: 'CURRENT_DATE' }),
|
|
624
|
-
t({ text: 'CURRENT_TIMESTAMP' }),
|
|
625
|
-
])(ctx);
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
function signedNumber(ctx) {
|
|
629
|
-
return s(
|
|
630
|
-
[a([t({ text: '+' }), t({ text: '-' }), e]), t({ type: 'numeric' })],
|
|
631
|
-
(v) => `${v[0] || ''}${v[1]}`
|
|
632
|
-
)(ctx);
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
module.exports = {
|
|
636
|
-
parseCreateTable,
|
|
637
|
-
parseCreateIndex,
|
|
638
|
-
};
|
|
1
|
+
const { tokenize } = require('./tokenizer');
|
|
2
|
+
const { s, a, m, o, l, n, t, e, f } = require('./parser-combinator');
|
|
3
|
+
|
|
4
|
+
const TOKENS = {
|
|
5
|
+
keyword:
|
|
6
|
+
/(?:ABORT|ACTION|ADD|AFTER|ALL|ALTER|ALWAYS|ANALYZE|AND|AS|ASC|ATTACH|AUTOINCREMENT|BEFORE|BEGIN|BETWEEN|BY|CASCADE|CASE|CAST|CHECK|COLLATE|COLUMN|COMMIT|CONFLICT|CONSTRAINT|CREATE|CROSS|CURRENT|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|DATABASE|DEFAULT|DEFERRED|DEFERRABLE|DELETE|DESC|DETACH|DISTINCT|DO|DROP|END|EACH|ELSE|ESCAPE|EXCEPT|EXCLUSIVE|EXCLUDE|EXISTS|EXPLAIN|FAIL|FILTER|FIRST|FOLLOWING|FOR|FOREIGN|FROM|FULL|GENERATED|GLOB|GROUP|GROUPS|HAVING|IF|IGNORE|IMMEDIATE|IN|INDEX|INDEXED|INITIALLY|INNER|INSERT|INSTEAD|INTERSECT|INTO|IS|ISNULL|JOIN|KEY|LAST|LEFT|LIKE|LIMIT|MATCH|MATERIALIZED|NATURAL|NO|NOT|NOTHING|NOTNULL|NULL|NULLS|OF|OFFSET|ON|OR|ORDER|OTHERS|OUTER|OVER|PARTITION|PLAN|PRAGMA|PRECEDING|PRIMARY|QUERY|RAISE|RANGE|RECURSIVE|REFERENCES|REGEXP|REINDEX|RELEASE|RENAME|REPLACE|RESTRICT|RETURNING|RIGHT|ROLLBACK|ROW|ROWS|SAVEPOINT|SELECT|SET|TABLE|TEMP|TEMPORARY|THEN|TIES|TO|TRANSACTION|TRIGGER|UNBOUNDED|UNION|UNIQUE|UPDATE|USING|VACUUM|VALUES|VIEW|VIRTUAL|WHEN|WHERE|WINDOW|WITH|WITHOUT)(?=\s+|-|\(|\)|;|\+|\*|\/|%|==|=|<=|<>|<<|<|>=|>>|>|!=|,|&|~|\|\||\||\.)/,
|
|
7
|
+
id: /"[^"]*(?:""[^"]*)*"|`[^`]*(?:``[^`]*)*`|\[[^[\]]*\]|[a-z_][a-z0-9_$]*/,
|
|
8
|
+
string: /'[^']*(?:''[^']*)*'/,
|
|
9
|
+
blob: /x'(?:[0-9a-f][0-9a-f])+'/,
|
|
10
|
+
numeric: /(?:\d+(?:\.\d*)?|\.\d+)(?:e(?:\+|-)?\d+)?|0x[0-9a-f]+/,
|
|
11
|
+
variable: /\?\d*|[@$:][a-z0-9_$]+/,
|
|
12
|
+
operator: /-|\(|\)|;|\+|\*|\/|%|==|=|<=|<>|<<|<|>=|>>|>|!=|,|&|~|\|\||\||\./,
|
|
13
|
+
_ws: /\s+/,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function parseCreateTable(sql) {
|
|
17
|
+
const result = createTable({ input: tokenize(sql, TOKENS) });
|
|
18
|
+
|
|
19
|
+
if (!result.success) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Parsing CREATE TABLE failed at [${result.input
|
|
22
|
+
.slice(result.index)
|
|
23
|
+
.map((t) => t.text)
|
|
24
|
+
.join(' ')}] of "${sql}"`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return result.ast;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseCreateIndex(sql) {
|
|
32
|
+
const result = createIndex({ input: tokenize(sql, TOKENS) });
|
|
33
|
+
|
|
34
|
+
if (!result.success) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`Parsing CREATE INDEX failed at [${result.input
|
|
37
|
+
.slice(result.index)
|
|
38
|
+
.map((t) => t.text)
|
|
39
|
+
.join(' ')}] of "${sql}"`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result.ast;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createTable(ctx) {
|
|
47
|
+
return s(
|
|
48
|
+
[
|
|
49
|
+
t({ text: 'CREATE' }, (v) => null),
|
|
50
|
+
temporary,
|
|
51
|
+
t({ text: 'TABLE' }, (v) => null),
|
|
52
|
+
exists,
|
|
53
|
+
schema,
|
|
54
|
+
table,
|
|
55
|
+
t({ text: '(' }, (v) => null),
|
|
56
|
+
columnDefinitionList,
|
|
57
|
+
tableConstraintList,
|
|
58
|
+
t({ text: ')' }, (v) => null),
|
|
59
|
+
rowid,
|
|
60
|
+
f,
|
|
61
|
+
],
|
|
62
|
+
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
63
|
+
)(ctx);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function temporary(ctx) {
|
|
67
|
+
return a([t({ text: 'TEMP' }), t({ text: 'TEMPORARY' }), e], (v) => ({
|
|
68
|
+
temporary: v !== null,
|
|
69
|
+
}))(ctx);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function rowid(ctx) {
|
|
73
|
+
return o(s([t({ text: 'WITHOUT' }), t({ text: 'ROWID' })]), (v) => ({
|
|
74
|
+
rowid: v !== null,
|
|
75
|
+
}))(ctx);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function columnDefinitionList(ctx) {
|
|
79
|
+
return a([
|
|
80
|
+
s([columnDefinition, t({ text: ',' }), columnDefinitionList], (v) => ({
|
|
81
|
+
columns: [v[0]].concat(v[2].columns),
|
|
82
|
+
})),
|
|
83
|
+
s([columnDefinition], (v) => ({ columns: [v[0]] })),
|
|
84
|
+
])(ctx);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function columnDefinition(ctx) {
|
|
88
|
+
return s(
|
|
89
|
+
[s([identifier], (v) => ({ name: v[0] })), typeName, columnConstraintList],
|
|
90
|
+
(v) => Object.assign({}, ...v)
|
|
91
|
+
)(ctx);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function typeName(ctx) {
|
|
95
|
+
return o(
|
|
96
|
+
s(
|
|
97
|
+
[
|
|
98
|
+
m(t({ type: 'id' })),
|
|
99
|
+
a([
|
|
100
|
+
s(
|
|
101
|
+
[
|
|
102
|
+
t({ text: '(' }),
|
|
103
|
+
signedNumber,
|
|
104
|
+
t({ text: ',' }),
|
|
105
|
+
signedNumber,
|
|
106
|
+
t({ text: ')' }),
|
|
107
|
+
],
|
|
108
|
+
(v) => `(${v[1]}, ${v[3]})`
|
|
109
|
+
),
|
|
110
|
+
s(
|
|
111
|
+
[t({ text: '(' }), signedNumber, t({ text: ')' })],
|
|
112
|
+
(v) => `(${v[1]})`
|
|
113
|
+
),
|
|
114
|
+
e,
|
|
115
|
+
]),
|
|
116
|
+
],
|
|
117
|
+
(v) => `${v[0].join(' ')}${v[1] || ''}`
|
|
118
|
+
),
|
|
119
|
+
(v) => ({ type: v })
|
|
120
|
+
)(ctx);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function columnConstraintList(ctx) {
|
|
124
|
+
return o(m(columnConstraint), (v) => ({
|
|
125
|
+
constraints: Object.assign(
|
|
126
|
+
{
|
|
127
|
+
primary: null,
|
|
128
|
+
notnull: null,
|
|
129
|
+
null: null,
|
|
130
|
+
unique: null,
|
|
131
|
+
check: null,
|
|
132
|
+
default: null,
|
|
133
|
+
collate: null,
|
|
134
|
+
references: null,
|
|
135
|
+
as: null,
|
|
136
|
+
},
|
|
137
|
+
...(v || [])
|
|
138
|
+
),
|
|
139
|
+
}))(ctx);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function columnConstraint(ctx) {
|
|
143
|
+
return a([
|
|
144
|
+
primaryColumnConstraint,
|
|
145
|
+
notnullColumnConstraint,
|
|
146
|
+
nullColumnConstraint,
|
|
147
|
+
uniqueColumnConstraint,
|
|
148
|
+
checkColumnConstraint,
|
|
149
|
+
defaultColumnConstraint,
|
|
150
|
+
collateColumnConstraint,
|
|
151
|
+
referencesColumnConstraint,
|
|
152
|
+
asColumnConstraint,
|
|
153
|
+
])(ctx);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function primaryColumnConstraint(ctx) {
|
|
157
|
+
return s(
|
|
158
|
+
[
|
|
159
|
+
constraintName,
|
|
160
|
+
t({ text: 'PRIMARY' }, (v) => null),
|
|
161
|
+
t({ text: 'KEY' }, (v) => null),
|
|
162
|
+
order,
|
|
163
|
+
conflictClause,
|
|
164
|
+
autoincrement,
|
|
165
|
+
],
|
|
166
|
+
(v) => ({ primary: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
167
|
+
)(ctx);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function autoincrement(ctx) {
|
|
171
|
+
return o(t({ text: 'AUTOINCREMENT' }), (v) => ({
|
|
172
|
+
autoincrement: v !== null,
|
|
173
|
+
}))(ctx);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function notnullColumnConstraint(ctx) {
|
|
177
|
+
return s(
|
|
178
|
+
[
|
|
179
|
+
constraintName,
|
|
180
|
+
t({ text: 'NOT' }, (v) => null),
|
|
181
|
+
t({ text: 'NULL' }, (v) => null),
|
|
182
|
+
conflictClause,
|
|
183
|
+
],
|
|
184
|
+
(v) => ({ notnull: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
185
|
+
)(ctx);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function nullColumnConstraint(ctx) {
|
|
189
|
+
return s(
|
|
190
|
+
[constraintName, t({ text: 'NULL' }, (v) => null), conflictClause],
|
|
191
|
+
(v) => ({ null: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
192
|
+
)(ctx);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function uniqueColumnConstraint(ctx) {
|
|
196
|
+
return s(
|
|
197
|
+
[constraintName, t({ text: 'UNIQUE' }, (v) => null), conflictClause],
|
|
198
|
+
(v) => ({ unique: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
199
|
+
)(ctx);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function checkColumnConstraint(ctx) {
|
|
203
|
+
return s(
|
|
204
|
+
[
|
|
205
|
+
constraintName,
|
|
206
|
+
t({ text: 'CHECK' }, (v) => null),
|
|
207
|
+
t({ text: '(' }, (v) => null),
|
|
208
|
+
s([expression], (v) => ({ expression: v[0] })),
|
|
209
|
+
t({ text: ')' }, (v) => null),
|
|
210
|
+
],
|
|
211
|
+
(v) => ({ check: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
212
|
+
)(ctx);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function defaultColumnConstraint(ctx) {
|
|
216
|
+
return s(
|
|
217
|
+
[
|
|
218
|
+
constraintName,
|
|
219
|
+
t({ text: 'DEFAULT' }, (v) => null),
|
|
220
|
+
a([
|
|
221
|
+
s([t({ text: '(' }), expression, t({ text: ')' })], (v) => ({
|
|
222
|
+
value: v[1],
|
|
223
|
+
expression: true,
|
|
224
|
+
})),
|
|
225
|
+
s([literalValue], (v) => ({ value: v[0], expression: false })),
|
|
226
|
+
s([signedNumber], (v) => ({ value: v[0], expression: false })),
|
|
227
|
+
]),
|
|
228
|
+
],
|
|
229
|
+
(v) => ({ default: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
230
|
+
)(ctx);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function collateColumnConstraint(ctx) {
|
|
234
|
+
return s(
|
|
235
|
+
[
|
|
236
|
+
constraintName,
|
|
237
|
+
t({ text: 'COLLATE' }, (v) => null),
|
|
238
|
+
t({ type: 'id' }, (v) => ({ collation: v.text })),
|
|
239
|
+
],
|
|
240
|
+
(v) => ({ collate: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
241
|
+
)(ctx);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function referencesColumnConstraint(ctx) {
|
|
245
|
+
return s(
|
|
246
|
+
[constraintName, s([foreignKeyClause], (v) => v[0].references)],
|
|
247
|
+
(v) => ({
|
|
248
|
+
references: Object.assign({}, ...v.filter((x) => x !== null)),
|
|
249
|
+
})
|
|
250
|
+
)(ctx);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function asColumnConstraint(ctx) {
|
|
254
|
+
return s(
|
|
255
|
+
[
|
|
256
|
+
constraintName,
|
|
257
|
+
o(s([t({ text: 'GENERATED' }), t({ text: 'ALWAYS' })]), (v) => ({
|
|
258
|
+
generated: v !== null,
|
|
259
|
+
})),
|
|
260
|
+
t({ text: 'AS' }, (v) => null),
|
|
261
|
+
t({ text: '(' }, (v) => null),
|
|
262
|
+
s([expression], (v) => ({ expression: v[0] })),
|
|
263
|
+
t({ text: ')' }, (v) => null),
|
|
264
|
+
a([t({ text: 'STORED' }), t({ text: 'VIRTUAL' }), e], (v) => ({
|
|
265
|
+
mode: v ? v.toUpperCase() : null,
|
|
266
|
+
})),
|
|
267
|
+
],
|
|
268
|
+
(v) => ({ as: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
269
|
+
)(ctx);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function tableConstraintList(ctx) {
|
|
273
|
+
return o(m(s([t({ text: ',' }), tableConstraint], (v) => v[1])), (v) => ({
|
|
274
|
+
constraints: v || [],
|
|
275
|
+
}))(ctx);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function tableConstraint(ctx) {
|
|
279
|
+
return a([
|
|
280
|
+
primaryTableConstraint,
|
|
281
|
+
uniqueTableConstraint,
|
|
282
|
+
checkTableConstraint,
|
|
283
|
+
foreignTableConstraint,
|
|
284
|
+
])(ctx);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function primaryTableConstraint(ctx) {
|
|
288
|
+
return s(
|
|
289
|
+
[
|
|
290
|
+
constraintName,
|
|
291
|
+
t({ text: 'PRIMARY' }, (v) => null),
|
|
292
|
+
t({ text: 'KEY' }, (v) => null),
|
|
293
|
+
t({ text: '(' }, (v) => null),
|
|
294
|
+
indexedColumnList,
|
|
295
|
+
t({ text: ')' }, (v) => null),
|
|
296
|
+
conflictClause,
|
|
297
|
+
],
|
|
298
|
+
(v) =>
|
|
299
|
+
Object.assign({ type: 'PRIMARY KEY' }, ...v.filter((x) => x !== null))
|
|
300
|
+
)(ctx);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function uniqueTableConstraint(ctx) {
|
|
304
|
+
return s(
|
|
305
|
+
[
|
|
306
|
+
constraintName,
|
|
307
|
+
t({ text: 'UNIQUE' }, (v) => null),
|
|
308
|
+
t({ text: '(' }, (v) => null),
|
|
309
|
+
indexedColumnList,
|
|
310
|
+
t({ text: ')' }, (v) => null),
|
|
311
|
+
conflictClause,
|
|
312
|
+
],
|
|
313
|
+
(v) => Object.assign({ type: 'UNIQUE' }, ...v.filter((x) => x !== null))
|
|
314
|
+
)(ctx);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function conflictClause(ctx) {
|
|
318
|
+
return o(
|
|
319
|
+
s(
|
|
320
|
+
[
|
|
321
|
+
t({ text: 'ON' }),
|
|
322
|
+
t({ text: 'CONFLICT' }),
|
|
323
|
+
a([
|
|
324
|
+
t({ text: 'ROLLBACK' }),
|
|
325
|
+
t({ text: 'ABORT' }),
|
|
326
|
+
t({ text: 'FAIL' }),
|
|
327
|
+
t({ text: 'IGNORE' }),
|
|
328
|
+
t({ text: 'REPLACE' }),
|
|
329
|
+
]),
|
|
330
|
+
],
|
|
331
|
+
(v) => v[2]
|
|
332
|
+
),
|
|
333
|
+
(v) => ({ conflict: v ? v.toUpperCase() : null })
|
|
334
|
+
)(ctx);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function checkTableConstraint(ctx) {
|
|
338
|
+
return s(
|
|
339
|
+
[
|
|
340
|
+
constraintName,
|
|
341
|
+
t({ text: 'CHECK' }, (v) => null),
|
|
342
|
+
t({ text: '(' }, (v) => null),
|
|
343
|
+
s([expression], (v) => ({ expression: v[0] })),
|
|
344
|
+
t({ text: ')' }, (v) => null),
|
|
345
|
+
],
|
|
346
|
+
(v) => Object.assign({ type: 'CHECK' }, ...v.filter((x) => x !== null))
|
|
347
|
+
)(ctx);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function foreignTableConstraint(ctx) {
|
|
351
|
+
return s(
|
|
352
|
+
[
|
|
353
|
+
constraintName,
|
|
354
|
+
t({ text: 'FOREIGN' }, (v) => null),
|
|
355
|
+
t({ text: 'KEY' }, (v) => null),
|
|
356
|
+
t({ text: '(' }, (v) => null),
|
|
357
|
+
columnNameList,
|
|
358
|
+
t({ text: ')' }, (v) => null),
|
|
359
|
+
foreignKeyClause,
|
|
360
|
+
],
|
|
361
|
+
(v) =>
|
|
362
|
+
Object.assign({ type: 'FOREIGN KEY' }, ...v.filter((x) => x !== null))
|
|
363
|
+
)(ctx);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function foreignKeyClause(ctx) {
|
|
367
|
+
return s(
|
|
368
|
+
[
|
|
369
|
+
t({ text: 'REFERENCES' }, (v) => null),
|
|
370
|
+
table,
|
|
371
|
+
columnNameListOptional,
|
|
372
|
+
o(m(a([deleteReference, updateReference, matchReference])), (v) =>
|
|
373
|
+
Object.assign({ delete: null, update: null, match: null }, ...(v || []))
|
|
374
|
+
),
|
|
375
|
+
deferrable,
|
|
376
|
+
],
|
|
377
|
+
(v) => ({ references: Object.assign({}, ...v.filter((x) => x !== null)) })
|
|
378
|
+
)(ctx);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function columnNameListOptional(ctx) {
|
|
382
|
+
return o(
|
|
383
|
+
s([t({ text: '(' }), columnNameList, t({ text: ')' })], (v) => v[1]),
|
|
384
|
+
(v) => ({ columns: v ? v.columns : [] })
|
|
385
|
+
)(ctx);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function columnNameList(ctx) {
|
|
389
|
+
return s(
|
|
390
|
+
[
|
|
391
|
+
o(m(s([identifier, t({ text: ',' })], (v) => v[0])), (v) =>
|
|
392
|
+
v !== null ? v : []
|
|
393
|
+
),
|
|
394
|
+
identifier,
|
|
395
|
+
],
|
|
396
|
+
(v) => ({ columns: v[0].concat([v[1]]) })
|
|
397
|
+
)(ctx);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function deleteReference(ctx) {
|
|
401
|
+
return s([t({ text: 'ON' }), t({ text: 'DELETE' }), onAction], (v) => ({
|
|
402
|
+
delete: v[2],
|
|
403
|
+
}))(ctx);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function updateReference(ctx) {
|
|
407
|
+
return s([t({ text: 'ON' }), t({ text: 'UPDATE' }), onAction], (v) => ({
|
|
408
|
+
update: v[2],
|
|
409
|
+
}))(ctx);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function matchReference(ctx) {
|
|
413
|
+
return s(
|
|
414
|
+
[t({ text: 'MATCH' }), a([t({ type: 'keyword' }), t({ type: 'id' })])],
|
|
415
|
+
(v) => ({ match: v[1] })
|
|
416
|
+
)(ctx);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function deferrable(ctx) {
|
|
420
|
+
return o(
|
|
421
|
+
s([
|
|
422
|
+
o(t({ text: 'NOT' })),
|
|
423
|
+
t({ text: 'DEFERRABLE' }),
|
|
424
|
+
o(
|
|
425
|
+
s(
|
|
426
|
+
[
|
|
427
|
+
t({ text: 'INITIALLY' }),
|
|
428
|
+
a([t({ text: 'DEFERRED' }), t({ text: 'IMMEDIATE' })]),
|
|
429
|
+
],
|
|
430
|
+
(v) => v[1].toUpperCase()
|
|
431
|
+
)
|
|
432
|
+
),
|
|
433
|
+
]),
|
|
434
|
+
(v) => ({ deferrable: v ? { not: v[0] !== null, initially: v[2] } : null })
|
|
435
|
+
)(ctx);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function constraintName(ctx) {
|
|
439
|
+
return o(
|
|
440
|
+
s([t({ text: 'CONSTRAINT' }), identifier], (v) => v[1]),
|
|
441
|
+
(v) => ({ name: v })
|
|
442
|
+
)(ctx);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function createIndex(ctx) {
|
|
446
|
+
return s(
|
|
447
|
+
[
|
|
448
|
+
t({ text: 'CREATE' }, (v) => null),
|
|
449
|
+
unique,
|
|
450
|
+
t({ text: 'INDEX' }, (v) => null),
|
|
451
|
+
exists,
|
|
452
|
+
schema,
|
|
453
|
+
index,
|
|
454
|
+
t({ text: 'ON' }, (v) => null),
|
|
455
|
+
table,
|
|
456
|
+
t({ text: '(' }, (v) => null),
|
|
457
|
+
indexedColumnList,
|
|
458
|
+
t({ text: ')' }, (v) => null),
|
|
459
|
+
where,
|
|
460
|
+
f,
|
|
461
|
+
],
|
|
462
|
+
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
463
|
+
)(ctx);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function unique(ctx) {
|
|
467
|
+
return o(t({ text: 'UNIQUE' }), (v) => ({ unique: v !== null }))(ctx);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function exists(ctx) {
|
|
471
|
+
return o(
|
|
472
|
+
s([t({ text: 'IF' }), t({ text: 'NOT' }), t({ text: 'EXISTS' })]),
|
|
473
|
+
(v) => ({ exists: v !== null })
|
|
474
|
+
)(ctx);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function schema(ctx) {
|
|
478
|
+
return o(
|
|
479
|
+
s([identifier, t({ text: '.' })], (v) => v[0]),
|
|
480
|
+
(v) => ({ schema: v })
|
|
481
|
+
)(ctx);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function index(ctx) {
|
|
485
|
+
return s([identifier], (v) => ({ index: v[0] }))(ctx);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function table(ctx) {
|
|
489
|
+
return s([identifier], (v) => ({ table: v[0] }))(ctx);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function where(ctx) {
|
|
493
|
+
return o(
|
|
494
|
+
s([t({ text: 'WHERE' }), expression], (v) => v[1]),
|
|
495
|
+
(v) => ({ where: v })
|
|
496
|
+
)(ctx);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function indexedColumnList(ctx) {
|
|
500
|
+
return a([
|
|
501
|
+
s([indexedColumn, t({ text: ',' }), indexedColumnList], (v) => ({
|
|
502
|
+
columns: [v[0]].concat(v[2].columns),
|
|
503
|
+
})),
|
|
504
|
+
s([indexedColumnExpression, t({ text: ',' }), indexedColumnList], (v) => ({
|
|
505
|
+
columns: [v[0]].concat(v[2].columns),
|
|
506
|
+
})),
|
|
507
|
+
l({ do: indexedColumn, next: t({ text: ')' }) }, (v) => ({
|
|
508
|
+
columns: [v],
|
|
509
|
+
})),
|
|
510
|
+
l({ do: indexedColumnExpression, next: t({ text: ')' }) }, (v) => ({
|
|
511
|
+
columns: [v],
|
|
512
|
+
})),
|
|
513
|
+
])(ctx);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function indexedColumn(ctx) {
|
|
517
|
+
return s(
|
|
518
|
+
[
|
|
519
|
+
s([identifier], (v) => ({ name: v[0], expression: false })),
|
|
520
|
+
collation,
|
|
521
|
+
order,
|
|
522
|
+
],
|
|
523
|
+
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
524
|
+
)(ctx);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function indexedColumnExpression(ctx) {
|
|
528
|
+
return s(
|
|
529
|
+
[
|
|
530
|
+
s([indexedExpression], (v) => ({ name: v[0], expression: true })),
|
|
531
|
+
collation,
|
|
532
|
+
order,
|
|
533
|
+
],
|
|
534
|
+
(v) => Object.assign({}, ...v.filter((x) => x !== null))
|
|
535
|
+
)(ctx);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function collation(ctx) {
|
|
539
|
+
return o(
|
|
540
|
+
s([t({ text: 'COLLATE' }), t({ type: 'id' })], (v) => v[1]),
|
|
541
|
+
(v) => ({ collation: v })
|
|
542
|
+
)(ctx);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function order(ctx) {
|
|
546
|
+
return a([t({ text: 'ASC' }), t({ text: 'DESC' }), e], (v) => ({
|
|
547
|
+
order: v ? v.toUpperCase() : null,
|
|
548
|
+
}))(ctx);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function indexedExpression(ctx) {
|
|
552
|
+
return m(
|
|
553
|
+
a([
|
|
554
|
+
n({
|
|
555
|
+
do: t({ type: 'keyword' }),
|
|
556
|
+
not: a([
|
|
557
|
+
t({ text: 'COLLATE' }),
|
|
558
|
+
t({ text: 'ASC' }),
|
|
559
|
+
t({ text: 'DESC' }),
|
|
560
|
+
]),
|
|
561
|
+
}),
|
|
562
|
+
t({ type: 'id' }),
|
|
563
|
+
t({ type: 'string' }),
|
|
564
|
+
t({ type: 'blob' }),
|
|
565
|
+
t({ type: 'numeric' }),
|
|
566
|
+
t({ type: 'variable' }),
|
|
567
|
+
n({
|
|
568
|
+
do: t({ type: 'operator' }),
|
|
569
|
+
not: a([t({ text: '(' }), t({ text: ')' }), t({ text: ',' })]),
|
|
570
|
+
}),
|
|
571
|
+
s([t({ text: '(' }), o(expression), t({ text: ')' })], (v) => v[1] || []),
|
|
572
|
+
])
|
|
573
|
+
)(ctx);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function expression(ctx) {
|
|
577
|
+
return m(
|
|
578
|
+
a([
|
|
579
|
+
t({ type: 'keyword' }),
|
|
580
|
+
t({ type: 'id' }),
|
|
581
|
+
t({ type: 'string' }),
|
|
582
|
+
t({ type: 'blob' }),
|
|
583
|
+
t({ type: 'numeric' }),
|
|
584
|
+
t({ type: 'variable' }),
|
|
585
|
+
n({
|
|
586
|
+
do: t({ type: 'operator' }),
|
|
587
|
+
not: a([t({ text: '(' }), t({ text: ')' })]),
|
|
588
|
+
}),
|
|
589
|
+
s([t({ text: '(' }), o(expression), t({ text: ')' })], (v) => v[1] || []),
|
|
590
|
+
])
|
|
591
|
+
)(ctx);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function identifier(ctx) {
|
|
595
|
+
return a([t({ type: 'id' }), t({ type: 'string' })], (v) =>
|
|
596
|
+
/^["`['][^]*["`\]']$/.test(v) ? v.substring(1, v.length - 1) : v
|
|
597
|
+
)(ctx);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function onAction(ctx) {
|
|
601
|
+
return a(
|
|
602
|
+
[
|
|
603
|
+
s([t({ text: 'SET' }), t({ text: 'NULL' })], (v) => `${v[0]} ${v[1]}`),
|
|
604
|
+
s([t({ text: 'SET' }), t({ text: 'DEFAULT' })], (v) => `${v[0]} ${v[1]}`),
|
|
605
|
+
t({ text: 'CASCADE' }),
|
|
606
|
+
t({ text: 'RESTRICT' }),
|
|
607
|
+
s([t({ text: 'NO' }), t({ text: 'ACTION' })], (v) => `${v[0]} ${v[1]}`),
|
|
608
|
+
],
|
|
609
|
+
(v) => v.toUpperCase()
|
|
610
|
+
)(ctx);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function literalValue(ctx) {
|
|
614
|
+
return a([
|
|
615
|
+
t({ type: 'numeric' }),
|
|
616
|
+
t({ type: 'string' }),
|
|
617
|
+
t({ type: 'id' }),
|
|
618
|
+
t({ type: 'blob' }),
|
|
619
|
+
t({ text: 'NULL' }),
|
|
620
|
+
t({ text: 'TRUE' }),
|
|
621
|
+
t({ text: 'FALSE' }),
|
|
622
|
+
t({ text: 'CURRENT_TIME' }),
|
|
623
|
+
t({ text: 'CURRENT_DATE' }),
|
|
624
|
+
t({ text: 'CURRENT_TIMESTAMP' }),
|
|
625
|
+
])(ctx);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function signedNumber(ctx) {
|
|
629
|
+
return s(
|
|
630
|
+
[a([t({ text: '+' }), t({ text: '-' }), e]), t({ type: 'numeric' })],
|
|
631
|
+
(v) => `${v[0] || ''}${v[1]}`
|
|
632
|
+
)(ctx);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
module.exports = {
|
|
636
|
+
parseCreateTable,
|
|
637
|
+
parseCreateIndex,
|
|
638
|
+
};
|