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

feat(expressions): add NULLIF helper #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ func COALESCE(vals ...interface{}) exp.SQLFunctionExpression {
return Func("COALESCE", vals...)
}

// Creates a new NULLIF sql function
//
// NULLIF(I("a"), "a") -> NULLIF("a", 'a')
// NULLIF("a", "a") -> NULLIF("a", 'a')
func NULLIF(column interface{}, value interface{}) exp.SQLFunctionExpression {
return Func("NULLIF", column, value)
}

//nolint:stylecheck,revive // sql function name
func ROW_NUMBER() exp.SQLFunctionExpression {
return Func("ROW_NUMBER")
Expand Down
15 changes: 15 additions & 0 deletions expressions_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,21 @@ func ExampleMIN_havingClause() {
// SELECT MIN("a") AS "MIN" FROM "test" GROUP BY "a" HAVING (MIN("a") > ?) [10]
}

func ExampleNULLIF() {
ds := goqu.From("test").Select(
goqu.NULLIF(goqu.C("a"), "a"),
goqu.NULLIF(goqu.C("a"), goqu.C("b")),
)
sql, args, _ := ds.ToSQL()
fmt.Println(sql, args)

sql, args, _ = ds.Prepared(true).ToSQL()
fmt.Println(sql, args)
// Output:
// SELECT NULLIF("a", 'a'), NULLIF("a", "b") FROM "test" []
// SELECT NULLIF("a", ?), NULLIF("a", "b") FROM "test" [a]
}

func ExampleOn() {
ds := goqu.From("test").Join(
goqu.T("my_table"),
Expand Down
4 changes: 4 additions & 0 deletions expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (ges *goquExpressionsSuite) TestCOALESCE() {
ges.Equal(exp.NewSQLFunctionExpression("COALESCE", goqu.I("col"), nil), goqu.COALESCE(goqu.I("col"), nil))
}

func (ges *goquExpressionsSuite) TestNULLIF() {
ges.Equal(exp.NewSQLFunctionExpression("NULLIF", goqu.I("col"), "test"), goqu.NULLIF(goqu.I("col"), "test"))
}

func (ges *goquExpressionsSuite) TestROW_NUMBER() {
ges.Equal(exp.NewSQLFunctionExpression("ROW_NUMBER"), goqu.ROW_NUMBER())
}
Expand Down