@@ -8,51 +8,76 @@ import (
8
8
"github.com/stephenafamo/bob"
9
9
)
10
10
11
- type Case [T bob.Expression ] struct {
12
- Whens []When
13
- Else bob.Expression
14
- }
15
-
16
- type When struct {
17
- Condition bob.Expression
18
- Then bob.Expression
19
- }
11
+ type (
12
+ caseExpr struct {
13
+ whens []when
14
+ elseExpr bob.Expression
15
+ }
16
+ when struct {
17
+ condition bob.Expression
18
+ then bob.Expression
19
+ }
20
+ )
20
21
21
- func (c Case [ T ] ) WriteSQL (ctx context.Context , w io.Writer , d bob.Dialect , start int ) ([]any , error ) {
22
+ func (c caseExpr ) WriteSQL (ctx context.Context , w io.Writer , d bob.Dialect , start int ) ([]any , error ) {
22
23
var args []any
23
24
24
- if c . Else == nil && len (c .Whens ) == 0 {
25
+ if len (c .whens ) == 0 {
25
26
return nil , errors .New ("case must have at least one when expression" )
26
27
}
27
28
28
- io . WriteString ( w , "CASE" )
29
- for _ , when := range c .Whens {
30
- io . WriteString ( w , " WHEN " )
31
- whenArgs , err := when .Condition .WriteSQL (ctx , w , d , start + len (args ))
29
+ w . Write ([] byte ( "CASE" ) )
30
+ for _ , when := range c .whens {
31
+ w . Write ([] byte ( " WHEN " ) )
32
+ whenArgs , err := when .condition .WriteSQL (ctx , w , d , start + len (args ))
32
33
if err != nil {
33
34
return nil , err
34
35
}
35
36
args = append (args , whenArgs ... )
36
37
37
- io . WriteString ( w , " THEN " )
38
- thenArgs , err := when .Then .WriteSQL (ctx , w , d , start + len (args ))
38
+ w . Write ([] byte ( " THEN " ) )
39
+ thenArgs , err := when .then .WriteSQL (ctx , w , d , start + len (args ))
39
40
if err != nil {
40
41
return nil , err
41
42
}
42
43
args = append (args , thenArgs ... )
43
44
}
44
45
45
- if c .Else != nil {
46
- io . WriteString ( w , " ELSE " )
47
- elseArgs , err := c .Else .WriteSQL (ctx , w , d , start + len (args ))
46
+ if c .elseExpr != nil {
47
+ w . Write ([] byte ( " ELSE " ) )
48
+ elseArgs , err := c .elseExpr .WriteSQL (ctx , w , d , start + len (args ))
48
49
if err != nil {
49
50
return nil , err
50
51
}
51
52
args = append (args , elseArgs ... )
52
53
}
53
- io .WriteString (w , " END" )
54
-
55
- // as
54
+ w .Write ([]byte (" END" ))
56
55
57
56
return args , nil
58
57
}
58
+
59
+ type CaseChain [T bob.Expression , B builder [T ]] func () caseExpr
60
+
61
+ func NewCase [T bob.Expression , B builder [T ]]() CaseChain [T , B ] {
62
+ return CaseChain [T , B ](func () caseExpr { return caseExpr {} })
63
+ }
64
+
65
+ func (cc CaseChain [T , B ]) WriteSQL (ctx context.Context , w io.Writer , d bob.Dialect , start int ) ([]any , error ) {
66
+ return cc ().WriteSQL (ctx , w , d , start )
67
+ }
68
+
69
+ func (cc CaseChain [T , B ]) When (condition , then bob.Expression ) CaseChain [T , B ] {
70
+ c := cc ()
71
+ c .whens = append (c .whens , when {condition : condition , then : then })
72
+ return CaseChain [T , B ](func () caseExpr { return c })
73
+ }
74
+
75
+ func (cc CaseChain [T , B ]) Else (then bob.Expression ) T {
76
+ c := cc ()
77
+ c .elseExpr = then
78
+ return X [T , B ](c )
79
+ }
80
+
81
+ func (cc CaseChain [T , B ]) End () T {
82
+ return X [T , B ](cc ())
83
+ }
0 commit comments