sqli

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: MIT Imports: 6 Imported by: 0

README

go-sqli

Interfaces for Golang's database/sql and sqlx.

go-sqli implements a thin layer that wraps all sql.* types in interfaces. So instead of returning *sql.Tx struct, sqli.DB.Begin() returns sqli.Tx interface. The following table maps the original types to their respective interfaces.

Original Struct sqli Interface
sql.DB sqli.DB
sql.Tx sqli.Tx
sql.Conn sqli.Conn
sql.Stmt sqli.Stmt
sql.Rows sqli.Rows
sql.Row sqli.Row
sql.ColumnType sqli.ColumnType
sqlx.DB sqli.DBx
sqlx.Tx sqli.Txx
sqlx.Conn sqli.Connx
sqlx.Stmt sqli.Stmtx
sqlx.NamedStmt sqli.NamedStmt
sqlx.Rows sqli.Rowsx
sqlx.Row sqli.Rowx

Why?

To be able to inject mock, stub or fake implementations while testing database interactions. I am using it to generate mocks using gomock. In contrast to go-sqlmock, it is somewhat less awkward since it would fit nicely with a project's existing workflow.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnType

type ColumnType interface {
	DatabaseTypeName() string
	DecimalSize() (int64, int64, bool)
	Length() (int64, bool)
	Name() string
	Nullable() (bool, bool)
	ScanType() reflect.Type
}

type Conn

type Conn interface {
	BeginTx(context.Context, *sql.TxOptions) (Tx, error)
	Close() error
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PingContext(context.Context) error
	PrepareContext(context.Context, string) (Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) Row
	Raw(func(driverConn interface{}) error) error
}

type Connx

type Connx interface {
	Conn
	BeginTxx(context.Context, *sql.TxOptions) (Txx, error)
	GetContext(context.Context, interface{}, string, ...interface{}) error
	PreparexContext(context.Context, string) (Stmtx, error)
	QueryRowxContext(context.Context, string, ...interface{}) Rowx
	QueryxContext(context.Context, string, ...interface{}) (Rowsx, error)
	Rebind(string) string
	SelectContext(context.Context, interface{}, string, ...interface{}) error
}

type DB

type DB interface {
	Querier

	Begin() (Tx, error)
	BeginTx(context.Context, *sql.TxOptions) (Tx, error)
	Close() error
	Conn(context.Context) (Conn, error)
	Driver() driver.Driver
	Ping() error
	PingContext(context.Context) error
	SetConnMaxIdleTime(time.Duration)
	SetConnMaxLifetime(time.Duration)
	SetMaxIdleConns(int)
	SetMaxOpenConns(int)
	Stats() sql.DBStats
}

func Open

func Open(driverName, dsn string) (DB, error)

func OpenDB

func OpenDB(c driver.Connector) DB

func With

func With(db *sql.DB) DB

type DBx

type DBx interface {
	DB
	Querierx

	BeginTxx(context.Context, *sql.TxOptions) (Txx, error)
	Beginx() (Txx, error)
	Connx(context.Context) (Connx, error)
	DriverName() string
	MapperFunc(func(string) string)
	MustBegin() Txx
	MustBeginTx(context.Context, *sql.TxOptions) Txx
	NamedQueryContext(context.Context, string, interface{}) (Rowsx, error)
	Rebind(string) string
	Unsafe() DBx
}

func Connectx

func Connectx(driverName, dsn string) (DBx, error)

func ConnectxContext

func ConnectxContext(ctx context.Context, driverName, dsn string) (DBx, error)

func MustConnectx

func MustConnectx(driverName, dsn string) DBx

func MustOpenx

func MustOpenx(driverName, dsn string) DBx

func Openx

func Openx(driverName, dsn string) (DBx, error)

func Withx

func Withx(db *sqlx.DB) DBx

type NamedStmt

type NamedStmt interface {
	Close() error
	Exec(interface{}) (sql.Result, error)
	ExecContext(context.Context, interface{}) (sql.Result, error)
	Get(interface{}, interface{}) error
	GetContext(context.Context, interface{}, interface{}) error
	MustExec(interface{}) sql.Result
	MustExecContext(context.Context, interface{}) sql.Result
	Query(interface{}) (Rows, error)
	QueryContext(context.Context, interface{}) (Rows, error)
	QueryRow(interface{}) Rowx
	QueryRowContext(context.Context, interface{}) Rowx
	QueryRowx(interface{}) Rowx
	QueryRowxContext(context.Context, interface{}) Rowx
	Queryx(interface{}) (Rowsx, error)
	QueryxContext(context.Context, interface{}) (Rowsx, error)
	Select(interface{}, interface{}) error
	SelectContext(context.Context, interface{}, interface{}) error
	Unsafe() NamedStmt
}

type Querier added in v0.2.0

type Querier interface {
	Exec(string, ...interface{}) (sql.Result, error)
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	Prepare(string) (Stmt, error)
	PrepareContext(context.Context, string) (Stmt, error)
	Query(string, ...interface{}) (Rows, error)
	QueryContext(context.Context, string, ...interface{}) (Rows, error)
	QueryRow(string, ...interface{}) Row
	QueryRowContext(context.Context, string, ...interface{}) Row
}

type Querierx added in v0.2.0

type Querierx interface {
	Querier

	BindNamed(string, interface{}) (string, []interface{}, error)
	DriverName() string
	Get(interface{}, string, ...interface{}) error
	GetContext(context.Context, interface{}, string, ...interface{}) error
	MustExec(string, ...interface{}) sql.Result
	MustExecContext(context.Context, string, ...interface{}) sql.Result
	NamedExec(string, interface{}) (sql.Result, error)
	NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
	NamedQuery(string, interface{}) (Rowsx, error)
	PrepareNamed(string) (NamedStmt, error)
	PrepareNamedContext(context.Context, string) (NamedStmt, error)
	Preparex(string) (Stmtx, error)
	PreparexContext(context.Context, string) (Stmtx, error)
	QueryRowx(string, ...interface{}) Rowx
	QueryRowxContext(context.Context, string, ...interface{}) Rowx
	Queryx(string, ...interface{}) (Rowsx, error)
	QueryxContext(context.Context, string, ...interface{}) (Rowsx, error)
	Rebind(string) string
	Select(interface{}, string, ...interface{}) error
	SelectContext(context.Context, interface{}, string, ...interface{}) error
}

type Row

type Row interface {
	Err() error
	Scan(...interface{}) error
}

type Rows

type Rows interface {
	Close() error
	ColumnTypes() ([]ColumnType, error)
	Columns() ([]string, error)
	Err() error
	Next() bool
	NextResultSet() bool
	Scan(...interface{}) error
}

type Rowsx

type Rowsx interface {
	Rows
	MapScan(map[string]interface{}) error
	SliceScan() ([]interface{}, error)
	StructScan(interface{}) error
}

type Rowx

type Rowx interface {
	Row
	ColumnTypes() ([]ColumnType, error)
	Columns() ([]string, error)
	MapScan(map[string]interface{}) error
	SliceScan() ([]interface{}, error)
	StructScan(interface{}) error
}

type Stmt

type Stmt interface {
	Close() error
	Exec(...interface{}) (sql.Result, error)
	ExecContext(context.Context, ...interface{}) (sql.Result, error)
	Query(...interface{}) (Rows, error)
	QueryContext(context.Context, ...interface{}) (Rows, error)
	QueryRow(...interface{}) Row
	QueryRowContext(context.Context, ...interface{}) Row
}

type Stmtx

type Stmtx interface {
	Stmt
	Get(interface{}, ...interface{}) error
	GetContext(context.Context, interface{}, ...interface{}) error
	MustExec(...interface{}) sql.Result
	MustExecContext(context.Context, ...interface{}) sql.Result
	QueryRowx(...interface{}) Rowx
	QueryRowxContext(context.Context, ...interface{}) Rowx
	Queryx(...interface{}) (Rowsx, error)
	QueryxContext(context.Context, ...interface{}) (Rowsx, error)
	Select(interface{}, ...interface{}) error
	SelectContext(context.Context, interface{}, ...interface{}) error
	Unsafe() Stmtx
}

type Tx

type Tx interface {
	Querier

	Commit() error
	Rollback() error
	Stmt(*sql.Stmt) Stmt
	StmtContext(context.Context, *sql.Stmt) Stmt
}

type Txx

type Txx interface {
	Tx
	Querierx

	NamedStmt(*sqlx.NamedStmt) NamedStmt
	NamedStmtContext(context.Context, *sqlx.NamedStmt) NamedStmt
	Stmtx(interface{}) Stmtx
	StmtxContext(context.Context, interface{}) Stmtx
	Unsafe() Txx
}

Jump to

Keyboard shortcuts

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