qrm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: Apache-2.0, BSD-3-Clause, MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRows = errors.New("qrm: no rows in result set")

ErrNoRows is returned by Query when query result set is empty

View Source
var GlobalConfig = Config{
	StrictScan:        false,
	JsonUnmarshalFunc: json.Unmarshal,
}

GlobalConfig is the package-wide configuration for SQL scanning. This variable is not thread safe, and it should be modified only once, for instance, during application initialization.

Functions

func Query

func Query(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error)

Query executes a Query Result Mapping (QRM) of the provided SQL `query` with a list of parameterized arguments `args` over the database connection `db` using the provided context `ctx` and stores the result in the destination `destPtr`.

The destination must be a pointer to either a struct or a slice of structs If the destination is a pointer to a struct and no rows are returned, the method returns qrm.ErrNoRows.

Parameters:

ctx      - The context for managing query execution (timeouts, cancellations).
db       - The database connection or transaction implementing the Queryable interface.
query    - The SQL query string to be executed.
args     - A slice of arguments to be used with the query.
destPtr  - A pointer to the variable where the query result will be stored. This can be a pointer to a struct or a slice of structs.

Returns:

rowsProcessed - The number of rows processed by the query execution.
err           - An error if query execution or result mapping fails, or if no rows are found when a struct is expected.

func QueryJsonArr

func QueryJsonArr(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error)

QueryJsonArr executes a SQL query that returns a JSON array, unmarshals the result into the provided destination, and returns the number of rows processed.

The query must return exactly one row with a single column; otherwise, an error is returned.

Parameters:

ctx      - The context for managing query execution (timeouts, cancellations).
db       - The database connection or transaction that implements the Queryable interface.
query    - The SQL query string to be executed.
args     - A slice of arguments to be used with the query.
destPtr  - A pointer to the variable where the unmarshaled JSON array will be stored.
          The destination should be a pointer to a slice of structs or []map[string]any.

Returns:

rowsProcessed - The number of rows processed by the query execution.
err           - An error if query execution or unmarshaling fails.

func QueryJsonObj

func QueryJsonObj(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error)

QueryJsonObj executes a SQL query that returns a JSON object, unmarshals the result into the provided destination, and returns the number of rows processed.

The query must return exactly one row with a single column; otherwise, an error is returned.

Parameters:

ctx      - The context for managing query execution (timeouts, cancellations).
db       - The database connection or transaction that implements the Queryable interface.
query    - The SQL query string to be executed.
args     - A slice of arguments to be used with the query.
destPtr  - A pointer to the variable where the unmarshaled JSON result will be stored.
          The destination should be a pointer to a struct or map[string]any.

Returns:

rowsProcessed - The number of rows processed by the query execution.
err           - An error if query execution or unmarshaling fails.

func ScanOneRowToDest

func ScanOneRowToDest(scanContext *ScanContext, rows *sql.Rows, destPtr interface{}) error

ScanOneRowToDest will scan one row into struct destination

Types

type Config

type Config struct {
	// StrictScan, when true, causes the scanning function to panic if it encounters any
	// unused columns in the SQL query result. This ensures that every column is mapped
	// to a field in the destination struct.
	// Does not apply to statements build with SELECT_JSON_OBJ or SELECT_JSON_ARR
	StrictScan bool

	// JsonUnmarshalFunc is called by the Query method to unmarshal JSON query results created by
	// SELECT_JSON_OBJ and SELECT_JSON_ARR statements.
	// It can be replaced with any implementation that matches the standard "encoding/json" `Unmarshal` function signature.
	// By default, it uses the `Unmarshal` function from Go's standard `encoding/json` package.
	JsonUnmarshalFunc func(data []byte, v any) error
}

Config holds the configuration settings for QRM scanning behavior.

type DB

type DB interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

DB is common database interface used by query result mapping Both *sql.DB and *sql.Tx implements DB interface

type Executable

type Executable interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Executable interface for sql ExecContext method

type Queryable

type Queryable interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

Queryable interface for sql QueryContext method

type ScanContext

type ScanContext struct {
	// contains filtered or unexported fields
}

ScanContext contains information about current row processed, mapping from the row to the destination types and type grouping information.

func NewScanContext

func NewScanContext(rows *sql.Rows) (*ScanContext, error)

NewScanContext creates new ScanContext from rows

func (*ScanContext) EnsureEveryColumnRead

func (s *ScanContext) EnsureEveryColumnRead()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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