-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolumns.go
More file actions
81 lines (72 loc) · 1.65 KB
/
columns.go
File metadata and controls
81 lines (72 loc) · 1.65 KB
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
package sqlparser
import (
"github.com/viant/sqlparser/query"
"strings"
)
// Columns represens column
type Columns []*Column
// Index indexes column by first non-empty alias or name or expr respectively
func (c Columns) Index() map[string]*Column {
var result = make(map[string]*Column)
for i, item := range c {
result[item.Identity()] = c[i]
}
return result
}
// ByName indexes column by name
func (c Columns) ByName() map[string]*Column {
var result = make(map[string]*Column)
for i, item := range c {
if item.Name != "" {
result[item.Name] = c[i]
}
}
return result
}
// ByLowerCase indexes column by lower cased name
func (c Columns) ByLowerCase() map[string]*Column {
var result = make(map[string]*Column)
for i, item := range c {
if item.Name != "" {
result[strings.ToLower(item.Identity())] = c[i]
}
}
return result
}
// Namespace returns namespace column
func (c Columns) Namespace(namespace string) Columns {
var result = Columns{}
for i, item := range c {
if item.Namespace == namespace {
result = append(result, c[i])
}
}
return result
}
// StarExpr returns star expr
func (c Columns) StarExpr(namespace string) *Column {
for _, item := range c {
if item.Name == "*" && (item.Namespace == namespace || namespace == "") {
return item
}
}
return nil
}
func (c Columns) IsStarExpr() bool {
if len(c) != 1 {
return false
}
return strings.HasSuffix(c[0].Expression, "*")
}
// NewColumns creates a columns
func NewColumns(list query.List) Columns {
var result Columns
if len(list) == 0 {
return result
}
for i := range list {
column := NewColumn(list[i])
result = append(result, column)
}
return result
}