feat: Add table name separator configuration function to support multi-level database table names
This commit is contained in:
parent
49b01a3e93
commit
acbe3e3672
17
gorm.go
17
gorm.go
@ -17,6 +17,9 @@ import (
|
||||
// for Config.cacheStore store PreparedStmtDB key
|
||||
const preparedStmtDBKey = "preparedStmt"
|
||||
|
||||
// DefaultTableNameSplit default table name split
|
||||
var DefaultTableNameSplit = 2
|
||||
|
||||
// Config GORM config
|
||||
type Config struct {
|
||||
// GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
|
||||
@ -73,6 +76,11 @@ type Config struct {
|
||||
cacheStore *sync.Map
|
||||
}
|
||||
|
||||
// SetTableNameSplit set default table name split
|
||||
func SetTableNameSplit(split int) {
|
||||
DefaultTableNameSplit = split
|
||||
}
|
||||
|
||||
// Apply update config to new config
|
||||
func (c *Config) Apply(config *Config) error {
|
||||
if config != c {
|
||||
@ -218,10 +226,11 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
|
||||
}
|
||||
|
||||
db.Statement = &Statement{
|
||||
DB: db,
|
||||
ConnPool: db.ConnPool,
|
||||
Context: context.Background(),
|
||||
Clauses: map[string]clause.Clause{},
|
||||
DB: db,
|
||||
ConnPool: db.ConnPool,
|
||||
Context: context.Background(),
|
||||
Clauses: map[string]clause.Clause{},
|
||||
TableNameSplit: DefaultTableNameSplit,
|
||||
}
|
||||
|
||||
if err == nil && !config.DisableAutomaticPing {
|
||||
|
@ -48,6 +48,7 @@ type Statement struct {
|
||||
assigns []interface{}
|
||||
scopes []func(*DB) *DB
|
||||
Result *result
|
||||
TableNameSplit int
|
||||
}
|
||||
|
||||
type join struct {
|
||||
@ -503,9 +504,9 @@ func (stmt *Statement) Parse(value interface{}) (err error) {
|
||||
|
||||
func (stmt *Statement) ParseWithSpecialTableName(value interface{}, specialTableName string) (err error) {
|
||||
if stmt.Schema, err = schema.ParseWithSpecialTableName(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, specialTableName); err == nil && stmt.Table == "" {
|
||||
if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == 2 {
|
||||
if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == stmt.TableNameSplit {
|
||||
stmt.TableExpr = &clause.Expr{SQL: stmt.Quote(stmt.Schema.Table)}
|
||||
stmt.Table = tables[1]
|
||||
stmt.Table = tables[stmt.TableNameSplit-1]
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user