From bba92226bdf7a539f5f34ebe71040a5405eb1c19 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Oct 2013 11:21:33 +0800 Subject: [PATCH] Yay, order works --- README.md | 1 - orm.go | 8 ++++---- orm_test.go | 12 +++++++++++- sql.go | 29 +++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3351915c..16e6f275 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ Yet Another ORM library for Go, aims for developer friendly ## TODO -* Order * Limit * Offset * Or query diff --git a/orm.go b/orm.go index 440a7027..21a7c892 100644 --- a/orm.go +++ b/orm.go @@ -20,7 +20,7 @@ type Orm struct { driver string whereClause []map[string]interface{} selectStr string - orderStr string + orderStrs []string offsetInt int limitInt int operation string @@ -63,11 +63,11 @@ func (s *Orm) Offset(value interface{}) *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] { - s.orderStr = value + s.orderStrs = append([]string{}, value) } else { - s.orderStr = s.orderStr + value + s.orderStrs = append(s.orderStrs, value) } return s } diff --git a/orm_test.go b/orm_test.go index c01947be..fc07d219 100644 --- a/orm_test.go +++ b/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 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") + } } diff --git a/sql.go b/sql.go index 4bcea230..4b82ee21 100644 --- a/sql.go +++ b/sql.go @@ -35,7 +35,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm { } 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 } @@ -92,11 +92,20 @@ func (s *Orm) pluck(value interface{}) { rows, err := s.db.Query(s.Sql, s.SqlVars...) s.Error = err + defer rows.Close() for rows.Next() { dest := reflect.New(dest_type).Elem().Interface() 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 } @@ -143,7 +152,7 @@ func (s *Orm) updateSql(value interface{}) { "UPDATE %v SET %v %v", s.TableName, strings.Join(sets, ", "), - s.whereSql(), + s.combinedSql(), ) return @@ -155,7 +164,7 @@ func (s *Orm) update(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 } @@ -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 { s.SqlVars = append(s.SqlVars, value) return fmt.Sprintf("$%d", len(s.SqlVars))