-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes_mysql.go
More file actions
50 lines (45 loc) · 1.19 KB
/
types_mysql.go
File metadata and controls
50 lines (45 loc) · 1.19 KB
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
package dalc
import (
"database/sql/driver"
"fmt"
"reflect"
"time"
)
func MySqlTcpDSN(name string, password string, address string, schema string, parseTime bool, loc string) string {
return fmt.Sprintf(
"%s:%s@tcp(%s)/%s?parseTime=%v&loc=%s",
name, password, address, schema, parseTime, loc,
)
}
// name:password@tcp(ip:host)/schema?parseTime=true&loc=Local
type MySQLTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
func (n *MySQLTime) Scan(value interface{}) (err error) {
if value == nil {
n.Time, n.Valid = time.Time{}, false
return nil
}
n.Valid = true
switch value.(type) {
case []uint8:
v, _ := value.([]uint8)
n.Time, err = time.ParseInLocation("2006-01-02 15:04:05", string(v), time.Local)
case string:
v, _ := value.(string)
n.Time, err = time.ParseInLocation("2006-01-02 15:04:05", v, time.Local)
case time.Time:
n.Time = value.(time.Time)
default:
err = fmt.Errorf("dalc scan mysql time type failed, %s is not []uint8 and string", reflect.TypeOf(value).Name())
}
return
}
// Value implements the driver Valuer interface.
func (n MySQLTime) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time, nil
}