@simplysm/orm-common 13.0.83 → 13.0.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ddl/initialize.d.ts +2 -2
- package/dist/ddl/initialize.js +1 -1
- package/dist/ddl/initialize.js.map +1 -1
- package/dist/ddl/table-ddl.d.ts +1 -1
- package/dist/exec/queryable.d.ts +115 -115
- package/dist/exec/queryable.js +68 -68
- package/dist/exec/queryable.js.map +1 -1
- package/dist/expr/expr.d.ts +248 -248
- package/dist/expr/expr.js +250 -250
- package/dist/query-builder/base/expr-renderer-base.d.ts +7 -7
- package/dist/query-builder/mssql/mssql-expr-renderer.d.ts +3 -3
- package/dist/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/mssql/mssql-expr-renderer.js +5 -5
- package/dist/query-builder/mssql/mssql-query-builder.d.ts +2 -2
- package/dist/query-builder/mssql/mssql-query-builder.d.ts.map +1 -1
- package/dist/query-builder/mssql/mssql-query-builder.js +7 -7
- package/dist/query-builder/mysql/mysql-expr-renderer.d.ts +2 -2
- package/dist/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/mysql/mysql-expr-renderer.js +4 -4
- package/dist/query-builder/mysql/mysql-query-builder.d.ts +10 -10
- package/dist/query-builder/mysql/mysql-query-builder.d.ts.map +1 -1
- package/dist/query-builder/mysql/mysql-query-builder.js +4 -4
- package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts +2 -2
- package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/postgresql/postgresql-expr-renderer.js +4 -4
- package/dist/query-builder/postgresql/postgresql-query-builder.d.ts +8 -8
- package/dist/query-builder/postgresql/postgresql-query-builder.d.ts.map +1 -1
- package/dist/query-builder/postgresql/postgresql-query-builder.js +7 -7
- package/dist/query-builder/query-builder.d.ts +1 -1
- package/dist/schema/factory/column-builder.d.ts +46 -46
- package/dist/schema/factory/column-builder.js +25 -25
- package/dist/schema/factory/index-builder.d.ts +22 -22
- package/dist/schema/factory/index-builder.js +14 -14
- package/dist/schema/factory/relation-builder.d.ts +93 -93
- package/dist/schema/factory/relation-builder.d.ts.map +1 -1
- package/dist/schema/factory/relation-builder.js +37 -37
- package/dist/schema/procedure-builder.d.ts +38 -38
- package/dist/schema/procedure-builder.d.ts.map +1 -1
- package/dist/schema/procedure-builder.js +26 -26
- package/dist/schema/table-builder.d.ts +38 -38
- package/dist/schema/table-builder.d.ts.map +1 -1
- package/dist/schema/table-builder.js +29 -29
- package/dist/schema/view-builder.d.ts +26 -26
- package/dist/schema/view-builder.d.ts.map +1 -1
- package/dist/schema/view-builder.js +18 -18
- package/dist/types/db.d.ts +40 -40
- package/dist/types/expr.d.ts +75 -75
- package/dist/types/expr.d.ts.map +1 -1
- package/dist/types/query-def.d.ts +32 -32
- package/dist/types/query-def.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/ddl/initialize.ts +16 -16
- package/src/ddl/table-ddl.ts +1 -1
- package/src/exec/queryable.ts +163 -163
- package/src/expr/expr.ts +257 -257
- package/src/query-builder/base/expr-renderer-base.ts +8 -8
- package/src/query-builder/mssql/mssql-expr-renderer.ts +20 -20
- package/src/query-builder/mssql/mssql-query-builder.ts +28 -28
- package/src/query-builder/mysql/mysql-expr-renderer.ts +22 -22
- package/src/query-builder/mysql/mysql-query-builder.ts +65 -65
- package/src/query-builder/postgresql/postgresql-expr-renderer.ts +15 -15
- package/src/query-builder/postgresql/postgresql-query-builder.ts +43 -43
- package/src/query-builder/query-builder.ts +1 -1
- package/src/schema/factory/column-builder.ts +48 -48
- package/src/schema/factory/index-builder.ts +22 -22
- package/src/schema/factory/relation-builder.ts +95 -95
- package/src/schema/procedure-builder.ts +38 -38
- package/src/schema/table-builder.ts +38 -38
- package/src/schema/view-builder.ts +28 -28
- package/src/types/db.ts +41 -41
- package/src/types/expr.ts +79 -79
- package/src/types/query-def.ts +37 -37
- package/tests/ddl/basic.expected.ts +8 -8
package/src/expr/expr.ts
CHANGED
|
@@ -66,9 +66,9 @@ export const expr = {
|
|
|
66
66
|
*
|
|
67
67
|
* Widen to base type matching dataType, remove literal type
|
|
68
68
|
*
|
|
69
|
-
* @param dataType -
|
|
70
|
-
* @param value -
|
|
71
|
-
* @returns
|
|
69
|
+
* @param dataType - The data type of the value ("string", "number", "boolean", "DateTime", "DateOnly", "Time", "Uuid", "Buffer")
|
|
70
|
+
* @param value - Value to wrap (undefined allowed)
|
|
71
|
+
* @returns Wrapped ExprUnit instance
|
|
72
72
|
*
|
|
73
73
|
* @example
|
|
74
74
|
* ```typescript
|
|
@@ -271,16 +271,16 @@ export const expr = {
|
|
|
271
271
|
/**
|
|
272
272
|
* range comparison (BETWEEN)
|
|
273
273
|
*
|
|
274
|
-
* from/to
|
|
274
|
+
* If from/to is undefined, that direction is unbounded
|
|
275
275
|
*
|
|
276
276
|
* @param source - Column or expression to compare
|
|
277
|
-
* @param from -
|
|
278
|
-
* @param to -
|
|
277
|
+
* @param from - Start value (no lower bound if undefined)
|
|
278
|
+
* @param to - End value (no upper bound if undefined)
|
|
279
279
|
* @returns WHERE condition expression
|
|
280
280
|
*
|
|
281
281
|
* @example
|
|
282
282
|
* ```typescript
|
|
283
|
-
* // range
|
|
283
|
+
* // Specify range
|
|
284
284
|
* db.user().where((u) => [expr.between(u.age, 18, 65)])
|
|
285
285
|
* // WHERE age BETWEEN 18 AND 65
|
|
286
286
|
*
|
|
@@ -307,9 +307,9 @@ export const expr = {
|
|
|
307
307
|
//#region ========== WHERE - NULL check ==========
|
|
308
308
|
|
|
309
309
|
/**
|
|
310
|
-
* NULL
|
|
310
|
+
* NULL check (IS NULL)
|
|
311
311
|
*
|
|
312
|
-
* @param source -
|
|
312
|
+
* @param source - Column or expression to check
|
|
313
313
|
* @returns WHERE condition expression
|
|
314
314
|
*
|
|
315
315
|
* @example
|
|
@@ -330,22 +330,22 @@ export const expr = {
|
|
|
330
330
|
//#region ========== WHERE - String search ==========
|
|
331
331
|
|
|
332
332
|
/**
|
|
333
|
-
* LIKE pattern
|
|
333
|
+
* LIKE pattern matching
|
|
334
334
|
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
335
|
+
* `%` matches zero or more characters, `_` matches a single character.
|
|
336
|
+
* Special characters are escaped with `\`
|
|
337
337
|
*
|
|
338
|
-
* @param source -
|
|
339
|
-
* @param pattern -
|
|
338
|
+
* @param source - Column or expression to search
|
|
339
|
+
* @param pattern - Search pattern (%, _ wildcards available)
|
|
340
340
|
* @returns WHERE condition expression
|
|
341
341
|
*
|
|
342
342
|
* @example
|
|
343
343
|
* ```typescript
|
|
344
|
-
* //
|
|
344
|
+
* // Prefix search
|
|
345
345
|
* db.user().where((u) => [expr.like(u.name, "John%")])
|
|
346
346
|
* // WHERE name LIKE 'John%' ESCAPE '\'
|
|
347
347
|
*
|
|
348
|
-
* //
|
|
348
|
+
* // Contains search
|
|
349
349
|
* db.user().where((u) => [expr.like(u.email, "%@gmail.com")])
|
|
350
350
|
* ```
|
|
351
351
|
*/
|
|
@@ -361,12 +361,12 @@ export const expr = {
|
|
|
361
361
|
},
|
|
362
362
|
|
|
363
363
|
/**
|
|
364
|
-
*
|
|
364
|
+
* Regular expression pattern matching
|
|
365
365
|
*
|
|
366
|
-
*
|
|
366
|
+
* Note: regex syntax may differ between DBMS implementations
|
|
367
367
|
*
|
|
368
|
-
* @param source -
|
|
369
|
-
* @param pattern -
|
|
368
|
+
* @param source - Column or expression to search
|
|
369
|
+
* @param pattern - Regular expression pattern
|
|
370
370
|
* @returns WHERE condition expression
|
|
371
371
|
*
|
|
372
372
|
* @example
|
|
@@ -391,10 +391,10 @@ export const expr = {
|
|
|
391
391
|
//#region ========== WHERE - IN ==========
|
|
392
392
|
|
|
393
393
|
/**
|
|
394
|
-
* IN operator -
|
|
394
|
+
* IN operator - Compare against a list of values
|
|
395
395
|
*
|
|
396
396
|
* @param source - Column or expression to compare
|
|
397
|
-
* @param values -
|
|
397
|
+
* @param values - List of values to compare against
|
|
398
398
|
* @returns WHERE condition expression
|
|
399
399
|
*
|
|
400
400
|
* @example
|
|
@@ -412,14 +412,14 @@ export const expr = {
|
|
|
412
412
|
},
|
|
413
413
|
|
|
414
414
|
/**
|
|
415
|
-
* IN (SELECT ...) -
|
|
415
|
+
* IN (SELECT ...) - Compare against subquery results
|
|
416
416
|
*
|
|
417
|
-
*
|
|
417
|
+
* The subquery must SELECT only a single column
|
|
418
418
|
*
|
|
419
419
|
* @param source - Column or expression to compare
|
|
420
|
-
* @param query -
|
|
420
|
+
* @param query - Queryable that returns a single column
|
|
421
421
|
* @returns WHERE condition expression
|
|
422
|
-
* @throws {Error}
|
|
422
|
+
* @throws {Error} When the subquery does not return a single column
|
|
423
423
|
*
|
|
424
424
|
* @example
|
|
425
425
|
* ```typescript
|
|
@@ -450,16 +450,16 @@ export const expr = {
|
|
|
450
450
|
},
|
|
451
451
|
|
|
452
452
|
/**
|
|
453
|
-
* EXISTS (SELECT ...) -
|
|
453
|
+
* EXISTS (SELECT ...) - Check if subquery returns any rows
|
|
454
454
|
*
|
|
455
|
-
*
|
|
455
|
+
* Returns true if the subquery returns one or more rows
|
|
456
456
|
*
|
|
457
|
-
* @param query -
|
|
457
|
+
* @param query - Queryable to check for existence
|
|
458
458
|
* @returns WHERE condition expression
|
|
459
459
|
*
|
|
460
460
|
* @example
|
|
461
461
|
* ```typescript
|
|
462
|
-
* //
|
|
462
|
+
* // Query users who have orders
|
|
463
463
|
* db.user().where((u) => [
|
|
464
464
|
* expr.exists(
|
|
465
465
|
* db.order().where((o) => [expr.eq(o.userId, u.id)])
|
|
@@ -469,7 +469,7 @@ export const expr = {
|
|
|
469
469
|
* ```
|
|
470
470
|
*/
|
|
471
471
|
exists(query: Queryable<any, any>): WhereExprUnit {
|
|
472
|
-
const { select: _, ...queryDefWithoutSelect } = query.getSelectQueryDef(); // EXISTS
|
|
472
|
+
const { select: _, ...queryDefWithoutSelect } = query.getSelectQueryDef(); // EXISTS does not need SELECT clause, saves packet size
|
|
473
473
|
return new WhereExprUnit({
|
|
474
474
|
type: "exists",
|
|
475
475
|
query: queryDefWithoutSelect,
|
|
@@ -481,10 +481,10 @@ export const expr = {
|
|
|
481
481
|
//#region ========== WHERE - Logical operators ==========
|
|
482
482
|
|
|
483
483
|
/**
|
|
484
|
-
* NOT operator -
|
|
484
|
+
* NOT operator - Negate a condition
|
|
485
485
|
*
|
|
486
|
-
* @param arg -
|
|
487
|
-
* @returns
|
|
486
|
+
* @param arg - Condition to negate
|
|
487
|
+
* @returns Negated WHERE condition expression
|
|
488
488
|
*
|
|
489
489
|
* @example
|
|
490
490
|
* ```typescript
|
|
@@ -500,12 +500,12 @@ export const expr = {
|
|
|
500
500
|
},
|
|
501
501
|
|
|
502
502
|
/**
|
|
503
|
-
* AND operator -
|
|
503
|
+
* AND operator - All conditions must be satisfied
|
|
504
504
|
*
|
|
505
|
-
*
|
|
505
|
+
* Combines multiple conditions with AND. Passing an array to where() automatically applies AND
|
|
506
506
|
*
|
|
507
|
-
* @param conditions -
|
|
508
|
-
* @returns
|
|
507
|
+
* @param conditions - List of conditions to combine with AND
|
|
508
|
+
* @returns Combined WHERE condition expression
|
|
509
509
|
*
|
|
510
510
|
* @example
|
|
511
511
|
* ```typescript
|
|
@@ -529,10 +529,10 @@ export const expr = {
|
|
|
529
529
|
},
|
|
530
530
|
|
|
531
531
|
/**
|
|
532
|
-
* OR operator -
|
|
532
|
+
* OR operator - At least one condition must be satisfied
|
|
533
533
|
*
|
|
534
|
-
* @param conditions -
|
|
535
|
-
* @returns
|
|
534
|
+
* @param conditions - List of conditions to combine with OR
|
|
535
|
+
* @returns Combined WHERE condition expression
|
|
536
536
|
*
|
|
537
537
|
* @example
|
|
538
538
|
* ```typescript
|
|
@@ -557,15 +557,15 @@ export const expr = {
|
|
|
557
557
|
|
|
558
558
|
//#endregion
|
|
559
559
|
|
|
560
|
-
//#region ========== SELECT -
|
|
560
|
+
//#region ========== SELECT - String ==========
|
|
561
561
|
|
|
562
562
|
/**
|
|
563
|
-
*
|
|
563
|
+
* String concatenation (CONCAT)
|
|
564
564
|
*
|
|
565
|
-
* NULL
|
|
565
|
+
* NULL values are treated as empty strings (auto-transformed per DBMS)
|
|
566
566
|
*
|
|
567
|
-
* @param args -
|
|
568
|
-
* @returns
|
|
567
|
+
* @param args - Strings to concatenate
|
|
568
|
+
* @returns Concatenated string expression
|
|
569
569
|
*
|
|
570
570
|
* @example
|
|
571
571
|
* ```typescript
|
|
@@ -583,11 +583,11 @@ export const expr = {
|
|
|
583
583
|
},
|
|
584
584
|
|
|
585
585
|
/**
|
|
586
|
-
*
|
|
586
|
+
* Extract specified length from the left of a string (LEFT)
|
|
587
587
|
*
|
|
588
|
-
* @param source -
|
|
589
|
-
* @param length -
|
|
590
|
-
* @returns
|
|
588
|
+
* @param source - Original string
|
|
589
|
+
* @param length - Number of characters to extract
|
|
590
|
+
* @returns Extracted string expression
|
|
591
591
|
*
|
|
592
592
|
* @example
|
|
593
593
|
* ```typescript
|
|
@@ -606,11 +606,11 @@ export const expr = {
|
|
|
606
606
|
},
|
|
607
607
|
|
|
608
608
|
/**
|
|
609
|
-
*
|
|
609
|
+
* Extract specified length from the right of a string (RIGHT)
|
|
610
610
|
*
|
|
611
|
-
* @param source -
|
|
612
|
-
* @param length -
|
|
613
|
-
* @returns
|
|
611
|
+
* @param source - Original string
|
|
612
|
+
* @param length - Number of characters to extract
|
|
613
|
+
* @returns Extracted string expression
|
|
614
614
|
*
|
|
615
615
|
* @example
|
|
616
616
|
* ```typescript
|
|
@@ -629,10 +629,10 @@ export const expr = {
|
|
|
629
629
|
},
|
|
630
630
|
|
|
631
631
|
/**
|
|
632
|
-
*
|
|
632
|
+
* Remove whitespace from both sides of a string (TRIM)
|
|
633
633
|
*
|
|
634
|
-
* @param source -
|
|
635
|
-
* @returns
|
|
634
|
+
* @param source - Original string
|
|
635
|
+
* @returns String expression with whitespace removed
|
|
636
636
|
*
|
|
637
637
|
* @example
|
|
638
638
|
* ```typescript
|
|
@@ -650,14 +650,14 @@ export const expr = {
|
|
|
650
650
|
},
|
|
651
651
|
|
|
652
652
|
/**
|
|
653
|
-
*
|
|
653
|
+
* Left padding (LPAD)
|
|
654
654
|
*
|
|
655
|
-
*
|
|
655
|
+
* Repeatedly adds fillString on the left until the target length is reached
|
|
656
656
|
*
|
|
657
|
-
* @param source -
|
|
658
|
-
* @param length -
|
|
659
|
-
* @param fillString -
|
|
660
|
-
* @returns
|
|
657
|
+
* @param source - Original string
|
|
658
|
+
* @param length - Target length
|
|
659
|
+
* @param fillString - String to use for padding
|
|
660
|
+
* @returns Padded string expression
|
|
661
661
|
*
|
|
662
662
|
* @example
|
|
663
663
|
* ```typescript
|
|
@@ -682,12 +682,12 @@ export const expr = {
|
|
|
682
682
|
},
|
|
683
683
|
|
|
684
684
|
/**
|
|
685
|
-
*
|
|
685
|
+
* String replacement (REPLACE)
|
|
686
686
|
*
|
|
687
|
-
* @param source -
|
|
688
|
-
* @param from -
|
|
689
|
-
* @param to -
|
|
690
|
-
* @returns
|
|
687
|
+
* @param source - Original string
|
|
688
|
+
* @param from - String to find
|
|
689
|
+
* @param to - Replacement string
|
|
690
|
+
* @returns Replaced string expression
|
|
691
691
|
*
|
|
692
692
|
* @example
|
|
693
693
|
* ```typescript
|
|
@@ -711,10 +711,10 @@ export const expr = {
|
|
|
711
711
|
},
|
|
712
712
|
|
|
713
713
|
/**
|
|
714
|
-
*
|
|
714
|
+
* Convert string to uppercase (UPPER)
|
|
715
715
|
*
|
|
716
|
-
* @param source -
|
|
717
|
-
* @returns
|
|
716
|
+
* @param source - Original string
|
|
717
|
+
* @returns Uppercase string expression
|
|
718
718
|
*
|
|
719
719
|
* @example
|
|
720
720
|
* ```typescript
|
|
@@ -732,10 +732,10 @@ export const expr = {
|
|
|
732
732
|
},
|
|
733
733
|
|
|
734
734
|
/**
|
|
735
|
-
*
|
|
735
|
+
* Convert string to lowercase (LOWER)
|
|
736
736
|
*
|
|
737
|
-
* @param source -
|
|
738
|
-
* @returns
|
|
737
|
+
* @param source - Original string
|
|
738
|
+
* @returns Lowercase string expression
|
|
739
739
|
*
|
|
740
740
|
* @example
|
|
741
741
|
* ```typescript
|
|
@@ -753,10 +753,10 @@ export const expr = {
|
|
|
753
753
|
},
|
|
754
754
|
|
|
755
755
|
/**
|
|
756
|
-
*
|
|
756
|
+
* String length (character count)
|
|
757
757
|
*
|
|
758
|
-
* @param source -
|
|
759
|
-
* @returns
|
|
758
|
+
* @param source - Original string
|
|
759
|
+
* @returns Character count
|
|
760
760
|
*
|
|
761
761
|
* @example
|
|
762
762
|
* ```typescript
|
|
@@ -774,12 +774,12 @@ export const expr = {
|
|
|
774
774
|
},
|
|
775
775
|
|
|
776
776
|
/**
|
|
777
|
-
*
|
|
777
|
+
* String byte length
|
|
778
778
|
*
|
|
779
|
-
* UTF-8
|
|
779
|
+
* In UTF-8, CJK characters are 3 bytes each
|
|
780
780
|
*
|
|
781
|
-
* @param source -
|
|
782
|
-
* @returns
|
|
781
|
+
* @param source - Original string
|
|
782
|
+
* @returns Byte count
|
|
783
783
|
*
|
|
784
784
|
* @example
|
|
785
785
|
* ```typescript
|
|
@@ -797,19 +797,19 @@ export const expr = {
|
|
|
797
797
|
},
|
|
798
798
|
|
|
799
799
|
/**
|
|
800
|
-
*
|
|
800
|
+
* Extract part of a string (SUBSTRING)
|
|
801
801
|
*
|
|
802
|
-
*
|
|
802
|
+
* Uses 1-based index per SQL standard
|
|
803
803
|
*
|
|
804
|
-
* @param source -
|
|
805
|
-
* @param start -
|
|
806
|
-
* @param length -
|
|
807
|
-
* @returns
|
|
804
|
+
* @param source - Original string
|
|
805
|
+
* @param start - Start position (starting from 1)
|
|
806
|
+
* @param length - Length to extract (to the end if omitted)
|
|
807
|
+
* @returns Extracted string expression
|
|
808
808
|
*
|
|
809
809
|
* @example
|
|
810
810
|
* ```typescript
|
|
811
811
|
* db.user().select((u) => ({
|
|
812
|
-
* // "Hello World"
|
|
812
|
+
* // From "Hello World", 5 characters starting at index 1: "Hello"
|
|
813
813
|
* prefix: expr.substring(u.name, 1, 5),
|
|
814
814
|
* }))
|
|
815
815
|
* // SELECT SUBSTRING(name, 1, 5) AS prefix
|
|
@@ -829,13 +829,13 @@ export const expr = {
|
|
|
829
829
|
},
|
|
830
830
|
|
|
831
831
|
/**
|
|
832
|
-
*
|
|
832
|
+
* Find position within a string (LOCATE/CHARINDEX)
|
|
833
833
|
*
|
|
834
|
-
* 1-based index
|
|
834
|
+
* Returns 1-based index, or 0 if not found
|
|
835
835
|
*
|
|
836
|
-
* @param source -
|
|
837
|
-
* @param search -
|
|
838
|
-
* @returns
|
|
836
|
+
* @param source - String to search in
|
|
837
|
+
* @param search - String to find
|
|
838
|
+
* @returns Position (starting from 1, 0 if not found)
|
|
839
839
|
*
|
|
840
840
|
* @example
|
|
841
841
|
* ```typescript
|
|
@@ -859,10 +859,10 @@ export const expr = {
|
|
|
859
859
|
//#region ========== SELECT - Number ==========
|
|
860
860
|
|
|
861
861
|
/**
|
|
862
|
-
*
|
|
862
|
+
* Absolute value (ABS)
|
|
863
863
|
*
|
|
864
|
-
* @param source -
|
|
865
|
-
* @returns
|
|
864
|
+
* @param source - Original number
|
|
865
|
+
* @returns Absolute value expression
|
|
866
866
|
*
|
|
867
867
|
* @example
|
|
868
868
|
* ```typescript
|
|
@@ -880,11 +880,11 @@ export const expr = {
|
|
|
880
880
|
},
|
|
881
881
|
|
|
882
882
|
/**
|
|
883
|
-
*
|
|
883
|
+
* Round (ROUND)
|
|
884
884
|
*
|
|
885
|
-
* @param source -
|
|
886
|
-
* @param digits -
|
|
887
|
-
* @returns
|
|
885
|
+
* @param source - Original number
|
|
886
|
+
* @param digits - Number of decimal places
|
|
887
|
+
* @returns Rounded number expression
|
|
888
888
|
*
|
|
889
889
|
* @example
|
|
890
890
|
* ```typescript
|
|
@@ -904,10 +904,10 @@ export const expr = {
|
|
|
904
904
|
},
|
|
905
905
|
|
|
906
906
|
/**
|
|
907
|
-
*
|
|
907
|
+
* Ceiling (CEILING)
|
|
908
908
|
*
|
|
909
|
-
* @param source -
|
|
910
|
-
* @returns
|
|
909
|
+
* @param source - Original number
|
|
910
|
+
* @returns Ceiling number expression
|
|
911
911
|
*
|
|
912
912
|
* @example
|
|
913
913
|
* ```typescript
|
|
@@ -926,10 +926,10 @@ export const expr = {
|
|
|
926
926
|
},
|
|
927
927
|
|
|
928
928
|
/**
|
|
929
|
-
*
|
|
929
|
+
* Floor (FLOOR)
|
|
930
930
|
*
|
|
931
|
-
* @param source -
|
|
932
|
-
* @returns
|
|
931
|
+
* @param source - Original number
|
|
932
|
+
* @returns Floor number expression
|
|
933
933
|
*
|
|
934
934
|
* @example
|
|
935
935
|
* ```typescript
|
|
@@ -952,10 +952,10 @@ export const expr = {
|
|
|
952
952
|
//#region ========== SELECT - Date ==========
|
|
953
953
|
|
|
954
954
|
/**
|
|
955
|
-
*
|
|
955
|
+
* Extract year (YEAR)
|
|
956
956
|
*
|
|
957
|
-
* @param source - DateTime
|
|
958
|
-
* @returns
|
|
957
|
+
* @param source - DateTime or DateOnly expression
|
|
958
|
+
* @returns Year (4-digit number)
|
|
959
959
|
*
|
|
960
960
|
* @example
|
|
961
961
|
* ```typescript
|
|
@@ -975,10 +975,10 @@ export const expr = {
|
|
|
975
975
|
},
|
|
976
976
|
|
|
977
977
|
/**
|
|
978
|
-
*
|
|
978
|
+
* Extract month (MONTH)
|
|
979
979
|
*
|
|
980
|
-
* @param source - DateTime
|
|
981
|
-
* @returns
|
|
980
|
+
* @param source - DateTime or DateOnly expression
|
|
981
|
+
* @returns Month (1~12)
|
|
982
982
|
*
|
|
983
983
|
* @example
|
|
984
984
|
* ```typescript
|
|
@@ -998,10 +998,10 @@ export const expr = {
|
|
|
998
998
|
},
|
|
999
999
|
|
|
1000
1000
|
/**
|
|
1001
|
-
*
|
|
1001
|
+
* Extract day (DAY)
|
|
1002
1002
|
*
|
|
1003
|
-
* @param source - DateTime
|
|
1004
|
-
* @returns
|
|
1003
|
+
* @param source - DateTime or DateOnly expression
|
|
1004
|
+
* @returns Day (1~31)
|
|
1005
1005
|
*
|
|
1006
1006
|
* @example
|
|
1007
1007
|
* ```typescript
|
|
@@ -1021,10 +1021,10 @@ export const expr = {
|
|
|
1021
1021
|
},
|
|
1022
1022
|
|
|
1023
1023
|
/**
|
|
1024
|
-
*
|
|
1024
|
+
* Extract hour (HOUR)
|
|
1025
1025
|
*
|
|
1026
|
-
* @param source - DateTime
|
|
1027
|
-
* @returns
|
|
1026
|
+
* @param source - DateTime or Time expression
|
|
1027
|
+
* @returns Hour (0~23)
|
|
1028
1028
|
*
|
|
1029
1029
|
* @example
|
|
1030
1030
|
* ```typescript
|
|
@@ -1044,10 +1044,10 @@ export const expr = {
|
|
|
1044
1044
|
},
|
|
1045
1045
|
|
|
1046
1046
|
/**
|
|
1047
|
-
*
|
|
1047
|
+
* Extract minute (MINUTE)
|
|
1048
1048
|
*
|
|
1049
|
-
* @param source - DateTime
|
|
1050
|
-
* @returns
|
|
1049
|
+
* @param source - DateTime or Time expression
|
|
1050
|
+
* @returns Minute (0~59)
|
|
1051
1051
|
*
|
|
1052
1052
|
* @example
|
|
1053
1053
|
* ```typescript
|
|
@@ -1067,10 +1067,10 @@ export const expr = {
|
|
|
1067
1067
|
},
|
|
1068
1068
|
|
|
1069
1069
|
/**
|
|
1070
|
-
*
|
|
1070
|
+
* Extract second (SECOND)
|
|
1071
1071
|
*
|
|
1072
|
-
* @param source - DateTime
|
|
1073
|
-
* @returns
|
|
1072
|
+
* @param source - DateTime or Time expression
|
|
1073
|
+
* @returns Second (0~59)
|
|
1074
1074
|
*
|
|
1075
1075
|
* @example
|
|
1076
1076
|
* ```typescript
|
|
@@ -1090,12 +1090,12 @@ export const expr = {
|
|
|
1090
1090
|
},
|
|
1091
1091
|
|
|
1092
1092
|
/**
|
|
1093
|
-
* ISO
|
|
1093
|
+
* Extract ISO week number
|
|
1094
1094
|
*
|
|
1095
|
-
* ISO 8601
|
|
1095
|
+
* ISO 8601 week number (starts Monday, 1~53)
|
|
1096
1096
|
*
|
|
1097
1097
|
* @param source - DateOnly expression
|
|
1098
|
-
* @returns ISO
|
|
1098
|
+
* @returns ISO week number
|
|
1099
1099
|
*
|
|
1100
1100
|
* @example
|
|
1101
1101
|
* ```typescript
|
|
@@ -1115,19 +1115,19 @@ export const expr = {
|
|
|
1115
1115
|
},
|
|
1116
1116
|
|
|
1117
1117
|
/**
|
|
1118
|
-
* ISO
|
|
1118
|
+
* ISO week start date (Monday)
|
|
1119
1119
|
*
|
|
1120
|
-
*
|
|
1120
|
+
* Returns the Monday of the week the given date belongs to
|
|
1121
1121
|
*
|
|
1122
1122
|
* @param source - DateOnly expression
|
|
1123
|
-
* @returns
|
|
1123
|
+
* @returns Week start date (Monday)
|
|
1124
1124
|
*
|
|
1125
1125
|
* @example
|
|
1126
1126
|
* ```typescript
|
|
1127
1127
|
* db.order().select((o) => ({
|
|
1128
1128
|
* weekStart: expr.isoWeekStartDate(o.orderDate),
|
|
1129
1129
|
* }))
|
|
1130
|
-
* // 2024-01-10 (
|
|
1130
|
+
* // 2024-01-10 (Wed) → 2024-01-08 (Mon)
|
|
1131
1131
|
* ```
|
|
1132
1132
|
*/
|
|
1133
1133
|
isoWeekStartDate<T extends DateOnly | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
@@ -1138,12 +1138,12 @@ export const expr = {
|
|
|
1138
1138
|
},
|
|
1139
1139
|
|
|
1140
1140
|
/**
|
|
1141
|
-
* ISO
|
|
1141
|
+
* ISO year-month (first day of the month)
|
|
1142
1142
|
*
|
|
1143
|
-
*
|
|
1143
|
+
* Returns the first day of the month for the given date
|
|
1144
1144
|
*
|
|
1145
1145
|
* @param source - DateOnly expression
|
|
1146
|
-
* @returns
|
|
1146
|
+
* @returns First day of the month
|
|
1147
1147
|
*
|
|
1148
1148
|
* @example
|
|
1149
1149
|
* ```typescript
|
|
@@ -1161,12 +1161,12 @@ export const expr = {
|
|
|
1161
1161
|
},
|
|
1162
1162
|
|
|
1163
1163
|
/**
|
|
1164
|
-
*
|
|
1164
|
+
* Calculate date difference (DATEDIFF)
|
|
1165
1165
|
*
|
|
1166
|
-
* @param unit -
|
|
1167
|
-
* @param from -
|
|
1168
|
-
* @param to -
|
|
1169
|
-
* @returns
|
|
1166
|
+
* @param unit - Unit ("year", "month", "day", "hour", "minute", "second")
|
|
1167
|
+
* @param from - Start date
|
|
1168
|
+
* @param to - End date
|
|
1169
|
+
* @returns Difference value (to - from)
|
|
1170
1170
|
*
|
|
1171
1171
|
* @example
|
|
1172
1172
|
* ```typescript
|
|
@@ -1190,12 +1190,12 @@ export const expr = {
|
|
|
1190
1190
|
},
|
|
1191
1191
|
|
|
1192
1192
|
/**
|
|
1193
|
-
*
|
|
1193
|
+
* Add to date (DATEADD)
|
|
1194
1194
|
*
|
|
1195
|
-
* @param unit -
|
|
1196
|
-
* @param source -
|
|
1197
|
-
* @param value -
|
|
1198
|
-
* @returns
|
|
1195
|
+
* @param unit - Unit ("year", "month", "day", "hour", "minute", "second")
|
|
1196
|
+
* @param source - Original date
|
|
1197
|
+
* @param value - Value to add (negative allowed)
|
|
1198
|
+
* @returns Calculated date
|
|
1199
1199
|
*
|
|
1200
1200
|
* @example
|
|
1201
1201
|
* ```typescript
|
|
@@ -1219,13 +1219,13 @@ export const expr = {
|
|
|
1219
1219
|
},
|
|
1220
1220
|
|
|
1221
1221
|
/**
|
|
1222
|
-
* Date
|
|
1222
|
+
* Date format (DATE_FORMAT)
|
|
1223
1223
|
*
|
|
1224
|
-
*
|
|
1224
|
+
* Format string rules may differ between DBMS implementations
|
|
1225
1225
|
*
|
|
1226
1226
|
* @param source - Date expression
|
|
1227
|
-
* @param format -
|
|
1228
|
-
* @returns
|
|
1227
|
+
* @param format - Format string (e.g., "%Y-%m-%d")
|
|
1228
|
+
* @returns Formatted string expression
|
|
1229
1229
|
*
|
|
1230
1230
|
* @example
|
|
1231
1231
|
* ```typescript
|
|
@@ -1252,12 +1252,12 @@ export const expr = {
|
|
|
1252
1252
|
//#region ========== SELECT - Condition ==========
|
|
1253
1253
|
|
|
1254
1254
|
/**
|
|
1255
|
-
* NULL
|
|
1255
|
+
* NULL replacement (COALESCE/IFNULL)
|
|
1256
1256
|
*
|
|
1257
|
-
*
|
|
1257
|
+
* Returns the first non-null value. If the last argument is non-nullable, the result is also non-nullable
|
|
1258
1258
|
*
|
|
1259
|
-
* @param args -
|
|
1260
|
-
* @returns
|
|
1259
|
+
* @param args - Values to inspect (last is default value)
|
|
1260
|
+
* @returns First non-null value
|
|
1261
1261
|
*
|
|
1262
1262
|
* @example
|
|
1263
1263
|
* ```typescript
|
|
@@ -1270,18 +1270,18 @@ export const expr = {
|
|
|
1270
1270
|
coalesce,
|
|
1271
1271
|
|
|
1272
1272
|
/**
|
|
1273
|
-
*
|
|
1273
|
+
* Return NULL if value matches (NULLIF)
|
|
1274
1274
|
*
|
|
1275
|
-
* source === value
|
|
1275
|
+
* Returns NULL if source === value, otherwise returns source
|
|
1276
1276
|
*
|
|
1277
|
-
* @param source -
|
|
1278
|
-
* @param value -
|
|
1279
|
-
* @returns NULL
|
|
1277
|
+
* @param source - Original value
|
|
1278
|
+
* @param value - Value to compare
|
|
1279
|
+
* @returns NULL or original value
|
|
1280
1280
|
*
|
|
1281
1281
|
* @example
|
|
1282
1282
|
* ```typescript
|
|
1283
1283
|
* db.user().select((u) => ({
|
|
1284
|
-
* //
|
|
1284
|
+
* // Convert empty string to NULL
|
|
1285
1285
|
* bio: expr.nullIf(u.bio, ""),
|
|
1286
1286
|
* }))
|
|
1287
1287
|
* // SELECT NULLIF(bio, '') AS bio
|
|
@@ -1299,11 +1299,11 @@ export const expr = {
|
|
|
1299
1299
|
},
|
|
1300
1300
|
|
|
1301
1301
|
/**
|
|
1302
|
-
* WHERE
|
|
1302
|
+
* Transform WHERE expression to boolean
|
|
1303
1303
|
*
|
|
1304
|
-
*
|
|
1304
|
+
* Used when condition results should be used as a boolean column in SELECT clause
|
|
1305
1305
|
*
|
|
1306
|
-
* @param condition -
|
|
1306
|
+
* @param condition - Condition to transform
|
|
1307
1307
|
* @returns boolean expression
|
|
1308
1308
|
*
|
|
1309
1309
|
* @example
|
|
@@ -1324,7 +1324,7 @@ export const expr = {
|
|
|
1324
1324
|
/**
|
|
1325
1325
|
* CASE WHEN expression builder
|
|
1326
1326
|
*
|
|
1327
|
-
*
|
|
1327
|
+
* Build conditional branches using method chaining
|
|
1328
1328
|
*
|
|
1329
1329
|
* @returns SwitchExprBuilder instance
|
|
1330
1330
|
*
|
|
@@ -1345,12 +1345,12 @@ export const expr = {
|
|
|
1345
1345
|
},
|
|
1346
1346
|
|
|
1347
1347
|
/**
|
|
1348
|
-
*
|
|
1348
|
+
* Simple IF condition (ternary operator)
|
|
1349
1349
|
*
|
|
1350
1350
|
* @param condition - Condition
|
|
1351
|
-
* @param then -
|
|
1352
|
-
* @param else_ -
|
|
1353
|
-
* @returns
|
|
1351
|
+
* @param then - Value when condition is true
|
|
1352
|
+
* @param else_ - Value when condition is false
|
|
1353
|
+
* @returns Conditional value expression
|
|
1354
1354
|
*
|
|
1355
1355
|
* @example
|
|
1356
1356
|
* ```typescript
|
|
@@ -1366,7 +1366,7 @@ export const expr = {
|
|
|
1366
1366
|
else_: ExprInput<T>,
|
|
1367
1367
|
): ExprUnit<T> {
|
|
1368
1368
|
const allValues = [then, else_];
|
|
1369
|
-
// 1.
|
|
1369
|
+
// 1. Find dataType from ExprUnit
|
|
1370
1370
|
const exprUnit = allValues.find((v): v is ExprUnit<T> => v instanceof ExprUnit);
|
|
1371
1371
|
if (exprUnit) {
|
|
1372
1372
|
return new ExprUnit(exprUnit.dataType, {
|
|
@@ -1377,7 +1377,7 @@ export const expr = {
|
|
|
1377
1377
|
});
|
|
1378
1378
|
}
|
|
1379
1379
|
|
|
1380
|
-
// 2. non-null
|
|
1380
|
+
// 2. Infer from non-null literal
|
|
1381
1381
|
const nonNullLiteral = allValues.find((v) => v != null) as ColumnPrimitive;
|
|
1382
1382
|
if (nonNullLiteral == null) {
|
|
1383
1383
|
throw new Error("At least one of if's then/else must be non-null.");
|
|
@@ -1394,21 +1394,21 @@ export const expr = {
|
|
|
1394
1394
|
//#endregion
|
|
1395
1395
|
|
|
1396
1396
|
//#region ========== SELECT - Aggregate ==========
|
|
1397
|
-
// SUM, AVG, MAX
|
|
1397
|
+
// Aggregates like SUM, AVG, MAX return NULL only when all values are NULL or no rows exist (rows with NULL values are ignored)
|
|
1398
1398
|
|
|
1399
1399
|
/**
|
|
1400
|
-
*
|
|
1400
|
+
* Count rows (COUNT)
|
|
1401
1401
|
*
|
|
1402
|
-
* @param arg -
|
|
1403
|
-
* @param distinct - true
|
|
1404
|
-
* @returns
|
|
1402
|
+
* @param arg - Column to count (all rows if omitted)
|
|
1403
|
+
* @param distinct - If true, remove duplicates
|
|
1404
|
+
* @returns Row count
|
|
1405
1405
|
*
|
|
1406
1406
|
* @example
|
|
1407
1407
|
* ```typescript
|
|
1408
|
-
* //
|
|
1408
|
+
* // Total row count
|
|
1409
1409
|
* db.user().select(() => ({ total: expr.count() }))
|
|
1410
1410
|
*
|
|
1411
|
-
* //
|
|
1411
|
+
* // Distinct count
|
|
1412
1412
|
* db.order().select((o) => ({
|
|
1413
1413
|
* uniqueCustomers: expr.count(o.customerId, true),
|
|
1414
1414
|
* }))
|
|
@@ -1423,12 +1423,12 @@ export const expr = {
|
|
|
1423
1423
|
},
|
|
1424
1424
|
|
|
1425
1425
|
/**
|
|
1426
|
-
*
|
|
1426
|
+
* Sum (SUM)
|
|
1427
1427
|
*
|
|
1428
|
-
* NULL
|
|
1428
|
+
* NULL values are ignored. Returns NULL if all values are NULL
|
|
1429
1429
|
*
|
|
1430
|
-
* @param arg -
|
|
1431
|
-
* @returns
|
|
1430
|
+
* @param arg - Number column to sum
|
|
1431
|
+
* @returns Sum (or NULL)
|
|
1432
1432
|
*
|
|
1433
1433
|
* @example
|
|
1434
1434
|
* ```typescript
|
|
@@ -1446,12 +1446,12 @@ export const expr = {
|
|
|
1446
1446
|
},
|
|
1447
1447
|
|
|
1448
1448
|
/**
|
|
1449
|
-
*
|
|
1449
|
+
* Average (AVG)
|
|
1450
1450
|
*
|
|
1451
|
-
* NULL
|
|
1451
|
+
* NULL values are ignored. Returns NULL if all values are NULL
|
|
1452
1452
|
*
|
|
1453
|
-
* @param arg -
|
|
1454
|
-
* @returns
|
|
1453
|
+
* @param arg - Number column to average
|
|
1454
|
+
* @returns Average (or NULL)
|
|
1455
1455
|
*
|
|
1456
1456
|
* @example
|
|
1457
1457
|
* ```typescript
|
|
@@ -1469,12 +1469,12 @@ export const expr = {
|
|
|
1469
1469
|
},
|
|
1470
1470
|
|
|
1471
1471
|
/**
|
|
1472
|
-
*
|
|
1472
|
+
* Maximum value (MAX)
|
|
1473
1473
|
*
|
|
1474
|
-
* NULL
|
|
1474
|
+
* NULL values are ignored. Returns NULL if all values are NULL
|
|
1475
1475
|
*
|
|
1476
|
-
* @param arg -
|
|
1477
|
-
* @returns
|
|
1476
|
+
* @param arg - Column to find maximum of
|
|
1477
|
+
* @returns Maximum value (or NULL)
|
|
1478
1478
|
*
|
|
1479
1479
|
* @example
|
|
1480
1480
|
* ```typescript
|
|
@@ -1492,12 +1492,12 @@ export const expr = {
|
|
|
1492
1492
|
},
|
|
1493
1493
|
|
|
1494
1494
|
/**
|
|
1495
|
-
*
|
|
1495
|
+
* Minimum value (MIN)
|
|
1496
1496
|
*
|
|
1497
|
-
* NULL
|
|
1497
|
+
* NULL values are ignored. Returns NULL if all values are NULL
|
|
1498
1498
|
*
|
|
1499
|
-
* @param arg -
|
|
1500
|
-
* @returns
|
|
1499
|
+
* @param arg - Column to find minimum of
|
|
1500
|
+
* @returns Minimum value (or NULL)
|
|
1501
1501
|
*
|
|
1502
1502
|
* @example
|
|
1503
1503
|
* ```typescript
|
|
@@ -1519,10 +1519,10 @@ export const expr = {
|
|
|
1519
1519
|
//#region ========== SELECT - Other ==========
|
|
1520
1520
|
|
|
1521
1521
|
/**
|
|
1522
|
-
*
|
|
1522
|
+
* Greatest value among multiple values (GREATEST)
|
|
1523
1523
|
*
|
|
1524
|
-
* @param args -
|
|
1525
|
-
* @returns
|
|
1524
|
+
* @param args - Values to compare
|
|
1525
|
+
* @returns Greatest value
|
|
1526
1526
|
*
|
|
1527
1527
|
* @example
|
|
1528
1528
|
* ```typescript
|
|
@@ -1540,10 +1540,10 @@ export const expr = {
|
|
|
1540
1540
|
},
|
|
1541
1541
|
|
|
1542
1542
|
/**
|
|
1543
|
-
*
|
|
1543
|
+
* Least value among multiple values (LEAST)
|
|
1544
1544
|
*
|
|
1545
|
-
* @param args -
|
|
1546
|
-
* @returns
|
|
1545
|
+
* @param args - Values to compare
|
|
1546
|
+
* @returns Least value
|
|
1547
1547
|
*
|
|
1548
1548
|
* @example
|
|
1549
1549
|
* ```typescript
|
|
@@ -1561,9 +1561,9 @@ export const expr = {
|
|
|
1561
1561
|
},
|
|
1562
1562
|
|
|
1563
1563
|
/**
|
|
1564
|
-
*
|
|
1564
|
+
* Row number (sequential number for all rows without ROW_NUMBER)
|
|
1565
1565
|
*
|
|
1566
|
-
* @returns
|
|
1566
|
+
* @returns Row number (starting from 1)
|
|
1567
1567
|
*
|
|
1568
1568
|
* @example
|
|
1569
1569
|
* ```typescript
|
|
@@ -1580,15 +1580,15 @@ export const expr = {
|
|
|
1580
1580
|
},
|
|
1581
1581
|
|
|
1582
1582
|
/**
|
|
1583
|
-
*
|
|
1583
|
+
* Generate random number (RAND/RANDOM)
|
|
1584
1584
|
*
|
|
1585
|
-
* 0
|
|
1585
|
+
* Returns a random number between 0 and 1. Mainly used for random ordering in ORDER BY
|
|
1586
1586
|
*
|
|
1587
|
-
* @returns 0
|
|
1587
|
+
* @returns Random number between 0 and 1
|
|
1588
1588
|
*
|
|
1589
1589
|
* @example
|
|
1590
1590
|
* ```typescript
|
|
1591
|
-
* //
|
|
1591
|
+
* // Random sorting
|
|
1592
1592
|
* db.user().orderBy(() => expr.random()).limit(10)
|
|
1593
1593
|
* ```
|
|
1594
1594
|
*/
|
|
@@ -1601,9 +1601,9 @@ export const expr = {
|
|
|
1601
1601
|
/**
|
|
1602
1602
|
* type transformation (CAST)
|
|
1603
1603
|
*
|
|
1604
|
-
* @param source -
|
|
1605
|
-
* @param targetType -
|
|
1606
|
-
* @returns
|
|
1604
|
+
* @param source - Expression to transform
|
|
1605
|
+
* @param targetType - Target data type
|
|
1606
|
+
* @returns Transformed expression
|
|
1607
1607
|
*
|
|
1608
1608
|
* @example
|
|
1609
1609
|
* ```typescript
|
|
@@ -1625,12 +1625,12 @@ export const expr = {
|
|
|
1625
1625
|
},
|
|
1626
1626
|
|
|
1627
1627
|
/**
|
|
1628
|
-
*
|
|
1628
|
+
* Scalar Subquery - Subquery that returns a single value in SELECT clause
|
|
1629
1629
|
*
|
|
1630
|
-
*
|
|
1630
|
+
* The subquery must return exactly one row and one column
|
|
1631
1631
|
*
|
|
1632
1632
|
* @param dataType - Data type of the returned value
|
|
1633
|
-
* @param queryable -
|
|
1633
|
+
* @param queryable - Queryable that returns a scalar value
|
|
1634
1634
|
* @returns Subquery result expression
|
|
1635
1635
|
*
|
|
1636
1636
|
* @example
|
|
@@ -1662,12 +1662,12 @@ export const expr = {
|
|
|
1662
1662
|
//#region ========== SELECT - Window Functions ==========
|
|
1663
1663
|
|
|
1664
1664
|
/**
|
|
1665
|
-
* ROW_NUMBER() -
|
|
1665
|
+
* ROW_NUMBER() - Row number within a partition
|
|
1666
1666
|
*
|
|
1667
|
-
*
|
|
1667
|
+
* Assigns sequential numbers starting from 1 within each partition
|
|
1668
1668
|
*
|
|
1669
1669
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1670
|
-
* @returns
|
|
1670
|
+
* @returns Row number (starting from 1)
|
|
1671
1671
|
*
|
|
1672
1672
|
* @example
|
|
1673
1673
|
* ```typescript
|
|
@@ -1690,10 +1690,10 @@ export const expr = {
|
|
|
1690
1690
|
},
|
|
1691
1691
|
|
|
1692
1692
|
/**
|
|
1693
|
-
* RANK() -
|
|
1693
|
+
* RANK() - Rank within a partition (ties get same rank, next rank is skipped)
|
|
1694
1694
|
*
|
|
1695
1695
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1696
|
-
* @returns
|
|
1696
|
+
* @returns Rank (skips after ties: 1, 1, 3)
|
|
1697
1697
|
*
|
|
1698
1698
|
* @example
|
|
1699
1699
|
* ```typescript
|
|
@@ -1714,10 +1714,10 @@ export const expr = {
|
|
|
1714
1714
|
},
|
|
1715
1715
|
|
|
1716
1716
|
/**
|
|
1717
|
-
* DENSE_RANK() -
|
|
1717
|
+
* DENSE_RANK() - Dense rank within a partition (ties get same rank, next rank is consecutive)
|
|
1718
1718
|
*
|
|
1719
1719
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1720
|
-
* @returns
|
|
1720
|
+
* @returns Dense rank (consecutive after ties: 1, 1, 2)
|
|
1721
1721
|
*
|
|
1722
1722
|
* @example
|
|
1723
1723
|
* ```typescript
|
|
@@ -1738,15 +1738,15 @@ export const expr = {
|
|
|
1738
1738
|
},
|
|
1739
1739
|
|
|
1740
1740
|
/**
|
|
1741
|
-
* NTILE(n) -
|
|
1741
|
+
* NTILE(n) - Split partition into n groups
|
|
1742
1742
|
*
|
|
1743
|
-
* @param n -
|
|
1743
|
+
* @param n - Number of groups to split into
|
|
1744
1744
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1745
|
-
* @returns
|
|
1745
|
+
* @returns Group number (1 ~ n)
|
|
1746
1746
|
*
|
|
1747
1747
|
* @example
|
|
1748
1748
|
* ```typescript
|
|
1749
|
-
* //
|
|
1749
|
+
* // Quartile split to find top 25%
|
|
1750
1750
|
* db.user().select((u) => ({
|
|
1751
1751
|
* name: u.name,
|
|
1752
1752
|
* quartile: expr.ntile(4, {
|
|
@@ -1764,12 +1764,12 @@ export const expr = {
|
|
|
1764
1764
|
},
|
|
1765
1765
|
|
|
1766
1766
|
/**
|
|
1767
|
-
* LAG() -
|
|
1767
|
+
* LAG() - Reference value from a previous row
|
|
1768
1768
|
*
|
|
1769
|
-
* @param column -
|
|
1769
|
+
* @param column - Column to reference
|
|
1770
1770
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1771
|
-
* @param options - offset (
|
|
1772
|
-
* @returns
|
|
1771
|
+
* @param options - offset (default 1), default (default value when no previous row)
|
|
1772
|
+
* @returns Previous row's value (or default value/NULL)
|
|
1773
1773
|
*
|
|
1774
1774
|
* @example
|
|
1775
1775
|
* ```typescript
|
|
@@ -1801,12 +1801,12 @@ export const expr = {
|
|
|
1801
1801
|
},
|
|
1802
1802
|
|
|
1803
1803
|
/**
|
|
1804
|
-
* LEAD() -
|
|
1804
|
+
* LEAD() - Reference value from a following row
|
|
1805
1805
|
*
|
|
1806
|
-
* @param column -
|
|
1806
|
+
* @param column - Column to reference
|
|
1807
1807
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1808
|
-
* @param options - offset (
|
|
1809
|
-
* @returns
|
|
1808
|
+
* @param options - offset (default 1), default (default value when no following row)
|
|
1809
|
+
* @returns Following row's value (or default value/NULL)
|
|
1810
1810
|
*
|
|
1811
1811
|
* @example
|
|
1812
1812
|
* ```typescript
|
|
@@ -1838,11 +1838,11 @@ export const expr = {
|
|
|
1838
1838
|
},
|
|
1839
1839
|
|
|
1840
1840
|
/**
|
|
1841
|
-
* FIRST_VALUE() -
|
|
1841
|
+
* FIRST_VALUE() - First value in the partition/frame
|
|
1842
1842
|
*
|
|
1843
|
-
* @param column -
|
|
1843
|
+
* @param column - Column to reference
|
|
1844
1844
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1845
|
-
* @returns
|
|
1845
|
+
* @returns First value
|
|
1846
1846
|
*
|
|
1847
1847
|
* @example
|
|
1848
1848
|
* ```typescript
|
|
@@ -1867,11 +1867,11 @@ export const expr = {
|
|
|
1867
1867
|
},
|
|
1868
1868
|
|
|
1869
1869
|
/**
|
|
1870
|
-
* LAST_VALUE() -
|
|
1870
|
+
* LAST_VALUE() - Last value in the partition/frame
|
|
1871
1871
|
*
|
|
1872
|
-
* @param column -
|
|
1872
|
+
* @param column - Column to reference
|
|
1873
1873
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1874
|
-
* @returns
|
|
1874
|
+
* @returns Last value
|
|
1875
1875
|
*
|
|
1876
1876
|
* @example
|
|
1877
1877
|
* ```typescript
|
|
@@ -1896,15 +1896,15 @@ export const expr = {
|
|
|
1896
1896
|
},
|
|
1897
1897
|
|
|
1898
1898
|
/**
|
|
1899
|
-
* SUM() OVER - Window
|
|
1899
|
+
* SUM() OVER - Window sum
|
|
1900
1900
|
*
|
|
1901
|
-
* @param column -
|
|
1901
|
+
* @param column - Column to sum
|
|
1902
1902
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1903
|
-
* @returns
|
|
1903
|
+
* @returns Sum within window
|
|
1904
1904
|
*
|
|
1905
1905
|
* @example
|
|
1906
1906
|
* ```typescript
|
|
1907
|
-
* //
|
|
1907
|
+
* // Running total
|
|
1908
1908
|
* db.order().select((o) => ({
|
|
1909
1909
|
* ...o,
|
|
1910
1910
|
* runningTotal: expr.sumOver(o.amount, {
|
|
@@ -1923,15 +1923,15 @@ export const expr = {
|
|
|
1923
1923
|
},
|
|
1924
1924
|
|
|
1925
1925
|
/**
|
|
1926
|
-
* AVG() OVER - Window
|
|
1926
|
+
* AVG() OVER - Window average
|
|
1927
1927
|
*
|
|
1928
|
-
* @param column -
|
|
1928
|
+
* @param column - Column to average
|
|
1929
1929
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1930
|
-
* @returns
|
|
1930
|
+
* @returns Average within window
|
|
1931
1931
|
*
|
|
1932
1932
|
* @example
|
|
1933
1933
|
* ```typescript
|
|
1934
|
-
* //
|
|
1934
|
+
* // Moving average
|
|
1935
1935
|
* db.stock().select((s) => ({
|
|
1936
1936
|
* ...s,
|
|
1937
1937
|
* movingAvg: expr.avgOver(s.price, {
|
|
@@ -1950,11 +1950,11 @@ export const expr = {
|
|
|
1950
1950
|
},
|
|
1951
1951
|
|
|
1952
1952
|
/**
|
|
1953
|
-
* COUNT() OVER - Window
|
|
1953
|
+
* COUNT() OVER - Window count
|
|
1954
1954
|
*
|
|
1955
1955
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1956
|
-
* @param column -
|
|
1957
|
-
* @returns
|
|
1956
|
+
* @param column - Column to count (all rows if omitted)
|
|
1957
|
+
* @returns Row count within window
|
|
1958
1958
|
*
|
|
1959
1959
|
* @example
|
|
1960
1960
|
* ```typescript
|
|
@@ -1975,11 +1975,11 @@ export const expr = {
|
|
|
1975
1975
|
},
|
|
1976
1976
|
|
|
1977
1977
|
/**
|
|
1978
|
-
* MIN() OVER - Window
|
|
1978
|
+
* MIN() OVER - Window minimum
|
|
1979
1979
|
*
|
|
1980
|
-
* @param column -
|
|
1980
|
+
* @param column - Column to find minimum of
|
|
1981
1981
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1982
|
-
* @returns
|
|
1982
|
+
* @returns Minimum value within window
|
|
1983
1983
|
*
|
|
1984
1984
|
* @example
|
|
1985
1985
|
* ```typescript
|
|
@@ -2003,11 +2003,11 @@ export const expr = {
|
|
|
2003
2003
|
},
|
|
2004
2004
|
|
|
2005
2005
|
/**
|
|
2006
|
-
* MAX() OVER - Window
|
|
2006
|
+
* MAX() OVER - Window maximum
|
|
2007
2007
|
*
|
|
2008
|
-
* @param column -
|
|
2008
|
+
* @param column - Column to find maximum of
|
|
2009
2009
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
2010
|
-
* @returns
|
|
2010
|
+
* @returns Maximum value within window
|
|
2011
2011
|
*
|
|
2012
2012
|
* @example
|
|
2013
2013
|
* ```typescript
|
|
@@ -2035,9 +2035,9 @@ export const expr = {
|
|
|
2035
2035
|
//#region ========== Helper ==========
|
|
2036
2036
|
|
|
2037
2037
|
/**
|
|
2038
|
-
* ExprInput
|
|
2038
|
+
* Transform ExprInput to Expr (internal use)
|
|
2039
2039
|
*
|
|
2040
|
-
* @param value -
|
|
2040
|
+
* @param value - Value to transform
|
|
2041
2041
|
* @returns Expr JSON AST
|
|
2042
2042
|
*/
|
|
2043
2043
|
toExpr(value: ExprInput<ColumnPrimitive>): Expr {
|
|
@@ -2049,7 +2049,7 @@ export const expr = {
|
|
|
2049
2049
|
|
|
2050
2050
|
//#region ========== Internal Helpers ==========
|
|
2051
2051
|
|
|
2052
|
-
//
|
|
2052
|
+
// Return the first non-null value among multiple values (COALESCE)
|
|
2053
2053
|
function coalesce<TPrimitive extends ColumnPrimitive>(
|
|
2054
2054
|
...args: [
|
|
2055
2055
|
ExprInput<TPrimitive | undefined>,
|
|
@@ -2071,7 +2071,7 @@ function coalesce<TPrimitive extends ColumnPrimitive>(
|
|
|
2071
2071
|
|
|
2072
2072
|
function createSwitchBuilder<TPrimitive extends ColumnPrimitive>(): SwitchExprBuilder<TPrimitive> {
|
|
2073
2073
|
const cases: { when: WhereExpr; then: Expr }[] = [];
|
|
2074
|
-
const thenValues: ExprInput<TPrimitive>[] = []; // then
|
|
2074
|
+
const thenValues: ExprInput<TPrimitive>[] = []; // Store then values
|
|
2075
2075
|
|
|
2076
2076
|
return {
|
|
2077
2077
|
case(condition: WhereExprUnit, then: ExprInput<TPrimitive>): typeof this {
|
|
@@ -2084,7 +2084,7 @@ function createSwitchBuilder<TPrimitive extends ColumnPrimitive>(): SwitchExprBu
|
|
|
2084
2084
|
},
|
|
2085
2085
|
default(value: ExprInput<TPrimitive>): ExprUnit<TPrimitive> {
|
|
2086
2086
|
const allValues = [...thenValues, value];
|
|
2087
|
-
// 1.
|
|
2087
|
+
// 1. Find dataType from ExprUnit
|
|
2088
2088
|
const exprUnit = allValues.find((v): v is ExprUnit<TPrimitive> => v instanceof ExprUnit);
|
|
2089
2089
|
if (exprUnit) {
|
|
2090
2090
|
return new ExprUnit(exprUnit.dataType, {
|
|
@@ -2094,7 +2094,7 @@ function createSwitchBuilder<TPrimitive extends ColumnPrimitive>(): SwitchExprBu
|
|
|
2094
2094
|
});
|
|
2095
2095
|
}
|
|
2096
2096
|
|
|
2097
|
-
// 2. non-null
|
|
2097
|
+
// 2. Infer from non-null literal
|
|
2098
2098
|
const nonNullLiteral = allValues.find((v) => v != null) as ColumnPrimitive;
|
|
2099
2099
|
if (nonNullLiteral == null) {
|
|
2100
2100
|
throw new Error("At least one of switch's case/default must be non-null.");
|