@@ -536,46 +536,6 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
536
536
return nil
537
537
}
538
538
539
- func (engine * Engine ) tableName (beanOrTableName interface {}) (string , error ) {
540
- v := rValue (beanOrTableName )
541
- if v .Type ().Kind () == reflect .String {
542
- return beanOrTableName .(string ), nil
543
- } else if v .Type ().Kind () == reflect .Struct {
544
- return engine .tbName (v ), nil
545
- }
546
- return "" , errors .New ("bean should be a struct or struct's point" )
547
- }
548
-
549
- func (engine * Engine ) tbSchemaName (v string ) string {
550
- // Add schema name as prefix of table name.
551
- // Only for postgres database.
552
- if engine .dialect .DBType () == core .POSTGRES &&
553
- engine .dialect .URI ().Schema != "" &&
554
- engine .dialect .URI ().Schema != postgresPublicSchema &&
555
- strings .Index (v , "." ) == - 1 {
556
- return engine .dialect .URI ().Schema + "." + v
557
- }
558
- return v
559
- }
560
-
561
- func (engine * Engine ) tbName (v reflect.Value ) string {
562
- if tb , ok := v .Interface ().(TableName ); ok {
563
- return engine .tbSchemaName (tb .TableName ())
564
-
565
- }
566
-
567
- if v .Type ().Kind () == reflect .Ptr {
568
- if tb , ok := reflect .Indirect (v ).Interface ().(TableName ); ok {
569
- return engine .tbSchemaName (tb .TableName ())
570
- }
571
- } else if v .CanAddr () {
572
- if tb , ok := v .Addr ().Interface ().(TableName ); ok {
573
- return engine .tbSchemaName (tb .TableName ())
574
- }
575
- }
576
- return engine .tbSchemaName (engine .TableMapper .Obj2Table (reflect .Indirect (v ).Type ().Name ()))
577
- }
578
-
579
539
// Cascade use cascade or not
580
540
func (engine * Engine ) Cascade (trueOrFalse ... bool ) * Session {
581
541
session := engine .NewSession ()
@@ -859,7 +819,7 @@ func (engine *Engine) TableInfo(bean interface{}) *Table {
859
819
if err != nil {
860
820
engine .logger .Error (err )
861
821
}
862
- return & Table {tb , engine .tbName ( v )}
822
+ return & Table {tb , engine .TableName ( bean )}
863
823
}
864
824
865
825
func addIndex (indexName string , table * core.Table , col * core.Column , indexType int ) {
@@ -895,20 +855,8 @@ var (
895
855
func (engine * Engine ) mapType (v reflect.Value ) (* core.Table , error ) {
896
856
t := v .Type ()
897
857
table := engine .newTable ()
898
- if tb , ok := v .Interface ().(TableName ); ok {
899
- table .Name = tb .TableName ()
900
- } else {
901
- if v .CanAddr () {
902
- if tb , ok = v .Addr ().Interface ().(TableName ); ok {
903
- table .Name = tb .TableName ()
904
- }
905
- }
906
- if table .Name == "" {
907
- table .Name = engine .TableMapper .Obj2Table (t .Name ())
908
- }
909
- }
910
-
911
858
table .Type = t
859
+ table .Name = engine .tbNameForMap (v )
912
860
913
861
var idFieldColName string
914
862
var hasCacheTag , hasNoCacheTag bool
@@ -1186,7 +1134,7 @@ func (engine *Engine) ClearCacheBean(bean interface{}, id string) error {
1186
1134
if t .Kind () != reflect .Struct {
1187
1135
return errors .New ("error params" )
1188
1136
}
1189
- tableName := engine .tbName ( v )
1137
+ tableName := engine .TableName ( bean )
1190
1138
table , err := engine .autoMapType (v )
1191
1139
if err != nil {
1192
1140
return err
@@ -1210,7 +1158,7 @@ func (engine *Engine) ClearCache(beans ...interface{}) error {
1210
1158
if t .Kind () != reflect .Struct {
1211
1159
return errors .New ("error params" )
1212
1160
}
1213
- tableName := engine .tbName ( v )
1161
+ tableName := engine .TableName ( bean )
1214
1162
table , err := engine .autoMapType (v )
1215
1163
if err != nil {
1216
1164
return err
@@ -1237,13 +1185,13 @@ func (engine *Engine) Sync(beans ...interface{}) error {
1237
1185
1238
1186
for _ , bean := range beans {
1239
1187
v := rValue (bean )
1240
- tableName := engine .tbName ( v )
1188
+ tableNameNoSchema := engine .tbNameNoSchema ( v . Interface () )
1241
1189
table , err := engine .autoMapType (v )
1242
1190
if err != nil {
1243
1191
return err
1244
1192
}
1245
1193
1246
- isExist , err := session .Table (bean ).isTableExist (tableName )
1194
+ isExist , err := session .Table (bean ).isTableExist (tableNameNoSchema )
1247
1195
if err != nil {
1248
1196
return err
1249
1197
}
@@ -1269,12 +1217,12 @@ func (engine *Engine) Sync(beans ...interface{}) error {
1269
1217
}
1270
1218
} else {
1271
1219
for _ , col := range table .Columns () {
1272
- isExist , err := engine .dialect .IsColumnExist (tableName , col .Name )
1220
+ isExist , err := engine .dialect .IsColumnExist (tableNameNoSchema , col .Name )
1273
1221
if err != nil {
1274
1222
return err
1275
1223
}
1276
1224
if ! isExist {
1277
- if err := session .statement .setRefValue ( v ); err != nil {
1225
+ if err := session .statement .setRefBean ( bean ); err != nil {
1278
1226
return err
1279
1227
}
1280
1228
err = session .addColumn (col .Name )
@@ -1285,35 +1233,35 @@ func (engine *Engine) Sync(beans ...interface{}) error {
1285
1233
}
1286
1234
1287
1235
for name , index := range table .Indexes {
1288
- if err := session .statement .setRefValue ( v ); err != nil {
1236
+ if err := session .statement .setRefBean ( bean ); err != nil {
1289
1237
return err
1290
1238
}
1291
1239
if index .Type == core .UniqueType {
1292
- isExist , err := session .isIndexExist2 (tableName , index .Cols , true )
1240
+ isExist , err := session .isIndexExist2 (tableNameNoSchema , index .Cols , true )
1293
1241
if err != nil {
1294
1242
return err
1295
1243
}
1296
1244
if ! isExist {
1297
- if err := session .statement .setRefValue ( v ); err != nil {
1245
+ if err := session .statement .setRefBean ( bean ); err != nil {
1298
1246
return err
1299
1247
}
1300
1248
1301
- err = session .addUnique (tableName , name )
1249
+ err = session .addUnique (tableNameNoSchema , name )
1302
1250
if err != nil {
1303
1251
return err
1304
1252
}
1305
1253
}
1306
1254
} else if index .Type == core .IndexType {
1307
- isExist , err := session .isIndexExist2 (tableName , index .Cols , false )
1255
+ isExist , err := session .isIndexExist2 (tableNameNoSchema , index .Cols , false )
1308
1256
if err != nil {
1309
1257
return err
1310
1258
}
1311
1259
if ! isExist {
1312
- if err := session .statement .setRefValue ( v ); err != nil {
1260
+ if err := session .statement .setRefBean ( bean ); err != nil {
1313
1261
return err
1314
1262
}
1315
1263
1316
- err = session .addIndex (tableName , name )
1264
+ err = session .addIndex (tableNameNoSchema , name )
1317
1265
if err != nil {
1318
1266
return err
1319
1267
}
@@ -1649,6 +1597,11 @@ func (engine *Engine) SetTZDatabase(tz *time.Location) {
1649
1597
engine .DatabaseTZ = tz
1650
1598
}
1651
1599
1600
+ // SetSchema sets the schema of database
1601
+ func (engine * Engine ) SetSchema (schema string ) {
1602
+ engine .dialect .URI ().Schema = schema
1603
+ }
1604
+
1652
1605
// Unscoped always disable struct tag "deleted"
1653
1606
func (engine * Engine ) Unscoped () * Session {
1654
1607
session := engine .NewSession ()
0 commit comments