Skip to content

Commit

Permalink
Add TableName with NamingStrategy support, close #5726
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 7, 2022
1 parent e8f48b5 commit 34fbe84
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
7 changes: 7 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 5 additions & 7 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
26 changes: 26 additions & 0 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
. "gorm.io/gorm/utils/tests"
)

Expand Down Expand Up @@ -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)
}
}
10 changes: 9 additions & 1 deletion utils/tests/dummy_dialecter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down

0 comments on commit 34fbe84

Please sign in to comment.