https://github.com/xgfone/go-sqlx
A set of the simple, flexible and powerful SQL builders with zero-config.
https://github.com/xgfone/go-sqlx
builder database go go-sql golang mysql sql sql-builder sqlbuilder
Last synced: 8 months ago
JSON representation
A set of the simple, flexible and powerful SQL builders with zero-config.
- Host: GitHub
- URL: https://github.com/xgfone/go-sqlx
- Owner: xgfone
- License: apache-2.0
- Created: 2020-04-17T14:29:31.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-25T09:23:05.000Z (over 1 year ago)
- Last Synced: 2024-10-30T20:49:10.976Z (over 1 year ago)
- Topics: builder, database, go, go-sql, golang, mysql, sql, sql-builder, sqlbuilder
- Language: Go
- Homepage:
- Size: 237 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SQL Builder
[](https://github.com/xgfone/go-sqlx/actions/workflows/go.yml)
[](https://pkg.go.dev/github.com/xgfone/go-sqlx)
[](https://raw.githubusercontent.com/xgfone/go-sqlx/master/LICENSE)


Package `sqlx` provides a set of flexible and powerful SQL builders, not ORM, which is inspired by [go-sqlbuilder](https://github.com/huandu/go-sqlbuilder). The built result can be used by [`DB.Query()`](https://pkg.go.dev/database/sql#DB.Query) and [`DB.Exec()`](https://pkg.go.dev/database/sql#DB.Exec)
## Install
```shell
$ go get -u github.com/xgfone/go-sqlx
```
## Usage
```go
package main
import (
"fmt"
"github.com/xgfone/go-op"
"github.com/xgfone/go-sqlx"
)
func main() {
builder := sqlx.Select("*").From("table")
builder.Where(op.Equal("id", 123), op.Between("age", 20, 30))
// You can set the dialect by hand, which is DefaultDialect by default.
// DefaultDialect is the MySQL dialect, but you can modify it.
// builder.SetDialect(Sqlite3)
sql, args := builder.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// SELECT * FROM `table` WHERE (`id`=? AND `age` BETWEEN ? AND ?)
// [123 20 30]
}
```
You can use `sqlx.DB`, which is the proxy of builder and `sql.DB`, it will automatically set the dialect by the sql driver name. For example,
```go
// Set the dialect to MySQL.
db, _ := sqlx.Open("mysql", "user:password@tcp(127.0.0.1:3306)/db")
builder := db.Select("*").From("table").Where(op.Equal("id", 123))
sql, args := builder.Build()
rows := db.QueryRows(sql, args.Args()...)
// Or
// rows := builder.QueryRows()
if rows.Err != nil {
// TODO: ...
return
}
defer rows.Close()
// TODO: ...
```
### Intercept SQL
```go
package main
import (
"fmt"
"github.com/xgfone/go-op"
"github.com/xgfone/go-sqlx"
)
func main() {
// Open DB connecting the mysql server and set the dialect to MySQL.
db, err := sqlx.Open("mysql", "user:password@tcp(127.0.0.1:3306)/db")
if err != nil {
fmt.Println(err)
return
}
defer db.Close()
// Set the interceptor to print the sql statement.
db.Interceptor = sqlx.InterceptorFunc(func(sql string, args []any) (string, []any, error) {
fmt.Println(sql)
return sql, args, nil
})
// Build the SELECT SQL statement
builder := db.Select("*").From("table")
builder.Where(op.Equal("id", 123))
rows := builder.QueryRows()
if rows.Err != nil {
fmt.Println(err)
return
}
// TODO: ...
// Interceptor will output:
// SELECT * FROM `table` WHERE `id`=?
}
```