Documentation ¶
Index ¶
- Constants
- func EscapeQuote(str string, quote byte) string
- func FetchAll[T any](ctx context.Context, db DB, query Query, rowmapper func(*Row) T) ([]T, error)
- func FetchExists(ctx context.Context, db DB, query Query) (exists bool, err error)
- func FetchOne[T any](ctx context.Context, db DB, query Query, rowmapper func(*Row) T) (T, error)
- func ParseBytes(b []byte) (uuid [16]byte, err error)
- func QuoteIdentifier(dialect string, identifier string) string
- func Sprint(dialect string, v any) (string, error)
- func Sprintf(dialect string, query string, args []any) (string, error)
- func UUID(value [16]byte) driver.Valuer
- func WriteValue(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, ...) error
- func Writef(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, ...) error
- type Cursor
- type DB
- type DialectCase
- type DialectExpression
- type DialectValuer
- type Expression
- type Parameter
- func BoolParam(name string, b bool) Parameter
- func BytesParam(name string, b []byte) Parameter
- func Float64Param(name string, num float64) Parameter
- func Int64Param(name string, num int64) Parameter
- func IntParam(name string, num int) Parameter
- func Param(name string, value any) Parameter
- func StringParam(name string, s string) Parameter
- func TimeParam(name string, t time.Time) Parameter
- func UUIDParam(name string, value [16]byte) Parameter
- type PreparedExec
- type PreparedFetch
- func (preparedFetch *PreparedFetch[T]) Close() error
- func (preparedFetch *PreparedFetch[T]) FetchAll(ctx context.Context, params ...Parameter) ([]T, error)
- func (preparedFetch *PreparedFetch[T]) FetchCursor(ctx context.Context, params ...Parameter) (cursor *Cursor[T], err error)
- func (preparedFetch *PreparedFetch[T]) FetchOne(ctx context.Context, params ...Parameter) (T, error)
- type Query
- type Result
- type Row
- func (row *Row) Bool(format string, values ...any) bool
- func (row *Row) Bytes(b []byte, format string, values ...any) []byte
- func (row *Row) Float64(format string, values ...any) float64
- func (row *Row) Int(format string, values ...any) int
- func (row *Row) Int64(format string, values ...any) int64
- func (row *Row) NullBool(format string, values ...any) sql.NullBool
- func (row *Row) NullFloat64(format string, values ...any) sql.NullFloat64
- func (row *Row) NullInt64(format string, values ...any) sql.NullInt64
- func (row *Row) NullString(format string, values ...any) sql.NullString
- func (row *Row) NullTime(format string, values ...any) sql.NullTime
- func (row *Row) Scan(destPtr any, format string, values ...any)
- func (row *Row) String(format string, values ...any) string
- func (row *Row) Time(format string, values ...any) time.Time
- func (row *Row) UUID(format string, values ...any) [16]byte
- type SQLWriter
- type Timestamp
Constants ¶
const ( DialectSQLite = "sqlite" DialectPostgres = "postgres" DialectMySQL = "mysql" DialectSQLServer = "sqlserver" )
Dialects supported.
Variables ¶
This section is empty.
Functions ¶
func EscapeQuote ¶
EscapeQuote will escape the relevant quote in a string by doubling up on it (as per SQL rules).
func FetchExists ¶
FetchExists returns a boolean indicating if running the given Query on the given DB returned any results.
func ParseBytes ¶
ParseBytes decodes b into a UUID or returns an error. Both the UUID form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
func QuoteIdentifier ¶
QuoteIdentifier quotes an identifier if necessary using dialect-specific quoting rules.
func Sprint ¶
Sprint is the equivalent of Sprintf but for converting a single value into its SQL representation.
func Sprintf ¶
Sprintf will interpolate SQL args into a query string containing prepared statement parameters. It returns an error if an argument cannot be properly represented in SQL.
func UUID ¶
UUIDValue takes in a type whose underlying type must be a [16]byte and returns a driver.Valuer.
func WriteValue ¶
func WriteValue(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int, value any) error
WriteValue is the equivalent of Writef but for writing a single value into the Output.
func Writef ¶
func Writef(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int, format string, values []any) error
Writef is a fmt.Sprintf-style function that will write a format string and values slice into an Output. The only recognized placeholder is '{}'. Placeholders can be anonymous (e.g. {}), ordinal (e.g. {1}, {2}, {3}) or named (e.g. {name}, {email}, {age}).
- Anonymous placeholders refer to successive values in the values slice. Anonymous placeholders are treated like a series of incrementing ordinal placeholders.
- Ordinal placeholders refer to a specific value in the values slice using 1-based indexing.
- Named placeholders refer to their corresponding sql.NamedArg value in the values slice. If there are multiple sql.NamedArg values with the same name, the last one wins.
If a value is an SQLWriter, its WriteSQL method will be called. Else if a value is a slice, it will undergo slice expansion (https://bokwoon.neocities.org/sq.html#value-expansion). Otherwise, the value is added to the query args slice.
Types ¶
type Cursor ¶
type Cursor[T any] struct { // contains filtered or unexported fields }
A Cursor represents a database cursor.
func FetchCursor ¶
func FetchCursor[T any](ctx context.Context, db DB, query Query, rowmapper func(*Row) T) (cursor *Cursor[T], err error)
FetchCursor returns a new cursor.
type DB ¶
type DB interface { QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) }
DB is a database/sql abstraction that can query the database. *sql.Conn, *sql.DB and *sql.Tx all implement DB.
type DialectCase ¶
type DialectExpression ¶
type DialectExpression struct { Default any Cases []DialectCase }
type DialectValuer ¶
DialectValuer is any type that will yield a different driver.Valuer depending on the SQL dialect.
type Expression ¶
Expression represents an SQL expression.
func Expr ¶
func Expr(format string, values ...any) Expression
Expr creates a new Expression using Writef syntax.
func (Expression) Append ¶
func (expr Expression) Append(format string, values ...any) Expression
type Parameter ¶
Parameter is identical to sql.NamedArg, but implements the Field interface.
func BytesParam ¶
BytesParam creates a new Parameter using a []byte value.
func Float64Param ¶
Float64Param creates a new NumberParameter from an float64 value.
func Int64Param ¶
Int64Param creates a new Parameter from an int64 value.
func StringParam ¶
StringParam creates a new Parameter from a string value.
type PreparedExec ¶
type PreparedExec struct {
// contains filtered or unexported fields
}
PreparedExec represents a exec query that wraps an explicitly-prepared *sql.Stmt. Such a query can be run for different prepared statement parameters without having to re-prepare the query in the database.
func PrepareExec ¶
PrepareExec takes a database instance + query and converts it to a PreparedExec.
func (*PreparedExec) Close ¶
func (preparedExec *PreparedExec) Close() error
Close closes the PreparedExec.
type PreparedFetch ¶
type PreparedFetch[T any] struct { // contains filtered or unexported fields }
PreparedFetch represents a fetch query that wraps an explicitly-prepared *sql.Stmt. Such a query can be run for different prepared statement parameters without having to re-prepare the query in the database.
func PrepareFetch ¶
func PrepareFetch[T any](ctx context.Context, db DB, query Query, rowmapper func(*Row) T) (preparedFetch *PreparedFetch[T], err error)
PrepareFetch takes a database instance + query + rowmapper and converts it to a PreparedFetch.
func (*PreparedFetch[T]) Close ¶
func (preparedFetch *PreparedFetch[T]) Close() error
Close closes the PreparedFetch.
func (*PreparedFetch[T]) FetchAll ¶
func (preparedFetch *PreparedFetch[T]) FetchAll(ctx context.Context, params ...Parameter) ([]T, error)
FetchAll returns all the results from running the PreparedFetch with the given params.
func (*PreparedFetch[T]) FetchCursor ¶
func (preparedFetch *PreparedFetch[T]) FetchCursor(ctx context.Context, params ...Parameter) (cursor *Cursor[T], err error)
FetchCursor fetches runs the PreparedFetch with the given params and returns a database cursor.
type Query ¶
type Query struct { Dialect string // Database dialect. Format string // Query format string. Values []any // Query values. Debug bool // If Debug is true, the query is logged to console. }
Query represents a database query.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row represents the state of a row after a call to rows.Next().
func (*Row) Bytes ¶
Bytes returns the []byte value of the expression.
If an existing byte slice is provided, it appends into to that slice (allowing for byte slices to be reused). It returns a nil slice if the expression is NULL, unless a byte slice was provided in which case it returns the same byte slice but with its length truncated to 0.
func (*Row) NullFloat64 ¶
func (row *Row) NullFloat64(format string, values ...any) sql.NullFloat64
NullFloat64 returns the sql.NullFloat64 valye of the expression.
func (*Row) NullString ¶
func (row *Row) NullString(format string, values ...any) sql.NullString
NullString returns the sql.NullString value of the expression.
type SQLWriter ¶
type SQLWriter interface { // WriteSQL writes the SQL representation of the SQLWriter into the query // string (*bytes.Buffer) and args slice (*[]any). // // The params map is used to hold the mappings between named parameters in // the query to the corresponding index in the args slice and is used for // rebinding args by their parameter name. The params map may be nil, check // first before writing to it. WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error }
SQLWriter is anything that can be converted to SQL.
type Timestamp ¶
Timestamp is as a replacement for sql.NullTime but with the following enhancements:
1. Timestamp.Value() returns an int64 unix timestamp if the dialect is SQLite, otherwise it returns a time.Time (similar to sql.NullTime).
2. Timestamp.Scan() additionally supports scanning from int64 and text (string/[]byte) values on top of what sql.NullTime already supports. The following text timestamp formats are supported:
var timestampFormats = []string{ "2006-01-02 15:04:05.999999999-07:00", "2006-01-02T15:04:05.999999999-07:00", "2006-01-02 15:04:05.999999999", "2006-01-02T15:04:05.999999999", "2006-01-02 15:04:05", "2006-01-02T15:04:05", "2006-01-02 15:04", "2006-01-02T15:04", "2006-01-02", }
func NewTimestamp ¶
func (*Timestamp) DialectValuer ¶
DialectValuer implements the DialectValuer interface.
func (*Timestamp) Scan ¶
Scan implements the sql.Scanner interface. It additionally supports scanning from int64 and text (string/[]byte) values on top of what sql.NullTime already supports. The following text timestamp formats are supported:
var timestampFormats = []string{ "2006-01-02 15:04:05.999999999-07:00", "2006-01-02T15:04:05.999999999-07:00", "2006-01-02 15:04:05.999999999", "2006-01-02T15:04:05.999999999", "2006-01-02 15:04:05", "2006-01-02T15:04:05", "2006-01-02 15:04", "2006-01-02T15:04", "2006-01-02", }