@riao/dbal 3.8.0 → 3.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/LICENSE.md +20 -20
  3. package/README.md +148 -148
  4. package/builder/builder.d.ts +7 -7
  5. package/builder/builder.js +19 -19
  6. package/builder/index.d.ts +2 -2
  7. package/builder/index.js +7 -7
  8. package/builder/sql-builder.d.ts +53 -53
  9. package/builder/sql-builder.js +157 -157
  10. package/builder/statement-builder.d.ts +12 -12
  11. package/builder/statement-builder.js +32 -32
  12. package/column/column-options.d.ts +156 -156
  13. package/column/column-options.js +2 -2
  14. package/column/column-type.d.ts +18 -18
  15. package/column/column-type.js +27 -27
  16. package/column/column-values.d.ts +34 -34
  17. package/column/column-values.js +2 -2
  18. package/column/index.d.ts +3 -3
  19. package/column/index.js +19 -19
  20. package/column-pack/date-time/create-timestamp-column.d.ts +2 -2
  21. package/column-pack/date-time/create-timestamp-column.js +10 -10
  22. package/column-pack/date-time/index.d.ts +2 -2
  23. package/column-pack/date-time/index.js +7 -7
  24. package/column-pack/date-time/update-timestamp-column.d.ts +2 -2
  25. package/column-pack/date-time/update-timestamp-column.js +14 -14
  26. package/column-pack/index.d.ts +6 -6
  27. package/column-pack/index.js +22 -22
  28. package/column-pack/keys/big-int-key-column.d.ts +2 -2
  29. package/column-pack/keys/big-int-key-column.js +10 -10
  30. package/column-pack/keys/index.d.ts +3 -3
  31. package/column-pack/keys/index.js +9 -9
  32. package/column-pack/keys/int-key-column.d.ts +2 -2
  33. package/column-pack/keys/int-key-column.js +10 -10
  34. package/column-pack/keys/uuid-key-column.d.ts +2 -2
  35. package/column-pack/keys/uuid-key-column.js +11 -11
  36. package/column-pack/person/date-of-birth-column.d.ts +2 -2
  37. package/column-pack/person/date-of-birth-column.js +8 -8
  38. package/column-pack/person/first-name-column.d.ts +2 -2
  39. package/column-pack/person/first-name-column.js +9 -9
  40. package/column-pack/person/index.d.ts +3 -3
  41. package/column-pack/person/index.js +9 -9
  42. package/column-pack/person/last-name-column.d.ts +2 -2
  43. package/column-pack/person/last-name-column.js +9 -9
  44. package/column-pack/resource/content-column.d.ts +2 -2
  45. package/column-pack/resource/content-column.js +9 -9
  46. package/column-pack/resource/description-column.d.ts +2 -2
  47. package/column-pack/resource/description-column.js +9 -9
  48. package/column-pack/resource/index.d.ts +6 -6
  49. package/column-pack/resource/index.js +15 -15
  50. package/column-pack/resource/is-active-column.d.ts +2 -2
  51. package/column-pack/resource/is-active-column.js +9 -9
  52. package/column-pack/resource/name-column.d.ts +2 -2
  53. package/column-pack/resource/name-column.js +10 -10
  54. package/column-pack/resource/path-column.d.ts +2 -2
  55. package/column-pack/resource/path-column.js +11 -11
  56. package/column-pack/resource/title-column.d.ts +2 -2
  57. package/column-pack/resource/title-column.js +10 -10
  58. package/column-pack/system/filename-column.d.ts +2 -2
  59. package/column-pack/system/filename-column.js +9 -9
  60. package/column-pack/system/filepath-column.d.ts +2 -2
  61. package/column-pack/system/filepath-column.js +9 -9
  62. package/column-pack/system/index.d.ts +4 -4
  63. package/column-pack/system/index.js +11 -11
  64. package/column-pack/system/ip-address-column.d.ts +2 -2
  65. package/column-pack/system/ip-address-column.js +9 -9
  66. package/column-pack/system/url-column.d.ts +3 -3
  67. package/column-pack/system/url-column.js +13 -13
  68. package/column-pack/user/email-column.d.ts +2 -2
  69. package/column-pack/user/email-column.js +11 -11
  70. package/column-pack/user/index.d.ts +4 -4
  71. package/column-pack/user/index.js +11 -11
  72. package/column-pack/user/password-column.d.ts +2 -2
  73. package/column-pack/user/password-column.js +10 -10
  74. package/column-pack/user/temp-email-column.d.ts +2 -2
  75. package/column-pack/user/temp-email-column.js +5 -5
  76. package/column-pack/user/username-column.d.ts +2 -2
  77. package/column-pack/user/username-column.js +11 -11
  78. package/comparison/comparison-aliases.d.ts +10 -10
  79. package/comparison/comparison-aliases.js +40 -40
  80. package/comparison/comparison-token.d.ts +17 -17
  81. package/comparison/comparison-token.js +15 -15
  82. package/comparison/comparison.d.ts +13 -13
  83. package/comparison/comparison.js +71 -71
  84. package/comparison/index.d.ts +4 -4
  85. package/comparison/index.js +24 -24
  86. package/comparison/is-comparison-token.d.ts +2 -2
  87. package/comparison/is-comparison-token.js +8 -8
  88. package/config/configure-db.d.ts +14 -14
  89. package/config/configure-db.js +18 -18
  90. package/config/database-env.d.ts +16 -16
  91. package/config/database-env.js +14 -14
  92. package/config/index.d.ts +2 -2
  93. package/config/index.js +7 -7
  94. package/database/connection-options.d.ts +7 -7
  95. package/database/connection-options.js +2 -2
  96. package/database/database.d.ts +204 -204
  97. package/database/database.js +330 -330
  98. package/database/driver-query.d.ts +10 -10
  99. package/database/driver-query.js +2 -2
  100. package/database/driver.d.ts +12 -12
  101. package/database/driver.js +47 -47
  102. package/database/get-database-path.d.ts +6 -6
  103. package/database/get-database-path.js +12 -12
  104. package/database/index.d.ts +7 -7
  105. package/database/index.js +26 -26
  106. package/database/load-database.d.ts +9 -9
  107. package/database/load-database.js +36 -36
  108. package/database/transaction.d.ts +8 -8
  109. package/database/transaction.js +2 -2
  110. package/ddl/alter-table.d.ts +26 -26
  111. package/ddl/alter-table.js +2 -2
  112. package/ddl/create-database.d.ts +3 -3
  113. package/ddl/create-database.js +2 -2
  114. package/ddl/create-index.d.ts +5 -5
  115. package/ddl/create-index.js +2 -2
  116. package/ddl/create-table.d.ts +8 -8
  117. package/ddl/create-table.js +2 -2
  118. package/ddl/create-user.d.ts +4 -4
  119. package/ddl/create-user.js +2 -2
  120. package/ddl/ddl-builder.d.ts +96 -96
  121. package/ddl/ddl-builder.js +565 -565
  122. package/ddl/ddl-repository.d.ts +135 -135
  123. package/ddl/ddl-repository.js +228 -228
  124. package/ddl/drop-database.d.ts +4 -4
  125. package/ddl/drop-database.js +2 -2
  126. package/ddl/drop-table.d.ts +4 -4
  127. package/ddl/drop-table.js +2 -2
  128. package/ddl/drop-user.d.ts +4 -4
  129. package/ddl/drop-user.js +2 -2
  130. package/ddl/foreign-key-constraint.d.ts +19 -19
  131. package/ddl/foreign-key-constraint.js +2 -2
  132. package/ddl/grant.d.ts +12 -12
  133. package/ddl/grant.js +2 -2
  134. package/ddl/index.d.ts +13 -13
  135. package/ddl/index.js +23 -23
  136. package/ddl/truncate.d.ts +3 -3
  137. package/ddl/truncate.js +2 -2
  138. package/dml/case-expression.d.ts +16 -16
  139. package/dml/case-expression.js +11 -11
  140. package/dml/delete.d.ts +8 -8
  141. package/dml/delete.js +2 -2
  142. package/dml/from.d.ts +4 -4
  143. package/dml/from.js +2 -2
  144. package/dml/group-by.d.ts +4 -4
  145. package/dml/group-by.js +2 -2
  146. package/dml/index.d.ts +11 -11
  147. package/dml/index.js +30 -30
  148. package/dml/insert.d.ts +14 -14
  149. package/dml/insert.js +2 -2
  150. package/dml/join.d.ts +7 -7
  151. package/dml/join.js +2 -2
  152. package/dml/order-by.d.ts +2 -2
  153. package/dml/order-by.js +2 -2
  154. package/dml/query-builder.d.ts +106 -106
  155. package/dml/query-builder.js +797 -793
  156. package/dml/query-builder.js.map +1 -1
  157. package/dml/query-repository.d.ts +84 -84
  158. package/dml/query-repository.js +239 -225
  159. package/dml/query-repository.js.map +1 -1
  160. package/dml/select.d.ts +29 -29
  161. package/dml/select.js +2 -2
  162. package/dml/set-options.d.ts +5 -5
  163. package/dml/set-options.js +2 -2
  164. package/dml/subquery.d.ts +5 -5
  165. package/dml/subquery.js +10 -10
  166. package/dml/update.d.ts +11 -11
  167. package/dml/update.js +2 -2
  168. package/expression/expression-token.d.ts +11 -11
  169. package/expression/expression-token.js +12 -12
  170. package/expression/expression.d.ts +6 -6
  171. package/expression/expression.js +2 -2
  172. package/expression/identifier.d.ts +7 -7
  173. package/expression/identifier.js +15 -15
  174. package/expression/index.d.ts +7 -7
  175. package/expression/index.js +25 -25
  176. package/expression/is-expression-token.d.ts +2 -2
  177. package/expression/is-expression-token.js +7 -7
  178. package/expression/key-val-expression.d.ts +7 -7
  179. package/expression/key-val-expression.js +2 -2
  180. package/expression/literal.d.ts +3 -3
  181. package/expression/literal.js +2 -2
  182. package/expression/logical.d.ts +18 -18
  183. package/expression/logical.js +30 -30
  184. package/expression/math.d.ts +17 -17
  185. package/expression/math.js +36 -36
  186. package/expression/raw.d.ts +8 -8
  187. package/expression/raw.js +16 -16
  188. package/expression/simple-expression.d.ts +8 -8
  189. package/expression/simple-expression.js +2 -2
  190. package/functions/function-aliases.d.ts +4 -4
  191. package/functions/function-aliases.js +12 -12
  192. package/functions/function-token.d.ts +29 -29
  193. package/functions/function-token.js +15 -15
  194. package/functions/functions.d.ts +19 -19
  195. package/functions/functions.js +94 -94
  196. package/functions/index.d.ts +4 -4
  197. package/functions/index.js +22 -22
  198. package/functions/is-database-function.d.ts +8 -8
  199. package/functions/is-database-function.js +14 -14
  200. package/functions/signatures/count.d.ts +14 -11
  201. package/functions/signatures/count.js +2 -2
  202. package/index.d.ts +16 -16
  203. package/index.js +32 -32
  204. package/migration/index.d.ts +3 -3
  205. package/migration/index.js +9 -9
  206. package/migration/migration-package.d.ts +14 -14
  207. package/migration/migration-package.js +10 -10
  208. package/migration/migration-record.d.ts +10 -10
  209. package/migration/migration-record.js +2 -2
  210. package/migration/migration-runner.d.ts +33 -33
  211. package/migration/migration-runner.js +180 -180
  212. package/migration/migration.d.ts +12 -12
  213. package/migration/migration.js +27 -27
  214. package/migration/migrations/001-create-migration-table.d.ts +8 -8
  215. package/migration/migrations/001-create-migration-table.js +54 -54
  216. package/migration/migrations/002-add-migration-package-column.d.ts +9 -9
  217. package/migration/migrations/002-add-migration-package-column.js +44 -44
  218. package/package.json +1 -1
  219. package/record/index.d.ts +1 -1
  220. package/record/index.js +17 -17
  221. package/record/record.d.ts +2 -2
  222. package/record/record.js +2 -2
  223. package/repository/index.d.ts +2 -2
  224. package/repository/index.js +5 -5
  225. package/repository/repository-options.d.ts +6 -6
  226. package/repository/repository-options.js +2 -2
  227. package/repository/repository.d.ts +29 -29
  228. package/repository/repository.js +68 -68
  229. package/schema/index.d.ts +3 -3
  230. package/schema/index.js +5 -5
  231. package/schema/schema-query-repository.d.ts +54 -54
  232. package/schema/schema-query-repository.js +159 -159
  233. package/schema/schema-table.d.ts +9 -9
  234. package/schema/schema-table.js +2 -2
  235. package/schema/schema.d.ts +4 -4
  236. package/schema/schema.js +2 -2
  237. package/seed/auto-seed.d.ts +9 -9
  238. package/seed/auto-seed.js +61 -61
  239. package/seed/create-seed-table.d.ts +8 -8
  240. package/seed/create-seed-table.js +64 -64
  241. package/seed/index.d.ts +4 -4
  242. package/seed/index.js +9 -9
  243. package/seed/seed-record.d.ts +11 -11
  244. package/seed/seed-record.js +2 -2
  245. package/seed/seed-runner.d.ts +20 -20
  246. package/seed/seed-runner.js +91 -91
  247. package/seed/seed.d.ts +12 -12
  248. package/seed/seed.js +27 -27
  249. package/tokens/index.d.ts +1 -1
  250. package/tokens/index.js +5 -5
  251. package/triggers/index.d.ts +2 -2
  252. package/triggers/index.js +18 -18
  253. package/triggers/prebuilt/index.d.ts +1 -1
  254. package/triggers/prebuilt/index.js +5 -5
  255. package/triggers/prebuilt/update-timestamp-trigger.d.ts +16 -16
  256. package/triggers/prebuilt/update-timestamp-trigger.js +24 -24
  257. package/triggers/trigger-options.d.ts +22 -22
  258. package/triggers/trigger-options.js +2 -2
  259. package/triggers/trigger.d.ts +19 -19
  260. package/triggers/trigger.js +23 -23
@@ -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