From 721090d39ad611089ed61d6196c75cbd192afc7a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 30 Oct 2013 15:33:34 +0800 Subject: [PATCH] Document Attrs in README --- README.md | 23 +++++++++++++++++++++++ do.go | 1 + 2 files changed, 24 insertions(+) diff --git a/README.md b/README.md index b45b6e16..82841d7f 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/do.go b/do.go index 73e2801f..d78880f9 100644 --- a/do.go +++ b/do.go @@ -108,6 +108,7 @@ func (s *Do) prepareCreateSql() { strings.Join(sqls, ","), s.model.returningStr(), ) + debug(s.sql) return }