-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathclient.go
122 lines (102 loc) · 3.22 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package db
import (
"database/sql"
"gorm.io/gorm"
)
//go:generate counterfeiter -o fakes/fake_client.go . Client
type Client interface {
Close() error
Where(query interface{}, args ...interface{}) Client
Create(value interface{}) (int64, error)
Delete(value interface{}, where ...interface{}) (int64, error)
Save(value interface{}) (int64, error)
Update(column string, value interface{}) (int64, error)
First(out interface{}, where ...interface{}) error
Find(out interface{}, where ...interface{}) error
AutoMigrate(values ...interface{}) error
Begin() Client
Rollback() error
Commit() error
HasTable(value interface{}) bool
AddUniqueIndex(indexName string, columns interface{}) error
RemoveIndex(indexName string, columns interface{}) error
Model(value interface{}) Client
Exec(query string, args ...interface{}) int64
Rows(tableName string) (*sql.Rows, error)
DropColumn(column string) error
}
type gormClient struct {
db *gorm.DB
}
func NewGormClient(db *gorm.DB) Client {
return &gormClient{db: db}
}
func (c *gormClient) DropColumn(name string) error {
return c.DropColumn(name)
}
func (c *gormClient) Close() error {
return c.Close()
}
func (c *gormClient) AddUniqueIndex(indexName string, columns interface{}) error {
return c.db.Migrator().CreateIndex(columns, indexName)
}
func (c *gormClient) RemoveIndex(indexName string, columns interface{}) error {
return c.db.Migrator().DropIndex(columns, indexName)
}
func (c *gormClient) Model(value interface{}) Client {
var newClient gormClient
newClient.db = c.db.Model(value)
return &newClient
}
func (c *gormClient) Where(query interface{}, args ...interface{}) Client {
var newClient gormClient
newClient.db = c.db.Where(query, args...)
return &newClient
}
func (c *gormClient) Create(value interface{}) (int64, error) {
newDb := c.db.Create(value)
return newDb.RowsAffected, newDb.Error
}
func (c *gormClient) Delete(value interface{}, where ...interface{}) (int64, error) {
newDb := c.db.Delete(value, where...)
return newDb.RowsAffected, newDb.Error
}
func (c *gormClient) Save(value interface{}) (int64, error) {
newDb := c.db.Save(value)
return newDb.RowsAffected, newDb.Error
}
func (c *gormClient) Update(column string, value interface{}) (int64, error) {
newDb := c.db.Update(column, value)
return newDb.RowsAffected, newDb.Error
}
func (c *gormClient) First(out interface{}, where ...interface{}) error {
return c.db.First(out, where...).Error
}
func (c *gormClient) Find(out interface{}, where ...interface{}) error {
return c.db.Find(out, where...).Error
}
func (c *gormClient) AutoMigrate(values ...interface{}) error {
return c.db.AutoMigrate(values...)
}
func (c *gormClient) Begin() Client {
var newClient gormClient
newClient.db = c.db.Begin()
return &newClient
}
func (c *gormClient) Rollback() error {
return c.db.Rollback().Error
}
func (c *gormClient) Commit() error {
return c.db.Commit().Error
}
func (c *gormClient) HasTable(value interface{}) bool {
return c.db.Migrator().HasTable(value)
}
func (c *gormClient) Exec(query string, args ...interface{}) int64 {
dbClient := c.db.Exec(query, args)
return dbClient.RowsAffected
}
func (c *gormClient) Rows(tablename string) (*sql.Rows, error) {
tableDb := c.db.Table(tablename)
return tableDb.Rows()
}