Documentation
¶
Overview ¶
Package gdb provides ORM features for popular relationship databases.
数据库ORM, 默认内置支持MySQL, 其他数据库需要手动import对应的数据库引擎第三方包.
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) Safe(safe ...bool) *Model
- func (md *Model) Save() (result sql.Result, err error)
- func (md *Model) Scan(objPointer interface{}) error
- func (md *Model) Select() (Result, error)
- func (md *Model) Struct(objPointer interface{}) error
- func (md *Model) Structs(objPointerSlice 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) ToStructs(objPointerSlice interface{}) error
- 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 interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) BatchSave(table string, list interface{}, 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) GetScan(objPointer interface{}, query string, args ...interface{}) error
- func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) error
- func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...interface{}) error
- func (tx *TX) GetValue(query string, args ...interface{}) (Value, error)
- func (tx *TX) Insert(table string, data interface{}, batch ...int) (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 interface{}, batch ...int) (sql.Result, error)
- func (tx *TX) Rollback() error
- func (tx *TX) Save(table string, data interface{}, batch ...int) (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 ¶
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) // 创建链式操作对象(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)")
func (*Model) Data ¶
链式操作,操作数据项,参数data类型支持 string/map/slice/struct/*struct , 也可以是:key,value,key,value,...。
func (*Model) Insert ¶
链式操作, CURD - Insert/BatchInsert。 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作, 如果Data方法传递的是slice类型,那么为批量操作。
func (*Model) Replace ¶
链式操作, CURD - Replace/BatchReplace。 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作, 如果Data方法传递的是slice类型,那么为批量操作。
func (*Model) Safe ¶
标识当前对象运行安全模式(可被修改)。 1. 默认情况下,模型对象的对象属性无法被修改, 每一次链式操作都是克隆一个新的模型对象,这样所有的操作都不会污染模型对象。 但是链式操作如果需要分开执行,那么需要将新的克隆对象赋值给旧的模型对象继续操作。 2. 当标识模型对象为可修改,那么在当前模型对象的所有链式操作均会影响下一次的链式操作, 即使是链式操作分开执行。 3. 大部分ORM框架默认模型对象是可修改的,但是GF框架的ORM提供给开发者更灵活,更安全的链式操作选项。
func (*Model) Save ¶
链式操作, CURD - Save/BatchSave。 根据Data方法传递的参数类型决定该操作是单条操作还是批量操作, 如果Data方法传递的是slice类型,那么为批量操作。
func (*Model) Scan ¶
链式操作,将结果转换为指定的struct/*struct/[]struct/[]*struct, 参数应该为指针类型,否则返回失败。 该方法自动识别参数类型,调用Struct/Structs方法。
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操作:批量数据指定批次量写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条
func (*TX) GetScan ¶
将结果转换为指定的struct/*struct/[]struct/[]*struct, 参数应该为指针类型,否则返回失败。 该方法自动识别参数类型,调用Struct/Structs方法。
func (*TX) GetStructs ¶
数据库查询,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。