-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.go
145 lines (118 loc) · 3.47 KB
/
model.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
package yago
import (
"encoding"
"github.com/orus-io/qb"
)
// A Model provides handy access to a struct definition.
type Model interface {
GetMapper() Mapper
}
// ScalarField A simple scalar field
type ScalarField struct {
Column qb.ColumnElem
}
// MarshaledScalarField A text marshaled scalar field
type MarshaledScalarField struct {
ScalarField
}
// NewScalarField returns a new ScalarField
func NewScalarField(column qb.ColumnElem) ScalarField {
return ScalarField{
Column: column,
}
}
// NewMarshaledScalarField returns a new ScalarField
func NewMarshaledScalarField(column qb.ColumnElem) MarshaledScalarField {
return MarshaledScalarField{NewScalarField(column)}
}
// Accept calls the underlying column 'Accept'.
func (f ScalarField) Accept(context *qb.CompilerContext) string {
return f.Column.Accept(context)
}
// Like returns a LIKE clause
func (f ScalarField) Like(pattern string) qb.Clause {
return f.Column.Like(pattern)
}
// NotIn returns a NOT IN clause
func (f ScalarField) NotIn(values ...interface{}) qb.Clause {
return f.Column.NotIn(values...)
}
// In returns a IN clause
func (f ScalarField) In(values ...interface{}) qb.Clause {
return f.Column.In(values...)
}
// NotEq returns a != clause
func (f ScalarField) NotEq(value interface{}) qb.Clause {
return f.Column.NotEq(value)
}
// Eq returns a = clause
func (f ScalarField) Eq(value interface{}) qb.Clause {
return f.Column.Eq(value)
}
// Gt returns a > clause
func (f ScalarField) Gt(value interface{}) qb.Clause {
return f.Column.Gt(value)
}
// Lt returns a < clause
func (f ScalarField) Lt(value interface{}) qb.Clause {
return f.Column.Lt(value)
}
// Gte returns a >= clause
func (f ScalarField) Gte(value interface{}) qb.Clause {
return f.Column.Gte(value)
}
// Lte returns a <= clause
func (f ScalarField) Lte(value interface{}) qb.Clause {
return f.Column.Lte(value)
}
// marshalValue marshals the value if it implements encoding.TextMarshaler
func (f MarshaledScalarField) marshalValue(value interface{}) interface{} {
tm, ok := value.(encoding.TextMarshaler)
if ok {
b, err := tm.MarshalText()
if err != nil {
panic(err)
}
return b
}
return value
}
func (f MarshaledScalarField) marshalValues(values []interface{}) []interface{} {
var marshaled = make([]interface{}, len(values))
for _, value := range values {
marshaled = append(marshaled, f.marshalValue(value))
}
return marshaled
}
// NotIn returns a NOT IN clause
func (f MarshaledScalarField) NotIn(values ...interface{}) qb.Clause {
return f.Column.NotIn(f.marshalValues(values)...)
}
// In returns a IN clause
func (f MarshaledScalarField) In(values ...interface{}) qb.Clause {
return f.Column.In(f.marshalValues(values)...)
}
// NotEq returns a != clause
func (f MarshaledScalarField) NotEq(value interface{}) qb.Clause {
return f.Column.NotEq(f.marshalValue(value))
}
// Eq returns a = clause
func (f MarshaledScalarField) Eq(value interface{}) qb.Clause {
return f.Column.Eq(f.marshalValue(value))
}
// Gt returns a > clause
func (f MarshaledScalarField) Gt(value interface{}) qb.Clause {
return f.Column.Gt(f.marshalValue(value))
}
// Lt returns a < clause
func (f MarshaledScalarField) Lt(value interface{}) qb.Clause {
return f.Column.Lt(f.marshalValue(value))
}
// Gte returns a >= clause
func (f MarshaledScalarField) Gte(value interface{}) qb.Clause {
return f.Column.Gte(f.marshalValue(value))
}
// Lte returns a <= clause
func (f MarshaledScalarField) Lte(value interface{}) qb.Clause {
return f.Column.Lte(f.marshalValue(value))
}