Files
db/README.md
what 5ae9aee833 docs: 补充 v1-legacy 分支说明与安装方式
master 分支已经变成历史不相关的新项目(db-v2),这里补充说明本分支的定位,
以及依赖方(如 zg-estate)应该怎么锁定/更新到这个分支,避免误跑 go get -u
拉到新项目上。
2026-08-20 16:46:07 +08:00

124 lines
4.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# WORK IN PROGRESS
# Get Started
A golang ORM Framework like Laravel's Eloquent
Example
```golang
type User struct {
Id int64 `goelo:"column:id;primaryKey"`
UserName sql.NullString `goelo:"column:name"`
Age int `goelo:"column:age"`
Status int `goelo:"column:status"`
Friends []UserT `goelo:"BelongsToMany:FriendsRelation"`
Address Address `goelo:"HasOne:AddressRelation"`
CreatedAt time.Time `goelo:"column:created_at,timestatmp:create"`
UpdatedAt sql.NullTime `goelo:"column:updated_at,timestatmp:update"`
}
//Find/First/Get
var user User
DB.Table("users").Find(&user,1)
DB.Table("users").Where("name","john").First(&user)
DB.Table("users").Where("name","john").FirstOrCreate(&user)
//Column/Aggeregate
var age int
DB.Table("users").Where("id","=",20).Value(&age,"age")
DB.Table("users").Max(&age,"age")
var salary int
DB.Table("user").Where("age",">=",30).Avg(&salary,"salary")
//Find record to map
var m = make(map[string]interface{})
DB.Query().From("users").Find(&m,3)
var ms []map[string]interface{}
DB.Query().From("users").Get(&ms)
//Pagination
var users []User
DB.Table("users").Where("id", ">", 10).Where("id", "<", 28).Paginate(&users, 10, 1)
//Chunk/ChunkById
var total int
totalP := &total
DB.Table("users").OrderBy("id").Chunk(&[]User{}, 10, func(dest interface{}) error {
us := dest.(*[]User)
for _, user := range *us {
assert.Equal(t, user.UserName, sql.NullString{
String: fmt.Sprintf("user-%d", user.Age),
Valid: true,
})
*totalP++
}
return nil
})
var total int
totalP := &total
DB.Table("users").ChunkById(&[]User{}, 10, func(dest interface{}) error {
us := dest.(*[]User)
for _, user := range *us {
assert.Equal(t, user.UserName, sql.NullString{
String: fmt.Sprintf("user-%d", user.Age),
Valid: true,
})
*totalP++
}
return nil
})
//Query clause
DB.Where("name","john@apple.com").OrWhere("email","john@apple.com").First(&user)
DB.Where("is_admin", 1).Where(map[string]interface{}{
"name": "Joe", "location": "LA",
}, db.BOOLEAN_OR).Where(func(builder *db.Builder){
builder.WhereYear("created_at", "<", 2010).WhereColumn("first_name", "last_name").OrWhereNull("activited_at")
}).ToSql()
// sql:"select `name`, `age`, `email` where `is_admin` = ? or (`name` = ? and `location` = ?) and (year(`created_at`) < ? and `first_name` = `last_name` or `activited_at` is null)"
// Insert/Update/Delete
DB.Table("users").Insert(map[string]interface{}{
"name": "go-eloquent",
"age": 18,
"created_at": now,
})
DB.Table("users").Where("id", id).Update(map[string]interface{}{
"name": "newname",
"age": 18,
"updated_at": now.Add(time.Hour * 1),
})
DB.Table("users").Where("id", id).Delete()
```
More Details,visit [Docs](https://glitterlip.github.io/go-eloquent-docs/)
# Credits
[https://github.com/go-gorm/gorm](https://github.com/go-gorm/gorm)
[https://github.com/jmoiron/sqlx](https://github.com/jmoiron/sqlx)
[https://github.com/qclaogui/database](https://github.com/qclaogui/database)
# 分支说明(v1-legacy
`master` 分支现在是完全不同的新项目(fork 自 [doug-martin/goqu](https://github.com/doug-martin/goqu)
风格的 `exp` 表达式树 + `SelectDataset`/`InsertDataset`/`UpdateDataset`/`DeleteDataset`,历史
跟这条老线不相关),上面这份文档描述的 `db.Builder`/`DatabaseManager` 链式查询构造器只在
`v1-legacy` 分支上还继续维护。
给还在维护、但还没迁移到新架构的老项目用,比如 `zg-estate`(通过 `framework`/`contracts`/`orm`
间接依赖)。只接受独立于新架构的 bug 修复。
## 依赖方怎么安装/更新
Go modules 不会记住某个版本是从哪个分支解析出来的,`go.mod`/`go.sum` 里存的只是一次性解析出
的 commit 伪版本号,分支上有新提交也不会自动同步。
**首次锁定,或者要拿这个分支上新提交的更新**,都执行同一条命令:
```
go get git.fsdpf.net/go/db@v1-legacy
```
**禁止**跑裸的 `go get -u`(或者 `go get git.fsdpf.net/go/db@latest`)——`master` 现在是历史
完全不相关的新项目,`@latest`/`-u` 会解析到 `master` 的 HEAD,等于直接换成一个 API 完全不兼容
的实现,而不是停留在这个分支上。