Support select

This commit is contained in:
Jinzhu 2013-10-27 09:50:11 +08:00
parent 334d05645f
commit dd719e4512
3 changed files with 21 additions and 4 deletions

View File

@ -3,10 +3,8 @@
Yet Another ORM library for Go, aims for developer friendly Yet Another ORM library for Go, aims for developer friendly
## TODO ## TODO
* Pluck
* Order * Order
* Limit * Limit
* Select
* Offset * Offset
* Or query * Or query
* Not query * Not query

View File

@ -225,7 +225,18 @@ func TestComplexWhere(t *testing.T) {
} }
} }
func TestOrder(t *testing.T) { func TestSelect(t *testing.T) {
var user User
db.Where("name = ?", "3").Select("name").Find(&user)
if user.Id != 0 {
t.Errorf("Should not got ID because I am only looking for age, %+v", user.Id)
}
if user.Name != "3" {
t.Errorf("Should got Name = 3 when searching it, %+v", user.Id)
}
}
func TestPluck(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)
debug(ages) debug(ages)

10
sql.go
View File

@ -26,7 +26,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 * FROM %v %v", s.TableName, s.whereSql()) s.Sql = fmt.Sprintf("SELECT %v FROM %v %v", s.selectSql(), s.TableName, s.whereSql())
return return
} }
@ -190,6 +190,14 @@ func (s *Orm) whereSql() (sql string) {
return return
} }
func (s *Orm) selectSql() string {
if len(s.selectStr) == 0 {
return " * "
} else {
return s.selectStr
}
}
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))