-
Notifications
You must be signed in to change notification settings - Fork 0
/
alter_table_action_add.go
103 lines (89 loc) · 2.93 KB
/
alter_table_action_add.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gosql
// where add action is one of:
//
// ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
// ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]
// ADD table_constraint [ NOT VALID ]
// ADD table_constraint_using_index
type alterTableActionAdd struct {
// ordered expression
ordered orderedExpression
// column
column column
// table constraint
tableConstrain constraintTable
// table constraint using index
tableConstrainUsingIndex constraintTableUsingIndex
}
// Column add column
func (a *alterTableActionAdd) Column(name string, dataType string) *column {
return a.column.Name(name).Type(dataType)
}
// IfNotExists is not exists
func (a *alterTableActionAdd) IfNotExists() *alterTableActionAdd {
a.ordered.Add(1, a.ordered.Concat("IF NOT EXISTS"))
return a
}
// NotValid table constraint
func (a *alterTableActionAdd) NotValid() *alterTableActionAdd {
a.ordered.Add(1, a.ordered.Concat("NOT VALID"))
return a
}
// GeneratedAlways add GENERATED ALWAYS
func (a *alterTableActionAdd) GeneratedAlways() *alterTableActionAdd {
a.ordered.Add(0, a.ordered.Concat("ADD GENERATED ALWAYS"))
return a
}
// GeneratedByDefault add GENERATED BY DEFAULT
func (a *alterTableActionAdd) GeneratedByDefault() *alterTableActionAdd {
a.ordered.Add(0, a.ordered.Concat("ADD GENERATED BY DEFAULT"))
return a
}
// AsIdentity add sequence options
func (a *alterTableActionAdd) AsIdentity(options string) *alterTableActionAdd {
a.ordered.Add(1, a.ordered.Concat("AS IDENTITY ", options))
return a
}
// TableConstraint return table constraint
func (a *alterTableActionAdd) TableConstraint() *constraintTable {
return &a.tableConstrain
}
// TableConstraintUsingIndex return table constraint using index
func (a *alterTableActionAdd) TableConstraintUsingIndex() *constraintTableUsingIndex {
return &a.tableConstrainUsingIndex
}
// IsEmpty check if empty
func (a *alterTableActionAdd) IsEmpty() bool {
return a == nil || a.ordered.IsEmpty() && a.column.IsEmpty() && a.tableConstrain.IsEmpty() && a.tableConstrainUsingIndex.IsEmpty()
}
// Reset reset item data
func (a *alterTableActionAdd) Reset() *alterTableActionAdd {
a.ordered.Reset()
return a
}
// Grow memory
func (a *alterTableActionAdd) Grow(n int) *alterTableActionAdd {
a.ordered.Grow(n)
return a
}
// GetArguments get arguments
func (a *alterTableActionAdd) GetArguments() []any {
return a.ordered.GetArguments()
}
// String render alter table query
func (a *alterTableActionAdd) String() string {
if a.IsEmpty() {
return ""
}
if !a.column.IsEmpty() {
a.ordered.Add(0, a.ordered.Concat("ADD COLUMN"))
a.ordered.Add(2, a.ordered.Concat(a.column.String()))
}
if !a.tableConstrain.IsEmpty() {
a.ordered.Add(0, a.ordered.Concat("ADD ", a.tableConstrain.String()))
}
if !a.tableConstrainUsingIndex.IsEmpty() {
a.ordered.Add(0, a.ordered.Concat("ADD ", a.tableConstrainUsingIndex.String()))
}
return a.ordered.String()
}