https://github.com/xujiajun/godbal
Database Abstraction Layer (dbal) for Go. Support SQL builder and get result easily (now only support mysql)
https://github.com/xujiajun/godbal
dbal golang mysql sqlbuilder
Last synced: 7 months ago
JSON representation
Database Abstraction Layer (dbal) for Go. Support SQL builder and get result easily (now only support mysql)
- Host: GitHub
- URL: https://github.com/xujiajun/godbal
- Owner: xujiajun
- License: mit
- Created: 2018-02-28T05:47:42.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-30T05:57:00.000Z (almost 7 years ago)
- Last Synced: 2025-04-24T02:07:47.540Z (7 months ago)
- Topics: dbal, golang, mysql, sqlbuilder
- Language: Go
- Homepage:
- Size: 50.8 KB
- Stars: 59
- Watchers: 5
- Forks: 29
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - godbal
- awesome-go-plus - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily.  (Database / SQL Query Builders)
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- awesome-go - godbal - | - | - | (Database / Advanced Console UIs)
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily (now only support mysql) - ★ 48 (Database)
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. - :arrow_down:0 - :star:43 (Database / Advanced Console UIs)
- awesome-go-with-stars - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- fucking-awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- awesome-go-cn - godbal
- awesome-go-cn - godbal
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- awesome-go-extra - godbal - 02-28T05:47:42Z|2019-01-30T05:57:00Z| (Generators / SQL Query Builders)
- awesome-go-cn - godbal
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / SQL Query Builders)
- awesome-Char - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / Advanced Console UIs)
- awesome-go - godbal - Database Abstraction Layer (dbal) for go. Support SQL builder and get result easily. (Database / Advanced Console UIs)
README
# godbal [](https://godoc.org/github.com/xujiajun/godbal/driver/mysql) [](https://godoc.org/github.com/xujiajun/godbal) [](https://goreportcard.com/report/github.com/xujiajun/godbal)
[](https://coveralls.io/github/xujiajun/godbal?branch=master) [](https://raw.githubusercontent.com/xujiajun/godbal/master/LICENSE) [](https://github.com/avelino/awesome-go#database)
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
* [select](https://github.com/xujiajun/godbal/blob/master/examples/select/main.go)
* [insert](https://github.com/xujiajun/godbal/blob/master/examples/insert/main.go)
* [delete](https://github.com/xujiajun/godbal/blob/master/examples/delete/main.go)
* [update](https://github.com/xujiajun/godbal/blob/master/examples/update/main.go)
* [join](https://github.com/xujiajun/godbal/blob/master/examples/join/main.go)
* [transaction](https://github.com/xujiajun/godbal/blob/master/examples/transaction/main.go)
## Contributing
If you'd like to help out with the project. You can put up a Pull Request.
## Author
* [xujiajun](https://github.com/xujiajun)
## License
The godbal is open-sourced software licensed under the [MIT Licensed](http://www.opensource.org/licenses/MIT)