-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_info.go
44 lines (38 loc) · 1.05 KB
/
column_info.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
package pgtalk
import (
"fmt"
"strconv"
"strings"
)
type ColumnInfo struct {
tableInfo TableInfo
columnName string
isPrimary bool
notNull bool
isMixedCase bool
}
// MakeColumnInfo creates a ColumnInfo describing a column in a table.
// The last argument is now ignored (used to be table attribute number, field ordinal).
func MakeColumnInfo(tableInfo TableInfo, columnName string, isPrimary bool, isNotNull bool, _ uint16) ColumnInfo {
return ColumnInfo{
tableInfo: tableInfo,
columnName: columnName,
notNull: isNotNull,
isPrimary: isPrimary,
isMixedCase: strings.ToLower(columnName) != columnName,
}
}
func (c ColumnInfo) Name() string {
if c.isMixedCase {
return strconv.Quote(c.columnName)
}
return c.columnName
}
func (c ColumnInfo) SQLOn(w WriteContext) {
fmt.Fprintf(w, "%s.%s", w.TableAlias(c.tableInfo.Name, c.tableInfo.Alias), c.Name())
}
// TableAlias changes the table alias for this column info.
func (c ColumnInfo) TableAlias(alias string) ColumnInfo {
c.tableInfo = c.tableInfo.WithAlias(alias)
return c
}