refactor notificationsForUser since xorm In support slice of customerize type (#956)

This commit is contained in:
Lunny Xiao 2017-02-16 12:07:00 +08:00 committed by GitHub
parent 0642cb330c
commit 46320f9630
3 changed files with 27 additions and 14 deletions

View file

@ -189,23 +189,20 @@ func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPa
return notificationsForUser(x, user, statuses, page, perPage) return notificationsForUser(x, user, statuses, page, perPage)
} }
func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) { func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
// FIXME: Xorm does not support aliases types (like NotificationStatus) on In() method if len(statuses) == 0 {
s := make([]uint8, len(statuses)) return
for i, status := range statuses {
s[i] = uint8(status)
} }
sess := e. sess := e.
Where("user_id = ?", user.ID). Where("user_id = ?", user.ID).
In("status", s). In("status", statuses).
OrderBy("updated_unix DESC") OrderBy("updated_unix DESC")
if page > 0 && perPage > 0 { if page > 0 && perPage > 0 {
sess.Limit(perPage, (page-1)*perPage) sess.Limit(perPage, (page-1)*perPage)
} }
err = sess. err = sess.Find(&notifications)
Find(&notifications)
return return
} }

View file

@ -6,6 +6,7 @@ package builder
import ( import (
"fmt" "fmt"
"reflect"
"strings" "strings"
) )
@ -195,11 +196,26 @@ func (condIn condIn) WriteTo(w Writer) error {
if len(condIn.vals) <= 0 { if len(condIn.vals) <= 0 {
return ErrNoInConditions return ErrNoInConditions
} }
questionMark := strings.Repeat("?,", len(condIn.vals))
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil { v := reflect.ValueOf(condIn.vals[0])
return err if v.Kind() == reflect.Slice {
l := v.Len()
questionMark := strings.Repeat("?,", l)
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
return err
}
for i := 0; i < l; i++ {
w.Append(v.Index(i).Interface())
}
} else {
questionMark := strings.Repeat("?,", len(condIn.vals))
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
return err
}
w.Append(condIn.vals...)
} }
w.Append(condIn.vals...)
} }
return nil return nil
} }

6
vendor/vendor.json vendored
View file

@ -437,10 +437,10 @@
"revisionTime": "2016-11-01T11:13:14Z" "revisionTime": "2016-11-01T11:13:14Z"
}, },
{ {
"checksumSHA1": "sDRC697KuCbpI+1i2VPhyqgAvjs=", "checksumSHA1": "Fh6Svimt+QyXHbaVxgSV7qwUHL8=",
"path": "github.com/go-xorm/builder", "path": "github.com/go-xorm/builder",
"revision": "db75972580de4a7c6c20fff5b16a924c3de3fa12", "revision": "9c357861b643b7dd1023551fdf116b8d42030146",
"revisionTime": "2016-12-14T02:05:24Z" "revisionTime": "2017-02-16T03:03:40Z"
}, },
{ {
"checksumSHA1": "OCcksAYN5m0kc5yJF/Ba4VVHqeA=", "checksumSHA1": "OCcksAYN5m0kc5yJF/Ba4VVHqeA=",