Merge f57d7f2d9ba42c25a963de34496c0513664b9ab9 into 9acaa33324bbcc78239a1c913d4f1292c12177b9

This commit is contained in:
Vladislav Fursov 2017-05-27 20:05:10 +00:00 committed by GitHub
commit b76bdceea6
4 changed files with 39 additions and 3 deletions

View File

@ -446,6 +446,12 @@ func (s *DB) Table(name string) *DB {
return clone
}
func (s *DB) Prefix(name string) *DB {
clone := s.clone()
clone.search.Prefix(name)
return clone
}
// Debug start debug mode
func (s *DB) Debug() *DB {
return s.clone().LogMode(true)

View File

@ -259,7 +259,19 @@ func TestTableName(t *testing.T) {
if DB.NewScope([]Cart{}).TableName() != "shopping_cart" {
t.Errorf("[]Cart's singular table name should be shopping_cart")
}
// Test prefix
if DB.Prefix("my_prefix_").NewScope(Order{}).TableName() != "my_prefix_order" {
t.Errorf("Order's table name should be my_prefix_order")
}
DB.SingularTable(false)
// Test prefix
if DB.Prefix("my_prefix_").NewScope(Order{}).TableName() != "my_prefix_orders" {
t.Errorf("Order's table name should be my_prefix_orders")
}
}
func TestNullValues(t *testing.T) {

View File

@ -301,17 +301,29 @@ type dbTabler interface {
// TableName return table name
func (scope *Scope) TableName() string {
if scope.Search != nil && len(scope.Search.tableName) > 0 {
return scope.Search.tableName
return scope.Search.prefix + scope.Search.tableName
}
if tabler, ok := scope.Value.(tabler); ok {
if scope.Search != nil {
return scope.Search.prefix + tabler.TableName()
}
return tabler.TableName()
}
if tabler, ok := scope.Value.(dbTabler); ok {
if scope.Search != nil {
return scope.Search.prefix + tabler.TableName(scope.db)
}
return tabler.TableName(scope.db)
}
if scope.Search != nil {
return scope.Search.prefix + scope.GetModelStruct().TableName(scope.db.Model(scope.Value))
}
return scope.GetModelStruct().TableName(scope.db.Model(scope.Value))
}
@ -319,9 +331,9 @@ func (scope *Scope) TableName() string {
func (scope *Scope) QuotedTableName() (name string) {
if scope.Search != nil && len(scope.Search.tableName) > 0 {
if strings.Index(scope.Search.tableName, " ") != -1 {
return scope.Search.tableName
return scope.Search.prefix + scope.Search.tableName
}
return scope.Quote(scope.Search.tableName)
return scope.Quote(scope.Search.prefix + scope.Search.tableName)
}
return scope.Quote(scope.TableName())

View File

@ -22,6 +22,7 @@ type search struct {
limit interface{}
group string
tableName string
prefix string
raw bool
Unscoped bool
ignoreOrderQuery bool
@ -141,6 +142,11 @@ func (s *search) Table(name string) *search {
return s
}
func (s *search) Prefix(name string) *search {
s.prefix = name
return s
}
func (s *search) getInterfaceAsSQL(value interface{}) (str string) {
switch value.(type) {
case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: