Documentation ¶
Index ¶
- Constants
- func DbTxDone[A DbTx](val A)
- func DbTxDoneWith[A DbTx](val A, err error)
- func ScanAny[Src Rows](src Src, out any)
- func ScanNext[Row any, Src ColumnerScanner](src Src) Row
- func ScanReflect[Src Rows](src Src, out r.Value)
- func ScanVal[Row any, Src Rows](src Src) Row
- func ScanVals[Row any, Src Rows](src Src) (out []Row)
- type Arr
- func (self Arr[A]) AppendInner(buf []byte) []byte
- func (self Arr[A]) AppendTo(buf []byte) []byte
- func (self *Arr[A]) Clear()
- func (self Arr[A]) IsNull() bool
- func (self *Arr[A]) Parse(src string) (err error)
- func (self *Arr[A]) Scan(src any) error
- func (self Arr[A]) String() string
- func (self Arr[A]) Value() (driver.Value, error)
- type ColumnerScanner
- type Db
- type DbConn
- type DbTx
- type DbTxer
- type Like
- type Rows
- type Rune
- func (self Rune) AppendTo(buf []byte) []byte
- func (self *Rune) Clear()
- func (self Rune) IsNotNull() bool
- func (self Rune) IsNull() bool
- func (self Rune) MarshalJSON() ([]byte, error)
- func (self Rune) MarshalText() ([]byte, error)
- func (self *Rune) Parse(src string) error
- func (self *Rune) Scan(src any) error
- func (self Rune) String() string
- func (self *Rune) UnmarshalJSON(src []byte) error
- func (self *Rune) UnmarshalText(src []byte) error
- func (self Rune) Value() (driver.Value, error)
Constants ¶
const ErrMultipleRows gg.ErrStr = `expected one row, got multiple`
Returned by `ScanVal` and `ScanAny` when there are too many rows.
Variables ¶
This section is empty.
Functions ¶
func DbTxDone ¶
func DbTxDone[A DbTx](val A)
Must be deferred. Commit if there was no panic, rollback if there was a panic. Usage:
defer DbTxDone(conn)
func DbTxDoneWith ¶ added in v0.0.6
Commit if there was no error, rollback if there was an error. Used internally by `DbTxDone`.
func ScanAny ¶
Decodes `Rows` into the given dynamically typed output. Counterpart to `ScanVals` and `ScanVal` which are statically typed. Output must be a non-nil pointer to one of the following:
- Slice of scalars.
- Slice of structs.
- Single scalar.
- Single struct.
Always closes the rows. If output is not a slice, verifies that there is EXACTLY one row in total, otherwise panics.
func ScanNext ¶ added in v0.0.2
func ScanNext[Row any, Src ColumnerScanner](src Src) Row
Takes `Rows` and decodes the next row into a value of the given type. Output type must be either scalar or struct. Panics on errors. Must be called only after `Rows.Next`.
func ScanReflect ¶ added in v0.0.11
Variant of `ScanAny` that takes a reflect value rather than `any`.
Types ¶
type Arr ¶
type Arr[A any] []A
Short for "array". A slice type that supports SQL array encoding and decoding, using the `{}` format. Examples:
Arr[int]{10, 20} <-> '{10,20}' Arr[Arr[int]]{{10, 20}, {30, 40}} <-> '{{10,20},{30,40}}'
func ToArr ¶
Shortcut for converting to `Arr`. Workaround for the lack of type inference in type literals and casts. This is a free cast with no reallocation.
func (Arr[A]) AppendInner ¶
Same as `.AppenderTo` but without the enclosing `{}`.
func (Arr[A]) AppendTo ¶ added in v0.1.6
Implement `AppenderTo`, appending the array's SQL encoding to the buffer. If the slice is nil, appends nothing.
type ColumnerScanner ¶
Sub-interface of `Rows` used by `ScanNext`.
type DbConn ¶
type DbConn interface { QueryContext(context.Context, string, ...any) (*sql.Rows, error) ExecContext(context.Context, string, ...any) (sql.Result, error) }
Implemented by stdlib types such as `sql.Conn` and `sql.Tx`.
type Like ¶ added in v0.0.21
type Like string
Variant of `string` intended as an operand for SQL "like" and "ilike" operators. When generating an SQL argument via `.Value`, the string is wrapped in `%` to ensure partial match, escaping any pre-existing `%` and `_`. As a special case, an empty string is used as-is, and doesn't match anything when used with `like` or `ilike`.
func (Like) Esc ¶ added in v0.0.21
Returns an escaped string suitable as an operand for SQL "like" or "ilike". As a special case, an empty string is returned as-is.
type Rune ¶ added in v0.0.14
type Rune rune
Variant of Go `rune` compatible with text, JSON, and SQL (with caveats). In text and JSON, behaves like `string`. In Go and SQL, behaves like `rune`/`int32`. As a special case, zero value is considered empty in text, and null in JSON and SQL. When parsing, input must be empty or single char.
Some databases, or their Go drivers, may not support representing chars as int32. For example, Postgres doesn't have an analog of Go `rune`. Its "char" type is a variable-sized string. This type is not compatible with such databases.
func RuneFrom ¶ added in v0.0.14
Shortcut for casting an arbitrary rune-like into `Rune`. Useful for higher-order functions such as `gg.Map`.
func (Rune) AppendTo ¶ added in v0.1.6
Implement `AppenderTo`, appending the same representation as `.String`.
func (*Rune) Clear ¶ added in v0.0.14
func (self *Rune) Clear()
Implement `Clearer`. Zeroes the receiver.
func (Rune) MarshalJSON ¶ added in v0.0.14
Implement `json.Marshaler`. If `.IsNull`, returns a representation of JSON null. Otherwise uses an equivalent of `json.Marshal(self.String())`.
func (Rune) MarshalText ¶ added in v0.0.14
Implement `encoding.TextMarshaler`, returning the same representation as `.String`.
func (*Rune) Parse ¶ added in v0.0.14
Implement `Parser`. If the input is empty, clears the receiver via `.Clear`. If the input has more than one character, returns an error. Otherwise uses the first and only character from the input.
func (*Rune) Scan ¶ added in v0.0.14
Implement SQL `Scanner`, decoding arbitrary input, which must be one of:
- Nil -> use `.Clear`.
- Text -> use `.Parse`.
- Rune -> assign as-is.
func (Rune) String ¶ added in v0.0.14
Implement `fmt.Stringer`. If zero, returns an empty string. Otherwise returns a string containing exactly one character.
func (*Rune) UnmarshalJSON ¶ added in v0.0.14
Implement `json.Unmarshaler`. If the input is empty or represents JSON null, clears the receiver via `.Clear`. Otherwise requires the input to be a JSON string and decodes it via `.Parse`.
func (*Rune) UnmarshalText ¶ added in v0.0.14
Implement `encoding.TextUnmarshaler`, using the same logic as `.Parse`.