Skip to content

Commit

Permalink
Exec support builder (go-xorm#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Aug 9, 2018
1 parent 4b224e8 commit 4ce90f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,10 +1333,10 @@ func (engine *Engine) DropIndexes(bean interface{}) error {
}

// Exec raw sql
func (engine *Engine) Exec(sql string, args ...interface{}) (sql.Result, error) {
func (engine *Engine) Exec(sqlorArgs ...interface{}) (sql.Result, error) {
session := engine.NewSession()
defer session.Close()
return session.Exec(sql, args...)
return session.Exec(sqlorArgs...)
}

// Query a raw sql and return records as []map[string][]byte
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Interface interface {
Delete(interface{}) (int64, error)
Distinct(columns ...string) *Session
DropIndexes(bean interface{}) error
Exec(string, ...interface{}) (sql.Result, error)
Exec(sqlOrAgrs ...interface{}) (sql.Result, error)
Exist(bean ...interface{}) (bool, error)
Find(interface{}, ...interface{}) error
FindAndCount(interface{}, ...interface{}) (int64, error)
Expand Down
12 changes: 1 addition & 11 deletions session_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@ import (

func (session *Session) genQuerySQL(sqlorArgs ...interface{}) (string, []interface{}, error) {
if len(sqlorArgs) > 0 {
switch sqlorArgs[0].(type) {
case string:
return sqlorArgs[0].(string), sqlorArgs[1:], nil
case *builder.Builder:
return sqlorArgs[0].(*builder.Builder).ToSQL()
case builder.Builder:
bd := sqlorArgs[0].(builder.Builder)
return bd.ToSQL()
default:
return "", nil, ErrUnSupportedType
}
return convertSQLOrArgs(sqlorArgs...)
}

if session.statement.RawSQL != "" {
Expand Down
26 changes: 25 additions & 1 deletion session_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"time"

"github.com/go-xorm/builder"
"github.com/go-xorm/core"
)

Expand Down Expand Up @@ -193,11 +194,34 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er
return session.DB().Exec(sqlStr, args...)
}

func convertSQLOrArgs(sqlorArgs ...interface{}) (string, []interface{}, error) {
switch sqlorArgs[0].(type) {
case string:
return sqlorArgs[0].(string), sqlorArgs[1:], nil
case *builder.Builder:
return sqlorArgs[0].(*builder.Builder).ToSQL()
case builder.Builder:
bd := sqlorArgs[0].(builder.Builder)
return bd.ToSQL()
}

return "", nil, ErrUnSupportedType
}

// Exec raw sql
func (session *Session) Exec(sqlStr string, args ...interface{}) (sql.Result, error) {
func (session *Session) Exec(sqlorArgs ...interface{}) (sql.Result, error) {
if session.isAutoClose {
defer session.Close()
}

if len(sqlorArgs) == 0 {
return nil, ErrUnSupportedType
}

sqlStr, args, err := convertSQLOrArgs(sqlorArgs...)
if err != nil {
return nil, err
}

return session.exec(sqlStr, args...)
}

0 comments on commit 4ce90f9

Please sign in to comment.