@@ -13,21 +13,23 @@ type Client interface {
1313 Create (value interface {}) (int64 , error )
1414 Delete (value interface {}, where ... interface {}) (int64 , error )
1515 Save (value interface {}) (int64 , error )
16- Update (attrs ... interface {}) (int64 , error )
16+ Update (column string , value interface {}) (int64 , error )
1717 First (out interface {}, where ... interface {}) error
1818 Find (out interface {}, where ... interface {}) error
1919 AutoMigrate (values ... interface {}) error
2020 Begin () Client
2121 Rollback () error
2222 Commit () error
2323 HasTable (value interface {}) bool
24- AddUniqueIndex (indexName string , columns ... string ) ( Client , error )
25- RemoveIndex (indexName string ) ( Client , error )
24+ AddUniqueIndex (indexName string , columns interface {}) error
25+ RemoveIndex (indexName string , columns interface {}) error
2626 Model (value interface {}) Client
2727 Exec (query string , args ... interface {}) int64
28+ ExecWithError (query string , args ... interface {}) error
2829 Rows (tableName string ) (* sql.Rows , error )
2930 DropColumn (column string ) error
30- Dialect () gorm.Dialect
31+ Dialect () gorm.Dialector
32+ Migrator () gorm.Migrator
3133}
3234
3335type gormClient struct {
@@ -38,29 +40,25 @@ func NewGormClient(db *gorm.DB) Client {
3840 return & gormClient {db : db }
3941}
4042func (c * gormClient ) DropColumn (name string ) error {
41- return c .DropColumn ( name ). Error ( )
43+ return c .db . Migrator (). DropColumn ( c . db . Statement . Table , name )
4244}
4345func (c * gormClient ) Close () error {
44- return c .db .Close ()
45- }
46- func (c * gormClient ) AddUniqueIndex (indexName string , columns ... string ) (Client , error ) {
47- var newClient gormClient
48- newClient .db , err := c .AddUniqueIndex (indexName , columns ... )
46+ sqlDB , err := c .db .DB ()
4947 if err != nil {
50-
48+ return err
5149 }
52-
53- return & newClient , newClient .db .Error
50+ return sqlDB .Close ()
51+ }
52+ func (c * gormClient ) AddUniqueIndex (indexName string , columns interface {}) error {
53+ return c .db .Migrator ().CreateIndex (columns , indexName )
5454}
5555
56- func (c * gormClient ) Dialect () gorm.Dialect {
57- return c .db .Dialect ()
56+ func (c * gormClient ) Dialect () gorm.Dialector {
57+ return c .db .Dialector
5858}
5959
60- func (c * gormClient ) RemoveIndex (indexName string ) (Client , error ) {
61- var newClient gormClient
62- newClient .db = c .db .RemoveIndex (indexName )
63- return & newClient , newClient .db .Error
60+ func (c * gormClient ) RemoveIndex (indexName string , columns interface {}) error {
61+ return c .db .Migrator ().DropIndex (columns , indexName )
6462}
6563
6664func (c * gormClient ) Model (value interface {}) Client {
@@ -89,8 +87,8 @@ func (c *gormClient) Save(value interface{}) (int64, error) {
8987 return newDb .RowsAffected , newDb .Error
9088}
9189
92- func (c * gormClient ) Update (attrs ... interface {}) (int64 , error ) {
93- newDb := c .db .Update (attrs ... )
90+ func (c * gormClient ) Update (column string , value interface {}) (int64 , error ) {
91+ newDb := c .db .Update (column , value )
9492 return newDb .RowsAffected , newDb .Error
9593}
9694
@@ -103,7 +101,7 @@ func (c *gormClient) Find(out interface{}, where ...interface{}) error {
103101}
104102
105103func (c * gormClient ) AutoMigrate (values ... interface {}) error {
106- return c .db .AutoMigrate (values ... ). Error
104+ return c .db .AutoMigrate (values ... )
107105}
108106
109107func (c * gormClient ) Begin () Client {
@@ -121,14 +119,22 @@ func (c *gormClient) Commit() error {
121119}
122120
123121func (c * gormClient ) HasTable (value interface {}) bool {
124- return c .db .HasTable (value )
122+ return c .db .Migrator (). HasTable (value )
125123}
126124
127125func (c * gormClient ) Exec (query string , args ... interface {}) int64 {
128- dbClient := c .db .Exec (query , args )
126+ dbClient := c .db .Exec (query , args ... )
129127 return dbClient .RowsAffected
130128}
131129
130+ func (c * gormClient ) ExecWithError (query string , args ... interface {}) error {
131+ return c .db .Exec (query , args ... ).Error
132+ }
133+
134+ func (c * gormClient ) Migrator () gorm.Migrator {
135+ return c .db .Migrator ()
136+ }
137+
132138func (c * gormClient ) Rows (tablename string ) (* sql.Rows , error ) {
133139 tableDb := c .db .Table (tablename )
134140 return tableDb .Rows ()
0 commit comments