chore(postgres): support add schema as prefix of table name (#875)

* chore(postgres): support add schema as prefix of table name

* fix: ignore DefaultPostgresSchema

* docs: [ci skip] add desc for postgres.
This commit is contained in:
Bo-Yi Wu 2018-04-08 23:49:59 +08:00 committed by Lunny Xiao
parent 9242b921d8
commit 468154dfd5
1 changed files with 17 additions and 4 deletions

View File

@ -546,21 +546,34 @@ func (engine *Engine) tableName(beanOrTableName interface{}) (string, error) {
return "", errors.New("bean should be a struct or struct's point")
}
func (engine *Engine) tbSchemaName(v string) string {
// Add schema name as prefix of table name.
// Only for postgres database.
if engine.dialect.DBType() == core.POSTGRES &&
engine.dialect.URI().Schema != "" &&
engine.dialect.URI().Schema != DefaultPostgresSchema &&
strings.Index(v, ".") == -1 {
return engine.dialect.URI().Schema + "." + v
}
return v
}
func (engine *Engine) tbName(v reflect.Value) string {
if tb, ok := v.Interface().(TableName); ok {
return tb.TableName()
return engine.tbSchemaName(tb.TableName())
}
if v.Type().Kind() == reflect.Ptr {
if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
return tb.TableName()
return engine.tbSchemaName(tb.TableName())
}
} else if v.CanAddr() {
if tb, ok := v.Addr().Interface().(TableName); ok {
return tb.TableName()
return engine.tbSchemaName(tb.TableName())
}
}
return engine.TableMapper.Obj2Table(reflect.Indirect(v).Type().Name())
return engine.tbSchemaName(engine.TableMapper.Obj2Table(reflect.Indirect(v).Type().Name()))
}
// Cascade use cascade or not