From cee203b0d3d391c54e81ab9fb92dfabd9828e6d6 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Oct 2013 22:18:06 +0800 Subject: [PATCH] Update README --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++---------- chain.go | 8 ++++-- orm_test.go | 2 +- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 51257e75..39498722 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Yet Another ORM library for Go, aims for developer friendly -## USAGE +## Basic Usage ```go db, _ = Open("postgres", "user=gorm dbname=gorm sslmode=disable") @@ -39,41 +39,62 @@ db.Where("name = ? and age >= ?", "3", "22").Find(&users) db.Where("name in (?)", []string["jinzhu", "jinzhu 2"]).Find(&users) db.Where("birthday < ?", time.Now()).Find(&users) +// Inline search condition +db.First(&user, 23) // select * from users where id = 23 limit 1; +db.First(&user, "name = ?", "jinzhu") // select * from users where name = "jinzhu" limit 1; +db.Find(&users, "name = ?", "jinzhu") // select * from users where name = "jinzhu"; +db.Find(&users, "name <> ? and age > ?", "jinzhu", 20) // select * from users where name <> "jinzhu" and age > 20; + // Select db.Select("name").Find(&users) // Order db.Order("age desc, name").Find(&users) +db.Order("age desc").Order("name").Find(&users) // Limit db.Limit(3).Find(&users) +db.Limit(10).Find(&ten_users).Limit(20).Find(&twenty_users).Limit(-1).Find(&all_users) // Offset db.Offset(3).Find(&users) +db.Offset(10).Find(&users).Offset(20).Find(&users).Offset(-1).Find(&users) // Or -db.Where("name = ?", "Jinzhu").Or("name = ?", "Jinzhu 2").Find(&users) +db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users) // Count -db.Where("name = ?", "1").Or("name = ?", "3").Find(&users).Count(&count) -db.Model(&User{}).Where("name = ?", "1").Count(&count) +db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count) +db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count) // CreatedAt (auto insert current time on create) -If your struct has field CreatedAt, it will be filled with the current time when insert into database +If your struct has field CreatedAt, +it will be filled with the current time when insert into database // UpdatedAt (auto update the time on save) -If your struct has field UpdatedAt, it will be filled with the current time when update it +If your struct has field UpdatedAt, +it will be filled with the current time when update it // Callbacks -Below callbacks defined now: -BeforeCreate, BeforeUpdate, BeforeSave, AfterCreate, AfterUpdate, AfterSave -BeforeDelete, AfterDelete +Below callbacks are defined now: + +`BeforeCreate`, `BeforeUpdate`, `BeforeSave`, `AfterCreate`, `AfterUpdate`, `AfterSave` +`BeforeDelete`, `AfterDelete` Callbacks is a function defined to a model, if the function return error, will prevent the database operations. -// Pluck (get all users's age as map) + func (u *User) BeforeUpdate() (err error) { + if u.readonly() { + err = errors.New("Read Only User") + } + return + } + +// Pluck (get users's age as map) var ages []int64 -db.Model(&User{}).Pluck("age", &ages) +db.Find(&users).Pluck("age", &ages) +var names []string +db.Model(&User{}).Pluck("name", &names) // Query Chains db.Where("name <> ?", "jinzhu").Where("age >= ? and role <> ?", 20, "admin").Find(&users) @@ -85,10 +106,33 @@ db.CreateTable(&User{}) db.Exec("drop table users;") ``` +## Cool Examples + +```go +// Already excited about the basic usage? Let's see some magic! + +db.First(&first_article).Count(&total_count).Limit(10).Find(&first_page_articles).Offset(10).Find(&second_page_articles) +// first_article return the latest article +// total_count return the total numbers of articles +// first_page_articles return the latest 10 articles +// second_page_articles return the latest 10 to 20 articles + +db.Where("created_at > ?", "2013/10/10").Find(&cancelled_orders, "state = ?", "cancelled").Find(&shipped_orders, "state = ?", "shipped") +// cancelled_orders return all cancelled orders since 2013/10/10 +// shipped_orders return all shipped orders since 2013/10/10 + +db.Model(&Order{}).Where("amount > ?", 10000).Pluck("user_id", &paid_user_ids) +db.Where("user_id = ?", paid_user_ids).Find(&:paid_users) + +db.Where("product_name = ?", "fancy_product").Find(&orders).Find(&shopping_cart) + +// Open your mind, add more cool examples +``` + ## TODO -* Update, Updates +* Update, Updates like rails's update_attribute, update_attributes * Soft Delete -* Even more complex where query (with map or struct) +* Query with map or struct * FindOrInitialize / FindOrCreate * SQL Log * Auto Migration diff --git a/chain.go b/chain.go index 399538c7..17c6cc92 100644 --- a/chain.go +++ b/chain.go @@ -146,7 +146,9 @@ func (s *Chain) Exec(sql string) *Chain { } func (s *Chain) First(out interface{}, where ...interface{}) *Chain { - s.do(out).query(where...) + do := s.do(out) + do.limitStr = "1" + do.query(where...) return s } @@ -156,7 +158,9 @@ func (s *Chain) Find(out interface{}, where ...interface{}) *Chain { } func (s *Chain) Pluck(column string, value interface{}) (orm *Chain) { - s.Select(column).do(s.value).pluck(value) + do := s.do(s.value) + do.selectStr = column + do.pluck(value) return s } diff --git a/orm_test.go b/orm_test.go index f4be05e8..30bf01d6 100644 --- a/orm_test.go +++ b/orm_test.go @@ -103,7 +103,7 @@ func TestSaveAndFind(t *testing.T) { } user := &User{} - db.First(user) + db.First(user, "name = ?", name) if user.Name != name { t.Errorf("User should be saved and fetched correctly") }