sqlt

package module
v1.7.2 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

README

sqlt

GitHub go.mod Go version GitHub release (latest by date) Go Report Card

Minimal set of useful things to work with database in Go.

Installation

go get -u github.com/mono83/sqlt

Provided custom types

  • sqlt.TrueFalse - to read boolean value stored in database as enum(true,false)
  • sqlt.UnixSecinds - to read time.Time value stored in database as integer unix timestamp in seconds

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCallbackNotSet = errors.New("callback not set")

ErrCallbackNotSet is returned by CallbackDB when callback for corresponding method not set.

View Source
var ErrDurationNanosUnsupportedType = errors.New("unsupported type")
View Source
var ErrJSONUnsupportedType = errors.New("unsupported type")
View Source
var ErrUnixMillisUnsupportedType = errors.New("unsupported type")
View Source
var ErrUnixSecondsUnsupportedType = errors.New("unsupported type")

Functions

func InsertID

func InsertID(result sql.Result, err error) (int64, error)

InsertID returns auto incremental identifier of last inserted entity as int64.

func InsertIDU

func InsertIDU(result sql.Result, err error) (uint64, error)

InsertIDU returns auto incremental identifier of last inserted entity as uint64.

func IterateScan

func IterateScan(rows *sql.Rows, callback func([]string, []*sql.ColumnType, []any)) error

IterateScan scans given rows ony by one passing obtained data to callback function.

func IterateScanE added in v1.6.0

func IterateScanE(rows *sql.Rows, callback func([]string, []*sql.ColumnType, []any) error) error

IterateScanE scans given rows ony by one passing obtained data to callback function. On any error stops iteration and returns it.

func PlaceholdersString added in v1.6.0

func PlaceholdersString(n int) string

PlaceholdersString constructs placeholders string like ?,?,?

func StdConvert

func StdConvert(src any, def *sql.ColumnType) any

StdConvert performs conversion from interface{} to defined in sql.ColumnType type using standard ruleset.

Types

type CallbackDB added in v1.2.1

type CallbackDB struct {
	OnGet    func(dest any, query string, args ...any) error
	OnSelect func(dest any, query string, args ...any) error
	OnExec   func(query string, args ...any) (sql.Result, error)
}

CallbackDB is the simplest implementation of ReaderExecutor interface providing to configure which function will be invoked on corresponding method. This can be useful in unit tests

func (CallbackDB) Exec added in v1.2.1

func (c CallbackDB) Exec(query string, args ...any) (sql.Result, error)

Exec is Executor interface implementation

func (CallbackDB) Get added in v1.2.1

func (c CallbackDB) Get(dest any, query string, args ...any) error

Get is Getter interface implementation

func (CallbackDB) Select added in v1.2.1

func (c CallbackDB) Select(dest any, query string, args ...any) error

Select is Selector interface implementation

type DurationNanos added in v1.7.2

type DurationNanos int64

DurationNanos is type duration in nanoseconds

func (DurationNanos) Duration added in v1.7.2

func (u DurationNanos) Duration() time.Duration

Duration returns Go's duration type

func (DurationNanos) Int64 added in v1.7.2

func (u DurationNanos) Int64() int64

Int64 returns nanoseconds count

func (DurationNanos) NativeSQL added in v1.7.2

func (u DurationNanos) NativeSQL() any

NativeSQL returns data in SQL native format

func (*DurationNanos) Scan added in v1.7.2

func (u *DurationNanos) Scan(src any) error

Scan is sql.Scanner interface implementation

func (DurationNanos) String added in v1.7.2

func (u DurationNanos) String() string

String return string representation of time in UTC

func (DurationNanos) Value added in v1.7.2

func (u DurationNanos) Value() (driver.Value, error)

Value is a driver.Valuer interface implementation

type Executor

type Executor interface {
	Exec(query string, args ...any) (sql.Result, error)
}

Executor is a thin interface for database connection capable to modify data

type GenericListReader added in v1.6.0

type GenericListReader[I, T any] func(ID ...I) ([]T, error)

GenericListReader defines accessor to database providing ability to read list(slice) of records of type [T] using slice of identifiers of type [I]

type GenericReader added in v1.6.0

type GenericReader[I, T any] func(ID I) (*T, error)

GenericReader defines accessor to database providing ability to read single entity of type [T] using identifier with type [I]

type Getter

type Getter interface {
	Get(dest any, query string, args ...any) error
}

Getter is a thin interface for database connection capable to read data

type JSON added in v1.7.2

type JSON string

JSON wraps value stored in JSON format

func (JSON) NativeSQL added in v1.7.2

func (j JSON) NativeSQL() any

NativeSQL returns data in SQL native format

func (*JSON) Scan added in v1.7.2

func (j *JSON) Scan(src any) error

Scan is sql.Scanner interface implementation

func (JSON) String added in v1.7.2

func (j JSON) String() string

String return JSON string

func (JSON) Unmarshal added in v1.7.2

func (j JSON) Unmarshal(target any) error

Unmarshal reads value into given target struct

func (JSON) Value added in v1.7.2

func (j JSON) Value() (driver.Value, error)

Value is a sql.driver.Valuer interface implementation

type Querier added in v1.5.0

type Querier interface {
	Query(query string, args ...any) (*sql.Rows, error)
}

Querier is a thin interface for database connection capable to query data

type QuerierExecutor added in v1.6.0

type QuerierExecutor interface {
	Querier
	Executor
}

QuerierExecutor is a thin interface for database connection capable to both read and write data

type Reader

type Reader interface {
	Getter
	Selector
}

Reader is an interface defining connection able to perform read operations

type ReaderExecutor

type ReaderExecutor interface {
	Reader
	Executor
}

ReaderExecutor is a thin interface for database connection capable to both read and write data

type Rowset

type Rowset struct {
	ColumnNames []string
	ColumnTypes []*sql.ColumnType
	Rows        [][]any
}

Rowset is plain two-dimensional data table containing data, obtained from SQL database in non-structured way (via interface{})

func (*Rowset) Clear

func (r *Rowset) Clear()

Clear cleans up rowset contents.

func (Rowset) Each

func (r Rowset) Each(f func([]string, []*sql.ColumnType, []any))

Each iterated over each row, passing it with corresponding metadata to callback function.

func (Rowset) MapValues

func (r Rowset) MapValues(f func(src any, def *sql.ColumnType) any) Rowset

MapValues maps all values in rowset using given conversion function.

func (Rowset) MapValuesStd

func (r Rowset) MapValuesStd() Rowset

MapValuesStd maps all values in rowset using standard conversion function.

func (*Rowset) Scan

func (r *Rowset) Scan(rows *sql.Rows) error

Scan method fills rowset contents using data from given sql.Rows.

func (Rowset) Size

func (r Rowset) Size() int

Size returns amount of rows.

func (Rowset) SliceMap

func (r Rowset) SliceMap() (out []map[string]any)

SliceMap returns content of rowset as slice of maps.

type Selector

type Selector interface {
	Select(dest any, query string, args ...any) error
}

Selector is a thin interface for database connection capable to read data

type TrueFalse

type TrueFalse bool

TrueFalse wraps values stored in database as enum(true,false)

func (TrueFalse) Bool

func (t TrueFalse) Bool() bool

Bool returns boolean representation

func (TrueFalse) IsTrue

func (t TrueFalse) IsTrue() bool

IsTrue returns boolean representation, is an alias to Bool

func (TrueFalse) NativeSQL

func (t TrueFalse) NativeSQL() any

NativeSQL returns data in SQL native format

func (*TrueFalse) Scan

func (t *TrueFalse) Scan(value any) error

Scan is sql.Scanner interface implementation

func (*TrueFalse) String

func (t *TrueFalse) String() string

String returns string representation of boolean value

func (TrueFalse) Value

func (t TrueFalse) Value() (driver.Value, error)

Value is a sql.driver.Valuer interface implementation

type UnixMillis added in v1.7.2

type UnixMillis int64

UnixMillis is type containing timestamp in unix milliseconds

func (UnixMillis) Int64 added in v1.7.2

func (u UnixMillis) Int64() int64

Int64 returns unix milliseconds as int64

func (UnixMillis) NativeSQL added in v1.7.2

func (u UnixMillis) NativeSQL() any

NativeSQL returns data in SQL native format

func (*UnixMillis) Scan added in v1.7.2

func (u *UnixMillis) Scan(src any) error

Scan is sql.Scanner interface implementation

func (UnixMillis) String added in v1.7.2

func (u UnixMillis) String() string

String return string representation of time in UTC

func (UnixMillis) Time added in v1.7.2

func (u UnixMillis) Time() time.Time

Time return unix milliseconds formatted as UTC time

func (UnixMillis) Value added in v1.7.2

func (u UnixMillis) Value() (driver.Value, error)

Value is a driver.Valuer interface implementation

type UnixSeconds

type UnixSeconds int64

UnixSeconds is type containing timestamp in unix seconds

func (UnixSeconds) Int64

func (u UnixSeconds) Int64() int64

Int64 returns unix seconds as int64

func (UnixSeconds) NativeSQL

func (u UnixSeconds) NativeSQL() any

NativeSQL returns data in SQL native format

func (*UnixSeconds) Scan

func (u *UnixSeconds) Scan(src any) error

Scan is sql.Scanner interface implementation

func (UnixSeconds) String

func (u UnixSeconds) String() string

String return string representation of time in UTC

func (UnixSeconds) Time

func (u UnixSeconds) Time() time.Time

Time return unix seconds formatted as UTC time

func (UnixSeconds) Value

func (u UnixSeconds) Value() (driver.Value, error)

Value is a driver.Valuer interface implementation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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