Documentation ¶
Overview ¶
Package util provides utility functions for SQL.
Index ¶
- func Args(valueOrSlices ...any) []any
- func Count(db QueryAble, query string, args []any) (count int64, err error)
- func CountBuilder(db QueryAble, b sqlf.Builder) (count int64, err error)
- func Interpolate(query string, args []any, options ...InterpolateOption) (string, error)
- func Scan[T any](db QueryAble, query string, args []any, fn NewScanDestFunc[T]) ([]T, error)
- func ScanBuilder[T any](db QueryAble, b sqlf.Builder, fn NewScanDestFunc[T]) ([]T, error)
- func ScanRow(rows *sql.Rows, dest ...any) error
- func Ttoa[T any](slice []T) []any
- type InterpolateOption
- type NewScanDestFunc
- type QueryAble
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Args ¶
Args is a help func to create a slice of query args from values, slices and array. it concatenates all values to a single slice, and flattens any slices and arrays in the first level. e.g.
Args(1, []int{2, 3}, []string{"a", "b", "c"}) => []any{1, 2, 3, "a", "b", "c"}
Example ¶
package main import ( "fmt" "github.com/qjebbs/go-sqlf/util" ) func main() { print := func(v any) { fmt.Printf("%#v\n", v) } print(util.Args(1, 2, 3)) print(util.Args([]int{1, 2, 3})) print(util.Args(&[]int{1, 2, 3})) print(util.Args([3]int{1, 2, 3})) print(util.Args(1, []int{2, 3}, []string{"a", "b", "c"})) }
Output: []interface {}{1, 2, 3} []interface {}{1, 2, 3} []interface {}{1, 2, 3} []interface {}{1, 2, 3} []interface {}{1, 2, 3, "a", "b", "c"}
func Count ¶
Count count the number of rows of the query.
Example ¶
package main import ( "database/sql" "fmt" "github.com/qjebbs/go-sqlf/util" ) func main() { var db *sql.DB if db != nil { count, err := util.Count(db, "SELECT * FROM foo", nil) if err != nil { panic(err) } fmt.Println(count) } }
Output:
func CountBuilder ¶
CountBuilder is like Count, but it builds query from sqlf.Builder.
Example ¶
var ( db *sql.DB builder sqlf.Builder ) if db != nil && builder != nil { count, err := util.CountBuilder(db, builder) if err != nil { panic(err) } fmt.Println(count) }
Output:
func Interpolate ¶
func Interpolate(query string, args []any, options ...InterpolateOption) (string, error)
Interpolate interpolates the args into the query, use it only for debug purposes to avoid SQL injection attacks.
Example ¶
package main import ( "fmt" "time" "github.com/qjebbs/go-sqlf/util" ) func main() { query := "SELECT * FROM foo WHERE status = ? AND created_at > ?" args := []any{"ok", time.Unix(0, 0)} interpolated, err := util.Interpolate(query, args, util.WithTimeFormat("2006-01-02 15:04:05")) if err != nil { panic(err) } fmt.Println(interpolated) }
Output: SELECT * FROM foo WHERE status = 'ok' AND created_at > '1970-01-01 08:00:00'
func Scan ¶
Scan scans query rows with scanner
Example ¶
package main import ( "database/sql" "fmt" "github.com/qjebbs/go-sqlf/util" ) func main() { type foo struct { ID int64 Name string } var db *sql.DB if db != nil { query := "SELECT id, name FROM foo LIMIT 10" r, err := util.Scan( db, query, nil, func() (*foo, []any) { r := &foo{} return r, []any{&r.ID, &r.Name} }, ) if err != nil { panic(err) } fmt.Println(r) } }
Output:
func ScanBuilder ¶
func ScanBuilder[T any](db QueryAble, b sqlf.Builder, fn NewScanDestFunc[T]) ([]T, error)
ScanBuilder is like Scan, but it builds query from sqlf.Builder
Example ¶
type foo struct { ID int64 Name string } var db *sql.DB if db != nil { builder := &sqlf.Fragment{ Raw: "SELECT id, name FROM foo WHERE id IN (#join('#argDollar', ', '))", Args: []any{1, 2, 3}, } r, err := util.ScanBuilder( db, builder, func() (*foo, []any) { r := &foo{} return r, []any{&r.ID, &r.Name} }, ) if err != nil { panic(err) } fmt.Println(r) }
Output:
Types ¶
type InterpolateOption ¶
type InterpolateOption func(*interpolateOptions)
InterpolateOption is the option of Interpolate.
func WithTimeFormat ¶
func WithTimeFormat(format string) InterpolateOption
WithTimeFormat sets the format of time value.
type NewScanDestFunc ¶
NewScanDestFunc is the function to create a new scan destination, returning the destination and its fields to scan.
type QueryAble ¶
type QueryAble interface { Exec(query string, args ...any) (sql.Result, error) Prepare(query string) (*sql.Stmt, error) Query(query string, args ...any) (*sql.Rows, error) QueryRow(query string, args ...any) *sql.Row }
QueryAble is the interface for query-able *sql.DB, *sql.Tx, etc.