util

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package util provides utility functions for SQL.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args(valueOrSlices ...any) []any

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

func Count(db QueryAble, query string, args []any) (count int64, err error)

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

func CountBuilder(db QueryAble, b sqlf.Builder) (count int64, err error)

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

func Scan[T any](db QueryAble, query string, args []any, fn NewScanDestFunc[T]) ([]T, error)

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:

func ScanRow

func ScanRow(rows *sql.Rows, dest ...any) error

ScanRow scans a single row to dest, unlike rows.Scan(), it drops the extra columns. It's useful when *sqlb.QueryBuilder.OrderBy() add extra column to the query.

func Ttoa

func Ttoa[T any](slice []T) []any

Ttoa is a help func to convert a slice to []any.

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

type NewScanDestFunc[T any] func() (T, []any)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL