mysql

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package mysql @Title client.go @Description @Author haogooder @Update 2022/2/8

Package mysql @Title conf.go @Description @Author haogooder @Update 2022/2/8

Package mysql @Title elem.go @Description @Author haogooder @Update 2022/2/8

Package mysql @Title table.go @Description @Author haogooder @Update 2022/2/8

Index

Constants

This section is empty.

Variables

View Source
var (
	// 一些错误信息
	ErrorOverflowMaxInt64  = errors.New("this value overflow math.MaxInt64")
	ErrorOverflowMaxUint64 = errors.New("this value overflow math.MaxUint64")
	ErrorLessThanMinInt64  = errors.New("this value less than math.MinInt64")
	ErrorLessThanZero      = errors.New("this value less than zero")
	ErrorBadComparisonType = errors.New("invalid type for comparison")
	ErrorBadComparison     = errors.New("incompatible types for comparison")
	ErrorNoComparison      = errors.New("missing argument for comparison")
	ErrorInvalidInputType  = errors.New("invalid input type")
)

Functions

func ByteToFloat64

func ByteToFloat64(bytes []byte) float64

bytes转float

func ByteToStr

func ByteToStr(b []byte) string

字节切片转为字符串

func BytesToInt64

func BytesToInt64(buf []byte) int64

bytes转int64

func ConvertArgs

func ConvertArgs(param []ElemType) []interface{}

转换查询SQL用的参数

func Float64ToByte

func Float64ToByte(float float64) []byte

float64转byte

func FormatCond

func FormatCond(cond map[string]interface{}, delim string) (sqlCond string, param []interface{})

*

  • 仅用在预编译的查询语句中
  • 格式化要查询的条件语句,只支持简单语句

func FormatFieldNameToGolangType

func FormatFieldNameToGolangType(fieleName string) string

格式字段的名称

func GetAllMySQLTables

func GetAllMySQLTables(db *DBase) (ret []string, err error)

提取所有的表

func GetMySQLAllTablesStruct

func GetMySQLAllTablesStruct(db *DBase) (ret string, err error)

生成mysql里所有表的go结构体形式

func GetMySQLTableStruct

func GetMySQLTableStruct(db *DBase, tableName string) (ret string, err error)

* CREATE TABLE `admin_menu` (

`menu_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
`menu_name` varchar(100) NOT NULL DEFAULT '' COMMENT '菜单名称',
`menu_path` varchar(100) NOT NULL DEFAULT '' COMMENT '菜单路径',
`icon_name` varchar(100) NOT NULL DEFAULT '' COMMENT '图标名称',
`icon_color` varchar(100) NOT NULL DEFAULT '' COMMENT '图标的颜色',
`parent_menu_id` int(11) NOT NULL DEFAULT '0' COMMENT '父菜单ID',
`child_menu_num` int(11) NOT NULL DEFAULT '0' COMMENT '子菜单数量',
`menu_sort` int(11) NOT NULL DEFAULT '0' COMMENT '同级菜单的排序值',
PRIMARY KEY (`menu_id`)

) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COMMENT='菜单表'

获取表的结构

func Int64ToBytes

func Int64ToBytes(i int64) []byte

int64转byte

func StrToByte

func StrToByte(s string) []byte

字符串转为字节切片

func TryBestToBool

func TryBestToBool(value interface{}) (bool, error)

尽最大努力转为bool类型

func TryBestToFloat

func TryBestToFloat(value interface{}) (float64, error)

尽最大努力转换为float64

func TryBestToInt64

func TryBestToInt64(value interface{}) (int64, error)

尽最大努力将给定的类型转换为int64 如"45.67"->45、"98.4abc3"->98、"34.87"->34

func TryBestToString

func TryBestToString(value interface{}) (string, error)

尽最大努力转换为字符串

func TryBestToUint64

func TryBestToUint64(value interface{}) (uint64, error)

尽最大努力将给定的类型转换为uint64 如"45.67"->45、"98.4abc3"->98、"34.87"->34

Types

type Basickind

type Basickind int
const (
	// 转换时最大值
	MaxInt64Float  = float64(math.MaxInt64)
	MinInt64Float  = float64(math.MinInt64)
	MaxUint64Float = float64(math.MaxUint64)
	// 基本类型归纳,类型转换时用得着
	InvalidKind Basickind = iota
	BoolKind
	ComplexKind
	IntKind
	FloatKind
	StringKind
	UintKind
	PtrKind
	ContainerKind
	FuncKind
)

func GetBasicKind

func GetBasicKind(v reflect.Value) (Basickind, error)

转换成特定类型,便于判断

type DBase

type DBase struct {
	Db      *sql.DB
	IsDebug bool
	Conf    MySQLConf
}

MySQL连接的类

func NewDBase

func NewDBase(conf MySQLConf) *DBase

func (*DBase) BeginTransaction

func (my *DBase) BeginTransaction() (*sql.Tx, error)

开启事务

func (*DBase) Close

func (my *DBase) Close() error

关闭连接

func (*DBase) CommitTransaction

func (my *DBase) CommitTransaction(tx *sql.Tx) error

提交事务

func (*DBase) Conn

func (my *DBase) Conn() (*DBase, error)

连接MySQL

func (*DBase) DeleteData

func (my *DBase) DeleteData(table string, cond map[string]interface{}) (int64, bool, error)

删除一条数据,返回lastAffectedRows

func (*DBase) Execute

func (my *DBase) Execute(sql string, args ...interface{}) (int64, bool, error)

执行一条insert/update/delete语句,返回影响行数

func (*DBase) FetchCols

func (my *DBase) FetchCols(sql string, args ...interface{}) ([]ElemType, error)

提取所有行的第一个字段的列表

func (*DBase) FetchCondRows

func (my *DBase) FetchCondRows(table string, cond map[string]interface{}, fields ...string) (ret []map[string]ElemType, err error)

提取多行数据 table为表名 cond为查询条件,全为and fields为要查询的字段,为空时表示查询全部

func (*DBase) FetchForUpdate

func (my *DBase) FetchForUpdate(table string, cond map[string]interface{}) (map[string]ElemType, error)

执行一条select ... for update语句

func (*DBase) FetchOne

func (my *DBase) FetchOne(sql string, args ...interface{}) (ret ElemType, err error)

提取单行的单个字段

func (*DBase) FetchRow

func (my *DBase) FetchRow(sql string, args ...interface{}) (map[string]ElemType, error)

提取一行数据

func (*DBase) FetchRows

func (my *DBase) FetchRows(sql string, args ...interface{}) ([]map[string]ElemType, error)

提取多行数据

func (*DBase) GetDB

func (my *DBase) GetDB() *sql.DB

提取原始的DB对象

func (*DBase) InsertBatchData

func (my *DBase) InsertBatchData(table string, fields []string, data [][]interface{}, isIgnore bool) (int64, bool, error)

批量写入数据,返回影响行数

func (*DBase) InsertData

func (my *DBase) InsertData(table string, data map[string]interface{}, isIgnore bool) (int64, bool, error)

写入一条数据,返回lastInsertId

func (*DBase) InsertUpdateData

func (my *DBase) InsertUpdateData(table string, insert map[string]interface{}, update map[string]interface{}) (int64, bool, error)

执行一条:INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1 语句

func (*DBase) OpenConnNum

func (my *DBase) OpenConnNum() int

目前的打开连接数

func (*DBase) Ping

func (my *DBase) Ping() error

Ping

func (*DBase) RollBackTransaction

func (my *DBase) RollBackTransaction(tx *sql.Tx) error

回滚事务

func (*DBase) SetDebug

func (my *DBase) SetDebug(d bool)

提取原始的DB对象

func (*DBase) UpdateData

func (my *DBase) UpdateData(table string, data map[string]interface{}, cond map[string]interface{}) (int64, bool, error)

更新一条数据,返回lastAffectedRows

type ElemType

type ElemType struct {
	Data   interface{}   // 数据元素
	RefVal reflect.Value // 通过反射获取的value
}

基本的元素类型

func MakeElemType

func MakeElemType(d interface{}) ElemType

任意类型的数据转为结构体

func (ElemType) IsArray

func (et ElemType) IsArray() bool

判断原始数据的类型是否为array

func (ElemType) IsBool

func (et ElemType) IsBool() bool

判断原始数据的类型是否为bool

func (ElemType) IsByteSlice

func (et ElemType) IsByteSlice() bool

是否为字符切片

func (ElemType) IsChan

func (et ElemType) IsChan() bool

判断原始数据的类型是否为chan

func (ElemType) IsComplexType

func (et ElemType) IsComplexType() bool

是否为复合类型:slice/array/map/chan

func (ElemType) IsFloat32

func (et ElemType) IsFloat32() bool

判断原始数据的类型是否为float32

func (ElemType) IsFloat64

func (et ElemType) IsFloat64() bool

判断原始数据的类型是否为float64

func (ElemType) IsInt

func (et ElemType) IsInt() bool

判断原始数据的类型是否为int

func (ElemType) IsInt16

func (et ElemType) IsInt16() bool

判断原始数据的类型是否为int16

func (ElemType) IsInt32

func (et ElemType) IsInt32() bool

判断原始数据的类型是否为int32

func (ElemType) IsInt64

func (et ElemType) IsInt64() bool

判断原始数据的类型是否为int64

func (ElemType) IsInt8

func (et ElemType) IsInt8() bool

判断原始数据的类型是否为int8

func (ElemType) IsMap

func (et ElemType) IsMap() bool

判断原始数据的类型是否为map

func (ElemType) IsSimpleType

func (et ElemType) IsSimpleType() bool

是否为简单类型:int/uint/string/bool/float....

func (ElemType) IsSlice

func (et ElemType) IsSlice() bool

判断原始数据的类型是否为slice

func (ElemType) IsString

func (et ElemType) IsString() bool

判断原始数据的类型是否为string

func (ElemType) IsUint

func (et ElemType) IsUint() bool

判断原始数据的类型是否为uint

func (ElemType) IsUint16

func (et ElemType) IsUint16() bool

判断原始数据的类型是否为uint16

func (ElemType) IsUint32

func (et ElemType) IsUint32() bool

判断原始数据的类型是否为uint32

func (ElemType) IsUint64

func (et ElemType) IsUint64() bool

判断原始数据的类型是否为uint64

func (ElemType) IsUint8

func (et ElemType) IsUint8() bool

判断原始数据的类型是否为uint8

func (ElemType) Kind

func (et ElemType) Kind() reflect.Kind

原始数据的类型

func (ElemType) Len

func (et ElemType) Len() (int, error)

提取原始数据的长度,只有string/slice/map/array/chan

func (ElemType) RawData

func (et ElemType) RawData() interface{}

获取原始数据

func (ElemType) ToBool

func (et ElemType) ToBool() (bool, error)

转换为bool类型,如果是bool型则直接返回

func (ElemType) ToFloat32

func (et ElemType) ToFloat32() (float32, error)

转换为float类型

func (ElemType) ToFloat64

func (et ElemType) ToFloat64() (float64, error)

转换为float64类型

func (ElemType) ToInt

func (et ElemType) ToInt() (int, error)

转换为int类型

func (ElemType) ToInt16

func (et ElemType) ToInt16() (int16, error)

转换为int16类型

func (ElemType) ToInt32

func (et ElemType) ToInt32() (int32, error)

转换为int32类型

func (ElemType) ToInt64

func (et ElemType) ToInt64() (int64, error)

转换为int64类型

func (ElemType) ToInt8

func (et ElemType) ToInt8() (int8, error)

转换为int8类型

func (ElemType) ToMap

func (et ElemType) ToMap() (map[ElemType]ElemType, error)

转换为map类型 如果原始数据是map则直接返回 如果是json字符串则尝试去解析 否则,报错

func (ElemType) ToSlice

func (et ElemType) ToSlice() ([]ElemType, error)

转换为slice类型 原始数据若为array/slice,则直接返回 原始数据为map时只返回[]value 原始数据若为数字、字符串等简单类型则将其放到slice中返回,即强制转为slice 否则,报错

func (ElemType) ToString

func (et ElemType) ToString() string

转换为string类型

func (ElemType) ToUint

func (et ElemType) ToUint() (uint, error)

转换为uint类型

func (ElemType) ToUint16

func (et ElemType) ToUint16() (uint16, error)

转换为uint16类型

func (ElemType) ToUint32

func (et ElemType) ToUint32() (uint32, error)

转换为uint32类型

func (ElemType) ToUint64

func (et ElemType) ToUint64() (uint64, error)

转换为uint64类型

func (ElemType) ToUint8

func (et ElemType) ToUint8() (uint8, error)

转换为uint8类型

type MySQLConf

type MySQLConf struct {
	Host            string        // 连接地址
	Port            uint16        // 端口号,默认3306
	User            string        // 用户名
	Passwd          string        // 密码
	DbName          string        // DB名称
	Charset         string        // 设置的字符编码,默认utf8
	Timeout         time.Duration // 连接超时时间,单位秒,默认5秒
	AutoCommit      bool          // 是否自动提交,默认为true
	MaxIdleConns    int           // 允许最大空闲连接数,默认为2
	MaxOpenConns    int           // 最多允许打开多少个连接,默认0不限制
	ConnMaxLiftTime time.Duration // 连接的最大生存时间,默认0不限制,单位秒
}

存储MySQL的连接账号信息

func MakeMySQLConf

func MakeMySQLConf() MySQLConf

func (MySQLConf) SetAutoCommit

func (mc MySQLConf) SetAutoCommit(b bool) MySQLConf

设置是否自动提交

func (MySQLConf) SetCharset

func (mc MySQLConf) SetCharset(c string) MySQLConf

设置连接时的字符编码

func (MySQLConf) SetConnMaxLiftTime

func (mc MySQLConf) SetConnMaxLiftTime(t time.Duration) MySQLConf

设置每个连接最长的生存周期

func (MySQLConf) SetDbName

func (mc MySQLConf) SetDbName(d string) MySQLConf

设置数据库名称

func (MySQLConf) SetHost

func (mc MySQLConf) SetHost(h string) MySQLConf

设置连接地址

func (MySQLConf) SetMaxIdleConns

func (mc MySQLConf) SetMaxIdleConns(c int) MySQLConf

设置允许的最多多少个空闲连接

func (MySQLConf) SetMaxOpenConns

func (mc MySQLConf) SetMaxOpenConns(c int) MySQLConf

设置最多允许打开的连接数

func (MySQLConf) SetPasswd

func (mc MySQLConf) SetPasswd(p string) MySQLConf

设置密码

func (MySQLConf) SetPort

func (mc MySQLConf) SetPort(p uint16) MySQLConf

设置端口号

func (MySQLConf) SetTimeout

func (mc MySQLConf) SetTimeout(t time.Duration) MySQLConf

设置连接时的超时时间

func (MySQLConf) SetUser

func (mc MySQLConf) SetUser(u string) MySQLConf

设置用户名

Jump to

Keyboard shortcuts

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