drizzle-databend 0.1.11 → 0.1.12

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 (107) hide show
  1. package/dist/columns.d.ts +25 -37
  2. package/dist/databend-core/alias.d.ts +1 -0
  3. package/dist/databend-core/columns/all.d.ts +35 -0
  4. package/dist/databend-core/columns/array.d.ts +16 -0
  5. package/dist/databend-core/columns/bigint.d.ts +24 -0
  6. package/dist/databend-core/columns/binary.d.ts +13 -0
  7. package/dist/databend-core/columns/bitmap.d.ts +13 -0
  8. package/dist/databend-core/columns/boolean.d.ts +13 -0
  9. package/dist/databend-core/columns/common.d.ts +45 -0
  10. package/dist/databend-core/columns/custom.d.ts +33 -0
  11. package/dist/databend-core/columns/date.d.ts +16 -0
  12. package/dist/databend-core/columns/decimal.d.ts +19 -0
  13. package/dist/databend-core/columns/double.d.ts +14 -0
  14. package/dist/databend-core/columns/float.d.ts +14 -0
  15. package/dist/databend-core/columns/index.d.ts +21 -0
  16. package/dist/databend-core/columns/integer.d.ts +13 -0
  17. package/dist/databend-core/columns/map.d.ts +17 -0
  18. package/dist/databend-core/columns/smallint.d.ts +13 -0
  19. package/dist/databend-core/columns/text.d.ts +13 -0
  20. package/dist/databend-core/columns/timestamp.d.ts +16 -0
  21. package/dist/databend-core/columns/tinyint.d.ts +13 -0
  22. package/dist/databend-core/columns/tuple.d.ts +16 -0
  23. package/dist/databend-core/columns/varchar.d.ts +17 -0
  24. package/dist/databend-core/columns/variant.d.ts +15 -0
  25. package/dist/databend-core/db.d.ts +31 -0
  26. package/dist/databend-core/dialect.d.ts +29 -0
  27. package/dist/databend-core/index.d.ts +18 -0
  28. package/dist/databend-core/indexes.d.ts +24 -0
  29. package/dist/databend-core/primary-keys.d.ts +20 -0
  30. package/dist/databend-core/query-builders/count.d.ts +18 -0
  31. package/dist/databend-core/query-builders/delete.d.ts +18 -0
  32. package/dist/databend-core/query-builders/index.d.ts +5 -0
  33. package/dist/databend-core/query-builders/insert.d.ts +27 -0
  34. package/dist/databend-core/query-builders/query-builder.d.ts +19 -0
  35. package/dist/databend-core/query-builders/query.d.ts +37 -0
  36. package/dist/databend-core/query-builders/raw.d.ts +17 -0
  37. package/dist/databend-core/query-builders/select.d.ts +65 -0
  38. package/dist/databend-core/query-builders/update.d.ts +27 -0
  39. package/dist/databend-core/schema.d.ts +12 -0
  40. package/dist/databend-core/session.d.ts +28 -0
  41. package/dist/databend-core/subquery.d.ts +1 -0
  42. package/dist/databend-core/table.d.ts +12 -0
  43. package/dist/databend-core/utils.d.ts +7 -0
  44. package/dist/databend-core/view-base.d.ts +5 -0
  45. package/dist/databend-core/view-common.d.ts +1 -0
  46. package/dist/databend-core/view.d.ts +30 -0
  47. package/dist/dialect.d.ts +1 -10
  48. package/dist/driver.d.ts +4 -4
  49. package/dist/index.d.ts +10 -0
  50. package/dist/index.mjs +2584 -205
  51. package/dist/session.d.ts +22 -19
  52. package/dist/sql/result-mapper.d.ts +2 -3
  53. package/dist/sql/selection.d.ts +2 -1
  54. package/package.json +4 -4
  55. package/src/columns.ts +8 -7
  56. package/src/databend-core/alias.ts +5 -0
  57. package/src/databend-core/columns/all.ts +38 -0
  58. package/src/databend-core/columns/array.ts +46 -0
  59. package/src/databend-core/columns/bigint.ts +52 -0
  60. package/src/databend-core/columns/binary.ts +27 -0
  61. package/src/databend-core/columns/bitmap.ts +27 -0
  62. package/src/databend-core/columns/boolean.ts +27 -0
  63. package/src/databend-core/columns/common.ts +97 -0
  64. package/src/databend-core/columns/custom.ts +86 -0
  65. package/src/databend-core/columns/date.ts +49 -0
  66. package/src/databend-core/columns/decimal.ts +44 -0
  67. package/src/databend-core/columns/double.ts +34 -0
  68. package/src/databend-core/columns/float.ts +31 -0
  69. package/src/databend-core/columns/index.ts +21 -0
  70. package/src/databend-core/columns/integer.ts +27 -0
  71. package/src/databend-core/columns/map.ts +49 -0
  72. package/src/databend-core/columns/smallint.ts +27 -0
  73. package/src/databend-core/columns/text.ts +27 -0
  74. package/src/databend-core/columns/timestamp.ts +51 -0
  75. package/src/databend-core/columns/tinyint.ts +27 -0
  76. package/src/databend-core/columns/tuple.ts +46 -0
  77. package/src/databend-core/columns/varchar.ts +35 -0
  78. package/src/databend-core/columns/variant.ts +45 -0
  79. package/src/databend-core/db.ts +153 -0
  80. package/src/databend-core/dialect.ts +724 -0
  81. package/src/databend-core/index.ts +18 -0
  82. package/src/databend-core/indexes.ts +67 -0
  83. package/src/databend-core/primary-keys.ts +48 -0
  84. package/src/databend-core/query-builders/count.ts +47 -0
  85. package/src/databend-core/query-builders/delete.ts +56 -0
  86. package/src/databend-core/query-builders/index.ts +5 -0
  87. package/src/databend-core/query-builders/insert.ts +105 -0
  88. package/src/databend-core/query-builders/query-builder.ts +77 -0
  89. package/src/databend-core/query-builders/query.ts +124 -0
  90. package/src/databend-core/query-builders/raw.ts +37 -0
  91. package/src/databend-core/query-builders/select.ts +377 -0
  92. package/src/databend-core/query-builders/update.ts +82 -0
  93. package/src/databend-core/schema.ts +29 -0
  94. package/src/databend-core/session.ts +85 -0
  95. package/src/databend-core/subquery.ts +1 -0
  96. package/src/databend-core/table.ts +67 -0
  97. package/src/databend-core/utils.ts +34 -0
  98. package/src/databend-core/view-base.ts +6 -0
  99. package/src/databend-core/view-common.ts +1 -0
  100. package/src/databend-core/view.ts +127 -0
  101. package/src/dialect.ts +3 -119
  102. package/src/driver.ts +6 -7
  103. package/src/index.ts +27 -0
  104. package/src/migrator.ts +1 -2
  105. package/src/session.ts +42 -57
  106. package/src/sql/result-mapper.ts +12 -54
  107. package/src/sql/selection.ts +2 -1
package/dist/index.mjs CHANGED
@@ -125,8 +125,140 @@ async function closeClientConnection(connection) {
125
125
  }
126
126
  }
127
127
 
128
+ // src/databend-core/columns/custom.ts
129
+ import { entityKind as entityKind2 } from "drizzle-orm/entity";
130
+
131
+ // src/databend-core/columns/common.ts
132
+ import { Column } from "drizzle-orm/column";
133
+ import { ColumnBuilder } from "drizzle-orm/column-builder";
134
+ import { entityKind } from "drizzle-orm/entity";
135
+ var DatabendColumnBuilder = class extends ColumnBuilder {
136
+ static [entityKind] = "DatabendColumnBuilder";
137
+ generatedAlwaysAs(as, _config) {
138
+ this.config.generated = { as, type: "always", mode: "stored" };
139
+ return this;
140
+ }
141
+ unique(name, config) {
142
+ this.config.isUnique = true;
143
+ this.config.uniqueName = name;
144
+ this.config.uniqueType = config?.nulls;
145
+ return this;
146
+ }
147
+ /** @internal */
148
+ buildForeignKeys(_column, _table) {
149
+ return [];
150
+ }
151
+ /** @internal */
152
+ buildExtraConfigColumn(table) {
153
+ return new DatabendExtraConfigColumn(table, this.config);
154
+ }
155
+ };
156
+ var DatabendColumn = class extends Column {
157
+ static [entityKind] = "DatabendColumn";
158
+ constructor(table, config) {
159
+ super(table, config);
160
+ }
161
+ getSQLType() {
162
+ return "";
163
+ }
164
+ };
165
+ var DatabendExtraConfigColumn = class extends DatabendColumn {
166
+ static [entityKind] = "DatabendExtraConfigColumn";
167
+ getSQLType() {
168
+ return this.getSQLType();
169
+ }
170
+ indexConfig = {
171
+ order: this.config.order ?? "asc",
172
+ nulls: this.config.nulls ?? "last",
173
+ opClass: this.config.opClass
174
+ };
175
+ defaultConfig = {
176
+ order: "asc",
177
+ nulls: "last",
178
+ opClass: void 0
179
+ };
180
+ asc() {
181
+ this.indexConfig.order = "asc";
182
+ return this;
183
+ }
184
+ desc() {
185
+ this.indexConfig.order = "desc";
186
+ return this;
187
+ }
188
+ nullsFirst() {
189
+ this.indexConfig.nulls = "first";
190
+ return this;
191
+ }
192
+ nullsLast() {
193
+ this.indexConfig.nulls = "last";
194
+ return this;
195
+ }
196
+ };
197
+ var IndexedColumn = class {
198
+ static [entityKind] = "IndexedColumn";
199
+ name;
200
+ keyAsName;
201
+ type;
202
+ indexConfig;
203
+ constructor(name, keyAsName, type, indexConfig) {
204
+ this.name = name;
205
+ this.keyAsName = keyAsName;
206
+ this.type = type;
207
+ this.indexConfig = indexConfig;
208
+ }
209
+ };
210
+
211
+ // src/databend-core/columns/custom.ts
212
+ var DatabendCustomColumnBuilder = class extends DatabendColumnBuilder {
213
+ static [entityKind2] = "DatabendCustomColumnBuilder";
214
+ sqlDataType;
215
+ mapTo;
216
+ mapFrom;
217
+ constructor(name, fieldConfig, customTypeParams) {
218
+ super(name, "custom", "DatabendCustomColumn");
219
+ this.sqlDataType = customTypeParams.dataType(fieldConfig);
220
+ this.mapTo = customTypeParams.toDriver;
221
+ this.mapFrom = customTypeParams.fromDriver;
222
+ }
223
+ /** @internal */
224
+ build(table) {
225
+ return new DatabendCustomColumn(
226
+ table,
227
+ this.config,
228
+ this.sqlDataType,
229
+ this.mapTo,
230
+ this.mapFrom
231
+ );
232
+ }
233
+ };
234
+ var DatabendCustomColumn = class extends DatabendColumn {
235
+ static [entityKind2] = "DatabendCustomColumn";
236
+ sqlDataType;
237
+ mapTo;
238
+ mapFrom;
239
+ constructor(table, config, sqlDataType, mapTo, mapFrom) {
240
+ super(table, config);
241
+ this.sqlDataType = sqlDataType;
242
+ this.mapTo = mapTo;
243
+ this.mapFrom = mapFrom;
244
+ }
245
+ getSQLType() {
246
+ return this.sqlDataType;
247
+ }
248
+ mapFromDriverValue(value) {
249
+ return this.mapFrom ? this.mapFrom(value) : value;
250
+ }
251
+ mapToDriverValue(value) {
252
+ return this.mapTo ? this.mapTo(value) : value;
253
+ }
254
+ };
255
+ function customType(customTypeParams) {
256
+ return (dbName, fieldConfig) => {
257
+ return new DatabendCustomColumnBuilder(dbName, fieldConfig, customTypeParams);
258
+ };
259
+ }
260
+
128
261
  // src/columns.ts
129
- import { customType } from "drizzle-orm/pg-core";
130
262
  var databendVariant = (name) => customType({
131
263
  dataType() {
132
264
  return "VARIANT";
@@ -221,118 +353,2331 @@ var databendTimestamp = (name) => customType({
221
353
  const normalized = hasOffset ? str.replace(" ", "T") : `${str.replace(" ", "T")}Z`;
222
354
  return new Date(normalized);
223
355
  }
224
- })(name);
225
- var databendDate = (name) => customType({
226
- dataType() {
227
- return "DATE";
228
- },
229
- toDriver(value) {
230
- if (value instanceof Date) {
231
- return value.toISOString().slice(0, 10);
232
- }
233
- return value;
234
- },
235
- fromDriver(value) {
236
- if (value instanceof Date) {
237
- return value.toISOString().slice(0, 10);
356
+ })(name);
357
+ var databendDate = (name) => customType({
358
+ dataType() {
359
+ return "DATE";
360
+ },
361
+ toDriver(value) {
362
+ if (value instanceof Date) {
363
+ return value.toISOString().slice(0, 10);
364
+ }
365
+ return value;
366
+ },
367
+ fromDriver(value) {
368
+ if (value instanceof Date) {
369
+ return value.toISOString().slice(0, 10);
370
+ }
371
+ return value.slice(0, 10);
372
+ }
373
+ })(name);
374
+
375
+ // src/databend-core/alias.ts
376
+ import { TableAliasProxyHandler } from "drizzle-orm/alias";
377
+ function alias(table, alias2) {
378
+ return new Proxy(table, new TableAliasProxyHandler(alias2, false));
379
+ }
380
+
381
+ // src/databend-core/columns/bigint.ts
382
+ import { entityKind as entityKind3 } from "drizzle-orm/entity";
383
+ var DatabendBigInt53Builder = class extends DatabendColumnBuilder {
384
+ static [entityKind3] = "DatabendBigInt53Builder";
385
+ constructor(name) {
386
+ super(name, "number", "DatabendBigInt53");
387
+ }
388
+ /** @internal */
389
+ build(table) {
390
+ return new DatabendBigInt53(table, this.config);
391
+ }
392
+ };
393
+ var DatabendBigInt53 = class extends DatabendColumn {
394
+ static [entityKind3] = "DatabendBigInt53";
395
+ getSQLType() {
396
+ return "bigint";
397
+ }
398
+ };
399
+ var DatabendBigInt64Builder = class extends DatabendColumnBuilder {
400
+ static [entityKind3] = "DatabendBigInt64Builder";
401
+ constructor(name) {
402
+ super(name, "bigint", "DatabendBigInt64");
403
+ }
404
+ /** @internal */
405
+ build(table) {
406
+ return new DatabendBigInt64(table, this.config);
407
+ }
408
+ };
409
+ var DatabendBigInt64 = class extends DatabendColumn {
410
+ static [entityKind3] = "DatabendBigInt64";
411
+ getSQLType() {
412
+ return "bigint";
413
+ }
414
+ mapFromDriverValue(value) {
415
+ return BigInt(value);
416
+ }
417
+ };
418
+ function bigint(name) {
419
+ return new DatabendBigInt53Builder(name ?? "");
420
+ }
421
+
422
+ // src/databend-core/columns/binary.ts
423
+ import { entityKind as entityKind4 } from "drizzle-orm/entity";
424
+ var DatabendBinaryBuilder = class extends DatabendColumnBuilder {
425
+ static [entityKind4] = "DatabendBinaryBuilder";
426
+ constructor(name) {
427
+ super(name, "string", "DatabendBinary");
428
+ }
429
+ /** @internal */
430
+ build(table) {
431
+ return new DatabendBinary(table, this.config);
432
+ }
433
+ };
434
+ var DatabendBinary = class extends DatabendColumn {
435
+ static [entityKind4] = "DatabendBinary";
436
+ getSQLType() {
437
+ return "binary";
438
+ }
439
+ };
440
+ function binary(name) {
441
+ return new DatabendBinaryBuilder(name ?? "");
442
+ }
443
+
444
+ // src/databend-core/columns/bitmap.ts
445
+ import { entityKind as entityKind5 } from "drizzle-orm/entity";
446
+ var DatabendBitmapBuilder = class extends DatabendColumnBuilder {
447
+ static [entityKind5] = "DatabendBitmapBuilder";
448
+ constructor(name) {
449
+ super(name, "string", "DatabendBitmap");
450
+ }
451
+ /** @internal */
452
+ build(table) {
453
+ return new DatabendBitmap(table, this.config);
454
+ }
455
+ };
456
+ var DatabendBitmap = class extends DatabendColumn {
457
+ static [entityKind5] = "DatabendBitmap";
458
+ getSQLType() {
459
+ return "bitmap";
460
+ }
461
+ };
462
+ function bitmap(name) {
463
+ return new DatabendBitmapBuilder(name ?? "");
464
+ }
465
+
466
+ // src/databend-core/columns/boolean.ts
467
+ import { entityKind as entityKind6 } from "drizzle-orm/entity";
468
+ var DatabendBooleanBuilder = class extends DatabendColumnBuilder {
469
+ static [entityKind6] = "DatabendBooleanBuilder";
470
+ constructor(name) {
471
+ super(name, "boolean", "DatabendBoolean");
472
+ }
473
+ /** @internal */
474
+ build(table) {
475
+ return new DatabendBoolean(table, this.config);
476
+ }
477
+ };
478
+ var DatabendBoolean = class extends DatabendColumn {
479
+ static [entityKind6] = "DatabendBoolean";
480
+ getSQLType() {
481
+ return "boolean";
482
+ }
483
+ };
484
+ function boolean(name) {
485
+ return new DatabendBooleanBuilder(name ?? "");
486
+ }
487
+
488
+ // src/databend-core/columns/date.ts
489
+ import { entityKind as entityKind7 } from "drizzle-orm/entity";
490
+ import { sql } from "drizzle-orm/sql/sql";
491
+ var DatabendDateBuilder = class extends DatabendColumnBuilder {
492
+ static [entityKind7] = "DatabendDateBuilder";
493
+ constructor(name) {
494
+ super(name, "string", "DatabendDate");
495
+ }
496
+ defaultNow() {
497
+ return this.default(sql`now()`);
498
+ }
499
+ /** @internal */
500
+ build(table) {
501
+ return new DatabendDate(table, this.config);
502
+ }
503
+ };
504
+ var DatabendDate = class extends DatabendColumn {
505
+ static [entityKind7] = "DatabendDate";
506
+ getSQLType() {
507
+ return "date";
508
+ }
509
+ mapFromDriverValue(value) {
510
+ if (value instanceof Date) {
511
+ return value.toISOString().slice(0, 10);
512
+ }
513
+ if (typeof value === "string") {
514
+ return value.slice(0, 10);
515
+ }
516
+ return value;
517
+ }
518
+ mapToDriverValue(value) {
519
+ if (value instanceof Date) {
520
+ return value.toISOString().slice(0, 10);
521
+ }
522
+ return value;
523
+ }
524
+ };
525
+ function date(name) {
526
+ return new DatabendDateBuilder(name ?? "");
527
+ }
528
+
529
+ // src/databend-core/columns/decimal.ts
530
+ import { entityKind as entityKind8 } from "drizzle-orm/entity";
531
+ var DatabendDecimalBuilder = class extends DatabendColumnBuilder {
532
+ static [entityKind8] = "DatabendDecimalBuilder";
533
+ constructor(name, precision, scale) {
534
+ super(name, "string", "DatabendDecimal");
535
+ this.config.precision = precision;
536
+ this.config.scale = scale;
537
+ }
538
+ /** @internal */
539
+ build(table) {
540
+ return new DatabendDecimal(table, this.config);
541
+ }
542
+ };
543
+ var DatabendDecimal = class extends DatabendColumn {
544
+ static [entityKind8] = "DatabendDecimal";
545
+ precision;
546
+ scale;
547
+ constructor(table, config) {
548
+ super(table, config);
549
+ this.precision = config.precision;
550
+ this.scale = config.scale;
551
+ }
552
+ getSQLType() {
553
+ if (this.precision !== void 0 && this.scale !== void 0) {
554
+ return `decimal(${this.precision}, ${this.scale})`;
555
+ }
556
+ if (this.precision !== void 0) {
557
+ return `decimal(${this.precision})`;
558
+ }
559
+ return "decimal";
560
+ }
561
+ };
562
+ function decimal(name, config) {
563
+ return new DatabendDecimalBuilder(name ?? "", config?.precision, config?.scale);
564
+ }
565
+
566
+ // src/databend-core/columns/double.ts
567
+ import { entityKind as entityKind9 } from "drizzle-orm/entity";
568
+ var DatabendDoublePrecisionBuilder = class extends DatabendColumnBuilder {
569
+ static [entityKind9] = "DatabendDoublePrecisionBuilder";
570
+ constructor(name) {
571
+ super(name, "number", "DatabendDoublePrecision");
572
+ }
573
+ /** @internal */
574
+ build(table) {
575
+ return new DatabendDoublePrecision(table, this.config);
576
+ }
577
+ };
578
+ var DatabendDoublePrecision = class extends DatabendColumn {
579
+ static [entityKind9] = "DatabendDoublePrecision";
580
+ getSQLType() {
581
+ return "double";
582
+ }
583
+ mapFromDriverValue(value) {
584
+ if (typeof value === "string") {
585
+ return Number.parseFloat(value);
586
+ }
587
+ return value;
588
+ }
589
+ };
590
+ function doublePrecision(name) {
591
+ return new DatabendDoublePrecisionBuilder(name ?? "");
592
+ }
593
+
594
+ // src/databend-core/columns/float.ts
595
+ import { entityKind as entityKind10 } from "drizzle-orm/entity";
596
+ var DatabendRealBuilder = class extends DatabendColumnBuilder {
597
+ static [entityKind10] = "DatabendRealBuilder";
598
+ constructor(name) {
599
+ super(name, "number", "DatabendReal");
600
+ }
601
+ /** @internal */
602
+ build(table) {
603
+ return new DatabendReal(table, this.config);
604
+ }
605
+ };
606
+ var DatabendReal = class extends DatabendColumn {
607
+ static [entityKind10] = "DatabendReal";
608
+ getSQLType() {
609
+ return "real";
610
+ }
611
+ };
612
+ function real(name) {
613
+ return new DatabendRealBuilder(name ?? "");
614
+ }
615
+ function float(name) {
616
+ return new DatabendRealBuilder(name ?? "");
617
+ }
618
+
619
+ // src/databend-core/columns/integer.ts
620
+ import { entityKind as entityKind11 } from "drizzle-orm/entity";
621
+ var DatabendIntegerBuilder = class extends DatabendColumnBuilder {
622
+ static [entityKind11] = "DatabendIntegerBuilder";
623
+ constructor(name) {
624
+ super(name, "number", "DatabendInteger");
625
+ }
626
+ /** @internal */
627
+ build(table) {
628
+ return new DatabendInteger(table, this.config);
629
+ }
630
+ };
631
+ var DatabendInteger = class extends DatabendColumn {
632
+ static [entityKind11] = "DatabendInteger";
633
+ getSQLType() {
634
+ return "integer";
635
+ }
636
+ };
637
+ function integer(name) {
638
+ return new DatabendIntegerBuilder(name ?? "");
639
+ }
640
+
641
+ // src/databend-core/columns/smallint.ts
642
+ import { entityKind as entityKind12 } from "drizzle-orm/entity";
643
+ var DatabendSmallIntBuilder = class extends DatabendColumnBuilder {
644
+ static [entityKind12] = "DatabendSmallIntBuilder";
645
+ constructor(name) {
646
+ super(name, "number", "DatabendSmallInt");
647
+ }
648
+ /** @internal */
649
+ build(table) {
650
+ return new DatabendSmallInt(table, this.config);
651
+ }
652
+ };
653
+ var DatabendSmallInt = class extends DatabendColumn {
654
+ static [entityKind12] = "DatabendSmallInt";
655
+ getSQLType() {
656
+ return "smallint";
657
+ }
658
+ };
659
+ function smallint(name) {
660
+ return new DatabendSmallIntBuilder(name ?? "");
661
+ }
662
+
663
+ // src/databend-core/columns/text.ts
664
+ import { entityKind as entityKind13 } from "drizzle-orm/entity";
665
+ var DatabendTextBuilder = class extends DatabendColumnBuilder {
666
+ static [entityKind13] = "DatabendTextBuilder";
667
+ constructor(name) {
668
+ super(name, "string", "DatabendText");
669
+ }
670
+ /** @internal */
671
+ build(table) {
672
+ return new DatabendText(table, this.config);
673
+ }
674
+ };
675
+ var DatabendText = class extends DatabendColumn {
676
+ static [entityKind13] = "DatabendText";
677
+ getSQLType() {
678
+ return "text";
679
+ }
680
+ };
681
+ function text(name) {
682
+ return new DatabendTextBuilder(name ?? "");
683
+ }
684
+
685
+ // src/databend-core/columns/timestamp.ts
686
+ import { entityKind as entityKind14 } from "drizzle-orm/entity";
687
+ import { sql as sql2 } from "drizzle-orm/sql/sql";
688
+ var DatabendTimestampBuilder = class extends DatabendColumnBuilder {
689
+ static [entityKind14] = "DatabendTimestampBuilder";
690
+ constructor(name) {
691
+ super(name, "date", "DatabendTimestamp");
692
+ }
693
+ defaultNow() {
694
+ return this.default(sql2`now()`);
695
+ }
696
+ /** @internal */
697
+ build(table) {
698
+ return new DatabendTimestamp(table, this.config);
699
+ }
700
+ };
701
+ var DatabendTimestamp = class extends DatabendColumn {
702
+ static [entityKind14] = "DatabendTimestamp";
703
+ getSQLType() {
704
+ return "timestamp";
705
+ }
706
+ mapFromDriverValue(value) {
707
+ if (value instanceof Date) {
708
+ return value;
709
+ }
710
+ const str = String(value);
711
+ const hasOffset = str.endsWith("Z") || /[+-]\d{2}:?\d{2}$/.test(str);
712
+ const normalized = hasOffset ? str.replace(" ", "T") : `${str.replace(" ", "T")}Z`;
713
+ return new Date(normalized);
714
+ }
715
+ mapToDriverValue(value) {
716
+ if (value instanceof Date) {
717
+ return value.toISOString();
718
+ }
719
+ return value;
720
+ }
721
+ };
722
+ function timestamp(name) {
723
+ return new DatabendTimestampBuilder(name ?? "");
724
+ }
725
+
726
+ // src/databend-core/columns/tinyint.ts
727
+ import { entityKind as entityKind15 } from "drizzle-orm/entity";
728
+ var DatabendTinyIntBuilder = class extends DatabendColumnBuilder {
729
+ static [entityKind15] = "DatabendTinyIntBuilder";
730
+ constructor(name) {
731
+ super(name, "number", "DatabendTinyInt");
732
+ }
733
+ /** @internal */
734
+ build(table) {
735
+ return new DatabendTinyInt(table, this.config);
736
+ }
737
+ };
738
+ var DatabendTinyInt = class extends DatabendColumn {
739
+ static [entityKind15] = "DatabendTinyInt";
740
+ getSQLType() {
741
+ return "tinyint";
742
+ }
743
+ };
744
+ function tinyint(name) {
745
+ return new DatabendTinyIntBuilder(name ?? "");
746
+ }
747
+
748
+ // src/databend-core/columns/varchar.ts
749
+ import { entityKind as entityKind16 } from "drizzle-orm/entity";
750
+ var DatabendVarcharBuilder = class extends DatabendColumnBuilder {
751
+ static [entityKind16] = "DatabendVarcharBuilder";
752
+ constructor(name, length) {
753
+ super(name, "string", "DatabendVarchar");
754
+ this.config.length = length;
755
+ }
756
+ /** @internal */
757
+ build(table) {
758
+ return new DatabendVarchar(table, this.config);
759
+ }
760
+ };
761
+ var DatabendVarchar = class extends DatabendColumn {
762
+ static [entityKind16] = "DatabendVarchar";
763
+ length;
764
+ constructor(table, config) {
765
+ super(table, config);
766
+ this.length = config.length;
767
+ }
768
+ getSQLType() {
769
+ return this.length ? `varchar(${this.length})` : "varchar";
770
+ }
771
+ };
772
+ function varchar(name, config) {
773
+ return new DatabendVarcharBuilder(name ?? "", config?.length);
774
+ }
775
+
776
+ // src/databend-core/columns/variant.ts
777
+ import { entityKind as entityKind17 } from "drizzle-orm/entity";
778
+ var DatabendVariantBuilder = class extends DatabendColumnBuilder {
779
+ static [entityKind17] = "DatabendVariantBuilder";
780
+ constructor(name) {
781
+ super(name, "json", "DatabendVariant");
782
+ }
783
+ /** @internal */
784
+ build(table) {
785
+ return new DatabendVariant(table, this.config);
786
+ }
787
+ };
788
+ var DatabendVariant = class extends DatabendColumn {
789
+ static [entityKind17] = "DatabendVariant";
790
+ getSQLType() {
791
+ return "variant";
792
+ }
793
+ mapFromDriverValue(value) {
794
+ if (typeof value === "string") {
795
+ try {
796
+ return JSON.parse(value);
797
+ } catch {
798
+ return value;
799
+ }
800
+ }
801
+ return value;
802
+ }
803
+ mapToDriverValue(value) {
804
+ if (typeof value === "string") {
805
+ return value;
806
+ }
807
+ return JSON.stringify(value);
808
+ }
809
+ };
810
+ function variant(name) {
811
+ return new DatabendVariantBuilder(name ?? "");
812
+ }
813
+
814
+ // src/databend-core/columns/all.ts
815
+ function getDatabendColumnBuilders() {
816
+ return {
817
+ bigint,
818
+ binary,
819
+ bitmap,
820
+ boolean,
821
+ customType,
822
+ date,
823
+ decimal,
824
+ doublePrecision,
825
+ float,
826
+ integer,
827
+ real,
828
+ smallint,
829
+ text,
830
+ timestamp,
831
+ tinyint,
832
+ varchar,
833
+ variant
834
+ };
835
+ }
836
+
837
+ // src/databend-core/dialect.ts
838
+ import { aliasedTable, aliasedTableColumn, mapColumnsInAliasedSQLToAlias, mapColumnsInSQLToAlias } from "drizzle-orm/alias";
839
+ import { CasingCache } from "drizzle-orm/casing";
840
+ import { Column as Column2 } from "drizzle-orm/column";
841
+ import { entityKind as entityKind20, is } from "drizzle-orm/entity";
842
+ import { DrizzleError } from "drizzle-orm/errors";
843
+ import {
844
+ getOperators,
845
+ getOrderByOperators,
846
+ Many,
847
+ normalizeRelation,
848
+ One
849
+ } from "drizzle-orm/relations";
850
+ import { and, eq, View as View2 } from "drizzle-orm/sql";
851
+ import { Param, SQL, sql as sql3 } from "drizzle-orm/sql/sql";
852
+ import { Subquery } from "drizzle-orm/subquery";
853
+ import { getTableName, getTableUniqueName, Table as Table2 } from "drizzle-orm/table";
854
+ import { orderSelectedFields } from "drizzle-orm/utils";
855
+ import { ViewBaseConfig } from "drizzle-orm/view-common";
856
+
857
+ // src/databend-core/table.ts
858
+ import { entityKind as entityKind18 } from "drizzle-orm/entity";
859
+ import { Table } from "drizzle-orm/table";
860
+ var DatabendTable = class extends Table {
861
+ static [entityKind18] = "DatabendTable";
862
+ /** @internal */
863
+ static Symbol = Object.assign({}, Table.Symbol, {});
864
+ constructor(name, schema, baseName) {
865
+ super(name, schema, baseName);
866
+ this[Table.Symbol.ExtraConfigBuilder] = void 0;
867
+ this[Table.Symbol.ExtraConfigColumns] = {};
868
+ }
869
+ };
870
+ function databendTableWithSchema(name, columns, extraConfig, schema, baseName = name) {
871
+ const rawTable = new DatabendTable(name, schema, baseName);
872
+ const parsedColumns = typeof columns === "function" ? columns(getDatabendColumnBuilders()) : columns;
873
+ const builtColumns = Object.fromEntries(
874
+ Object.entries(parsedColumns).map(([name2, colBuilderBase]) => {
875
+ const colBuilder = colBuilderBase;
876
+ colBuilder.setName(name2);
877
+ const column = colBuilder.build(rawTable);
878
+ return [name2, column];
879
+ })
880
+ );
881
+ const builtColumnsForExtraConfig = Object.fromEntries(
882
+ Object.entries(parsedColumns).map(([name2, colBuilderBase]) => {
883
+ const colBuilder = colBuilderBase;
884
+ colBuilder.setName(name2);
885
+ const column = colBuilder.buildExtraConfigColumn(rawTable);
886
+ return [name2, column];
887
+ })
888
+ );
889
+ const table = Object.assign(rawTable, builtColumns);
890
+ table[Table.Symbol.Columns] = builtColumns;
891
+ table[Table.Symbol.ExtraConfigColumns] = builtColumnsForExtraConfig;
892
+ if (extraConfig) {
893
+ table[DatabendTable.Symbol.ExtraConfigBuilder] = extraConfig;
894
+ }
895
+ return table;
896
+ }
897
+ var databendTable = (name, columns, extraConfig) => {
898
+ return databendTableWithSchema(name, columns, extraConfig, void 0);
899
+ };
900
+ function databendTableCreator(customizeTableName) {
901
+ return (name, columns, extraConfig) => {
902
+ return databendTableWithSchema(customizeTableName(name), columns, extraConfig, void 0, name);
903
+ };
904
+ }
905
+
906
+ // src/databend-core/view-base.ts
907
+ import { entityKind as entityKind19 } from "drizzle-orm/entity";
908
+ import { View } from "drizzle-orm/sql/sql";
909
+ var DatabendViewBase = class extends View {
910
+ static [entityKind19] = "DatabendViewBase";
911
+ };
912
+
913
+ // src/databend-core/dialect.ts
914
+ var DatabendDialect = class {
915
+ static [entityKind20] = "DatabendDialect";
916
+ /** @internal */
917
+ casing;
918
+ constructor(config) {
919
+ this.casing = new CasingCache(config?.casing);
920
+ }
921
+ async migrate(migrations, session, config) {
922
+ const migrationConfig = typeof config === "string" ? { migrationsFolder: config } : config;
923
+ const migrationsSchema = migrationConfig.migrationsSchema ?? "default";
924
+ const migrationsTable = migrationConfig.migrationsTable ?? "__drizzle_migrations";
925
+ const migrationTableCreate = sql3`
926
+ CREATE TABLE IF NOT EXISTS ${sql3.identifier(migrationsSchema)}.${sql3.identifier(
927
+ migrationsTable
928
+ )} (
929
+ id INT NOT NULL,
930
+ hash VARCHAR NOT NULL,
931
+ created_at BIGINT
932
+ )
933
+ `;
934
+ await session.execute(migrationTableCreate);
935
+ const dbMigrations = await session.all(
936
+ sql3`SELECT id, hash, created_at FROM ${sql3.identifier(
937
+ migrationsSchema
938
+ )}.${sql3.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
939
+ );
940
+ const lastDbMigration = dbMigrations[0];
941
+ await session.transaction(async (tx) => {
942
+ for await (const migration of migrations) {
943
+ if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
944
+ for (const stmt of migration.sql) {
945
+ await tx.execute(sql3.raw(stmt));
946
+ }
947
+ await tx.execute(
948
+ sql3`INSERT INTO ${sql3.identifier(
949
+ migrationsSchema
950
+ )}.${sql3.identifier(migrationsTable)} (id, hash, created_at)
951
+ VALUES (
952
+ (SELECT COALESCE(MAX(id), 0) + 1 FROM ${sql3.identifier(
953
+ migrationsSchema
954
+ )}.${sql3.identifier(migrationsTable)}),
955
+ ${migration.hash},
956
+ ${migration.folderMillis}
957
+ )`
958
+ );
959
+ }
960
+ }
961
+ });
962
+ }
963
+ escapeName(name) {
964
+ return `"${name}"`;
965
+ }
966
+ escapeParam(num) {
967
+ return `$${num + 1}`;
968
+ }
969
+ escapeString(str) {
970
+ return `'${str.replace(/'/g, "''")}'`;
971
+ }
972
+ buildWithCTE(queries) {
973
+ if (!queries?.length) return void 0;
974
+ const withSqlChunks = [sql3`with `];
975
+ for (const [i, w] of queries.entries()) {
976
+ withSqlChunks.push(sql3`${sql3.identifier(w._.alias)} as (${w._.sql})`);
977
+ if (i < queries.length - 1) {
978
+ withSqlChunks.push(sql3`, `);
979
+ }
980
+ }
981
+ withSqlChunks.push(sql3` `);
982
+ return sql3.join(withSqlChunks);
983
+ }
984
+ buildDeleteQuery({ table, where, withList }) {
985
+ const withSql = this.buildWithCTE(withList);
986
+ const whereSql = where ? sql3` where ${where}` : void 0;
987
+ return sql3`${withSql}delete from ${table}${whereSql}`;
988
+ }
989
+ buildUpdateSet(table, set) {
990
+ const tableColumns = table[Table2.Symbol.Columns];
991
+ const columnNames = Object.keys(tableColumns).filter(
992
+ (colName) => set[colName] !== void 0 || tableColumns[colName]?.onUpdateFn !== void 0
993
+ );
994
+ const setSize = columnNames.length;
995
+ return sql3.join(
996
+ columnNames.flatMap((colName, i) => {
997
+ const col = tableColumns[colName];
998
+ const value = set[colName] ?? sql3.param(col.onUpdateFn(), col);
999
+ const res = sql3`${sql3.identifier(this.casing.getColumnCasing(col))} = ${value}`;
1000
+ if (i < setSize - 1) {
1001
+ return [res, sql3.raw(", ")];
1002
+ }
1003
+ return [res];
1004
+ })
1005
+ );
1006
+ }
1007
+ buildUpdateQuery({ table, set, where, withList }) {
1008
+ const withSql = this.buildWithCTE(withList);
1009
+ const tableName = table[DatabendTable.Symbol.Name];
1010
+ const tableSchema = table[DatabendTable.Symbol.Schema];
1011
+ const origTableName = table[DatabendTable.Symbol.OriginalName];
1012
+ const alias2 = tableName === origTableName ? void 0 : tableName;
1013
+ const tableSql = sql3`${tableSchema ? sql3`${sql3.identifier(tableSchema)}.` : void 0}${sql3.identifier(origTableName)}${alias2 && sql3` ${sql3.identifier(alias2)}`}`;
1014
+ const setSql = this.buildUpdateSet(table, set);
1015
+ const whereSql = where ? sql3` where ${where}` : void 0;
1016
+ return sql3`${withSql}update ${tableSql} set ${setSql}${whereSql}`;
1017
+ }
1018
+ buildSelection(fields, { isSingleTable = false } = {}) {
1019
+ const columnsLen = fields.length;
1020
+ const chunks = fields.flatMap(({ field }, i) => {
1021
+ const chunk = [];
1022
+ if (is(field, SQL.Aliased) && field.isSelectionField) {
1023
+ chunk.push(sql3.identifier(field.fieldAlias));
1024
+ } else if (is(field, SQL.Aliased) || is(field, SQL)) {
1025
+ const query = is(field, SQL.Aliased) ? field.sql : field;
1026
+ if (isSingleTable) {
1027
+ chunk.push(
1028
+ new SQL(
1029
+ query.queryChunks.map((c) => {
1030
+ if (is(c, DatabendColumn)) {
1031
+ return sql3.identifier(this.casing.getColumnCasing(c));
1032
+ }
1033
+ return c;
1034
+ })
1035
+ )
1036
+ );
1037
+ } else {
1038
+ chunk.push(query);
1039
+ }
1040
+ if (is(field, SQL.Aliased)) {
1041
+ chunk.push(sql3` as ${sql3.identifier(field.fieldAlias)}`);
1042
+ }
1043
+ } else if (is(field, Column2)) {
1044
+ if (isSingleTable) {
1045
+ chunk.push(sql3.identifier(this.casing.getColumnCasing(field)));
1046
+ } else {
1047
+ chunk.push(field);
1048
+ }
1049
+ }
1050
+ if (i < columnsLen - 1) {
1051
+ chunk.push(sql3`, `);
1052
+ }
1053
+ return chunk;
1054
+ });
1055
+ return sql3.join(chunks);
1056
+ }
1057
+ buildJoins(joins) {
1058
+ if (!joins || joins.length === 0) {
1059
+ return void 0;
1060
+ }
1061
+ const joinsArray = [];
1062
+ for (const [index2, joinMeta] of joins.entries()) {
1063
+ if (index2 === 0) {
1064
+ joinsArray.push(sql3` `);
1065
+ }
1066
+ const table = joinMeta.table;
1067
+ const lateralSql = joinMeta.lateral ? sql3` lateral` : void 0;
1068
+ if (is(table, DatabendTable)) {
1069
+ const t = table;
1070
+ const tableName = t[DatabendTable.Symbol.Name];
1071
+ const tableSchema = t[DatabendTable.Symbol.Schema];
1072
+ const origTableName = t[DatabendTable.Symbol.OriginalName];
1073
+ const alias2 = tableName === origTableName ? void 0 : joinMeta.alias;
1074
+ joinsArray.push(
1075
+ sql3`${sql3.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql3`${sql3.identifier(tableSchema)}.` : void 0}${sql3.identifier(origTableName)}${alias2 && sql3` ${sql3.identifier(alias2)}`} on ${joinMeta.on}`
1076
+ );
1077
+ } else if (is(table, View2)) {
1078
+ const viewName = table[ViewBaseConfig].name;
1079
+ const viewSchema = table[ViewBaseConfig].schema;
1080
+ const origViewName = table[ViewBaseConfig].originalName;
1081
+ const alias2 = viewName === origViewName ? void 0 : joinMeta.alias;
1082
+ joinsArray.push(
1083
+ sql3`${sql3.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql3`${sql3.identifier(viewSchema)}.` : void 0}${sql3.identifier(origViewName)}${alias2 && sql3` ${sql3.identifier(alias2)}`} on ${joinMeta.on}`
1084
+ );
1085
+ } else {
1086
+ joinsArray.push(
1087
+ sql3`${sql3.raw(joinMeta.joinType)} join${lateralSql} ${table} on ${joinMeta.on}`
1088
+ );
1089
+ }
1090
+ if (index2 < joins.length - 1) {
1091
+ joinsArray.push(sql3` `);
1092
+ }
1093
+ }
1094
+ return sql3.join(joinsArray);
1095
+ }
1096
+ buildFromTable(table) {
1097
+ if (is(table, Table2)) {
1098
+ const t = table;
1099
+ if (t[Table2.Symbol.OriginalName] !== t[Table2.Symbol.Name]) {
1100
+ let fullName = sql3`${sql3.identifier(t[Table2.Symbol.OriginalName])}`;
1101
+ if (t[Table2.Symbol.Schema]) {
1102
+ fullName = sql3`${sql3.identifier(t[Table2.Symbol.Schema])}.${fullName}`;
1103
+ }
1104
+ return sql3`${fullName} ${sql3.identifier(t[Table2.Symbol.Name])}`;
1105
+ }
1106
+ }
1107
+ return table;
1108
+ }
1109
+ buildSelectQuery({
1110
+ withList,
1111
+ fields,
1112
+ fieldsFlat,
1113
+ where,
1114
+ having,
1115
+ table,
1116
+ joins,
1117
+ orderBy,
1118
+ groupBy,
1119
+ limit,
1120
+ offset,
1121
+ distinct,
1122
+ setOperators
1123
+ }) {
1124
+ const fieldsList = fieldsFlat ?? orderSelectedFields(fields);
1125
+ for (const f of fieldsList) {
1126
+ if (is(f.field, Column2) && getTableName(f.field.table) !== (is(table, Subquery) ? table._.alias : is(table, DatabendViewBase) ? table[ViewBaseConfig].name : is(table, SQL) ? void 0 : getTableName(table)) && !((table2) => joins?.some(
1127
+ ({ alias: alias2 }) => alias2 === (table2[Table2.Symbol.IsAlias] ? getTableName(table2) : table2[Table2.Symbol.BaseName])
1128
+ ))(f.field.table)) {
1129
+ const tableName = getTableName(f.field.table);
1130
+ throw new Error(
1131
+ `Your "${f.path.join("->")}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`
1132
+ );
1133
+ }
1134
+ }
1135
+ const isSingleTable = !joins || joins.length === 0;
1136
+ const withSql = this.buildWithCTE(withList);
1137
+ let distinctSql;
1138
+ if (distinct) {
1139
+ distinctSql = distinct === true ? sql3` distinct` : sql3` distinct on (${sql3.join(distinct.on, sql3`, `)})`;
1140
+ }
1141
+ const selection = this.buildSelection(fieldsList, { isSingleTable });
1142
+ const tableSql = this.buildFromTable(table);
1143
+ const joinsSql = this.buildJoins(joins);
1144
+ const whereSql = where ? sql3` where ${where}` : void 0;
1145
+ const havingSql = having ? sql3` having ${having}` : void 0;
1146
+ let orderBySql;
1147
+ if (orderBy && orderBy.length > 0) {
1148
+ orderBySql = sql3` order by ${sql3.join(orderBy, sql3`, `)}`;
1149
+ }
1150
+ let groupBySql;
1151
+ if (groupBy && groupBy.length > 0) {
1152
+ groupBySql = sql3` group by ${sql3.join(groupBy, sql3`, `)}`;
1153
+ }
1154
+ const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql3` limit ${limit}` : void 0;
1155
+ const offsetSql = offset ? sql3` offset ${offset}` : void 0;
1156
+ const finalQuery = sql3`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}`;
1157
+ if (setOperators.length > 0) {
1158
+ return this.buildSetOperations(finalQuery, setOperators);
1159
+ }
1160
+ return finalQuery;
1161
+ }
1162
+ buildSetOperations(leftSelect, setOperators) {
1163
+ const [setOperator, ...rest] = setOperators;
1164
+ if (!setOperator) {
1165
+ throw new Error("Cannot pass undefined values to any set operator");
1166
+ }
1167
+ if (rest.length === 0) {
1168
+ return this.buildSetOperationQuery({ leftSelect, setOperator });
1169
+ }
1170
+ return this.buildSetOperations(
1171
+ this.buildSetOperationQuery({ leftSelect, setOperator }),
1172
+ rest
1173
+ );
1174
+ }
1175
+ buildSetOperationQuery({
1176
+ leftSelect,
1177
+ setOperator: { type, isAll, rightSelect, limit, orderBy, offset }
1178
+ }) {
1179
+ const leftChunk = sql3`(${leftSelect.getSQL()}) `;
1180
+ const rightChunk = sql3`(${rightSelect.getSQL()})`;
1181
+ let orderBySql;
1182
+ if (orderBy && orderBy.length > 0) {
1183
+ const orderByValues = [];
1184
+ for (const singleOrderBy of orderBy) {
1185
+ if (is(singleOrderBy, DatabendColumn)) {
1186
+ orderByValues.push(sql3.identifier(singleOrderBy.name));
1187
+ } else if (is(singleOrderBy, SQL)) {
1188
+ for (let i = 0; i < singleOrderBy.queryChunks.length; i++) {
1189
+ const chunk = singleOrderBy.queryChunks[i];
1190
+ if (is(chunk, DatabendColumn)) {
1191
+ singleOrderBy.queryChunks[i] = sql3.identifier(chunk.name);
1192
+ }
1193
+ }
1194
+ orderByValues.push(sql3`${singleOrderBy}`);
1195
+ } else {
1196
+ orderByValues.push(sql3`${singleOrderBy}`);
1197
+ }
1198
+ }
1199
+ orderBySql = sql3` order by ${sql3.join(orderByValues, sql3`, `)} `;
1200
+ }
1201
+ const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql3` limit ${limit}` : void 0;
1202
+ const operatorChunk = sql3.raw(`${type} ${isAll ? "all " : ""}`);
1203
+ const offsetSql = offset ? sql3` offset ${offset}` : void 0;
1204
+ return sql3`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
1205
+ }
1206
+ buildInsertQuery({ table, values: valuesOrSelect, withList, select }) {
1207
+ const valuesSqlList = [];
1208
+ const columns = table[Table2.Symbol.Columns];
1209
+ const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
1210
+ const insertOrder = colEntries.map(
1211
+ ([, column]) => sql3.identifier(this.casing.getColumnCasing(column))
1212
+ );
1213
+ if (select) {
1214
+ const select2 = valuesOrSelect;
1215
+ if (is(select2, SQL)) {
1216
+ valuesSqlList.push(select2);
1217
+ } else {
1218
+ valuesSqlList.push(select2.getSQL());
1219
+ }
1220
+ } else {
1221
+ const values = valuesOrSelect;
1222
+ valuesSqlList.push(sql3.raw("values "));
1223
+ for (const [valueIndex, value] of values.entries()) {
1224
+ const valueList = [];
1225
+ for (const [fieldName, col] of colEntries) {
1226
+ const colValue = value[fieldName];
1227
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
1228
+ if (col.defaultFn !== void 0) {
1229
+ const defaultFnResult = col.defaultFn();
1230
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql3.param(defaultFnResult, col);
1231
+ valueList.push(defaultValue);
1232
+ } else if (!col.default && col.onUpdateFn !== void 0) {
1233
+ const onUpdateFnResult = col.onUpdateFn();
1234
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql3.param(onUpdateFnResult, col);
1235
+ valueList.push(newValue);
1236
+ } else {
1237
+ valueList.push(sql3`default`);
1238
+ }
1239
+ } else {
1240
+ valueList.push(colValue);
1241
+ }
1242
+ }
1243
+ valuesSqlList.push(valueList);
1244
+ if (valueIndex < values.length - 1) {
1245
+ valuesSqlList.push(sql3`, `);
1246
+ }
1247
+ }
1248
+ }
1249
+ const withSql = this.buildWithCTE(withList);
1250
+ const valuesSql = sql3.join(valuesSqlList);
1251
+ return sql3`${withSql}insert into ${table} ${insertOrder} ${valuesSql}`;
1252
+ }
1253
+ prepareTyping(encoder) {
1254
+ if (is(encoder, DatabendDecimal) || is(encoder, DatabendColumn)) {
1255
+ const sqlType = encoder.getSQLType?.();
1256
+ if (sqlType) {
1257
+ const lower = sqlType.toLowerCase();
1258
+ if (lower === "integer" || lower === "int" || lower === "smallint" || lower === "tinyint" || lower === "bigint" || lower === "real" || lower === "double" || lower === "float" || lower.startsWith("decimal")) {
1259
+ return "decimal";
1260
+ }
1261
+ if (lower === "timestamp") {
1262
+ return "timestamp";
1263
+ }
1264
+ if (lower === "date") {
1265
+ return "date";
1266
+ }
1267
+ }
1268
+ }
1269
+ if (is(encoder, DatabendTimestamp)) {
1270
+ return "timestamp";
1271
+ }
1272
+ if (is(encoder, DatabendVariant)) {
1273
+ return "none";
1274
+ }
1275
+ return "none";
1276
+ }
1277
+ sqlToQuery(sql22, invokeSource) {
1278
+ return sql22.toQuery({
1279
+ casing: this.casing,
1280
+ escapeName: this.escapeName,
1281
+ escapeParam: this.escapeParam,
1282
+ escapeString: this.escapeString,
1283
+ prepareTyping: this.prepareTyping.bind(this),
1284
+ invokeSource
1285
+ });
1286
+ }
1287
+ buildRelationalQueryWithoutPK({
1288
+ fullSchema,
1289
+ schema,
1290
+ tableNamesMap,
1291
+ table,
1292
+ tableConfig,
1293
+ queryConfig: config,
1294
+ tableAlias,
1295
+ nestedQueryRelation,
1296
+ joinOn
1297
+ }) {
1298
+ let selection = [];
1299
+ let limit, offset, orderBy = [], where;
1300
+ const joins = [];
1301
+ if (config === true) {
1302
+ const selectionEntries = Object.entries(tableConfig.columns);
1303
+ selection = selectionEntries.map(([key, value]) => ({
1304
+ dbKey: value.name,
1305
+ tsKey: key,
1306
+ field: aliasedTableColumn(value, tableAlias),
1307
+ relationTableTsKey: void 0,
1308
+ isJson: false,
1309
+ selection: []
1310
+ }));
1311
+ } else {
1312
+ const aliasedColumns = Object.fromEntries(
1313
+ Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)])
1314
+ );
1315
+ if (config.where) {
1316
+ const whereSql = typeof config.where === "function" ? config.where(aliasedColumns, getOperators()) : config.where;
1317
+ where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
1318
+ }
1319
+ const fieldsSelection = [];
1320
+ let selectedColumns = [];
1321
+ if (config.columns) {
1322
+ let isIncludeMode = false;
1323
+ for (const [field, value] of Object.entries(config.columns)) {
1324
+ if (value === void 0) continue;
1325
+ if (field in tableConfig.columns) {
1326
+ if (!isIncludeMode && value === true) {
1327
+ isIncludeMode = true;
1328
+ }
1329
+ selectedColumns.push(field);
1330
+ }
1331
+ }
1332
+ if (selectedColumns.length > 0) {
1333
+ selectedColumns = isIncludeMode ? selectedColumns.filter((c) => config.columns?.[c] === true) : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
1334
+ }
1335
+ } else {
1336
+ selectedColumns = Object.keys(tableConfig.columns);
1337
+ }
1338
+ for (const field of selectedColumns) {
1339
+ const column = tableConfig.columns[field];
1340
+ fieldsSelection.push({ tsKey: field, value: column });
1341
+ }
1342
+ let selectedRelations = [];
1343
+ if (config.with) {
1344
+ selectedRelations = Object.entries(config.with).filter((entry) => !!entry[1]).map(([tsKey, queryConfig]) => ({
1345
+ tsKey,
1346
+ queryConfig,
1347
+ relation: tableConfig.relations[tsKey]
1348
+ }));
1349
+ }
1350
+ if (config.extras) {
1351
+ const extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql: sql3 }) : config.extras;
1352
+ for (const [tsKey, value] of Object.entries(extras)) {
1353
+ fieldsSelection.push({
1354
+ tsKey,
1355
+ value: mapColumnsInAliasedSQLToAlias(value, tableAlias)
1356
+ });
1357
+ }
1358
+ }
1359
+ for (const { tsKey, value } of fieldsSelection) {
1360
+ selection.push({
1361
+ dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
1362
+ tsKey,
1363
+ field: is(value, Column2) ? aliasedTableColumn(value, tableAlias) : value,
1364
+ relationTableTsKey: void 0,
1365
+ isJson: false,
1366
+ selection: []
1367
+ });
1368
+ }
1369
+ let orderByOrig = typeof config.orderBy === "function" ? config.orderBy(aliasedColumns, getOrderByOperators()) : config.orderBy ?? [];
1370
+ if (!Array.isArray(orderByOrig)) {
1371
+ orderByOrig = [orderByOrig];
1372
+ }
1373
+ orderBy = orderByOrig.map((orderByValue) => {
1374
+ if (is(orderByValue, Column2)) {
1375
+ return aliasedTableColumn(orderByValue, tableAlias);
1376
+ }
1377
+ return mapColumnsInSQLToAlias(orderByValue, tableAlias);
1378
+ });
1379
+ limit = config.limit;
1380
+ offset = config.offset;
1381
+ for (const {
1382
+ tsKey: selectedRelationTsKey,
1383
+ queryConfig: selectedRelationConfigValue,
1384
+ relation
1385
+ } of selectedRelations) {
1386
+ const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
1387
+ const relationTableName = getTableUniqueName(relation.referencedTable);
1388
+ const relationTableTsName = tableNamesMap[relationTableName];
1389
+ const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
1390
+ const joinOn2 = and(
1391
+ ...normalizedRelation.fields.map(
1392
+ (field2, i) => eq(
1393
+ aliasedTableColumn(normalizedRelation.references[i], relationTableAlias),
1394
+ aliasedTableColumn(field2, tableAlias)
1395
+ )
1396
+ )
1397
+ );
1398
+ const builtRelation = this.buildRelationalQueryWithoutPK({
1399
+ fullSchema,
1400
+ schema,
1401
+ tableNamesMap,
1402
+ table: fullSchema[relationTableTsName],
1403
+ tableConfig: schema[relationTableTsName],
1404
+ queryConfig: is(relation, One) ? selectedRelationConfigValue === true ? { limit: 1 } : { ...selectedRelationConfigValue, limit: 1 } : selectedRelationConfigValue,
1405
+ tableAlias: relationTableAlias,
1406
+ joinOn: joinOn2,
1407
+ nestedQueryRelation: relation
1408
+ });
1409
+ const field = sql3`${sql3.identifier(relationTableAlias)}.${sql3.identifier("data")}`.as(selectedRelationTsKey);
1410
+ joins.push({
1411
+ on: sql3`true`,
1412
+ table: new Subquery(builtRelation.sql, {}, relationTableAlias),
1413
+ alias: relationTableAlias,
1414
+ joinType: "left",
1415
+ lateral: true
1416
+ });
1417
+ selection.push({
1418
+ dbKey: selectedRelationTsKey,
1419
+ tsKey: selectedRelationTsKey,
1420
+ field,
1421
+ relationTableTsKey: relationTableTsName,
1422
+ isJson: true,
1423
+ selection: builtRelation.selection
1424
+ });
1425
+ }
1426
+ }
1427
+ if (selection.length === 0) {
1428
+ throw new DrizzleError({ message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")` });
1429
+ }
1430
+ let result;
1431
+ where = and(joinOn, where);
1432
+ if (nestedQueryRelation) {
1433
+ let field = sql3`json_build_array(${sql3.join(
1434
+ selection.map(
1435
+ ({ field: field2, tsKey, isJson }) => isJson ? sql3`${sql3.identifier(`${tableAlias}_${tsKey}`)}.${sql3.identifier("data")}` : is(field2, SQL.Aliased) ? field2.sql : field2
1436
+ ),
1437
+ sql3`, `
1438
+ )})`;
1439
+ if (is(nestedQueryRelation, Many)) {
1440
+ field = sql3`coalesce(json_agg(${field}${orderBy.length > 0 ? sql3` order by ${sql3.join(orderBy, sql3`, `)}` : void 0}), '[]'::json)`;
1441
+ }
1442
+ const nestedSelection = [{
1443
+ dbKey: "data",
1444
+ tsKey: "data",
1445
+ field: field.as("data"),
1446
+ isJson: true,
1447
+ relationTableTsKey: tableConfig.tsName,
1448
+ selection
1449
+ }];
1450
+ const needsSubquery = limit !== void 0 || offset !== void 0 || orderBy.length > 0;
1451
+ if (needsSubquery) {
1452
+ result = this.buildSelectQuery({
1453
+ table: aliasedTable(table, tableAlias),
1454
+ fields: {},
1455
+ fieldsFlat: [{ path: [], field: sql3.raw("*") }],
1456
+ where,
1457
+ limit,
1458
+ offset,
1459
+ orderBy,
1460
+ setOperators: []
1461
+ });
1462
+ where = void 0;
1463
+ limit = void 0;
1464
+ offset = void 0;
1465
+ orderBy = [];
1466
+ } else {
1467
+ result = aliasedTable(table, tableAlias);
1468
+ }
1469
+ result = this.buildSelectQuery({
1470
+ table: is(result, DatabendTable) ? result : new Subquery(result, {}, tableAlias),
1471
+ fields: {},
1472
+ fieldsFlat: nestedSelection.map(({ field: field2 }) => ({
1473
+ path: [],
1474
+ field: is(field2, Column2) ? aliasedTableColumn(field2, tableAlias) : field2
1475
+ })),
1476
+ joins,
1477
+ where,
1478
+ limit,
1479
+ offset,
1480
+ orderBy,
1481
+ setOperators: []
1482
+ });
1483
+ } else {
1484
+ result = this.buildSelectQuery({
1485
+ table: aliasedTable(table, tableAlias),
1486
+ fields: {},
1487
+ fieldsFlat: selection.map(({ field }) => ({
1488
+ path: [],
1489
+ field: is(field, Column2) ? aliasedTableColumn(field, tableAlias) : field
1490
+ })),
1491
+ joins,
1492
+ where,
1493
+ limit,
1494
+ offset,
1495
+ orderBy,
1496
+ setOperators: []
1497
+ });
1498
+ }
1499
+ return {
1500
+ tableTsKey: tableConfig.tsName,
1501
+ sql: result,
1502
+ selection
1503
+ };
1504
+ }
1505
+ };
1506
+
1507
+ // src/databend-core/indexes.ts
1508
+ import { entityKind as entityKind21, is as is2 } from "drizzle-orm/entity";
1509
+ import { SQL as SQL2 } from "drizzle-orm/sql/sql";
1510
+ var IndexBuilderOn = class {
1511
+ constructor(unique, name) {
1512
+ this.unique = unique;
1513
+ this.name = name;
1514
+ }
1515
+ static [entityKind21] = "DatabendIndexBuilderOn";
1516
+ on(...columns) {
1517
+ return new IndexBuilder(
1518
+ columns.map((it) => {
1519
+ if (is2(it, SQL2)) {
1520
+ return it;
1521
+ }
1522
+ const clonedIndexedColumn = new IndexedColumn(it.name, !!it.keyAsName, it.columnType, it.indexConfig);
1523
+ it.indexConfig = JSON.parse(JSON.stringify(it.defaultConfig));
1524
+ return clonedIndexedColumn;
1525
+ }),
1526
+ this.unique,
1527
+ this.name
1528
+ );
1529
+ }
1530
+ };
1531
+ var IndexBuilder = class {
1532
+ static [entityKind21] = "DatabendIndexBuilder";
1533
+ /** @internal */
1534
+ config;
1535
+ constructor(columns, unique, name) {
1536
+ this.config = {
1537
+ name,
1538
+ columns,
1539
+ unique
1540
+ };
1541
+ }
1542
+ where(condition) {
1543
+ this.config.where = condition;
1544
+ return this;
1545
+ }
1546
+ /** @internal */
1547
+ build(table) {
1548
+ return new Index(this.config, table);
1549
+ }
1550
+ };
1551
+ var Index = class {
1552
+ static [entityKind21] = "DatabendIndex";
1553
+ config;
1554
+ constructor(config, table) {
1555
+ this.config = { ...config, table };
1556
+ }
1557
+ };
1558
+ function index(name) {
1559
+ return new IndexBuilderOn(false, name);
1560
+ }
1561
+ function uniqueIndex(name) {
1562
+ return new IndexBuilderOn(true, name);
1563
+ }
1564
+
1565
+ // src/databend-core/primary-keys.ts
1566
+ import { entityKind as entityKind22 } from "drizzle-orm/entity";
1567
+ function primaryKey(...config) {
1568
+ if (config[0].columns) {
1569
+ return new PrimaryKeyBuilder(config[0].columns, config[0].name);
1570
+ }
1571
+ return new PrimaryKeyBuilder(config);
1572
+ }
1573
+ var PrimaryKeyBuilder = class {
1574
+ static [entityKind22] = "DatabendPrimaryKeyBuilder";
1575
+ /** @internal */
1576
+ columns;
1577
+ /** @internal */
1578
+ name;
1579
+ constructor(columns, name) {
1580
+ this.columns = columns;
1581
+ this.name = name;
1582
+ }
1583
+ /** @internal */
1584
+ build(table) {
1585
+ return new PrimaryKey(table, this.columns, this.name);
1586
+ }
1587
+ };
1588
+ var PrimaryKey = class {
1589
+ constructor(table, columns, name) {
1590
+ this.table = table;
1591
+ this.columns = columns;
1592
+ this.name = name;
1593
+ }
1594
+ static [entityKind22] = "DatabendPrimaryKey";
1595
+ columns;
1596
+ name;
1597
+ getName() {
1598
+ return this.name ?? `${this.table[DatabendTable.Symbol.Name]}_${this.columns.map((column) => column.name).join("_")}_pk`;
1599
+ }
1600
+ };
1601
+
1602
+ // src/databend-core/schema.ts
1603
+ import { entityKind as entityKind23, is as is3 } from "drizzle-orm/entity";
1604
+ import { SQL as SQL3, sql as sql4 } from "drizzle-orm/sql/sql";
1605
+ var DatabendSchema = class {
1606
+ constructor(schemaName) {
1607
+ this.schemaName = schemaName;
1608
+ }
1609
+ static [entityKind23] = "DatabendSchema";
1610
+ table = (name, columns, extraConfig) => {
1611
+ return databendTableWithSchema(name, columns, extraConfig, this.schemaName);
1612
+ };
1613
+ getSQL() {
1614
+ return new SQL3([sql4.identifier(this.schemaName)]);
1615
+ }
1616
+ shouldOmitSQLParens() {
1617
+ return true;
1618
+ }
1619
+ };
1620
+ function databendSchema(name) {
1621
+ return new DatabendSchema(name);
1622
+ }
1623
+
1624
+ // src/databend-core/utils.ts
1625
+ import { is as is4 } from "drizzle-orm/entity";
1626
+ import { Table as Table3 } from "drizzle-orm/table";
1627
+ function getTableConfig(table) {
1628
+ const columns = Object.values(table[Table3.Symbol.Columns]);
1629
+ const indexes = [];
1630
+ const primaryKeys = [];
1631
+ const name = table[Table3.Symbol.Name];
1632
+ const schema = table[Table3.Symbol.Schema];
1633
+ const extraConfigBuilder = table[DatabendTable.Symbol.ExtraConfigBuilder];
1634
+ if (extraConfigBuilder !== void 0) {
1635
+ const extraConfig = extraConfigBuilder(table[Table3.Symbol.ExtraConfigColumns]);
1636
+ const extraValues = Array.isArray(extraConfig) ? extraConfig.flat(1) : Object.values(extraConfig);
1637
+ for (const builder of extraValues) {
1638
+ if (is4(builder, IndexBuilder)) {
1639
+ indexes.push(builder.build(table));
1640
+ } else if (is4(builder, PrimaryKeyBuilder)) {
1641
+ primaryKeys.push(builder.build(table));
1642
+ }
1643
+ }
1644
+ }
1645
+ return {
1646
+ columns,
1647
+ indexes,
1648
+ primaryKeys,
1649
+ name,
1650
+ schema
1651
+ };
1652
+ }
1653
+
1654
+ // src/databend-core/view.ts
1655
+ import { entityKind as entityKind26, is as is7 } from "drizzle-orm/entity";
1656
+ import { SelectionProxyHandler as SelectionProxyHandler3 } from "drizzle-orm/selection-proxy";
1657
+ import { getTableColumns as getTableColumns2 } from "drizzle-orm/utils";
1658
+
1659
+ // src/databend-core/query-builders/query-builder.ts
1660
+ import { entityKind as entityKind25, is as is6 } from "drizzle-orm/entity";
1661
+ import { SelectionProxyHandler as SelectionProxyHandler2 } from "drizzle-orm/selection-proxy";
1662
+ import { WithSubquery } from "drizzle-orm/subquery";
1663
+
1664
+ // src/databend-core/query-builders/select.ts
1665
+ import { entityKind as entityKind24, is as is5 } from "drizzle-orm/entity";
1666
+ import { TypedQueryBuilder } from "drizzle-orm/query-builders/query-builder";
1667
+ import { QueryPromise } from "drizzle-orm/query-promise";
1668
+ import { SelectionProxyHandler } from "drizzle-orm/selection-proxy";
1669
+ import { SQL as SQL4, View as View3 } from "drizzle-orm/sql/sql";
1670
+ import { Subquery as Subquery2 } from "drizzle-orm/subquery";
1671
+ import { Table as Table4 } from "drizzle-orm/table";
1672
+ import { tracer } from "drizzle-orm/tracing";
1673
+ import {
1674
+ applyMixins,
1675
+ getTableColumns,
1676
+ getTableLikeName,
1677
+ haveSameKeys,
1678
+ orderSelectedFields as orderSelectedFields2
1679
+ } from "drizzle-orm/utils";
1680
+ import { ViewBaseConfig as ViewBaseConfig2 } from "drizzle-orm/view-common";
1681
+ var DatabendSelectBuilder = class {
1682
+ static [entityKind24] = "DatabendSelectBuilder";
1683
+ fields;
1684
+ session;
1685
+ dialect;
1686
+ withList = [];
1687
+ distinct;
1688
+ constructor(config) {
1689
+ this.fields = config.fields;
1690
+ this.session = config.session;
1691
+ this.dialect = config.dialect;
1692
+ if (config.withList) {
1693
+ this.withList = config.withList;
1694
+ }
1695
+ this.distinct = config.distinct;
1696
+ }
1697
+ from(source) {
1698
+ const isPartialSelect = !!this.fields;
1699
+ let fields;
1700
+ if (this.fields) {
1701
+ fields = this.fields;
1702
+ } else if (is5(source, Subquery2)) {
1703
+ fields = Object.fromEntries(
1704
+ Object.keys(source._.selectedFields).map((key) => [key, source[key]])
1705
+ );
1706
+ } else if (is5(source, DatabendViewBase)) {
1707
+ fields = source[ViewBaseConfig2].selectedFields;
1708
+ } else if (is5(source, SQL4)) {
1709
+ fields = {};
1710
+ } else {
1711
+ fields = getTableColumns(source);
1712
+ }
1713
+ return new DatabendSelectBase({
1714
+ table: source,
1715
+ fields,
1716
+ isPartialSelect,
1717
+ session: this.session,
1718
+ dialect: this.dialect,
1719
+ withList: this.withList,
1720
+ distinct: this.distinct
1721
+ });
1722
+ }
1723
+ };
1724
+ var DatabendSelectQueryBuilderBase = class extends TypedQueryBuilder {
1725
+ static [entityKind24] = "DatabendSelectQueryBuilder";
1726
+ _;
1727
+ config;
1728
+ joinsNotNullableMap;
1729
+ tableName;
1730
+ isPartialSelect;
1731
+ session;
1732
+ dialect;
1733
+ constructor({ table, fields, isPartialSelect, session, dialect, withList, distinct }) {
1734
+ super();
1735
+ this.config = {
1736
+ withList,
1737
+ table,
1738
+ fields: { ...fields },
1739
+ distinct,
1740
+ setOperators: []
1741
+ };
1742
+ this.isPartialSelect = isPartialSelect;
1743
+ this.session = session;
1744
+ this.dialect = dialect;
1745
+ this._ = {
1746
+ selectedFields: fields
1747
+ };
1748
+ this.tableName = getTableLikeName(table);
1749
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
1750
+ }
1751
+ createJoin(joinType) {
1752
+ return (table, on) => {
1753
+ const baseTableName = this.tableName;
1754
+ const tableName = getTableLikeName(table);
1755
+ if (typeof tableName === "string" && this.config.joins?.some((join) => join.alias === tableName)) {
1756
+ throw new Error(`Alias "${tableName}" is already used in this query`);
1757
+ }
1758
+ if (!this.isPartialSelect) {
1759
+ if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === "string") {
1760
+ this.config.fields = {
1761
+ [baseTableName]: this.config.fields
1762
+ };
1763
+ }
1764
+ if (typeof tableName === "string" && !is5(table, SQL4)) {
1765
+ const selection = is5(table, Subquery2) ? table._.selectedFields : is5(table, View3) ? table[ViewBaseConfig2].selectedFields : table[Table4.Symbol.Columns];
1766
+ this.config.fields[tableName] = selection;
1767
+ }
1768
+ }
1769
+ if (typeof on === "function") {
1770
+ on = on(
1771
+ new Proxy(
1772
+ this.config.fields,
1773
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
1774
+ )
1775
+ );
1776
+ }
1777
+ if (!this.config.joins) {
1778
+ this.config.joins = [];
1779
+ }
1780
+ this.config.joins.push({ on, table, joinType, alias: tableName });
1781
+ if (typeof tableName === "string") {
1782
+ switch (joinType) {
1783
+ case "left":
1784
+ this.joinsNotNullableMap[tableName] = false;
1785
+ break;
1786
+ case "right":
1787
+ this.joinsNotNullableMap = Object.fromEntries(
1788
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
1789
+ );
1790
+ this.joinsNotNullableMap[tableName] = true;
1791
+ break;
1792
+ case "inner":
1793
+ this.joinsNotNullableMap[tableName] = true;
1794
+ break;
1795
+ case "full":
1796
+ this.joinsNotNullableMap = Object.fromEntries(
1797
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
1798
+ );
1799
+ this.joinsNotNullableMap[tableName] = false;
1800
+ break;
1801
+ }
1802
+ }
1803
+ return this;
1804
+ };
1805
+ }
1806
+ leftJoin = this.createJoin("left");
1807
+ rightJoin = this.createJoin("right");
1808
+ innerJoin = this.createJoin("inner");
1809
+ fullJoin = this.createJoin("full");
1810
+ createSetOperator(type, isAll) {
1811
+ return (rightSelection) => {
1812
+ const rightSelect = typeof rightSelection === "function" ? rightSelection(getDatabendSetOperators()) : rightSelection;
1813
+ if (!haveSameKeys(this.getSelectedFields(), rightSelect.getSelectedFields())) {
1814
+ throw new Error(
1815
+ "Set operator error (union / intersect / except): selected fields are not the same or are in a different order"
1816
+ );
1817
+ }
1818
+ this.config.setOperators.push({ type, isAll, rightSelect });
1819
+ return this;
1820
+ };
1821
+ }
1822
+ union = this.createSetOperator("union", false);
1823
+ unionAll = this.createSetOperator("union", true);
1824
+ intersect = this.createSetOperator("intersect", false);
1825
+ intersectAll = this.createSetOperator("intersect", true);
1826
+ except = this.createSetOperator("except", false);
1827
+ exceptAll = this.createSetOperator("except", true);
1828
+ /** @internal */
1829
+ addSetOperators(setOperators) {
1830
+ this.config.setOperators.push(...setOperators);
1831
+ return this;
1832
+ }
1833
+ where(where) {
1834
+ if (typeof where === "function") {
1835
+ where = where(
1836
+ new Proxy(
1837
+ this.config.fields,
1838
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
1839
+ )
1840
+ );
1841
+ }
1842
+ this.config.where = where;
1843
+ return this;
1844
+ }
1845
+ having(having) {
1846
+ if (typeof having === "function") {
1847
+ having = having(
1848
+ new Proxy(
1849
+ this.config.fields,
1850
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
1851
+ )
1852
+ );
1853
+ }
1854
+ this.config.having = having;
1855
+ return this;
1856
+ }
1857
+ groupBy(...columns) {
1858
+ if (typeof columns[0] === "function") {
1859
+ const groupBy = columns[0](
1860
+ new Proxy(
1861
+ this.config.fields,
1862
+ new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
1863
+ )
1864
+ );
1865
+ this.config.groupBy = Array.isArray(groupBy) ? groupBy : [groupBy];
1866
+ } else {
1867
+ this.config.groupBy = columns;
1868
+ }
1869
+ return this;
1870
+ }
1871
+ orderBy(...columns) {
1872
+ if (typeof columns[0] === "function") {
1873
+ const orderBy = columns[0](
1874
+ new Proxy(
1875
+ this.config.fields,
1876
+ new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
1877
+ )
1878
+ );
1879
+ const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
1880
+ if (this.config.setOperators.length > 0) {
1881
+ this.config.setOperators.at(-1).orderBy = orderByArray;
1882
+ } else {
1883
+ this.config.orderBy = orderByArray;
1884
+ }
1885
+ } else {
1886
+ const orderByArray = columns;
1887
+ if (this.config.setOperators.length > 0) {
1888
+ this.config.setOperators.at(-1).orderBy = orderByArray;
1889
+ } else {
1890
+ this.config.orderBy = orderByArray;
1891
+ }
1892
+ }
1893
+ return this;
1894
+ }
1895
+ limit(limit) {
1896
+ if (this.config.setOperators.length > 0) {
1897
+ this.config.setOperators.at(-1).limit = limit;
1898
+ } else {
1899
+ this.config.limit = limit;
1900
+ }
1901
+ return this;
1902
+ }
1903
+ offset(offset) {
1904
+ if (this.config.setOperators.length > 0) {
1905
+ this.config.setOperators.at(-1).offset = offset;
1906
+ } else {
1907
+ this.config.offset = offset;
1908
+ }
1909
+ return this;
1910
+ }
1911
+ /** @internal */
1912
+ getSQL() {
1913
+ return this.dialect.buildSelectQuery(this.config);
1914
+ }
1915
+ toSQL() {
1916
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
1917
+ return rest;
1918
+ }
1919
+ as(alias2) {
1920
+ return new Proxy(
1921
+ new Subquery2(this.getSQL(), this.config.fields, alias2),
1922
+ new SelectionProxyHandler({ alias: alias2, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
1923
+ );
1924
+ }
1925
+ /** @internal */
1926
+ getSelectedFields() {
1927
+ return new Proxy(
1928
+ this.config.fields,
1929
+ new SelectionProxyHandler({ alias: this.tableName, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
1930
+ );
1931
+ }
1932
+ $dynamic() {
1933
+ return this;
1934
+ }
1935
+ };
1936
+ var DatabendSelectBase = class extends DatabendSelectQueryBuilderBase {
1937
+ static [entityKind24] = "DatabendSelect";
1938
+ /** @internal */
1939
+ _prepare(name) {
1940
+ const { session, config, dialect, joinsNotNullableMap } = this;
1941
+ if (!session) {
1942
+ throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
1943
+ }
1944
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
1945
+ const fieldsList = orderSelectedFields2(config.fields);
1946
+ const query = session.prepareQuery(dialect.sqlToQuery(this.getSQL()), fieldsList, name, true);
1947
+ query.joinsNotNullableMap = joinsNotNullableMap;
1948
+ return query;
1949
+ });
1950
+ }
1951
+ prepare(name) {
1952
+ return this._prepare(name);
1953
+ }
1954
+ execute = (placeholderValues) => {
1955
+ return tracer.startActiveSpan("drizzle.operation", () => {
1956
+ return this._prepare().execute(placeholderValues);
1957
+ });
1958
+ };
1959
+ };
1960
+ applyMixins(DatabendSelectBase, [QueryPromise]);
1961
+ function createSetOperator(type, isAll) {
1962
+ return (leftSelect, rightSelect, ...restSelects) => {
1963
+ const setOperators = [rightSelect, ...restSelects].map((select) => ({
1964
+ type,
1965
+ isAll,
1966
+ rightSelect: select
1967
+ }));
1968
+ for (const setOperator of setOperators) {
1969
+ if (!haveSameKeys(leftSelect.getSelectedFields(), setOperator.rightSelect.getSelectedFields())) {
1970
+ throw new Error(
1971
+ "Set operator error (union / intersect / except): selected fields are not the same or are in a different order"
1972
+ );
1973
+ }
1974
+ }
1975
+ return leftSelect.addSetOperators(setOperators);
1976
+ };
1977
+ }
1978
+ var getDatabendSetOperators = () => ({
1979
+ union,
1980
+ unionAll,
1981
+ intersect,
1982
+ intersectAll,
1983
+ except,
1984
+ exceptAll
1985
+ });
1986
+ var union = createSetOperator("union", false);
1987
+ var unionAll = createSetOperator("union", true);
1988
+ var intersect = createSetOperator("intersect", false);
1989
+ var intersectAll = createSetOperator("intersect", true);
1990
+ var except = createSetOperator("except", false);
1991
+ var exceptAll = createSetOperator("except", true);
1992
+
1993
+ // src/databend-core/query-builders/query-builder.ts
1994
+ var QueryBuilder = class {
1995
+ static [entityKind25] = "DatabendQueryBuilder";
1996
+ dialect;
1997
+ dialectConfig;
1998
+ constructor(dialect) {
1999
+ this.dialect = is6(dialect, DatabendDialect) ? dialect : void 0;
2000
+ this.dialectConfig = is6(dialect, DatabendDialect) ? void 0 : dialect;
2001
+ }
2002
+ $with(alias2) {
2003
+ const queryBuilder = this;
2004
+ return {
2005
+ as(qb) {
2006
+ if (typeof qb === "function") {
2007
+ qb = qb(queryBuilder);
2008
+ }
2009
+ return new Proxy(
2010
+ new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias2, true),
2011
+ new SelectionProxyHandler2({ alias: alias2, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
2012
+ );
2013
+ }
2014
+ };
2015
+ }
2016
+ with(...queries) {
2017
+ const self = this;
2018
+ function select(fields) {
2019
+ return new DatabendSelectBuilder({
2020
+ fields: fields ?? void 0,
2021
+ session: void 0,
2022
+ dialect: self.getDialect(),
2023
+ withList: queries
2024
+ });
2025
+ }
2026
+ function selectDistinct(fields) {
2027
+ return new DatabendSelectBuilder({
2028
+ fields: fields ?? void 0,
2029
+ session: void 0,
2030
+ dialect: self.getDialect(),
2031
+ distinct: true
2032
+ });
2033
+ }
2034
+ return { select, selectDistinct };
2035
+ }
2036
+ select(fields) {
2037
+ return new DatabendSelectBuilder({
2038
+ fields: fields ?? void 0,
2039
+ session: void 0,
2040
+ dialect: this.getDialect()
2041
+ });
2042
+ }
2043
+ selectDistinct(fields) {
2044
+ return new DatabendSelectBuilder({
2045
+ fields: fields ?? void 0,
2046
+ session: void 0,
2047
+ dialect: this.getDialect(),
2048
+ distinct: true
2049
+ });
2050
+ }
2051
+ getDialect() {
2052
+ if (!this.dialect) {
2053
+ this.dialect = new DatabendDialect(this.dialectConfig);
2054
+ }
2055
+ return this.dialect;
2056
+ }
2057
+ };
2058
+
2059
+ // src/databend-core/view-common.ts
2060
+ var DatabendViewConfig = Symbol.for("drizzle:DatabendViewConfig");
2061
+
2062
+ // src/databend-core/view.ts
2063
+ var DefaultViewBuilderCore = class {
2064
+ constructor(name, schema) {
2065
+ this.name = name;
2066
+ this.schema = schema;
2067
+ }
2068
+ static [entityKind26] = "DatabendDefaultViewBuilderCore";
2069
+ config = {};
2070
+ with(config) {
2071
+ this.config.with = config;
2072
+ return this;
2073
+ }
2074
+ };
2075
+ var ViewBuilder = class extends DefaultViewBuilderCore {
2076
+ static [entityKind26] = "DatabendViewBuilder";
2077
+ as(qb) {
2078
+ if (typeof qb === "function") {
2079
+ qb = qb(new QueryBuilder());
2080
+ }
2081
+ const selectionProxy = new SelectionProxyHandler3({
2082
+ alias: this.name,
2083
+ sqlBehavior: "error",
2084
+ sqlAliasedBehavior: "alias",
2085
+ replaceOriginalName: true
2086
+ });
2087
+ const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
2088
+ return new Proxy(
2089
+ new DatabendView({
2090
+ databendConfig: this.config,
2091
+ config: {
2092
+ name: this.name,
2093
+ schema: this.schema,
2094
+ selectedFields: aliasedSelection,
2095
+ query: qb.getSQL().inlineParams()
2096
+ }
2097
+ }),
2098
+ selectionProxy
2099
+ );
2100
+ }
2101
+ };
2102
+ var ManualViewBuilder = class extends DefaultViewBuilderCore {
2103
+ static [entityKind26] = "DatabendManualViewBuilder";
2104
+ columns;
2105
+ constructor(name, columns, schema) {
2106
+ super(name, schema);
2107
+ this.columns = getTableColumns2(databendTable(name, columns));
2108
+ }
2109
+ existing() {
2110
+ return new Proxy(
2111
+ new DatabendView({
2112
+ databendConfig: void 0,
2113
+ config: {
2114
+ name: this.name,
2115
+ schema: this.schema,
2116
+ selectedFields: this.columns,
2117
+ query: void 0
2118
+ }
2119
+ }),
2120
+ new SelectionProxyHandler3({
2121
+ alias: this.name,
2122
+ sqlBehavior: "error",
2123
+ sqlAliasedBehavior: "alias",
2124
+ replaceOriginalName: true
2125
+ })
2126
+ );
2127
+ }
2128
+ as(query) {
2129
+ return new Proxy(
2130
+ new DatabendView({
2131
+ databendConfig: this.config,
2132
+ config: {
2133
+ name: this.name,
2134
+ schema: this.schema,
2135
+ selectedFields: this.columns,
2136
+ query: query.inlineParams()
2137
+ }
2138
+ }),
2139
+ new SelectionProxyHandler3({
2140
+ alias: this.name,
2141
+ sqlBehavior: "error",
2142
+ sqlAliasedBehavior: "alias",
2143
+ replaceOriginalName: true
2144
+ })
2145
+ );
2146
+ }
2147
+ };
2148
+ var DatabendView = class extends DatabendViewBase {
2149
+ static [entityKind26] = "DatabendView";
2150
+ [DatabendViewConfig];
2151
+ constructor({ databendConfig, config }) {
2152
+ super(config);
2153
+ if (databendConfig) {
2154
+ this[DatabendViewConfig] = {
2155
+ with: databendConfig.with
2156
+ };
238
2157
  }
239
- return value.slice(0, 10);
240
2158
  }
241
- })(name);
2159
+ };
2160
+ function databendViewWithSchema(name, selection, schema) {
2161
+ if (selection) {
2162
+ return new ManualViewBuilder(name, selection, schema);
2163
+ }
2164
+ return new ViewBuilder(name, schema);
2165
+ }
2166
+ function databendView(name, columns) {
2167
+ return databendViewWithSchema(name, columns, void 0);
2168
+ }
242
2169
 
243
2170
  // src/driver.ts
244
2171
  import { Client } from "databend-driver";
245
- import { entityKind as entityKind3 } from "drizzle-orm/entity";
2172
+ import { entityKind as entityKind36 } from "drizzle-orm/entity";
246
2173
  import { DefaultLogger } from "drizzle-orm/logger";
247
- import { PgDatabase } from "drizzle-orm/pg-core/db";
248
2174
  import {
249
2175
  createTableRelationsHelpers,
250
2176
  extractTablesRelationalConfig
251
2177
  } from "drizzle-orm/relations";
252
2178
 
253
- // src/dialect.ts
254
- import {
255
- sql
256
- } from "drizzle-orm";
257
- import { entityKind, is } from "drizzle-orm/entity";
258
- import {
259
- PgBigInt53,
260
- PgBigInt64,
261
- PgDate,
262
- PgDateString,
263
- PgDialect,
264
- PgDoublePrecision,
265
- PgInteger,
266
- PgNumeric,
267
- PgReal,
268
- PgSmallInt,
269
- PgTime,
270
- PgTimestamp,
271
- PgTimestampString,
272
- PgUUID
273
- } from "drizzle-orm/pg-core";
274
- var DatabendDialect = class extends PgDialect {
275
- static [entityKind] = "DatabendPgDialect";
276
- // Databend does not support savepoints
277
- areSavepointsUnsupported() {
278
- return true;
2179
+ // src/databend-core/db.ts
2180
+ import { entityKind as entityKind33 } from "drizzle-orm/entity";
2181
+ import { SelectionProxyHandler as SelectionProxyHandler4 } from "drizzle-orm/selection-proxy";
2182
+ import { sql as sql6 } from "drizzle-orm/sql/sql";
2183
+ import { WithSubquery as WithSubquery2 } from "drizzle-orm/subquery";
2184
+
2185
+ // src/databend-core/query-builders/count.ts
2186
+ import { entityKind as entityKind27 } from "drizzle-orm/entity";
2187
+ import { SQL as SQL5, sql as sql5 } from "drizzle-orm/sql/sql";
2188
+ var DatabendCountBuilder = class _DatabendCountBuilder extends SQL5 {
2189
+ static [entityKind27] = "DatabendCountBuilder";
2190
+ [Symbol.toStringTag] = "DatabendCountBuilder";
2191
+ sql;
2192
+ session;
2193
+ constructor(params) {
2194
+ super(_DatabendCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
2195
+ this.mapWith(Number);
2196
+ this.session = params.session;
2197
+ this.sql = _DatabendCountBuilder.buildCount(params.source, params.filters);
279
2198
  }
280
- async migrate(migrations, session, config) {
281
- const migrationConfig = typeof config === "string" ? { migrationsFolder: config } : config;
282
- const migrationsSchema = migrationConfig.migrationsSchema ?? "default";
283
- const migrationsTable = migrationConfig.migrationsTable ?? "__drizzle_migrations";
284
- const migrationTableCreate = sql`
285
- CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(
286
- migrationsTable
287
- )} (
288
- id INT NOT NULL,
289
- hash VARCHAR NOT NULL,
290
- created_at BIGINT
291
- )
292
- `;
293
- await session.execute(migrationTableCreate);
294
- const dbMigrations = await session.all(
295
- sql`SELECT id, hash, created_at FROM ${sql.identifier(
296
- migrationsSchema
297
- )}.${sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
2199
+ static buildEmbeddedCount(source, filters) {
2200
+ return sql5`(select count(*) from ${source}${sql5.raw(" where ").if(filters)}${filters})`;
2201
+ }
2202
+ static buildCount(source, filters) {
2203
+ return sql5`select count(*) as count from ${source}${sql5.raw(" where ").if(filters)}${filters};`;
2204
+ }
2205
+ // biome-ignore lint/suspicious/noThenProperty: Promise-like interface required for await support
2206
+ then(onfulfilled, onrejected) {
2207
+ return Promise.resolve(this.session.count(this.sql)).then(onfulfilled, onrejected);
2208
+ }
2209
+ catch(onRejected) {
2210
+ return this.then(void 0, onRejected);
2211
+ }
2212
+ finally(onFinally) {
2213
+ return this.then(
2214
+ (value) => {
2215
+ onFinally?.();
2216
+ return value;
2217
+ },
2218
+ (reason) => {
2219
+ onFinally?.();
2220
+ throw reason;
2221
+ }
298
2222
  );
299
- const lastDbMigration = dbMigrations[0];
300
- await session.transaction(async (tx) => {
301
- for await (const migration of migrations) {
302
- if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
303
- for (const stmt of migration.sql) {
304
- await tx.execute(sql.raw(stmt));
305
- }
306
- await tx.execute(
307
- sql`INSERT INTO ${sql.identifier(
308
- migrationsSchema
309
- )}.${sql.identifier(migrationsTable)} (id, hash, created_at)
310
- VALUES (
311
- (SELECT COALESCE(MAX(id), 0) + 1 FROM ${sql.identifier(
312
- migrationsSchema
313
- )}.${sql.identifier(migrationsTable)}),
314
- ${migration.hash},
315
- ${migration.folderMillis}
316
- )`
2223
+ }
2224
+ };
2225
+
2226
+ // src/databend-core/query-builders/delete.ts
2227
+ import { entityKind as entityKind28 } from "drizzle-orm/entity";
2228
+ import { QueryPromise as QueryPromise2 } from "drizzle-orm/query-promise";
2229
+ import { tracer as tracer2 } from "drizzle-orm/tracing";
2230
+ var DatabendDeleteBase = class extends QueryPromise2 {
2231
+ constructor(table, session, dialect, withList) {
2232
+ super();
2233
+ this.session = session;
2234
+ this.dialect = dialect;
2235
+ this.config = { table, withList };
2236
+ }
2237
+ static [entityKind28] = "DatabendDelete";
2238
+ config;
2239
+ where(where) {
2240
+ this.config.where = where;
2241
+ return this;
2242
+ }
2243
+ /** @internal */
2244
+ getSQL() {
2245
+ return this.dialect.buildDeleteQuery(this.config);
2246
+ }
2247
+ toSQL() {
2248
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
2249
+ return rest;
2250
+ }
2251
+ /** @internal */
2252
+ _prepare(name) {
2253
+ return tracer2.startActiveSpan("drizzle.prepareQuery", () => {
2254
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), void 0, name, true);
2255
+ });
2256
+ }
2257
+ prepare(name) {
2258
+ return this._prepare(name);
2259
+ }
2260
+ execute = (placeholderValues) => {
2261
+ return tracer2.startActiveSpan("drizzle.operation", () => {
2262
+ return this._prepare().execute(placeholderValues);
2263
+ });
2264
+ };
2265
+ $dynamic() {
2266
+ return this;
2267
+ }
2268
+ };
2269
+
2270
+ // src/databend-core/query-builders/insert.ts
2271
+ import { entityKind as entityKind29, is as is8 } from "drizzle-orm/entity";
2272
+ import { QueryPromise as QueryPromise3 } from "drizzle-orm/query-promise";
2273
+ import { Param as Param2, SQL as SQL6 } from "drizzle-orm/sql/sql";
2274
+ import { Table as Table5 } from "drizzle-orm/table";
2275
+ import { tracer as tracer3 } from "drizzle-orm/tracing";
2276
+ import { haveSameKeys as haveSameKeys2 } from "drizzle-orm/utils";
2277
+ var DatabendInsertBuilder = class {
2278
+ constructor(table, session, dialect, withList) {
2279
+ this.table = table;
2280
+ this.session = session;
2281
+ this.dialect = dialect;
2282
+ this.withList = withList;
2283
+ }
2284
+ static [entityKind29] = "DatabendInsertBuilder";
2285
+ values(values) {
2286
+ values = Array.isArray(values) ? values : [values];
2287
+ if (values.length === 0) {
2288
+ throw new Error("values() must be called with at least one value");
2289
+ }
2290
+ const mappedValues = values.map((entry) => {
2291
+ const result = {};
2292
+ const cols = this.table[Table5.Symbol.Columns];
2293
+ for (const colKey of Object.keys(entry)) {
2294
+ const colValue = entry[colKey];
2295
+ result[colKey] = is8(colValue, SQL6) ? colValue : new Param2(colValue, cols[colKey]);
2296
+ }
2297
+ return result;
2298
+ });
2299
+ return new DatabendInsertBase(
2300
+ this.table,
2301
+ mappedValues,
2302
+ this.session,
2303
+ this.dialect,
2304
+ this.withList,
2305
+ false
2306
+ );
2307
+ }
2308
+ select(selectQuery) {
2309
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
2310
+ if (!is8(select, SQL6) && !haveSameKeys2(this.table[Table5.Symbol.Columns], select._.selectedFields)) {
2311
+ throw new Error(
2312
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
2313
+ );
2314
+ }
2315
+ return new DatabendInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
2316
+ }
2317
+ };
2318
+ var DatabendInsertBase = class extends QueryPromise3 {
2319
+ constructor(table, values, session, dialect, withList, select) {
2320
+ super();
2321
+ this.session = session;
2322
+ this.dialect = dialect;
2323
+ this.config = { table, values, withList, select };
2324
+ }
2325
+ static [entityKind29] = "DatabendInsert";
2326
+ config;
2327
+ /** @internal */
2328
+ getSQL() {
2329
+ return this.dialect.buildInsertQuery(this.config);
2330
+ }
2331
+ toSQL() {
2332
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
2333
+ return rest;
2334
+ }
2335
+ /** @internal */
2336
+ _prepare(name) {
2337
+ return tracer3.startActiveSpan("drizzle.prepareQuery", () => {
2338
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), void 0, name, true);
2339
+ });
2340
+ }
2341
+ prepare(name) {
2342
+ return this._prepare(name);
2343
+ }
2344
+ execute = (placeholderValues) => {
2345
+ return tracer3.startActiveSpan("drizzle.operation", () => {
2346
+ return this._prepare().execute(placeholderValues);
2347
+ });
2348
+ };
2349
+ $dynamic() {
2350
+ return this;
2351
+ }
2352
+ };
2353
+
2354
+ // src/databend-core/query-builders/update.ts
2355
+ import { entityKind as entityKind30 } from "drizzle-orm/entity";
2356
+ import { QueryPromise as QueryPromise4 } from "drizzle-orm/query-promise";
2357
+ import "drizzle-orm/sql/sql";
2358
+ import "drizzle-orm/table";
2359
+ import { tracer as tracer4 } from "drizzle-orm/tracing";
2360
+ import { mapUpdateSet } from "drizzle-orm/utils";
2361
+ var DatabendUpdateBuilder = class {
2362
+ constructor(table, session, dialect, withList) {
2363
+ this.table = table;
2364
+ this.session = session;
2365
+ this.dialect = dialect;
2366
+ this.withList = withList;
2367
+ }
2368
+ static [entityKind30] = "DatabendUpdateBuilder";
2369
+ set(values) {
2370
+ return new DatabendUpdateBase(
2371
+ this.table,
2372
+ mapUpdateSet(this.table, values),
2373
+ this.session,
2374
+ this.dialect,
2375
+ this.withList
2376
+ );
2377
+ }
2378
+ };
2379
+ var DatabendUpdateBase = class extends QueryPromise4 {
2380
+ constructor(table, set, session, dialect, withList) {
2381
+ super();
2382
+ this.session = session;
2383
+ this.dialect = dialect;
2384
+ this.config = { set, table, withList };
2385
+ }
2386
+ static [entityKind30] = "DatabendUpdate";
2387
+ config;
2388
+ where(where) {
2389
+ this.config.where = where;
2390
+ return this;
2391
+ }
2392
+ /** @internal */
2393
+ getSQL() {
2394
+ return this.dialect.buildUpdateQuery(this.config);
2395
+ }
2396
+ toSQL() {
2397
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
2398
+ return rest;
2399
+ }
2400
+ /** @internal */
2401
+ _prepare(name) {
2402
+ return tracer4.startActiveSpan("drizzle.prepareQuery", () => {
2403
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), void 0, name, true);
2404
+ });
2405
+ }
2406
+ prepare(name) {
2407
+ return this._prepare(name);
2408
+ }
2409
+ execute = (placeholderValues) => {
2410
+ return tracer4.startActiveSpan("drizzle.operation", () => {
2411
+ return this._prepare().execute(placeholderValues);
2412
+ });
2413
+ };
2414
+ $dynamic() {
2415
+ return this;
2416
+ }
2417
+ };
2418
+
2419
+ // src/databend-core/query-builders/query.ts
2420
+ import { entityKind as entityKind31 } from "drizzle-orm/entity";
2421
+ import { QueryPromise as QueryPromise5 } from "drizzle-orm/query-promise";
2422
+ import { mapRelationalRow } from "drizzle-orm/relations";
2423
+ import { tracer as tracer5 } from "drizzle-orm/tracing";
2424
+ var RelationalQueryBuilder = class {
2425
+ constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
2426
+ this.fullSchema = fullSchema;
2427
+ this.schema = schema;
2428
+ this.tableNamesMap = tableNamesMap;
2429
+ this.table = table;
2430
+ this.tableConfig = tableConfig;
2431
+ this.dialect = dialect;
2432
+ this.session = session;
2433
+ }
2434
+ static [entityKind31] = "DatabendRelationalQueryBuilder";
2435
+ findMany(config) {
2436
+ return new DatabendRelationalQuery(
2437
+ this.fullSchema,
2438
+ this.schema,
2439
+ this.tableNamesMap,
2440
+ this.table,
2441
+ this.tableConfig,
2442
+ this.dialect,
2443
+ this.session,
2444
+ config ? config : {},
2445
+ "many"
2446
+ );
2447
+ }
2448
+ findFirst(config) {
2449
+ return new DatabendRelationalQuery(
2450
+ this.fullSchema,
2451
+ this.schema,
2452
+ this.tableNamesMap,
2453
+ this.table,
2454
+ this.tableConfig,
2455
+ this.dialect,
2456
+ this.session,
2457
+ config ? { ...config, limit: 1 } : { limit: 1 },
2458
+ "first"
2459
+ );
2460
+ }
2461
+ };
2462
+ var DatabendRelationalQuery = class extends QueryPromise5 {
2463
+ constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
2464
+ super();
2465
+ this.fullSchema = fullSchema;
2466
+ this.schema = schema;
2467
+ this.tableNamesMap = tableNamesMap;
2468
+ this.table = table;
2469
+ this.tableConfig = tableConfig;
2470
+ this.dialect = dialect;
2471
+ this.session = session;
2472
+ this.config = config;
2473
+ this.mode = mode;
2474
+ }
2475
+ static [entityKind31] = "DatabendRelationalQuery";
2476
+ /** @internal */
2477
+ _prepare(name) {
2478
+ return tracer5.startActiveSpan("drizzle.prepareQuery", () => {
2479
+ const { query, builtQuery } = this._toSQL();
2480
+ return this.session.prepareQuery(
2481
+ builtQuery,
2482
+ void 0,
2483
+ name,
2484
+ true,
2485
+ (rawRows, mapColumnValue) => {
2486
+ const rows = rawRows.map(
2487
+ (row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection, mapColumnValue)
317
2488
  );
2489
+ if (this.mode === "first") {
2490
+ return rows[0];
2491
+ }
2492
+ return rows;
318
2493
  }
319
- }
2494
+ );
320
2495
  });
321
2496
  }
322
- prepareTyping(encoder) {
323
- if (is(encoder, PgNumeric) || is(encoder, PgInteger) || is(encoder, PgSmallInt) || is(encoder, PgReal) || is(encoder, PgDoublePrecision) || is(encoder, PgBigInt53) || is(encoder, PgBigInt64)) {
324
- return "decimal";
325
- } else if (is(encoder, PgTime)) {
326
- return "time";
327
- } else if (is(encoder, PgTimestamp) || is(encoder, PgTimestampString)) {
328
- return "timestamp";
329
- } else if (is(encoder, PgDate) || is(encoder, PgDateString)) {
330
- return "date";
331
- } else if (is(encoder, PgUUID)) {
332
- return "uuid";
333
- } else {
334
- return "none";
2497
+ prepare(name) {
2498
+ return this._prepare(name);
2499
+ }
2500
+ _getQuery() {
2501
+ return this.dialect.buildRelationalQueryWithoutPK({
2502
+ fullSchema: this.fullSchema,
2503
+ schema: this.schema,
2504
+ tableNamesMap: this.tableNamesMap,
2505
+ table: this.table,
2506
+ tableConfig: this.tableConfig,
2507
+ queryConfig: this.config,
2508
+ tableAlias: this.tableConfig.tsName
2509
+ });
2510
+ }
2511
+ /** @internal */
2512
+ getSQL() {
2513
+ return this._getQuery().sql;
2514
+ }
2515
+ _toSQL() {
2516
+ const query = this._getQuery();
2517
+ const builtQuery = this.dialect.sqlToQuery(query.sql);
2518
+ return { query, builtQuery };
2519
+ }
2520
+ toSQL() {
2521
+ return this._toSQL().builtQuery;
2522
+ }
2523
+ execute() {
2524
+ return tracer5.startActiveSpan("drizzle.operation", () => {
2525
+ return this._prepare().execute(void 0);
2526
+ });
2527
+ }
2528
+ };
2529
+
2530
+ // src/databend-core/query-builders/raw.ts
2531
+ import { entityKind as entityKind32 } from "drizzle-orm/entity";
2532
+ import { QueryPromise as QueryPromise6 } from "drizzle-orm/query-promise";
2533
+ var DatabendRaw = class extends QueryPromise6 {
2534
+ constructor(execute, sql8, query, mapBatchResult) {
2535
+ super();
2536
+ this.execute = execute;
2537
+ this.sql = sql8;
2538
+ this.query = query;
2539
+ this.mapBatchResult = mapBatchResult;
2540
+ }
2541
+ static [entityKind32] = "DatabendRaw";
2542
+ /** @internal */
2543
+ getSQL() {
2544
+ return this.sql;
2545
+ }
2546
+ getQuery() {
2547
+ return this.query;
2548
+ }
2549
+ mapResult(result, isFromBatch) {
2550
+ return isFromBatch ? this.mapBatchResult(result) : result;
2551
+ }
2552
+ _prepare() {
2553
+ return this;
2554
+ }
2555
+ /** @internal */
2556
+ isResponseInArrayMode() {
2557
+ return false;
2558
+ }
2559
+ };
2560
+
2561
+ // src/databend-core/db.ts
2562
+ var DatabendDatabase = class {
2563
+ constructor(dialect, session, schema) {
2564
+ this.dialect = dialect;
2565
+ this.session = session;
2566
+ this._ = schema ? {
2567
+ schema: schema.schema,
2568
+ fullSchema: schema.fullSchema,
2569
+ tableNamesMap: schema.tableNamesMap,
2570
+ session
2571
+ } : {
2572
+ schema: void 0,
2573
+ fullSchema: {},
2574
+ tableNamesMap: {},
2575
+ session
2576
+ };
2577
+ this.query = {};
2578
+ if (this._.schema) {
2579
+ for (const [tableName, columns] of Object.entries(this._.schema)) {
2580
+ this.query[tableName] = new RelationalQueryBuilder(
2581
+ schema.fullSchema,
2582
+ this._.schema,
2583
+ this._.tableNamesMap,
2584
+ schema.fullSchema[tableName],
2585
+ columns,
2586
+ dialect,
2587
+ session
2588
+ );
2589
+ }
2590
+ }
2591
+ }
2592
+ static [entityKind33] = "DatabendDatabase";
2593
+ _;
2594
+ query;
2595
+ $with(alias2) {
2596
+ const self = this;
2597
+ return {
2598
+ as(qb) {
2599
+ if (typeof qb === "function") {
2600
+ qb = qb(new QueryBuilder(self.dialect));
2601
+ }
2602
+ const sqlObj = typeof qb.getSQL === "function" ? qb.getSQL() : qb;
2603
+ const fields = typeof qb.getSelectedFields === "function" ? qb.getSelectedFields() : {};
2604
+ return new Proxy(
2605
+ new WithSubquery2(sqlObj, fields, alias2, true),
2606
+ new SelectionProxyHandler4({ alias: alias2, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
2607
+ );
2608
+ }
2609
+ };
2610
+ }
2611
+ $count(source, filters) {
2612
+ return new DatabendCountBuilder({ source, filters, session: this.session });
2613
+ }
2614
+ with(...queries) {
2615
+ const self = this;
2616
+ function select(fields) {
2617
+ return new DatabendSelectBuilder({
2618
+ fields: fields ?? void 0,
2619
+ session: self.session,
2620
+ dialect: self.dialect,
2621
+ withList: queries
2622
+ });
335
2623
  }
2624
+ function selectDistinct(fields) {
2625
+ return new DatabendSelectBuilder({
2626
+ fields: fields ?? void 0,
2627
+ session: self.session,
2628
+ dialect: self.dialect,
2629
+ withList: queries,
2630
+ distinct: true
2631
+ });
2632
+ }
2633
+ function update(table) {
2634
+ return new DatabendUpdateBuilder(table, self.session, self.dialect, queries);
2635
+ }
2636
+ function insert(table) {
2637
+ return new DatabendInsertBuilder(table, self.session, self.dialect, queries);
2638
+ }
2639
+ function delete_(table) {
2640
+ return new DatabendDeleteBase(table, self.session, self.dialect, queries);
2641
+ }
2642
+ return { select, selectDistinct, update, insert, delete: delete_ };
2643
+ }
2644
+ select(fields) {
2645
+ return new DatabendSelectBuilder({
2646
+ fields: fields ?? void 0,
2647
+ session: this.session,
2648
+ dialect: this.dialect
2649
+ });
2650
+ }
2651
+ selectDistinct(fields) {
2652
+ return new DatabendSelectBuilder({
2653
+ fields: fields ?? void 0,
2654
+ session: this.session,
2655
+ dialect: this.dialect,
2656
+ distinct: true
2657
+ });
2658
+ }
2659
+ update(table) {
2660
+ return new DatabendUpdateBuilder(table, this.session, this.dialect);
2661
+ }
2662
+ insert(table) {
2663
+ return new DatabendInsertBuilder(table, this.session, this.dialect);
2664
+ }
2665
+ delete(table) {
2666
+ return new DatabendDeleteBase(table, this.session, this.dialect);
2667
+ }
2668
+ execute(query) {
2669
+ const sequel = typeof query === "string" ? sql6.raw(query) : query.getSQL();
2670
+ const builtQuery = this.dialect.sqlToQuery(sequel);
2671
+ const prepared = this.session.prepareQuery(builtQuery, void 0, void 0, false);
2672
+ return new DatabendRaw(
2673
+ () => prepared.execute(void 0),
2674
+ sequel,
2675
+ builtQuery,
2676
+ (result) => prepared.mapResult(result, true)
2677
+ );
2678
+ }
2679
+ transaction(transaction) {
2680
+ return this.session.transaction(transaction);
336
2681
  }
337
2682
  };
338
2683
 
@@ -501,54 +2846,97 @@ function createDatabendConnectionPool(client, options = {}) {
501
2846
  }
502
2847
 
503
2848
  // src/session.ts
504
- import { entityKind as entityKind2 } from "drizzle-orm/entity";
505
- import { TransactionRollbackError } from "drizzle-orm/errors";
2849
+ import { entityKind as entityKind35 } from "drizzle-orm/entity";
2850
+ import { TransactionRollbackError as TransactionRollbackError2 } from "drizzle-orm/errors";
506
2851
  import { NoopLogger } from "drizzle-orm/logger";
507
- import { PgTransaction } from "drizzle-orm/pg-core";
508
- import { PgPreparedQuery, PgSession } from "drizzle-orm/pg-core/session";
509
- import { fillPlaceholders, sql as sql2 } from "drizzle-orm/sql/sql";
2852
+ import { fillPlaceholders, sql as sql7 } from "drizzle-orm/sql/sql";
2853
+
2854
+ // src/databend-core/session.ts
2855
+ import { entityKind as entityKind34 } from "drizzle-orm/entity";
2856
+ import { TransactionRollbackError } from "drizzle-orm/errors";
2857
+ import { tracer as tracer6 } from "drizzle-orm/tracing";
2858
+ var DatabendPreparedQuery = class {
2859
+ constructor(query) {
2860
+ this.query = query;
2861
+ }
2862
+ static [entityKind34] = "DatabendPreparedQuery";
2863
+ /** @internal */
2864
+ joinsNotNullableMap;
2865
+ getQuery() {
2866
+ return this.query;
2867
+ }
2868
+ mapResult(response, _isFromBatch) {
2869
+ return response;
2870
+ }
2871
+ };
2872
+ var DatabendSession = class {
2873
+ constructor(dialect) {
2874
+ this.dialect = dialect;
2875
+ }
2876
+ static [entityKind34] = "DatabendSession";
2877
+ execute(query) {
2878
+ return tracer6.startActiveSpan("drizzle.operation", () => {
2879
+ const prepared = tracer6.startActiveSpan("drizzle.prepareQuery", () => {
2880
+ return this.prepareQuery(
2881
+ this.dialect.sqlToQuery(query),
2882
+ void 0,
2883
+ void 0,
2884
+ false
2885
+ );
2886
+ });
2887
+ return prepared.execute(void 0);
2888
+ });
2889
+ }
2890
+ all(query) {
2891
+ return this.prepareQuery(
2892
+ this.dialect.sqlToQuery(query),
2893
+ void 0,
2894
+ void 0,
2895
+ false
2896
+ ).all();
2897
+ }
2898
+ async count(sql8) {
2899
+ const res = await this.execute(sql8);
2900
+ return Number(res[0]["count"]);
2901
+ }
2902
+ prepareQuery(_query, _fields, _name, _isResponseInArrayMode, _customResultMapper) {
2903
+ throw new Error("prepareQuery not implemented");
2904
+ }
2905
+ transaction(_transaction, _config) {
2906
+ throw new Error("transaction not implemented");
2907
+ }
2908
+ };
2909
+ var DatabendTransaction = class extends DatabendDatabase {
2910
+ constructor(dialect, session, schema, nestedIndex = 0) {
2911
+ super(dialect, session, schema);
2912
+ this.nestedIndex = nestedIndex;
2913
+ this.schema = schema;
2914
+ }
2915
+ static [entityKind34] = "DatabendTransaction";
2916
+ rollback() {
2917
+ throw new TransactionRollbackError();
2918
+ }
2919
+ };
510
2920
 
511
2921
  // src/sql/result-mapper.ts
512
2922
  import {
513
- Column,
514
- getTableName,
515
- is as is2,
516
- SQL
2923
+ Column as Column3,
2924
+ getTableName as getTableName2,
2925
+ is as is10,
2926
+ SQL as SQL8
517
2927
  } from "drizzle-orm";
518
- import {
519
- PgCustomColumn,
520
- PgDate as PgDate2,
521
- PgDateString as PgDateString2,
522
- PgTime as PgTime2,
523
- PgTimestamp as PgTimestamp2,
524
- PgTimestampString as PgTimestampString2
525
- } from "drizzle-orm/pg-core";
526
2928
  function toDecoderInput(decoder, value) {
527
2929
  void decoder;
528
2930
  return value;
529
2931
  }
530
- function normalizeTimestampString(value, withTimezone) {
531
- if (value instanceof Date) {
532
- const iso = value.toISOString().replace("T", " ");
533
- return withTimezone ? iso.replace("Z", "+00") : iso.replace("Z", "");
534
- }
535
- if (typeof value === "string") {
536
- const normalized = value.replace("T", " ");
537
- if (withTimezone) {
538
- return normalized.includes("+") ? normalized : `${normalized}+00`;
539
- }
540
- return normalized.replace(/\+00$/, "");
541
- }
542
- return value;
543
- }
544
- function normalizeTimestamp(value, withTimezone) {
2932
+ function normalizeTimestamp(value, _withTimezone) {
545
2933
  if (value instanceof Date) {
546
2934
  return value;
547
2935
  }
548
2936
  if (typeof value === "string") {
549
2937
  const hasOffset = value.endsWith("Z") || /[+-]\d{2}:?\d{2}$/.test(value.trim());
550
2938
  const spaced = value.replace(" ", "T");
551
- const normalized = withTimezone || hasOffset ? spaced : `${spaced}+00`;
2939
+ const normalized = hasOffset ? spaced : `${spaced}Z`;
552
2940
  return new Date(normalized);
553
2941
  }
554
2942
  return value;
@@ -562,57 +2950,19 @@ function normalizeDateString(value) {
562
2950
  }
563
2951
  return value;
564
2952
  }
565
- function normalizeDateValue(value) {
566
- if (value instanceof Date) {
567
- return value;
568
- }
569
- if (typeof value === "string") {
570
- return /* @__PURE__ */ new Date(`${value.slice(0, 10)}T00:00:00Z`);
571
- }
572
- return value;
573
- }
574
- function normalizeTime(value) {
575
- if (typeof value === "bigint") {
576
- const totalMillis = Number(value) / 1e3;
577
- const date = new Date(totalMillis);
578
- return date.toISOString().split("T")[1].replace("Z", "");
579
- }
580
- if (value instanceof Date) {
581
- return value.toISOString().split("T")[1].replace("Z", "");
582
- }
583
- return value;
584
- }
585
2953
  function mapDriverValue(decoder, rawValue) {
586
- if (is2(decoder, PgTimestampString2)) {
587
- return decoder.mapFromDriverValue(
588
- toDecoderInput(
589
- decoder,
590
- normalizeTimestampString(rawValue, decoder.withTimezone)
591
- )
592
- );
593
- }
594
- if (is2(decoder, PgTimestamp2)) {
595
- const normalized = normalizeTimestamp(rawValue, decoder.withTimezone);
2954
+ if (is10(decoder, DatabendTimestamp)) {
2955
+ const normalized = normalizeTimestamp(rawValue, false);
596
2956
  if (normalized instanceof Date) {
597
2957
  return normalized;
598
2958
  }
599
2959
  return decoder.mapFromDriverValue(toDecoderInput(decoder, normalized));
600
2960
  }
601
- if (is2(decoder, PgDateString2)) {
2961
+ if (is10(decoder, DatabendDate)) {
602
2962
  return decoder.mapFromDriverValue(
603
2963
  toDecoderInput(decoder, normalizeDateString(rawValue))
604
2964
  );
605
2965
  }
606
- if (is2(decoder, PgDate2)) {
607
- return decoder.mapFromDriverValue(
608
- toDecoderInput(decoder, normalizeDateValue(rawValue))
609
- );
610
- }
611
- if (is2(decoder, PgTime2)) {
612
- return decoder.mapFromDriverValue(
613
- toDecoderInput(decoder, normalizeTime(rawValue))
614
- );
615
- }
616
2966
  return decoder.mapFromDriverValue(toDecoderInput(decoder, rawValue));
617
2967
  }
618
2968
  function mapResultRow(columns, row, joinsNotNullableMap) {
@@ -620,13 +2970,13 @@ function mapResultRow(columns, row, joinsNotNullableMap) {
620
2970
  const result = columns.reduce(
621
2971
  (acc, { path, field }, columnIndex) => {
622
2972
  let decoder;
623
- if (is2(field, Column)) {
2973
+ if (is10(field, Column3)) {
624
2974
  decoder = field;
625
- } else if (is2(field, SQL)) {
2975
+ } else if (is10(field, SQL8)) {
626
2976
  decoder = field.decoder;
627
2977
  } else {
628
- const col = field.sql.queryChunks.find((chunk) => is2(chunk, Column));
629
- if (is2(col, PgCustomColumn)) {
2978
+ const col = field.sql.queryChunks.find((chunk) => is10(chunk, Column3));
2979
+ if (is10(col, DatabendCustomColumn)) {
630
2980
  decoder = col;
631
2981
  } else {
632
2982
  decoder = field.sql.decoder;
@@ -643,18 +2993,18 @@ function mapResultRow(columns, row, joinsNotNullableMap) {
643
2993
  }
644
2994
  const rawValue = row[columnIndex];
645
2995
  const value = node[pathChunk] = rawValue === null ? null : mapDriverValue(decoder, rawValue);
646
- if (joinsNotNullableMap && is2(field, Column) && path.length === 2) {
2996
+ if (joinsNotNullableMap && is10(field, Column3) && path.length === 2) {
647
2997
  const objectName = path[0];
648
2998
  if (!(objectName in nullifyMap)) {
649
- nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
650
- } else if (typeof nullifyMap[objectName] === "string" && nullifyMap[objectName] !== getTableName(field.table)) {
2999
+ nullifyMap[objectName] = value === null ? getTableName2(field.table) : false;
3000
+ } else if (typeof nullifyMap[objectName] === "string" && nullifyMap[objectName] !== getTableName2(field.table)) {
651
3001
  nullifyMap[objectName] = false;
652
3002
  }
653
3003
  continue;
654
3004
  }
655
- if (joinsNotNullableMap && is2(field, SQL.Aliased) && path.length === 2) {
656
- const col = field.sql.queryChunks.find((chunk) => is2(chunk, Column));
657
- const tableName = col?.table && getTableName(col?.table);
3005
+ if (joinsNotNullableMap && is10(field, SQL8.Aliased) && path.length === 2) {
3006
+ const col = field.sql.queryChunks.find((chunk) => is10(chunk, Column3));
3007
+ const tableName = col?.table && getTableName2(col?.table);
658
3008
  if (!tableName) {
659
3009
  continue;
660
3010
  }
@@ -683,7 +3033,7 @@ function mapResultRow(columns, row, joinsNotNullableMap) {
683
3033
  }
684
3034
 
685
3035
  // src/session.ts
686
- var DatabendPreparedQuery = class extends PgPreparedQuery {
3036
+ var DatabendPreparedQuery2 = class extends DatabendPreparedQuery {
687
3037
  constructor(client, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, typings) {
688
3038
  super({ sql: queryString, params });
689
3039
  this.client = client;
@@ -695,7 +3045,7 @@ var DatabendPreparedQuery = class extends PgPreparedQuery {
695
3045
  this.customResultMapper = customResultMapper;
696
3046
  this.typings = typings;
697
3047
  }
698
- static [entityKind2] = "DatabendPreparedQuery";
3048
+ static [entityKind35] = "DatabendPreparedQuery";
699
3049
  async execute(placeholderValues = {}) {
700
3050
  const params = fillPlaceholders(this.params, placeholderValues);
701
3051
  this.logger.logQuery(this.queryString, params);
@@ -724,7 +3074,7 @@ var DatabendPreparedQuery = class extends PgPreparedQuery {
724
3074
  return this._isResponseInArrayMode;
725
3075
  }
726
3076
  };
727
- var DatabendSession = class _DatabendSession extends PgSession {
3077
+ var DatabendSession2 = class _DatabendSession extends DatabendSession {
728
3078
  constructor(client, dialect, schema, options = {}) {
729
3079
  super(dialect);
730
3080
  this.client = client;
@@ -733,13 +3083,13 @@ var DatabendSession = class _DatabendSession extends PgSession {
733
3083
  this.dialect = dialect;
734
3084
  this.logger = options.logger ?? new NoopLogger();
735
3085
  }
736
- static [entityKind2] = "DatabendSession";
3086
+ static [entityKind35] = "DatabendSession";
737
3087
  dialect;
738
3088
  logger;
739
3089
  rollbackOnly = false;
740
3090
  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper) {
741
3091
  void name;
742
- return new DatabendPreparedQuery(
3092
+ return new DatabendPreparedQuery2(
743
3093
  this.client,
744
3094
  query.sql,
745
3095
  query.params,
@@ -765,26 +3115,26 @@ var DatabendSession = class _DatabendSession extends PgSession {
765
3115
  this.schema,
766
3116
  this.options
767
3117
  );
768
- const tx = new DatabendTransaction(
3118
+ const tx = new DatabendTransaction2(
769
3119
  this.dialect,
770
3120
  session,
771
3121
  this.schema
772
3122
  );
773
3123
  try {
774
- await tx.execute(sql2`BEGIN`);
3124
+ await tx.execute(sql7`BEGIN`);
775
3125
  if (config) {
776
3126
  await tx.setTransaction(config);
777
3127
  }
778
3128
  try {
779
3129
  const result = await transaction(tx);
780
3130
  if (session.isRollbackOnly()) {
781
- await tx.execute(sql2`ROLLBACK`);
782
- throw new TransactionRollbackError();
3131
+ await tx.execute(sql7`ROLLBACK`);
3132
+ throw new TransactionRollbackError2();
783
3133
  }
784
- await tx.execute(sql2`COMMIT`);
3134
+ await tx.execute(sql7`COMMIT`);
785
3135
  return result;
786
3136
  } catch (error) {
787
- await tx.execute(sql2`ROLLBACK`);
3137
+ await tx.execute(sql7`ROLLBACK`);
788
3138
  throw error;
789
3139
  }
790
3140
  } finally {
@@ -810,10 +3160,10 @@ var VALID_TRANSACTION_ACCESS_MODES = /* @__PURE__ */ new Set([
810
3160
  "read only",
811
3161
  "read write"
812
3162
  ]);
813
- var DatabendTransaction = class _DatabendTransaction extends PgTransaction {
814
- static [entityKind2] = "DatabendTransaction";
3163
+ var DatabendTransaction2 = class _DatabendTransaction extends DatabendTransaction {
3164
+ static [entityKind35] = "DatabendTransaction";
815
3165
  rollback() {
816
- throw new TransactionRollbackError();
3166
+ throw new TransactionRollbackError2();
817
3167
  }
818
3168
  getTransactionConfigSQL(config) {
819
3169
  if (config.isolationLevel && !VALID_TRANSACTION_ISOLATION_LEVELS.has(config.isolationLevel)) {
@@ -837,11 +3187,11 @@ var DatabendTransaction = class _DatabendTransaction extends PgTransaction {
837
3187
  if (config.accessMode) {
838
3188
  chunks.push(config.accessMode);
839
3189
  }
840
- return sql2.raw(chunks.join(" "));
3190
+ return sql7.raw(chunks.join(" "));
841
3191
  }
842
3192
  setTransaction(config) {
843
3193
  return this.session.execute(
844
- sql2`SET TRANSACTION ${this.getTransactionConfigSQL(config)}`
3194
+ sql7`SET TRANSACTION ${this.getTransactionConfigSQL(config)}`
845
3195
  );
846
3196
  }
847
3197
  async transaction(transaction) {
@@ -866,9 +3216,9 @@ var DatabendDriver = class {
866
3216
  this.dialect = dialect;
867
3217
  this.options = options;
868
3218
  }
869
- static [entityKind3] = "DatabendDriver";
3219
+ static [entityKind36] = "DatabendDriver";
870
3220
  createSession(schema) {
871
- return new DatabendSession(this.client, this.dialect, schema, {
3221
+ return new DatabendSession2(this.client, this.dialect, schema, {
872
3222
  logger: this.options.logger
873
3223
  });
874
3224
  }
@@ -895,7 +3245,7 @@ function createFromClient(client, config = {}, databendClient) {
895
3245
  }
896
3246
  const driver = new DatabendDriver(client, dialect, { logger });
897
3247
  const session = driver.createSession(schema);
898
- const db = new DatabendDatabase(
3248
+ const db = new DatabendDatabase2(
899
3249
  dialect,
900
3250
  session,
901
3251
  schema,
@@ -942,7 +3292,7 @@ function drizzle(clientOrConfigOrDsn, config) {
942
3292
  }
943
3293
  return createFromClient(clientOrConfigOrDsn, config);
944
3294
  }
945
- var DatabendDatabase = class extends PgDatabase {
3295
+ var DatabendDatabase2 = class extends DatabendDatabase {
946
3296
  constructor(dialect, session, schema, client, databendClient) {
947
3297
  super(dialect, session, schema);
948
3298
  this.dialect = dialect;
@@ -950,7 +3300,7 @@ var DatabendDatabase = class extends PgDatabase {
950
3300
  this.$client = client;
951
3301
  this.$databendClient = databendClient;
952
3302
  }
953
- static [entityKind3] = "DatabendDatabase";
3303
+ static [entityKind36] = "DatabendDatabase";
954
3304
  /** The underlying connection or pool */
955
3305
  $client;
956
3306
  /** The Databend Client instance (when created from DSN) */
@@ -980,24 +3330,53 @@ async function migrate(db, config) {
980
3330
  );
981
3331
  }
982
3332
  export {
983
- DatabendDatabase,
3333
+ DatabendColumn,
3334
+ DatabendColumnBuilder,
3335
+ DatabendDatabase2 as DatabendDatabase,
3336
+ DatabendDialect,
984
3337
  DatabendDriver,
985
- DatabendPreparedQuery,
986
- DatabendSession,
987
- DatabendTransaction,
3338
+ DatabendPreparedQuery2 as DatabendPreparedQuery,
3339
+ DatabendSession2 as DatabendSession,
3340
+ DatabendTable,
3341
+ DatabendTransaction2 as DatabendTransaction,
3342
+ alias,
3343
+ bigint,
3344
+ binary,
3345
+ bitmap,
3346
+ boolean,
988
3347
  closeClientConnection,
989
3348
  createDatabendConnectionPool,
990
3349
  databendArray,
991
3350
  databendDate,
992
3351
  databendMap,
3352
+ databendSchema,
3353
+ databendTable,
3354
+ databendTableCreator,
993
3355
  databendTimestamp,
994
3356
  databendTuple,
995
3357
  databendVariant,
3358
+ databendView,
3359
+ date,
3360
+ decimal,
3361
+ doublePrecision,
996
3362
  drizzle,
997
3363
  execOnClient,
998
3364
  executeArraysOnClient,
999
3365
  executeOnClient,
3366
+ float,
3367
+ getTableConfig,
3368
+ index,
3369
+ integer,
1000
3370
  isPool,
1001
3371
  migrate,
1002
- prepareParams
3372
+ prepareParams,
3373
+ primaryKey,
3374
+ real,
3375
+ smallint,
3376
+ text,
3377
+ timestamp,
3378
+ tinyint,
3379
+ uniqueIndex,
3380
+ varchar,
3381
+ variant
1003
3382
  };