Documentation ¶
Overview ¶
Package orm 简单的DAL 封装
Index ¶
- func Add(op *Op, entity Entity) error
- func AddOrUpdate(op *Op, entity Entity) (int64, error)
- func Del(op *Op, entity Entity, id interface{}) (bool, error)
- func DelByCondition(op *Op, entity Entity, condition string, params ...interface{}) (int64, error)
- func QueryColumnsForDestSlice(op *Op, entity Entity, destSlicePtr interface{}, columns []string, ...) (err error)
- func QueryCount(op *Op, entity Entity, column string, condition string, params ...interface{}) (num int64, err error)
- func Update(op *Op, entity Entity) (bool, error)
- func UpdateColumns(op *Op, entity Entity, columns string, condition string, params ...interface{}) (int64, error)
- func UpdateExcludeColumns(op *Op, entity Entity, columns ...string) (bool, error)
- func UpdateReplace(op *Op, entity Entity, replColumns map[string]ReplColumn, ...) (bool, error)
- type BaseShardEntity
- type DBConfig
- type DBConfigurer
- type DBError
- type DBService
- type DBShardConfig
- type DBShardConfigurer
- type Entity
- type EntityShardConfig
- type EntityShardConfigurer
- type EntityShardRuleConfig
- type EntitySlice
- type HashRule
- type Meta
- type NamedRule
- type NullTime
- type NumRangeRule
- type OneRule
- type Op
- func (p *Op) BeginTx() (err error)
- func (p *Op) Commit() error
- func (p *Op) DB() *sql.DB
- func (p *Op) DoInTrans(peration OpTxFunc) (rt interface{}, err error)
- func (p *Op) IsRollbackOnly() bool
- func (p *Op) Pool() *Pool
- func (p *Op) PoolName() string
- func (p *Op) Rollback() error
- func (p *Op) SetRollbackOnly(rollback bool)
- func (p *Op) SetupTableShard(entity Entity, ruleName string) error
- type OpCreator
- type OpTxFunc
- type Pool
- type PoolFunc
- type ReplColumn
- type ShardDBService
- type ShardEntity
- type ShardHandler
- type ShardPolicy
- type ShardRule
- type SimpleDBService
- type SimpleShardDBService
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddOrUpdate ¶
AddOrUpdate 添加或者更新实体(如果id已经存在),只支持MySql
func DelByCondition ¶
DelByCondition 根据条件删除
func QueryColumnsForDestSlice ¶
func QueryColumnsForDestSlice(op *Op, entity Entity, destSlicePtr interface{}, columns []string, condition string, params ...interface{}) (err error)
QueryColumnsForDestSlice 根据条件查询数据,结果保存到destSlicePtr
func QueryCount ¶
func QueryCount(op *Op, entity Entity, column string, condition string, params ...interface{}) (num int64, err error)
QueryCount 根据条件查询条数
func UpdateColumns ¶
func UpdateColumns(op *Op, entity Entity, columns string, condition string, params ...interface{}) (int64, error)
UpdateColumns 更新列
func UpdateExcludeColumns ¶
UpdateExcludeColumns 更新除columns之外的字段
func UpdateReplace ¶ added in v1.1.15
func UpdateReplace(op *Op, entity Entity, replColumns map[string]ReplColumn, excludeColumns map[string]struct{}) (bool, error)
UpdateReplace 更新实体
Types ¶
type BaseShardEntity ¶
type BaseShardEntity struct {
// contains filtered or unexported fields
}
BaseShardEntity 基础的分片实体
func (*BaseShardEntity) SetTableShardFunc ¶
func (p *BaseShardEntity) SetTableShardFunc(f ShardHandler)
SetTableShardFunc implements ShardEntity.SetTableShardFunc
func (*BaseShardEntity) TableShardFunc ¶
func (p *BaseShardEntity) TableShardFunc() ShardHandler
TableShardFunc implements ShardEntity.TableShardFunc
type DBConfig ¶
type DBConfig struct { User string `yaml:"user"` Pass string `yaml:"pass"` URL string `yaml:"url"` Schema string `yaml:"schema"` MaxConn int `yaml:"maxConn"` MaxIdle int `yaml:"maxIdle"` MaxTimeSecond int `yaml:"maxTimeSecond"` Charset string `yaml:"charset"` Ext map[string]string `yaml:"ext"` }
DBConfig 数据库配置
type DBConfigurer ¶
type DBConfigurer interface { c.Configurer DBConfig() *DBConfig }
DBConfigurer DB配置器
type DBShardConfig ¶
type DBShardConfig struct { Shards map[string]*DBConfig `yaml:"shards"` Default string `yaml:"default"` }
DBShardConfig db shard config
func (*DBShardConfig) DBShardConfig ¶
func (p *DBShardConfig) DBShardConfig() *DBShardConfig
DBShardConfig implements DBShardConfigurer
func (*DBShardConfig) Parse ¶
func (p *DBShardConfig) Parse() error
Parse implements Configurer.Parse
type DBShardConfigurer ¶
type DBShardConfigurer interface { c.Configurer DBShardConfig() *DBShardConfig }
DBShardConfigurer db shard configurer
type EntityShardConfig ¶
type EntityShardConfig struct { // pkgPath -> entity name -> rules Entities map[string]map[string][]*EntityShardRuleConfig `yaml:"entities"` // contains filtered or unexported fields }
EntityShardConfig entity shad config
func (*EntityShardConfig) EntityShardConfig ¶
func (p *EntityShardConfig) EntityShardConfig() *EntityShardConfig
EntityShardConfig implements EntityShardConfigurer
func (*EntityShardConfig) Parse ¶
func (p *EntityShardConfig) Parse() error
Parse implements Configurer.Parse
type EntityShardConfigurer ¶
type EntityShardConfigurer interface { c.Configurer EntityShardConfig() *EntityShardConfig }
EntityShardConfigurer entity shard configurer
type EntityShardRuleConfig ¶
type EntityShardRuleConfig struct { Name string `yaml:"name"` //名称 DBShard *OneRule `yaml:"db_shard"` //数据库实例的配置 TableShard *OneRule `yaml:"table_shard"` //数据库表的配置 Default bool `yaml:"default"` //是否是默认规则 // contains filtered or unexported fields }
EntityShardRuleConfig 实体的shard规则
func (*EntityShardRuleConfig) Parse ¶
func (p *EntityShardRuleConfig) Parse() error
Parse implements Configurer.Parse
type EntitySlice ¶
type EntitySlice []Entity
EntitySlice type for slice of EntityInterface
func (EntitySlice) ToInterface ¶
func (p EntitySlice) ToInterface() []interface{}
ToInterface convert EntitySlice to []interface{}
type HashRule ¶
type HashRule struct { Count int64 `yaml:"count"` //hash的个数 NamePrefix string `yaml:"name_prefix"` //名称的前缀 FieldName string `yaml:"field_name"` //hash取值的字段名 }
HashRule hash规则
func (*HashRule) ShardFieldName ¶
ShardFieldName 用于分片的字段名
type Meta ¶
type Meta interface { Name() string Type() reflect.Type FieldValue(entity Entity, name string) (val interface{}, err error) }
Meta meta
type NamedRule ¶
type NamedRule struct {
Name string `yaml:"name"`
}
NamedRule 指定命名
func (*NamedRule) ShardFieldName ¶
ShardFieldName 用于分片的字段名
type NumRangeRule ¶
type NumRangeRule struct { FieldName string `yaml:"field_name"` //分片取值的字段名 DefaultName string `yaml:"default_name"` //默认名称 Ranges []*struct { Begin int64 `yaml:"begin"` End int64 `yaml:"end"` Name string `yaml:"name"` } `yaml:"ranges"` }
NumRangeRule 数字区间
func (*NumRangeRule) Policy ¶
func (p *NumRangeRule) Policy() ShardPolicy
Policy implements ShardRule
func (*NumRangeRule) Shard ¶
func (p *NumRangeRule) Shard(val interface{}) (shardName string, err error)
Shard implements ShardRule.Shard
func (*NumRangeRule) ShardFieldName ¶
func (p *NumRangeRule) ShardFieldName() string
ShardFieldName 用于分片的字段名
type OneRule ¶
type OneRule struct { Hash *HashRule `yaml:"hash"` Named *NamedRule `yaml:"named"` NumRange *NumRangeRule `yaml:"num_range"` // contains filtered or unexported fields }
OneRule 选择一个
func (*OneRule) Policy ¶
func (p *OneRule) Policy() ShardPolicy
Policy implements ShardPolicy.Policy
type Op ¶
type Op struct {
// contains filtered or unexported fields
}
Op 数据库操作接口,与sql.DB对应,封装了事务等
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool 数据库连接池
func NewMySQLDBPool ¶
NewMySQLDBPool build mysql db pool from config
type ReplColumn ¶ added in v1.1.15
type ReplColumn struct { Repl string ParamVal interface{} }
ReplColumn replace column
type ShardDBService ¶
type ShardDBService interface { DBService // NewOpByShardName create op by shard name NewOpByShardName(name string) (op *Op, err error) // NewOpByEntity create Op for entity with rule name,if rule name is empty use default rule NewOpByEntity(entity Entity, ruleName string) (op *Op, err error) // contains filtered or unexported methods }
ShardDBService 支持分库,分表的DBService
type ShardEntity ¶
type ShardEntity interface { Entity //TableShardFunc table分片函数 TableShardFunc() ShardHandler //SetTableShardFunc table设置分片函数 SetTableShardFunc(ShardHandler) }
ShardEntity 支持按表分片实体的接口
type ShardPolicy ¶
type ShardPolicy string
ShardPolicy 分片规则
const ( //Hash hash shard Hash ShardPolicy = "hash" //Named named shard Named ShardPolicy = "named" //NumRange number range shard NumRange ShardPolicy = "num_range" )
type ShardRule ¶
type ShardRule interface { c.Configurer // Policy 返回策略名称 Policy() ShardPolicy // Shard 计算分片的名称 Shard(val interface{}) (shardName string, err error) // ShardFieldName 用于分片的字段名称 ShardFieldName() string }
ShardRule 分片规则的实现
type SimpleDBService ¶
type SimpleDBService struct { Config DBConfigurer `inject:"_"` // contains filtered or unexported fields }
SimpleDBService implements DBService interface
func NewSimpleDBService ¶
func NewSimpleDBService(poolFunc PoolFunc) *SimpleDBService
NewSimpleDBService build simple db service
func (*SimpleDBService) Init ¶
func (p *SimpleDBService) Init() error
Init implements Initable.Init()
func (*SimpleDBService) NewOp ¶
func (p *SimpleDBService) NewOp() (*Op, error)
NewOp implements DBService.NewOp()
type SimpleShardDBService ¶
type SimpleShardDBService struct { DBShardConfig DBShardConfigurer `inject:"_"` EntityShardConfig EntityShardConfigurer `inject:"_,optional"` // contains filtered or unexported fields }
SimpleShardDBService implements DBService interface
func NewSimpleShardDBService ¶
func NewSimpleShardDBService(poolFunc PoolFunc) *SimpleShardDBService
NewSimpleShardDBService create
func (*SimpleShardDBService) Init ¶
func (p *SimpleShardDBService) Init() error
Init implements Initable.Init()
func (*SimpleShardDBService) NewOp ¶
func (p *SimpleShardDBService) NewOp() (op *Op, err error)
NewOp create default op
func (*SimpleShardDBService) NewOpByEntity ¶
func (p *SimpleShardDBService) NewOpByEntity(entity Entity, ruleName string) (op *Op, err error)
NewOpByEntity create Op for entity with rule name,if rule name is empty use default rule
func (*SimpleShardDBService) NewOpByShardName ¶
func (p *SimpleShardDBService) NewOpByShardName(poolName string) (op *Op, err error)
NewOpByShardName create Op by shard name