diff --git a/builder.go b/builder.go index 9b0816d..5d57200 100644 --- a/builder.go +++ b/builder.go @@ -434,29 +434,31 @@ func (b *Builder) SelectRaw(expression string, bindings ...[]interface{}) *Build // CreateSub Creates a subquery and parse it. func (b *Builder) CreateSub(query interface{}) (string, []interface{}) { - var builder *Builder - if bT, ok := query.(*Builder); ok { - builder = bT - } else if function, ok := query.(func(builder *Builder)); ok { - builder = CloneBuilderWithTable(b) - function(builder) - } else if str, ok := query.(string); ok { - return b.ParseSub(str) - } else { - panic("can not create sub") + switch v := query.(type) { + case func(builder *Builder): + builder := CloneBuilderWithTable(b) + v(builder) + return b.ParseSub(builder) + case string, Expression, *Builder: + return b.ParseSub(v) } - return b.ParseSub(builder) + + panic("can not create sub") } /* ParseSub Parse the subquery into SQL and bindings. */ func (b *Builder) ParseSub(query interface{}) (string, []interface{}) { - if s, ok := query.(string); ok { - return s, []interface{}{} - } else if builder, ok := query.(*Builder); ok { - return builder.ToSql(), builder.GetBindings() + switch v := query.(type) { + case string: + return v, []interface{}{} + case Expression: + return string(v), []interface{}{} + case *Builder: + return v.ToSql(), v.GetBindings() } + panic("A subquery must be a query builder instance, a Closure, or a string.") }