Newer
Older
import (
"fmt"
"strings"
)
type columnT struct {
Name string
Alias string
}
// Column represents a SQL column.
type Column struct {
Name interface{}
}
// ColumnWithName creates and returns a Column with the given name.
func ColumnWithName(name string) *Column {
return &Column{Name: name}
}
// Hash returns a unique identifier for the struct.
func (c *Column) Hash() string {
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
}
// Compile transforms the ColumnValue into an equivalent SQL representation.
func (c *Column) Compile(layout *Template) (compiled string) {
if z, ok := layout.Read(c); ok {
return z
}
switch value := c.Name.(type) {
case string:
input := trimString(value)
chunks := separateByAS(input)
if len(chunks) == 1 {
chunks = separateBySpace(input)
}
name := chunks[0]
nameChunks := strings.SplitN(name, layout.ColumnSeparator, 2)
for i := range nameChunks {
nameChunks[i] = trimString(nameChunks[i])
nameChunks[i] = mustParse(layout.IdentifierQuote, Raw{Value: nameChunks[i]})
}
name = strings.Join(nameChunks, layout.ColumnSeparator)
var alias string
if len(chunks) > 1 {
alias = trimString(chunks[1])
alias = mustParse(layout.IdentifierQuote, Raw{Value: alias})
}
compiled = mustParse(layout.ColumnAliasLayout, columnT{name, alias})
case Raw:
compiled = value.String()
default:
compiled = fmt.Sprintf("%v", c.Name)
}
layout.Write(c, compiled)
return
}