Documentation
¶
Overview ¶
Package gdb provides ORM features for popular relationship databases.
数据库ORM, 默认内置支持MySQL, 其他数据库需要手动import对应的数据库引擎第三方包.
Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://gitee.com/johng/gf.
@author wenzi1<liyz23@qq.com> @date 20181109 说明:
1.需要导入sqlserver驱动: github.com/denisenkom/go-mssqldb 2.不支持save/replace方法 3.不支持LastInsertId方法
Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://gitee.com/johng/gf.
@author wenzi1<liyz23@qq.com> @date 20181026 说明:
1.需要导入oracle驱动: github.com/mattn/go-oci8 2.不支持save/replace方法,可以调用这2个方法估计会报错,还没测试过,(应该是可以通过oracle的merge来实现这2个功能的,还没仔细研究) 3.不支持LastInsertId方法
Index ¶
- Constants
- func AddConfigGroup(group string, nodes ConfigGroup)
- func AddConfigNode(group string, node ConfigNode)
- func AddDefaultConfigGroup(nodes ConfigGroup)
- func AddDefaultConfigNode(node ConfigNode)
- func SetConfig(c Config)
- func SetDefaultGroup(groupName string)
- type Config
- type ConfigGroup
- type ConfigNode
- type DB
- type List
- type Map
- type Model
- func (md *Model) All() (Result, error)
- func (md *Model) And(where interface{}, args ...interface{}) *Model
- func (md *Model) Batch(batch int) *Model
- func (md *Model) Cache(time int, name ...string) *Model
- func (md *Model) Chunk(limit int, callback func(result Result, err error) bool)
- func (md *Model) Clone() *Model
- func (md *Model) Count() (int, error)
- func (md *Model) Data(data ...interface{}) *Model
- func (md *Model) Delete() (result sql.Result, err error)
- func (md *Model) Fields(fields string) *Model
- func (md *Model) Filter() *Model
- func (md *Model) ForPage(page, limit int) *Model
- func (md *Model) GroupBy(groupBy string) *Model
- func (md *Model) InnerJoin(joinTable string, on string) *Model
- func (md *Model) Insert() (result sql.Result, err error)
- func (md *Model) LeftJoin(joinTable string, on string) *Model
- func (md *Model) Limit(start int, limit int) *Model
- func (md *Model) One() (Record, error)
- func (md *Model) Or(where interface{}, args ...interface{}) *Model
- func (md *Model) OrderBy(orderBy string) *Model
- func (md *Model) Replace() (result sql.Result, err error)
- func (md *Model) RightJoin(joinTable string, on string) *Model
- func (md *Model) Save() (result sql.Result, err error)
- func (md *Model) Select() (Result, error)
- func (md *Model) Struct(obj interface{}) error
- func (md *Model) Update() (result sql.Result, err error)
- func (md *Model) Value() (Value, error)
- func (md *Model) Where(where interface{}, args ...interface{}) *Model
- type Record
- type Result
- func (r Result) ToIntMap(key string) map[int]Map
- func (r Result) ToIntRecord(key string) map[int]Record
- func (r Result) ToJson() string
- func (r Result) ToList() List
- func (r Result) ToStringMap(key string) map[string]Map
- func (r Result) ToStringRecord(key string) map[string]Record
- func (r Result) ToUintMap(key string) map[uint]Map
- func (r Result) ToUintRecord(key string) map[uint]Record
- func (r Result) ToXml(rootTag ...string) string
- type Sql
- type TX
- func (tx *TX) BatchInsert(table string, list List, batch int) (sql.Result, error)
- func (tx *TX) BatchReplace(table string, list List, batch int) (sql.Result, error)
- func (tx *TX) BatchSave(table string, list List, batch int) (sql.Result, error)
- func (tx *TX) Commit() error
- func (tx *TX) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)
- func (tx *TX) Exec(query string, args ...interface{}) (sql.Result, error)
- func (tx *TX) From(tables string) *Model
- func (tx *TX) GetAll(query string, args ...interface{}) (Result, error)
- func (tx *TX) GetCount(query string, args ...interface{}) (int, error)
- func (tx *TX) GetOne(query string, args ...interface{}) (Record, error)
- func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) error
- func (tx *TX) GetValue(query string, args ...interface{}) (Value, error)
- func (tx *TX) Insert(table string, data Map) (sql.Result, error)
- func (tx *TX) Prepare(query string) (*sql.Stmt, error)
- func (tx *TX) Query(query string, args ...interface{}) (rows *sql.Rows, err error)
- func (tx *TX) Replace(table string, data Map) (sql.Result, error)
- func (tx *TX) Rollback() error
- func (tx *TX) Save(table string, data Map) (sql.Result, error)
- func (tx *TX) Table(tables string) *Model
- func (tx *TX) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
- type Value
Constants ¶
const ( OPTION_INSERT = 0 OPTION_REPLACE = 1 OPTION_SAVE = 2 OPTION_IGNORE = 3 )
const (
DEFAULT_GROUP_NAME = "default" // 默认配置名称
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ConfigNode ¶
type ConfigNode struct { Host string // 地址 Port string // 端口 User string // 账号 Pass string // 密码 Name string // 数据库名称 Type string // 数据库类型:mysql, sqlite, mssql, pgsql, oracle(目前仅支持mysql) Role string // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave Charset string // (可选,默认为 utf8)编码,默认为 utf8 Priority int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义 Linkinfo string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能) MaxIdleConnCount int // (可选)连接池最大限制的连接数 MaxOpenConnCount int // (可选)连接池最大打开的连接数 MaxConnLifetime int // (可选,单位秒)连接对象可重复使用的时间长度 }
数据库单项配置
type DB ¶ added in v1.3.8
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(obj 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 Map) (sql.Result, error) Replace(table string, data Map) (sql.Result, error) Save(table string, data Map) (sql.Result, error) // 数据表插入/更新/保存操作(批量) BatchInsert(table string, list List, batch int) (sql.Result, error) BatchReplace(table string, list List, batch int) (sql.Result, error) BatchSave(table string, list List, 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) // 创建链式操作对象(Table为From的别名) Table(tables string) *Model From(tables string) *Model // 设置管理 SetDebug(debug bool) SetSchema(schema string) GetQueriedSqls() []*Sql PrintQueriedSqls() SetMaxIdleConns(n int) SetMaxOpenConns(n int) SetConnMaxLifetime(n int) // contains filtered or unexported methods }
数据库操作接口
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
数据库链式操作模型对象
func (*Model) Cache ¶
查询缓存/清除缓存操作,需要注意的是,事务查询不支持缓存。 当time < 0时表示清除缓存, time=0时表示不过期, time > 0时表示过期时间,time过期时间单位:秒; name表示自定义的缓存名称,便于业务层精准定位缓存项(如果业务层需要手动清理时,必须指定缓存名称), 例如:查询缓存时设置名称,清理缓存时可以给定清理的缓存名称进行精准清理。
func (*Model) Count ¶
链式操作,查询数量,fields可以为空,也可以自定义查询字段, 当给定自定义查询字段时,该字段必须为数量结果,否则会引起歧义,使用如:md.Fields("COUNT(id)")
type Record ¶
返回数据表记录Map
type Result ¶
type Result []Record
返回数据表记录List
func (Result) ToIntRecord ¶
将结果列表按照指定的字段值做map[int]Record
func (Result) ToStringMap ¶
将结果列表按照指定的字段值做map[string]Map
func (Result) ToStringRecord ¶
将结果列表按照指定的字段值做map[string]Record
func (Result) ToUintRecord ¶
将结果列表按照指定的字段值做map[uint]Record
type Sql ¶
type Sql struct { Sql string // SQL语句(可能带有预处理占位符) Args []interface{} // 预处理参数值列表 Error error // 执行结果(nil为成功) Start int64 // 执行开始时间(毫秒) End int64 // 执行结束时间(毫秒) Func string // 执行方法 }
执行的SQL对象
type TX ¶ added in v1.3.8
type TX struct {
// contains filtered or unexported fields
}
数据库事务对象
func (*TX) BatchInsert ¶ added in v1.3.8
CURD操作:批量数据指定批次量写入
func (*TX) BatchReplace ¶ added in v1.3.8
CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条