Add prefix support
This commit is contained in:
		
							parent
							
								
									b507cdf93d
								
							
						
					
					
						commit
						f57d7f2d9b
					
				
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							@ -414,6 +414,12 @@ func (s *DB) Table(name string) *DB {
 | 
				
			|||||||
	return clone
 | 
						return clone
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *DB) Prefix(name string) *DB {
 | 
				
			||||||
 | 
						clone := s.clone()
 | 
				
			||||||
 | 
						clone.search.Prefix(name)
 | 
				
			||||||
 | 
						return clone
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Debug start debug mode
 | 
					// Debug start debug mode
 | 
				
			||||||
func (s *DB) Debug() *DB {
 | 
					func (s *DB) Debug() *DB {
 | 
				
			||||||
	return s.clone().LogMode(true)
 | 
						return s.clone().LogMode(true)
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										12
									
								
								main_test.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								main_test.go
									
									
									
									
									
								
							@ -247,7 +247,19 @@ func TestTableName(t *testing.T) {
 | 
				
			|||||||
	if DB.NewScope([]Cart{}).TableName() != "shopping_cart" {
 | 
						if DB.NewScope([]Cart{}).TableName() != "shopping_cart" {
 | 
				
			||||||
		t.Errorf("[]Cart's singular table name should be 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)
 | 
						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) {
 | 
					func TestNullValues(t *testing.T) {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										18
									
								
								scope.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								scope.go
									
									
									
									
									
								
							@ -301,17 +301,29 @@ type dbTabler interface {
 | 
				
			|||||||
// TableName return table name
 | 
					// TableName return table name
 | 
				
			||||||
func (scope *Scope) TableName() string {
 | 
					func (scope *Scope) TableName() string {
 | 
				
			||||||
	if scope.Search != nil && len(scope.Search.tableName) > 0 {
 | 
						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 tabler, ok := scope.Value.(tabler); ok {
 | 
				
			||||||
 | 
							if scope.Search != nil {
 | 
				
			||||||
 | 
								return scope.Search.prefix + tabler.TableName()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return tabler.TableName()
 | 
							return tabler.TableName()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if tabler, ok := scope.Value.(dbTabler); ok {
 | 
						if tabler, ok := scope.Value.(dbTabler); ok {
 | 
				
			||||||
 | 
							if scope.Search != nil {
 | 
				
			||||||
 | 
								return scope.Search.prefix + tabler.TableName(scope.db)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return 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))
 | 
						return scope.GetModelStruct().TableName(scope.db.Model(scope.Value))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -319,9 +331,9 @@ func (scope *Scope) TableName() string {
 | 
				
			|||||||
func (scope *Scope) QuotedTableName() (name string) {
 | 
					func (scope *Scope) QuotedTableName() (name string) {
 | 
				
			||||||
	if scope.Search != nil && len(scope.Search.tableName) > 0 {
 | 
						if scope.Search != nil && len(scope.Search.tableName) > 0 {
 | 
				
			||||||
		if strings.Index(scope.Search.tableName, " ") != -1 {
 | 
							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())
 | 
						return scope.Quote(scope.TableName())
 | 
				
			||||||
 | 
				
			|||||||
@ -19,6 +19,7 @@ type search struct {
 | 
				
			|||||||
	limit            interface{}
 | 
						limit            interface{}
 | 
				
			||||||
	group            string
 | 
						group            string
 | 
				
			||||||
	tableName        string
 | 
						tableName        string
 | 
				
			||||||
 | 
						prefix           string
 | 
				
			||||||
	raw              bool
 | 
						raw              bool
 | 
				
			||||||
	Unscoped         bool
 | 
						Unscoped         bool
 | 
				
			||||||
	countingQuery    bool
 | 
						countingQuery    bool
 | 
				
			||||||
@ -132,6 +133,11 @@ func (s *search) Table(name string) *search {
 | 
				
			|||||||
	return s
 | 
						return s
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *search) Prefix(name string) *search {
 | 
				
			||||||
 | 
						s.prefix = name
 | 
				
			||||||
 | 
						return s
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *search) getInterfaceAsSQL(value interface{}) (str string) {
 | 
					func (s *search) getInterfaceAsSQL(value interface{}) (str string) {
 | 
				
			||||||
	switch value.(type) {
 | 
						switch value.(type) {
 | 
				
			||||||
	case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
 | 
						case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user