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:
parent
9242b921d8
commit
468154dfd5
21
engine.go
21
engine.go
|
@ -546,21 +546,34 @@ func (engine *Engine) tableName(beanOrTableName interface{}) (string, error) {
|
||||||
return "", errors.New("bean should be a struct or struct's point")
|
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 {
|
func (engine *Engine) tbName(v reflect.Value) string {
|
||||||
if tb, ok := v.Interface().(TableName); ok {
|
if tb, ok := v.Interface().(TableName); ok {
|
||||||
return tb.TableName()
|
return engine.tbSchemaName(tb.TableName())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.Type().Kind() == reflect.Ptr {
|
if v.Type().Kind() == reflect.Ptr {
|
||||||
if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
|
if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
|
||||||
return tb.TableName()
|
return engine.tbSchemaName(tb.TableName())
|
||||||
}
|
}
|
||||||
} else if v.CanAddr() {
|
} else if v.CanAddr() {
|
||||||
if tb, ok := v.Addr().Interface().(TableName); ok {
|
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
|
// Cascade use cascade or not
|
||||||
|
|
Loading…
Reference in New Issue