sqlx

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sqlx contains utilities for working with SQL databases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Begin

func Begin(ctx context.Context, db *sql.DB) *sql.Tx

Begin starts a new transaction.

func Commit

func Commit(tx *sql.Tx)

Commit commits the given transaction.

func Conn

func Conn(ctx context.Context, db *sql.DB) *sql.Conn

Conn returns a single connection from the pool or panics if unable to do so.

func Exec

func Exec(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
) sql.Result

Exec executes a statement on the given DB.

func ExecRow

func ExecRow(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
)

ExecRow executes a statement on the given DB.

It panics if the statement does not affect exactly one row.

Note that for MySQL an column value must actually change to consider the row updated. Further, it returns a value of 2 for an INSERT .. ON DUPLICATE KEY UPDATE that results in a change to an existing row.

func Insert

func Insert(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
) int64

Insert executes an insert statement on the given DB and returns the last insert ID.

func Must

func Must(err error)

Must panics if err is non-nil.

func Prepare

func Prepare(ctx context.Context, db DB, query string) *sql.Stmt

Prepare prepares a statement or panics if unable to do so.

func QueryBool

func QueryBool(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
) (v bool)

QueryBool executes a single-column, single-row query on the given DB and returns a single bool result.

func QueryInt64

func QueryInt64(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
) (v int64)

QueryInt64 executes a single-column, single-row query on the given DB and returns a single uint64 result.

func QueryInto

func QueryInto(
	ctx context.Context,
	db DB,
	value interface{},
	query string,
	args ...interface{},
)

QueryInto executes single-column, single-row query on the given DB and scans the result into a value.

func Recover

func Recover(err *error)

Recover recovers from a panic caused by one of the MustXXX() functions.

It is intended to be used in a defer statement. The error that caused the panic is assigned to *err.

func Scan

func Scan(rows Scanner, targets ...interface{})

Scan scans values from a row or row-set.

func ScanInt64

func ScanInt64(rows Scanner) int64

ScanInt64 scans a single int64 alue from a row or row-set.

func TryExecRow

func TryExecRow(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
) bool

TryExecRow executes a statement on the given DB.

It returns false if the update does not affect exactly one row.

Note that for MySQL an column value must actually change to consider the row updated. Further, it returns a value of 2 for an INSERT .. ON DUPLICATE KEY UPDATE that results in a change to an existing row.

func TryInsert

func TryInsert(
	ctx context.Context,
	db DB,
	query string,
	args ...interface{},
) (int64, bool)

TryInsert executes an insert statement on the given DB and returns the last insert ID.

If no rows were affected, it returns false.

Types

type DB

type DB interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

DB is an interface satisfied by *sql.DB, *sql.Conn and *sql.Tx.

type PanicSentinel

type PanicSentinel struct {
	// Cause is the error that caused the panic.
	Cause error
}

PanicSentinel is a wrapper value used to identify panic's that are caused by one of the MustXXX() functions.

type Scanner

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

Scanner is an interface satisfied by *sql.Rows and *sql.Row.

Jump to

Keyboard shortcuts

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