sqlb
Typed SQL builder with compile-checked columns and joins, Postgres and ClickHouse packages and a driver conformance suite.
go get github.com/ubgo/sqlb
- Language
- Go
- Go version
- 1.24
- Core deps
- 0 direct
- Latest tag
- v0.1.1
- License
- Apache-2.0
- Go modules
- 8
How it fits together
passessqlbtestsqlbtest is the shared conformance suite: each one marked green has a test that runs it.
Highlights from the README
- A column is an
Expr[T]. Comparing it with the wrong type, joining it to a mismatched column, or binding it to the wrong pointer is a compile error, not a runtime surprise. - A query carries its own scan targets. The column list and the destinations come from one slice, so inserting a column in the middle cannot silently shift a scan.
- Zero third-party dependencies in the core, enforced by a gate rather than by good intentions. Embedding
sqlbadds no driver, no cgo and no version conflict. - Engine packages carry what one engine spells differently. JSONB, arrays, ranges, full-text search,
DISTINCT ON,ON CONFLICT,MERGEandRETURNINGinpg; JSON1, FTS5 and both upsert forms insqlite;PREWHERE,FINALandLIMIT BYinch. Thepgsurface is the large one — about five hundred constructs to the core’s two hundred and sixty, which is what the universality rule produces: almost nothing is spelled identically by four engines. - Extensions are sub-packages over exported guarded constructors, so adding pgvector support, or the next extension nobody has written yet, never edits the engine package.
Code from the README
dist := vector.L2(embedding, vector.ParamVector(query))
_, err := sqlb.NewSelect(sqlb.SQLiteDialect{}).Columns(sqlb.Bind(dist, &d)).From(docs).Build()
// err: sqlb: construct is not supported by this dialect
// vector.L2 under dialect "sqlite"package users
import (
"time"
"github.com/ubgo/sqlb"
)
var Table = sqlb.Table{Name: "app_user", Alias: "u"}
var (
ID = sqlb.Col[string](Table, "id")
Email = sqlb.Col[string](Table, "email")
CreatedAt = sqlb.Col[time.Time](Table, "created_at")
)import driverpgx "github.com/ubgo/sqlb/contrib/driver-pgx"
db, err := driverpgx.Open("postgres://user:pass@localhost:5432/app?sslmode=disable")
if err != nil {
return err
}
defer db.Close()import "github.com/ubgo/sqlb/pg"
q, err := pg.Select().Columns(sqlb.Bind(users.Email, &email)).
From(users.Table).
Where(sqlb.Eq(users.ID, "usr_1")).
Build()From the sqlb README.