https://github.com/qjebbs/go-sqlf
go-sqlf focuses only on building SQL queries by free combination of fragments.
https://github.com/qjebbs/go-sqlf
golang sql sqlbuilder
Last synced: 15 days ago
JSON representation
go-sqlf focuses only on building SQL queries by free combination of fragments.
- Host: GitHub
- URL: https://github.com/qjebbs/go-sqlf
- Owner: qjebbs
- License: mit
- Created: 2023-04-10T07:14:57.000Z (over 3 years ago)
- Default Branch: v4
- Last Pushed: 2026-04-30T09:11:24.000Z (3 months ago)
- Last Synced: 2026-04-30T10:25:09.446Z (3 months ago)
- Topics: golang, sql, sqlbuilder
- Language: Go
- Homepage:
- Size: 353 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## Go SQL Fragment (go-sqlf)
Package `sqlf` is dedicated to building SQL queries by composing fragments.
Unlike other SQL builders or ORMs, `*Fragment` is the only concept you need to understand.
It uses the same bind variable syntax (`?` / `$1`) as `database/sql`, which can refer to both ordinary args and fragment builders in args.
A `*Fragment` is created by `F()`.
```go
import (
"context"
"fmt"
"github.com/qjebbs/go-sqlf/v4"
"github.com/qjebbs/go-sqlf/v4/dialect"
)
func Example_basic() {
b := sqlf.F(
"SELECT * FROM foo WHERE ?",
sqlf.Join([]sqlf.Builder{
sqlf.F("baz = $1", true),
sqlf.F("bar BETWEEN ? AND ?", 1, 100),
}, " AND "),
)
ctx := sqlf.NewContext(context.Background(), dialect.PostgreSQL{})
query, args, _ := b.Build(ctx)
fmt.Println(query)
fmt.Println(args)
// Output:
// SELECT * FROM foo WHERE baz = $1 AND bar BETWEEN $2 AND $3
// [true 1 100]
}
```
## The Go SQL Tools Family
This project is part of a family of Go SQL tools, each designed for a different level of abstraction and automation:
1. **go-sqlf (this project)** — Minimalist SQL fragment builder. For simple, manual SQL composition with parameter binding and zero magic.
2. **[go-sqlb](https://github.com/qjebbs/go-sqlb)** — Advanced SQL builder. For programmatically building complex queries (CTE, JOIN, expressions, etc.) with chainable, declarative, and composable APIs.
3. **[go-sqlm](https://github.com/qjebbs/sqlm)** — Struct mapping. Declarative struct mapping, automatic CRUD, batch operations, and high-performance zero-reflection code generation.