Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include CASE WHEN expression starter #294

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dialect/mysql/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func TestSelect(t *testing.T) {
sm.Where(mysql.Quote("id").In(mysql.Arg(100, 200, 300))),
),
},
"select with case": {
ExpectedSQL: "SELECT id, name, CASE WHEN (`id` = '1') THEN 'A' ELSE 'B' END FROM users",
Query: mysql.Select(
sm.Columns(
"id",
"name",
mysql.Case().
When(mysql.Quote("id").EQ(mysql.S("1")), mysql.S("A")).
Else(mysql.S("B")),
// as
),
sm.From("users"),
),
},
"select distinct": {
ExpectedSQL: "SELECT DISTINCT id, name FROM users WHERE (`id` IN (?, ?, ?))",
ExpectedArgs: []any{100, 200, 300},
Expand Down
30 changes: 30 additions & 0 deletions dialect/mysql/starters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package mysql

import (
"context"
"io"

"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/mysql/dialect"
"github.com/stephenafamo/bob/expr"
Expand Down Expand Up @@ -100,3 +103,30 @@ func Raw(query string, args ...any) Expression {
func Cast(exp bob.Expression, typname string) Expression {
return bmod.Cast(exp, typname)
}

type CaseChain[T bob.Expression] func() expr.Case[T]
stephenafamo marked this conversation as resolved.
Show resolved Hide resolved

func (c CaseChain[T]) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return c().WriteSQL(ctx, w, d, start)
}

func Case() CaseChain[Expression] {
return CaseChain[Expression](func() expr.Case[Expression] { return expr.Case[Expression]{} })
}

func (c CaseChain[T]) When(condition, then T) CaseChain[T] {
cExpr := c()
cExpr.Whens = append(cExpr.Whens, expr.When{Condition: condition, Then: then})
return CaseChain[T](func() expr.Case[T] { return cExpr })
}

func (c CaseChain[T]) Else(then T) Expression {
cExpr := c()
cExpr.Else = then
var e dialect.Expression
return e.New(cExpr)
}
stephenafamo marked this conversation as resolved.
Show resolved Hide resolved

// func (c CaseChain[T]) As(alias string) T {
// return bmod.X(c())
// }
4 changes: 4 additions & 0 deletions expr/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ func (e Builder[T, B]) Quote(aa ...string) T {
func (e Builder[T, B]) Cast(exp bob.Expression, typname string) T {
return X[T, B](Cast(exp, typname))
}

func (e Builder[T, B]) Case(c Case[bob.Expression]) T {
return X[T, B](c)
}
58 changes: 58 additions & 0 deletions expr/case.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package expr

import (
"context"
"errors"
"io"

"github.com/stephenafamo/bob"
)

type Case[T bob.Expression] struct {
stephenafamo marked this conversation as resolved.
Show resolved Hide resolved
Whens []When
Else bob.Expression
}

type When struct {
Condition bob.Expression
Then bob.Expression
}

func (c Case[T]) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
var args []any

if c.Else == nil && len(c.Whens) == 0 {
return nil, errors.New("case must have at least one when expression")
}

io.WriteString(w, "CASE")
for _, when := range c.Whens {
io.WriteString(w, " WHEN ")
whenArgs, err := when.Condition.WriteSQL(ctx, w, d, start+len(args))
if err != nil {
return nil, err
}
args = append(args, whenArgs...)

io.WriteString(w, " THEN ")
thenArgs, err := when.Then.WriteSQL(ctx, w, d, start+len(args))
if err != nil {
return nil, err
}
args = append(args, thenArgs...)
}

if c.Else != nil {
io.WriteString(w, " ELSE ")
elseArgs, err := c.Else.WriteSQL(ctx, w, d, start+len(args))
if err != nil {
return nil, err
}
args = append(args, elseArgs...)
}
io.WriteString(w, " END")

// as

return args, nil
}