Documentation
¶
Overview ¶
数据库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 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
- func (db *Db) BatchInsert(table string, list List, batch int) (sql.Result, error)
- func (db *Db) BatchReplace(table string, list List, batch int) (sql.Result, error)
- func (db *Db) BatchSave(table string, list List, batch int) (sql.Result, error)
- func (db *Db) Begin() (*Tx, error)
- func (db *Db) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)
- func (db *Db) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Db) From(tables string) *Model
- func (db *Db) GetAll(query string, args ...interface{}) (Result, error)
- func (db *Db) GetCount(query string, args ...interface{}) (int, error)
- func (db *Db) GetOne(query string, args ...interface{}) (Record, error)
- func (db *Db) GetQueriedSqls() []*Sql
- func (db *Db) GetStruct(obj interface{}, query string, args ...interface{}) error
- func (db *Db) GetValue(query string, args ...interface{}) (Value, error)
- func (db *Db) Insert(table string, data Map) (sql.Result, error)
- func (db *Db) Master() (*sql.DB, error)
- func (db *Db) PingMaster() error
- func (db *Db) PingSlave() error
- func (db *Db) Prepare(query string) (*sql.Stmt, error)
- func (db *Db) PrintQueriedSqls()
- func (db *Db) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *Db) Replace(table string, data Map) (sql.Result, error)
- func (db *Db) Save(table string, data Map) (sql.Result, error)
- func (db *Db) Select(tables, fields string, condition interface{}, groupBy, orderBy string, ...) (Result, error)
- func (db *Db) SetConnMaxLifetime(n int)
- func (db *Db) SetDebug(debug bool)
- func (db *Db) SetMaxIdleConns(n int)
- func (db *Db) SetMaxOpenConns(n int)
- func (db *Db) Slave() (*sql.DB, error)
- func (db *Db) Table(tables string) *Model
- func (db *Db) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
- type Link
- 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) 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) 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{}) (*sql.Rows, 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) Select(tables, fields string, condition interface{}, groupBy, orderBy string, ...) (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 ¶
type Db struct {
// contains filtered or unexported fields
}
数据库链接对象
func (*Db) BatchInsert ¶
CURD操作:批量数据指定批次量写入
func (*Db) BatchReplace ¶
CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条
func (*Db) Begin ¶
事务操作,开启,会返回一个底层的事务操作对象链接如需要嵌套事务,那么可以使用该对象,否则请忽略 只有在tx.Commit/tx.Rollback时,链接会自动Close
func (*Db) Select ¶
func (db *Db) Select(tables, fields string, condition interface{}, groupBy, orderBy string, first, limit int, args ...interface{}) (Result, error)
数据表查询,其中tables可以是多个联表查询语句,这种查询方式较复杂,建议使用链式操作
func (*Db) SetConnMaxLifetime ¶
设置数据库连接可重复利用的时间,超过该时间则被关闭废弃 如果 d <= 0 表示该链接会一直重复利用
type Link ¶
type Link interface { // 打开数据库连接,建立数据库操作对象 Open(c *ConfigNode) (*sql.DB, error) // SQL操作方法 Query(q string, args ...interface{}) (*sql.Rows, error) Exec(q string, args ...interface{}) (sql.Result, error) Prepare(q string) (*sql.Stmt, error) // 数据库查询 GetAll(q string, args ...interface{}) (Result, error) GetOne(q string, args ...interface{}) (Record, error) GetValue(q string, args ...interface{}) (Value, error) // Ping PingMaster() error PingSlave() error // 连接属性设置 SetMaxIdleConns(n int) SetMaxOpenConns(n int) SetConnMaxLifetime(n int) // 开启事务操作 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 // 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 ¶
type Tx struct {
// contains filtered or unexported fields
}
数据库事务对象
func (*Tx) BatchInsert ¶
CURD操作:批量数据指定批次量写入
func (*Tx) BatchReplace ¶
CURD操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条