Documentation
¶
Index ¶
- Constants
- Variables
- type Config
- type ConnPool
- func (pool *ConnPool) BeginTx(ctx context.Context, opt *sql.TxOptions) (gorm.ConnPool, error)
- func (pool *ConnPool) Commit() error
- func (pool ConnPool) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (pool *ConnPool) Ping() error
- func (pool ConnPool) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (pool ConnPool) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (pool ConnPool) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (pool *ConnPool) Rollback() error
- func (pool *ConnPool) String() string
- type Sharding
- type ShardingDialector
- type ShardingMigrator
Constants ¶
View Source
const ( // Use Snowflake primary key generator PKSnowflake = iota // Use PostgreSQL sequence primary key generator PKPGSequence // Use MySQL sequence primary key generator PKMySQLSequence // Use custom primary key generator PKCustom )
Variables ¶
View Source
var ( ErrMissingShardingKey = errors.New("sharding key or id required, and use operator =") ErrInvalidID = errors.New("invalid id format") ErrInsertDiffSuffix = errors.New("can not insert different suffix table in one query ") )
View Source
var (
ShardingIgnoreStoreKey = "sharding_ignore"
)
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// When DoubleWrite enabled, data will double write to both main table and sharding table.
DoubleWrite bool
// ShardingKey specifies the table column you want to used for sharding the table rows.
// For example, for a product order table, you may want to split the rows by `user_id`.
ShardingKey string
// NumberOfShards specifies how many tables you want to sharding.
NumberOfShards uint
// ShardingAlgorithm specifies a function to generate the sharding
// table's suffix by the column value.
// For example, this function implements a mod sharding algorithm.
//
// func(value any) (suffix string, err error) {
// if uid, ok := value.(int64);ok {
// return fmt.Sprintf("_%02d", user_id % 64), nil
// }
// return "", errors.New("invalid user_id")
// }
ShardingAlgorithm func(columnValue any) (suffix string, err error)
// ShardingSuffixs specifies a function to generate all table's suffix.
// Used to support Migrator and generate PrimaryKey.
// For example, this function get a mod all sharding suffixs.
//
// func () (suffixs []string) {
// numberOfShards := 5
// for i := 0; i < numberOfShards; i++ {
// suffixs = append(suffixs, fmt.Sprintf("_%02d", i%numberOfShards))
// }
// return
// }
ShardingSuffixs func() (suffixs []string)
// ShardingAlgorithmByPrimaryKey specifies a function to generate the sharding
// table's suffix by the primary key. Used when no sharding key specified.
// For example, this function use the Snowflake library to generate the suffix.
//
// func(id int64) (suffix string) {
// return fmt.Sprintf("_%02d", snowflake.ParseInt64(id).Node())
// }
ShardingAlgorithmByPrimaryKey func(id int64) (suffix string)
// PrimaryKeyGenerator specifies the primary key generate algorithm.
// Used only when insert and the record does not contains an id field.
// Options are PKSnowflake, PKPGSequence and PKCustom.
// When use PKCustom, you should also specify PrimaryKeyGeneratorFn.
PrimaryKeyGenerator int
// PrimaryKeyGeneratorFn specifies a function to generate the primary key.
// When use auto-increment like generator, the tableIdx argument could ignored.
// For example, this function use the Snowflake library to generate the primary key.
// If you don't want to auto-fill the `id` or use a primary key that isn't called `id`, just return 0.
//
// func(tableIdx int64) int64 {
// return nodes[tableIdx].Generate().Int64()
// }
PrimaryKeyGeneratorFn func(tableIdx int64) int64
// contains filtered or unexported fields
}
Config specifies the configuration for sharding.
type ConnPool ¶
ConnPool Implement a ConnPool for replace db.Statement.ConnPool in Gorm
func (ConnPool) ExecContext ¶
func (ConnPool) PrepareContext ¶
func (ConnPool) QueryContext ¶
func (pool ConnPool) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
https://github.com/go-gorm/gorm/blob/v1.21.11/callbacks/query.go#L18
func (ConnPool) QueryRowContext ¶
type Sharding ¶
func RegisterMoreToMore ¶
RegisterMoreToMore 每个表注册单独的分片策略
func (*Sharding) Initialize ¶
Initialize implement for Gorm plugin interface
type ShardingDialector ¶
func NewShardingDialector ¶
func NewShardingDialector(d gorm.Dialector, s *Sharding) ShardingDialector
type ShardingMigrator ¶
func (ShardingMigrator) AutoMigrate ¶
func (m ShardingMigrator) AutoMigrate(dst ...any) error
func (ShardingMigrator) BuildIndexOptions ¶
func (m ShardingMigrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})
BuildIndexOptions build index options
func (ShardingMigrator) DropTable ¶
func (m ShardingMigrator) DropTable(dst ...any) error
Click to show internal directories.
Click to hide internal directories.