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 | ||||
| 
 | ||||
| ## TODO | ||||
| * Order | ||||
| * Limit | ||||
| * Offset | ||||
| * Or query | ||||
|  | ||||
							
								
								
									
										8
									
								
								orm.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								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 | ||||
| } | ||||
|  | ||||
							
								
								
									
										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 | ||||
| 	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{}) { | ||||
| 	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)) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu