sqlxplus

package module
v0.0.0-...-bcd33b6 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: MIT Imports: 8 Imported by: 1

README

sqlxplus

golang sqlx wrap logger

Usage

go get github.com/nilorg/sqlxplus

Import

import "github.com/nilorg/sqlxplus"

Example

import (
    "github.com/jmoiron/sqlx"
    "github.com/nilorg/sqlxplus"
)

func main() {
	driverName := "mysql"
	dataSourceName := "xxx"
	xdb, err := sqlx.Connect(driverName, dataSourceName)
	if err != nil {
		log.Fatalln(err)
	}
	xdbpuls := &sqlxplus.DB{SqlxTx: xdb, Log: &StdLogger{}}
    var result map[string]interface{}
    err = sqlx.GetContext(ctx, xdbpuls.DB(), &result, "select * from user where id = ?", 1)
    if err != nil {
		log.Fatalln(err)
	}
}



Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContextNotFoundSqlx 上下文不存在Sqlx错误
	ErrContextNotFoundSqlx = errors.New("上下文中没有获取到Sqlx")
)
View Source
var ErrExecTransactionOptionDBIsNil = errors.New("db in options is nil")

Functions

func CheckSqlxTranContextExist

func CheckSqlxTranContextExist(ctx context.Context) bool

CheckSqlxTranContextExist 检查sqlx是否存在

func Exec

func Exec(e sqlx.Execer, query string, args ...interface{}) (sql.Result, error)

Exec error handling of sqlx.MustExec.

func ExecContext

func ExecContext(ctx context.Context, e sqlx.ExecerContext, query string, args ...interface{}) (sql.Result, error)

ExecContext error handling of sqlx.MustExecContext.

func ExecTransaction

func ExecTransaction(ctx context.Context, f TransactionFunc, opts ...ExecTransactionOption) (err error)

func InterfaceToString

func InterfaceToString(src interface{}) string

func MapScan

func MapScan(r sqlx.ColScanner, dest map[string]interface{}) (err error)

MapScan 解决sqlx字节数组问题

func NewSqlxContext

func NewSqlxContext(ctx context.Context, xdb *DB) context.Context

NewSqlxContext 创建Sqlx上下文

func NewSqlxTranContext

func NewSqlxTranContext(ctx context.Context, xtx *Tx) context.Context

NewSqlxTranContext 创建sqlx事务上下文

func StringIndex

func StringIndex(str string, s rune) (indexs []int)

func StringIndexReplace

func StringIndexReplace(str string, indexs []int, args []interface{}) string

Types

type CreateSqlxTranContextFunc

type CreateSqlxTranContextFunc func(ctx context.Context, tx *sqlx.Tx) context.Context

type DB

type DB struct {
	SqlxDB sqlxer
	Log    Logger
}

func (*DB) Exec

func (that *DB) Exec(query string, args ...interface{}) (result sql.Result, err error)

func (*DB) ExecContext

func (that *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error)

func (*DB) GetSqlxDB

func (that *DB) GetSqlxDB() *sqlx.DB

func (*DB) Prepare

func (that *DB) Prepare(query string) (stmt *sql.Stmt, err error)

func (*DB) PrepareContext

func (that *DB) PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)

func (*DB) Query

func (that *DB) Query(query string, args ...interface{}) (rows *sql.Rows, err error)

func (*DB) QueryContext

func (that *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)

func (*DB) QueryRowx

func (that *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (*DB) QueryRowxContext

func (that *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (*DB) Queryx

func (that *DB) Queryx(query string, args ...interface{}) (rows *sqlx.Rows, err error)

func (*DB) QueryxContext

func (that *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (rows *sqlx.Rows, err error)

type DBConnect

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

func FromSqlxContext

func FromSqlxContext(ctx context.Context) (conn *DBConnect, err error)

FromSqlxContext 从上下文中获取Sqlx

func (*DBConnect) Adapter

func (db *DBConnect) Adapter() sqlxer

func (*DBConnect) DB

func (db *DBConnect) DB() *DB

func (*DBConnect) IsTransaction

func (db *DBConnect) IsTransaction() bool

func (*DBConnect) Transaction

func (db *DBConnect) Transaction() *Tx

type ExecTransactionOption

type ExecTransactionOption func(o *ExecTransactionOptions)

func WithExecTransactionCreateSqlxTranContextFunc

func WithExecTransactionCreateSqlxTranContextFunc(f CreateSqlxTranContextFunc) ExecTransactionOption

func WithExecTransactionDB

func WithExecTransactionDB(db *sqlx.DB) ExecTransactionOption

func WithExecTransactionLogger

func WithExecTransactionLogger(log Logger) ExecTransactionOption

type ExecTransactionOptions

type ExecTransactionOptions struct {
	DB                        *sqlx.DB
	Log                       Logger
	CreateSqlxTranContextFunc CreateSqlxTranContextFunc
}

type Logger

type Logger interface {
	// Printf 打印
	Printf(ctx context.Context, query string, args ...interface{})
	// Println 打印
	Println(ctx context.Context, args ...interface{})
	// Errorf 错误
	Errorf(ctx context.Context, format string, args ...interface{})
	// Errorln 错误
	Errorln(ctx context.Context, args ...interface{})
}

Logger logger

type SqlxKey

type SqlxKey struct{}

type SqlxTranKey

type SqlxTranKey struct{}

type StdLogger

type StdLogger struct {
}

StdLogger ...

func (StdLogger) Errorf

func (StdLogger) Errorf(ctx context.Context, format string, args ...interface{})

Errorf 错误

func (StdLogger) Errorln

func (StdLogger) Errorln(ctx context.Context, args ...interface{})

Errorln 错误

func (StdLogger) Printf

func (StdLogger) Printf(ctx context.Context, query string, args ...interface{})

Printf 打印

func (StdLogger) Println

func (StdLogger) Println(ctx context.Context, args ...interface{})

Println 打印

type TransactionFunc

type TransactionFunc func(context.Context) error

type Tx

type Tx struct {
	SqlxTx sqlxer
	Log    Logger
}

func (*Tx) Exec

func (that *Tx) Exec(query string, args ...interface{}) (result sql.Result, err error)

func (*Tx) ExecContext

func (that *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error)

func (*Tx) GetSqlxTx

func (that *Tx) GetSqlxTx() *sqlx.Tx

func (*Tx) Prepare

func (that *Tx) Prepare(query string) (stmt *sql.Stmt, err error)

func (*Tx) PrepareContext

func (that *Tx) PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)

func (*Tx) Query

func (that *Tx) Query(query string, args ...interface{}) (rows *sql.Rows, err error)

func (*Tx) QueryContext

func (that *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)

func (*Tx) QueryRowx

func (that *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (*Tx) QueryRowxContext

func (that *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (*Tx) Queryx

func (that *Tx) Queryx(query string, args ...interface{}) (rows *sqlx.Rows, err error)

func (*Tx) QueryxContext

func (that *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (rows *sqlx.Rows, err error)

Jump to

Keyboard shortcuts

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