sqlx

package
v1.5.6 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: MIT Imports: 22 Imported by: 397

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotMatchDestination is an error that indicates not matching destination to scan.
	ErrNotMatchDestination = errors.New("not matching destination to scan")
	// ErrNotReadableValue is an error that indicates value is not addressable or interfaceable.
	ErrNotReadableValue = errors.New("value not addressable or interfaceable")
	// ErrNotSettable is an error that indicates the passed in variable is not settable.
	ErrNotSettable = errors.New("passed in variable is not settable")
	// ErrUnsupportedValueType is an error that indicates unsupported unmarshal type.
	ErrUnsupportedValueType = errors.New("unsupported unmarshal type")
)
View Source
var (
	// ErrNotFound is an alias of sql.ErrNoRows
	ErrNotFound = sql.ErrNoRows
)

Functions

func DisableLog added in v1.4.1

func DisableLog()

DisableLog disables logging of sql statements, includes info and slow logs.

func DisableStmtLog added in v1.4.1

func DisableStmtLog()

DisableStmtLog disables info logging of sql statements, but keeps slow logs.

func SetSlowThreshold

func SetSlowThreshold(threshold time.Duration)

SetSlowThreshold sets the slow threshold.

Types

type BulkInserter

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

A BulkInserter is used to batch insert records. Postgresql is not supported yet, because of the sql is formated with symbol `$`. Oracle is not supported yet, because of the sql is formated with symbol `:`.

func NewBulkInserter

func NewBulkInserter(sqlConn SqlConn, stmt string) (*BulkInserter, error)

NewBulkInserter returns a BulkInserter.

func (*BulkInserter) Flush

func (bi *BulkInserter) Flush()

Flush flushes all the pending records.

func (*BulkInserter) Insert

func (bi *BulkInserter) Insert(args ...any) error

Insert inserts given args.

func (*BulkInserter) SetResultHandler

func (bi *BulkInserter) SetResultHandler(handler ResultHandler)

SetResultHandler sets the given handler.

func (*BulkInserter) UpdateOrDelete

func (bi *BulkInserter) UpdateOrDelete(fn func())

UpdateOrDelete runs update or delete queries, which flushes pending records first.

func (*BulkInserter) UpdateStmt

func (bi *BulkInserter) UpdateStmt(stmt string) error

UpdateStmt updates the insert statement.

type ResultHandler

type ResultHandler func(sql.Result, error)

ResultHandler defines the method of result handlers.

type Session

type Session interface {
	Exec(query string, args ...any) (sql.Result, error)
	ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error)
	Prepare(query string) (StmtSession, error)
	PrepareCtx(ctx context.Context, query string) (StmtSession, error)
	QueryRow(v any, query string, args ...any) error
	QueryRowCtx(ctx context.Context, v any, query string, args ...any) error
	QueryRowPartial(v any, query string, args ...any) error
	QueryRowPartialCtx(ctx context.Context, v any, query string, args ...any) error
	QueryRows(v any, query string, args ...any) error
	QueryRowsCtx(ctx context.Context, v any, query string, args ...any) error
	QueryRowsPartial(v any, query string, args ...any) error
	QueryRowsPartialCtx(ctx context.Context, v any, query string, args ...any) error
}

Session stands for raw connections or transaction sessions

func NewSessionFromTx

func NewSessionFromTx(tx *sql.Tx) Session

NewSessionFromTx returns a Session with the given sql.Tx. Use it with caution, it's provided for other ORM to interact with.

type SqlConn

type SqlConn interface {
	Session
	// RawDB is for other ORM to operate with, use it with caution.
	// Notice: don't close it.
	RawDB() (*sql.DB, error)
	Transact(fn func(Session) error) error
	TransactCtx(ctx context.Context, fn func(context.Context, Session) error) error
}

SqlConn only stands for raw connections, so Transact method can be called.

func NewMysql

func NewMysql(datasource string, opts ...SqlOption) SqlConn

NewMysql returns a mysql connection.

func NewSqlConn

func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn

NewSqlConn returns a SqlConn with given driver name and datasource.

func NewSqlConnFromDB

func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn

NewSqlConnFromDB returns a SqlConn with the given sql.DB. Use it with caution, it's provided for other ORM to interact with.

func NewSqlConnFromSession added in v1.5.3

func NewSqlConnFromSession(session Session) SqlConn

NewSqlConnFromSession returns a SqlConn with the given session.

type SqlOption

type SqlOption func(*commonSqlConn)

SqlOption defines the method to customize a sql connection.

func WithAcceptable added in v1.5.4

func WithAcceptable(acceptable func(err error) bool) SqlOption

WithAcceptable returns a SqlOption that setting the acceptable function. acceptable is the func to check if the error can be accepted.

type StmtSession

type StmtSession interface {
	Close() error
	Exec(args ...any) (sql.Result, error)
	ExecCtx(ctx context.Context, args ...any) (sql.Result, error)
	QueryRow(v any, args ...any) error
	QueryRowCtx(ctx context.Context, v any, args ...any) error
	QueryRowPartial(v any, args ...any) error
	QueryRowPartialCtx(ctx context.Context, v any, args ...any) error
	QueryRows(v any, args ...any) error
	QueryRowsCtx(ctx context.Context, v any, args ...any) error
	QueryRowsPartial(v any, args ...any) error
	QueryRowsPartialCtx(ctx context.Context, v any, args ...any) error
}

StmtSession interface represents a session that can be used to execute statements.

Jump to

Keyboard shortcuts

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