mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
sharding feature develop
This commit is contained in:
@ -17,38 +17,39 @@ import (
|
||||
|
||||
// Model is core struct implementing the DAO for ORM.
|
||||
type Model struct {
|
||||
db DB // Underlying DB interface.
|
||||
tx *TX // Underlying TX interface.
|
||||
rawSql string // rawSql is the raw SQL string which marks a raw SQL based Model not a table based Model.
|
||||
schema string // Custom database schema.
|
||||
linkType int // Mark for operation on master or slave.
|
||||
tablesInit string // Table names when model initialization.
|
||||
tables string // Operation table names, which can be more than one table names and aliases, like: "user", "user u", "user u, user_detail ud".
|
||||
fields string // Operation fields, multiple fields joined using char ','.
|
||||
fieldsEx string // Excluded operation fields, multiple fields joined using char ','.
|
||||
withArray []interface{} // Arguments for With feature.
|
||||
withAll bool // Enable model association operations on all objects that have "with" tag in the struct.
|
||||
extraArgs []interface{} // Extra custom arguments for sql, which are prepended to the arguments before sql committed to underlying driver.
|
||||
whereHolder []ModelWhereHolder // Condition strings for where operation.
|
||||
groupBy string // Used for "group by" statement.
|
||||
orderBy string // Used for "order by" statement.
|
||||
having []interface{} // Used for "having..." statement.
|
||||
start int // Used for "select ... start, limit ..." statement.
|
||||
limit int // Used for "select ... start, limit ..." statement.
|
||||
option int // Option for extra operation features.
|
||||
offset int // Offset statement for some databases grammar.
|
||||
data interface{} // Data for operation, which can be type of map/[]map/struct/*struct/string, etc.
|
||||
batch int // Batch number for batch Insert/Replace/Save operations.
|
||||
filter bool // Filter data and where key-value pairs according to the fields of the table.
|
||||
distinct string // Force the query to only return distinct results.
|
||||
lockInfo string // Lock for update or in shared lock.
|
||||
cacheEnabled bool // Enable sql result cache feature, which is mainly for indicating cache duration(especially 0) usage.
|
||||
cacheOption CacheOption // Cache option for query statement.
|
||||
hook HookHandler // Hook functions for model hook feature.
|
||||
unscoped bool // Disables soft deleting features when select/delete operations.
|
||||
safe bool // If true, it clones and returns a new model object whenever operation done; or else it changes the attribute of current model.
|
||||
onDuplicate interface{} // onDuplicate is used for ON "DUPLICATE KEY UPDATE" statement.
|
||||
onDuplicateEx interface{} // onDuplicateEx is used for excluding some columns ON "DUPLICATE KEY UPDATE" statement.
|
||||
db DB // Underlying DB interface.
|
||||
tx *TX // Underlying TX interface.
|
||||
rawSql string // rawSql is the raw SQL string which marks a raw SQL based Model not a table based Model.
|
||||
schema string // Custom database schema.
|
||||
linkType int // Mark for operation on master or slave.
|
||||
tablesInit string // Table names when model initialization.
|
||||
tables string // Operation table names, which can be more than one table names and aliases, like: "user", "user u", "user u, user_detail ud".
|
||||
fields string // Operation fields, multiple fields joined using char ','.
|
||||
fieldsEx string // Excluded operation fields, multiple fields joined using char ','.
|
||||
withArray []interface{} // Arguments for With feature.
|
||||
withAll bool // Enable model association operations on all objects that have "with" tag in the struct.
|
||||
extraArgs []interface{} // Extra custom arguments for sql, which are prepended to the arguments before sql committed to underlying driver.
|
||||
whereHolder []ModelWhereHolder // Condition strings for where operation.
|
||||
groupBy string // Used for "group by" statement.
|
||||
orderBy string // Used for "order by" statement.
|
||||
having []interface{} // Used for "having..." statement.
|
||||
start int // Used for "select ... start, limit ..." statement.
|
||||
limit int // Used for "select ... start, limit ..." statement.
|
||||
option int // Option for extra operation features.
|
||||
offset int // Offset statement for some databases grammar.
|
||||
data interface{} // Data for operation, which can be type of map/[]map/struct/*struct/string, etc.
|
||||
batch int // Batch number for batch Insert/Replace/Save operations.
|
||||
filter bool // Filter data and where key-value pairs according to the fields of the table.
|
||||
distinct string // Force the query to only return distinct results.
|
||||
lockInfo string // Lock for update or in shared lock.
|
||||
cacheEnabled bool // Enable sql result cache feature, which is mainly for indicating cache duration(especially 0) usage.
|
||||
cacheOption CacheOption // Cache option for query statement.
|
||||
hookHandler HookHandler // Hook functions for model hook feature.
|
||||
shardingHandler ShardingHandler // Custom sharding handler for sharding feature.
|
||||
unscoped bool // Disables soft deleting features when select/delete operations.
|
||||
safe bool // If true, it clones and returns a new model object whenever operation done; or else it changes the attribute of current model.
|
||||
onDuplicate interface{} // onDuplicate is used for ON "DUPLICATE KEY UPDATE" statement.
|
||||
onDuplicateEx interface{} // onDuplicateEx is used for excluding some columns ON "DUPLICATE KEY UPDATE" statement.
|
||||
}
|
||||
|
||||
// ModelHandler is a function that handles given Model and returns a new Model that is custom modified.
|
||||
|
||||
@ -40,7 +40,7 @@ func (m *Model) Delete(where ...interface{}) (result sql.Result, err error) {
|
||||
db: m.db,
|
||||
link: m.getLink(true),
|
||||
},
|
||||
handler: m.hook.Update,
|
||||
handler: m.hookHandler.Update,
|
||||
},
|
||||
Table: m.tables,
|
||||
Data: fmt.Sprintf(`%s=?`, m.db.GetCore().QuoteString(fieldNameDelete)),
|
||||
@ -63,7 +63,7 @@ func (m *Model) Delete(where ...interface{}) (result sql.Result, err error) {
|
||||
db: m.db,
|
||||
link: m.getLink(true),
|
||||
},
|
||||
handler: m.hook.Delete,
|
||||
handler: m.hookHandler.Delete,
|
||||
},
|
||||
Table: m.tables,
|
||||
Condition: conditionStr,
|
||||
|
||||
@ -143,6 +143,6 @@ func (h *HookDeleteInput) Next(ctx context.Context) (result sql.Result, err erro
|
||||
// Hook sets the hook functions for current model.
|
||||
func (m *Model) Hook(hook HookHandler) *Model {
|
||||
model := m.getModel()
|
||||
model.hook = hook
|
||||
model.hookHandler = hook
|
||||
return model
|
||||
}
|
||||
|
||||
@ -317,7 +317,7 @@ func (m *Model) doInsertWithOption(insertOption int) (result sql.Result, err err
|
||||
db: m.db,
|
||||
link: m.getLink(true),
|
||||
},
|
||||
handler: m.hook.Insert,
|
||||
handler: m.hookHandler.Insert,
|
||||
},
|
||||
Table: m.tables,
|
||||
Data: list,
|
||||
|
||||
@ -539,7 +539,7 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
|
||||
db: m.db,
|
||||
link: m.getLink(false),
|
||||
},
|
||||
handler: m.hook.Select,
|
||||
handler: m.hookHandler.Select,
|
||||
},
|
||||
Table: m.tables,
|
||||
Sql: sql,
|
||||
|
||||
80
database/gdb/gdb_model_sharding.go
Normal file
80
database/gdb/gdb_model_sharding.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// ShardingInput is input parameters for custom sharding handler.
|
||||
type ShardingInput struct {
|
||||
Table string // Current operation table name.
|
||||
Schema string // Current operation schema, usually empty string which means uses default schema from configuration.
|
||||
Data map[string]Value // Accurate key-value pairs from SELECT/INSERT/UPDATE/DELETE statement.
|
||||
}
|
||||
|
||||
// ShardingOutput is output parameters for custom sharding handler.
|
||||
type ShardingOutput struct {
|
||||
Table string // New table name for current operation. Use empty string for no changes of table name.
|
||||
Schema string // New schema name for current operation. Use empty string for using default schema from configuration.
|
||||
}
|
||||
|
||||
type ShardingHandler func(ctx context.Context, in ShardingInput) (out *ShardingOutput, err error)
|
||||
|
||||
type callShardingHandlerInput struct {
|
||||
Table string
|
||||
InsertData List
|
||||
UpdateData interface{}
|
||||
Condition string
|
||||
Sql string
|
||||
}
|
||||
|
||||
func (m *Model) callShardingHandler(ctx context.Context, in callShardingHandlerInput) (out *ShardingOutput, err error) {
|
||||
if m.shardingHandler == nil {
|
||||
return &ShardingOutput{}, nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Model) shardingDataFromInsertData(data List) (shardingData map[string]Value, err error) {
|
||||
if len(data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
shardingData = make(map[string]Value)
|
||||
// If given batch data(in batch insert scenario), it uses the first data.
|
||||
for k, v := range data[0] {
|
||||
shardingData[k] = gvar.New(v)
|
||||
}
|
||||
return shardingData, nil
|
||||
}
|
||||
|
||||
func (m *Model) shardingDataFromUpdateData(data interface{}) (shardingData map[string]Value, err error) {
|
||||
shardingData = make(map[string]Value)
|
||||
switch value := data.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range value {
|
||||
shardingData[k] = gvar.New(v)
|
||||
}
|
||||
case string:
|
||||
|
||||
default:
|
||||
return nil, gerror.Newf(`unsupported data of type "%s" for sharding`, reflect.TypeOf(data))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Model) shardingDataFromSql(sql string, args []interface{}) (shardingData map[string]Value, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Model) shardingDataFromCondition(condition string) (shardingData map[string]Value, err error) {
|
||||
return
|
||||
}
|
||||
@ -82,7 +82,7 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
|
||||
db: m.db,
|
||||
link: m.getLink(true),
|
||||
},
|
||||
handler: m.hook.Update,
|
||||
handler: m.hookHandler.Update,
|
||||
},
|
||||
Table: m.tables,
|
||||
Data: newData,
|
||||
|
||||
Reference in New Issue
Block a user