This repository has been archived by the owner on Nov 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columns.go
96 lines (79 loc) · 1.65 KB
/
columns.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
package chem
import (
"fmt"
"sort"
)
type Column interface {
Table() Table
toColumnExpression(withTableName bool) string
}
type columns struct {
columns *[]Column
less func(left, right Column) bool
}
func (cols columns) Len() int {
return len(*cols.columns)
}
func (cols columns) Swap(i, j int) {
(*cols.columns)[i], (*cols.columns)[j] = (*cols.columns)[j], (*cols.columns)[i]
}
func (cols columns) Less(i, j int) bool {
return cols.less((*cols.columns)[i], (*cols.columns)[j])
}
func lessColumnsByUnqualifiedName(left, right Column) bool {
return left.toColumnExpression(false) < right.toColumnExpression(false)
}
func sortColumns(cols []Column) []Column {
copiedSlice := cols[:]
sorter := columns{
columns: &copiedSlice,
less: lessColumnsByUnqualifiedName,
}
sort.Sort(sorter)
return *sorter.columns
}
type BaseColumn struct {
Container Table
Name string
}
func (c BaseColumn) toColumnExpression(withTableName bool) string {
if withTableName {
return fmt.Sprintf("%v.%v", c.Container.Name(), c.Name)
}
return c.Name
}
func (c BaseColumn) Table() Table {
return c.Container
}
func (c BaseColumn) Asc() Ordering {
return ColumnOrdering{
column: c,
descending: false,
}
}
func (c BaseColumn) Desc() Ordering {
return ColumnOrdering{
column: c,
descending: true,
}
}
type IntegerColumn struct {
BaseColumn
}
func (c IntegerColumn) Equals(i int) Filter {
return ValueFilter{
column: c,
operator: equalsOperator,
value: i,
}
}
type StringColumn struct {
BaseColumn
}
func (c StringColumn) Equals(s string) Filter {
return ValueFilter{
column: c,
operator: equalsOperator,
value: s,
}
}