/
column.go
93 lines (82 loc) · 2.2 KB
/
column.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
package sq
import "time"
type colmode int
const (
colmodeInsert colmode = iota
colmodeUpdate
)
// Column keeps track of what the values mapped to what Field in an InsertQuery/SelectQuery.
type Column struct {
// mode determines if INSERT or UPDATE
mode colmode
// INSERT
rowStart bool
rowEnd bool
firstField string
insertColumns Fields
rowValues RowValues
// UPDATE
assignments Assignments
}
// Set maps the value to the Field.
func (col *Column) Set(field Field, value interface{}) {
if field == nil {
// should I panic with an error here instead?
return
}
switch col.mode {
case colmodeUpdate:
col.assignments = append(col.assignments, FieldAssignment{
Field: field,
Value: value,
})
case colmodeInsert:
fallthrough
default:
name := field.GetName()
if !col.rowStart {
col.rowStart = true
col.firstField = name
col.insertColumns = append(col.insertColumns, field)
col.rowValues = append(col.rowValues, RowValue{value})
return
}
switch name {
case col.firstField: // Start a new RowValue
if !col.rowEnd {
col.rowEnd = true
}
col.rowValues = append(col.rowValues, RowValue{value})
default: // Append to last RowValue
if !col.rowEnd {
col.insertColumns = append(col.insertColumns, field)
}
last := len(col.rowValues) - 1
col.rowValues[last] = append(col.rowValues[last], value)
}
}
}
// SetBool maps the bool value to the BooleanField.
func (col *Column) SetBool(field BooleanField, value bool) {
col.Set(field, value)
}
// SetFloat64 maps the float64 value to the NumberField.
func (col *Column) SetFloat64(field NumberField, value float64) {
col.Set(field, value)
}
// SetInt maps the int value to the NumberField.
func (col *Column) SetInt(field NumberField, value int) {
col.Set(field, value)
}
// SetInt64 maps the int64 value to the NumberField.
func (col *Column) SetInt64(field NumberField, value int64) {
col.Set(field, value)
}
// SetString maps the string value to the StringField.
func (col *Column) SetString(field StringField, value string) {
col.Set(field, value)
}
// SetTime maps the time.Time value to the TimeField.
func (col *Column) SetTime(field TimeField, value time.Time) {
col.Set(field, value)
}