mysqlClient

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 18 Imported by: 0

README

mysql

有两种,gorm和database/sql

全局 MysqlGorm 客户端

var MysqlGorm map[string]*gorm.DB


配置文件读取配置

func InitMysqlGorm()


NewORM 连接 orm

func NewORM(database, user, password, host, port string, disablePrepared bool) (*gorm.DB, error)


GetGorm 获取gorm对象

func GetGorm(name string) *gorm.DB


SetGorm 设置gorm对象

func SetGorm(c *gorm.DB, name string)


MysqlDB 全局对象

给mysql对象进行连接

func NewMysqlDB(host, port, user, password, database string) (err error)


创建一个mysql对象

func NewMysql(host, port, user, password, database string) (*Mysql, error)


获取mysql 连接

func GetMysqlDBConn() (*Mysql, error)


CloseLog 关闭日志

func (m *Mysql) CloseLog()


SetMaxOpenConn 最大连接数

func (m *Mysql) SetMaxOpenConn(number int)


SetMaxIdleConn 最大idle 数

func (m *Mysql) SetMaxIdleConn(number int)


Conn 连接mysql

func (m *Mysql) Conn() (err error)


IsHaveTable 表是否存在

func (m *Mysql) IsHaveTable(table string) bool


TableInfo 表信息
type TableInfo struct {
	Field   string
	Type    string
	Null    string
	Key     string
	Default interface{}
	Extra   string
}

Describe 获取表结构

func (m *Mysql) Describe(table string) (*TableDescribe, error)


Select 查询语句 返回 map

func (m *Mysql) Select(sql string) ([]map[string]string, error)


NewTable 创建表

func (m *Mysql) NewTable(table string, fields map[string]string) error

参数

fields  字段:类型; name:varchar(10);

NewTableGd 创建新的固定map顺序为字段的表

func (m *Mysql) NewTableGd(table string, fields *utils.GDMap) error


Insert 新增数据

func (m *Mysql) Insert(table string, fieldData map[string]interface{}) error


InsertAt 新增数据 如果没有表则先创建表

func (m *Mysql) InsertAt(table string, fieldData map[string]interface{}) error


InsertAtGd 固定顺序map写入

func (m *Mysql) InsertAtGd(table string, fieldData *utils.GDMap) error


InsertAtJson json字符串存入数据库

func (m *Mysql) InsertAtJson(table, jsonStr string) error


Update 更新sql

func (m *Mysql) Update(sql string) error


Exec 执行sql

func (m *Mysql) Exec(sql string) error


Query 执行selete sql

func (m *Mysql) Query(sql string) ([]map[string]string, error)


Delete 执行delete sql

func (m *Mysql) Delete(sql string) error


ToVarChar 写入mysql 的字符类型

func (m *Mysql) ToVarChar(data interface{}) string


DeleteTable 删除表

func (m *Mysql) DeleteTable(tableName string) error


HasTable 判断表是否存

func (m *Mysql) HasTable(tableName string) bool


GetFieldList 获取表字段

func (m *Mysql) GetFieldList(table string) (fieldList []string)


ToXls 数据库查询输出到excel

func (m *Mysql) ToXls(sql, outPath string)


StringValueMysql 用于mysql字符拼接使用

func StringValueMysql(i interface{}) string


Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SqlShowTables    = "SHOW TABLES"
	ErrTableNameNull = fmt.Errorf("err: table name is null.")
	ErrTableIsNull   = fmt.Errorf("err: table is null.")
)
View Source
var MysqlDB = &Mysql{}
View Source
var MysqlGorm map[string]*gorm.DB

Functions

func GetGorm

func GetGorm(name string) *gorm.DB

GetGorm 获取gorm对象

func InitMysqlGorm

func InitMysqlGorm()

InitMysqlGorm 配置文件读取配置

func NewMysqlDB

func NewMysqlDB(host, port, user, password, database string) (err error)

NewMysqlDB 给mysql对象进行连接

func NewORM

func NewORM(database, user, password, host, port string, disablePrepared bool) (*gorm.DB, error)

NewORM 连接 orm

func SetGorm

func SetGorm(c *gorm.DB, name string)

func StringValueMysql

func StringValueMysql(i interface{}) string

StringValueMysql 用于mysql字符拼接使用

Types

type Mysql

type Mysql struct {
	DB *sql.DB
	// contains filtered or unexported fields
}

Mysql 客户端对象

func GetMysqlDBConn

func GetMysqlDBConn() (*Mysql, error)

GetMysqlDBConn 获取mysql 连接

func NewMysql

func NewMysql(host, port, user, password, database string) (*Mysql, error)

NewMysql 创建一个mysql对象

func (*Mysql) CloseLog

func (m *Mysql) CloseLog()

CloseLog 关闭日志

func (*Mysql) Conn

func (m *Mysql) Conn() (err error)

Conn 连接mysql

func (*Mysql) Delete

func (m *Mysql) Delete(sql string) error

Delete 执行delete sql

func (*Mysql) DeleteTable

func (m *Mysql) DeleteTable(tableName string) error

DeleteTable 删除表

func (*Mysql) Describe

func (m *Mysql) Describe(table string) (*TableDescribe, error)

Describe 获取表结构

func (*Mysql) Exec

func (m *Mysql) Exec(sql string) error

Exec 执行sql

func (*Mysql) GetFieldList

func (m *Mysql) GetFieldList(table string) (fieldList []string)

GetFieldList 获取表字段

func (*Mysql) HasTable

func (m *Mysql) HasTable(tableName string) bool

HasTable 判断表是否存

func (*Mysql) Insert

func (m *Mysql) Insert(table string, fieldData map[string]interface{}) error

Insert 新增数据

func (*Mysql) InsertAt

func (m *Mysql) InsertAt(table string, fieldData map[string]interface{}) error

InsertAt 新增数据 如果没有表则先创建表

func (*Mysql) InsertAtGd

func (m *Mysql) InsertAtGd(table string, fieldData *utils.GDMap) error

InsertAtGd 固定顺序map写入

func (*Mysql) InsertAtJson

func (m *Mysql) InsertAtJson(table, jsonStr string) error

InsertAtJson json字符串存入数据库

func (*Mysql) IsHaveTable

func (m *Mysql) IsHaveTable(table string) bool

IsHaveTable 表是否存在

func (*Mysql) NewTable

func (m *Mysql) NewTable(table string, fields map[string]string) error

NewTable 创建表 字段顺序不固定 fields 字段:类型; name:varchar(10);

func (*Mysql) NewTableGd

func (m *Mysql) NewTableGd(table string, fields *utils.GDMap) error

NewTableGd 创建新的固定map顺序为字段的表

func (*Mysql) Query

func (m *Mysql) Query(sql string) ([]map[string]string, error)

Query 执行 select sql

func (*Mysql) Select

func (m *Mysql) Select(sql string) ([]map[string]string, error)

Select 查询语句 返回 map

func (*Mysql) SetMaxIdleConn

func (m *Mysql) SetMaxIdleConn(number int)

SetMaxIdleConn 最大idle 数

func (*Mysql) SetMaxOpenConn

func (m *Mysql) SetMaxOpenConn(number int)

SetMaxOpenConn 最大连接数

func (*Mysql) ToVarChar

func (m *Mysql) ToVarChar(data interface{}) string

ToVarChar 写入mysql 的字符类型

func (*Mysql) ToXls

func (m *Mysql) ToXls(sql, outPath string)

ToXls 数据库查询输出到excel

func (*Mysql) Update

func (m *Mysql) Update(sql string) error

Update 更新sql

type Page added in v0.1.4

type Page struct {
	Page  int `json:"page"`
	Limit int `json:"limit"`
}

func NewPage added in v0.1.4

func NewPage(page, limit int) *Page

func (*Page) Pagination added in v0.1.4

func (page *Page) Pagination() func(db *gorm.DB) *gorm.DB

Pagination 分页

func (*Page) String added in v0.1.4

func (page *Page) String() string

type TableDescribe

type TableDescribe struct {
	Base map[string]string
}

type TableInfo

type TableInfo struct {
	Field   string
	Type    string
	Null    string
	Key     string
	Default interface{}
	Extra   string
}

TableInfo 表信息

Jump to

Keyboard shortcuts

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