Documentation ¶
Index ¶
- Variables
- func Begin(ctx context.Context, opts *sql.TxOptions) (context.Context, *sql.Tx, error)
- func Exec(ctx context.Context, query QuerySource) (sql.Result, error)
- func QuerierFrom(ctx context.Context) (Querier, Dialect, error)
- func Query[T Struct](ctx context.Context, query QuerySource) ([]T, error)
- func QueryFirst[T Struct](ctx context.Context, query QuerySource) (*T, error)
- func WithDatabase(ctx context.Context, db *Database) context.Context
- type ArgumentSource
- type Database
- type Dialect
- type Insert
- type Iterator
- type Querier
- type QuerySource
- type SQL
- type Struct
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoDatabaseInContext = errors.New("noorm: no database in context") ErrNoQuerierInContext = errors.New("noorm: no querier in context") )
var (
ErrInvalidArg = errors.New("noorm: invalid arg")
)
var (
ErrInvalidTargetType = errors.New("noorm: invalid target type")
)
Functions ¶
func Exec ¶
Exec executes a query without returning rows. Exec expects a Querier to be present in the context (see WithDatabase).
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).
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 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 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.
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 }