sqlr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: MIT Imports: 2 Imported by: 2

README

SQLR

PkgGoDev

Defines a default set of Interfaces to work with the default database/sql.

Querier

The sql.DB and the sql.Tx have the same signature when quering so for that we have created the sqlr.Querier which is fulfilled for either sql.DB and sql.Tx so in some contexts you don't need to have the specific implementation this interface can be used

Scanner

Used to abstract the logic of Scanning an sql.Row and sql.Rows reusable:

type Scanner interface {
  Scan(dest ...interface{}) error
}

Example usage:

func scanUsers(rows *sql.Rows) ([]*user.User, error) {
	users := make([]*user.User, 0, 0)
	defer rows.Close()
	for rows.Next() {
		u, err := scanUser(rows)
		if err != nil {
			return nil, err
		}
		users = append(users, u)
	}

	if err := rows.Err(); err != nil {
		return nil, err
	}

	return users, nil
}

func scanUser(s sqlr.Scanner) (*user.User, error) {
	var u user.User

	err := s.Scan(
		&u.Name,
	)
	if err != nil {
		return nil, err
	}

	return u, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executer

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

Executer executes request in a specific context

type Querier

type Querier interface {
	RowQuerier
	RowsQuerier
	Executer
}

Querier is a global request sender

type RowQuerier

type RowQuerier interface {
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

RowQuerier requests a row in a specific context

type RowsQuerier

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

RowsQuerier requests rows in a specific context

type Scanner

type Scanner interface {
	Scan(dest ...interface{}) error
}

Scanner is an abstraction of a type which can scan his values onto variables passed in dest. May be used for sql.Row and sql.Rows

Jump to

Keyboard shortcuts

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