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