mirror of https://github.com/go-gorm/gorm.git
Add TableName with NamingStrategy support, close #5726
This commit is contained in:
parent
e8f48b5c15
commit
34fbe84580
|
@ -71,6 +71,10 @@ type Tabler interface {
|
|||
TableName() string
|
||||
}
|
||||
|
||||
type TablerWithNamer interface {
|
||||
TableName(Namer) string
|
||||
}
|
||||
|
||||
// Parse get data type from dialector
|
||||
func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) {
|
||||
return ParseWithSpecialTableName(dest, cacheStore, namer, "")
|
||||
|
@ -125,6 +129,9 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
|
|||
if tabler, ok := modelValue.Interface().(Tabler); ok {
|
||||
tableName = tabler.TableName()
|
||||
}
|
||||
if tabler, ok := modelValue.Interface().(TablerWithNamer); ok {
|
||||
tableName = tabler.TableName(namer)
|
||||
}
|
||||
if en, ok := namer.(embeddedNamer); ok {
|
||||
tableName = en.Table
|
||||
}
|
||||
|
|
12
tests/go.mod
12
tests/go.mod
|
@ -3,17 +3,15 @@ module gorm.io/gorm/tests
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/denisenkom/go-mssqldb v0.12.2 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jinzhu/now v1.1.5
|
||||
github.com/lib/pq v1.10.7
|
||||
github.com/mattn/go-sqlite3 v1.14.15 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
|
||||
gorm.io/driver/mysql v1.3.6
|
||||
gorm.io/driver/postgres v1.3.10
|
||||
gorm.io/driver/sqlite v1.3.6
|
||||
gorm.io/driver/sqlserver v1.3.2
|
||||
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
|
||||
gorm.io/driver/mysql v1.4.0
|
||||
gorm.io/driver/postgres v1.4.1
|
||||
gorm.io/driver/sqlite v1.4.1
|
||||
gorm.io/driver/sqlserver v1.4.0
|
||||
gorm.io/gorm v1.23.10
|
||||
)
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"testing"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
"gorm.io/gorm/utils/tests"
|
||||
. "gorm.io/gorm/utils/tests"
|
||||
)
|
||||
|
||||
|
@ -145,3 +147,27 @@ func TestTableWithAllFields(t *testing.T) {
|
|||
|
||||
AssertEqual(t, r.Statement.Vars, []interface{}{2, 4, 1, 3})
|
||||
}
|
||||
|
||||
type UserWithTableNamer struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
}
|
||||
|
||||
func (UserWithTableNamer) TableName(namer schema.Namer) string {
|
||||
return namer.TableName("user")
|
||||
}
|
||||
|
||||
func TestTableWithNamer(t *testing.T) {
|
||||
var db, _ = gorm.Open(tests.DummyDialector{}, &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: "t_",
|
||||
}})
|
||||
|
||||
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Model(&UserWithTableNamer{}).Find(&UserWithTableNamer{})
|
||||
})
|
||||
|
||||
if !regexp.MustCompile("SELECT \\* FROM `t_users`").MatchString(sql) {
|
||||
t.Errorf("Table with namer, got %v", sql)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package tests
|
|||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/callbacks"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
|
@ -13,7 +14,14 @@ func (DummyDialector) Name() string {
|
|||
return "dummy"
|
||||
}
|
||||
|
||||
func (DummyDialector) Initialize(*gorm.DB) error {
|
||||
func (DummyDialector) Initialize(db *gorm.DB) error {
|
||||
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
|
||||
CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"},
|
||||
UpdateClauses: []string{"UPDATE", "SET", "WHERE", "RETURNING"},
|
||||
DeleteClauses: []string{"DELETE", "FROM", "WHERE", "RETURNING"},
|
||||
LastInsertIDReversed: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue