-
Notifications
You must be signed in to change notification settings - Fork 0
/
joinType.go
142 lines (123 loc) · 3.61 KB
/
joinType.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
package squl
import (
"bytes"
"encoding/json"
fmt "golang.org/x/xerrors"
"github.com/trivigy/squl/internal/global"
)
// JoinType describes the types of joins available.
type JoinType int
const (
// JoinTypeDefault indicates the usage of JOIN.
JoinTypeDefault JoinType = iota + 1
// JoinTypeInner indicates the usage of INNER JOIN.
JoinTypeInner
// JoinTypeLeft indicates the usage of LEFT JOIN.
JoinTypeLeft
// JoinTypeOuterLeft indicates the usage of LEFT OUTER JOIN.
JoinTypeOuterLeft
// JoinTypeRight indicates the usage of RIGHT JOIN.
JoinTypeRight
// JoinTypeOuterRight indicates the usage of RIGHT OUTER JOIN.
JoinTypeOuterRight
// JoinTypeFull indicates the usage of FULL JOIN.
JoinTypeFull
// JoinTypeOuterFull indicates the usage of FULL OUTER JOIN.
JoinTypeOuterFull
// JoinTypeCross indicates the usage of CROSS JOIN.
JoinTypeCross
)
const (
joinTypeDefaultStr = "default"
joinTypeInnerStr = "inner"
joinTypeLeftStr = "left"
joinTypeOuterLeftStr = "outerLeft"
joinTypeRightStr = "right"
joinTypeOuterRightStr = "outerRight"
joinTypeFullStr = "full"
joinTypeOuterFullStr = "outerFull"
joinTypeCrossStr = "cross"
)
var toStringJoinType = map[JoinType]string{
JoinType(Unknown): unknownStr,
JoinTypeDefault: joinTypeDefaultStr,
JoinTypeInner: joinTypeInnerStr,
JoinTypeLeft: joinTypeLeftStr,
JoinTypeOuterLeft: joinTypeOuterLeftStr,
JoinTypeRight: joinTypeRightStr,
JoinTypeOuterRight: joinTypeOuterRightStr,
JoinTypeFull: joinTypeFullStr,
JoinTypeOuterFull: joinTypeOuterFullStr,
JoinTypeCross: joinTypeCrossStr,
}
// NewJoinType creates a new instance of the enum from raw string.
func NewJoinType(raw string) (JoinType, error) {
switch raw {
case joinTypeDefaultStr:
return JoinTypeDefault, nil
case joinTypeInnerStr:
return JoinTypeInner, nil
case joinTypeLeftStr:
return JoinTypeLeft, nil
case joinTypeOuterLeftStr:
return JoinTypeOuterLeft, nil
case joinTypeRightStr:
return JoinTypeRight, nil
case joinTypeOuterRightStr:
return JoinTypeOuterRight, nil
case joinTypeFullStr:
return JoinTypeFull, nil
case joinTypeOuterFullStr:
return JoinTypeOuterFull, nil
case joinTypeCrossStr:
return JoinTypeCross, nil
default:
return JoinType(Unknown), fmt.Errorf(global.ErrFmt, pkg.Name(), fmt.Errorf("unknown type %q", raw))
}
}
// String returns the string representation of the enum type
func (r JoinType) String() string {
return toStringJoinType[r]
}
// UnmarshalJSON unmarshals a quoted json string to enum type.
func (r *JoinType) UnmarshalJSON(rbytes []byte) error {
var s string
if err := json.Unmarshal(rbytes, &s); err != nil {
return err
}
switch s {
case joinTypeDefaultStr:
*r = JoinTypeDefault
case joinTypeInnerStr:
*r = JoinTypeInner
case joinTypeLeftStr:
*r = JoinTypeLeft
case joinTypeOuterLeftStr:
*r = JoinTypeOuterLeft
case joinTypeRightStr:
*r = JoinTypeRight
case joinTypeOuterRightStr:
*r = JoinTypeOuterRight
case joinTypeFullStr:
*r = JoinTypeFull
case joinTypeOuterFullStr:
*r = JoinTypeOuterFull
case joinTypeCrossStr:
*r = JoinTypeCross
default:
*r = Unknown
return fmt.Errorf(global.ErrFmt, pkg.Name(), fmt.Errorf("unknown type %q", s))
}
return nil
}
// MarshalJSON marshals the enum as a quoted json string.
func (r JoinType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
if _, err := buffer.WriteString(toStringJoinType[r]); err != nil {
return nil, fmt.Errorf(global.ErrFmt, pkg.Name(), err)
}
if _, err := buffer.WriteString(`"`); err != nil {
return nil, fmt.Errorf(global.ErrFmt, pkg.Name(), err)
}
return buffer.Bytes(), nil
}