This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goshia.go
200 lines (173 loc) · 4.3 KB
/
goshia.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package goshia
import (
"errors"
"time"
"github.com/teacat/rushia/v3"
"gorm.io/gorm"
)
var (
ErrAlreadyBegun = errors.New("goshia: transaction has already begun")
ErrNotTransaction = errors.New("goshia: processing non-transaction")
)
type Type int
const (
TypeMySQL Type = iota + 1
TypeSQLite
)
// Goshia
type Goshia struct {
Gorm *gorm.DB
Type Type
isTransaction bool
}
//
type Transaction struct {
}
// New
func New(g *gorm.DB) *Goshia {
typ := TypeMySQL
if g.Dialector.Name() == "sqlite" {
typ = TypeSQLite
}
return &Goshia{
Gorm: g,
Type: typ,
}
}
// Query
func (g *Goshia) Query(q *rushia.Query, dest interface{}) error {
query, params := rushia.Build(q)
return g.Gorm.Raw(query, params...).Scan(dest).Error
}
// QueryCount
func (g *Goshia) QueryCount(q *rushia.Query, dest interface{}) (count int, err error) {
countQuery, countParams := rushia.Build(q.Copy().ClearLimit().Select("COUNT(*)"))
if err := g.Gorm.Raw(countQuery, countParams...).Scan(&count).Error; err != nil {
return 0, err
}
query, params := rushia.Build(q)
return count, g.Gorm.Raw(query, params...).Scan(dest).Error
}
// Exec
func (g *Goshia) Exec(q *rushia.Query) (err error) {
query, params := rushia.Build(q)
result := g.Gorm.Exec(query, params...)
err = result.Error
return
}
// ExecAffected
func (g *Goshia) ExecAffected(q *rushia.Query) (affectedRows int, err error) {
query, params := rushia.Build(q)
result := g.Gorm.Exec(query, params...)
affectedRows = int(result.RowsAffected)
err = result.Error
return
}
// ExecID
func (g *Goshia) ExecID(q *rushia.Query) (id int, err error) {
query, params := rushia.Build(q)
err = g.Gorm.Transaction(func(tx *gorm.DB) error {
if err := tx.Exec(query, params...).Error; err != nil {
return err
}
switch g.Type {
case TypeMySQL:
if err := tx.Raw(`SELECT LAST_INSERT_ID()`).Scan(&id).Error; err != nil {
return err
}
case TypeSQLite:
if err := tx.Raw(`SELECT LAST_INSERT_ROWID()`).Scan(&id).Error; err != nil {
return err
}
}
return nil
}, nil)
return
}
// Transaction
func (g *Goshia) Transaction(handler func(tx *Goshia) error) error {
return g.Gorm.Transaction(func(tx *gorm.DB) error {
return handler(&Goshia{
Gorm: tx,
isTransaction: true,
})
})
}
// Begin
func (g *Goshia) Begin() *Goshia {
if g.isTransaction {
panic(ErrAlreadyBegun)
}
return &Goshia{
Gorm: g.Gorm.Begin(),
isTransaction: true,
}
}
// Rollback
func (g *Goshia) Rollback() *Goshia {
if !g.isTransaction {
panic(ErrNotTransaction)
}
return &Goshia{
Gorm: g.Gorm.Rollback(),
isTransaction: true,
}
}
// RollbackTo
func (g *Goshia) RollbackTo(name string) *Goshia {
if !g.isTransaction {
panic(ErrNotTransaction)
}
return &Goshia{
Gorm: g.Gorm.RollbackTo(name),
isTransaction: true,
}
}
// Commit
func (g *Goshia) Commit() *Goshia {
if !g.isTransaction {
panic(ErrNotTransaction)
}
return &Goshia{
Gorm: g.Gorm.Commit(),
isTransaction: false,
}
}
// SavePoint
func (g *Goshia) SavePoint(name string) *Goshia {
if !g.isTransaction {
panic(ErrNotTransaction)
}
return &Goshia{
Gorm: g.Gorm.SavePoint(name),
isTransaction: false,
}
}
// String 會回傳一個指針字串,這用來填補 SQL 中的 Nullable 欄位。
func String(v string) *string {
return &v
}
// Bool 會回傳一個指針布林值,這用來填補 SQL 中的 Nullable 欄位。
func Bool(v bool) *bool {
return &v
}
// Int 會回傳一個指針正整數,這用來填補 SQL 中的 Nullable 欄位。
func Int(v int) *int {
return &v
}
// SliceInt 會回傳一個指針切片正整數,這用來填補 SQL 中的 Nullable 欄位。
func SliceInt(v []int) *[]int {
return &v
}
// SliceString 會回傳一個指針切片字串,這用來填補 SQL 中的 Nullable 欄位。
func SliceString(v []string) *[]string {
return &v
}
// Float64 會回傳一個指針浮點數,這用來填補 SQL 中的 Nullable 欄位。
func Float64(v float64) *float64 {
return &v
}
// Time 會回傳一個指針時間,這用來填補 SQL 中的 Nullable 欄位。
func Time(v time.Time) *time.Time {
return &v
}