-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
set.go
70 lines (60 loc) · 1.34 KB
/
set.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
package types
import (
"database/sql/driver"
"strings"
"github.com/si3nloong/sqlike/reflext"
sqldriver "github.com/si3nloong/sqlike/sql/driver"
"github.com/si3nloong/sqlike/sqlike/columns"
"github.com/si3nloong/sqlike/util"
)
// Set : sql data type of `SET`
type Set []string
// DataType :
func (s Set) DataType(_ sqldriver.Info, sf reflext.StructFielder) columns.Column {
blr := util.AcquireString()
defer util.ReleaseString(blr)
var def *string
blr.WriteString("SET(")
blr.WriteByte('\'')
val, ok := sf.Tag().LookUp("set")
if ok {
paths := strings.Split(val, "|")
if len(paths) >= 64 {
panic("maximum 64 of SET value")
}
def = &paths[0]
blr.WriteString(strings.Join(paths, "','"))
}
blr.WriteByte('\'')
blr.WriteByte(')')
return columns.Column{
Name: sf.Name(),
Type: blr.String(),
DataType: "SET",
Nullable: reflext.IsNullable(sf.Type()),
Charset: &latin1,
Collation: &latin1Bin,
DefaultValue: def,
}
}
// Value :
func (s Set) Value() (driver.Value, error) {
if s == nil {
return nil, nil
}
return strings.Join(s, ","), nil
}
// Scan :
func (s *Set) Scan(it interface{}) error {
switch vi := it.(type) {
case []byte:
s.unmarshal(string(vi))
case string:
s.unmarshal(vi)
case nil:
}
return nil
}
func (s *Set) unmarshal(val string) {
*s = strings.Split(val, ",")
}