Skip to content

Commit 793d6d1

Browse files
authored
Merge pull request #29 from sjjian/alter_table_constraint_ast
support alter table constraint ast
2 parents b5a4d36 + a04f759 commit 793d6d1

File tree

6 files changed

+94
-22
lines changed

6 files changed

+94
-22
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ this is an oracle sql parser. ref: https://docs.oracle.com/en/database/oracle/or
88
|Alter table|Modify column| :heavy_check_mark:|:heavy_check_mark:|
99
|Alter table|Drop column| :heavy_check_mark:|:heavy_check_mark:|
1010
|Alter table|Rename column| :heavy_check_mark:|:heavy_check_mark:|
11-
|Alter table|Add constraint| :heavy_check_mark:| |
12-
|Alter table|Modify constraint| :heavy_check_mark:| |
13-
|Alter table|Rename constraint| :heavy_check_mark:| |
14-
|Alter table|Drop constraint| :heavy_check_mark:| |
11+
|Alter table|Add constraint| :heavy_check_mark:| :heavy_check_mark:|
12+
|Alter table|Modify constraint| :heavy_check_mark:| :heavy_check_mark:|
13+
|Alter table|Rename constraint| :heavy_check_mark:| :heavy_check_mark:|
14+
|Alter table|Drop constraint| :heavy_check_mark:| :heavy_check_mark:|
1515
|Create table|Relational table|:heavy_check_mark:|:heavy_check_mark:|
1616
|Create index|Relational table|:heavy_check_mark:| |
1717
|Drop table|-|:heavy_check_mark:|:heavy_check_mark:|

ast/base.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type ColumnDefault struct {
3939
type ConstraintType int
4040

4141
const (
42-
ConstraintTypeNull ConstraintType = iota
42+
ConstraintTypeDefault ConstraintType = iota
43+
ConstraintTypeNull
4344
ConstraintTypeNotNull
4445
ConstraintTypeUnique
4546
ConstraintTypePK

ast/ddl.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ type RenameColumnClause struct {
4949

5050
type AddConstraintClause struct {
5151
alterTableClause
52+
Constraints []*OutOfLineConstraint
5253
}
5354

5455
type ModifyConstraintClause struct {
5556
alterTableClause
57+
Constraint *OutOfLineConstraint
5658
}
5759

5860
type RenameConstraintClause struct {
5961
alterTableClause
62+
OldName *element.Identifier
63+
NewName *element.Identifier
6064
}
6165

6266
type DropConstraintClause struct {
6367
alterTableClause
68+
Constraint *OutOfLineConstraint
6469
}
6570

6671
/*
@@ -132,4 +137,4 @@ type DropTableStmt struct {
132137
type DropIndexStmt struct {
133138
node
134139
IndexName *IndexName
135-
}
140+
}

sql.go

+39-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql.y

+39-8
Original file line numberDiff line numberDiff line change
@@ -1162,24 +1162,42 @@ RenameColumnClause:
11621162
ConstraintClauses:
11631163
_add OutOfLineConstraint // Note: in docs is _add OutOfLineConstraints, but actual is _add OutOfLineConstraint.
11641164
{
1165-
$$ = []ast.AlterTableClause{&ast.AddConstraintClause{}}
1165+
$$ = []ast.AlterTableClause{&ast.AddConstraintClause{
1166+
Constraints: []*ast.OutOfLineConstraint{$2.(*ast.OutOfLineConstraint)},
1167+
}}
11661168
}
11671169
//| _add OutOfLineRefConstraint
11681170
| _modify _constraint Identifier ConstraintState CascadeOrEmpty
11691171
{
1170-
$$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{}}
1172+
constraint := &ast.OutOfLineConstraint{}
1173+
constraint.Name = $3.(*element.Identifier)
1174+
$$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{
1175+
Constraint: constraint,
1176+
}}
11711177
}
11721178
| _modify _primary _key ConstraintState CascadeOrEmpty
11731179
{
1174-
$$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{}}
1180+
constraint := &ast.OutOfLineConstraint{}
1181+
constraint.Type = ast.ConstraintTypePK
1182+
$$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{
1183+
Constraint: constraint,
1184+
}}
11751185
}
11761186
| _modify _unique '(' ColumnNameList ')' ConstraintState CascadeOrEmpty
11771187
{
1178-
$$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{}}
1188+
constraint := &ast.OutOfLineConstraint{}
1189+
constraint.Type = ast.ConstraintTypeUnique
1190+
constraint.Columns = $4.([]*element.Identifier)
1191+
$$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{
1192+
Constraint: constraint,
1193+
}}
11791194
}
11801195
| _rename _constraint Identifier _to Identifier
11811196
{
1182-
$$ = []ast.AlterTableClause{&ast.RenameConstraintClause{}}
1197+
$$ = []ast.AlterTableClause{&ast.RenameConstraintClause{
1198+
OldName: $3.(*element.Identifier),
1199+
NewName: $5.(*element.Identifier),
1200+
}}
11831201
}
11841202
| DropConstraintClauses
11851203
{
@@ -1203,15 +1221,28 @@ DropConstraintClauses:
12031221
DropConstraintClause:
12041222
_drop _primary _key CascadeOrEmpty DropConstraintProps
12051223
{
1206-
$$ = &ast.DropConstraintClause{}
1224+
constraint := &ast.OutOfLineConstraint{}
1225+
constraint.Type = ast.ConstraintTypePK
1226+
$$ = &ast.DropConstraintClause{
1227+
Constraint: constraint,
1228+
}
12071229
}
12081230
| _drop _unique '(' ColumnNameList ')' CascadeOrEmpty DropConstraintProps
12091231
{
1210-
$$ = &ast.DropConstraintClause{}
1232+
constraint := &ast.OutOfLineConstraint{}
1233+
constraint.Type = ast.ConstraintTypeUnique
1234+
constraint.Columns = $4.([]*element.Identifier)
1235+
$$ = &ast.DropConstraintClause{
1236+
Constraint: constraint,
1237+
}
12111238
}
12121239
| _drop _constraint Identifier CascadeOrEmpty DropConstraintProps
12131240
{
1214-
$$ = &ast.DropConstraintClause{}
1241+
constraint := &ast.OutOfLineConstraint{}
1242+
constraint.Name = $3.(*element.Identifier)
1243+
$$ = &ast.DropConstraintClause{
1244+
Constraint: constraint,
1245+
}
12151246
}
12161247

12171248
CascadeOrEmpty:

test/alter_table_constraint.sql

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ alter table db1.table1 modify primary key using index;
1616

1717
alter table db1.table1 modify primary key rely using index idx_1 enable validate;
1818

19+
alter table db1.table1 modify constraint idx_1 using index;
20+
21+
alter table db1.table1 modify unique(name,age) using index;
22+
1923
alter table db1.table1 drop primary key keep index;
2024

2125
alter table db1.table1 drop primary key keep index drop unique(name,age) drop index;

0 commit comments

Comments
 (0)