xujiajun/godbal
Fork: 29 Star: 59 (更新于 2024-11-15 01:56:58)
license: MIT
Language: Go .
Database Abstraction Layer (dbal) for Go. Support SQL builder and get result easily (now only support mysql)
最后发布版本: v1.0.0 ( 2018-04-15 11:16:59)
godbal
Database Abstraction Layer (dbal) for go (now only support mysql)
Motivation
I wanted a DBAL that No ORM、No Reflect、Concurrency Save, support SQL builder following good practices and well tested code.
Requirements
Go 1.7 or above.
Installation
go get github.com/xujiajun/godbal
Supported Databases
- mysql
Getting Started
Godbal helps you build SQL queries from composable parts easily:
database, _ := godbal.NewMysql("root:123@tcp(127.0.0.1:3306)/test?charset=utf8").Open()
queryBuilder := mysql.NewQueryBuilder(database)
sql := queryBuilder.Select("uid,username,price,flag").From("userinfo", "").SetFirstResult(0).
SetMaxResults(3).OrderBy("uid", "DESC").GetSQL()
fmt.Println(sql)
Output:
SELECT uid,username,price,flag FROM userinfo ORDER BY uid DESC LIMIT 0,3
Godbal helps you get result easily:
rows, _ := queryBuilder.QueryAndGetMap()
jsonString, _ := json.Marshal(&rows)
fmt.Print(string(jsonString))
Output like:
{"0":{"flag":"1","price":"111.00","uid":"6","username":"johnny2"},"1":{"flag":"1","price":"111.00","uid":"5","username":"johnny2"},"2":{"flag":"0","price":"123.99","uid":"4","username":"joe"}}
Full example:
package main
import (
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/xujiajun/godbal"
"github.com/xujiajun/godbal/driver/mysql"
)
func main() {
database, err := godbal.NewMysql("root:123@tcp(127.0.0.1:3306)/test?charset=utf8").Open()
if err != nil {
panic(err)
}
err = database.Ping()
if err != nil {
panic(err)
}
queryBuilder := mysql.NewQueryBuilder(database)
sql := queryBuilder.Select("uid,username,price,flag").From("userinfo", "").SetFirstResult(0).
SetMaxResults(3).OrderBy("uid", "DESC").GetSQL()
fmt.Println(sql) // SELECT uid,username,price,flag FROM userinfo ORDER BY uid DESC LIMIT 0,3
rows, _ := queryBuilder.QueryAndGetMap()
jsonString, _ := json.Marshal(&rows)
fmt.Print(string(jsonString))
// result like: {"0":{"flag":"1","price":"111.00","uid":"6","username":"johnny2"},"1":{"flag":"1","price":"111.00","uid":"5","username":"johnny2"},"2":{"flag":"0","price":"123.99","uid":"4","username":"joe"}}
}
More examples
Contributing
If you'd like to help out with the project. You can put up a Pull Request.
Author
License
The godbal is open-sourced software licensed under the MIT Licensed
最近版本更新:(数据更新于 2024-10-12 18:51:21)
2018-04-15 11:16:59 v1.0.0
主题(topics):
dbal, golang, mysql, sqlbuilder
xujiajun/godbal同语言 Go最近更新仓库
2024-11-22 20:14:47 daytonaio/daytona
2024-11-22 15:17:23 SagerNet/sing-box
2024-11-21 22:49:20 containerd/containerd
2024-11-21 13:50:50 XTLS/Xray-core
2024-11-21 07:36:18 kubernetes/kubernetes
2024-11-21 06:27:30 ollama/ollama