@simplysm/orm-common 13.0.68 → 13.0.70

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 (204) hide show
  1. package/README.md +54 -1447
  2. package/dist/create-db-context.d.ts +10 -10
  3. package/dist/create-db-context.js +9 -9
  4. package/dist/create-db-context.js.map +1 -1
  5. package/dist/ddl/column-ddl.d.ts +4 -4
  6. package/dist/ddl/initialize.d.ts +17 -17
  7. package/dist/ddl/initialize.js +2 -2
  8. package/dist/ddl/initialize.js.map +1 -1
  9. package/dist/ddl/relation-ddl.d.ts +6 -6
  10. package/dist/ddl/schema-ddl.d.ts +4 -4
  11. package/dist/ddl/table-ddl.d.ts +24 -24
  12. package/dist/ddl/table-ddl.js +4 -4
  13. package/dist/ddl/table-ddl.js.map +1 -1
  14. package/dist/errors/db-transaction-error.d.ts +15 -15
  15. package/dist/errors/db-transaction-error.d.ts.map +1 -1
  16. package/dist/exec/executable.d.ts +23 -23
  17. package/dist/exec/executable.js +3 -3
  18. package/dist/exec/executable.js.map +1 -1
  19. package/dist/exec/queryable.d.ts +160 -160
  20. package/dist/exec/queryable.js +119 -119
  21. package/dist/exec/queryable.js.map +1 -1
  22. package/dist/exec/search-parser.d.ts +37 -37
  23. package/dist/exec/search-parser.d.ts.map +1 -1
  24. package/dist/expr/expr-unit.d.ts +4 -4
  25. package/dist/expr/expr.d.ts +257 -257
  26. package/dist/expr/expr.js +265 -265
  27. package/dist/expr/expr.js.map +1 -1
  28. package/dist/query-builder/base/expr-renderer-base.d.ts +9 -9
  29. package/dist/query-builder/base/expr-renderer-base.js +2 -2
  30. package/dist/query-builder/base/expr-renderer-base.js.map +1 -1
  31. package/dist/query-builder/base/query-builder-base.d.ts +26 -26
  32. package/dist/query-builder/base/query-builder-base.d.ts.map +1 -1
  33. package/dist/query-builder/base/query-builder-base.js +22 -22
  34. package/dist/query-builder/base/query-builder-base.js.map +1 -1
  35. package/dist/query-builder/mssql/mssql-expr-renderer.d.ts +4 -4
  36. package/dist/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -1
  37. package/dist/query-builder/mssql/mssql-expr-renderer.js +18 -18
  38. package/dist/query-builder/mssql/mssql-expr-renderer.js.map +1 -1
  39. package/dist/query-builder/mssql/mssql-query-builder.d.ts +2 -2
  40. package/dist/query-builder/mssql/mssql-query-builder.d.ts.map +1 -1
  41. package/dist/query-builder/mssql/mssql-query-builder.js +11 -11
  42. package/dist/query-builder/mssql/mssql-query-builder.js.map +1 -1
  43. package/dist/query-builder/mysql/mysql-expr-renderer.d.ts +4 -4
  44. package/dist/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -1
  45. package/dist/query-builder/mysql/mysql-expr-renderer.js +17 -17
  46. package/dist/query-builder/mysql/mysql-expr-renderer.js.map +1 -1
  47. package/dist/query-builder/mysql/mysql-query-builder.d.ts +8 -8
  48. package/dist/query-builder/mysql/mysql-query-builder.d.ts.map +1 -1
  49. package/dist/query-builder/mysql/mysql-query-builder.js +5 -5
  50. package/dist/query-builder/mysql/mysql-query-builder.js.map +1 -1
  51. package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts +4 -4
  52. package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -1
  53. package/dist/query-builder/postgresql/postgresql-expr-renderer.js +17 -17
  54. package/dist/query-builder/postgresql/postgresql-expr-renderer.js.map +1 -1
  55. package/dist/query-builder/postgresql/postgresql-query-builder.d.ts +5 -5
  56. package/dist/query-builder/postgresql/postgresql-query-builder.d.ts.map +1 -1
  57. package/dist/query-builder/postgresql/postgresql-query-builder.js +8 -8
  58. package/dist/query-builder/postgresql/postgresql-query-builder.js.map +1 -1
  59. package/dist/query-builder/query-builder.d.ts +1 -1
  60. package/dist/schema/factory/column-builder.d.ts +79 -79
  61. package/dist/schema/factory/column-builder.js +42 -42
  62. package/dist/schema/factory/index-builder.d.ts +39 -39
  63. package/dist/schema/factory/index-builder.js +26 -26
  64. package/dist/schema/factory/relation-builder.d.ts +99 -99
  65. package/dist/schema/factory/relation-builder.d.ts.map +1 -1
  66. package/dist/schema/factory/relation-builder.js +38 -38
  67. package/dist/schema/procedure-builder.d.ts +49 -49
  68. package/dist/schema/procedure-builder.d.ts.map +1 -1
  69. package/dist/schema/procedure-builder.js +33 -33
  70. package/dist/schema/table-builder.d.ts +59 -59
  71. package/dist/schema/table-builder.d.ts.map +1 -1
  72. package/dist/schema/table-builder.js +43 -43
  73. package/dist/schema/view-builder.d.ts +49 -49
  74. package/dist/schema/view-builder.d.ts.map +1 -1
  75. package/dist/schema/view-builder.js +32 -32
  76. package/dist/types/column.d.ts +22 -22
  77. package/dist/types/column.js +1 -1
  78. package/dist/types/column.js.map +1 -1
  79. package/dist/types/db.d.ts +40 -40
  80. package/dist/types/expr.d.ts +59 -59
  81. package/dist/types/expr.d.ts.map +1 -1
  82. package/dist/types/query-def.d.ts +44 -44
  83. package/dist/types/query-def.d.ts.map +1 -1
  84. package/dist/utils/result-parser.d.ts +11 -11
  85. package/dist/utils/result-parser.js +3 -3
  86. package/dist/utils/result-parser.js.map +1 -1
  87. package/package.json +5 -5
  88. package/src/create-db-context.ts +20 -20
  89. package/src/ddl/column-ddl.ts +4 -4
  90. package/src/ddl/initialize.ts +259 -259
  91. package/src/ddl/relation-ddl.ts +89 -89
  92. package/src/ddl/schema-ddl.ts +4 -4
  93. package/src/ddl/table-ddl.ts +189 -189
  94. package/src/errors/db-transaction-error.ts +13 -13
  95. package/src/exec/executable.ts +25 -25
  96. package/src/exec/queryable.ts +2033 -2033
  97. package/src/exec/search-parser.ts +57 -57
  98. package/src/expr/expr-unit.ts +4 -4
  99. package/src/expr/expr.ts +2140 -2140
  100. package/src/query-builder/base/expr-renderer-base.ts +237 -237
  101. package/src/query-builder/base/query-builder-base.ts +213 -213
  102. package/src/query-builder/mssql/mssql-expr-renderer.ts +607 -607
  103. package/src/query-builder/mssql/mssql-query-builder.ts +650 -650
  104. package/src/query-builder/mysql/mysql-expr-renderer.ts +613 -613
  105. package/src/query-builder/mysql/mysql-query-builder.ts +759 -759
  106. package/src/query-builder/postgresql/postgresql-expr-renderer.ts +611 -611
  107. package/src/query-builder/postgresql/postgresql-query-builder.ts +686 -686
  108. package/src/query-builder/query-builder.ts +19 -19
  109. package/src/schema/factory/column-builder.ts +423 -423
  110. package/src/schema/factory/index-builder.ts +164 -164
  111. package/src/schema/factory/relation-builder.ts +453 -453
  112. package/src/schema/procedure-builder.ts +232 -232
  113. package/src/schema/table-builder.ts +319 -319
  114. package/src/schema/view-builder.ts +221 -221
  115. package/src/types/column.ts +188 -188
  116. package/src/types/db.ts +208 -208
  117. package/src/types/expr.ts +697 -697
  118. package/src/types/query-def.ts +513 -513
  119. package/src/utils/result-parser.ts +458 -458
  120. package/tests/db-context/create-db-context.spec.ts +224 -0
  121. package/tests/db-context/define-db-context.spec.ts +68 -0
  122. package/tests/ddl/basic.expected.ts +341 -0
  123. package/tests/ddl/basic.spec.ts +714 -0
  124. package/tests/ddl/column-builder.expected.ts +310 -0
  125. package/tests/ddl/column-builder.spec.ts +637 -0
  126. package/tests/ddl/index-builder.expected.ts +38 -0
  127. package/tests/ddl/index-builder.spec.ts +202 -0
  128. package/tests/ddl/procedure-builder.expected.ts +52 -0
  129. package/tests/ddl/procedure-builder.spec.ts +234 -0
  130. package/tests/ddl/relation-builder.expected.ts +36 -0
  131. package/tests/ddl/relation-builder.spec.ts +372 -0
  132. package/tests/ddl/table-builder.expected.ts +113 -0
  133. package/tests/ddl/table-builder.spec.ts +433 -0
  134. package/tests/ddl/view-builder.expected.ts +38 -0
  135. package/tests/ddl/view-builder.spec.ts +176 -0
  136. package/tests/dml/delete.expected.ts +96 -0
  137. package/tests/dml/delete.spec.ts +160 -0
  138. package/tests/dml/insert.expected.ts +192 -0
  139. package/tests/dml/insert.spec.ts +288 -0
  140. package/tests/dml/update.expected.ts +176 -0
  141. package/tests/dml/update.spec.ts +318 -0
  142. package/tests/dml/upsert.expected.ts +215 -0
  143. package/tests/dml/upsert.spec.ts +242 -0
  144. package/tests/errors/queryable-errors.spec.ts +177 -0
  145. package/tests/escape.spec.ts +100 -0
  146. package/tests/examples/pivot.expected.ts +211 -0
  147. package/tests/examples/pivot.spec.ts +533 -0
  148. package/tests/examples/sampling.expected.ts +69 -0
  149. package/tests/examples/sampling.spec.ts +104 -0
  150. package/tests/examples/unpivot.expected.ts +120 -0
  151. package/tests/examples/unpivot.spec.ts +226 -0
  152. package/tests/exec/search-parser.spec.ts +283 -0
  153. package/tests/executable/basic.expected.ts +18 -0
  154. package/tests/executable/basic.spec.ts +54 -0
  155. package/tests/expr/comparison.expected.ts +282 -0
  156. package/tests/expr/comparison.spec.ts +400 -0
  157. package/tests/expr/conditional.expected.ts +134 -0
  158. package/tests/expr/conditional.spec.ts +276 -0
  159. package/tests/expr/date.expected.ts +332 -0
  160. package/tests/expr/date.spec.ts +526 -0
  161. package/tests/expr/math.expected.ts +62 -0
  162. package/tests/expr/math.spec.ts +106 -0
  163. package/tests/expr/string.expected.ts +218 -0
  164. package/tests/expr/string.spec.ts +356 -0
  165. package/tests/expr/utility.expected.ts +147 -0
  166. package/tests/expr/utility.spec.ts +182 -0
  167. package/tests/select/basic.expected.ts +322 -0
  168. package/tests/select/basic.spec.ts +502 -0
  169. package/tests/select/filter.expected.ts +357 -0
  170. package/tests/select/filter.spec.ts +1068 -0
  171. package/tests/select/group.expected.ts +169 -0
  172. package/tests/select/group.spec.ts +244 -0
  173. package/tests/select/join.expected.ts +582 -0
  174. package/tests/select/join.spec.ts +805 -0
  175. package/tests/select/order.expected.ts +150 -0
  176. package/tests/select/order.spec.ts +189 -0
  177. package/tests/select/recursive-cte.expected.ts +244 -0
  178. package/tests/select/recursive-cte.spec.ts +514 -0
  179. package/tests/select/result-meta.spec.ts +270 -0
  180. package/tests/select/subquery.expected.ts +363 -0
  181. package/tests/select/subquery.spec.ts +537 -0
  182. package/tests/select/view.expected.ts +155 -0
  183. package/tests/select/view.spec.ts +235 -0
  184. package/tests/select/window.expected.ts +345 -0
  185. package/tests/select/window.spec.ts +618 -0
  186. package/tests/setup/MockExecutor.ts +18 -0
  187. package/tests/setup/TestDbContext.ts +59 -0
  188. package/tests/setup/models/Company.ts +13 -0
  189. package/tests/setup/models/Employee.ts +10 -0
  190. package/tests/setup/models/MonthlySales.ts +11 -0
  191. package/tests/setup/models/Post.ts +16 -0
  192. package/tests/setup/models/Sales.ts +10 -0
  193. package/tests/setup/models/User.ts +19 -0
  194. package/tests/setup/procedure/GetAllUsers.ts +9 -0
  195. package/tests/setup/procedure/GetUserById.ts +12 -0
  196. package/tests/setup/test-utils.ts +72 -0
  197. package/tests/setup/views/ActiveUsers.ts +8 -0
  198. package/tests/setup/views/UserSummary.ts +11 -0
  199. package/tests/types/nullable-queryable-record.spec.ts +145 -0
  200. package/tests/utils/result-parser-perf.spec.ts +210 -0
  201. package/tests/utils/result-parser.spec.ts +701 -0
  202. package/docs/expressions.md +0 -172
  203. package/docs/queries.md +0 -444
  204. package/docs/schema.md +0 -245
@@ -0,0 +1,533 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { createTestDb } from "../setup/TestDbContext";
3
+ import { expr } from "../../src/expr/expr";
4
+ import { createQueryBuilder } from "../../src/query-builder/query-builder";
5
+ import { dialects } from "../setup/test-utils";
6
+ import "../setup/test-utils";
7
+ import * as expected from "./pivot.expected";
8
+
9
+ describe("SELECT - PIVOT (groupBy + switch)", () => {
10
+ describe("Basic (SUM)", () => {
11
+ const db = createTestDb();
12
+ const def = db
13
+ .sales()
14
+ .groupBy((item) => [item.id, item.category])
15
+ .select((item) => ({
16
+ id: item.id,
17
+ category: item.category,
18
+ y2020: expr.sum(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
19
+ y2021: expr.sum(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
20
+ }))
21
+ .getSelectQueryDef();
22
+
23
+ it("Verify QueryDef", () => {
24
+ expect(def).toEqual({
25
+ type: "select",
26
+ as: "T1",
27
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
28
+ groupBy: [
29
+ { type: "column", path: ["T1", "id"] },
30
+ { type: "column", path: ["T1", "category"] },
31
+ ],
32
+ select: {
33
+ id: { type: "column", path: ["T1", "id"] },
34
+ category: { type: "column", path: ["T1", "category"] },
35
+ y2020: {
36
+ type: "sum",
37
+ arg: {
38
+ type: "if",
39
+ condition: {
40
+ type: "eq",
41
+ source: { type: "column", path: ["T1", "year"] },
42
+ target: { type: "value", value: 2020 },
43
+ },
44
+ then: { type: "column", path: ["T1", "amount"] },
45
+ else: { type: "value", value: undefined },
46
+ },
47
+ },
48
+ y2021: {
49
+ type: "sum",
50
+ arg: {
51
+ type: "if",
52
+ condition: {
53
+ type: "eq",
54
+ source: { type: "column", path: ["T1", "year"] },
55
+ target: { type: "value", value: 2021 },
56
+ },
57
+ then: { type: "column", path: ["T1", "amount"] },
58
+ else: { type: "value", value: undefined },
59
+ },
60
+ },
61
+ },
62
+ });
63
+ });
64
+
65
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
66
+ const builder = createQueryBuilder(dialect);
67
+ expect(builder.build(def)).toMatchSql(expected.pivotSum[dialect]);
68
+ });
69
+ });
70
+
71
+ describe("COUNT", () => {
72
+ const db = createTestDb();
73
+ const def = db
74
+ .sales()
75
+ .groupBy((item) => [item.id, item.category])
76
+ .select((item) => ({
77
+ id: item.id,
78
+ category: item.category,
79
+ y2020: expr.count(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
80
+ y2021: expr.count(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
81
+ }))
82
+ .getSelectQueryDef();
83
+
84
+ it("Verify QueryDef", () => {
85
+ expect(def).toEqual({
86
+ type: "select",
87
+ as: "T1",
88
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
89
+ groupBy: [
90
+ { type: "column", path: ["T1", "id"] },
91
+ { type: "column", path: ["T1", "category"] },
92
+ ],
93
+ select: {
94
+ id: { type: "column", path: ["T1", "id"] },
95
+ category: { type: "column", path: ["T1", "category"] },
96
+ y2020: {
97
+ type: "count",
98
+ arg: {
99
+ type: "if",
100
+ condition: {
101
+ type: "eq",
102
+ source: { type: "column", path: ["T1", "year"] },
103
+ target: { type: "value", value: 2020 },
104
+ },
105
+ then: { type: "column", path: ["T1", "amount"] },
106
+ else: { type: "value", value: undefined },
107
+ },
108
+ },
109
+ y2021: {
110
+ type: "count",
111
+ arg: {
112
+ type: "if",
113
+ condition: {
114
+ type: "eq",
115
+ source: { type: "column", path: ["T1", "year"] },
116
+ target: { type: "value", value: 2021 },
117
+ },
118
+ then: { type: "column", path: ["T1", "amount"] },
119
+ else: { type: "value", value: undefined },
120
+ },
121
+ },
122
+ },
123
+ });
124
+ });
125
+
126
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
127
+ const builder = createQueryBuilder(dialect);
128
+ expect(builder.build(def)).toMatchSql(expected.pivotCount[dialect]);
129
+ });
130
+ });
131
+
132
+ describe("AVG", () => {
133
+ const db = createTestDb();
134
+ const def = db
135
+ .sales()
136
+ .groupBy((item) => [item.id, item.category])
137
+ .select((item) => ({
138
+ id: item.id,
139
+ category: item.category,
140
+ y2020: expr.avg(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
141
+ y2021: expr.avg(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
142
+ }))
143
+ .getSelectQueryDef();
144
+
145
+ it("Verify QueryDef", () => {
146
+ expect(def).toEqual({
147
+ type: "select",
148
+ as: "T1",
149
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
150
+ groupBy: [
151
+ { type: "column", path: ["T1", "id"] },
152
+ { type: "column", path: ["T1", "category"] },
153
+ ],
154
+ select: {
155
+ id: { type: "column", path: ["T1", "id"] },
156
+ category: { type: "column", path: ["T1", "category"] },
157
+ y2020: {
158
+ type: "avg",
159
+ arg: {
160
+ type: "if",
161
+ condition: {
162
+ type: "eq",
163
+ source: { type: "column", path: ["T1", "year"] },
164
+ target: { type: "value", value: 2020 },
165
+ },
166
+ then: { type: "column", path: ["T1", "amount"] },
167
+ else: { type: "value", value: undefined },
168
+ },
169
+ },
170
+ y2021: {
171
+ type: "avg",
172
+ arg: {
173
+ type: "if",
174
+ condition: {
175
+ type: "eq",
176
+ source: { type: "column", path: ["T1", "year"] },
177
+ target: { type: "value", value: 2021 },
178
+ },
179
+ then: { type: "column", path: ["T1", "amount"] },
180
+ else: { type: "value", value: undefined },
181
+ },
182
+ },
183
+ },
184
+ });
185
+ });
186
+
187
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
188
+ const builder = createQueryBuilder(dialect);
189
+ expect(builder.build(def)).toMatchSql(expected.pivotAvg[dialect]);
190
+ });
191
+ });
192
+
193
+ describe("MAX", () => {
194
+ const db = createTestDb();
195
+ const def = db
196
+ .sales()
197
+ .groupBy((item) => [item.id, item.category])
198
+ .select((item) => ({
199
+ id: item.id,
200
+ category: item.category,
201
+ y2020: expr.max(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
202
+ y2021: expr.max(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
203
+ }))
204
+ .getSelectQueryDef();
205
+
206
+ it("Verify QueryDef", () => {
207
+ expect(def).toEqual({
208
+ type: "select",
209
+ as: "T1",
210
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
211
+ groupBy: [
212
+ { type: "column", path: ["T1", "id"] },
213
+ { type: "column", path: ["T1", "category"] },
214
+ ],
215
+ select: {
216
+ id: { type: "column", path: ["T1", "id"] },
217
+ category: { type: "column", path: ["T1", "category"] },
218
+ y2020: {
219
+ type: "max",
220
+ arg: {
221
+ type: "if",
222
+ condition: {
223
+ type: "eq",
224
+ source: { type: "column", path: ["T1", "year"] },
225
+ target: { type: "value", value: 2020 },
226
+ },
227
+ then: { type: "column", path: ["T1", "amount"] },
228
+ else: { type: "value", value: undefined },
229
+ },
230
+ },
231
+ y2021: {
232
+ type: "max",
233
+ arg: {
234
+ type: "if",
235
+ condition: {
236
+ type: "eq",
237
+ source: { type: "column", path: ["T1", "year"] },
238
+ target: { type: "value", value: 2021 },
239
+ },
240
+ then: { type: "column", path: ["T1", "amount"] },
241
+ else: { type: "value", value: undefined },
242
+ },
243
+ },
244
+ },
245
+ });
246
+ });
247
+
248
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
249
+ const builder = createQueryBuilder(dialect);
250
+ expect(builder.build(def)).toMatchSql(expected.pivotMax[dialect]);
251
+ });
252
+ });
253
+
254
+ describe("MIN", () => {
255
+ const db = createTestDb();
256
+ const def = db
257
+ .sales()
258
+ .groupBy((item) => [item.id, item.category])
259
+ .select((item) => ({
260
+ id: item.id,
261
+ category: item.category,
262
+ y2020: expr.min(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
263
+ y2021: expr.min(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
264
+ }))
265
+ .getSelectQueryDef();
266
+
267
+ it("Verify QueryDef", () => {
268
+ expect(def).toEqual({
269
+ type: "select",
270
+ as: "T1",
271
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
272
+ groupBy: [
273
+ { type: "column", path: ["T1", "id"] },
274
+ { type: "column", path: ["T1", "category"] },
275
+ ],
276
+ select: {
277
+ id: { type: "column", path: ["T1", "id"] },
278
+ category: { type: "column", path: ["T1", "category"] },
279
+ y2020: {
280
+ type: "min",
281
+ arg: {
282
+ type: "if",
283
+ condition: {
284
+ type: "eq",
285
+ source: { type: "column", path: ["T1", "year"] },
286
+ target: { type: "value", value: 2020 },
287
+ },
288
+ then: { type: "column", path: ["T1", "amount"] },
289
+ else: { type: "value", value: undefined },
290
+ },
291
+ },
292
+ y2021: {
293
+ type: "min",
294
+ arg: {
295
+ type: "if",
296
+ condition: {
297
+ type: "eq",
298
+ source: { type: "column", path: ["T1", "year"] },
299
+ target: { type: "value", value: 2021 },
300
+ },
301
+ then: { type: "column", path: ["T1", "amount"] },
302
+ else: { type: "value", value: undefined },
303
+ },
304
+ },
305
+ },
306
+ });
307
+ });
308
+
309
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
310
+ const builder = createQueryBuilder(dialect);
311
+ expect(builder.build(def)).toMatchSql(expected.pivotMin[dialect]);
312
+ });
313
+ });
314
+
315
+ describe("3 or more pivot values", () => {
316
+ const db = createTestDb();
317
+ const def = db
318
+ .sales()
319
+ .groupBy((item) => [item.id, item.category])
320
+ .select((item) => ({
321
+ id: item.id,
322
+ category: item.category,
323
+ y2019: expr.sum(expr.if(expr.eq(item.year, 2019), item.amount, undefined)),
324
+ y2020: expr.sum(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
325
+ y2021: expr.sum(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
326
+ y2022: expr.sum(expr.if(expr.eq(item.year, 2022), item.amount, undefined)),
327
+ }))
328
+ .getSelectQueryDef();
329
+
330
+ it("Verify QueryDef", () => {
331
+ expect(def).toEqual({
332
+ type: "select",
333
+ as: "T1",
334
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
335
+ groupBy: [
336
+ { type: "column", path: ["T1", "id"] },
337
+ { type: "column", path: ["T1", "category"] },
338
+ ],
339
+ select: {
340
+ id: { type: "column", path: ["T1", "id"] },
341
+ category: { type: "column", path: ["T1", "category"] },
342
+ y2019: {
343
+ type: "sum",
344
+ arg: {
345
+ type: "if",
346
+ condition: {
347
+ type: "eq",
348
+ source: { type: "column", path: ["T1", "year"] },
349
+ target: { type: "value", value: 2019 },
350
+ },
351
+ then: { type: "column", path: ["T1", "amount"] },
352
+ else: { type: "value", value: undefined },
353
+ },
354
+ },
355
+ y2020: {
356
+ type: "sum",
357
+ arg: {
358
+ type: "if",
359
+ condition: {
360
+ type: "eq",
361
+ source: { type: "column", path: ["T1", "year"] },
362
+ target: { type: "value", value: 2020 },
363
+ },
364
+ then: { type: "column", path: ["T1", "amount"] },
365
+ else: { type: "value", value: undefined },
366
+ },
367
+ },
368
+ y2021: {
369
+ type: "sum",
370
+ arg: {
371
+ type: "if",
372
+ condition: {
373
+ type: "eq",
374
+ source: { type: "column", path: ["T1", "year"] },
375
+ target: { type: "value", value: 2021 },
376
+ },
377
+ then: { type: "column", path: ["T1", "amount"] },
378
+ else: { type: "value", value: undefined },
379
+ },
380
+ },
381
+ y2022: {
382
+ type: "sum",
383
+ arg: {
384
+ type: "if",
385
+ condition: {
386
+ type: "eq",
387
+ source: { type: "column", path: ["T1", "year"] },
388
+ target: { type: "value", value: 2022 },
389
+ },
390
+ then: { type: "column", path: ["T1", "amount"] },
391
+ else: { type: "value", value: undefined },
392
+ },
393
+ },
394
+ },
395
+ });
396
+ });
397
+
398
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
399
+ const builder = createQueryBuilder(dialect);
400
+ expect(builder.build(def)).toMatchSql(expected.pivotMultipleYears[dialect]);
401
+ });
402
+ });
403
+
404
+ describe("String pivot column", () => {
405
+ const db = createTestDb();
406
+ const def = db
407
+ .sales()
408
+ .groupBy((item) => [item.id, item.year])
409
+ .select((item) => ({
410
+ id: item.id,
411
+ year: item.year,
412
+ food: expr.sum(expr.if(expr.eq(item.category, "Food"), item.amount, undefined)),
413
+ electronics: expr.sum(expr.if(expr.eq(item.category, "Electronics"), item.amount, undefined)),
414
+ }))
415
+ .getSelectQueryDef();
416
+
417
+ it("Verify QueryDef", () => {
418
+ expect(def).toEqual({
419
+ type: "select",
420
+ as: "T1",
421
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
422
+ groupBy: [
423
+ { type: "column", path: ["T1", "id"] },
424
+ { type: "column", path: ["T1", "year"] },
425
+ ],
426
+ select: {
427
+ id: { type: "column", path: ["T1", "id"] },
428
+ year: { type: "column", path: ["T1", "year"] },
429
+ food: {
430
+ type: "sum",
431
+ arg: {
432
+ type: "if",
433
+ condition: {
434
+ type: "eq",
435
+ source: { type: "column", path: ["T1", "category"] },
436
+ target: { type: "value", value: "Food" },
437
+ },
438
+ then: { type: "column", path: ["T1", "amount"] },
439
+ else: { type: "value", value: undefined },
440
+ },
441
+ },
442
+ electronics: {
443
+ type: "sum",
444
+ arg: {
445
+ type: "if",
446
+ condition: {
447
+ type: "eq",
448
+ source: { type: "column", path: ["T1", "category"] },
449
+ target: { type: "value", value: "Electronics" },
450
+ },
451
+ then: { type: "column", path: ["T1", "amount"] },
452
+ else: { type: "value", value: undefined },
453
+ },
454
+ },
455
+ },
456
+ });
457
+ });
458
+
459
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
460
+ const builder = createQueryBuilder(dialect);
461
+ expect(builder.build(def)).toMatchSql(expected.pivotStringColumn[dialect]);
462
+ });
463
+ });
464
+
465
+ describe("WHERE -> PIVOT (groupBy)", () => {
466
+ const db = createTestDb();
467
+ const def = db
468
+ .sales()
469
+ .where((item) => [expr.eq(item.category, "Food")])
470
+ .groupBy((item) => [item.id, item.category])
471
+ .select((item) => ({
472
+ id: item.id,
473
+ category: item.category,
474
+ y2020: expr.sum(expr.if(expr.eq(item.year, 2020), item.amount, undefined)),
475
+ y2021: expr.sum(expr.if(expr.eq(item.year, 2021), item.amount, undefined)),
476
+ }))
477
+ .getSelectQueryDef();
478
+
479
+ it("Verify QueryDef", () => {
480
+ expect(def).toEqual({
481
+ type: "select",
482
+ as: "T1",
483
+ from: { database: "TestDb", schema: "TestSchema", name: "Sales" },
484
+ where: [
485
+ {
486
+ type: "eq",
487
+ source: { type: "column", path: ["T1", "category"] },
488
+ target: { type: "value", value: "Food" },
489
+ },
490
+ ],
491
+ groupBy: [
492
+ { type: "column", path: ["T1", "id"] },
493
+ { type: "column", path: ["T1", "category"] },
494
+ ],
495
+ select: {
496
+ id: { type: "column", path: ["T1", "id"] },
497
+ category: { type: "column", path: ["T1", "category"] },
498
+ y2020: {
499
+ type: "sum",
500
+ arg: {
501
+ type: "if",
502
+ condition: {
503
+ type: "eq",
504
+ source: { type: "column", path: ["T1", "year"] },
505
+ target: { type: "value", value: 2020 },
506
+ },
507
+ then: { type: "column", path: ["T1", "amount"] },
508
+ else: { type: "value", value: undefined },
509
+ },
510
+ },
511
+ y2021: {
512
+ type: "sum",
513
+ arg: {
514
+ type: "if",
515
+ condition: {
516
+ type: "eq",
517
+ source: { type: "column", path: ["T1", "year"] },
518
+ target: { type: "value", value: 2021 },
519
+ },
520
+ then: { type: "column", path: ["T1", "amount"] },
521
+ else: { type: "value", value: undefined },
522
+ },
523
+ },
524
+ },
525
+ });
526
+ });
527
+
528
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
529
+ const builder = createQueryBuilder(dialect);
530
+ expect(builder.build(def)).toMatchSql(expected.pivotWithWhere[dialect]);
531
+ });
532
+ });
533
+ });
@@ -0,0 +1,69 @@
1
+ import { mysql, pgsql, tsql } from "@simplysm/core-common";
2
+ import type { ExpectedSql } from "../setup/test-utils";
3
+
4
+ //#region ========== 랜덤 샘플링 ==========
5
+
6
+ export const samplingBasic: ExpectedSql = {
7
+ mysql: mysql`
8
+ SELECT *
9
+ FROM \`TestDb\`.\`User\` AS \`T1\`
10
+ ORDER BY RAND()
11
+ LIMIT 5
12
+ `,
13
+ mssql: tsql`
14
+ SELECT TOP 5 *
15
+ FROM [TestDb].[TestSchema].[User] AS [T1]
16
+ ORDER BY NEWID()
17
+ `,
18
+ postgresql: pgsql`
19
+ SELECT *
20
+ FROM "TestSchema"."User" AS "T1"
21
+ ORDER BY RANDOM()
22
+ LIMIT 5
23
+ `,
24
+ };
25
+
26
+ export const samplingWithWhere: ExpectedSql = {
27
+ mysql: mysql`
28
+ SELECT *
29
+ FROM \`TestDb\`.\`User\` AS \`T1\`
30
+ WHERE \`T1\`.\`age\` >= 20
31
+ ORDER BY RAND()
32
+ LIMIT 3
33
+ `,
34
+ mssql: tsql`
35
+ SELECT TOP 3 *
36
+ FROM [TestDb].[TestSchema].[User] AS [T1]
37
+ WHERE [T1].[age] >= 20
38
+ ORDER BY NEWID()
39
+ `,
40
+ postgresql: pgsql`
41
+ SELECT *
42
+ FROM "TestSchema"."User" AS "T1"
43
+ WHERE "T1"."age" >= 20
44
+ ORDER BY RANDOM()
45
+ LIMIT 3
46
+ `,
47
+ };
48
+
49
+ export const samplingWithSelect: ExpectedSql = {
50
+ mysql: mysql`
51
+ SELECT \`T1\`.\`id\` AS \`id\`, \`T1\`.\`name\` AS \`name\`
52
+ FROM \`TestDb\`.\`User\` AS \`T1\`
53
+ ORDER BY RAND()
54
+ LIMIT 10
55
+ `,
56
+ mssql: tsql`
57
+ SELECT TOP 10 [T1].[id] AS [id], [T1].[name] AS [name]
58
+ FROM [TestDb].[TestSchema].[User] AS [T1]
59
+ ORDER BY NEWID()
60
+ `,
61
+ postgresql: pgsql`
62
+ SELECT "T1"."id" AS "id", "T1"."name" AS "name"
63
+ FROM "TestSchema"."User" AS "T1"
64
+ ORDER BY RANDOM()
65
+ LIMIT 10
66
+ `,
67
+ };
68
+
69
+ //#endregion
@@ -0,0 +1,104 @@
1
+ /**
2
+ * 랜덤 샘플링 예제
3
+ *
4
+ * sample() 메서드 대신 expr.random()과 orderBy/top을 조합하여 랜덤 샘플링 구현
5
+ *
6
+ * 사용법: db.user().orderBy(() => expr.random()).top(5)
7
+ */
8
+ import { describe, expect, it } from "vitest";
9
+ import { createTestDb } from "../setup/TestDbContext";
10
+ import { expr } from "../../src/expr/expr";
11
+ import { createQueryBuilder } from "../../src/query-builder/query-builder";
12
+ import { dialects } from "../setup/test-utils";
13
+ import "../setup/test-utils";
14
+ import * as expected from "./sampling.expected";
15
+
16
+ describe("EXAMPLE - Random sampling", () => {
17
+ describe("Basic sampling (ORDER BY RANDOM + TOP)", () => {
18
+ const db = createTestDb();
19
+ const def = db
20
+ .user()
21
+ .orderBy(() => expr.random())
22
+ .top(5)
23
+ .getSelectQueryDef();
24
+
25
+ it("Verify QueryDef", () => {
26
+ expect(def).toEqual({
27
+ type: "select",
28
+ as: "T1",
29
+ from: { database: "TestDb", schema: "TestSchema", name: "User" },
30
+ orderBy: [[{ type: "random" }]],
31
+ top: 5,
32
+ });
33
+ });
34
+
35
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
36
+ const builder = createQueryBuilder(dialect);
37
+ expect(builder.build(def)).toMatchSql(expected.samplingBasic[dialect]);
38
+ });
39
+ });
40
+
41
+ describe("Conditional sampling (WHERE + ORDER BY RANDOM + TOP)", () => {
42
+ const db = createTestDb();
43
+ const def = db
44
+ .user()
45
+ .where((item) => [expr.gte(item.age, 20)])
46
+ .orderBy(() => expr.random())
47
+ .top(3)
48
+ .getSelectQueryDef();
49
+
50
+ it("Verify QueryDef", () => {
51
+ expect(def).toEqual({
52
+ type: "select",
53
+ as: "T1",
54
+ from: { database: "TestDb", schema: "TestSchema", name: "User" },
55
+ where: [
56
+ {
57
+ type: "gte",
58
+ source: { type: "column", path: ["T1", "age"] },
59
+ target: { type: "value", value: 20 },
60
+ },
61
+ ],
62
+ orderBy: [[{ type: "random" }]],
63
+ top: 3,
64
+ });
65
+ });
66
+
67
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
68
+ const builder = createQueryBuilder(dialect);
69
+ expect(builder.build(def)).toMatchSql(expected.samplingWithWhere[dialect]);
70
+ });
71
+ });
72
+
73
+ describe("Column selection with sampling", () => {
74
+ const db = createTestDb();
75
+ const def = db
76
+ .user()
77
+ .select((item) => ({
78
+ id: item.id,
79
+ name: item.name,
80
+ }))
81
+ .orderBy(() => expr.random())
82
+ .top(10)
83
+ .getSelectQueryDef();
84
+
85
+ it("Verify QueryDef", () => {
86
+ expect(def).toEqual({
87
+ type: "select",
88
+ as: "T1",
89
+ from: { database: "TestDb", schema: "TestSchema", name: "User" },
90
+ select: {
91
+ id: { type: "column", path: ["T1", "id"] },
92
+ name: { type: "column", path: ["T1", "name"] },
93
+ },
94
+ orderBy: [[{ type: "random" }]],
95
+ top: 10,
96
+ });
97
+ });
98
+
99
+ it.each(dialects)("[%s] Verify SQL", (dialect) => {
100
+ const builder = createQueryBuilder(dialect);
101
+ expect(builder.build(def)).toMatchSql(expected.samplingWithSelect[dialect]);
102
+ });
103
+ });
104
+ });