Repository navigation
Expand file tree
/
Copy pathcolumn_def.go
More file actions
72 lines (66 loc) · 2.46 KB
/
Copy pathcolumn_def.go
File metadata and controls
72 lines (66 loc) · 2.46 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
package ast
import "github.com/sqlc-dev/sqlc/internal/sql/format"
type ColumnDef struct {
Tag NodeTag[ColumnDef] `json:"tag"`
Colname string `json:"colname"`
// TypeName is the column's type for the catalog. When Typeless is set
// the author wrote no type at all — SQLite allows it — and the column
// prints without one, whatever TypeName carries.
TypeName *TypeName `json:"type_name,omitempty"`
Typeless bool `json:"typeless"`
IsNotNull bool `json:"is_not_null"`
IsUnsigned bool `json:"is_unsigned"`
IsArray bool `json:"is_array"`
ArrayDims int `json:"array_dims"`
Vals *List `json:"vals,omitempty"`
Length *int `json:"length,omitempty"`
PrimaryKey bool `json:"primary_key"`
// IsHidden marks a column a relation offers by name without listing it,
// like the column an sqlite fts5 table names after itself. The legacy
// catalog drops hidden columns; the core catalog keeps them out of star
// expansions and models.
IsHidden bool `json:"is_hidden"`
// From pg.ColumnDef
Inhcount int `json:"inhcount"`
IsLocal bool `json:"is_local"`
IsFromType bool `json:"is_from_type"`
IsFromParent bool `json:"is_from_parent"`
Storage byte `json:"storage"`
RawDefault Node `json:"raw_default,omitempty"`
CookedDefault Node `json:"cooked_default,omitempty"`
Identity byte `json:"identity"`
CollClause *CollateClause `json:"coll_clause,omitempty"`
CollOid Oid `json:"coll_oid"`
Constraints *List `json:"constraints,omitempty"`
Fdwoptions *List `json:"fdwoptions,omitempty"`
Location int `json:"location"`
Comment string `json:"comment"`
}
func (n *ColumnDef) Pos() int {
return n.Location
}
func (n *ColumnDef) Format(buf *TrackedBuffer, d format.Dialect) {
if n == nil {
return
}
buf.WriteString(n.Colname)
if !n.Typeless {
buf.WriteString(" ")
buf.astFormat(n.TypeName, d)
// MySQL integer types: signedness is part of the type.
if n.IsUnsigned {
buf.WriteString(" unsigned")
}
}
// Use IsArray from ColumnDef since TypeName.ArrayBounds may not be set
// (for type resolution compatibility)
if n.IsArray && !items(n.TypeName.ArrayBounds) {
buf.WriteString("[]")
}
if n.PrimaryKey {
buf.WriteString(" PRIMARY KEY")
} else if n.IsNotNull {
buf.WriteString(" NOT NULL")
}
buf.astFormat(n.Constraints, d)
}