@riao/dbal 3.8.0 → 3.9.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 +3 -3
- package/LICENSE.md +20 -20
- package/README.md +148 -148
- package/builder/builder.d.ts +7 -7
- package/builder/builder.js +19 -19
- package/builder/index.d.ts +2 -2
- package/builder/index.js +7 -7
- package/builder/sql-builder.d.ts +54 -53
- package/builder/sql-builder.js +161 -157
- package/builder/sql-builder.js.map +1 -1
- package/builder/statement-builder.d.ts +12 -12
- package/builder/statement-builder.js +32 -32
- package/column/column-options.d.ts +156 -156
- package/column/column-options.js +2 -2
- package/column/column-type.d.ts +18 -18
- package/column/column-type.js +27 -27
- package/column/column-values.d.ts +34 -34
- package/column/column-values.js +2 -2
- package/column/create-column-template.d.ts +11 -0
- package/column/create-column-template.js +57 -0
- package/column/create-column-template.js.map +1 -0
- package/column/index.d.ts +4 -3
- package/column/index.js +20 -19
- package/column/index.js.map +1 -1
- package/column-pack/date-time/create-timestamp-column.d.ts +2 -2
- package/column-pack/date-time/create-timestamp-column.js +10 -10
- package/column-pack/date-time/index.d.ts +2 -2
- package/column-pack/date-time/index.js +7 -7
- package/column-pack/date-time/update-timestamp-column.d.ts +2 -2
- package/column-pack/date-time/update-timestamp-column.js +14 -14
- package/column-pack/index.d.ts +6 -6
- package/column-pack/index.js +22 -22
- package/column-pack/keys/big-int-key-column.d.ts +2 -2
- package/column-pack/keys/big-int-key-column.js +10 -10
- package/column-pack/keys/index.d.ts +3 -3
- package/column-pack/keys/index.js +9 -9
- package/column-pack/keys/int-key-column.d.ts +2 -2
- package/column-pack/keys/int-key-column.js +10 -10
- package/column-pack/keys/uuid-key-column.d.ts +2 -2
- package/column-pack/keys/uuid-key-column.js +11 -11
- package/column-pack/person/date-of-birth-column.d.ts +2 -2
- package/column-pack/person/date-of-birth-column.js +8 -8
- package/column-pack/person/first-name-column.d.ts +2 -2
- package/column-pack/person/first-name-column.js +9 -9
- package/column-pack/person/index.d.ts +3 -3
- package/column-pack/person/index.js +9 -9
- package/column-pack/person/last-name-column.d.ts +2 -2
- package/column-pack/person/last-name-column.js +9 -9
- package/column-pack/resource/content-column.d.ts +2 -2
- package/column-pack/resource/content-column.js +9 -9
- package/column-pack/resource/description-column.d.ts +2 -2
- package/column-pack/resource/description-column.js +9 -9
- package/column-pack/resource/index.d.ts +6 -6
- package/column-pack/resource/index.js +15 -15
- package/column-pack/resource/is-active-column.d.ts +2 -2
- package/column-pack/resource/is-active-column.js +9 -9
- package/column-pack/resource/name-column.d.ts +2 -2
- package/column-pack/resource/name-column.js +10 -10
- package/column-pack/resource/path-column.d.ts +2 -2
- package/column-pack/resource/path-column.js +11 -11
- package/column-pack/resource/title-column.d.ts +2 -2
- package/column-pack/resource/title-column.js +10 -10
- package/column-pack/system/filename-column.d.ts +2 -2
- package/column-pack/system/filename-column.js +9 -9
- package/column-pack/system/filepath-column.d.ts +2 -2
- package/column-pack/system/filepath-column.js +9 -9
- package/column-pack/system/index.d.ts +4 -4
- package/column-pack/system/index.js +11 -11
- package/column-pack/system/ip-address-column.d.ts +2 -2
- package/column-pack/system/ip-address-column.js +9 -9
- package/column-pack/system/url-column.d.ts +3 -3
- package/column-pack/system/url-column.js +13 -13
- package/column-pack/user/email-column.d.ts +2 -2
- package/column-pack/user/email-column.js +11 -11
- package/column-pack/user/index.d.ts +4 -4
- package/column-pack/user/index.js +11 -11
- package/column-pack/user/password-column.d.ts +2 -2
- package/column-pack/user/password-column.js +10 -10
- package/column-pack/user/temp-email-column.d.ts +2 -2
- package/column-pack/user/temp-email-column.js +5 -5
- package/column-pack/user/username-column.d.ts +2 -2
- package/column-pack/user/username-column.js +11 -11
- package/comparison/comparison-aliases.d.ts +10 -10
- package/comparison/comparison-aliases.js +40 -40
- package/comparison/comparison-token.d.ts +17 -17
- package/comparison/comparison-token.js +15 -15
- package/comparison/comparison.d.ts +13 -13
- package/comparison/comparison.js +71 -71
- package/comparison/index.d.ts +4 -4
- package/comparison/index.js +24 -24
- package/comparison/is-comparison-token.d.ts +2 -2
- package/comparison/is-comparison-token.js +8 -8
- package/config/configure-db.d.ts +14 -14
- package/config/configure-db.js +18 -18
- package/config/database-env.d.ts +16 -16
- package/config/database-env.js +14 -14
- package/config/index.d.ts +2 -2
- package/config/index.js +7 -7
- package/database/connection-options.d.ts +7 -7
- package/database/connection-options.js +2 -2
- package/database/database.d.ts +204 -204
- package/database/database.js +330 -330
- package/database/driver-query.d.ts +10 -10
- package/database/driver-query.js +2 -2
- package/database/driver.d.ts +12 -12
- package/database/driver.js +47 -47
- package/database/get-database-path.d.ts +6 -6
- package/database/get-database-path.js +12 -12
- package/database/index.d.ts +7 -7
- package/database/index.js +26 -26
- package/database/load-database.d.ts +9 -9
- package/database/load-database.js +36 -36
- package/database/transaction.d.ts +8 -8
- package/database/transaction.js +2 -2
- package/ddl/alter-table.d.ts +26 -26
- package/ddl/alter-table.js +2 -2
- package/ddl/create-database.d.ts +3 -3
- package/ddl/create-database.js +2 -2
- package/ddl/create-index.d.ts +5 -5
- package/ddl/create-index.js +2 -2
- package/ddl/create-table.d.ts +8 -8
- package/ddl/create-table.js +2 -2
- package/ddl/create-user.d.ts +4 -4
- package/ddl/create-user.js +2 -2
- package/ddl/ddl-builder.d.ts +96 -96
- package/ddl/ddl-builder.js +565 -565
- package/ddl/ddl-repository.d.ts +135 -135
- package/ddl/ddl-repository.js +228 -228
- package/ddl/drop-database.d.ts +4 -4
- package/ddl/drop-database.js +2 -2
- package/ddl/drop-table.d.ts +4 -4
- package/ddl/drop-table.js +2 -2
- package/ddl/drop-user.d.ts +4 -4
- package/ddl/drop-user.js +2 -2
- package/ddl/foreign-key-constraint.d.ts +19 -19
- package/ddl/foreign-key-constraint.js +2 -2
- package/ddl/grant.d.ts +12 -12
- package/ddl/grant.js +2 -2
- package/ddl/index.d.ts +13 -13
- package/ddl/index.js +23 -23
- package/ddl/truncate.d.ts +3 -3
- package/ddl/truncate.js +2 -2
- package/dml/case-expression.d.ts +16 -16
- package/dml/case-expression.js +11 -11
- package/dml/delete.d.ts +8 -8
- package/dml/delete.js +2 -2
- package/dml/from.d.ts +4 -4
- package/dml/from.js +2 -2
- package/dml/group-by.d.ts +4 -4
- package/dml/group-by.js +2 -2
- package/dml/index.d.ts +11 -11
- package/dml/index.js +30 -30
- package/dml/insert.d.ts +14 -14
- package/dml/insert.js +2 -2
- package/dml/join.d.ts +7 -7
- package/dml/join.js +2 -2
- package/dml/order-by.d.ts +2 -2
- package/dml/order-by.js +2 -2
- package/dml/query-builder.d.ts +125 -106
- package/dml/query-builder.js +991 -793
- package/dml/query-builder.js.map +1 -1
- package/dml/query-repository.d.ts +84 -84
- package/dml/query-repository.js +239 -225
- package/dml/query-repository.js.map +1 -1
- package/dml/select.d.ts +44 -29
- package/dml/select.js +2 -2
- package/dml/set-options.d.ts +5 -5
- package/dml/set-options.js +2 -2
- package/dml/subquery.d.ts +5 -5
- package/dml/subquery.js +10 -10
- package/dml/update.d.ts +11 -11
- package/dml/update.js +2 -2
- package/expression/expression-token.d.ts +11 -11
- package/expression/expression-token.js +12 -12
- package/expression/expression.d.ts +6 -6
- package/expression/expression.js +2 -2
- package/expression/identifier.d.ts +7 -7
- package/expression/identifier.js +15 -15
- package/expression/index.d.ts +7 -7
- package/expression/index.js +25 -25
- package/expression/is-expression-token.d.ts +2 -2
- package/expression/is-expression-token.js +7 -7
- package/expression/key-val-expression.d.ts +7 -7
- package/expression/key-val-expression.js +2 -2
- package/expression/literal.d.ts +3 -3
- package/expression/literal.js +2 -2
- package/expression/logical.d.ts +28 -18
- package/expression/logical.js +54 -30
- package/expression/logical.js.map +1 -1
- package/expression/math.d.ts +17 -17
- package/expression/math.js +36 -36
- package/expression/raw.d.ts +8 -8
- package/expression/raw.js +16 -16
- package/expression/simple-expression.d.ts +8 -8
- package/expression/simple-expression.js +2 -2
- package/functions/function-aliases.d.ts +6 -4
- package/functions/function-aliases.js +16 -12
- package/functions/function-aliases.js.map +1 -1
- package/functions/function-token.d.ts +33 -29
- package/functions/function-token.js +19 -15
- package/functions/function-token.js.map +1 -1
- package/functions/functions.d.ts +23 -19
- package/functions/functions.js +126 -94
- package/functions/functions.js.map +1 -1
- package/functions/index.d.ts +4 -4
- package/functions/index.js +22 -22
- package/functions/is-database-function.d.ts +8 -8
- package/functions/is-database-function.js +14 -14
- package/functions/signatures/count.d.ts +14 -11
- package/functions/signatures/count.js +2 -2
- package/index.d.ts +16 -16
- package/index.js +32 -32
- package/migration/index.d.ts +3 -3
- package/migration/index.js +9 -9
- package/migration/migration-package.d.ts +14 -14
- package/migration/migration-package.js +10 -10
- package/migration/migration-record.d.ts +10 -10
- package/migration/migration-record.js +2 -2
- package/migration/migration-runner.d.ts +33 -33
- package/migration/migration-runner.js +180 -180
- package/migration/migration.d.ts +12 -12
- package/migration/migration.js +27 -27
- package/migration/migrations/001-create-migration-table.d.ts +8 -8
- package/migration/migrations/001-create-migration-table.js +54 -54
- package/migration/migrations/002-add-migration-package-column.d.ts +9 -9
- package/migration/migrations/002-add-migration-package-column.js +44 -44
- package/package.json +1 -1
- package/record/index.d.ts +1 -1
- package/record/index.js +17 -17
- package/record/record.d.ts +2 -2
- package/record/record.js +2 -2
- package/repository/index.d.ts +2 -2
- package/repository/index.js +5 -5
- package/repository/repository-options.d.ts +6 -6
- package/repository/repository-options.js +2 -2
- package/repository/repository.d.ts +29 -29
- package/repository/repository.js +68 -68
- package/schema/index.d.ts +3 -3
- package/schema/index.js +5 -5
- package/schema/schema-query-repository.d.ts +54 -54
- package/schema/schema-query-repository.js +159 -159
- package/schema/schema-table.d.ts +9 -9
- package/schema/schema-table.js +2 -2
- package/schema/schema.d.ts +4 -4
- package/schema/schema.js +2 -2
- package/seed/auto-seed.d.ts +9 -9
- package/seed/auto-seed.js +61 -61
- package/seed/create-seed-table.d.ts +8 -8
- package/seed/create-seed-table.js +64 -64
- package/seed/index.d.ts +4 -4
- package/seed/index.js +9 -9
- package/seed/seed-record.d.ts +11 -11
- package/seed/seed-record.js +2 -2
- package/seed/seed-runner.d.ts +20 -20
- package/seed/seed-runner.js +91 -91
- package/seed/seed.d.ts +12 -12
- package/seed/seed.js +27 -27
- package/tokens/index.d.ts +1 -1
- package/tokens/index.js +5 -5
- package/triggers/index.d.ts +2 -2
- package/triggers/index.js +18 -18
- package/triggers/prebuilt/index.d.ts +1 -1
- package/triggers/prebuilt/index.js +5 -5
- package/triggers/prebuilt/update-timestamp-trigger.d.ts +16 -16
- package/triggers/prebuilt/update-timestamp-trigger.js +24 -24
- package/triggers/trigger-options.d.ts +22 -22
- package/triggers/trigger-options.js +2 -2
- package/triggers/trigger.d.ts +19 -19
- package/triggers/trigger.js +23 -23
package/ddl/ddl-builder.js
CHANGED
|
@@ -1,566 +1,566 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DataDefinitionBuilder = void 0;
|
|
4
|
-
const statement_builder_1 = require("../builder/statement-builder");
|
|
5
|
-
const column_1 = require("../column");
|
|
6
|
-
const dml_1 = require("../dml");
|
|
7
|
-
const expression_1 = require("../expression");
|
|
8
|
-
const functions_1 = require("../functions");
|
|
9
|
-
const function_token_1 = require("../functions/function-token");
|
|
10
|
-
class DataDefinitionBuilder extends statement_builder_1.StatementBuilder {
|
|
11
|
-
constructor() {
|
|
12
|
-
super(...arguments);
|
|
13
|
-
this.columnTypes = column_1.ColumnType;
|
|
14
|
-
this.queryBuilderType = dml_1.DatabaseQueryBuilder;
|
|
15
|
-
}
|
|
16
|
-
getQueryBuilder() {
|
|
17
|
-
return new this.queryBuilderType();
|
|
18
|
-
}
|
|
19
|
-
getColumnTypes() {
|
|
20
|
-
return this.columnTypes;
|
|
21
|
-
}
|
|
22
|
-
// ------------------------------------------------------------------------
|
|
23
|
-
// Create Database
|
|
24
|
-
// ------------------------------------------------------------------------
|
|
25
|
-
createDatabaseStatement() {
|
|
26
|
-
this.sql.append('CREATE DATABASE ');
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
createDatabase(options) {
|
|
30
|
-
this.createDatabaseStatement();
|
|
31
|
-
this.sql.append(options.name);
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
// ------------------------------------------------------------------------
|
|
35
|
-
// Create User
|
|
36
|
-
// ------------------------------------------------------------------------
|
|
37
|
-
createUserStatement() {
|
|
38
|
-
this.sql.append('CREATE USER ');
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
createUserPassword(password) {
|
|
42
|
-
this.sql.append(`WITH PASSWORD '${password}' `);
|
|
43
|
-
return this;
|
|
44
|
-
}
|
|
45
|
-
createUser(options) {
|
|
46
|
-
this.createUserStatement();
|
|
47
|
-
this.sql.append(options.name + ' ');
|
|
48
|
-
if (options.password) {
|
|
49
|
-
this.createUserPassword(options.password);
|
|
50
|
-
}
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
|
-
// ------------------------------------------------------------------------
|
|
54
|
-
// Create Table
|
|
55
|
-
// ------------------------------------------------------------------------
|
|
56
|
-
createTableStatement() {
|
|
57
|
-
this.sql.append('CREATE TABLE ');
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
ifNotExists() {
|
|
61
|
-
this.sql.append('IF NOT EXISTS ');
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
createColumnType(column) {
|
|
65
|
-
this.sql.append(this.columnTypes[column.type]);
|
|
66
|
-
if ('length' in column) {
|
|
67
|
-
this.sql.append(`(${column.length})`);
|
|
68
|
-
}
|
|
69
|
-
if ('significant' in column && 'decimal' in column) {
|
|
70
|
-
const significant = column.significant + column.decimal;
|
|
71
|
-
const decimal = column.decimal;
|
|
72
|
-
this.sql.append(`(${significant}, ${decimal})`);
|
|
73
|
-
}
|
|
74
|
-
this.sql.space();
|
|
75
|
-
return this;
|
|
76
|
-
}
|
|
77
|
-
columnRequired() {
|
|
78
|
-
this.sql.append('NOT NULL ');
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
columnDefaultValue(column) {
|
|
82
|
-
this.sql.append('DEFAULT ');
|
|
83
|
-
if (column.default === null) {
|
|
84
|
-
this.columnDefaultNull();
|
|
85
|
-
}
|
|
86
|
-
else if (column.default === true) {
|
|
87
|
-
this.columnDefaultTrue();
|
|
88
|
-
}
|
|
89
|
-
else if (column.default === false) {
|
|
90
|
-
this.columnDefaultFalse();
|
|
91
|
-
}
|
|
92
|
-
else if ((0, expression_1.isExpressionToken)(column.default)) {
|
|
93
|
-
// CURRENT_TIMESTAMP does not work when wrapped in parenthesis for
|
|
94
|
-
// default value; but all other expressions should be wrapped
|
|
95
|
-
let wrapParens = true;
|
|
96
|
-
if ((0, functions_1.isDatabaseFunction)(column.default) &&
|
|
97
|
-
column.default.fn ===
|
|
98
|
-
function_token_1.DatabaseFunctionKeys.CURRENT_TIMESTAMP) {
|
|
99
|
-
wrapParens = false;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
this.sql.openParens();
|
|
103
|
-
}
|
|
104
|
-
const dml = this.getQueryBuilder();
|
|
105
|
-
dml.expression(column.default);
|
|
106
|
-
this.appendBuilder(dml);
|
|
107
|
-
if (wrapParens) {
|
|
108
|
-
this.sql.closeParens();
|
|
109
|
-
}
|
|
110
|
-
this.sql.append(' ');
|
|
111
|
-
}
|
|
112
|
-
else if (typeof column.default === 'string') {
|
|
113
|
-
const escaped = column.default
|
|
114
|
-
.replace(/\\/g, '\\\\')
|
|
115
|
-
.replace(/'/g, '\\\'')
|
|
116
|
-
.replace(/"/g, '\\"');
|
|
117
|
-
this.sql.append(`'${escaped}' `);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
this.sql.append(column.default + ' ');
|
|
121
|
-
}
|
|
122
|
-
return this;
|
|
123
|
-
}
|
|
124
|
-
columnDefaultNull() {
|
|
125
|
-
this.sql.append('NULL ');
|
|
126
|
-
return this;
|
|
127
|
-
}
|
|
128
|
-
columnDefaultTrue() {
|
|
129
|
-
this.sql.append('TRUE ');
|
|
130
|
-
return this;
|
|
131
|
-
}
|
|
132
|
-
columnDefaultFalse() {
|
|
133
|
-
this.sql.append('FALSE ');
|
|
134
|
-
return this;
|
|
135
|
-
}
|
|
136
|
-
columnAutoIncrement() {
|
|
137
|
-
this.sql.append('AUTO_INCREMENT ');
|
|
138
|
-
return this;
|
|
139
|
-
}
|
|
140
|
-
createTableColumn(column) {
|
|
141
|
-
this.sql.columnName(column.name);
|
|
142
|
-
this.sql.space();
|
|
143
|
-
this.createColumnType(column);
|
|
144
|
-
if (column.required) {
|
|
145
|
-
this.columnRequired();
|
|
146
|
-
}
|
|
147
|
-
if (column.default !== undefined) {
|
|
148
|
-
this.columnDefaultValue(column);
|
|
149
|
-
}
|
|
150
|
-
if (column.autoIncrement) {
|
|
151
|
-
this.columnAutoIncrement();
|
|
152
|
-
}
|
|
153
|
-
this.sql.trimEnd(' ');
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
createTablePrimaryKeys(names) {
|
|
157
|
-
this.sql.append(', PRIMARY KEY (');
|
|
158
|
-
this.sql.append(names.map((key) => this.sql.getEnclosedName(key)).join(','));
|
|
159
|
-
this.sql.append(')');
|
|
160
|
-
return this;
|
|
161
|
-
}
|
|
162
|
-
createTableColumns(options) {
|
|
163
|
-
var _a;
|
|
164
|
-
this.sql.append('(');
|
|
165
|
-
if (options.foreignKeys === undefined) {
|
|
166
|
-
options.foreignKeys = [];
|
|
167
|
-
}
|
|
168
|
-
for (const column of options.columns) {
|
|
169
|
-
this.createTableColumn(column);
|
|
170
|
-
this.sql.append(', ');
|
|
171
|
-
if ('fk' in column) {
|
|
172
|
-
options.foreignKeys.push(Object.assign(Object.assign({}, column.fk), { column: column.name }));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
this.sql.trimEnd(', ');
|
|
176
|
-
const primaryKeys = options.columns
|
|
177
|
-
.filter((column) => column.primaryKey)
|
|
178
|
-
.map((column) => column.name);
|
|
179
|
-
if (primaryKeys.length > 0) {
|
|
180
|
-
this.createTablePrimaryKeys(primaryKeys);
|
|
181
|
-
}
|
|
182
|
-
for (const uniqueColumn of options.columns.filter((column) => column.isUnique)) {
|
|
183
|
-
this.sql.append(', ');
|
|
184
|
-
this.uniqueConstraint(options.name, uniqueColumn.name);
|
|
185
|
-
}
|
|
186
|
-
for (const fk of (_a = options.foreignKeys) !== null && _a !== void 0 ? _a : []) {
|
|
187
|
-
this.sql.append(', ');
|
|
188
|
-
this.foreignKeyConstraint(options.name, fk);
|
|
189
|
-
}
|
|
190
|
-
this.sql.trimEnd(' ');
|
|
191
|
-
this.sql.append(')');
|
|
192
|
-
return this;
|
|
193
|
-
}
|
|
194
|
-
createTable(options) {
|
|
195
|
-
this.createTableStatement();
|
|
196
|
-
if (options.ifNotExists) {
|
|
197
|
-
this.ifNotExists();
|
|
198
|
-
}
|
|
199
|
-
this.sql.tableName(options.name);
|
|
200
|
-
this.sql.space();
|
|
201
|
-
this.createTableColumns(options);
|
|
202
|
-
this.createTableTriggers(options);
|
|
203
|
-
return this;
|
|
204
|
-
}
|
|
205
|
-
constraintStatement() {
|
|
206
|
-
this.sql.append('CONSTRAINT ');
|
|
207
|
-
return this;
|
|
208
|
-
}
|
|
209
|
-
createTableTriggers(options) {
|
|
210
|
-
const pk = options.columns.find((column) => column.primaryKey);
|
|
211
|
-
const triggers = options.columns
|
|
212
|
-
.filter((column) => column.triggers !== undefined)
|
|
213
|
-
.map((column) => column.triggers({
|
|
214
|
-
table: options.name,
|
|
215
|
-
column: column.name,
|
|
216
|
-
idColumn: pk.name,
|
|
217
|
-
}))
|
|
218
|
-
.flat();
|
|
219
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
220
|
-
let i = this;
|
|
221
|
-
for (const trigger of triggers) {
|
|
222
|
-
i.next = i.createNext();
|
|
223
|
-
i = i.next;
|
|
224
|
-
i.createTrigger(trigger.getTrigger({
|
|
225
|
-
queryBuilder: new this.queryBuilderType(),
|
|
226
|
-
}));
|
|
227
|
-
}
|
|
228
|
-
return this;
|
|
229
|
-
}
|
|
230
|
-
// ------------------------------------------------------------------------
|
|
231
|
-
// Indexes
|
|
232
|
-
// ------------------------------------------------------------------------
|
|
233
|
-
createIndex(options) {
|
|
234
|
-
if (!options.name) {
|
|
235
|
-
options.name = `idx_${options.table}_${options.column}`;
|
|
236
|
-
}
|
|
237
|
-
this.sql.append(`CREATE INDEX ${options.name} ON `);
|
|
238
|
-
this.sql.tableName(options.table);
|
|
239
|
-
this.sql.openParens();
|
|
240
|
-
this.sql.columnName(options.column);
|
|
241
|
-
this.sql.closeParens();
|
|
242
|
-
return this;
|
|
243
|
-
}
|
|
244
|
-
// ------------------------------------------------------------------------
|
|
245
|
-
// Grant permissions
|
|
246
|
-
// ------------------------------------------------------------------------
|
|
247
|
-
grantOnDatabase(database) {
|
|
248
|
-
this.sql.append(database + '.*');
|
|
249
|
-
return this;
|
|
250
|
-
}
|
|
251
|
-
grantOn(on) {
|
|
252
|
-
if (on === '*') {
|
|
253
|
-
this.sql.append('*.*');
|
|
254
|
-
}
|
|
255
|
-
else if ('database' in on && 'table' in on) {
|
|
256
|
-
this.sql.append(on.database + '.' + on.table);
|
|
257
|
-
}
|
|
258
|
-
else if ('database' in on) {
|
|
259
|
-
this.grantOnDatabase(on.database);
|
|
260
|
-
}
|
|
261
|
-
this.sql.space();
|
|
262
|
-
return this;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* IMPORTANT: UNSTABLE. Grant is currently only compatible with
|
|
266
|
-
* mysql and mssql and may cause unintended side-effects and/or
|
|
267
|
-
* privilege escalation.
|
|
268
|
-
* This should not be used in most circumstances!
|
|
269
|
-
* Use a dedicated database connection for this method, as it may change
|
|
270
|
-
* the database connection's default database.
|
|
271
|
-
* This method and it's arguments are likely to change in future versions
|
|
272
|
-
*
|
|
273
|
-
* @param options Grant options
|
|
274
|
-
* @returns
|
|
275
|
-
*/
|
|
276
|
-
grant(options) {
|
|
277
|
-
if (!Array.isArray(options.privileges)) {
|
|
278
|
-
options.privileges = [options.privileges];
|
|
279
|
-
}
|
|
280
|
-
if (!Array.isArray(options.to)) {
|
|
281
|
-
options.to = [options.to];
|
|
282
|
-
}
|
|
283
|
-
this.sql.append('GRANT ');
|
|
284
|
-
this.sql.append(options.privileges.join(', ') + ' ');
|
|
285
|
-
this.sql.append('ON ');
|
|
286
|
-
this.grantOn(options.on);
|
|
287
|
-
this.sql.append('TO ' + options.to.join(', ') + ' ');
|
|
288
|
-
return this;
|
|
289
|
-
}
|
|
290
|
-
// ------------------------------------------------------------------------
|
|
291
|
-
// Foreign Key Constraint
|
|
292
|
-
// ------------------------------------------------------------------------
|
|
293
|
-
foreignKeyStatement() {
|
|
294
|
-
this.sql.append('FOREIGN KEY ');
|
|
295
|
-
return this;
|
|
296
|
-
}
|
|
297
|
-
foreignKeyName(childTable, fk) {
|
|
298
|
-
var _a;
|
|
299
|
-
const columns = 'columns' in fk ? fk.columns : [fk.column];
|
|
300
|
-
this.sql.append((_a = fk.name) !== null && _a !== void 0 ? _a : `fk_${childTable}_${columns.join('_')}`);
|
|
301
|
-
this.sql.space();
|
|
302
|
-
return this;
|
|
303
|
-
}
|
|
304
|
-
foreignKeyColumns(columns) {
|
|
305
|
-
this.sql.openParens();
|
|
306
|
-
this.sql.commaSeparate(columns.map((column) => this.sql.getEnclosedName(column)));
|
|
307
|
-
this.sql.closeParens();
|
|
308
|
-
return this;
|
|
309
|
-
}
|
|
310
|
-
referencesStatement(table, columns) {
|
|
311
|
-
this.sql.append('REFERENCES ');
|
|
312
|
-
this.sql.tableName(table);
|
|
313
|
-
this.sql.openParens();
|
|
314
|
-
this.sql.commaSeparate(columns.map((column) => this.sql.getEnclosedName(column)));
|
|
315
|
-
this.sql.closeParens();
|
|
316
|
-
return this;
|
|
317
|
-
}
|
|
318
|
-
fkOnUpdate(option) {
|
|
319
|
-
this.sql.append('ON UPDATE ' + option + ' ');
|
|
320
|
-
return this;
|
|
321
|
-
}
|
|
322
|
-
fkOnDelete(option) {
|
|
323
|
-
this.sql.append('ON DELETE ' + option + ' ');
|
|
324
|
-
return this;
|
|
325
|
-
}
|
|
326
|
-
foreignKeyConstraint(childTable, fk) {
|
|
327
|
-
const columns = 'columns' in fk ? fk.columns : [fk.column];
|
|
328
|
-
const referencesColumns = 'referencesColumns' in fk
|
|
329
|
-
? fk.referencesColumns
|
|
330
|
-
: [fk.referencesColumn];
|
|
331
|
-
this.constraintStatement();
|
|
332
|
-
this.foreignKeyName(childTable, fk);
|
|
333
|
-
this.foreignKeyStatement();
|
|
334
|
-
this.foreignKeyColumns(columns);
|
|
335
|
-
this.referencesStatement(fk.referencesTable, referencesColumns);
|
|
336
|
-
if (fk.onUpdate) {
|
|
337
|
-
this.fkOnUpdate(fk.onUpdate);
|
|
338
|
-
}
|
|
339
|
-
if (fk.onDelete) {
|
|
340
|
-
this.fkOnDelete(fk.onDelete);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
// ------------------------------------------------------------------------
|
|
344
|
-
// Constraints
|
|
345
|
-
// ------------------------------------------------------------------------
|
|
346
|
-
disableForeignKeyChecks() {
|
|
347
|
-
this.sql.append('SET FOREIGN_KEY_CHECKS=0');
|
|
348
|
-
return this;
|
|
349
|
-
}
|
|
350
|
-
enableForeignKeyChecks() {
|
|
351
|
-
this.sql.append('SET FOREIGN_KEY_CHECKS=1');
|
|
352
|
-
return this;
|
|
353
|
-
}
|
|
354
|
-
uniqueConstraint(table, column) {
|
|
355
|
-
this.sql.append(`CONSTRAINT uq_${table}_${column} `);
|
|
356
|
-
this.sql.append('UNIQUE(' + this.sql.getEnclosedName(column) + ')');
|
|
357
|
-
return this;
|
|
358
|
-
}
|
|
359
|
-
// ------------------------------------------------------------------------
|
|
360
|
-
// Alter Table
|
|
361
|
-
// ------------------------------------------------------------------------
|
|
362
|
-
alterTableStatement(table) {
|
|
363
|
-
this.sql.append('ALTER TABLE ');
|
|
364
|
-
this.sql.tableName(table);
|
|
365
|
-
this.sql.space();
|
|
366
|
-
return this;
|
|
367
|
-
}
|
|
368
|
-
addColumns(options) {
|
|
369
|
-
this.alterTableStatement(options.table);
|
|
370
|
-
this.sql.append('ADD ');
|
|
371
|
-
const addForeignKeys = [];
|
|
372
|
-
for (const column of options.columns) {
|
|
373
|
-
this.createTableColumn(column);
|
|
374
|
-
this.sql.append(', ');
|
|
375
|
-
if (column.fk) {
|
|
376
|
-
addForeignKeys.push(Object.assign(Object.assign({}, column.fk), { column: column.name }));
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
this.sql.trimEnd(', ');
|
|
380
|
-
if (addForeignKeys.length) {
|
|
381
|
-
let i;
|
|
382
|
-
for (const fk of addForeignKeys) {
|
|
383
|
-
i = (i !== null && i !== void 0 ? i : this).createNext();
|
|
384
|
-
i.addForeignKey(Object.assign(Object.assign({}, fk), { table: options.table }));
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return this;
|
|
388
|
-
}
|
|
389
|
-
addForeignKey(options) {
|
|
390
|
-
this.alterTableStatement(options.table);
|
|
391
|
-
this.sql.append('ADD ');
|
|
392
|
-
this.foreignKeyConstraint(options.table, options);
|
|
393
|
-
return this;
|
|
394
|
-
}
|
|
395
|
-
alterColumnStatement(column) {
|
|
396
|
-
this.sql.append('ALTER COLUMN ');
|
|
397
|
-
this.sql.columnName(column);
|
|
398
|
-
this.sql.space();
|
|
399
|
-
return this;
|
|
400
|
-
}
|
|
401
|
-
changeColumn(options) {
|
|
402
|
-
this.alterTableStatement(options.table);
|
|
403
|
-
this.alterColumnStatement(options.column);
|
|
404
|
-
this.createTableColumn(options.options);
|
|
405
|
-
if (options.options.fk !== undefined) {
|
|
406
|
-
// TODO: Add changeColumn inline FK support
|
|
407
|
-
throw new Error('Foreign Keys are currently not supported with changeColumn(). ' +
|
|
408
|
-
'Please submit an issue on Github to request this feature.');
|
|
409
|
-
}
|
|
410
|
-
return this;
|
|
411
|
-
}
|
|
412
|
-
dropColumn(options) {
|
|
413
|
-
this.alterTableStatement(options.table);
|
|
414
|
-
this.sql.append('DROP COLUMN ');
|
|
415
|
-
this.sql.columnName(options.column);
|
|
416
|
-
return this;
|
|
417
|
-
}
|
|
418
|
-
dropForeignKey(options) {
|
|
419
|
-
this.alterTableStatement(options.table);
|
|
420
|
-
this.sql.append('DROP ');
|
|
421
|
-
this.foreignKeyStatement();
|
|
422
|
-
this.sql.append(options.fk);
|
|
423
|
-
return this;
|
|
424
|
-
}
|
|
425
|
-
renameTable(options) {
|
|
426
|
-
this.alterTableStatement(options.table);
|
|
427
|
-
this.sql.append('RENAME ');
|
|
428
|
-
this.sql.tableName(options.to);
|
|
429
|
-
return this;
|
|
430
|
-
}
|
|
431
|
-
// ------------------------------------------------------------------------
|
|
432
|
-
// Drop Database
|
|
433
|
-
// ------------------------------------------------------------------------
|
|
434
|
-
dropDatabase(options) {
|
|
435
|
-
this.sql.append('DROP DATABASE ');
|
|
436
|
-
if (options.ifExists) {
|
|
437
|
-
this.sql.append('IF EXISTS ');
|
|
438
|
-
}
|
|
439
|
-
this.sql.append(options.name);
|
|
440
|
-
return this;
|
|
441
|
-
}
|
|
442
|
-
// ------------------------------------------------------------------------
|
|
443
|
-
// Drop Table
|
|
444
|
-
// ------------------------------------------------------------------------
|
|
445
|
-
dropTable(options) {
|
|
446
|
-
if (Array.isArray(options.tables)) {
|
|
447
|
-
options.tables = options.tables.join(',');
|
|
448
|
-
}
|
|
449
|
-
this.sql.append('DROP TABLE ');
|
|
450
|
-
if (options.ifExists) {
|
|
451
|
-
this.sql.append('IF EXISTS ');
|
|
452
|
-
}
|
|
453
|
-
this.sql.append(options.tables
|
|
454
|
-
.split(',')
|
|
455
|
-
.map((table) => this.sql.getEnclosedName(table))
|
|
456
|
-
.join(','));
|
|
457
|
-
return this;
|
|
458
|
-
}
|
|
459
|
-
// ------------------------------------------------------------------------
|
|
460
|
-
// Drop User
|
|
461
|
-
// ------------------------------------------------------------------------
|
|
462
|
-
dropUser(options) {
|
|
463
|
-
if (Array.isArray(options.names)) {
|
|
464
|
-
options.names = options.names.join(',');
|
|
465
|
-
}
|
|
466
|
-
this.sql.append('DROP USER ');
|
|
467
|
-
if (options.ifExists) {
|
|
468
|
-
this.sql.append('IF EXISTS ');
|
|
469
|
-
}
|
|
470
|
-
this.sql.append(options.names);
|
|
471
|
-
return this;
|
|
472
|
-
}
|
|
473
|
-
// ------------------------------------------------------------------------
|
|
474
|
-
// Truncate Table
|
|
475
|
-
// ------------------------------------------------------------------------
|
|
476
|
-
truncate(options) {
|
|
477
|
-
this.sql.append('TRUNCATE TABLE ');
|
|
478
|
-
this.sql.tableName(options.table);
|
|
479
|
-
return this;
|
|
480
|
-
}
|
|
481
|
-
// ------------------------------------------------------------------------
|
|
482
|
-
// Triggers
|
|
483
|
-
// ------------------------------------------------------------------------
|
|
484
|
-
createTrigger(options) {
|
|
485
|
-
this.createTriggerStatement();
|
|
486
|
-
if (!options.name) {
|
|
487
|
-
options.name = this.getTriggerName(options);
|
|
488
|
-
}
|
|
489
|
-
this.sql.append(options.name + ' ');
|
|
490
|
-
this.createTriggerTableEvent(options);
|
|
491
|
-
this.createTriggerForEachRow();
|
|
492
|
-
this.createTriggerBody(options.body);
|
|
493
|
-
this.sql.endStatement();
|
|
494
|
-
return this;
|
|
495
|
-
}
|
|
496
|
-
createTriggerStatement() {
|
|
497
|
-
this.sql.append('CREATE TRIGGER ');
|
|
498
|
-
return this;
|
|
499
|
-
}
|
|
500
|
-
getTriggerName(options) {
|
|
501
|
-
return (`${options.table}_` +
|
|
502
|
-
`${options.timing.toLowerCase()}_` +
|
|
503
|
-
`${options.event.toLowerCase()}`);
|
|
504
|
-
}
|
|
505
|
-
createTriggerTableEvent(options) {
|
|
506
|
-
this.createTriggerEvent(options);
|
|
507
|
-
this.createTriggerTable(options.table);
|
|
508
|
-
return this;
|
|
509
|
-
}
|
|
510
|
-
createTriggerTable(table) {
|
|
511
|
-
this.sql.append('ON ');
|
|
512
|
-
this.sql.tableName(table);
|
|
513
|
-
this.sql.space();
|
|
514
|
-
return this;
|
|
515
|
-
}
|
|
516
|
-
createTriggerEvent(options) {
|
|
517
|
-
this.sql.append(options.timing + ' ' + options.event + ' ');
|
|
518
|
-
return this;
|
|
519
|
-
}
|
|
520
|
-
createTriggerBody(sql) {
|
|
521
|
-
this.createTriggerBeginStatement();
|
|
522
|
-
if (typeof sql === 'string') {
|
|
523
|
-
this.sql.append(sql);
|
|
524
|
-
}
|
|
525
|
-
else if (sql instanceof dml_1.DatabaseQueryBuilder) {
|
|
526
|
-
this.appendBuilder(sql);
|
|
527
|
-
}
|
|
528
|
-
this.sql.endStatement();
|
|
529
|
-
this.createTriggerEndStatement();
|
|
530
|
-
return this;
|
|
531
|
-
}
|
|
532
|
-
createTriggerBeginStatement() {
|
|
533
|
-
this.sql.append('BEGIN ');
|
|
534
|
-
return this;
|
|
535
|
-
}
|
|
536
|
-
createTriggerEndStatement() {
|
|
537
|
-
this.sql.append('END');
|
|
538
|
-
return this;
|
|
539
|
-
}
|
|
540
|
-
createTriggerForEachRow() {
|
|
541
|
-
this.sql.append('FOR EACH ROW ');
|
|
542
|
-
return this;
|
|
543
|
-
}
|
|
544
|
-
dropTrigger(options) {
|
|
545
|
-
this.dropTriggerStatement();
|
|
546
|
-
if (options.ifExists) {
|
|
547
|
-
this.ifExists();
|
|
548
|
-
}
|
|
549
|
-
this.dropTriggerName(options);
|
|
550
|
-
return this;
|
|
551
|
-
}
|
|
552
|
-
dropTriggerStatement() {
|
|
553
|
-
this.sql.append('DROP TRIGGER ');
|
|
554
|
-
return this;
|
|
555
|
-
}
|
|
556
|
-
ifExists() {
|
|
557
|
-
this.sql.append('IF EXISTS ');
|
|
558
|
-
return this;
|
|
559
|
-
}
|
|
560
|
-
dropTriggerName(options) {
|
|
561
|
-
this.sql.columnName(options.name);
|
|
562
|
-
return this;
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
exports.DataDefinitionBuilder = DataDefinitionBuilder;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataDefinitionBuilder = void 0;
|
|
4
|
+
const statement_builder_1 = require("../builder/statement-builder");
|
|
5
|
+
const column_1 = require("../column");
|
|
6
|
+
const dml_1 = require("../dml");
|
|
7
|
+
const expression_1 = require("../expression");
|
|
8
|
+
const functions_1 = require("../functions");
|
|
9
|
+
const function_token_1 = require("../functions/function-token");
|
|
10
|
+
class DataDefinitionBuilder extends statement_builder_1.StatementBuilder {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(...arguments);
|
|
13
|
+
this.columnTypes = column_1.ColumnType;
|
|
14
|
+
this.queryBuilderType = dml_1.DatabaseQueryBuilder;
|
|
15
|
+
}
|
|
16
|
+
getQueryBuilder() {
|
|
17
|
+
return new this.queryBuilderType();
|
|
18
|
+
}
|
|
19
|
+
getColumnTypes() {
|
|
20
|
+
return this.columnTypes;
|
|
21
|
+
}
|
|
22
|
+
// ------------------------------------------------------------------------
|
|
23
|
+
// Create Database
|
|
24
|
+
// ------------------------------------------------------------------------
|
|
25
|
+
createDatabaseStatement() {
|
|
26
|
+
this.sql.append('CREATE DATABASE ');
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
createDatabase(options) {
|
|
30
|
+
this.createDatabaseStatement();
|
|
31
|
+
this.sql.append(options.name);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
// ------------------------------------------------------------------------
|
|
35
|
+
// Create User
|
|
36
|
+
// ------------------------------------------------------------------------
|
|
37
|
+
createUserStatement() {
|
|
38
|
+
this.sql.append('CREATE USER ');
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
createUserPassword(password) {
|
|
42
|
+
this.sql.append(`WITH PASSWORD '${password}' `);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
createUser(options) {
|
|
46
|
+
this.createUserStatement();
|
|
47
|
+
this.sql.append(options.name + ' ');
|
|
48
|
+
if (options.password) {
|
|
49
|
+
this.createUserPassword(options.password);
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
// ------------------------------------------------------------------------
|
|
54
|
+
// Create Table
|
|
55
|
+
// ------------------------------------------------------------------------
|
|
56
|
+
createTableStatement() {
|
|
57
|
+
this.sql.append('CREATE TABLE ');
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
ifNotExists() {
|
|
61
|
+
this.sql.append('IF NOT EXISTS ');
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
createColumnType(column) {
|
|
65
|
+
this.sql.append(this.columnTypes[column.type]);
|
|
66
|
+
if ('length' in column) {
|
|
67
|
+
this.sql.append(`(${column.length})`);
|
|
68
|
+
}
|
|
69
|
+
if ('significant' in column && 'decimal' in column) {
|
|
70
|
+
const significant = column.significant + column.decimal;
|
|
71
|
+
const decimal = column.decimal;
|
|
72
|
+
this.sql.append(`(${significant}, ${decimal})`);
|
|
73
|
+
}
|
|
74
|
+
this.sql.space();
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
columnRequired() {
|
|
78
|
+
this.sql.append('NOT NULL ');
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
columnDefaultValue(column) {
|
|
82
|
+
this.sql.append('DEFAULT ');
|
|
83
|
+
if (column.default === null) {
|
|
84
|
+
this.columnDefaultNull();
|
|
85
|
+
}
|
|
86
|
+
else if (column.default === true) {
|
|
87
|
+
this.columnDefaultTrue();
|
|
88
|
+
}
|
|
89
|
+
else if (column.default === false) {
|
|
90
|
+
this.columnDefaultFalse();
|
|
91
|
+
}
|
|
92
|
+
else if ((0, expression_1.isExpressionToken)(column.default)) {
|
|
93
|
+
// CURRENT_TIMESTAMP does not work when wrapped in parenthesis for
|
|
94
|
+
// default value; but all other expressions should be wrapped
|
|
95
|
+
let wrapParens = true;
|
|
96
|
+
if ((0, functions_1.isDatabaseFunction)(column.default) &&
|
|
97
|
+
column.default.fn ===
|
|
98
|
+
function_token_1.DatabaseFunctionKeys.CURRENT_TIMESTAMP) {
|
|
99
|
+
wrapParens = false;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
this.sql.openParens();
|
|
103
|
+
}
|
|
104
|
+
const dml = this.getQueryBuilder();
|
|
105
|
+
dml.expression(column.default);
|
|
106
|
+
this.appendBuilder(dml);
|
|
107
|
+
if (wrapParens) {
|
|
108
|
+
this.sql.closeParens();
|
|
109
|
+
}
|
|
110
|
+
this.sql.append(' ');
|
|
111
|
+
}
|
|
112
|
+
else if (typeof column.default === 'string') {
|
|
113
|
+
const escaped = column.default
|
|
114
|
+
.replace(/\\/g, '\\\\')
|
|
115
|
+
.replace(/'/g, '\\\'')
|
|
116
|
+
.replace(/"/g, '\\"');
|
|
117
|
+
this.sql.append(`'${escaped}' `);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
this.sql.append(column.default + ' ');
|
|
121
|
+
}
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
columnDefaultNull() {
|
|
125
|
+
this.sql.append('NULL ');
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
columnDefaultTrue() {
|
|
129
|
+
this.sql.append('TRUE ');
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
columnDefaultFalse() {
|
|
133
|
+
this.sql.append('FALSE ');
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
columnAutoIncrement() {
|
|
137
|
+
this.sql.append('AUTO_INCREMENT ');
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
createTableColumn(column) {
|
|
141
|
+
this.sql.columnName(column.name);
|
|
142
|
+
this.sql.space();
|
|
143
|
+
this.createColumnType(column);
|
|
144
|
+
if (column.required) {
|
|
145
|
+
this.columnRequired();
|
|
146
|
+
}
|
|
147
|
+
if (column.default !== undefined) {
|
|
148
|
+
this.columnDefaultValue(column);
|
|
149
|
+
}
|
|
150
|
+
if (column.autoIncrement) {
|
|
151
|
+
this.columnAutoIncrement();
|
|
152
|
+
}
|
|
153
|
+
this.sql.trimEnd(' ');
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
createTablePrimaryKeys(names) {
|
|
157
|
+
this.sql.append(', PRIMARY KEY (');
|
|
158
|
+
this.sql.append(names.map((key) => this.sql.getEnclosedName(key)).join(','));
|
|
159
|
+
this.sql.append(')');
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
createTableColumns(options) {
|
|
163
|
+
var _a;
|
|
164
|
+
this.sql.append('(');
|
|
165
|
+
if (options.foreignKeys === undefined) {
|
|
166
|
+
options.foreignKeys = [];
|
|
167
|
+
}
|
|
168
|
+
for (const column of options.columns) {
|
|
169
|
+
this.createTableColumn(column);
|
|
170
|
+
this.sql.append(', ');
|
|
171
|
+
if ('fk' in column) {
|
|
172
|
+
options.foreignKeys.push(Object.assign(Object.assign({}, column.fk), { column: column.name }));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
this.sql.trimEnd(', ');
|
|
176
|
+
const primaryKeys = options.columns
|
|
177
|
+
.filter((column) => column.primaryKey)
|
|
178
|
+
.map((column) => column.name);
|
|
179
|
+
if (primaryKeys.length > 0) {
|
|
180
|
+
this.createTablePrimaryKeys(primaryKeys);
|
|
181
|
+
}
|
|
182
|
+
for (const uniqueColumn of options.columns.filter((column) => column.isUnique)) {
|
|
183
|
+
this.sql.append(', ');
|
|
184
|
+
this.uniqueConstraint(options.name, uniqueColumn.name);
|
|
185
|
+
}
|
|
186
|
+
for (const fk of (_a = options.foreignKeys) !== null && _a !== void 0 ? _a : []) {
|
|
187
|
+
this.sql.append(', ');
|
|
188
|
+
this.foreignKeyConstraint(options.name, fk);
|
|
189
|
+
}
|
|
190
|
+
this.sql.trimEnd(' ');
|
|
191
|
+
this.sql.append(')');
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
createTable(options) {
|
|
195
|
+
this.createTableStatement();
|
|
196
|
+
if (options.ifNotExists) {
|
|
197
|
+
this.ifNotExists();
|
|
198
|
+
}
|
|
199
|
+
this.sql.tableName(options.name);
|
|
200
|
+
this.sql.space();
|
|
201
|
+
this.createTableColumns(options);
|
|
202
|
+
this.createTableTriggers(options);
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
constraintStatement() {
|
|
206
|
+
this.sql.append('CONSTRAINT ');
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
createTableTriggers(options) {
|
|
210
|
+
const pk = options.columns.find((column) => column.primaryKey);
|
|
211
|
+
const triggers = options.columns
|
|
212
|
+
.filter((column) => column.triggers !== undefined)
|
|
213
|
+
.map((column) => column.triggers({
|
|
214
|
+
table: options.name,
|
|
215
|
+
column: column.name,
|
|
216
|
+
idColumn: pk.name,
|
|
217
|
+
}))
|
|
218
|
+
.flat();
|
|
219
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
220
|
+
let i = this;
|
|
221
|
+
for (const trigger of triggers) {
|
|
222
|
+
i.next = i.createNext();
|
|
223
|
+
i = i.next;
|
|
224
|
+
i.createTrigger(trigger.getTrigger({
|
|
225
|
+
queryBuilder: new this.queryBuilderType(),
|
|
226
|
+
}));
|
|
227
|
+
}
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
// ------------------------------------------------------------------------
|
|
231
|
+
// Indexes
|
|
232
|
+
// ------------------------------------------------------------------------
|
|
233
|
+
createIndex(options) {
|
|
234
|
+
if (!options.name) {
|
|
235
|
+
options.name = `idx_${options.table}_${options.column}`;
|
|
236
|
+
}
|
|
237
|
+
this.sql.append(`CREATE INDEX ${options.name} ON `);
|
|
238
|
+
this.sql.tableName(options.table);
|
|
239
|
+
this.sql.openParens();
|
|
240
|
+
this.sql.columnName(options.column);
|
|
241
|
+
this.sql.closeParens();
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
// ------------------------------------------------------------------------
|
|
245
|
+
// Grant permissions
|
|
246
|
+
// ------------------------------------------------------------------------
|
|
247
|
+
grantOnDatabase(database) {
|
|
248
|
+
this.sql.append(database + '.*');
|
|
249
|
+
return this;
|
|
250
|
+
}
|
|
251
|
+
grantOn(on) {
|
|
252
|
+
if (on === '*') {
|
|
253
|
+
this.sql.append('*.*');
|
|
254
|
+
}
|
|
255
|
+
else if ('database' in on && 'table' in on) {
|
|
256
|
+
this.sql.append(on.database + '.' + on.table);
|
|
257
|
+
}
|
|
258
|
+
else if ('database' in on) {
|
|
259
|
+
this.grantOnDatabase(on.database);
|
|
260
|
+
}
|
|
261
|
+
this.sql.space();
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* IMPORTANT: UNSTABLE. Grant is currently only compatible with
|
|
266
|
+
* mysql and mssql and may cause unintended side-effects and/or
|
|
267
|
+
* privilege escalation.
|
|
268
|
+
* This should not be used in most circumstances!
|
|
269
|
+
* Use a dedicated database connection for this method, as it may change
|
|
270
|
+
* the database connection's default database.
|
|
271
|
+
* This method and it's arguments are likely to change in future versions
|
|
272
|
+
*
|
|
273
|
+
* @param options Grant options
|
|
274
|
+
* @returns
|
|
275
|
+
*/
|
|
276
|
+
grant(options) {
|
|
277
|
+
if (!Array.isArray(options.privileges)) {
|
|
278
|
+
options.privileges = [options.privileges];
|
|
279
|
+
}
|
|
280
|
+
if (!Array.isArray(options.to)) {
|
|
281
|
+
options.to = [options.to];
|
|
282
|
+
}
|
|
283
|
+
this.sql.append('GRANT ');
|
|
284
|
+
this.sql.append(options.privileges.join(', ') + ' ');
|
|
285
|
+
this.sql.append('ON ');
|
|
286
|
+
this.grantOn(options.on);
|
|
287
|
+
this.sql.append('TO ' + options.to.join(', ') + ' ');
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
// ------------------------------------------------------------------------
|
|
291
|
+
// Foreign Key Constraint
|
|
292
|
+
// ------------------------------------------------------------------------
|
|
293
|
+
foreignKeyStatement() {
|
|
294
|
+
this.sql.append('FOREIGN KEY ');
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
foreignKeyName(childTable, fk) {
|
|
298
|
+
var _a;
|
|
299
|
+
const columns = 'columns' in fk ? fk.columns : [fk.column];
|
|
300
|
+
this.sql.append((_a = fk.name) !== null && _a !== void 0 ? _a : `fk_${childTable}_${columns.join('_')}`);
|
|
301
|
+
this.sql.space();
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
foreignKeyColumns(columns) {
|
|
305
|
+
this.sql.openParens();
|
|
306
|
+
this.sql.commaSeparate(columns.map((column) => this.sql.getEnclosedName(column)));
|
|
307
|
+
this.sql.closeParens();
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
referencesStatement(table, columns) {
|
|
311
|
+
this.sql.append('REFERENCES ');
|
|
312
|
+
this.sql.tableName(table);
|
|
313
|
+
this.sql.openParens();
|
|
314
|
+
this.sql.commaSeparate(columns.map((column) => this.sql.getEnclosedName(column)));
|
|
315
|
+
this.sql.closeParens();
|
|
316
|
+
return this;
|
|
317
|
+
}
|
|
318
|
+
fkOnUpdate(option) {
|
|
319
|
+
this.sql.append('ON UPDATE ' + option + ' ');
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
fkOnDelete(option) {
|
|
323
|
+
this.sql.append('ON DELETE ' + option + ' ');
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
326
|
+
foreignKeyConstraint(childTable, fk) {
|
|
327
|
+
const columns = 'columns' in fk ? fk.columns : [fk.column];
|
|
328
|
+
const referencesColumns = 'referencesColumns' in fk
|
|
329
|
+
? fk.referencesColumns
|
|
330
|
+
: [fk.referencesColumn];
|
|
331
|
+
this.constraintStatement();
|
|
332
|
+
this.foreignKeyName(childTable, fk);
|
|
333
|
+
this.foreignKeyStatement();
|
|
334
|
+
this.foreignKeyColumns(columns);
|
|
335
|
+
this.referencesStatement(fk.referencesTable, referencesColumns);
|
|
336
|
+
if (fk.onUpdate) {
|
|
337
|
+
this.fkOnUpdate(fk.onUpdate);
|
|
338
|
+
}
|
|
339
|
+
if (fk.onDelete) {
|
|
340
|
+
this.fkOnDelete(fk.onDelete);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
// ------------------------------------------------------------------------
|
|
344
|
+
// Constraints
|
|
345
|
+
// ------------------------------------------------------------------------
|
|
346
|
+
disableForeignKeyChecks() {
|
|
347
|
+
this.sql.append('SET FOREIGN_KEY_CHECKS=0');
|
|
348
|
+
return this;
|
|
349
|
+
}
|
|
350
|
+
enableForeignKeyChecks() {
|
|
351
|
+
this.sql.append('SET FOREIGN_KEY_CHECKS=1');
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
uniqueConstraint(table, column) {
|
|
355
|
+
this.sql.append(`CONSTRAINT uq_${table}_${column} `);
|
|
356
|
+
this.sql.append('UNIQUE(' + this.sql.getEnclosedName(column) + ')');
|
|
357
|
+
return this;
|
|
358
|
+
}
|
|
359
|
+
// ------------------------------------------------------------------------
|
|
360
|
+
// Alter Table
|
|
361
|
+
// ------------------------------------------------------------------------
|
|
362
|
+
alterTableStatement(table) {
|
|
363
|
+
this.sql.append('ALTER TABLE ');
|
|
364
|
+
this.sql.tableName(table);
|
|
365
|
+
this.sql.space();
|
|
366
|
+
return this;
|
|
367
|
+
}
|
|
368
|
+
addColumns(options) {
|
|
369
|
+
this.alterTableStatement(options.table);
|
|
370
|
+
this.sql.append('ADD ');
|
|
371
|
+
const addForeignKeys = [];
|
|
372
|
+
for (const column of options.columns) {
|
|
373
|
+
this.createTableColumn(column);
|
|
374
|
+
this.sql.append(', ');
|
|
375
|
+
if (column.fk) {
|
|
376
|
+
addForeignKeys.push(Object.assign(Object.assign({}, column.fk), { column: column.name }));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
this.sql.trimEnd(', ');
|
|
380
|
+
if (addForeignKeys.length) {
|
|
381
|
+
let i;
|
|
382
|
+
for (const fk of addForeignKeys) {
|
|
383
|
+
i = (i !== null && i !== void 0 ? i : this).createNext();
|
|
384
|
+
i.addForeignKey(Object.assign(Object.assign({}, fk), { table: options.table }));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return this;
|
|
388
|
+
}
|
|
389
|
+
addForeignKey(options) {
|
|
390
|
+
this.alterTableStatement(options.table);
|
|
391
|
+
this.sql.append('ADD ');
|
|
392
|
+
this.foreignKeyConstraint(options.table, options);
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
alterColumnStatement(column) {
|
|
396
|
+
this.sql.append('ALTER COLUMN ');
|
|
397
|
+
this.sql.columnName(column);
|
|
398
|
+
this.sql.space();
|
|
399
|
+
return this;
|
|
400
|
+
}
|
|
401
|
+
changeColumn(options) {
|
|
402
|
+
this.alterTableStatement(options.table);
|
|
403
|
+
this.alterColumnStatement(options.column);
|
|
404
|
+
this.createTableColumn(options.options);
|
|
405
|
+
if (options.options.fk !== undefined) {
|
|
406
|
+
// TODO: Add changeColumn inline FK support
|
|
407
|
+
throw new Error('Foreign Keys are currently not supported with changeColumn(). ' +
|
|
408
|
+
'Please submit an issue on Github to request this feature.');
|
|
409
|
+
}
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
dropColumn(options) {
|
|
413
|
+
this.alterTableStatement(options.table);
|
|
414
|
+
this.sql.append('DROP COLUMN ');
|
|
415
|
+
this.sql.columnName(options.column);
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
418
|
+
dropForeignKey(options) {
|
|
419
|
+
this.alterTableStatement(options.table);
|
|
420
|
+
this.sql.append('DROP ');
|
|
421
|
+
this.foreignKeyStatement();
|
|
422
|
+
this.sql.append(options.fk);
|
|
423
|
+
return this;
|
|
424
|
+
}
|
|
425
|
+
renameTable(options) {
|
|
426
|
+
this.alterTableStatement(options.table);
|
|
427
|
+
this.sql.append('RENAME ');
|
|
428
|
+
this.sql.tableName(options.to);
|
|
429
|
+
return this;
|
|
430
|
+
}
|
|
431
|
+
// ------------------------------------------------------------------------
|
|
432
|
+
// Drop Database
|
|
433
|
+
// ------------------------------------------------------------------------
|
|
434
|
+
dropDatabase(options) {
|
|
435
|
+
this.sql.append('DROP DATABASE ');
|
|
436
|
+
if (options.ifExists) {
|
|
437
|
+
this.sql.append('IF EXISTS ');
|
|
438
|
+
}
|
|
439
|
+
this.sql.append(options.name);
|
|
440
|
+
return this;
|
|
441
|
+
}
|
|
442
|
+
// ------------------------------------------------------------------------
|
|
443
|
+
// Drop Table
|
|
444
|
+
// ------------------------------------------------------------------------
|
|
445
|
+
dropTable(options) {
|
|
446
|
+
if (Array.isArray(options.tables)) {
|
|
447
|
+
options.tables = options.tables.join(',');
|
|
448
|
+
}
|
|
449
|
+
this.sql.append('DROP TABLE ');
|
|
450
|
+
if (options.ifExists) {
|
|
451
|
+
this.sql.append('IF EXISTS ');
|
|
452
|
+
}
|
|
453
|
+
this.sql.append(options.tables
|
|
454
|
+
.split(',')
|
|
455
|
+
.map((table) => this.sql.getEnclosedName(table))
|
|
456
|
+
.join(','));
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
459
|
+
// ------------------------------------------------------------------------
|
|
460
|
+
// Drop User
|
|
461
|
+
// ------------------------------------------------------------------------
|
|
462
|
+
dropUser(options) {
|
|
463
|
+
if (Array.isArray(options.names)) {
|
|
464
|
+
options.names = options.names.join(',');
|
|
465
|
+
}
|
|
466
|
+
this.sql.append('DROP USER ');
|
|
467
|
+
if (options.ifExists) {
|
|
468
|
+
this.sql.append('IF EXISTS ');
|
|
469
|
+
}
|
|
470
|
+
this.sql.append(options.names);
|
|
471
|
+
return this;
|
|
472
|
+
}
|
|
473
|
+
// ------------------------------------------------------------------------
|
|
474
|
+
// Truncate Table
|
|
475
|
+
// ------------------------------------------------------------------------
|
|
476
|
+
truncate(options) {
|
|
477
|
+
this.sql.append('TRUNCATE TABLE ');
|
|
478
|
+
this.sql.tableName(options.table);
|
|
479
|
+
return this;
|
|
480
|
+
}
|
|
481
|
+
// ------------------------------------------------------------------------
|
|
482
|
+
// Triggers
|
|
483
|
+
// ------------------------------------------------------------------------
|
|
484
|
+
createTrigger(options) {
|
|
485
|
+
this.createTriggerStatement();
|
|
486
|
+
if (!options.name) {
|
|
487
|
+
options.name = this.getTriggerName(options);
|
|
488
|
+
}
|
|
489
|
+
this.sql.append(options.name + ' ');
|
|
490
|
+
this.createTriggerTableEvent(options);
|
|
491
|
+
this.createTriggerForEachRow();
|
|
492
|
+
this.createTriggerBody(options.body);
|
|
493
|
+
this.sql.endStatement();
|
|
494
|
+
return this;
|
|
495
|
+
}
|
|
496
|
+
createTriggerStatement() {
|
|
497
|
+
this.sql.append('CREATE TRIGGER ');
|
|
498
|
+
return this;
|
|
499
|
+
}
|
|
500
|
+
getTriggerName(options) {
|
|
501
|
+
return (`${options.table}_` +
|
|
502
|
+
`${options.timing.toLowerCase()}_` +
|
|
503
|
+
`${options.event.toLowerCase()}`);
|
|
504
|
+
}
|
|
505
|
+
createTriggerTableEvent(options) {
|
|
506
|
+
this.createTriggerEvent(options);
|
|
507
|
+
this.createTriggerTable(options.table);
|
|
508
|
+
return this;
|
|
509
|
+
}
|
|
510
|
+
createTriggerTable(table) {
|
|
511
|
+
this.sql.append('ON ');
|
|
512
|
+
this.sql.tableName(table);
|
|
513
|
+
this.sql.space();
|
|
514
|
+
return this;
|
|
515
|
+
}
|
|
516
|
+
createTriggerEvent(options) {
|
|
517
|
+
this.sql.append(options.timing + ' ' + options.event + ' ');
|
|
518
|
+
return this;
|
|
519
|
+
}
|
|
520
|
+
createTriggerBody(sql) {
|
|
521
|
+
this.createTriggerBeginStatement();
|
|
522
|
+
if (typeof sql === 'string') {
|
|
523
|
+
this.sql.append(sql);
|
|
524
|
+
}
|
|
525
|
+
else if (sql instanceof dml_1.DatabaseQueryBuilder) {
|
|
526
|
+
this.appendBuilder(sql);
|
|
527
|
+
}
|
|
528
|
+
this.sql.endStatement();
|
|
529
|
+
this.createTriggerEndStatement();
|
|
530
|
+
return this;
|
|
531
|
+
}
|
|
532
|
+
createTriggerBeginStatement() {
|
|
533
|
+
this.sql.append('BEGIN ');
|
|
534
|
+
return this;
|
|
535
|
+
}
|
|
536
|
+
createTriggerEndStatement() {
|
|
537
|
+
this.sql.append('END');
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
createTriggerForEachRow() {
|
|
541
|
+
this.sql.append('FOR EACH ROW ');
|
|
542
|
+
return this;
|
|
543
|
+
}
|
|
544
|
+
dropTrigger(options) {
|
|
545
|
+
this.dropTriggerStatement();
|
|
546
|
+
if (options.ifExists) {
|
|
547
|
+
this.ifExists();
|
|
548
|
+
}
|
|
549
|
+
this.dropTriggerName(options);
|
|
550
|
+
return this;
|
|
551
|
+
}
|
|
552
|
+
dropTriggerStatement() {
|
|
553
|
+
this.sql.append('DROP TRIGGER ');
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
ifExists() {
|
|
557
|
+
this.sql.append('IF EXISTS ');
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
dropTriggerName(options) {
|
|
561
|
+
this.sql.columnName(options.name);
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
exports.DataDefinitionBuilder = DataDefinitionBuilder;
|
|
566
566
|
//# sourceMappingURL=ddl-builder.js.map
|