Browse Source

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

xormplus 7 years ago
parent
commit
f4d7071c60
1 changed files with 17 additions and 4 deletions
  1. 17 4
      engine.go

+ 17 - 4
engine.go

@@ -555,21 +555,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