Document Attrs in README

This commit is contained in:
Jinzhu 2013-10-30 15:33:34 +08:00
parent 0a47d75870
commit 721090d39a
2 changed files with 24 additions and 0 deletions

View File

@ -93,6 +93,16 @@ db.Where(User{Name: "Jinzhu"}).FirstOrInit(&user)
db.FirstOrInit(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
// FirstOrInit With Attrs
db.Where(User{Name: "noexisting_user"}).Attrs(User{Age: 20}).FirstOrInit(&user)
//// user -> select * from users where name = 'noexisting_user';
//// user -> User{Name: "noexisting_user", Age: 20}
db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 20}).FirstOrInit(&user)
//// user -> select * from users where name = 'jinzhu';
//// user -> User{Id: 111, Name: "Jinzhu", Age: 18}
//// Do you notice the difference? The value in Attrs won't be used when search,
//// But is used to initalize the object if no record found
// FirstOrCreate
db.FirstOrCreate(&user, User{Name: "noexisting_user"})
//// user -> User{Id: 112, Name: "noexisting_user"}
@ -101,6 +111,15 @@ db.Where(User{Name: "Jinzhu"}).FirstOrCreate(&user)
db.FirstOrCreate(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
// FirstOrCreate With Attrs
db.Where(User{Name: "noexisting_user"}).Attrs(User{Age: 20}).FirstOrCreate(&user)
//// user -> select * from users where name = 'noexisting_user';
//// user -> User{Id: 112, Name: "noexisting_user", Age: 20}
db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 20}).FirstOrCreate(&user)
//// user -> select * from users where name = 'jinzhu';
//// user -> User{Id: 111, Name: "Jinzhu", Age: 18}
//// You must noticed that the Attrs is similar to FirstOrInit with Attrs, yes?
// Select
db.Select("name").Find(&users)
//// users -> select name from users;
@ -264,6 +283,10 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us
//// users1 -> select * from users where mail_type = 'TEXT';
//// users2 -> select * from deleted_users where mail_type = 'TEXT';
db.Where("email = ?", "x@example.org"').Attrs(User{FromIp: "111.111.111.111"}).FirstOrCreate(&user)
//// user -> select * from users where email = 'x@example.org'
//// (if no record found) -> INSERT INTO "users" (email,from_ip) VALUES ("x@example.org", "111.111.111.111")
// Open your mind, add more cool examples
```

1
do.go
View File

@ -108,6 +108,7 @@ func (s *Do) prepareCreateSql() {
strings.Join(sqls, ","),
s.model.returningStr(),
)
debug(s.sql)
return
}