From 34fbe84580290c32ba006b714669bb356224cb07 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 7 Oct 2022 21:18:37 +0800 Subject: [PATCH] Add TableName with NamingStrategy support, close #5726 --- schema/schema.go | 7 +++++++ tests/go.mod | 12 +++++------- tests/table_test.go | 26 ++++++++++++++++++++++++++ utils/tests/dummy_dialecter.go | 10 +++++++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index 42ff5c45..9b3d30f6 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -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 } diff --git a/tests/go.mod b/tests/go.mod index c1e1e0ce..d28c4bb9 100644 --- a/tests/go.mod +++ b/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 ) diff --git a/tests/table_test.go b/tests/table_test.go index 0289b7b8..f538c691 100644 --- a/tests/table_test.go +++ b/tests/table_test.go @@ -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) + } +} diff --git a/utils/tests/dummy_dialecter.go b/utils/tests/dummy_dialecter.go index 2990c20f..c89b944a 100644 --- a/utils/tests/dummy_dialecter.go +++ b/utils/tests/dummy_dialecter.go @@ -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 }