2020-09-14 10:15:23 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2020-10-12 09:45:17 +08:00
|
|
|
"database/sql/driver"
|
2024-11-25 01:05:42 +08:00
|
|
|
"errors"
|
2020-09-14 10:15:23 +08:00
|
|
|
"fmt"
|
2020-10-12 09:45:17 +08:00
|
|
|
"time"
|
2020-09-14 10:15:23 +08:00
|
|
|
)
|
|
|
|
|
2024-11-25 01:05:42 +08:00
|
|
|
// returns a failed scan error.
|
|
|
|
// 失败的扫描错误
|
|
|
|
var failedScanError = func(src interface{}) error {
|
|
|
|
return errors.New(fmt.Sprintf("failed to scan value: %v", src))
|
|
|
|
}
|
|
|
|
|
2021-07-24 01:03:39 +08:00
|
|
|
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
|
2024-11-25 01:05:42 +08:00
|
|
|
func (c *Carbon) Scan(src interface{}) error {
|
|
|
|
switch v := src.(type) {
|
|
|
|
case []byte:
|
|
|
|
*c = Parse(string(v))
|
|
|
|
case string:
|
|
|
|
*c = Parse(v)
|
|
|
|
case time.Time:
|
|
|
|
*c = CreateFromStdTime(v)
|
|
|
|
}
|
2024-11-25 04:19:40 +08:00
|
|
|
if c.Error == nil {
|
|
|
|
return nil
|
2020-10-12 09:45:17 +08:00
|
|
|
}
|
2024-11-25 04:19:40 +08:00
|
|
|
return failedScanError(src)
|
2020-10-12 09:45:17 +08:00
|
|
|
}
|
2020-10-12 09:51:16 +08:00
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// Value the interface providing the Value method for package database/sql/driver.
|
2020-10-12 09:45:17 +08:00
|
|
|
func (c Carbon) Value() (driver.Value, error) {
|
2021-04-07 12:03:31 +08:00
|
|
|
if c.IsZero() {
|
2020-10-12 09:45:17 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
return c.StdTime(), nil
|
2023-12-22 16:06:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for Carbon struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (c Carbon) GormDataType() string {
|
|
|
|
return "time"
|
2020-10-12 09:45:17 +08:00
|
|
|
}
|
2024-10-17 15:15:30 +08:00
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateTime struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateTime) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateTimeMilli struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateTimeMilli) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateTimeMicro struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateTimeMicro) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateTimeNano struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateTimeNano) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for Date struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t Date) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateMilli struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateMilli) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateMicro struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateMicro) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for DateNano struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t DateNano) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for Time struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t Time) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for TimeMilli struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t TimeMilli) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for TimeMicro struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t TimeMicro) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for TimeNano struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t TimeNano) GormDataType() string {
|
|
|
|
return "time"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for Timestamp struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t Timestamp) GormDataType() string {
|
|
|
|
return "int"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for TimestampMilli struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t TimestampMilli) GormDataType() string {
|
|
|
|
return "int"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for TimestampMicro struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t TimestampMicro) GormDataType() string {
|
|
|
|
return "int"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GormDataType implements the interface GormDataTypeInterface for TimestampNano struct.
|
|
|
|
// 实现 GormDataTypeInterface 接口
|
|
|
|
func (t TimestampNano) GormDataType() string {
|
|
|
|
return "int"
|
|
|
|
}
|