Skip to content

Commit

Permalink
Add missing MustSql methods (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
swithek authored Jun 4, 2021
1 parent 2499a26 commit cd1fe0a
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 9 deletions.
10 changes: 10 additions & 0 deletions case.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func (b CaseBuilder) ToSql() (string, []interface{}, error) {
return data.ToSql()
}

// MustSql builds the query into a SQL string and bound args.
// It panics if there are any errors.
func (b CaseBuilder) MustSql() (string, []interface{}) {
sql, args, err := b.ToSql()
if err != nil {
panic(err)
}
return sql, args
}

// what sets optional value for CASE construct "CASE [value] ..."
func (b CaseBuilder) what(expr interface{}) CaseBuilder {
return builder.Set(b, "What", newPart(expr)).(CaseBuilder)
Expand Down
9 changes: 9 additions & 0 deletions case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@ func TestCaseWithNoWhenClause(t *testing.T) {

assert.Equal(t, "case expression must contain at lease one WHEN clause", err.Error())
}

func TestCaseBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestCaseBuilderMustSql should have panicked!")
}
}()
Case("").MustSql()
}
10 changes: 10 additions & 0 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ func (b DeleteBuilder) ToSql() (string, []interface{}, error) {
return data.ToSql()
}

// MustSql builds the query into a SQL string and bound args.
// It panics if there are any errors.
func (b DeleteBuilder) MustSql() (string, []interface{}) {
sql, args, err := b.ToSql()
if err != nil {
panic(err)
}
return sql, args
}

// Prefix adds an expression to the beginning of the query
func (b DeleteBuilder) Prefix(sql string, args ...interface{}) DeleteBuilder {
return b.PrefixExpr(Expr(sql, args...))
Expand Down
9 changes: 9 additions & 0 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func TestDeleteBuilderToSqlErr(t *testing.T) {
assert.Error(t, err)
}

func TestDeleteBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestDeleteBuilderMustSql should have panicked!")
}
}()
Delete("").MustSql()
}

func TestDeleteBuilderPlaceholders(t *testing.T) {
b := Delete("test").Where("x = ? AND y = ?", 1, 2)

Expand Down
10 changes: 10 additions & 0 deletions insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ func (b InsertBuilder) ToSql() (string, []interface{}, error) {
return data.ToSql()
}

// MustSql builds the query into a SQL string and bound args.
// It panics if there are any errors.
func (b InsertBuilder) MustSql() (string, []interface{}) {
sql, args, err := b.ToSql()
if err != nil {
panic(err)
}
return sql, args
}

// Prefix adds an expression to the beginning of the query
func (b InsertBuilder) Prefix(sql string, args ...interface{}) InsertBuilder {
return b.PrefixExpr(Expr(sql, args...))
Expand Down
9 changes: 9 additions & 0 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func TestInsertBuilderToSqlErr(t *testing.T) {
assert.Error(t, err)
}

func TestInsertBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestInsertBuilderMustSql should have panicked!")
}
}()
Insert("").MustSql()
}

func TestInsertBuilderPlaceholders(t *testing.T) {
b := Insert("test").Values(1, 2)

Expand Down
2 changes: 2 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func (b SelectBuilder) ToSql() (string, []interface{}, error) {
return data.ToSql()
}

// MustSql builds the query into a SQL string and bound args.
// It panics if there are any errors.
func (b SelectBuilder) MustSql() (string, []interface{}) {
sql, args, err := b.ToSql()
if err != nil {
Expand Down
16 changes: 7 additions & 9 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,14 @@ func TestSelectBuilderNestedSelectDollar(t *testing.T) {
assert.Equal(t, "SELECT * FROM foo WHERE x = $1 AND NOT EXISTS ( SELECT * FROM bar WHERE y = $2 )", outerSql)
}

func TestMustSql(t *testing.T) {
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestUserFail should have panicked!")
}
}()
// This function should cause a panic
Select().From("foo").MustSql()
func TestSelectBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestSelectBuilderMustSql should have panicked!")
}
}()
// This function should cause a panic
Select().From("foo").MustSql()
}

func TestSelectWithoutWhereClause(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ func (b UpdateBuilder) ToSql() (string, []interface{}, error) {
return data.ToSql()
}

// MustSql builds the query into a SQL string and bound args.
// It panics if there are any errors.
func (b UpdateBuilder) MustSql() (string, []interface{}) {
sql, args, err := b.ToSql()
if err != nil {
panic(err)
}
return sql, args
}

// Prefix adds an expression to the beginning of the query
func (b UpdateBuilder) Prefix(sql string, args ...interface{}) UpdateBuilder {
return b.PrefixExpr(Expr(sql, args...))
Expand Down
9 changes: 9 additions & 0 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func TestUpdateBuilderToSqlErr(t *testing.T) {
assert.Error(t, err)
}

func TestUpdateBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestUpdateBuilderMustSql should have panicked!")
}
}()
Update("").MustSql()
}

func TestUpdateBuilderPlaceholders(t *testing.T) {
b := Update("test").SetMap(Eq{"x": 1, "y": 2})

Expand Down

0 comments on commit cd1fe0a

Please sign in to comment.