forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalter.go
100 lines (90 loc) · 2.43 KB
/
alter.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
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package ddl
import (
"fmt"
"github.com/pingcap/tidb/parser/coldef"
)
// AlterTableSpecification.Action types.
const (
AlterTableOpt int = iota + 1
AlterAddColumn
AlterAddConstr
AlterDropColumn
AlterDropPrimaryKey
AlterDropIndex
AlterDropForeignKey
// TODO: Add more actions
)
// ColumnPosition Types
const (
ColumnPositionNone int = iota
ColumnPositionFirst
ColumnPositionAfter
)
// ColumnPosition represent the position of the newly added column
type ColumnPosition struct {
// ColumnPositionNone | ColumnPositionFirst | ColumnPositionAfter
Type int
// RelativeColumn is the column the newly added column after if type is ColumnPositionAfter
RelativeColumn string
}
// String implements fmt.Stringer
func (cp *ColumnPosition) String() string {
switch cp.Type {
case ColumnPositionFirst:
return "FIRST"
case ColumnPositionAfter:
return fmt.Sprintf("AFTER %s", cp.RelativeColumn)
default:
return ""
}
}
// AlterSpecification alter table specification
type AlterSpecification struct {
Action int
Name string
Constraint *coldef.TableConstraint
TableOpts []*coldef.TableOpt
Column *coldef.ColumnDef
Position *ColumnPosition
}
// String implements fmt.Stringer
func (as *AlterSpecification) String() string {
switch as.Action {
case AlterTableOpt:
// TODO: Finish this
return ""
case AlterAddConstr:
if as.Constraint != nil {
return fmt.Sprintf("ADD %s", as.Constraint.String())
}
return ""
case AlterDropColumn:
return fmt.Sprintf("DROP COLUMN %s", as.Name)
case AlterDropPrimaryKey:
return fmt.Sprintf("DROP PRIMARY KEY")
case AlterDropIndex:
return fmt.Sprintf("DROP INDEX %s", as.Name)
case AlterDropForeignKey:
return fmt.Sprintf("DROP FOREIGN KEY %s", as.Name)
case AlterAddColumn:
ps := as.Position.String()
if len(ps) > 0 {
return fmt.Sprintf("ADD Column %s %s", as.Column.String(), ps)
}
return fmt.Sprintf("ADD Column %s", as.Column.String())
default:
return ""
}
}