dmysql

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 17 Imported by: 0

README

mysql

mysql low level api

Useage

step 1: // 新建一个mysql管理器 mgr, err := New(hosts, usr, passwd, db, charset, WithMaxConnSize(16), // 最大连接数 WithDebug(true), // 启用debug WithDialTimeout(time.Second*1), // 连接超时时间 WithReadTimeout(time.Second*2), // 读超时 WithWriteTimeout(time.Second*2), // 写超时 WithAutoCommit(true), // 启用自动提交 WithPoolSize(4)) // 连接池大小 step 2: conn,err := mgr.Get() // 获取一个连接 defer mgr.Put(conn) // 将连接归还到连接池 // conn.Xxxx 使用连接操作mysql

API

  • Select Select(table string, fields []string, condPattern string, condArgs ...interface{}) table:表名 fields:指定列名,[]string{"*"}代表所有 condPattern: where条件,使用占位符表达式 condArgs: condPattern中对应的占位符的值
    err = conn.Select(table, []string{"name", "age"}, "where name = ?", "update1")
    row, err := conn.FetchRowMap()
    
  • Insert Insert(table string,params map[string]interface{}) params: map[field] = value
    id, err = conn.Insert(table, map[string]interface{}{"name": "ja"})
    // id 为lastInsertID
    
  • Update Update(table string, updator map[string]interface{}, condPattern string,condArgs ...interface{}) (affect int64, err error) updator: 更新的字段和对应的新值 affect: 影响的行数
    _, err = conn.Update(table, map[string]interface{}{"name": "update1", "age": 222}, "where name = ?", "update")
    
  • Delete
  • Query
  • Execute
  • Upsert

FAQ

1、防注入支持吗 你别自己拼SQL条件就行,底层有防注入实现,条件建议使用占位符

Documentation

Overview

Package dmysql ...

Package dmysql ...

Package dmysql ...

Package dmysql ...

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyValues nil val
	ErrEmptyValues = errors.New("values is nil")
	// ErrEmptyTable nil table name
	ErrEmptyTable = errors.New("table name is nil")
	// ErrEmptyCondition cond is nil
	ErrEmptyCondition = errors.New("where condition is nil")
	// ErrMissMatchRow not found any row
	ErrMissMatchRow = errors.New("missed match")
	// ErrNilTransaction transaction is nil
	ErrNilTransaction = errors.New("transaction is nil")
	// ErrUnsupportedMode unsupported AcquireConnMode
	ErrUnsupportedMode = errors.New("unsupported AcquireConnMode")
	// ErrEmptyConnPool empty connection pool
	ErrEmptyConnPool = errors.New("empty connection pool")
	// ErrAcquiredConnTimeout acquire connection timed out
	ErrAcquiredConnTimeout = errors.New("acquire connection timed out")
	// ErrNotOpened close or nil db
	ErrNotOpened = errors.New("close or nil db")
)

Functions

func And

func And(conds []*Cond, orderBy, groupBy string) (sqlPattern string, args []interface{})

And ...

func DecodeRowMap

func DecodeRowMap(target interface{}, r RowMap) error

DecodeRowMap use reflect decode RowMap into struct

func DecodeRowMaps

func DecodeRowMaps(sts []interface{}, rs []RowMap) error

DecodeRowMaps ...

func Or

func Or(conds []*Cond, orderBy, groupBy string) (sqlPattern string, args []interface{})

Or ...

Types

type AcquireConnMode

type AcquireConnMode uint8

AcquireConnMode ...

const (
	// AcquireConnModeUnblock return conn if there is conn or return nil if there is no free
	AcquireConnModeUnblock AcquireConnMode = iota
	// AcquireConnModeTimeout wait for free conn util timeout
	AcquireConnModeTimeout
	// AcquireConnModeBlock wait for free conn forever
	AcquireConnModeBlock
)

type Cond

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

Cond ...

func Eq

func Eq(field string, val interface{}) *Cond

Eq ...

func Gt

func Gt(field string, val interface{}) *Cond

Gt ...

func Gte

func Gte(field string, val interface{}) *Cond

Gte ...

func In

func In(field string, val interface{}) *Cond

In ...

func Like

func Like(field string, val interface{}) *Cond

Like ...

func Lt

func Lt(field string, val interface{}) *Cond

Lt ...

func Lte

func Lte(field string, val interface{}) *Cond

Lte ...

func NotEq

func NotEq(field string, val interface{}) *Cond

NotEq ...

func NotIn

func NotIn(field string, val interface{}) *Cond

NotIn ...

func NotLike

func NotLike(field string, val interface{}) *Cond

NotLike ...

type Manager

type Manager struct {
	Connected int
	// contains filtered or unexported fields
}

Manager Mysql conn manager

func New

func New(hosts []string, usr, passwd, db, charset string, opts ...Option) (*Manager, error)

New return manager with some params and options

func (*Manager) Get

func (mgr *Manager) Get() (*MySQL, error)

Get return conn for pool

func (*Manager) Put

func (mgr *Manager) Put(db *MySQL)

Put put back conn into pool. Close it if pool is full

type MySQL

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

MySQL conn obj

func (*MySQL) AffectRows

func (m *MySQL) AffectRows(ctx context.Context) int64

AffectRows ...

func (*MySQL) Begin

func (m *MySQL) Begin(ctx context.Context) (err error)

Begin tr begin

func (*MySQL) Close

func (m *MySQL) Close() error

Close ...

func (*MySQL) Commit

func (m *MySQL) Commit(ctx context.Context) (err error)

Commit tx commit

func (*MySQL) Delete

func (m *MySQL) Delete(ctx context.Context, table string, condPattern string, condArgs ...interface{}) (affect int64, err error)

Delete ...

func (*MySQL) Execute

func (m *MySQL) Execute(ctx context.Context, sqlPattern string, args ...interface{}) (err error)

Execute low level api to exec sql

func (*MySQL) FetchAll

func (m *MySQL) FetchAll(ctx context.Context) (all []Row, err error)

FetchAll ...

func (*MySQL) FetchAllMap

func (m *MySQL) FetchAllMap(ctx context.Context) (allMap []RowMap, err error)

FetchAllMap ...

func (*MySQL) FetchOne

func (m *MySQL) FetchOne(ctx context.Context) (firstCol string, err error)

FetchOne ...

func (*MySQL) FetchOneRow

func (m *MySQL) FetchOneRow(ctx context.Context) (row Row, err error)

FetchOneRow ...

func (*MySQL) FetchRow

func (m *MySQL) FetchRow(ctx context.Context) (rows Row, err error)

FetchRow ...

func (*MySQL) FetchRowMap

func (m *MySQL) FetchRowMap(ctx context.Context) (rowMap RowMap, err error)

FetchRowMap ...

func (*MySQL) FetchRowMapInterface

func (m *MySQL) FetchRowMapInterface(ctx context.Context) (rowMapIntf map[string]interface{}, err error)

FetchRowMapInterface ...

func (*MySQL) Info

func (m *MySQL) Info() string

func (*MySQL) Insert

func (m *MySQL) Insert(ctx context.Context, table string, kvPairs map[string]interface{}) (lastID int64, err error)

Insert do

func (*MySQL) LastInsertID

func (m *MySQL) LastInsertID(ctx context.Context) (id int64)

LastInsertID ...

func (*MySQL) MultiInsert

func (m *MySQL) MultiInsert(ctx context.Context, table string, batchData []map[string]interface{}) (lastID int64, err error)

MultiInsert ...

func (*MySQL) Ping

func (m *MySQL) Ping() error

Ping ...

func (*MySQL) Query

func (m *MySQL) Query(ctx context.Context, sqlPattern string, args ...interface{}) (err error)

Query do query

func (*MySQL) RollBack

func (m *MySQL) RollBack(ctx context.Context) (err error)

RollBack tx rollback

func (*MySQL) Select

func (m *MySQL) Select(ctx context.Context, table string, fields []string, condPattern string, condArgs ...interface{}) error

Select ...

func (*MySQL) Update

func (m *MySQL) Update(ctx context.Context, table string, updator map[string]interface{}, condPattern string,
	condArgs ...interface{}) (affect int64, err error)

Update ...

func (*MySQL) Upsert

func (m *MySQL) Upsert(ctx context.Context, table string, data map[string]interface{}, updateKeys []string) (lastID int64, err error)

Upsert ...

type Option

type Option func(o *option)

Option option func

func WithAcquireConnMode

func WithAcquireConnMode(mode AcquireConnMode) Option

WithAcquireConnMode ...

func WithAutoCommit

func WithAutoCommit(auto bool) Option

WithAutoCommit set auto_commit

func WithColumnsWithAlias

func WithColumnsWithAlias(colWithAlias bool) Option

WithColumnsWithAlias set columns

func WithDebug

func WithDebug(debug bool) Option

WithDebug set debug model

func WithDialTimeout

func WithDialTimeout(t time.Duration) Option

WithDialTimeout set conn timeout

func WithDisfServiceName

func WithDisfServiceName(sn string, disfEnable bool) Option

WithDisfServiceName set disf service name and enable disf

func WithKeepSilent

func WithKeepSilent(silent bool) Option

WithKeepSilent ignore unusable host when init conn_pool

func WithLoc

func WithLoc(loc string) Option

WithLoc set loc option

func WithLogger

func WithLogger(log logger.Logger) Option

WithLogger set logger

func WithMaxConnSize

func WithMaxConnSize(size int) Option

WithMaxConnSize set max pool size

func WithParseTime

func WithParseTime(parseTime bool) Option

WithParseTime set parse time

func WithPoolSize

func WithPoolSize(size int) Option

WithPoolSize set pool size

func WithReadTimeout

func WithReadTimeout(t time.Duration) Option

WithReadTimeout set read timeout

func WithWriteTimeout

func WithWriteTimeout(t time.Duration) Option

WithWriteTimeout set write timeout

type Row

type Row []string

Row just val

type RowMap

type RowMap map[string]string

RowMap row result

Jump to

Keyboard shortcuts

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