sqlz

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sqlz contains core types such as Kind and Record.

Index

Constants

View Source
const (
	TableTypeTable   = "table"
	TableTypeView    = "view"
	TableTypeVirtual = "virtual"
)

Canonical driver-independent names for table types.

Variables

View Source
var (
	RTypeInt         = reflect.TypeOf(0)
	RTypeInt8        = reflect.TypeOf(int8(0))
	RTypeInt16       = reflect.TypeOf(int16(0))
	RTypeInt32       = reflect.TypeOf(int32(0))
	RTypeInt64       = reflect.TypeOf(int64(0))
	RTypeNullInt64   = reflect.TypeOf(sql.NullInt64{})
	RTypeDecimal     = reflect.TypeOf(decimal.Decimal{})
	RTypeNullDecimal = reflect.TypeOf(decimal.NullDecimal{})
	RTypeFloat32     = reflect.TypeOf(float32(0))
	RTypeFloat64     = reflect.TypeOf(float64(0))
	RTypeNullFloat64 = reflect.TypeOf(sql.NullFloat64{})
	RTypeBool        = reflect.TypeOf(true)
	RTypeNullBool    = reflect.TypeOf(sql.NullBool{})
	RTypeString      = reflect.TypeOf("")
	RTypeNullString  = reflect.TypeOf(sql.NullString{})
	RTypeTime        = reflect.TypeOf(time.Time{})
	RTypeNullTime    = reflect.TypeOf(sql.NullTime{})
	RTypeBytes       = reflect.TypeOf([]byte{})
	RTypeNil         = reflect.TypeOf(nil)
	RTypeAny         = reflect.TypeOf((any)(nil))
)

Cached results from reflect.TypeOf for commonly used types.

Functions

func CloseRows added in v0.48.0

func CloseRows(log *slog.Logger, rows *sql.Rows)

CloseRows closes the supplied *sql.Rows, logging a warning on close errors other than context errors. If log is nil, no logging is performed. If rows is nil, CloseRows is no-op.

func ConnectorWith added in v0.43.0

func ConnectorWith(impl driver.Connector, fn func(ctx context.Context, conn driver.Conn) error) driver.Connector

ConnectorWith returns a stdlib driver.Connector that wraps the supplied impl, applying the non-nil fn to the connection returned by Connect. If fn is nil, impl is returned unchanged.

If impl implements io.Closer, the returned connector will also implement io.Closer. This behavior conforms to the expectations of driver.Connector:

If a Connector implements io.Closer, the sql package's DB.Close
method will call Close and return error (if any).

func ExecAffected added in v0.15.0

func ExecAffected(ctx context.Context, db Execer, query string, args ...any) (affected int64, err error)

ExecAffected invokes db.ExecContext, returning the count of rows affected and any error.

func RequireSingleConn added in v0.43.0

func RequireSingleConn(db DB) error

RequireSingleConn returns nil if db is a type that guarantees a single database connection. That is, RequireSingleConn returns an error if db does not have type *sql.Conn or *sql.Tx.

func RowsScanColumn added in v0.47.4

func RowsScanColumn[T any](ctx context.Context, rows *sql.Rows) (vals []T, err error)

RowsScanColumn scans a single-column *sql.Rows into a slice of T. If the returned value could be null, use a nullable type, e.g. sql.NullString. Arg rows is always closed. On any error, the returned slice is nil.

Types

type DB

type DB interface {
	Execer
	Queryer
	Preparer
}

DB is a union of Execer, Queryer and Preparer. DB's method set is implemented by sql.DB, sql.Conn and sql.Tx. This can probably be better named, as it conflicts with sql.DB.

type Execer

type Execer interface {
	// ExecContext is documented by sql.DB.ExecContext.
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Execer abstracts the ExecContext method from sql.DB and friends.

type NullBool

type NullBool struct {
	sql.NullBool
}

NullBool is similar to sql.NullBool, but accepts more boolean representations, e.g. "YES", "Y", "NO", "N", etc. These boolean values are returned by SQL Server and Postgres at times.

func (*NullBool) Scan

func (n *NullBool) Scan(value any) error

Scan implements the Scanner interface.

type Preparer

type Preparer interface {
	// PrepareContext is documented by sql.DB.PrepareContext.
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Preparer abstracts the PrepareContext method from sql.DB and friends.

type Queryer

type Queryer interface {
	// QueryContext is documented by sql.DB.QueryContext.
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

	// QueryRowContext is documented by sql.DB.QueryRowContext.
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Queryer abstracts the QueryContext and QueryRowContext methods from sql.DB and friends.

Jump to

Keyboard shortcuts

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