前言: github地址 asdine/storm
asdine/storm 是golang 開發(fā)的一個 orm工具. 內(nèi)置嵌入式數(shù)據(jù)庫為 golang 的流行 K/V 數(shù)據(jù)庫 bbolt
創(chuàng)建數(shù)據(jù)結(jié)構(gòu)
type User struct {
ID int //主鍵,默認(rèn) ID 值為主鍵, 申明其他類型為主鍵: `storm:"id"`
Group string `storm:"index"` //索引
Email string `storm:"unique"` // 不可重復(fù)
Name string // 普通字段, 不會創(chuàng)建索引
Age int
}
打開數(shù)據(jù)庫
db, err := storm.Open("my.db")
defer db.Close()
CRUD 以及數(shù)據(jù)表操作
//---------------以下是 數(shù)據(jù)庫 操作----------------------
// Init 為給定結(jié)構(gòu)創(chuàng)建索引和存儲桶
Init(data interface{}) error
eg: err := db.Init(new(User))
// ReIndex 重建存儲桶的所有索引. 優(yōu)化數(shù)據(jù)庫使用
ReIndex(data interface{}) error
eg: db.ReIndex(new(User)
// 保存結(jié)構(gòu)體數(shù)據(jù)
Save(data interface{}) error
eg: err := db.Save(&SimpleUser{ID: 10, Name: "John"})
// 更新結(jié)構(gòu)體數(shù)據(jù), 可以同時修改多個數(shù)據(jù)
Update(data interface{}) error
eg: err = db.Update(&User{ID: 10, Name: "Jack"})
// 更新單個字段
UpdateField(data interface{}, fieldName string, value interface{}) error
eg: err = db.UpdateField(&User{ID: 11}, "Name", "Jack")
附0值 eg: err = db.UpdateField(&User{ID: 10}, "Age", int(0))
// 刪除整個表
Drop(data interface{}) error
eg: err = db.From("b1").Drop("b2")
// DeleteStruct 從關(guān)聯(lián)的存儲表中刪除結(jié)構(gòu)
DeleteStruct(data interface{}) error
eg:
u1 := IndexedNameUser{ID: 10, Name: "John", age: 10}
err = db.DeleteStruct(&u1)
普通查詢
提供的方法
One(fieldName string, value interface{}, to interface{}) error
Find(fieldName string, value interface{}, to interface{}, options ...func(q *index.Options)) error
// AllByIndex 獲取在指定索引中索引的存儲桶的所有記錄
AllByIndex(fieldName string, to interface{}, options ...func(*index.Options)) error
// 如果沒有記錄,它不會返回任何錯誤宝恶,并且"to"參數(shù)設(shè)置為空切片
All(to interface{}, options ...func(*index.Options)) error
// 返回一個 query 對象, 使用 tree.go 提供的方法.
Select(matchers ...q.Matcher) Query
// 按指定范圍內(nèi)的指定索引返回一個或多個記錄
Range(fieldName string, min, max, to interface{}, options ...func(*index.Options)) error
// 返回一個或多個記錄符隙,其給定字段以指定的前綴開頭
Prefix(fieldName string, prefix string, to interface{}, options ...func(*index.Options)) error
// 計數(shù)存儲桶的所有記錄
Count(data interface{}) (int, error)
type User struct{
Name string `json:"name"`
Age int `json:"age"`
}
案例
- One
//定義接收參數(shù)
var user User
// 如果查詢到符合 Name = John 的數(shù)據(jù), 賦值給 user 變量
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
err = db.One("Name", "John", &user)
- Find
//定義接收參數(shù)數(shù)組
var users []User
// 如果查詢到符合 Name = John 的數(shù)據(jù), 賦值給 users 變量
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
//可以添加 option 參數(shù)
err = db.Find("Name", "John", &users)
err = db.One("Name", "John", &user, storm.Limit(10), storm.Skip(10), storm.Reverse())
- AllByIndex
//定義接收參數(shù)數(shù)組
var users []User
// 根據(jù)索引,查詢?nèi)康臄?shù)據(jù),
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
err = db.AllByIndex("Name", &users)
- All
//定義接收參數(shù)數(shù)組
var users []User
// 查詢?nèi)康臄?shù)據(jù)
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
//如果沒有記錄,它將不返回任何錯誤垫毙,并且"to"參數(shù)設(shè)置為空切片霹疫。
err = db.All(&users)
- Select 高級查詢用法
//定義接收參數(shù)數(shù)組
var users []User
//根據(jù)Matcher構(gòu)造Query 對象
query := db.Select(q.Gte("ID", 10), q.Lte("ID", 100))
query.Find(&users) //其他方法參考 query.go
- Range 范圍查詢
//定義接收參數(shù)數(shù)組
var users []User
//設(shè)定一個范圍, 查詢
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
err := db.Range("Age", 10, 21, &users)
- Prefix 模糊查詢
//定義接收參數(shù)數(shù)組
var users []User
//設(shè)定字符前綴, 查詢滿足前綴給定值的數(shù)據(jù)
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
err := db.Prefix("Name", "Jo", &users)
- Count 查詢數(shù)據(jù)量
//查詢bucket 中的數(shù)據(jù)總量
//如果數(shù)據(jù)量為0, 不會返回 err.
count, err := db.Prefix(&User{})
高級查詢 匹配器 Matcher 接口
// Eq 匹配器,檢查給定字段名(field) 是否等于給定值(value) 综芥。
func Eq(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &cmp{value: v, token: token.EQL})
}
eg: q.Eq("Name","Jone")
// EqF 匹配器, 檢查給定字段名 field1是否等于給定的另一個字段名稱field2
func EqF(field1, field2 string) Matcher {
return NewField2FieldMatcher(field1, field2, token.EQL)
}
// StrictEq 匹配器, 檢查給定字段名field是否深度等于(deeply equal) 給定的值
func StrictEq(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &strictEq{value: v})
}
// Gt 匹配器, 檢查給定字段名field是否大于給定的值
func Gt(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &cmp{value: v, token: token.GTR})
}
// GtF 匹配器, 檢查給定字段名 field1是否大于給定的另一個字段名稱field2
func GtF(field1, field2 string) Matcher {
return NewField2FieldMatcher(field1, field2, token.GTR)
}
// Gte 匹配器, 檢查給定字段名field是否大于等于 >= 給定的值
func Gte(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &cmp{value: v, token: token.GEQ})
}
// GteF 匹配器, 檢查給定字段名 field1是否大于或等于給定的另一個字段名稱field2
func GteF(field1, field2 string) Matcher {
return NewField2FieldMatcher(field1, field2, token.GEQ)
}
// Lt 匹配器, 檢查給定字段名field是否小于給定的值
func Lt(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &cmp{value: v, token: token.LSS})
}
// LtF 匹配器, 檢查給定字段名 field1是否小于給定的另一個字段名稱field2
func LtF(field1, field2 string) Matcher {
return NewField2FieldMatcher(field1, field2, token.LSS)
}
// Lte 匹配器, 檢查給定字段名field是否小于等于給定的值
func Lte(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &cmp{value: v, token: token.LEQ})
}
// LteF 匹配器, 檢查給定字段名 field1是否小于等于給定的另一個字段名稱field2
func LteF(field1, field2 string) Matcher {
return NewField2FieldMatcher(field1, field2, token.LEQ)
}
// In 匹配器, 檢查給定字段名field是否等于給定的切片中的一個值
// v 必須是一個切片.
func In(field string, v interface{}) Matcher {
return NewFieldMatcher(field, &in{list: v})
}
eg: q.Eq("Name",["a","b","c"])
// True 匹配器, 總是返回 true
func True() Matcher { return &trueMatcher{} }
// Or 匹配器, 傳入多個 Matcher 對象, 或的關(guān)系
func Or(matchers ...Matcher) Matcher { return &or{children: matchers} }
eg: q.Or( q.Eq("Name":"Jone" ), q.Eq("Name":"Zach"))
// And 匹配器, 傳入多個 Matcher 對象, 全部滿足, 與的關(guān)系
func And(matchers ...Matcher) Matcher { return &and{children: matchers} }
// Not 匹配器, 傳入多個 Matcher 對象, 全部不滿足, 非的關(guān)系
func Not(matchers ...Matcher) Matcher { return ¬{children: matchers} }
// Re 匹配器, 重新創(chuàng)建一個正則匹配規(guī)則, 檢查給定的字段 field 是否匹配給定的正在規(guī)則
// 請注意丽蝎,這僅支持類型字符串或 [字節(jié)]的字段。
// 第二個參數(shù)寫入正則的字符串表達(dá)式
func Re(field string, re string) Matcher { *** }
eg: q.Re("A", "\\d+")
高級查詢 匹配器 Query 接口
提供的方法
// 按給定編號跳過匹配記錄
Skip(int) Query
// 返回指定數(shù)量的數(shù)據(jù)
Limit(int) Query
// 按給定字段從左到右的降序排列膀藐。
OrderBy(...string) Query
eg: q.OrderBy("Name", "Age")
// 切換排序方案: 降序 <--> 升序
Reverse() Query
// 表名稱
Bucket(string) Query
// 查詢匹配列表
Find(interface{}) error
// 得第一條匹配記錄
First(interface{}) error
// 刪除所有匹配的記錄
Delete(interface{}) error
// 所有匹配記錄的數(shù)量
Count(interface{}) (int, error)
// 返回所有記錄的原始數(shù)據(jù). 未進(jìn)行解碼操作
Raw() ([][]byte, error)
// 對每個原始元素執(zhí)行給定的功能
RawEach(func([]byte, []byte) error) error
// 對每個元素執(zhí)行給定的功能
Each(interface{}, func(interface{}) error) error
KV數(shù)據(jù)庫操作
直接通過數(shù)據(jù) Key 操作數(shù)據(jù)庫, 提供的方法有:
// 獲取第 N 個元素
// 如果查詢不到數(shù)據(jù), 返回 err = storm.ErrNotFound
Get(bucketName string, key interface{}, to interface{}) error
eg: 查詢第10條元素
u2 := IndexedNameUser{}
err = db.Get("IndexedNameUser", 10, &u2)
// 添加 KV 數(shù)據(jù)庫的值
Set(bucketName string, key interface{}, value interface{}) error
eg: err := db.Set("files", "myfile.csv", "a,b,c,d")
// 刪除KV 數(shù)據(jù)庫的值
Delete(bucketName string, key interface{}) error
eg: err = db.Delete("files", "myfile.csv")
// 獲取 key 值為 bytes 類型的 value 值
GetBytes(bucketName string, key interface{}) ([]byte, error)
// 設(shè)置 key 值為 bytes 類型的 value 值
SetBytes(bucketName string, key interface{}, value []byte) error
// KeyExists 判斷 key 值是否已經(jīng)被創(chuàng)建
KeyExists(bucketName string, key interface{}) (bool, error)