Skip to content

Commit b1eb5fc

Browse files
authored
*: spport primary key (#617)
1 parent c51ae7d commit b1eb5fc

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

model/ddl.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ const (
6161
ActionRepairTable ActionType = 29
6262
ActionSetTiFlashReplica ActionType = 30
6363
ActionUpdateTiFlashReplicaStatus ActionType = 31
64+
ActionAddPrimaryKey ActionType = 32
65+
ActionDropPrimaryKey ActionType = 33
6466
)
6567

66-
// AddIndexStr is a string related to the operation of "add index".
67-
const AddIndexStr = "add index"
68+
const (
69+
// AddIndexStr is a string related to the operation of "add index".
70+
AddIndexStr = "add index"
71+
AddPrimaryKeyStr = "add primary key"
72+
)
6873

6974
var actionMap = map[ActionType]string{
7075
ActionCreateSchema: "create schema",
@@ -98,6 +103,8 @@ var actionMap = map[ActionType]string{
98103
ActionRepairTable: "repair table",
99104
ActionSetTiFlashReplica: "set tiflash replica",
100105
ActionUpdateTiFlashReplicaStatus: "update tiflash replica status",
106+
ActionAddPrimaryKey: AddPrimaryKeyStr,
107+
ActionDropPrimaryKey: "drop primary key",
101108
}
102109

103110
// String return current ddl action in string

parser.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,7 @@ ConstraintElem:
25452545
c := &ast.Constraint{
25462546
Tp: ast.ConstraintPrimaryKey,
25472547
Keys: $5.([]*ast.IndexColName),
2548+
Name: $3.([]interface{})[0].(string),
25482549
}
25492550
if $7 != nil {
25502551
c.Option = $7.(*ast.IndexOption)

parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ func (s *testParserSuite) TestDDL(c *C) {
21132113
{"create table test (create_date TIMESTAMP NOT NULL COMMENT '创建日期 create date' DEFAULT now());", true, "CREATE TABLE `test` (`create_date` TIMESTAMP NOT NULL COMMENT '创建日期 create date' DEFAULT CURRENT_TIMESTAMP())"},
21142114
{"create table ts (t int, v timestamp(3) default CURRENT_TIMESTAMP(3));", true, "CREATE TABLE `ts` (`t` INT,`v` TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3))"}, //TODO: The number yacc in parentheses has not been implemented yet.
21152115
// Create table with primary key name.
2116-
{"create table if not exists `t` (`id` int not null auto_increment comment '消息ID', primary key `pk_id` (`id`) );", true, "CREATE TABLE IF NOT EXISTS `t` (`id` INT NOT NULL AUTO_INCREMENT COMMENT '消息ID',PRIMARY KEY(`id`))"},
2116+
{"create table if not exists `t` (`id` int not null auto_increment comment '消息ID', primary key `pk_id` (`id`) );", true, "CREATE TABLE IF NOT EXISTS `t` (`id` INT NOT NULL AUTO_INCREMENT COMMENT '消息ID',PRIMARY KEY `pk_id`(`id`))"},
21172117
// Create table with like.
21182118
{"create table a like b", true, "CREATE TABLE `a` LIKE `b`"},
21192119
{"create table a (id int REFERENCES a (id) ON delete NO ACTION )", true, "CREATE TABLE `a` (`id` INT REFERENCES `a`(`id`) ON DELETE NO ACTION)"},
@@ -2313,7 +2313,7 @@ func (s *testParserSuite) TestDDL(c *C) {
23132313
{"ALTER TABLE t ADD INDEX (a) USING RTREE COMMENT 'a'", true, "ALTER TABLE `t` ADD INDEX(`a`) USING RTREE COMMENT 'a'"},
23142314
{"ALTER TABLE t ADD KEY (a) USING HASH COMMENT 'a'", true, "ALTER TABLE `t` ADD INDEX(`a`) USING HASH COMMENT 'a'"},
23152315
{"ALTER TABLE t ADD KEY IF NOT EXISTS (a) USING HASH COMMENT 'a'", true, "ALTER TABLE `t` ADD INDEX IF NOT EXISTS(`a`) USING HASH COMMENT 'a'"},
2316-
{"ALTER TABLE t ADD PRIMARY KEY ident USING RTREE ( a DESC , b )", true, "ALTER TABLE `t` ADD PRIMARY KEY(`a`, `b`) USING RTREE"},
2316+
{"ALTER TABLE t ADD PRIMARY KEY ident USING RTREE ( a DESC , b )", true, "ALTER TABLE `t` ADD PRIMARY KEY `ident`(`a`, `b`) USING RTREE"},
23172317
{"ALTER TABLE t ADD KEY USING RTREE ( a ) ", true, "ALTER TABLE `t` ADD INDEX(`a`) USING RTREE"},
23182318
{"ALTER TABLE t ADD KEY USING RTREE ( ident ASC , ident ( 123 ) )", true, "ALTER TABLE `t` ADD INDEX(`ident`, `ident`(123)) USING RTREE"},
23192319
{"ALTER TABLE t ADD PRIMARY KEY (a) COMMENT 'a'", true, "ALTER TABLE `t` ADD PRIMARY KEY(`a`) COMMENT 'a'"},
@@ -2637,7 +2637,7 @@ func (s *testParserSuite) TestDDL(c *C) {
26372637
{"create table stats_auto_recalc (a int) stats_auto_recalc=1;", true, "CREATE TABLE `stats_auto_recalc` (`a` INT) STATS_AUTO_RECALC = 1"},
26382638

26392639
// for TYPE/USING syntax
2640-
{"create table t (a int, primary key type type btree (a));", true, "CREATE TABLE `t` (`a` INT,PRIMARY KEY(`a`) USING BTREE)"},
2640+
{"create table t (a int, primary key type type btree (a));", true, "CREATE TABLE `t` (`a` INT,PRIMARY KEY `type`(`a`) USING BTREE)"},
26412641
{"create table t (a int, primary key type btree (a));", false, ""},
26422642
{"create table t (a int, primary key using btree (a));", true, "CREATE TABLE `t` (`a` INT,PRIMARY KEY(`a`) USING BTREE)"},
26432643
{"create table t (a int, primary key (a) type btree);", true, "CREATE TABLE `t` (`a` INT,PRIMARY KEY(`a`) USING BTREE)"},

0 commit comments

Comments
 (0)