mysql

package
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyCond = errors.New("条件不能为空")

防止在update、delete操作时,漏掉条件造成的严重后果 如果确实不需要条件,请将条件设置为 1=1

Functions

func Addslashes

func Addslashes(v string) string

转义字符串防止sql注入 会在 \ ' " 之前增加斜杠\

func CreateAnyTypeSlice added in v0.1.6

func CreateAnyTypeSlice(slice interface{}) ([]interface{}, bool)

CreateAnyTypeSlice interface{}转为 []interface{}

func Init added in v0.1.6

func Init(conf *gjson.Result) error

配置文件格式 [

{"name": "test_w", "user": "root", "passwd": "root", "host": "127.0.0.1", "port": 3306, "dbname": "test", "charset": "utf8", "maxOpenConns": 1000},
{"name": "test1-r", "user": "root", "passwd": "root", "host": "127.0.0.1", "port": 3306, "dbname": "test1", "charset": "utf8", "maxOpenConns": 1000}

]

框架初始化时调用,请不要在业务中调用 Init 初始化数据库连接

Types

type Builder

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

查询构建器

func (*Builder) Clear

func (b *Builder) Clear() *Builder

Clear 清空当前where (每次调用Where会向当前查询构建器中暂存条件,用于链式调用)

func (*Builder) Count

func (b *Builder) Count() int64

Count 查询统计个数

func (*Builder) Decrement

func (b *Builder) Decrement(column string, amount int64, set ...map[string]interface{}) (int64, error)

Decrement 自减 column string 自减的字段 amount int 自减的数量 set map[string]interface{} (可选参数)自减同时update的字段 返回影响的条数,错误信息

func (*Builder) Delete

func (b *Builder) Delete(limit ...int) (int64, error)

Delete 删除

func (*Builder) Fetch

func (b *Builder) Fetch(fields string, order string, group string, limit int, start int) ([]map[string]interface{}, error)

Fetch 查询并返回多条记录 field string 返回的字段 示例:"*" order string 排序 示例:"display_order DESC,id DESC" group string 分组字段 示例:"user_group" limit 结果数量 start 起始位置

func (*Builder) FetchOne

func (b *Builder) FetchOne(field string, order string, group string, start int) string

FetchOne 查询并返回单个字段 field string 返回的字段 示例:"count(*) AS count" order string 排序 示例:"display_order DESC,id DESC" group string 分组字段 示例:"user_group" start 起始位置

func (*Builder) FetchRow

func (b *Builder) FetchRow(fields string, order string, group string, start int) (map[string]interface{}, error)

FetchRow 查询并返回单条记录 field string 返回的字段 示例:"*" order string 排序 示例:"display_order DESC,id DESC" group string 分组字段 示例:"user_group" start 起始位置

func (*Builder) FetchWithPage

func (b *Builder) FetchWithPage(fields string, order string, group string, limit int, page int) (*ResultData, error)

FetchWithPage 查询并返回多条记录,且包含分页信息 field string 返回的字段 示例:"*" order string 排序 示例:"display_order DESC,id DESC" group string 分组字段 示例:"user_group" limit 结果数量 page 第几页,从1开始

func (*Builder) Increment

func (b *Builder) Increment(column string, amount int64, set ...map[string]interface{}) (int64, error)

Increment 自增 column string 自增的字段 amount int 自增的数量 set map[string]interface{} (可选参数)自增同时update的字段 返回影响的条数,错误信息

func (*Builder) Insert

func (b *Builder) Insert(set map[string]interface{}) (int64, error)

Insert 单条插入 set map[string]interface{} 插入的数据 返回插入的id,错误信息

func (*Builder) MakeQueryString

func (b *Builder) MakeQueryString(fields string, order string, group string, limit int, start int) string

MakeQueryString 拼接查询语句字符串

func (*Builder) Update

func (b *Builder) Update(set map[string]interface{}, limit ...int) (int64, error)

Update 更新 set map[string]interface{} 更新的字段 limit (可选参数)限制更新limit 返回影响的条数,错误信息

func (*Builder) Where added in v0.1.6

func (b *Builder) Where(cond ...interface{}) *Builder

Where 自动识别查询 推荐使用 cond string 字符串查询条件 cond map[string]interface{} map查询条件,同WhereMap cond string, []interface{} In查询条件,同WhereIn cond string, []anytype In查询条件,同WhereIn

func (*Builder) WhereClause

func (b *Builder) WhereClause(cond string) *Builder

WhereClause 子句查询

func (*Builder) WhereIn

func (b *Builder) WhereIn(k string, in []interface{}) *Builder

WhereIn IN查询

func (*Builder) WhereMap

func (b *Builder) WhereMap(cond map[string]interface{}) *Builder

WhereMap Map查询 会自动将map拼接为`k1`='v2' AND `k2`='v2' 的形式 map的某个key对应的值为任意类型切片时,会将此key及其对应的切片转换为IN查询条件

func (*Builder) WhereNotIn added in v0.1.6

func (b *Builder) WhereNotIn(k string, in []interface{}) *Builder

WhereNotIn NOT IN查询

type Db

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

func Database added in v0.1.6

func Database(name string) (*Db, error)

Database 通过名称获取数据库

func (*Db) Exec

func (d *Db) Exec(query string) (sql.Result, error)

执行sql

func (*Db) GetAll

func (d *Db) GetAll(query string) ([]map[string]interface{}, error)

直接执行sql原生语句并返回多行

func (*Db) GetRow

func (d *Db) GetRow(query string) (map[string]interface{}, error)

直接执行sql原生语句并返回1行

func (*Db) GetTablePrefix added in v0.1.6

func (d *Db) GetTablePrefix() string

GetTablePrefix 获取表前缀

func (*Db) Query

func (d *Db) Query(query string) (*sql.Rows, error)

查询sql并返回结果集

func (*Db) Table

func (d *Db) Table(table string) *Builder

获取查询构建器

type ResultData

type ResultData struct {
	List      []map[string]interface{} `json:"list"`
	Count     int64                    `json:"count"`
	PerPage   int                      `json:"pre_page"`
	Page      int                      `json:"page"`
	PageCount int                      `json:"page_count"`
	Start     int                      `json:"start"`
	Mark      int                      `json:"mark"`
}

分页返回数据 - 返回结果定义

func Multi

func Multi(count int64, page int, perpage int) ResultData

Multi 构造分页返回结果 count int64 总数 perpage int64 每页数量 page int64 当前页

func (*ResultData) ToMap

func (r *ResultData) ToMap() map[string]interface{}

分页结果集转化为map

type ResultRow

type ResultRow struct {
}

分页返回数据 - 表字段名定义

Jump to

Keyboard shortcuts

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