orm

package
v1.1.40 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package orm 简单的DAL 封装

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(op *Op, entity Entity) error

Add 添加实体

func AddOrUpdate

func AddOrUpdate(op *Op, entity Entity) (int64, error)

AddOrUpdate 添加或者更新实体(如果id已经存在),只支持MySql

func Del

func Del(op *Op, entity Entity, id interface{}) (bool, error)

Del 根据ID删除实体

func DelByCondition

func DelByCondition(op *Op, entity Entity, condition string, params ...interface{}) (int64, error)

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 Update

func Update(op *Op, entity Entity) (bool, error)

Update 更新实体

func UpdateColumns

func UpdateColumns(op *Op, entity Entity, columns string, condition string, params ...interface{}) (int64, error)

UpdateColumns 更新列

func UpdateExcludeColumns

func UpdateExcludeColumns(op *Op, entity Entity, columns ...string) (bool, error)

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 数据库配置

func (*DBConfig) DBConfig

func (p *DBConfig) DBConfig() *DBConfig

DBConfig implements DBConfigurer

func (*DBConfig) Parse

func (p *DBConfig) Parse() error

Parse implements DBConfigurer

type DBConfigurer

type DBConfigurer interface {
	c.Configurer
	DBConfig() *DBConfig
}

DBConfigurer DB配置器

type DBError

type DBError struct {
	Msg string
	Err error
}

DBError 数据库操作错误

func NewDBError

func NewDBError(err error, msg string) *DBError

NewDBError 构建数据库操作错误

func NewDBErrorf

func NewDBErrorf(err error, msgFormat string, args ...interface{}) *DBError

NewDBErrorf 使用fmt.Sprintf构建

func (*DBError) Error

func (e *DBError) Error() string

type DBService

type DBService interface {
	c.Initable
	OpCreator
}

DBService is the service that supply DBOper

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 Entity

type Entity interface {
	TableName() string
}

Entity 实体接口

func Get

func Get(op *Op, entity Entity, id interface{}) (Entity, error)

Get 根据ID查询实体

func Query

func Query(op *Op, entity Entity, condition string, params ...interface{}) ([]Entity, error)

Query 根据条件查询实体

func QueryColumns

func QueryColumns(op *Op, entity Entity, columns []string, condition string, params ...interface{}) ([]Entity, error)

QueryColumns 根据条件查询columns指定的字段

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) Parse

func (p *HashRule) Parse() error

Parse implements Configurer

func (*HashRule) Policy

func (p *HashRule) Policy() ShardPolicy

Policy implements ShardRule

func (*HashRule) Shard

func (p *HashRule) Shard(val interface{}) (shardName string, err error)

Shard implements ShardRule.Shard

func (*HashRule) ShardFieldName

func (p *HashRule) ShardFieldName() string

ShardFieldName 用于分片的字段名

type Meta

type Meta interface {
	Name() string
	Type() reflect.Type
	FieldValue(entity Entity, name string) (val interface{}, err error)
}

Meta meta

func AddMeta

func AddMeta(entity Entity) Meta

AddMeta register entity meta

func MetaOf

func MetaOf(entity Entity) Meta

MetaOf parse meta

type NamedRule

type NamedRule struct {
	Name string `yaml:"name"`
}

NamedRule 指定命名

func (*NamedRule) Parse

func (p *NamedRule) Parse() error

Parse implements Configurer

func (*NamedRule) Policy

func (p *NamedRule) Policy() ShardPolicy

Policy implements ShardRule

func (*NamedRule) Shard

func (p *NamedRule) Shard(val interface{}) (shardName string, err error)

Shard implements ShardRule.Shard

func (*NamedRule) ShardFieldName

func (p *NamedRule) ShardFieldName() string

ShardFieldName 用于分片的字段名

type NullTime

type NullTime = sql.NullTime

NullTime null time

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) Parse

func (p *NumRangeRule) Parse() error

Parse implements Configurer

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) Parse

func (p *OneRule) Parse() error

Parse implements Configurer

func (*OneRule) Policy

func (p *OneRule) Policy() ShardPolicy

Policy implements ShardPolicy.Policy

func (*OneRule) Shard

func (p *OneRule) Shard(val interface{}) (shardName string, err error)

Shard implements ShardPolicy.Shard

func (*OneRule) ShardFieldName

func (p *OneRule) ShardFieldName() string

ShardFieldName 用于分片的字段名

type Op

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

Op 数据库操作接口,与sql.DB对应,封装了事务等

func (*Op) BeginTx

func (p *Op) BeginTx() (err error)

BeginTx 开始事务,支持简单的嵌套调用,如果已经开始了事务,则直接返回成功

func (*Op) Commit

func (p *Op) Commit() error

Commit 提交事务

func (*Op) DB

func (p *Op) DB() *sql.DB

DB sql.DB

func (*Op) DoInTrans

func (p *Op) DoInTrans(peration OpTxFunc) (rt interface{}, err error)

DoInTrans 在事务中执行

func (*Op) IsRollbackOnly

func (p *Op) IsRollbackOnly() bool

IsRollbackOnly 是否只回滚

func (*Op) Pool

func (p *Op) Pool() *Pool

Pool pool

func (*Op) PoolName

func (p *Op) PoolName() string

PoolName name of pool

func (*Op) Rollback

func (p *Op) Rollback() error

Rollback 回滚事务

func (*Op) SetRollbackOnly

func (p *Op) SetRollbackOnly(rollback bool)

SetRollbackOnly 设置只回滚

func (*Op) SetupTableShard

func (p *Op) SetupTableShard(entity Entity, ruleName string) error

SetupTableShard use op pool setup entity table shard

type OpCreator

type OpCreator interface {
	//NewOp create a new Op
	NewOp() (*Op, error)
}

OpCreator Op

type OpTxFunc

type OpTxFunc func(tx *sql.Tx) (interface{}, error)

OpTxFunc 在事务中处理的函数

type Pool

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

Pool 数据库连接池

func NewMySQLDBPool

func NewMySQLDBPool(config *DBConfig) (*Pool, error)

NewMySQLDBPool build mysql db pool from config

func (*Pool) Name

func (p *Pool) Name() string

Name pool name

func (*Pool) NewOp

func (p *Pool) NewOp() *Op

NewOp 创建DBOper

type PoolFunc

type PoolFunc func(config *DBConfig) (pool *Pool, err error)

PoolFunc the func to crate db pool

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 ShardHandler

type ShardHandler func() (shardName string, err error)

ShardHandler 分片处理

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"
)

func (ShardPolicy) IsValid

func (p ShardPolicy) IsValid() bool

IsValid 是否有效

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

Jump to

Keyboard shortcuts

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