-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathdatabase.go
executable file
·390 lines (357 loc) · 9.71 KB
/
database.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package carbon
import (
"bytes"
"database/sql/driver"
"fmt"
"strconv"
"time"
)
// timestamp precision constants
// 时间戳精度常量
const (
PrecisionSecond = 9
PrecisionMillisecond = 999
PrecisionMicrosecond = 999999
PrecisionNanosecond = 999999999
)
// LayoutFactory defines a LayoutFactory interface
// 定义 LayoutFactory 接口
type LayoutFactory interface {
~string
SetLayout() string
}
// LayoutType defines a LayoutType generic struct
// 定义 LayoutType 泛型结构体
type LayoutType[T LayoutFactory] struct {
*Carbon
}
// FormatFactory defines a FormatFactory interface.
// 定义 FormatFactory 接口
type FormatFactory interface {
~string
SetFormat() string
}
// FormatType defines a FormatType generic struct.
// 定义 FormatType 泛型结构体
type FormatType[T FormatFactory] struct {
*Carbon
}
// TimestampFactory defines a TimestampFactory interface.
// 定义 TimestampFactory 接口
type TimestampFactory interface {
~int64
SetPrecision() int64
}
// TimestampType defines a TimestampType generic struct.
// 定义 TimestampType 泛型结构体
type TimestampType[T TimestampFactory] struct {
*Carbon
}
// NewLayoutType returns a new LayoutType generic instance.
// 返回 LayoutType 泛型实例
func NewLayoutType[T LayoutFactory](carbon *Carbon) LayoutType[T] {
return LayoutType[T]{
Carbon: carbon,
}
}
// NewFormatType returns a new FormatType generic instance.
// 返回 FormatType 泛型实例
func NewFormatType[T FormatFactory](carbon *Carbon) FormatType[T] {
return FormatType[T]{
Carbon: carbon,
}
}
// NewTimestampType returns a new TimestampType generic instance.
// 返回 TimestampType 泛型实例
func NewTimestampType[T TimestampFactory](carbon *Carbon) TimestampType[T] {
return TimestampType[T]{
Carbon: carbon,
}
}
// Scan implements driver.Scanner interface for LayoutType generic struct.
// 实现 driver.Scanner 接口
func (t *LayoutType[T]) Scan(src any) error {
c := NewCarbon()
switch v := src.(type) {
case []byte:
c = Parse(string(v), DefaultTimezone)
case string:
c = Parse(v, DefaultTimezone)
case time.Time:
c = CreateFromStdTime(v, DefaultTimezone)
case int64:
c = CreateFromTimestamp(v, DefaultTimezone)
default:
return failedScanError(v)
}
*t = NewLayoutType[T](c)
return t.Error
}
// Value implements driver.Valuer interface for LayoutType generic struct.
// 实现 driver.Valuer 接口
func (t LayoutType[T]) Value() (driver.Value, error) {
if t.IsNil() || t.IsZero() {
return nil, nil
}
if t.HasError() {
return nil, t.Error
}
return t.StdTime(), nil
}
// MarshalJSON implements json.Marshal interface for LayoutType generic struct.
// 实现 json.Marshaler 接口
func (t LayoutType[T]) MarshalJSON() ([]byte, error) {
emptyBytes := []byte(`""`)
if t.IsNil() || t.IsZero() {
return emptyBytes, nil
}
if t.HasError() {
return emptyBytes, t.Error
}
return []byte(fmt.Sprintf(`"%s"`, t.Layout(t.getLayout(), t.Timezone()))), nil
}
// UnmarshalJSON implements json.Unmarshal interface for LayoutType generic struct.
// 实现 json.Unmarshaler 接口
func (t *LayoutType[T]) UnmarshalJSON(b []byte) error {
value := string(bytes.Trim(b, `"`))
if value == "" || value == "null" || value == "0" {
t.Carbon = nil
return nil
}
*t = NewLayoutType[T](ParseByLayout(value, t.getLayout()))
return t.Error
}
// String implements Stringer interface for LayoutType generic struct.
// 实现 Stringer 接口
func (t LayoutType[T]) String() string {
if t.IsZero() || t.IsInvalid() {
return ""
}
return t.Layout(t.getLayout(), t.Timezone())
}
// GormDataType sets gorm data type for LayoutType generic struct.
// 设置 gorm 数据类型
func (t LayoutType[T]) GormDataType() string {
return "time"
}
// getLayout returns the set layout.
// 返回设置的布局模板
func (t LayoutType[T]) getLayout() string {
var factory T
return factory.SetLayout()
}
// Scan implements driver.Scanner interface for FormatType generic struct.
// 实现 driver.Scanner 接口
func (t *FormatType[T]) Scan(src any) error {
c := NewCarbon()
switch v := src.(type) {
case []byte:
c = Parse(string(v), DefaultTimezone)
case string:
c = Parse(v, DefaultTimezone)
case time.Time:
c = CreateFromStdTime(v, DefaultTimezone)
case int64:
c = CreateFromTimestamp(v, DefaultTimezone)
default:
return failedScanError(v)
}
*t = NewFormatType[T](c)
return t.Error
}
// Value implements driver.Valuer interface for FormatType generic struct.
// 实现 driver.Valuer 接口
func (t FormatType[T]) Value() (driver.Value, error) {
if t.IsNil() || t.IsZero() {
return nil, nil
}
if t.HasError() {
return nil, t.Error
}
return t.StdTime(), nil
}
// MarshalJSON implements json.Marshal interface for FormatType generic struct.
// 实现 json.Marshaler 接口
func (t FormatType[T]) MarshalJSON() ([]byte, error) {
emptyBytes := []byte(`""`)
if t.IsNil() || t.IsZero() {
return emptyBytes, nil
}
if t.HasError() {
return emptyBytes, t.Error
}
return []byte(fmt.Sprintf(`"%s"`, t.Format(t.getFormat(), t.Timezone()))), nil
}
// UnmarshalJSON implements json.Unmarshal interface for FormatType generic struct.
// 实现 json.Unmarshaler 接口
func (t *FormatType[T]) UnmarshalJSON(b []byte) error {
value := string(bytes.Trim(b, `"`))
if value == "" || value == "null" || value == "0" {
t.Carbon = nil
return nil
}
*t = NewFormatType[T](ParseByFormat(value, t.getFormat()))
return t.Error
}
// String implements Stringer interface for FormatType generic struct.
// 实现 Stringer 接口
func (t FormatType[T]) String() string {
if t.IsZero() || t.IsInvalid() {
return ""
}
return t.Format(t.getFormat(), t.Timezone())
}
// GormDataType sets gorm data type for FormatType generic struct.
// 设置 gorm 数据类型
func (t FormatType[T]) GormDataType() string {
return "time"
}
// getFormat returns the set format.
// 返回设置的格式模板
func (t FormatType[T]) getFormat() string {
var factory T
return factory.SetFormat()
}
// Scan implements driver.Scanner interface for TimestampType generic struct.
// 实现 driver.Scanner 接口
func (t *TimestampType[T]) Scan(src any) (err error) {
ts := int64(0)
c := NewCarbon()
switch v := src.(type) {
case []byte:
ts, err = strconv.ParseInt(string(v), 10, 64)
if err != nil {
return invalidTimestampError(string(v))
}
case string:
ts, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return invalidTimestampError(v)
}
case int64:
ts = v
case time.Time:
c = CreateFromStdTime(v, DefaultTimezone)
*t = NewTimestampType[T](c)
return t.Error
default:
return failedScanError(src)
}
switch t.getPrecision() {
case PrecisionSecond:
c = CreateFromTimestamp(ts, DefaultTimezone)
case PrecisionMillisecond:
c = CreateFromTimestampMilli(ts, DefaultTimezone)
case PrecisionMicrosecond:
c = CreateFromTimestampMicro(ts, DefaultTimezone)
case PrecisionNanosecond:
c = CreateFromTimestampNano(ts, DefaultTimezone)
}
*t = NewTimestampType[T](c)
return t.Error
}
// Value implements driver.Valuer interface for TimestampType generic struct.
// 实现 driver.Valuer 接口
func (t TimestampType[T]) Value() (driver.Value, error) {
if t.IsNil() || t.IsZero() {
return nil, nil
}
if t.HasError() {
return nil, t.Error
}
v := int64(0)
switch t.getPrecision() {
case PrecisionSecond:
v = t.Timestamp()
case PrecisionMillisecond:
v = t.TimestampMilli()
case PrecisionMicrosecond:
v = t.TimestampMicro()
case PrecisionNanosecond:
v = t.TimestampNano()
}
return v, nil
}
// MarshalJSON implements json.Marshal interface for TimestampType generic struct.
// 实现 json.Marshaler 接口
func (t TimestampType[T]) MarshalJSON() ([]byte, error) {
ts := int64(0)
if t.IsNil() || t.IsZero() {
return []byte(fmt.Sprintf(`%d`, ts)), nil
}
if t.HasError() {
return []byte(fmt.Sprintf(`%d`, ts)), t.Error
}
switch t.getPrecision() {
case PrecisionSecond:
ts = t.Timestamp()
case PrecisionMillisecond:
ts = t.TimestampMilli()
case PrecisionMicrosecond:
ts = t.TimestampMicro()
case PrecisionNanosecond:
ts = t.TimestampNano()
}
return []byte(fmt.Sprintf(`%d`, ts)), nil
}
// UnmarshalJSON implements json.Unmarshal interface for TimestampType generic struct.
// 实现 json.Unmarshaler 接口
func (t *TimestampType[T]) UnmarshalJSON(b []byte) error {
value := string(bytes.Trim(b, `"`))
c := NewCarbon()
if value == "" || value == "null" || value == "0" {
t.Carbon = nil
return nil
}
ts, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return invalidTimestampError(value)
}
switch t.getPrecision() {
case PrecisionSecond:
c = CreateFromTimestamp(ts, DefaultTimezone)
case PrecisionMillisecond:
c = CreateFromTimestampMilli(ts, DefaultTimezone)
case PrecisionMicrosecond:
c = CreateFromTimestampMicro(ts, DefaultTimezone)
case PrecisionNanosecond:
c = CreateFromTimestampNano(ts, DefaultTimezone)
}
*t = NewTimestampType[T](c)
return t.Error
}
// String implements Stringer interface for TimestampType generic struct.
// 实现 Stringer 接口
func (t TimestampType[T]) String() string {
return strconv.FormatInt(t.Int64(), 10)
}
// Int64 returns the timestamp value.
// 返回时间戳
func (t TimestampType[T]) Int64() int64 {
ts := int64(0)
if t.IsZero() || t.IsInvalid() {
return ts
}
switch t.getPrecision() {
case PrecisionSecond:
ts = t.Timestamp()
case PrecisionMillisecond:
ts = t.TimestampMilli()
case PrecisionMicrosecond:
ts = t.TimestampMicro()
case PrecisionNanosecond:
ts = t.TimestampNano()
}
return ts
}
// GormDataType sets gorm data type for TimestampType generic struct.
// 设置 gorm 数据类型
func (t TimestampType[T]) GormDataType() string {
return "time"
}
// getPrecision returns the set timestamp precision.
// 返回设置的时间戳精度
func (t TimestampType[T]) getPrecision() int64 {
var factory T
return factory.SetPrecision()
}