Skip to content
/ go-sqlf Public

go-sqlf focuses only on building SQL queries by free combination of fragments.

License

Notifications You must be signed in to change notification settings

qjebbs/go-sqlf

Repository files navigation

Go SQL Fragment (go-sqlf)

Warning

This package is in an alpha stage. The API is subject to change.

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().

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]
}

Query Builder

Package go-sqlb provides complex SQL builders and struct mapping capabilities, while go-sqlf is the underlying foundation.

About

go-sqlf focuses only on building SQL queries by free combination of fragments.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages