noorm

package
v1.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoDatabaseInContext = errors.New("noorm: no database in context")
	ErrNoQuerierInContext  = errors.New("noorm: no querier in context")
)
View Source
var (
	ErrInvalidArg = errors.New("noorm: invalid arg")
)
View Source
var (
	ErrInvalidTargetType = errors.New("noorm: invalid target type")
)

Functions

func Begin

func Begin(ctx context.Context, opts *sql.TxOptions) (context.Context, *sql.Tx, error)

func Exec

func Exec(ctx context.Context, query QuerySource) (sql.Result, error)

Exec executes a query without returning rows. Exec expects a Querier to be present in the context (see WithDatabase).

func QuerierFrom

func QuerierFrom(ctx context.Context) (Querier, Dialect, error)

func Query

func Query[T Struct](ctx context.Context, query QuerySource) ([]T, error)

Query executes a query and returns a slice of T. Query expects a Querier to be present in the context (see WithDatabase).

func QueryFirst

func QueryFirst[T Struct](ctx context.Context, query QuerySource) (*T, error)

QueryFirst executes a query and returns the first result. If the query yields no rows, sql.ErrNoRows is returned. If the query yields more than one row, the remaining rows are discarded. QueryFirst expects a Querier to be present in the context (see WithDatabase).

func WithDatabase

func WithDatabase(ctx context.Context, db *Database) context.Context

Types

type ArgumentSource

type ArgumentSource interface {
	// contains filtered or unexported methods
}

ArgumentSource captures the provided named or positional arguments as a single type. See `Named`, `Positional` and `None` for implementations.

func Named

func Named(args Struct) ArgumentSource

Named uses the fields of a struct as named arguments for a query. Field names can be overwritten with struct tags.

func None

func None() ArgumentSource

None is used when you do not need to provide any arguments for a query.

func Positional

func Positional(args ...any) ArgumentSource

Positional uses the positional index of the the provided args as their name in a query. The index starts counting at 0.

type Database

type Database struct {
	*sql.DB
	// contains filtered or unexported fields
}

func New

func New(db *sql.DB, dialect Dialect) *Database

func Open

func Open(driverName, dataSourceName string) (*Database, error)

type Dialect

type Dialect interface {
	// Placeholder returns a positional argument placeholder.
	// The parameter `position` is the index of the parameter starting at 0.
	Placeholder(position int) string
	// QuoteIdentifier quotes an identifier (eg. column or table name).
	QuoteIdentifier(identifier string) string
}

Dialect provides database specific sql query helpers.

type Insert

type Insert struct {
	Tablename string
	Model     Struct
	Returning bool
	// contains filtered or unexported fields
}

type Iterator

type Iterator[T Struct] interface {
	// Next proceeds with the next row.
	// Next must be called before the first row can be scanned.
	Next() bool
	// Err returns the latest iteration error andshould be checked whenever Next returns false.
	Err() error
	// Value scans the current row into a new T.
	Value() (T, error)
	// Close closes the underlying *sql.Rows.
	Close() error
}

Iterator is a typed wrapper for *sql.Rows, which scans rows into T.

func Iterate

func Iterate[T Struct](ctx context.Context, query QuerySource) (Iterator[T], error)

Iterate executes a query and returns an iterator of the rows. Iterate expects a Querier to be present in the context (see WithDatabase).

type Querier

type Querier interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
}

Querier is an abstraction over both *sql.DB and *sql.Tx.

type QuerySource

type QuerySource interface {
	// contains filtered or unexported methods
}

type SQL

type SQL struct {
	Query string
	Args  ArgumentSource
	// contains filtered or unexported fields
}

type Struct

type Struct any

Struct must be a struct.

Jump to

Keyboard shortcuts

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