Skip to content

Commit

Permalink
fix go-xorm#997 group by (go-xorm#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Aug 9, 2018
1 parent 4ce90f9 commit 1eee8a3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
8 changes: 8 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions session_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ 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()
}
}
} 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 = "*"
}
Expand Down
9 changes: 9 additions & 0 deletions session_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions session_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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()
}
}
} 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 = "*"
}
Expand Down
4 changes: 2 additions & 2 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,15 +933,15 @@ 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()
}
}
} 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)
}
}
}
Expand Down

0 comments on commit 1eee8a3

Please sign in to comment.