db

package
v0.0.0-...-34dd132 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrOK ok
	ErrOK = 0
	// ErrException exception 异常
	ErrException = 1
	// ErrExists exists 数据存在
	ErrExists = 2
	// ErrNotFound not found 未找到关键数据
	ErrNotFound = 3
	// ErrAuthorized authorized 未认证
	ErrAuthorized = 4
	// ErrNotConnect connect error 数据库内部连接错误
	ErrNotConnect = 5
	// data not found 数据未找到
	ErrDataNotFound = 6
	// expired 已过期
	ErrExpired = 7
	// insufficient balance 余额不足
	ErrInsufficientBalance = 8
	// Credit Limit 超额
	ErrCreditLimit = 9
)

DB error code

Variables

View Source
var (
	ErrNoneConnect = errors.New(`not connect to database`)
)

Functions

func Exec

func Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error)

Exec exec

func ExecContext

func ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error)

Exec exec

func Get

func Get(dest interface{}, query string, args ...interface{}) (err error)

Get get

func Int64ArrayToIn

func Int64ArrayToIn(arrs []int64) string

Int64ArrayToIn array to db IN string

func IsolationDefault

func IsolationDefault(readOnly bool) *sql.TxOptions

IsolationDefault

func IsolationLinearizable

func IsolationLinearizable(readOnly bool) *sql.TxOptions

IsolationLinearizable

func IsolationReadCommitted

func IsolationReadCommitted(readOnly bool) *sql.TxOptions

IsolationReadCommitted

func IsolationReadUncommitted

func IsolationReadUncommitted(readOnly bool) *sql.TxOptions

IsolationReadUncommitted

func IsolationRepeatableRead

func IsolationRepeatableRead(readOnly bool) *sql.TxOptions

IsolationRepeatableRead

func IsolationSerializable

func IsolationSerializable(readOnly bool) *sql.TxOptions

IsolationSerializable

func IsolationSnapshot

func IsolationSnapshot(readOnly bool) *sql.TxOptions

IsolationSnapshot

func IsolationWriteCommitted

func IsolationWriteCommitted(readOnly bool) *sql.TxOptions

IsolationWriteCommitted

func NamedExec

func NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

NamedExec exec with named args

func NamedExecContext

func NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

NamedExecContext exec with named args

func NewNamedStmt

func NewNamedStmt() *sqlx.NamedStmt

NewNamedStmt alloc *sqlx.NamedStmt from pool

func Ping

func Ping() (err error)

Ping ping connect

func PingContext

func PingContext(ctx context.Context) (err error)

PingContext ping connect

func PutNamedStmt

func PutNamedStmt(stmt *sqlx.NamedStmt)

PutNamedStmt release *sqlx.NamedStmt to pool

func Query

func Query(dest interface{}, query string, args interface{}) (err error)

Query get rows with named args

func QueryContext

func QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

QueryContext get rows with named args

func QueryRow

func QueryRow(dest interface{}, query string, args interface{}) (err error)

QueryRow get row with named args

func QueryRowContext

func QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

QueryRowContext get row with named args

func Release

func Release()

Release free db connect

func ReleaseConfig

func ReleaseConfig(dbx *DB)

ReleaseConfig free db connect

func ReplyToReplyData

func ReplyToReplyData(reply Reply) *myth.ReplyData

ReplyToReplyData db reply to response

func Row

func Row(dest interface{}, query string, args interface{}) (err error)

Row get row with named args

func RowContext

func RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

RowContext get row with named args

func Rows

func Rows(dest interface{}, query string, args interface{}) (err error)

Rows get rows with named args

func RowsContext

func RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

RowsContext get rows with named args

func Select

func Select(dest interface{}, query string, args ...interface{}) (err error)

Select select

func SetConfig

func SetConfig(cfg Config)

SetConfig set

func Stats

func Stats() (s sql.DBStats, err error)

Stats returns database statistics.

Types

type Config

type Config struct {
	Driver       string
	DNS          string
	MaxOpenConns int
	MaxIdle      int
	MaxIdleTime  time.Duration
	MaxLifeTime  time.Duration
}

Config config

type DB

type DB struct {
	Driver string
	// contains filtered or unexported fields
}

DB define

func New

func New() *DB

New new DB object

func NewConfig

func NewConfig(config Config) (dbx *DB, err error)

NewConfig new DB dynamic object

func (*DB) Begin

func (d *DB) Begin() (tx *Tx, err error)

Begin starts a transaction. The default isolation level is dependent on the driver.

func (*DB) BeginTrans

func (d *DB) BeginTrans() (err error)

BeginTrans begin trans

func (*DB) BeginTransx

func (d *DB) BeginTransx(ctx context.Context, opts *sql.TxOptions) (err error)

BeginTransx begin trans

func (*DB) BeginTx

func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *Tx, err error)

BeginTx starts a transaction.

func (*DB) Close

func (d *DB) Close()

Close close database connect

func (*DB) Commit

func (d *DB) Commit() (err error)

Commit commit

func (*DB) Connect

func (d *DB) Connect() (err error)

Connect connect to database

func (*DB) ConnectContext

func (d *DB) ConnectContext(ctx context.Context) (err error)

ConnectContext connect to database

func (*DB) Exec

func (d *DB) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error)

Exec exec

func (*DB) ExecContext

func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error)

ExecContext exec

func (*DB) Get

func (d *DB) Get(dest interface{}, query string, args ...interface{}) (err error)

Get get row, QueryRow executes a query that is expected to return at most one row.

func (*DB) GetContext

func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

GetContext get

func (*DB) In

func (d *DB) In(query string, args ...interface{}) (q string, params []interface{}, err error)

* In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.

func (*DB) Insert

func (d *DB) Insert(query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

Insert insert into with named args

func (*DB) InsertContext

func (d *DB) InsertContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

InsertContext insert into with named args

func (*DB) InsertReply

func (d *DB) InsertReply(query string, args interface{}) (reply Reply)

InsertReply insert and return DbReply

func (*DB) Limit

func (d *DB) Limit(page, pagesize int) string

Limit MySQL limit

func (*DB) NamedExec

func (d *DB) NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

Exec exec, with named args

func (*DB) NamedExecContext

func (d *DB) NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

NamedExecContext exec, with named args

func (*DB) Ping

func (d *DB) Ping() (err error)

Ping Ping connect

func (*DB) PingContext

func (d *DB) PingContext(ctx context.Context) (err error)

PingContext Ping connect

func (*DB) PrepareNamed

func (d *DB) PrepareNamed(query string) (stmt *NamedStmt, err error)

PrepareNamed returns an sqlx.NamedStmt

func (*DB) PrepareNamedContext

func (d *DB) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error)

PrepareNamedContext returns an sqlx.NamedStmt

func (*DB) Preparex

func (d *DB) Preparex(query string) (stmt *Stmt, err error)

Preparex a statement within a transaction.

func (*DB) PreparexContext

func (d *DB) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error)

PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.

func (*DB) Query

func (d *DB) Query(dest interface{}, query string, args interface{}) (err error)

Query get rows with named args, Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryContext

func (d *DB) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

QueryContext get rows with named args, QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryRow

func (d *DB) QueryRow(dest interface{}, query string, args ...interface{}) (err error)

QueryRow get row, QueryRow executes a query that is expected to return at most one row.

func (*DB) QueryRowContext

func (d *DB) QueryRowContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

QueryRowContext get row, QueryRowContext executes a query that is expected to return at most one row.

func (*DB) Rollback

func (d *DB) Rollback() (err error)

Rollback rollback

func (*DB) Row

func (d *DB) Row(dest interface{}, query string, args interface{}) (err error)

Row get row with named args

func (*DB) RowContext

func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

RowContext get row with named args

func (*DB) Rows

func (d *DB) Rows(dest interface{}, query string, args interface{}) (err error)

Rows get rows with named args

func (*DB) RowsContext

func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

RowsContext get rows with named args

func (*DB) Select

func (d *DB) Select(dest interface{}, query string, args ...interface{}) (err error)

Select select

func (*DB) SelectContext

func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error)

SelectContext select

func (*DB) Stats

func (d *DB) Stats() (s sql.DBStats)

Stats Stats returns database statistics.

func (*DB) TransExec

func (d *DB) TransExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

TransExec trans execute with named args

func (*DB) TransExecContext

func (d *DB) TransExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

TransExec trans execute with named args

func (*DB) TransRow

func (d *DB) TransRow(dest interface{}, query string, args interface{}) (err error)

TransRow trans get row

func (*DB) TransUpdate

func (d *DB) TransUpdate(query string, args interface{}) (reply Reply)

TransUpdate trans update

func (*DB) Update

func (d *DB) Update(query string, args interface{}) (rowsAffected int64, err error)

Update update/delete with named args

func (*DB) UpdateContext

func (d *DB) UpdateContext(ctx context.Context, query string, args interface{}) (rowsAffected int64, err error)

Update update/delete with named args

func (*DB) UpdateReply

func (d *DB) UpdateReply(query string, args interface{}) (reply Reply)

UpdateReply update/delete and return DbReply

type M

type M map[string]any

M is a shortcut for map[string]any

func NewM

func NewM() M

NewM new map[string]any

func (M) Bytes

func (m M) Bytes() []byte

Bytes map to json bytes

func (M) Get

func (m M) Get(k string) (v any, ok bool)

Get value from map[string]interface{}

func (M) MarshalXML

func (m M) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML allows type Map to be used with xml.Marshal.

func (M) Set

func (m M) Set(k string, v any)

Set key-value to map[string]any

func (M) String

func (m M) String() string

String map to json string

type NamedStmt

type NamedStmt = sqlx.NamedStmt

type Reply

type Reply struct {
	OK           bool
	Err          error
	LastErr      error
	ErrCode      int
	LastID       int64
	RowsAffected int64
}

Reply db exec return insert/update/delete

func ReplyFaild

func ReplyFaild(errCode int, err, errText error) (reply Reply)

ReplyFaild exec faild

func ReplyOk

func ReplyOk(rowsAffected, lastID int64) Reply

ReplyOk exec ok

func (Reply) Ok

func (r Reply) Ok() bool

Ok check reply true/false

type Stmt

type Stmt = sqlx.Stmt

type Tx

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

Tx is an in-progress database transaction.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Exec

func (t *Tx) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error)

Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*Tx) ExecContext

func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error)

ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*Tx) NamedExec

func (t *Tx) NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

NamedExec executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args

func (*Tx) NamedExecContext

func (t *Tx) NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error)

NamedExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args

func (*Tx) PrepareNamed

func (t *Tx) PrepareNamed(query string) (stmt *NamedStmt, err error)

PrepareNamed returns an sqlx.NamedStmt

func (*Tx) PrepareNamedContext

func (t *Tx) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error)

PrepareNamedContext returns an sqlx.NamedStmt

func (*Tx) Preparex

func (t *Tx) Preparex(query string) (stmt *Stmt, err error)

Preparex a statement within a transaction.

func (*Tx) PreparexContext

func (t *Tx) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error)

PreparexContext a statement within a transaction.

func (*Tx) Query

func (t *Tx) Query(dest interface{}, query string, args interface{}) (err error)

Query executes a query that returns rows, typically a SELECT. with named args

func (*Tx) QueryContext

func (t *Tx) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

QueryContext executes a query that returns rows, typically a SELECT. with named args

func (*Tx) QueryRow

func (t *Tx) QueryRow(dest interface{}, query string, args interface{}) (err error)

QueryRow executes a query that returns rows, typically a SELECT. with named args

func (*Tx) QueryRowContext

func (t *Tx) QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error)

QueryRowContext get row with named args

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback aborts the transaction.

Jump to

Keyboard shortcuts

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