diff --git a/expressions.go b/expressions.go index 2b1eb23a..f07bf996 100644 --- a/expressions.go +++ b/expressions.go @@ -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") diff --git a/expressions_example_test.go b/expressions_example_test.go index 222701ee..7f69be3f 100644 --- a/expressions_example_test.go +++ b/expressions_example_test.go @@ -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"), diff --git a/expressions_test.go b/expressions_test.go index 7a25a638..ab314bfd 100644 --- a/expressions_test.go +++ b/expressions_test.go @@ -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()) }