Add Last Method, And update README for method First, Find

This commit is contained in:
Jinzhu 2013-11-04 17:58:56 +08:00
parent 215b736fe1
commit e78c10690b
5 changed files with 54 additions and 4 deletions

View File

@ -118,11 +118,23 @@ db.Save(&user)
```go ```go
// Get the first record // Get the first record
db.First(&user) db.First(&user)
//// SELECT * FROM users LIMIT 1; //// SELECT * FROM users ORDER BY id LIMIT 1;
// Search table `users` are guessed from the out struct type. // Search table `users` are guessed from the out struct type.
// You are possible to specify the table name with Model() if no out struct for some methods like Pluck() // You are possible to specify the table name with Model() if no out struct for some methods like Pluck()
// Or set table name with Table(), if so, it will ignore the out struct even have it. more details following. // Or set table name with Table(), if so, it will ignore the out struct even have it. more details following.
// Get the last record
db.Last(&user)
//// SELECT * FROM users ORDER BY id DESC LIMIT 1;
// Get a record without order by primary key
db.Find(&user)
//// SELECT * FROM users LIMIT 1;
// Get first record as map
db.First(&users)
//// SELECT * FROM users LIMIT 1;
// Get All records // Get All records
db.Find(&users) db.Find(&users)
//// SELECT * FROM users; //// SELECT * FROM users;

View File

@ -175,9 +175,12 @@ func (s *Chain) Exec(sql string) *Chain {
} }
func (s *Chain) First(out interface{}, where ...interface{}) *Chain { func (s *Chain) First(out interface{}, where ...interface{}) *Chain {
do := s.do(out) s.do(out).where(where...).first()
do.limitStr = "1" return s
do.where(where...).query() }
func (s *Chain) Last(out interface{}, where ...interface{}) *Chain {
s.do(out).where(where...).last()
return s return s
} }

14
do.go
View File

@ -287,6 +287,18 @@ func (s *Do) prepareQuerySql() {
return return
} }
func (s *Do) first() {
s.limitStr = "1"
s.orderStrs = append(s.orderStrs, s.model.primaryKeyDb())
s.query()
}
func (s *Do) last() {
s.limitStr = "1"
s.orderStrs = append(s.orderStrs, s.model.primaryKeyDb()+" DESC")
s.query()
}
func (s *Do) query() { func (s *Do) query() {
var ( var (
is_slice bool is_slice bool
@ -297,6 +309,8 @@ func (s *Do) query() {
if dest_out.Kind() == reflect.Slice { if dest_out.Kind() == reflect.Slice {
is_slice = true is_slice = true
dest_type = dest_out.Type().Elem() dest_type = dest_out.Type().Elem()
} else {
s.limitStr = "1"
} }
s.prepareQuerySql() s.prepareQuerySql()

View File

@ -117,6 +117,23 @@ func TestSaveAndFind(t *testing.T) {
db.Find(&users) db.Find(&users)
} }
func TestFirstAndLast(t *testing.T) {
var user1, user2, user3, user4 User
db.First(&user1)
db.Order("id").Find(&user2)
db.Last(&user3)
db.Order("id desc").Find(&user4)
if user1.Id != user2.Id || user3.Id != user4.Id {
t.Errorf("First and Last should works correctly")
}
var users []User
db.First(&users)
if len(users) != 1 {
t.Errorf("Find first record as map")
}
}
func TestSaveAndUpdate(t *testing.T) { func TestSaveAndUpdate(t *testing.T) {
name, name2, new_name := "update", "update2", "new_update" name, name2, new_name := "update", "update2", "new_update"
user := User{Name: name, Age: 1} user := User{Name: name, Age: 1}

View File

@ -34,6 +34,10 @@ func (s *DB) First(out interface{}, where ...interface{}) *Chain {
return s.buildChain().First(out, where...) return s.buildChain().First(out, where...)
} }
func (s *DB) Last(out interface{}, where ...interface{}) *Chain {
return s.buildChain().Last(out, where...)
}
func (s *DB) Attrs(attrs ...interface{}) *Chain { func (s *DB) Attrs(attrs ...interface{}) *Chain {
return s.buildChain().Attrs(attrs...) return s.buildChain().Attrs(attrs...)
} }