@riao/dbal 3.7.1 → 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 (269) 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 -2
  205. package/migration/index.js +9 -7
  206. package/migration/index.js.map +1 -1
  207. package/migration/migration-package.d.ts +14 -0
  208. package/migration/migration-package.js +11 -0
  209. package/migration/migration-package.js.map +1 -0
  210. package/migration/migration-record.d.ts +10 -9
  211. package/migration/migration-record.js +2 -2
  212. package/migration/migration-runner.d.ts +33 -22
  213. package/migration/migration-runner.js +180 -131
  214. package/migration/migration-runner.js.map +1 -1
  215. package/migration/migration.d.ts +12 -12
  216. package/migration/migration.js +27 -27
  217. package/migration/{create-migration-table.d.ts → migrations/001-create-migration-table.d.ts} +8 -8
  218. package/migration/{create-migration-table.js → migrations/001-create-migration-table.js} +55 -55
  219. package/migration/migrations/001-create-migration-table.js.map +1 -0
  220. package/migration/migrations/002-add-migration-package-column.d.ts +9 -0
  221. package/migration/migrations/002-add-migration-package-column.js +45 -0
  222. package/migration/migrations/002-add-migration-package-column.js.map +1 -0
  223. package/package.json +1 -1
  224. package/record/index.d.ts +1 -1
  225. package/record/index.js +17 -17
  226. package/record/record.d.ts +2 -2
  227. package/record/record.js +2 -2
  228. package/repository/index.d.ts +2 -2
  229. package/repository/index.js +5 -5
  230. package/repository/repository-options.d.ts +6 -6
  231. package/repository/repository-options.js +2 -2
  232. package/repository/repository.d.ts +29 -29
  233. package/repository/repository.js +68 -68
  234. package/schema/index.d.ts +3 -3
  235. package/schema/index.js +5 -5
  236. package/schema/schema-query-repository.d.ts +54 -54
  237. package/schema/schema-query-repository.js +159 -159
  238. package/schema/schema-table.d.ts +9 -9
  239. package/schema/schema-table.js +2 -2
  240. package/schema/schema.d.ts +4 -4
  241. package/schema/schema.js +2 -2
  242. package/seed/auto-seed.d.ts +9 -9
  243. package/seed/auto-seed.js +61 -61
  244. package/seed/create-seed-table.d.ts +8 -8
  245. package/seed/create-seed-table.js +64 -64
  246. package/seed/index.d.ts +4 -4
  247. package/seed/index.js +9 -9
  248. package/seed/seed-record.d.ts +11 -11
  249. package/seed/seed-record.js +2 -2
  250. package/seed/seed-runner.d.ts +20 -20
  251. package/seed/seed-runner.js +91 -91
  252. package/seed/seed.d.ts +12 -12
  253. package/seed/seed.js +27 -27
  254. package/tokens/index.d.ts +1 -1
  255. package/tokens/index.js +5 -5
  256. package/triggers/index.d.ts +2 -2
  257. package/triggers/index.js +18 -18
  258. package/triggers/prebuilt/index.d.ts +1 -1
  259. package/triggers/prebuilt/index.js +5 -5
  260. package/triggers/prebuilt/update-timestamp-trigger.d.ts +16 -16
  261. package/triggers/prebuilt/update-timestamp-trigger.js +24 -24
  262. package/triggers/trigger-options.d.ts +22 -22
  263. package/triggers/trigger-options.js +2 -2
  264. package/triggers/trigger.d.ts +19 -19
  265. package/triggers/trigger.js +23 -23
  266. package/column-pack/user/login-column.d.ts +0 -2
  267. package/column-pack/user/login-column.js +0 -12
  268. package/column-pack/user/login-column.js.map +0 -1
  269. package/migration/create-migration-table.js.map +0 -1
@@ -1,794 +1,798 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DatabaseQueryBuilder = void 0;
4
- const statement_builder_1 = require("../builder/statement-builder");
5
- const comparison_1 = require("../comparison");
6
- const tokens_1 = require("../tokens");
7
- const functions_1 = require("../functions");
8
- const expression_1 = require("../expression");
9
- const function_token_1 = require("../functions/function-token");
10
- const subquery_1 = require("./subquery");
11
- const case_expression_1 = require("./case-expression");
12
- class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
13
- // ------------------------------------------------------------------------
14
- // Expression
15
- // ------------------------------------------------------------------------
16
- expression(expr) {
17
- if (Array.isArray(expr)) {
18
- this.sql.openParens();
19
- for (const e of expr) {
20
- this.expression(e);
21
- }
22
- this.sql.closeParens();
23
- }
24
- else if ((0, expression_1.isExpressionToken)(expr)) {
25
- const token = expr;
26
- if ((0, comparison_1.isComparisonToken)(token)) {
27
- this.buildComparisonToken(token);
28
- }
29
- else if ((0, expression_1.isLogicalToken)(token)) {
30
- this.logical(token);
31
- }
32
- else if ((0, expression_1.isIdentifierToken)(token)) {
33
- this.sql.columnName(token.name);
34
- }
35
- else if ((0, functions_1.isDatabaseFunction)(token)) {
36
- this.databaseFunction(token);
37
- }
38
- else if ((0, expression_1.isMathToken)(token)) {
39
- this.math(token);
40
- }
41
- else if ((0, expression_1.isRawExprToken)(token)) {
42
- this.rawExpression(token);
43
- }
44
- }
45
- else if (expr && typeof expr === 'object') {
46
- if (expr instanceof Buffer || expr instanceof Date) {
47
- this.sql.placeholder(expr);
48
- }
49
- else if (expr instanceof subquery_1.Subquery) {
50
- this.subquery(expr);
51
- }
52
- else if (expr instanceof case_expression_1.CaseExpression) {
53
- this.caseExpression(expr);
54
- }
55
- else {
56
- this.keyValueExpression(expr);
57
- }
58
- }
59
- else {
60
- this.sql.placeholder(expr);
61
- }
62
- }
63
- subquery(subquery) {
64
- this.sql.openParens();
65
- this.select(subquery.query);
66
- this.sql.closeParens();
67
- this.sql.trimEnd(' ');
68
- }
69
- caseExpression(expr) {
70
- this.sql.openParens();
71
- this.sql.append('CASE ');
72
- if (expr.value) {
73
- this.expression(expr.value);
74
- this.sql.space();
75
- }
76
- for (const condition of expr.case) {
77
- this.sql.append('WHEN ');
78
- this.expression(condition.when);
79
- this.sql.append('THEN ');
80
- this.expression(condition.then);
81
- }
82
- if (expr.else) {
83
- this.sql.append('ELSE ');
84
- this.expression(expr.else);
85
- }
86
- this.sql.append('END');
87
- this.sql.closeParens();
88
- this.sql.trimEnd(' ');
89
- }
90
- rawExpression(expr) {
91
- this.sql.append(expr.sql);
92
- if (expr.params) {
93
- this.sql.appendParams(expr.params);
94
- }
95
- return this;
96
- }
97
- keyValueExpression(kv) {
98
- if (!Object.keys(kv).length) {
99
- return this;
100
- }
101
- this.sql.openParens();
102
- for (const key in kv) {
103
- const value = kv[key];
104
- this.sql.columnName(key);
105
- this.sql.space();
106
- if (value === null) {
107
- this.isNull();
108
- }
109
- else if ((0, expression_1.isExpressionToken)(value)) {
110
- const token = value;
111
- if ((0, comparison_1.isComparisonToken)(token)) {
112
- this.expression(value);
113
- }
114
- else if ((0, expression_1.isLogicalToken)(token)) {
115
- this.expression(value);
116
- }
117
- else if ((0, expression_1.isIdentifierToken)(token)) {
118
- this.equal(value);
119
- }
120
- else if ((0, functions_1.isDatabaseFunction)(token)) {
121
- this.equal(value);
122
- }
123
- else if ((0, expression_1.isRawExprToken)(token)) {
124
- this.equal(value);
125
- }
126
- }
127
- else {
128
- this.equal(value);
129
- }
130
- this.and();
131
- }
132
- this.sql.trimEnd(this.sql.operators.and);
133
- this.sql.closeParens();
134
- return this;
135
- }
136
- // ------------------------------------------------------------------------
137
- // Set
138
- // ------------------------------------------------------------------------
139
- set(options) {
140
- this.setStatement();
141
- this.setColumn(options);
142
- return this;
143
- }
144
- setStatement() {
145
- this.sql.append('SET ');
146
- return this;
147
- }
148
- setColumn(options) {
149
- this.sql.columnName(options.column);
150
- this.sql.append(' = ');
151
- this.expression(options.value);
152
- return this;
153
- }
154
- // ------------------------------------------------------------------------
155
- // Select
156
- // ------------------------------------------------------------------------
157
- selectColumn(column) {
158
- if (typeof column === 'string') {
159
- this.sql.columnName(column);
160
- }
161
- else if (typeof column === 'object') {
162
- if ('column' in column) {
163
- this.sql.columnName(column.column);
164
- }
165
- else if ('query' in column) {
166
- this.expression(column.query);
167
- }
168
- if (column.as) {
169
- this.sql.append(' AS ');
170
- this.sql.columnName(column.as);
171
- }
172
- }
173
- return this;
174
- }
175
- selectColumnList(columns) {
176
- if (columns) {
177
- for (const column of columns) {
178
- this.selectColumn(column);
179
- this.sql.append(', ');
180
- }
181
- this.sql.trimEnd(', ');
182
- this.sql.space();
183
- }
184
- else {
185
- this.sql.append('* ');
186
- }
187
- return this;
188
- }
189
- selectFrom(from) {
190
- this.sql.append('FROM ');
191
- if (typeof from === 'string') {
192
- this.sql.tableName(from);
193
- }
194
- else if (typeof from === 'object') {
195
- for (const alias in from) {
196
- const expr = from[alias];
197
- if (typeof expr === 'string') {
198
- this.sql.tableName(expr);
199
- }
200
- else {
201
- this.expression(expr);
202
- }
203
- this.sql.append(' ');
204
- this.sql.columnName(alias);
205
- this.sql.append(', ');
206
- }
207
- this.sql.trimEnd(', ');
208
- }
209
- this.sql.space();
210
- return this;
211
- }
212
- tableAlias(alias) {
213
- this.sql.append('AS ');
214
- this.sql.tableName(alias);
215
- this.sql.space();
216
- return this;
217
- }
218
- selectStatement() {
219
- this.sql.append('SELECT ');
220
- return this;
221
- }
222
- distinctStatement() {
223
- this.sql.append('DISTINCT ');
224
- return this;
225
- }
226
- selectTop(limit) {
227
- // This will be overridden in mssql for LIMIT func
228
- return this;
229
- }
230
- // ------------------------------------------------------------------------
231
- // Comparisons
232
- // ------------------------------------------------------------------------
233
- equal(value) {
234
- this.sql.append(this.sql.operators.equals + ' ');
235
- this.expression(value);
236
- }
237
- like(value) {
238
- this.sql.append(this.sql.operators.like + ' ');
239
- this.expression(value);
240
- }
241
- lt(value) {
242
- this.sql.append(this.sql.operators.lt + ' ');
243
- this.expression(value);
244
- }
245
- lte(value) {
246
- this.sql.append(this.sql.operators.lte + ' ');
247
- this.expression(value);
248
- }
249
- gt(value) {
250
- this.sql.append(this.sql.operators.gt + ' ');
251
- this.expression(value);
252
- }
253
- gte(value) {
254
- this.sql.append(this.sql.operators.gte + ' ');
255
- this.expression(value);
256
- }
257
- notEqual(value) {
258
- this.sql.append(this.sql.operators.notEqual + ' ');
259
- this.expression(value);
260
- }
261
- isNull() {
262
- this.sql.append(this.sql.operators.is + ' ' + this.sql.operators.null + ' ');
263
- }
264
- inArray(values) {
265
- this.sql.append(this.sql.operators.in + ' ');
266
- this.sql.openParens();
267
- for (const value of values) {
268
- this.expression(value);
269
- this.sql.append(', ');
270
- }
271
- this.sql.trimEnd(', ');
272
- this.sql.closeParens();
273
- }
274
- between(a, b) {
275
- this.sql.append(this.sql.operators.between + ' ');
276
- this.expression(a);
277
- this.sql.append('AND ');
278
- this.expression(b);
279
- }
280
- notNull() {
281
- this.sql.append('NOT NULL ');
282
- }
283
- buildComparisonToken(condition) {
284
- if (condition.op === comparison_1.ComparisonOperator.EQUALS) {
285
- this.equal(condition.value);
286
- }
287
- else if (condition.op === comparison_1.ComparisonOperator.NOT_EQUAL) {
288
- this.notEqual(condition.value);
289
- }
290
- else if (condition.op === comparison_1.ComparisonOperator.LIKE) {
291
- this.like(condition.value);
292
- }
293
- else if (condition.op === comparison_1.ComparisonOperator.LT) {
294
- this.lt(condition.value);
295
- }
296
- else if (condition.op === comparison_1.ComparisonOperator.LTE) {
297
- this.lte(condition.value);
298
- }
299
- else if (condition.op === comparison_1.ComparisonOperator.GT) {
300
- this.gt(condition.value);
301
- }
302
- else if (condition.op === comparison_1.ComparisonOperator.GTE) {
303
- this.gte(condition.value);
304
- }
305
- else if (condition.op === comparison_1.ComparisonOperator.IN_ARRAY) {
306
- this.inArray(condition.value);
307
- }
308
- else if (condition.op === comparison_1.ComparisonOperator.BETWEEN) {
309
- this.between(condition.value.a, condition.value.b);
310
- }
311
- return this;
312
- }
313
- // ------------------------------------------------------------------------
314
- // Logical
315
- // ------------------------------------------------------------------------
316
- and() {
317
- this.sql.append(this.sql.operators.and + ' ');
318
- }
319
- or() {
320
- this.sql.append(this.sql.operators.or + ' ');
321
- }
322
- not(value) {
323
- if (value === null) {
324
- this.sql.append(this.sql.operators.is + ' ');
325
- this.notNull();
326
- return;
327
- }
328
- const isComparison = (0, expression_1.isExpressionToken)(value) &&
329
- (0, comparison_1.isComparisonToken)(value);
330
- const conditionType = isComparison
331
- ? value === null || value === void 0 ? void 0 : value.op
332
- : null;
333
- if (isComparison &&
334
- (conditionType === comparison_1.ComparisonOperator.LIKE ||
335
- conditionType === comparison_1.ComparisonOperator.IN_ARRAY)) {
336
- this.sql.append('NOT ');
337
- this.buildComparisonToken(value);
338
- }
339
- else if (isComparison) {
340
- throw new Error('Cannot use not() with ' + conditionType);
341
- }
342
- else if (typeof value === 'object' || Array.isArray(value)) {
343
- this.sql.append('!');
344
- this.expression(value);
345
- }
346
- else {
347
- this.notEqual(value);
348
- }
349
- }
350
- logical(token) {
351
- if (token.op === expression_1.LogicalOperator.AND) {
352
- this.and();
353
- }
354
- else if (token.op === expression_1.LogicalOperator.OR) {
355
- this.or();
356
- }
357
- else if (token.op === expression_1.LogicalOperator.NOT) {
358
- this.not(token.expr);
359
- }
360
- }
361
- // ------------------------------------------------------------------------
362
- // Math
363
- // ------------------------------------------------------------------------
364
- addition() {
365
- this.sql.append(this.sql.operators.addition + ' ');
366
- }
367
- subtraction() {
368
- this.sql.append(this.sql.operators.subtraction + ' ');
369
- }
370
- multiplication() {
371
- this.sql.append(this.sql.operators.multiplication + ' ');
372
- }
373
- division() {
374
- this.sql.append(this.sql.operators.division + ' ');
375
- }
376
- modulo() {
377
- this.sql.append(this.sql.operators.modulo + ' ');
378
- }
379
- math(token) {
380
- if (token.op === expression_1.MathOperation.ADD) {
381
- this.addition();
382
- }
383
- if (token.op === expression_1.MathOperation.SUB) {
384
- this.subtraction();
385
- }
386
- if (token.op === expression_1.MathOperation.MUL) {
387
- this.multiplication();
388
- }
389
- if (token.op === expression_1.MathOperation.DIV) {
390
- this.division();
391
- }
392
- if (token.op === expression_1.MathOperation.MOD) {
393
- this.modulo();
394
- }
395
- }
396
- // ------------------------------------------------------------------------
397
- // Where
398
- // ------------------------------------------------------------------------
399
- where(where) {
400
- this.sql.append('WHERE ');
401
- this.expression(where);
402
- return this;
403
- }
404
- limit(nRecords) {
405
- this.sql.append('LIMIT ' + nRecords + ' ');
406
- return this;
407
- }
408
- offset(nRecords) {
409
- this.sql.append('OFFSET ' + nRecords + ' ');
410
- return this;
411
- }
412
- groupBy(by) {
413
- this.sql.append('GROUP BY ');
414
- for (const key of by) {
415
- this.sql.columnName(key);
416
- this.sql.append(', ');
417
- }
418
- this.sql.trimEnd(', ');
419
- this.sql.space();
420
- return this;
421
- }
422
- having(query) {
423
- this.sql.append('HAVING ');
424
- this.expression(query);
425
- return this;
426
- }
427
- orderBy(by) {
428
- this.sql.append('ORDER BY ');
429
- for (const key in by) {
430
- this.sql.columnName(key);
431
- this.sql.append(' ' + by[key] + ', ');
432
- }
433
- this.sql.trimEnd(', ');
434
- this.sql.space();
435
- }
436
- select(query) {
437
- var _a, _b;
438
- this.selectStatement();
439
- if (query.distinct) {
440
- this.distinctStatement();
441
- }
442
- if ('top' in query) {
443
- this.selectTop(query.top);
444
- }
445
- this.selectColumnList(query.columns);
446
- if (query.table) {
447
- this.selectFrom(query.table);
448
- }
449
- if (query.tableAlias) {
450
- this.tableAlias(query.tableAlias);
451
- }
452
- for (const join of (_a = query.join) !== null && _a !== void 0 ? _a : []) {
453
- this.join(join);
454
- }
455
- if (query.where) {
456
- this.where(query.where);
457
- }
458
- if ((_b = query.groupBy) === null || _b === void 0 ? void 0 : _b.length) {
459
- this.groupBy(query.groupBy);
460
- }
461
- if (query.having) {
462
- this.having(query.having);
463
- }
464
- if (query.orderBy) {
465
- this.orderBy(query.orderBy);
466
- }
467
- this.pagination(query);
468
- return this;
469
- }
470
- pagination(query) {
471
- if (query.limit) {
472
- this.limit(query.limit);
473
- }
474
- if (query.offset !== undefined) {
475
- this.offset(query.offset);
476
- }
477
- return this;
478
- }
479
- // ------------------------------------------------------------------------
480
- // Join
481
- // ------------------------------------------------------------------------
482
- join(join) {
483
- var _a;
484
- this.sql.append(((_a = join.type) !== null && _a !== void 0 ? _a : '') + ' JOIN ');
485
- this.sql.tableName(join.table);
486
- this.sql.space();
487
- if (join.alias) {
488
- this.tableAlias(join.alias);
489
- }
490
- if (join.on) {
491
- this.sql.append('ON ');
492
- this.expression(join.on);
493
- }
494
- }
495
- // ------------------------------------------------------------------------
496
- // Insert
497
- // ------------------------------------------------------------------------
498
- insertIntoStatement(table) {
499
- this.sql.append('INSERT INTO ');
500
- this.sql.tableName(table);
501
- this.sql.space();
502
- return this;
503
- }
504
- insertColumnNames(record) {
505
- this.sql.openParens();
506
- this.sql.commaSeparate(Object.keys(record).map((name) => this.sql.getEnclosedName(name)));
507
- this.sql.closeParens();
508
- return this;
509
- }
510
- insertOnDuplicateKeyUpdate(assignment) {
511
- this.sql.append('ON DUPLICATE KEY UPDATE ');
512
- this.updateKeyValues(assignment);
513
- return this;
514
- }
515
- insertIfNotExists() {
516
- this.insertOnDuplicateKeyUpdate({ id: (0, tokens_1.columnName)('id') });
517
- return this;
518
- }
519
- insertOutput(primaryKey) {
520
- // This will be overridden in mssql to return the insert id(s)
521
- return this;
522
- }
523
- insertReturning(primaryKey) {
524
- // This will be overridden in postgres to return the insert id(s)
525
- return this;
526
- }
527
- insert(options) {
528
- var _a;
529
- this.insertIntoStatement(options.table);
530
- if (!Array.isArray(options.records)) {
531
- options.records = [options.records];
532
- }
533
- const columns = {};
534
- const insertions = [];
535
- if (options.records.length) {
536
- for (const rec of options.records) {
537
- for (const key in rec) {
538
- if (!(key in columns)) {
539
- columns[key] = true;
540
- }
541
- }
542
- }
543
- for (const rec of options.records) {
544
- const insertion = {};
545
- for (const key in columns) {
546
- insertion[key] = (_a = rec[key]) !== null && _a !== void 0 ? _a : null;
547
- }
548
- insertions.push(insertion);
549
- }
550
- this.insertColumnNames(columns);
551
- }
552
- if (options.primaryKey) {
553
- this.insertOutput(options.primaryKey);
554
- }
555
- this.sql.append('VALUES ');
556
- for (let i = 0; i < insertions.length; i++) {
557
- const record = insertions[i];
558
- this.sql.openParens();
559
- for (const key in record) {
560
- this.expression(record[key]);
561
- this.sql = this.sql.trimEnd();
562
- this.sql.append(', ');
563
- }
564
- this.sql.trimEnd(', ');
565
- this.sql.closeParens();
566
- this.sql = this.sql.trimEnd();
567
- this.sql.append(', ');
568
- }
569
- this.sql.trimEnd(', ');
570
- this.sql.space();
571
- if (options.ifNotExists) {
572
- this.insertIfNotExists();
573
- }
574
- else if (options.onDuplicateKeyUpdate) {
575
- this.insertOnDuplicateKeyUpdate(options.onDuplicateKeyUpdate);
576
- }
577
- this.sql.trimEnd(' ');
578
- if (options.primaryKey) {
579
- this.insertReturning(options.primaryKey);
580
- }
581
- return this;
582
- }
583
- // ------------------------------------------------------------------------
584
- // Update
585
- // ------------------------------------------------------------------------
586
- updateStatement(table) {
587
- this.sql.append('UPDATE ');
588
- this.sql.tableName(table);
589
- this.sql.space();
590
- return this;
591
- }
592
- updateKeyValues(values) {
593
- const keys = Object.keys(values);
594
- for (const key of keys) {
595
- this.sql.columnName(key);
596
- this.sql.append(' = ');
597
- this.expression(values[key]);
598
- this.sql = this.sql.trimEnd();
599
- this.sql.append(', ');
600
- }
601
- this.sql.trimEnd(', ');
602
- this.sql.space();
603
- return this;
604
- }
605
- updateSetStatement(values) {
606
- this.sql.append('SET ');
607
- this.updateKeyValues(values);
608
- return this;
609
- }
610
- update(options) {
611
- var _a;
612
- this.updateStatement(options.table);
613
- for (const join of (_a = options.join) !== null && _a !== void 0 ? _a : []) {
614
- this.join(join);
615
- }
616
- this.updateSetStatement(options.set);
617
- if (options.from) {
618
- this.selectFrom(options.from);
619
- }
620
- if (options.where) {
621
- this.where(options.where);
622
- }
623
- return this;
624
- }
625
- // ------------------------------------------------------------------------
626
- // Delete
627
- // ------------------------------------------------------------------------
628
- deleteStatement(table) {
629
- this.sql.append('DELETE FROM ');
630
- this.sql.tableName(table);
631
- this.sql.space();
632
- return this;
633
- }
634
- delete(options) {
635
- var _a;
636
- this.deleteStatement(options.table);
637
- for (const join of (_a = options.join) !== null && _a !== void 0 ? _a : []) {
638
- this.join(join);
639
- }
640
- if (options.where) {
641
- this.where(options.where);
642
- }
643
- return this;
644
- }
645
- // ------------------------------------------------------------------------
646
- // Database Functions
647
- // ------------------------------------------------------------------------
648
- /**
649
- * Append a database function the query
650
- *
651
- * @param fn Database function token
652
- */
653
- databaseFunction(fn) {
654
- switch (fn.fn) {
655
- case function_token_1.DatabaseFunctionKeys.AVERAGE:
656
- this.average(fn);
657
- break;
658
- case function_token_1.DatabaseFunctionKeys.COUNT:
659
- this.count(fn);
660
- break;
661
- case function_token_1.DatabaseFunctionKeys.MIN:
662
- this.min(fn);
663
- break;
664
- case function_token_1.DatabaseFunctionKeys.MAX:
665
- this.max(fn);
666
- break;
667
- case function_token_1.DatabaseFunctionKeys.SUM:
668
- this.sum(fn);
669
- break;
670
- case function_token_1.DatabaseFunctionKeys.CURRENT_TIMESTAMP:
671
- this.currentTimestamp(fn);
672
- break;
673
- case function_token_1.DatabaseFunctionKeys.DATE:
674
- this.date(fn);
675
- break;
676
- case function_token_1.DatabaseFunctionKeys.YEAR:
677
- this.year(fn);
678
- break;
679
- case function_token_1.DatabaseFunctionKeys.UUID:
680
- this.uuid();
681
- break;
682
- }
683
- return this;
684
- }
685
- average(fn) {
686
- this.sql.append('AVG');
687
- this.sql.openParens();
688
- if (fn.params.options.distinct) {
689
- this.distinctStatement();
690
- }
691
- this.expression(fn.params.expr);
692
- this.sql.closeParens();
693
- return this;
694
- }
695
- count(fn) {
696
- var _a, _b, _c;
697
- this.sql.append('COUNT(');
698
- if ((_a = fn.params) === null || _a === void 0 ? void 0 : _a.distinct) {
699
- this.distinctStatement();
700
- }
701
- if ((_b = fn.params) === null || _b === void 0 ? void 0 : _b.expr) {
702
- this.expression(fn.params.expr);
703
- }
704
- else if ((_c = fn.params) === null || _c === void 0 ? void 0 : _c.column) {
705
- this.sql.columnName(fn.params.column);
706
- }
707
- else {
708
- this.sql.append('*');
709
- }
710
- this.sql.append(')');
711
- return this;
712
- }
713
- min(fn) {
714
- this.sql.append('MIN');
715
- this.sql.openParens();
716
- this.expression(fn.params);
717
- this.sql.closeParens();
718
- return this;
719
- }
720
- max(fn) {
721
- this.sql.append('MAX');
722
- this.sql.openParens();
723
- this.expression(fn.params);
724
- this.sql.closeParens();
725
- return this;
726
- }
727
- sum(fn) {
728
- this.sql.append('SUM');
729
- this.sql.openParens();
730
- if (fn.params.options.distinct) {
731
- this.distinctStatement();
732
- }
733
- this.expression(fn.params.expr);
734
- this.sql.closeParens();
735
- return this;
736
- }
737
- currentTimestamp(fn) {
738
- this.sql.append('CURRENT_TIMESTAMP');
739
- return this;
740
- }
741
- date(fn) {
742
- var _a;
743
- this.sql.append('date');
744
- this.sql.openParens();
745
- if ((_a = fn.params) === null || _a === void 0 ? void 0 : _a.expr) {
746
- this.expression(fn.params.expr);
747
- }
748
- else {
749
- this.expression(functions_1.DatabaseFunctions.currentTimestamp());
750
- }
751
- this.sql.closeParens();
752
- return this;
753
- }
754
- year(fn) {
755
- var _a;
756
- this.sql.append('year');
757
- this.sql.openParens();
758
- if ((_a = fn.params) === null || _a === void 0 ? void 0 : _a.expr) {
759
- this.expression(fn.params.expr);
760
- }
761
- else {
762
- this.expression(functions_1.DatabaseFunctions.currentTimestamp());
763
- }
764
- this.sql.closeParens();
765
- return this;
766
- }
767
- uuid() {
768
- this.sql.append('uuid()');
769
- return this;
770
- }
771
- // ------------------------------------------------------------------------
772
- // Triggers
773
- // ------------------------------------------------------------------------
774
- getTriggerOld() {
775
- return 'OLD';
776
- }
777
- getTriggerOldColumn(column) {
778
- return `${this.getTriggerOld()}.${column}`;
779
- }
780
- getTriggerNew() {
781
- return 'NEW';
782
- }
783
- getTriggerNewColumn(column) {
784
- return `${this.getTriggerNew()}.${column}`;
785
- }
786
- triggerSetValue(options) {
787
- return this.set({
788
- column: `NEW.${options.column}`,
789
- value: options.value,
790
- });
791
- }
792
- }
793
- exports.DatabaseQueryBuilder = DatabaseQueryBuilder;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DatabaseQueryBuilder = void 0;
4
+ const statement_builder_1 = require("../builder/statement-builder");
5
+ const comparison_1 = require("../comparison");
6
+ const tokens_1 = require("../tokens");
7
+ const functions_1 = require("../functions");
8
+ const expression_1 = require("../expression");
9
+ const function_token_1 = require("../functions/function-token");
10
+ const subquery_1 = require("./subquery");
11
+ const case_expression_1 = require("./case-expression");
12
+ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
13
+ // ------------------------------------------------------------------------
14
+ // Expression
15
+ // ------------------------------------------------------------------------
16
+ expression(expr) {
17
+ if (Array.isArray(expr)) {
18
+ this.sql.openParens();
19
+ for (const e of expr) {
20
+ this.expression(e);
21
+ }
22
+ this.sql.closeParens();
23
+ }
24
+ else if ((0, expression_1.isExpressionToken)(expr)) {
25
+ const token = expr;
26
+ if ((0, comparison_1.isComparisonToken)(token)) {
27
+ this.buildComparisonToken(token);
28
+ }
29
+ else if ((0, expression_1.isLogicalToken)(token)) {
30
+ this.logical(token);
31
+ }
32
+ else if ((0, expression_1.isIdentifierToken)(token)) {
33
+ this.sql.columnName(token.name);
34
+ }
35
+ else if ((0, functions_1.isDatabaseFunction)(token)) {
36
+ this.databaseFunction(token);
37
+ }
38
+ else if ((0, expression_1.isMathToken)(token)) {
39
+ this.math(token);
40
+ }
41
+ else if ((0, expression_1.isRawExprToken)(token)) {
42
+ this.rawExpression(token);
43
+ }
44
+ }
45
+ else if (expr && typeof expr === 'object') {
46
+ if (expr instanceof Buffer || expr instanceof Date) {
47
+ this.sql.placeholder(expr);
48
+ }
49
+ else if (expr instanceof subquery_1.Subquery) {
50
+ this.subquery(expr);
51
+ }
52
+ else if (expr instanceof case_expression_1.CaseExpression) {
53
+ this.caseExpression(expr);
54
+ }
55
+ else {
56
+ this.keyValueExpression(expr);
57
+ }
58
+ }
59
+ else {
60
+ this.sql.placeholder(expr);
61
+ }
62
+ }
63
+ subquery(subquery) {
64
+ this.sql.openParens();
65
+ this.select(subquery.query);
66
+ this.sql.closeParens();
67
+ this.sql.trimEnd(' ');
68
+ }
69
+ caseExpression(expr) {
70
+ this.sql.openParens();
71
+ this.sql.append('CASE ');
72
+ if (expr.value) {
73
+ this.expression(expr.value);
74
+ this.sql.space();
75
+ }
76
+ for (const condition of expr.case) {
77
+ this.sql.append('WHEN ');
78
+ this.expression(condition.when);
79
+ this.sql.append('THEN ');
80
+ this.expression(condition.then);
81
+ }
82
+ if (expr.else) {
83
+ this.sql.append('ELSE ');
84
+ this.expression(expr.else);
85
+ }
86
+ this.sql.append('END');
87
+ this.sql.closeParens();
88
+ this.sql.trimEnd(' ');
89
+ }
90
+ rawExpression(expr) {
91
+ this.sql.append(expr.sql);
92
+ if (expr.params) {
93
+ this.sql.appendParams(expr.params);
94
+ }
95
+ return this;
96
+ }
97
+ keyValueExpression(kv) {
98
+ if (!Object.keys(kv).length) {
99
+ return this;
100
+ }
101
+ this.sql.openParens();
102
+ for (const key in kv) {
103
+ const value = kv[key];
104
+ this.sql.columnName(key);
105
+ this.sql.space();
106
+ if (value === null) {
107
+ this.isNull();
108
+ }
109
+ else if ((0, expression_1.isExpressionToken)(value)) {
110
+ const token = value;
111
+ if ((0, comparison_1.isComparisonToken)(token)) {
112
+ this.expression(value);
113
+ }
114
+ else if ((0, expression_1.isLogicalToken)(token)) {
115
+ this.expression(value);
116
+ }
117
+ else if ((0, expression_1.isIdentifierToken)(token)) {
118
+ this.equal(value);
119
+ }
120
+ else if ((0, functions_1.isDatabaseFunction)(token)) {
121
+ this.equal(value);
122
+ }
123
+ else if ((0, expression_1.isRawExprToken)(token)) {
124
+ this.equal(value);
125
+ }
126
+ }
127
+ else {
128
+ this.equal(value);
129
+ }
130
+ this.and();
131
+ }
132
+ this.sql.trimEnd(this.sql.operators.and);
133
+ this.sql.closeParens();
134
+ return this;
135
+ }
136
+ // ------------------------------------------------------------------------
137
+ // Set
138
+ // ------------------------------------------------------------------------
139
+ set(options) {
140
+ this.setStatement();
141
+ this.setColumn(options);
142
+ return this;
143
+ }
144
+ setStatement() {
145
+ this.sql.append('SET ');
146
+ return this;
147
+ }
148
+ setColumn(options) {
149
+ this.sql.columnName(options.column);
150
+ this.sql.append(' = ');
151
+ this.expression(options.value);
152
+ return this;
153
+ }
154
+ // ------------------------------------------------------------------------
155
+ // Select
156
+ // ------------------------------------------------------------------------
157
+ selectColumn(column) {
158
+ if (typeof column === 'string') {
159
+ this.sql.columnName(column);
160
+ }
161
+ else if (typeof column === 'object') {
162
+ if ('column' in column) {
163
+ this.sql.columnName(column.column);
164
+ }
165
+ else if ('query' in column) {
166
+ this.expression(column.query);
167
+ }
168
+ if (column.as) {
169
+ this.sql.append(' AS ');
170
+ this.sql.columnName(column.as);
171
+ }
172
+ }
173
+ return this;
174
+ }
175
+ selectColumnList(columns) {
176
+ if (columns) {
177
+ for (const column of columns) {
178
+ this.selectColumn(column);
179
+ this.sql.append(', ');
180
+ }
181
+ this.sql.trimEnd(', ');
182
+ this.sql.space();
183
+ }
184
+ else {
185
+ this.sql.append('* ');
186
+ }
187
+ return this;
188
+ }
189
+ selectFrom(from) {
190
+ this.sql.append('FROM ');
191
+ if (typeof from === 'string') {
192
+ this.sql.tableName(from);
193
+ }
194
+ else if (typeof from === 'object') {
195
+ for (const alias in from) {
196
+ const expr = from[alias];
197
+ if (typeof expr === 'string') {
198
+ this.sql.tableName(expr);
199
+ }
200
+ else {
201
+ this.expression(expr);
202
+ }
203
+ this.sql.append(' ');
204
+ this.sql.columnName(alias);
205
+ this.sql.append(', ');
206
+ }
207
+ this.sql.trimEnd(', ');
208
+ }
209
+ this.sql.space();
210
+ return this;
211
+ }
212
+ tableAlias(alias) {
213
+ this.sql.append('AS ');
214
+ this.sql.tableName(alias);
215
+ this.sql.space();
216
+ return this;
217
+ }
218
+ selectStatement() {
219
+ this.sql.append('SELECT ');
220
+ return this;
221
+ }
222
+ distinctStatement() {
223
+ this.sql.append('DISTINCT ');
224
+ return this;
225
+ }
226
+ selectTop(limit) {
227
+ // This will be overridden in mssql for LIMIT func
228
+ return this;
229
+ }
230
+ // ------------------------------------------------------------------------
231
+ // Comparisons
232
+ // ------------------------------------------------------------------------
233
+ equal(value) {
234
+ this.sql.append(this.sql.operators.equals + ' ');
235
+ this.expression(value);
236
+ }
237
+ like(value) {
238
+ this.sql.append(this.sql.operators.like + ' ');
239
+ this.expression(value);
240
+ }
241
+ lt(value) {
242
+ this.sql.append(this.sql.operators.lt + ' ');
243
+ this.expression(value);
244
+ }
245
+ lte(value) {
246
+ this.sql.append(this.sql.operators.lte + ' ');
247
+ this.expression(value);
248
+ }
249
+ gt(value) {
250
+ this.sql.append(this.sql.operators.gt + ' ');
251
+ this.expression(value);
252
+ }
253
+ gte(value) {
254
+ this.sql.append(this.sql.operators.gte + ' ');
255
+ this.expression(value);
256
+ }
257
+ notEqual(value) {
258
+ this.sql.append(this.sql.operators.notEqual + ' ');
259
+ this.expression(value);
260
+ }
261
+ isNull() {
262
+ this.sql.append(this.sql.operators.is + ' ' + this.sql.operators.null + ' ');
263
+ }
264
+ inArray(values) {
265
+ this.sql.append(this.sql.operators.in + ' ');
266
+ this.sql.openParens();
267
+ for (const value of values) {
268
+ this.expression(value);
269
+ this.sql.append(', ');
270
+ }
271
+ this.sql.trimEnd(', ');
272
+ this.sql.closeParens();
273
+ }
274
+ between(a, b) {
275
+ this.sql.append(this.sql.operators.between + ' ');
276
+ this.expression(a);
277
+ this.sql.append('AND ');
278
+ this.expression(b);
279
+ }
280
+ notNull() {
281
+ this.sql.append('NOT NULL ');
282
+ }
283
+ buildComparisonToken(condition) {
284
+ if (condition.op === comparison_1.ComparisonOperator.EQUALS) {
285
+ this.equal(condition.value);
286
+ }
287
+ else if (condition.op === comparison_1.ComparisonOperator.NOT_EQUAL) {
288
+ this.notEqual(condition.value);
289
+ }
290
+ else if (condition.op === comparison_1.ComparisonOperator.LIKE) {
291
+ this.like(condition.value);
292
+ }
293
+ else if (condition.op === comparison_1.ComparisonOperator.LT) {
294
+ this.lt(condition.value);
295
+ }
296
+ else if (condition.op === comparison_1.ComparisonOperator.LTE) {
297
+ this.lte(condition.value);
298
+ }
299
+ else if (condition.op === comparison_1.ComparisonOperator.GT) {
300
+ this.gt(condition.value);
301
+ }
302
+ else if (condition.op === comparison_1.ComparisonOperator.GTE) {
303
+ this.gte(condition.value);
304
+ }
305
+ else if (condition.op === comparison_1.ComparisonOperator.IN_ARRAY) {
306
+ this.inArray(condition.value);
307
+ }
308
+ else if (condition.op === comparison_1.ComparisonOperator.BETWEEN) {
309
+ this.between(condition.value.a, condition.value.b);
310
+ }
311
+ return this;
312
+ }
313
+ // ------------------------------------------------------------------------
314
+ // Logical
315
+ // ------------------------------------------------------------------------
316
+ and() {
317
+ this.sql.append(this.sql.operators.and + ' ');
318
+ }
319
+ or() {
320
+ this.sql.append(this.sql.operators.or + ' ');
321
+ }
322
+ not(value) {
323
+ if (value === null) {
324
+ this.sql.append(this.sql.operators.is + ' ');
325
+ this.notNull();
326
+ return;
327
+ }
328
+ const isComparison = (0, expression_1.isExpressionToken)(value) &&
329
+ (0, comparison_1.isComparisonToken)(value);
330
+ const conditionType = isComparison
331
+ ? value === null || value === void 0 ? void 0 : value.op
332
+ : null;
333
+ if (isComparison &&
334
+ (conditionType === comparison_1.ComparisonOperator.LIKE ||
335
+ conditionType === comparison_1.ComparisonOperator.IN_ARRAY)) {
336
+ this.sql.append('NOT ');
337
+ this.buildComparisonToken(value);
338
+ }
339
+ else if (isComparison) {
340
+ throw new Error('Cannot use not() with ' + conditionType);
341
+ }
342
+ else if (typeof value === 'object' || Array.isArray(value)) {
343
+ this.sql.append('!');
344
+ this.expression(value);
345
+ }
346
+ else {
347
+ this.notEqual(value);
348
+ }
349
+ }
350
+ logical(token) {
351
+ if (token.op === expression_1.LogicalOperator.AND) {
352
+ this.and();
353
+ }
354
+ else if (token.op === expression_1.LogicalOperator.OR) {
355
+ this.or();
356
+ }
357
+ else if (token.op === expression_1.LogicalOperator.NOT) {
358
+ this.not(token.expr);
359
+ }
360
+ }
361
+ // ------------------------------------------------------------------------
362
+ // Math
363
+ // ------------------------------------------------------------------------
364
+ addition() {
365
+ this.sql.append(this.sql.operators.addition + ' ');
366
+ }
367
+ subtraction() {
368
+ this.sql.append(this.sql.operators.subtraction + ' ');
369
+ }
370
+ multiplication() {
371
+ this.sql.append(this.sql.operators.multiplication + ' ');
372
+ }
373
+ division() {
374
+ this.sql.append(this.sql.operators.division + ' ');
375
+ }
376
+ modulo() {
377
+ this.sql.append(this.sql.operators.modulo + ' ');
378
+ }
379
+ math(token) {
380
+ if (token.op === expression_1.MathOperation.ADD) {
381
+ this.addition();
382
+ }
383
+ if (token.op === expression_1.MathOperation.SUB) {
384
+ this.subtraction();
385
+ }
386
+ if (token.op === expression_1.MathOperation.MUL) {
387
+ this.multiplication();
388
+ }
389
+ if (token.op === expression_1.MathOperation.DIV) {
390
+ this.division();
391
+ }
392
+ if (token.op === expression_1.MathOperation.MOD) {
393
+ this.modulo();
394
+ }
395
+ }
396
+ // ------------------------------------------------------------------------
397
+ // Where
398
+ // ------------------------------------------------------------------------
399
+ where(where) {
400
+ this.sql.append('WHERE ');
401
+ this.expression(where);
402
+ return this;
403
+ }
404
+ limit(nRecords) {
405
+ this.sql.append('LIMIT ' + nRecords + ' ');
406
+ return this;
407
+ }
408
+ offset(nRecords) {
409
+ this.sql.append('OFFSET ' + nRecords + ' ');
410
+ return this;
411
+ }
412
+ groupBy(by) {
413
+ this.sql.append('GROUP BY ');
414
+ for (const key of by) {
415
+ this.sql.columnName(key);
416
+ this.sql.append(', ');
417
+ }
418
+ this.sql.trimEnd(', ');
419
+ this.sql.space();
420
+ return this;
421
+ }
422
+ having(query) {
423
+ this.sql.append('HAVING ');
424
+ this.expression(query);
425
+ return this;
426
+ }
427
+ orderBy(by) {
428
+ this.sql.append('ORDER BY ');
429
+ for (const key in by) {
430
+ this.sql.columnName(key);
431
+ this.sql.append(' ' + by[key] + ', ');
432
+ }
433
+ this.sql.trimEnd(', ');
434
+ this.sql.space();
435
+ }
436
+ select(query) {
437
+ var _a, _b;
438
+ this.selectStatement();
439
+ if (query.distinct) {
440
+ this.distinctStatement();
441
+ }
442
+ if ('top' in query) {
443
+ this.selectTop(query.top);
444
+ }
445
+ this.selectColumnList(query.columns);
446
+ if (query.table) {
447
+ this.selectFrom(query.table);
448
+ }
449
+ if (query.tableAlias) {
450
+ this.tableAlias(query.tableAlias);
451
+ }
452
+ for (const join of (_a = query.join) !== null && _a !== void 0 ? _a : []) {
453
+ this.join(join);
454
+ }
455
+ if (query.where) {
456
+ this.where(query.where);
457
+ }
458
+ if ((_b = query.groupBy) === null || _b === void 0 ? void 0 : _b.length) {
459
+ this.groupBy(query.groupBy);
460
+ }
461
+ if (query.having) {
462
+ this.having(query.having);
463
+ }
464
+ if (query.orderBy) {
465
+ this.orderBy(query.orderBy);
466
+ }
467
+ this.pagination(query);
468
+ return this;
469
+ }
470
+ pagination(query) {
471
+ if (query.limit) {
472
+ this.limit(query.limit);
473
+ }
474
+ if (query.offset !== undefined) {
475
+ this.offset(query.offset);
476
+ }
477
+ return this;
478
+ }
479
+ // ------------------------------------------------------------------------
480
+ // Join
481
+ // ------------------------------------------------------------------------
482
+ join(join) {
483
+ var _a;
484
+ this.sql.append(((_a = join.type) !== null && _a !== void 0 ? _a : '') + ' JOIN ');
485
+ this.sql.tableName(join.table);
486
+ this.sql.space();
487
+ if (join.alias) {
488
+ this.tableAlias(join.alias);
489
+ }
490
+ if (join.on) {
491
+ this.sql.append('ON ');
492
+ this.expression(join.on);
493
+ }
494
+ }
495
+ // ------------------------------------------------------------------------
496
+ // Insert
497
+ // ------------------------------------------------------------------------
498
+ insertIntoStatement(table) {
499
+ this.sql.append('INSERT INTO ');
500
+ this.sql.tableName(table);
501
+ this.sql.space();
502
+ return this;
503
+ }
504
+ insertColumnNames(record) {
505
+ this.sql.openParens();
506
+ this.sql.commaSeparate(Object.keys(record).map((name) => this.sql.getEnclosedName(name)));
507
+ this.sql.closeParens();
508
+ return this;
509
+ }
510
+ insertOnDuplicateKeyUpdate(assignment) {
511
+ this.sql.append('ON DUPLICATE KEY UPDATE ');
512
+ this.updateKeyValues(assignment);
513
+ return this;
514
+ }
515
+ insertIfNotExists() {
516
+ this.insertOnDuplicateKeyUpdate({ id: (0, tokens_1.columnName)('id') });
517
+ return this;
518
+ }
519
+ insertOutput(primaryKey) {
520
+ // This will be overridden in mssql to return the insert id(s)
521
+ return this;
522
+ }
523
+ insertReturning(primaryKey) {
524
+ // This will be overridden in postgres to return the insert id(s)
525
+ return this;
526
+ }
527
+ insert(options) {
528
+ var _a;
529
+ this.insertIntoStatement(options.table);
530
+ if (!Array.isArray(options.records)) {
531
+ options.records = [options.records];
532
+ }
533
+ const columns = {};
534
+ const insertions = [];
535
+ if (options.records.length) {
536
+ for (const rec of options.records) {
537
+ for (const key in rec) {
538
+ if (!(key in columns)) {
539
+ columns[key] = true;
540
+ }
541
+ }
542
+ }
543
+ for (const rec of options.records) {
544
+ const insertion = {};
545
+ for (const key in columns) {
546
+ insertion[key] = (_a = rec[key]) !== null && _a !== void 0 ? _a : null;
547
+ }
548
+ insertions.push(insertion);
549
+ }
550
+ this.insertColumnNames(columns);
551
+ }
552
+ if (options.primaryKey) {
553
+ this.insertOutput(options.primaryKey);
554
+ }
555
+ this.sql.append('VALUES ');
556
+ for (let i = 0; i < insertions.length; i++) {
557
+ const record = insertions[i];
558
+ this.sql.openParens();
559
+ for (const key in record) {
560
+ this.expression(record[key]);
561
+ this.sql = this.sql.trimEnd();
562
+ this.sql.append(', ');
563
+ }
564
+ this.sql.trimEnd(', ');
565
+ this.sql.closeParens();
566
+ this.sql = this.sql.trimEnd();
567
+ this.sql.append(', ');
568
+ }
569
+ this.sql.trimEnd(', ');
570
+ this.sql.space();
571
+ if (options.ifNotExists) {
572
+ this.insertIfNotExists();
573
+ }
574
+ else if (options.onDuplicateKeyUpdate) {
575
+ this.insertOnDuplicateKeyUpdate(options.onDuplicateKeyUpdate);
576
+ }
577
+ this.sql.trimEnd(' ');
578
+ if (options.primaryKey) {
579
+ this.insertReturning(options.primaryKey);
580
+ }
581
+ return this;
582
+ }
583
+ // ------------------------------------------------------------------------
584
+ // Update
585
+ // ------------------------------------------------------------------------
586
+ updateStatement(table) {
587
+ this.sql.append('UPDATE ');
588
+ this.sql.tableName(table);
589
+ this.sql.space();
590
+ return this;
591
+ }
592
+ updateKeyValues(values) {
593
+ const keys = Object.keys(values);
594
+ for (const key of keys) {
595
+ this.sql.columnName(key);
596
+ this.sql.append(' = ');
597
+ this.expression(values[key]);
598
+ this.sql = this.sql.trimEnd();
599
+ this.sql.append(', ');
600
+ }
601
+ this.sql.trimEnd(', ');
602
+ this.sql.space();
603
+ return this;
604
+ }
605
+ updateSetStatement(values) {
606
+ this.sql.append('SET ');
607
+ this.updateKeyValues(values);
608
+ return this;
609
+ }
610
+ update(options) {
611
+ var _a;
612
+ this.updateStatement(options.table);
613
+ for (const join of (_a = options.join) !== null && _a !== void 0 ? _a : []) {
614
+ this.join(join);
615
+ }
616
+ this.updateSetStatement(options.set);
617
+ if (options.from) {
618
+ this.selectFrom(options.from);
619
+ }
620
+ if (options.where) {
621
+ this.where(options.where);
622
+ }
623
+ return this;
624
+ }
625
+ // ------------------------------------------------------------------------
626
+ // Delete
627
+ // ------------------------------------------------------------------------
628
+ deleteStatement(table) {
629
+ this.sql.append('DELETE FROM ');
630
+ this.sql.tableName(table);
631
+ this.sql.space();
632
+ return this;
633
+ }
634
+ delete(options) {
635
+ var _a;
636
+ this.deleteStatement(options.table);
637
+ for (const join of (_a = options.join) !== null && _a !== void 0 ? _a : []) {
638
+ this.join(join);
639
+ }
640
+ if (options.where) {
641
+ this.where(options.where);
642
+ }
643
+ return this;
644
+ }
645
+ // ------------------------------------------------------------------------
646
+ // Database Functions
647
+ // ------------------------------------------------------------------------
648
+ /**
649
+ * Append a database function the query
650
+ *
651
+ * @param fn Database function token
652
+ */
653
+ databaseFunction(fn) {
654
+ switch (fn.fn) {
655
+ case function_token_1.DatabaseFunctionKeys.AVERAGE:
656
+ this.average(fn);
657
+ break;
658
+ case function_token_1.DatabaseFunctionKeys.COUNT:
659
+ this.count(fn);
660
+ break;
661
+ case function_token_1.DatabaseFunctionKeys.MIN:
662
+ this.min(fn);
663
+ break;
664
+ case function_token_1.DatabaseFunctionKeys.MAX:
665
+ this.max(fn);
666
+ break;
667
+ case function_token_1.DatabaseFunctionKeys.SUM:
668
+ this.sum(fn);
669
+ break;
670
+ case function_token_1.DatabaseFunctionKeys.CURRENT_TIMESTAMP:
671
+ this.currentTimestamp(fn);
672
+ break;
673
+ case function_token_1.DatabaseFunctionKeys.DATE:
674
+ this.date(fn);
675
+ break;
676
+ case function_token_1.DatabaseFunctionKeys.YEAR:
677
+ this.year(fn);
678
+ break;
679
+ case function_token_1.DatabaseFunctionKeys.UUID:
680
+ this.uuid();
681
+ break;
682
+ }
683
+ return this;
684
+ }
685
+ average(fn) {
686
+ this.sql.append('AVG');
687
+ this.sql.openParens();
688
+ if (fn.params.options.distinct) {
689
+ this.distinctStatement();
690
+ }
691
+ this.expression(fn.params.expr);
692
+ this.sql.closeParens();
693
+ return this;
694
+ }
695
+ count(fn) {
696
+ var _a, _b, _c, _d;
697
+ this.sql.append('COUNT(');
698
+ if ((_a = fn.params) === null || _a === void 0 ? void 0 : _a.distinct) {
699
+ this.distinctStatement();
700
+ }
701
+ if ((_b = fn.params) === null || _b === void 0 ? void 0 : _b.expr) {
702
+ this.expression(fn.params.expr);
703
+ }
704
+ else if ((_c = fn.params) === null || _c === void 0 ? void 0 : _c.column) {
705
+ this.sql.columnName(fn.params.column);
706
+ }
707
+ else if (((_d = fn.params) === null || _d === void 0 ? void 0 : _d.columns) && fn.params.columns.length > 0) {
708
+ // For multiple columns, separate with commas
709
+ this.sql.commaSeparate(fn.params.columns.map((col) => this.sql.getEnclosedName(col)));
710
+ }
711
+ else {
712
+ this.sql.append('*');
713
+ }
714
+ this.sql.append(')');
715
+ return this;
716
+ }
717
+ min(fn) {
718
+ this.sql.append('MIN');
719
+ this.sql.openParens();
720
+ this.expression(fn.params);
721
+ this.sql.closeParens();
722
+ return this;
723
+ }
724
+ max(fn) {
725
+ this.sql.append('MAX');
726
+ this.sql.openParens();
727
+ this.expression(fn.params);
728
+ this.sql.closeParens();
729
+ return this;
730
+ }
731
+ sum(fn) {
732
+ this.sql.append('SUM');
733
+ this.sql.openParens();
734
+ if (fn.params.options.distinct) {
735
+ this.distinctStatement();
736
+ }
737
+ this.expression(fn.params.expr);
738
+ this.sql.closeParens();
739
+ return this;
740
+ }
741
+ currentTimestamp(fn) {
742
+ this.sql.append('CURRENT_TIMESTAMP');
743
+ return this;
744
+ }
745
+ date(fn) {
746
+ var _a;
747
+ this.sql.append('date');
748
+ this.sql.openParens();
749
+ if ((_a = fn.params) === null || _a === void 0 ? void 0 : _a.expr) {
750
+ this.expression(fn.params.expr);
751
+ }
752
+ else {
753
+ this.expression(functions_1.DatabaseFunctions.currentTimestamp());
754
+ }
755
+ this.sql.closeParens();
756
+ return this;
757
+ }
758
+ year(fn) {
759
+ var _a;
760
+ this.sql.append('year');
761
+ this.sql.openParens();
762
+ if ((_a = fn.params) === null || _a === void 0 ? void 0 : _a.expr) {
763
+ this.expression(fn.params.expr);
764
+ }
765
+ else {
766
+ this.expression(functions_1.DatabaseFunctions.currentTimestamp());
767
+ }
768
+ this.sql.closeParens();
769
+ return this;
770
+ }
771
+ uuid() {
772
+ this.sql.append('uuid()');
773
+ return this;
774
+ }
775
+ // ------------------------------------------------------------------------
776
+ // Triggers
777
+ // ------------------------------------------------------------------------
778
+ getTriggerOld() {
779
+ return 'OLD';
780
+ }
781
+ getTriggerOldColumn(column) {
782
+ return `${this.getTriggerOld()}.${column}`;
783
+ }
784
+ getTriggerNew() {
785
+ return 'NEW';
786
+ }
787
+ getTriggerNewColumn(column) {
788
+ return `${this.getTriggerNew()}.${column}`;
789
+ }
790
+ triggerSetValue(options) {
791
+ return this.set({
792
+ column: `NEW.${options.column}`,
793
+ value: options.value,
794
+ });
795
+ }
796
+ }
797
+ exports.DatabaseQueryBuilder = DatabaseQueryBuilder;
794
798
  //# sourceMappingURL=query-builder.js.map