gdb

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2019 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package gdb provides ORM features for popular relationship databases.

数据库ORM, 默认内置支持MySQL, 其他数据库需要手动import对应的数据库引擎第三方包.

Index

Constants

View Source
const (
	OPTION_INSERT  = 0
	OPTION_REPLACE = 1
	OPTION_SAVE    = 2
	OPTION_IGNORE  = 3
)
View Source
const (
	DEFAULT_GROUP_NAME = "default" // 默认配置名称
)

Variables

This section is empty.

Functions

func AddConfigGroup

func AddConfigGroup(group string, nodes ConfigGroup)

添加数据库服务器集群配置

func AddConfigNode

func AddConfigNode(group string, node ConfigNode)

添加一台数据库服务器配置

func AddDefaultConfigGroup

func AddDefaultConfigGroup(nodes ConfigGroup)

添加默认链接的数据库服务器集群配置

func AddDefaultConfigNode

func AddDefaultConfigNode(node ConfigNode)

添加默认链接的一台数据库服务器配置

func GetDefaultGroup

func GetDefaultGroup() string

获取默认链接的数据库链接配置项(默认是 default)

func SetConfig

func SetConfig(config Config)

设置当前应用的数据库配置信息,进行全局数据库配置覆盖操作

func SetDefaultGroup

func SetDefaultGroup(name string)

设置默认链接的数据库链接配置项(默认是 default)

Types

type Config

type Config map[string]ConfigGroup

数据库分组配置

type ConfigGroup

type ConfigGroup []ConfigNode

数据库集群配置

func GetConfig

func GetConfig(group string) ConfigGroup

添加一台数据库服务器配置

type ConfigNode

type ConfigNode struct {
	Host             string // 地址
	Port             string // 端口
	User             string // 账号
	Pass             string // 密码
	Name             string // 数据库名称
	Type             string // 数据库类型:mysql, sqlite, mssql, pgsql, oracle
	Role             string // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave
	Debug            bool   // (可选)开启调试模式
	Weight           int    // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义
	Charset          string // (可选,默认为 utf8)编码,默认为 utf8
	LinkInfo         string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能)
	MaxIdleConnCount int    // (可选)连接池最大限制的连接数
	MaxOpenConnCount int    // (可选)连接池最大打开的连接数
	MaxConnLifetime  int    // (可选,单位秒)连接对象可重复使用的时间长度
}

数据库单项配置

func (*ConfigNode) String

func (node *ConfigNode) String() string

节点配置转换为字符串

type DB

type DB interface {
	// 建立数据库连接方法(开发者一般不需要直接调用)
	Open(config *ConfigNode) (*sql.DB, error)

	// SQL操作方法 API
	Query(query string, args ...interface{}) (*sql.Rows, error)
	Exec(sql string, args ...interface{}) (sql.Result, error)
	Prepare(sql string, execOnMaster ...bool) (*sql.Stmt, error)

	// 数据库查询
	GetAll(query string, args ...interface{}) (Result, error)
	GetOne(query string, args ...interface{}) (Record, error)
	GetValue(query string, args ...interface{}) (Value, error)
	GetCount(query string, args ...interface{}) (int, error)
	GetStruct(objPointer interface{}, query string, args ...interface{}) error
	GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error
	GetScan(objPointer interface{}, query string, args ...interface{}) error

	// 创建底层数据库master/slave链接对象
	Master() (*sql.DB, error)
	Slave() (*sql.DB, error)

	// Ping
	PingMaster() error
	PingSlave() error

	// 开启事务操作
	Begin() (*TX, error)

	// 数据表插入/更新/保存操作
	Insert(table string, data interface{}, batch ...int) (sql.Result, error)
	Replace(table string, data interface{}, batch ...int) (sql.Result, error)
	Save(table string, data interface{}, batch ...int) (sql.Result, error)

	// 数据表插入/更新/保存操作(批量)
	BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error)
	BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)
	BatchSave(table string, list interface{}, batch ...int) (sql.Result, error)

	// 数据修改/删除
	Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
	Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)

	// 创建链式操作对象
	From(tables string) *Model
	Table(tables string) *Model

	// 设置管理
	SetDebug(debug bool)
	SetSchema(schema string)
	GetQueriedSqls() []*Sql
	GetLastSql() *Sql
	PrintQueriedSqls()
	SetMaxIdleConnCount(n int)
	SetMaxOpenConnCount(n int)
	SetMaxConnLifetime(n int)
	// contains filtered or unexported methods
}

数据库操作接口

func Instance

func Instance(name ...string) (db DB, err error)

Instance returns an instance for DB operations. The parameter <name> specifies the configuration group name, which is DEFAULT_GROUP_NAME in default.

func New

func New(name ...string) (db DB, err error)

New creates ORM DB object with global configurations. The parameter <name> specifies the configuration group name, which is DEFAULT_GROUP_NAME in default.

type List

type List = []Map

关联数组列表(索引从0开始的数组),绑定多条记录(使用别名)

type Map

type Map = map[string]interface{}

关联数组,绑定一条数据表记录(使用别名)

type Model

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

数据库链式操作模型对象

func (*Model) All

func (md *Model) All() (Result, error)

链式操作,查询所有记录

func (*Model) And

func (md *Model) And(where interface{}, args ...interface{}) *Model

链式操作,添加AND条件到Where中

func (*Model) Batch

func (md *Model) Batch(batch int) *Model

设置批处理的大小

func (*Model) Cache

func (md *Model) Cache(time int, name ...string) *Model

查询缓存/清除缓存操作,需要注意的是,事务查询不支持缓存。 当time < 0时表示清除缓存, time=0时表示不过期, time > 0时表示过期时间,time过期时间单位:秒; name表示自定义的缓存名称,便于业务层精准定位缓存项(如果业务层需要手动清理时,必须指定缓存名称), 例如:查询缓存时设置名称,清理缓存时可以给定清理的缓存名称进行精准清理。

func (*Model) Chunk

func (md *Model) Chunk(limit int, callback func(result Result, err error) bool)

组块结果集。

func (*Model) Clone

func (md *Model) Clone() *Model

克隆一个当前对象

func (*Model) Count

func (md *Model) Count() (int, error)

链式操作,查询数量,fields可以为空,也可以自定义查询字段, 当给定自定义查询字段时,该字段必须为数量结果,否则会引起歧义,使用如:md.Fields("COUNT(id)")

func (*Model) Data

func (md *Model) Data(data ...interface{}) *Model

链式操作,操作数据项,参数data类型支持 string/map/slice/struct/*struct , 也可以是:key,value,key,value,...。

func (*Model) Delete

func (md *Model) Delete() (result sql.Result, err error)

链式操作, CURD - Delete

func (*Model) Fields

func (md *Model) Fields(fields string) *Model

链式操作,查询字段

func (*Model) Filter

func (md *Model) Filter() *Model

链式操作,过滤字段

func (*Model) ForPage

func (md *Model) ForPage(page, limit int) *Model

链式操作,翻页,注意分页页码从1开始,而Limit方法从0开始。

func (*Model) GroupBy

func (md *Model) GroupBy(groupBy string) *Model

链式操作,group by

func (*Model) InnerJoin

func (md *Model) InnerJoin(joinTable string, on string) *Model

链式操作,内联表

func (*Model) Insert

func (md *Model) Insert() (result sql.Result, err error)

链式操作, CURD - Insert/BatchInsert。 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作, 如果Data方法传递的是slice类型,那么为批量操作。

func (*Model) LeftJoin

func (md *Model) LeftJoin(joinTable string, on string) *Model

链式操作,左联表

func (*Model) Limit

func (md *Model) Limit(limit ...int) *Model

链式操作,limit。

如果给定一个参数,那么生成的SQL为:LIMIT limit[0]

如果给定两个参数,那么生成的SQL为:LIMIT limit[0], limit[1]

func (*Model) Offset

func (md *Model) Offset(offset int) *Model

链式操作,OFFSET语法(部分数据库支持)。 注意:可以使用Limit方法调用替换该方法特性,底层不同数据库将会自动替换LIMIT语法为OFFSET语法。

func (*Model) One

func (md *Model) One() (Record, error)

链式操作,查询单条记录

func (*Model) Or

func (md *Model) Or(where interface{}, args ...interface{}) *Model

链式操作,添加OR条件到Where中

func (*Model) OrderBy

func (md *Model) OrderBy(orderBy string) *Model

链式操作,order by

func (*Model) Replace

func (md *Model) Replace() (result sql.Result, err error)

链式操作, CURD - Replace/BatchReplace。 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作, 如果Data方法传递的是slice类型,那么为批量操作。

func (*Model) RightJoin

func (md *Model) RightJoin(joinTable string, on string) *Model

链式操作,右联表

func (*Model) Safe

func (md *Model) Safe(safe ...bool) *Model

标识当前对象运行安全模式(可被修改)。 1. 默认情况下,模型对象的对象属性无法被修改, 每一次链式操作都是克隆一个新的模型对象,这样所有的操作都不会污染模型对象。 但是链式操作如果需要分开执行,那么需要将新的克隆对象赋值给旧的模型对象继续操作。 2. 当标识模型对象为可修改,那么在当前模型对象的所有链式操作均会影响下一次的链式操作, 即使是链式操作分开执行。 3. 大部分ORM框架默认模型对象是可修改的,但是GF框架的ORM提供给开发者更灵活,更安全的链式操作选项。

func (*Model) Save

func (md *Model) Save() (result sql.Result, err error)

链式操作, CURD - Save/BatchSave。 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作, 如果Data方法传递的是slice类型,那么为批量操作。

func (*Model) Scan

func (md *Model) Scan(pointer interface{}) error

链式操作,将结果转换为指定的struct/*struct/[]struct/[]*struct, 参数应该为指针类型,否则返回失败。 该方法自动识别参数类型,调用Struct/Structs方法。

func (*Model) Select

func (md *Model) Select() (Result, error)

链式操作,select

func (*Model) Struct

func (md *Model) Struct(objPointer interface{}) error

链式操作,查询单条记录,并自动转换为struct对象, 参数必须为对象的指针,不能为空指针。

func (*Model) Structs

func (md *Model) Structs(objPointerSlice interface{}) error

链式操作,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。

func (*Model) Update

func (md *Model) Update() (result sql.Result, err error)

链式操作, CURD - Update

func (*Model) Value

func (md *Model) Value() (Value, error)

链式操作,查询字段值

func (*Model) Where

func (md *Model) Where(where interface{}, args ...interface{}) *Model

链式操作,condition,支持string & gdb.Map. 注意,多个Where调用时,会自动转换为And条件调用。

type Record

type Record map[string]Value

返回数据表记录Map

func (Record) ToJson

func (r Record) ToJson() string

将记录结果转换为JSON字符串

func (Record) ToMap

func (r Record) ToMap() Map

将Record转换为Map,其中最主要的区别是里面的键值被强制转换为string类型,方便json处理

func (Record) ToStruct

func (r Record) ToStruct(pointer interface{}) error

将Map变量映射到指定的struct对象中,注意参数应当是一个对象的指针

func (Record) ToXml

func (r Record) ToXml(rootTag ...string) string

将记录结果转换为XML字符串

type Result

type Result []Record

返回数据表记录List

func (Result) ToIntMap

func (r Result) ToIntMap(key string) map[int]Map

将结果列表按照指定的字段值做map[int]Map

func (Result) ToIntRecord

func (r Result) ToIntRecord(key string) map[int]Record

将结果列表按照指定的字段值做map[int]Record

func (Result) ToJson

func (r Result) ToJson() string

将结果集转换为JSON字符串

func (Result) ToList

func (r Result) ToList() List

将结果集转换为List类型返回,便于json处理

func (Result) ToStringMap

func (r Result) ToStringMap(key string) map[string]Map

将结果列表按照指定的字段值做map[string]Map

func (Result) ToStringRecord

func (r Result) ToStringRecord(key string) map[string]Record

将结果列表按照指定的字段值做map[string]Record

func (Result) ToStructs

func (r Result) ToStructs(pointer interface{}) (err error)

将结果列表转换为指定对象的slice。

func (Result) ToUintMap

func (r Result) ToUintMap(key string) map[uint]Map

将结果列表按照指定的字段值做map[uint]Map

func (Result) ToUintRecord

func (r Result) ToUintRecord(key string) map[uint]Record

将结果列表按照指定的字段值做map[uint]Record

func (Result) ToXml

func (r Result) ToXml(rootTag ...string) string

将结果集转换为XML字符串

type Sql

type Sql struct {
	Sql   string        // SQL语句(可能带有预处理占位符)
	Args  []interface{} // 预处理参数值列表
	Error error         // 执行结果(nil为成功)
	Start int64         // 执行开始时间(毫秒)
	End   int64         // 执行结束时间(毫秒)
	Func  string        // 执行方法
}

执行的SQL对象

type TX

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

数据库事务对象

func (*TX) BatchInsert

func (tx *TX) BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error)

CURD操作:批量数据指定批次量写入

func (*TX) BatchReplace

func (tx *TX) BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)

CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条

func (*TX) BatchSave

func (tx *TX) BatchSave(table string, list interface{}, batch ...int) (sql.Result, error)

CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据

func (*TX) Commit

func (tx *TX) Commit() error

事务操作,提交

func (*TX) Delete

func (tx *TX) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)

CURD操作:删除数据

func (*TX) Exec

func (tx *TX) Exec(query string, args ...interface{}) (sql.Result, error)

(事务)执行一条sql,并返回执行情况,主要用于非查询操作

func (*TX) From

func (tx *TX) From(tables string) *Model

(事务)链式操作,数据表字段,可支持多个表,以半角逗号连接

func (*TX) GetAll

func (tx *TX) GetAll(query string, args ...interface{}) (Result, error)

数据库查询,获取查询结果集,以列表结构返回

func (*TX) GetCount

func (tx *TX) GetCount(query string, args ...interface{}) (int, error)

数据库查询,获取查询数量

func (*TX) GetOne

func (tx *TX) GetOne(query string, args ...interface{}) (Record, error)

数据库查询,获取查询结果记录,以关联数组结构返回

func (*TX) GetScan

func (tx *TX) GetScan(objPointer interface{}, query string, args ...interface{}) error

将结果转换为指定的struct/*struct/[]struct/[]*struct, 参数应该为指针类型,否则返回失败。 该方法自动识别参数类型,调用Struct/Structs方法。

func (*TX) GetStruct

func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) error

数据库查询,获取查询结果记录,自动映射数据到给定的struct对象中

func (*TX) GetStructs

func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error

数据库查询,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。

func (*TX) GetValue

func (tx *TX) GetValue(query string, args ...interface{}) (Value, error)

数据库查询,获取查询字段值

func (*TX) Insert

func (tx *TX) Insert(table string, data interface{}, batch ...int) (sql.Result, error)

CURD操作:单条数据写入, 仅仅执行写入操作,如果存在冲突的主键或者唯一索引,那么报错返回

func (*TX) Prepare

func (tx *TX) Prepare(query string) (*sql.Stmt, error)

sql预处理,执行完成后调用返回值sql.Stmt.Exec完成sql操作

func (*TX) Query

func (tx *TX) Query(query string, args ...interface{}) (rows *sql.Rows, err error)

(事务)数据库sql查询操作,主要执行查询

func (*TX) Replace

func (tx *TX) Replace(table string, data interface{}, batch ...int) (sql.Result, error)

CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条

func (*TX) Rollback

func (tx *TX) Rollback() error

事务操作,回滚

func (*TX) Save

func (tx *TX) Save(table string, data interface{}, batch ...int) (sql.Result, error)

CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据

func (*TX) Table

func (tx *TX) Table(tables string) *Model

(事务)链式操作,数据表字段,可支持多个表,以半角逗号连接

func (*TX) Update

func (tx *TX) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)

CURD操作:数据更新,统一采用sql预处理, data参数支持字符串或者关联数组类型,内部会自行做判断处理.

type Value

type Value = *gvar.Var

返回数据表记录值

Jump to

Keyboard shortcuts

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