mirror of https://github.com/go-gorm/gorm.git
Yay, order works
This commit is contained in:
parent
6511cad317
commit
bba92226bd
|
@ -3,7 +3,6 @@
|
||||||
Yet Another ORM library for Go, aims for developer friendly
|
Yet Another ORM library for Go, aims for developer friendly
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
* Order
|
|
||||||
* Limit
|
* Limit
|
||||||
* Offset
|
* Offset
|
||||||
* Or query
|
* Or query
|
||||||
|
|
8
orm.go
8
orm.go
|
@ -20,7 +20,7 @@ type Orm struct {
|
||||||
driver string
|
driver string
|
||||||
whereClause []map[string]interface{}
|
whereClause []map[string]interface{}
|
||||||
selectStr string
|
selectStr string
|
||||||
orderStr string
|
orderStrs []string
|
||||||
offsetInt int
|
offsetInt int
|
||||||
limitInt int
|
limitInt int
|
||||||
operation string
|
operation string
|
||||||
|
@ -63,11 +63,11 @@ func (s *Orm) Offset(value interface{}) *Orm {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) Order(value string, reorder ...bool) *Orm {
|
func (s *Orm) Order(value string, reorder ...bool) *Orm {
|
||||||
defer s.validSql(s.orderStr)
|
defer s.validSql(value)
|
||||||
if len(reorder) > 0 && reorder[0] {
|
if len(reorder) > 0 && reorder[0] {
|
||||||
s.orderStr = value
|
s.orderStrs = append([]string{}, value)
|
||||||
} else {
|
} else {
|
||||||
s.orderStr = s.orderStr + value
|
s.orderStrs = append(s.orderStrs, value)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
12
orm_test.go
12
orm_test.go
|
@ -241,7 +241,17 @@ func TestSelect(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPluck(t *testing.T) {
|
func TestOrderAndPluck(t *testing.T) {
|
||||||
var ages []int64
|
var ages []int64
|
||||||
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages)
|
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages)
|
||||||
|
if ages[0] != 26 {
|
||||||
|
t.Errorf("The first age should be 26 because of ordered by")
|
||||||
|
}
|
||||||
|
|
||||||
|
ages = []int64{}
|
||||||
|
var names []string
|
||||||
|
db.Model(&User{}).Order("name").Order("age desc").Pluck("age", &ages).Pluck("name", &names)
|
||||||
|
if !(names[0] == "1" && names[2] == "3" && names[3] == "3" && ages[2] == 24 && ages[3] == 22) {
|
||||||
|
t.Errorf("Should be ordered correctly with multiple orders")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
29
sql.go
29
sql.go
|
@ -35,7 +35,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) querySql(out interface{}) {
|
func (s *Orm) querySql(out interface{}) {
|
||||||
s.Sql = fmt.Sprintf("SELECT %v FROM %v %v", s.selectSql(), s.TableName, s.whereSql())
|
s.Sql = fmt.Sprintf("SELECT %v FROM %v %v", s.selectSql(), s.TableName, s.combinedSql())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,11 +92,20 @@ func (s *Orm) pluck(value interface{}) {
|
||||||
|
|
||||||
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
rows, err := s.db.Query(s.Sql, s.SqlVars...)
|
||||||
s.Error = err
|
s.Error = err
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
dest := reflect.New(dest_type).Elem().Interface()
|
dest := reflect.New(dest_type).Elem().Interface()
|
||||||
s.Error = rows.Scan(&dest)
|
s.Error = rows.Scan(&dest)
|
||||||
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
switch dest.(type) {
|
||||||
|
case []uint8:
|
||||||
|
if dest_type.String() == "string" {
|
||||||
|
dest = string(dest.([]uint8))
|
||||||
|
}
|
||||||
|
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
||||||
|
default:
|
||||||
|
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -143,7 +152,7 @@ func (s *Orm) updateSql(value interface{}) {
|
||||||
"UPDATE %v SET %v %v",
|
"UPDATE %v SET %v %v",
|
||||||
s.TableName,
|
s.TableName,
|
||||||
strings.Join(sets, ", "),
|
strings.Join(sets, ", "),
|
||||||
s.whereSql(),
|
s.combinedSql(),
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -155,7 +164,7 @@ func (s *Orm) update(value interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) deleteSql(value interface{}) {
|
func (s *Orm) deleteSql(value interface{}) {
|
||||||
s.Sql = fmt.Sprintf("DELETE FROM %v %v", s.TableName, s.whereSql())
|
s.Sql = fmt.Sprintf("DELETE FROM %v %v", s.TableName, s.combinedSql())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,6 +215,18 @@ func (s *Orm) selectSql() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Orm) orderSql() (str string) {
|
||||||
|
if len(s.orderStrs) == 0 {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return " ORDER BY " + strings.Join(s.orderStrs, ",")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Orm) combinedSql() string {
|
||||||
|
return s.whereSql() + s.orderSql()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Orm) addToVars(value interface{}) string {
|
func (s *Orm) addToVars(value interface{}) string {
|
||||||
s.SqlVars = append(s.SqlVars, value)
|
s.SqlVars = append(s.SqlVars, value)
|
||||||
return fmt.Sprintf("$%d", len(s.SqlVars))
|
return fmt.Sprintf("$%d", len(s.SqlVars))
|
||||||
|
|
Loading…
Reference in New Issue