/
col.go
85 lines (74 loc) · 1.89 KB
/
col.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
package exp
import (
"fmt"
"reflect"
"github.com/doug-martin/goqu/v9/internal/util"
)
type columnList struct {
columns []Expression
}
func NewColumnListExpression(vals ...interface{}) ColumnListExpression {
cols := []Expression{}
for _, val := range vals {
switch t := val.(type) {
case nil: // do nothing
case string:
cols = append(cols, ParseIdentifier(t))
case ColumnListExpression:
cols = append(cols, t.Columns()...)
case Expression:
cols = append(cols, t)
default:
_, valKind := util.GetTypeInfo(val, reflect.Indirect(reflect.ValueOf(val)))
if valKind == reflect.Struct {
cm, err := util.GetColumnMap(val)
if err != nil {
panic(err.Error())
}
structCols := cm.Cols()
for _, col := range structCols {
i := ParseIdentifier(col)
var sc Expression = i
if i.IsQualified() {
sc = i.As(NewIdentifierExpression("", "", col))
}
cols = append(cols, sc)
}
} else {
panic(fmt.Sprintf("Cannot create expression from %+v", val))
}
}
}
return columnList{columns: cols}
}
func NewOrderedColumnList(vals ...OrderedExpression) ColumnListExpression {
exps := make([]interface{}, 0, len(vals))
for _, col := range vals {
exps = append(exps, col.Expression())
}
return NewColumnListExpression(exps...)
}
func (cl columnList) Clone() Expression {
newExps := make([]Expression, 0, len(cl.columns))
for _, exp := range cl.columns {
newExps = append(newExps, exp.Clone())
}
return columnList{columns: newExps}
}
func (cl columnList) Expression() Expression {
return cl
}
func (cl columnList) IsEmpty() bool {
return len(cl.columns) == 0
}
func (cl columnList) Columns() []Expression {
return cl.columns
}
func (cl columnList) Append(cols ...Expression) ColumnListExpression {
ret := columnList{}
exps := ret.columns
exps = append(exps, cl.columns...)
exps = append(exps, cols...)
ret.columns = exps
return ret
}