Better README

This commit is contained in:
Jinzhu 2013-10-28 09:05:44 +08:00
parent 692b08a7be
commit 7e68ad9773
2 changed files with 54 additions and 29 deletions

View File

@ -34,50 +34,61 @@ db.Where("name = ?", "jinzhu").First(&user)
db.Where("name = ?", "jinzhu").Find(&users) db.Where("name = ?", "jinzhu").Find(&users)
// Advanced Where Usage // Advanced Where Usage
//// select * from users name <> 'jinzhu';
db.Where("name <> ?", "jinzhu").Find(&users) db.Where("name <> ?", "jinzhu").Find(&users)
//// select * from users name = 'jinzhu' and age >= 22; //// users -> select * from users name <> 'jinzhu';
db.Where("name = ? and age >= ?", "jinzhu", "22").Find(&users) db.Where("name = ? and age >= ?", "jinzhu", "22").Find(&users)
//// select * from users name in ('jinzhu', 'jinzhu 2'); //// users -> select * from users name = 'jinzhu' and age >= 22;
db.Where("name in (?)", []string["jinzhu", "jinzhu 2"]).Find(&users) db.Where("name in (?)", []string["jinzhu", "jinzhu 2"]).Find(&users)
//// users -> select * from users name in ('jinzhu', 'jinzhu 2');
db.Where("name LIKE ?", "%jin%").Find(&users)
//// users -> select * from users name LIKE "%jinzhu%";
db.Where("birthday < ?", time.Now()).Find(&users) db.Where("birthday < ?", time.Now()).Find(&users)
// Inline search condition // Inline search condition
//// select * from users where id = 23 limit 1;
db.First(&user, 23) db.First(&user, 23)
//// select * from users where name = "jinzhu" limit 1; //// user -> select * from users where id = 23 limit 1;
db.First(&user, "name = ?", "jinzhu") db.First(&user, "name = ?", "jinzhu")
//// select * from users where name = "jinzhu"; //// user -> select * from users where name = "jinzhu" limit 1;
db.Find(&users, "name = ?", "jinzhu") db.Find(&users, "name = ?", "jinzhu")
//// select * from users where name <> "jinzhu" and age > 20; //// users -> select * from users where name = "jinzhu";
db.Find(&users, "name <> ? and age > ?", "jinzhu", 20) db.Find(&users, "name <> ? and age > ?", "jinzhu", 20)
//// users -> select * from users where name <> "jinzhu" and age > 20;
// Select // Select
//// select name from users;
db.Select("name").Find(&users) db.Select("name").Find(&users)
//// users -> select name from users;
// Order // Order
//// select * from users order by age desc, name;
db.Order("age desc, name").Find(&users) db.Order("age desc, name").Find(&users)
//// users -> select * from users order by age desc, name;
db.Order("age desc").Order("name").Find(&users) db.Order("age desc").Order("name").Find(&users)
//// users -> select * from users order by age desc, name;
// Limit // Limit
//// select * from users limit 3;
db.Limit(3).Find(&users) db.Limit(3).Find(&users)
db.Limit(10).Find(&ten_users).Limit(20).Find(&twenty_users).Limit(-1).Find(&all_users) //// users -> select * from users limit 3;
db.Limit(10).Find(&users1).Limit(20).Find(&users2).Limit(-1).Find(&users3)
//// users1 -> select * from users limit 10;
//// users2 -> select * from users limit 20;
//// users3 -> select * from users;
// Offset // Offset
//// select * from users offset 3; //// select * from users offset 3;
db.Offset(3).Find(&users) db.Offset(3).Find(&users)
db.Offset(10).Find(&users).Offset(20).Find(&users).Offset(-1).Find(&users) db.Offset(10).Find(&users1).Offset(20).Find(&users2).Offset(-1).Find(&users3)
//// user1 -> select * from users offset 10;
//// user2 -> select * from users offset 20;
//// user3 -> select * from users;
// Or // Or
//// select * from users where role = 'admin' or role = 'super_admin';
db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users) db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users)
//// users -> select * from users where role = 'admin' or role = 'super_admin';
// Count // Count
//// select count(*) from users where name = 'jinzhu' or name = 'jinzhu 2'';
db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count) db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count)
//// users -> select * from users where name = 'jinzhu' or name = 'jinzhu 2';
//// count -> select count(*) from users where name = 'jinzhu' or name = 'jinzhu 2';
db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count) db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count)
// CreatedAt (auto insert current time on create) // CreatedAt (auto insert current time on create)
@ -105,13 +116,15 @@ Callbacks is a function defined to a model, if the function return error, will p
// Pluck (get users's age as map) // Pluck (get users's age as map)
var ages []int64 var ages []int64
//// select age from users; db.Find(&users).Pluck("age", &ages)
db.Find(&users).Pluck("age", &ages) // ages => []int64{18, 10, 99...} //// ages -> select age from users;
var names []string var names []string
db.Model(&User{}).Pluck("name", &names) db.Model(&User{}).Pluck("name", &names)
//// names -> select name from users;
// Query Chains // Query Chains
db.Where("name <> ?", "jinzhu").Where("age >= ? and role <> ?", 20, "admin").Find(&users) db.Where("name <> ?", "jinzhu").Where("age >= ? and role <> ?", 20, "admin").Find(&users)
//// users -> select * from users where name <> 'jinzhu' andd age >= 20 and role <> 'admin';
// Create Table with struct // Create Table with struct
db.CreateTable(&User{}) db.CreateTable(&User{})
@ -126,23 +139,23 @@ db.Exec("drop table users;")
// Already excited about the basic usage? Let's see some magic! // 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) db.First(&first_article).Count(&total_count).Limit(10).Find(&first_page_articles).Offset(10).Find(&second_page_articles)
// first_article -> select * from articles limit 1 //// first_article -> select * from articles limit 1
// total_count -> select count(*) from articles //// total_count -> select count(*) from articles
// first_page_articles -> select * from articles limit 10 //// first_page_articles -> select * from articles limit 10
// second_page_articles -> select * from articles limit 10 offset 10 //// second_page_articles -> select * from articles limit 10 offset 10
db.Where("created_at > ?", "2013/10/10").Find(&cancelled_orders, "state = ?", "cancelled").Find(&shipped_orders, "state = ?", "shipped") db.Where("created_at > ?", "2013/10/10").Find(&cancelled_orders, "state = ?", "cancelled").Find(&shipped_orders, "state = ?", "shipped")
// cancelled_orders -> select * from orders where created_at > '2013/10/10' and state = 'cancelled' //// cancelled_orders -> select * from orders where created_at > '2013/10/10' and state = 'cancelled'
// shipped_orders -> select * from orders where created_at > '2013/10/10' and state = 'shipped' //// shipped_orders -> select * from orders where created_at > '2013/10/10' and state = 'shipped'
db.Model(&Order{}).Where("amount > ?", 10000).Pluck("user_id", &paid_user_ids) db.Model(&Order{}).Where("amount > ?", 10000).Pluck("user_id", &paid_user_ids)
// paid_user_ids -> select user_id from orders where amount > 10000 //// paid_user_ids -> select user_id from orders where amount > 10000
db.Where("user_id = ?", paid_user_ids).Find(&:paid_users) db.Where("user_id = ?", paid_user_ids).Find(&:paid_users)
// paid_users -> select * from users where user_id in (10, 20, 99) //// paid_users -> select * from users where user_id in (10, 20, 99)
db.Where("product_name = ?", "fancy_product").Find(&orders).Find(&shopping_cart) db.Where("product_name = ?", "fancy_product").Find(&orders).Find(&shopping_cart)
// orders -> select * from orders where product_name = 'fancy_product' //// orders -> select * from orders where product_name = 'fancy_product'
// shopping_cart -> select * from carts where product_name = 'fancy_product' //// shopping_cart -> select * from carts where product_name = 'fancy_product'
// Do you noticed the search table is different for above query, yay // Do you noticed the search table is different for above query, yay
// Open your mind, add more cool examples // Open your mind, add more cool examples

View File

@ -163,19 +163,31 @@ func TestWhere(t *testing.T) {
db.Save(&User{Name: name, Age: 1}) db.Save(&User{Name: name, Age: 1})
user := &User{} user := &User{}
db.Where("Name = ?", name).First(user) db.Where("name = ?", name).First(user)
if user.Name != name { if user.Name != name {
t.Errorf("Should found out user with name '%v'", name) t.Errorf("Should found out user with name '%v'", name)
} }
user = &User{} user = &User{}
orm := db.Where("Name = ?", "noexisting-user").First(user) orm := db.Where("name LIKE ?", "%nonono%").First(user)
if orm.Error == nil {
t.Errorf("Should return error when searching for none existing record, %+v", user)
}
user = &User{}
orm = db.Where("name LIKE ?", "%whe%").First(user)
if orm.Error != nil {
t.Errorf("Should not return error when searching for existing record, %+v", user)
}
user = &User{}
orm = db.Where("name = ?", "noexisting-user").First(user)
if orm.Error == nil { if orm.Error == nil {
t.Errorf("Should return error when looking for none existing record, %+v", user) t.Errorf("Should return error when looking for none existing record, %+v", user)
} }
users := []User{} users := []User{}
orm = db.Where("Name = ?", "none-noexisting").Find(&users) orm = db.Where("name = ?", "none-noexisting").Find(&users)
if orm.Error != nil { if orm.Error != nil {
t.Errorf("Shouldn't return error when looking for none existing records, %+v", users) t.Errorf("Shouldn't return error when looking for none existing records, %+v", users)
} }