From 4ce90f9a62d8f656b6dd55d80404cf3cf8b1894e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 9 Aug 2018 13:09:54 +0800 Subject: [PATCH 1/3] Exec support builder (#1064) --- engine.go | 4 ++-- interface.go | 2 +- session_query.go | 12 +----------- session_raw.go | 26 +++++++++++++++++++++++++- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/engine.go b/engine.go index 846889c36..7a5210073 100644 --- a/engine.go +++ b/engine.go @@ -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 diff --git a/interface.go b/interface.go index 0bc12ba00..0985372dd 100644 --- a/interface.go +++ b/interface.go @@ -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) diff --git a/session_query.go b/session_query.go index 5c9aeb391..7ddd08f1e 100644 --- a/session_query.go +++ b/session_query.go @@ -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 != "" { diff --git a/session_raw.go b/session_raw.go index 69bf9b3c6..47823d670 100644 --- a/session_raw.go +++ b/session_raw.go @@ -9,6 +9,7 @@ import ( "reflect" "time" + "github.com/go-xorm/builder" "github.com/go-xorm/core" ) @@ -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...) } From 1eee8a367ea7ffa7de0cc5bd8057734c4804f9d1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 9 Aug 2018 14:37:44 +0800 Subject: [PATCH 2/3] fix #997 group by (#1065) --- engine.go | 8 ++++++++ session_find.go | 4 ++-- session_find_test.go | 9 +++++++++ session_query.go | 4 ++-- statement.go | 4 ++-- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/engine.go b/engine.go index 7a5210073..d959d01f2 100644 --- a/engine.go +++ b/engine.go @@ -177,6 +177,14 @@ func (engine *Engine) QuoteStr() string { return engine.dialect.QuoteStr() } +func (engine *Engine) quoteColumns(columnStr string) string { + columns := strings.Split(columnStr, ",") + for i := 0; i < len(columns); i++ { + columns[i] = engine.Quote(strings.TrimSpace(columns[i])) + } + return strings.Join(columns, ",") +} + // Quote Use QuoteStr quote the string sql func (engine *Engine) Quote(value string) string { value = strings.TrimSpace(value) diff --git a/session_find.go b/session_find.go index 46bbf26c9..b75f83479 100644 --- a/session_find.go +++ b/session_find.go @@ -135,7 +135,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) if session.statement.JoinStr == "" { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.statement.Engine.Quote(strings.Replace(session.statement.GroupByStr, ",", session.engine.Quote(","), -1)) + columnStr = session.engine.quoteColumns(session.statement.GroupByStr) } else { columnStr = session.statement.genColumnStr() } @@ -143,7 +143,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) } else { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.statement.Engine.Quote(strings.Replace(session.statement.GroupByStr, ",", session.engine.Quote(","), -1)) + columnStr = session.engine.quoteColumns(session.statement.GroupByStr) } else { columnStr = "*" } diff --git a/session_find_test.go b/session_find_test.go index d0ec339cf..f9ebdc913 100644 --- a/session_find_test.go +++ b/session_find_test.go @@ -268,6 +268,15 @@ func TestOrder(t *testing.T) { fmt.Println(users2) } +func TestGroupBy(t *testing.T) { + assert.NoError(t, prepareEngine()) + assertSync(t, new(Userinfo)) + + users := make([]Userinfo, 0) + err := testEngine.GroupBy("id, username").Find(&users) + assert.NoError(t, err) +} + func TestHaving(t *testing.T) { assert.NoError(t, prepareEngine()) assertSync(t, new(Userinfo)) diff --git a/session_query.go b/session_query.go index 7ddd08f1e..1d0b156bc 100644 --- a/session_query.go +++ b/session_query.go @@ -35,7 +35,7 @@ func (session *Session) genQuerySQL(sqlorArgs ...interface{}) (string, []interfa if session.statement.JoinStr == "" { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.statement.Engine.Quote(strings.Replace(session.statement.GroupByStr, ",", session.engine.Quote(","), -1)) + columnStr = session.engine.quoteColumns(session.statement.GroupByStr) } else { columnStr = session.statement.genColumnStr() } @@ -43,7 +43,7 @@ func (session *Session) genQuerySQL(sqlorArgs ...interface{}) (string, []interfa } else { if columnStr == "" { if session.statement.GroupByStr != "" { - columnStr = session.statement.Engine.Quote(strings.Replace(session.statement.GroupByStr, ",", session.engine.Quote(","), -1)) + columnStr = session.engine.quoteColumns(session.statement.GroupByStr) } else { columnStr = "*" } diff --git a/statement.go b/statement.go index 7856936f5..56644036a 100644 --- a/statement.go +++ b/statement.go @@ -933,7 +933,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, if len(statement.JoinStr) == 0 { if len(columnStr) == 0 { if len(statement.GroupByStr) > 0 { - columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) + columnStr = statement.Engine.quoteColumns(statement.GroupByStr) } else { columnStr = statement.genColumnStr() } @@ -941,7 +941,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, } else { if len(columnStr) == 0 { if len(statement.GroupByStr) > 0 { - columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1)) + columnStr = statement.Engine.quoteColumns(statement.GroupByStr) } } } From 0b0a419f599b8006e9a0cb74b364b6713f62269d Mon Sep 17 00:00:00 2001 From: qianguozheng Date: Thu, 9 Aug 2018 21:07:07 +0800 Subject: [PATCH 3/3] Fix compile error (#1042) engine.ShowSQL is a method, not variable anymore --- examples/goroutine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/goroutine.go b/examples/goroutine.go index 59e56d105..0596ec23e 100644 --- a/examples/goroutine.go +++ b/examples/goroutine.go @@ -91,7 +91,7 @@ func main() { fmt.Println(err) return } - engine.ShowSQL = true + engine.ShowSQL(true) fmt.Println(engine) test(engine) fmt.Println("test end")