From a04f75943743778a4664e7619ca7edc1ca15b2f4 Mon Sep 17 00:00:00 2001 From: sjjian <921465802@qq.com> Date: Fri, 24 Dec 2021 16:36:58 +0800 Subject: [PATCH] support alter table constraint ast --- README.md | 8 +++--- ast/base.go | 3 ++- ast/ddl.go | 7 ++++- sql.go | 47 +++++++++++++++++++++++++++------ sql.y | 47 +++++++++++++++++++++++++++------ test/alter_table_constraint.sql | 4 +++ 6 files changed, 94 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c86bad4..b7072fd 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ this is an oracle sql parser. ref: https://docs.oracle.com/en/database/oracle/or |Alter table|Modify column| :heavy_check_mark:|:heavy_check_mark:| |Alter table|Drop column| :heavy_check_mark:|:heavy_check_mark:| |Alter table|Rename column| :heavy_check_mark:|:heavy_check_mark:| -|Alter table|Add constraint| :heavy_check_mark:| | -|Alter table|Modify constraint| :heavy_check_mark:| | -|Alter table|Rename constraint| :heavy_check_mark:| | -|Alter table|Drop constraint| :heavy_check_mark:| | +|Alter table|Add constraint| :heavy_check_mark:| :heavy_check_mark:| +|Alter table|Modify constraint| :heavy_check_mark:| :heavy_check_mark:| +|Alter table|Rename constraint| :heavy_check_mark:| :heavy_check_mark:| +|Alter table|Drop constraint| :heavy_check_mark:| :heavy_check_mark:| |Create table|Relational table|:heavy_check_mark:|:heavy_check_mark:| |Create index|Relational table|:heavy_check_mark:| | |Drop table|-|:heavy_check_mark:|:heavy_check_mark:| diff --git a/ast/base.go b/ast/base.go index 29c3b95..d80ee82 100644 --- a/ast/base.go +++ b/ast/base.go @@ -39,7 +39,8 @@ type ColumnDefault struct { type ConstraintType int const ( - ConstraintTypeNull ConstraintType = iota + ConstraintTypeDefault ConstraintType = iota + ConstraintTypeNull ConstraintTypeNotNull ConstraintTypeUnique ConstraintTypePK diff --git a/ast/ddl.go b/ast/ddl.go index 92cadf9..ffcca4b 100644 --- a/ast/ddl.go +++ b/ast/ddl.go @@ -49,18 +49,23 @@ type RenameColumnClause struct { type AddConstraintClause struct { alterTableClause + Constraints []*OutOfLineConstraint } type ModifyConstraintClause struct { alterTableClause + Constraint *OutOfLineConstraint } type RenameConstraintClause struct { alterTableClause + OldName *element.Identifier + NewName *element.Identifier } type DropConstraintClause struct { alterTableClause + Constraint *OutOfLineConstraint } /* @@ -132,4 +137,4 @@ type DropTableStmt struct { type DropIndexStmt struct { node IndexName *IndexName -} \ No newline at end of file +} diff --git a/sql.go b/sql.go index f25644b..60cc456 100644 --- a/sql.go +++ b/sql.go @@ -3747,23 +3747,41 @@ yynewstate: } case 303: { - yyVAL.anything = []ast.AlterTableClause{&ast.AddConstraintClause{}} + yyVAL.anything = []ast.AlterTableClause{&ast.AddConstraintClause{ + Constraints: []*ast.OutOfLineConstraint{yyS[yypt-0].anything.(*ast.OutOfLineConstraint)}, + }} } case 304: { - yyVAL.anything = []ast.AlterTableClause{&ast.ModifyConstraintClause{}} + constraint := &ast.OutOfLineConstraint{} + constraint.Name = yyS[yypt-2].anything.(*element.Identifier) + yyVAL.anything = []ast.AlterTableClause{&ast.ModifyConstraintClause{ + Constraint: constraint, + }} } case 305: { - yyVAL.anything = []ast.AlterTableClause{&ast.ModifyConstraintClause{}} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypePK + yyVAL.anything = []ast.AlterTableClause{&ast.ModifyConstraintClause{ + Constraint: constraint, + }} } case 306: { - yyVAL.anything = []ast.AlterTableClause{&ast.ModifyConstraintClause{}} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypeUnique + constraint.Columns = yyS[yypt-3].anything.([]*element.Identifier) + yyVAL.anything = []ast.AlterTableClause{&ast.ModifyConstraintClause{ + Constraint: constraint, + }} } case 307: { - yyVAL.anything = []ast.AlterTableClause{&ast.RenameConstraintClause{}} + yyVAL.anything = []ast.AlterTableClause{&ast.RenameConstraintClause{ + OldName: yyS[yypt-2].anything.(*element.Identifier), + NewName: yyS[yypt-0].anything.(*element.Identifier), + }} } case 308: { @@ -3779,15 +3797,28 @@ yynewstate: } case 311: { - yyVAL.anything = &ast.DropConstraintClause{} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypePK + yyVAL.anything = &ast.DropConstraintClause{ + Constraint: constraint, + } } case 312: { - yyVAL.anything = &ast.DropConstraintClause{} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypeUnique + constraint.Columns = yyS[yypt-3].anything.([]*element.Identifier) + yyVAL.anything = &ast.DropConstraintClause{ + Constraint: constraint, + } } case 313: { - yyVAL.anything = &ast.DropConstraintClause{} + constraint := &ast.OutOfLineConstraint{} + constraint.Name = yyS[yypt-2].anything.(*element.Identifier) + yyVAL.anything = &ast.DropConstraintClause{ + Constraint: constraint, + } } case 314: { diff --git a/sql.y b/sql.y index a89938f..6f3312c 100644 --- a/sql.y +++ b/sql.y @@ -1162,24 +1162,42 @@ RenameColumnClause: ConstraintClauses: _add OutOfLineConstraint // Note: in docs is _add OutOfLineConstraints, but actual is _add OutOfLineConstraint. { - $$ = []ast.AlterTableClause{&ast.AddConstraintClause{}} + $$ = []ast.AlterTableClause{&ast.AddConstraintClause{ + Constraints: []*ast.OutOfLineConstraint{$2.(*ast.OutOfLineConstraint)}, + }} } //| _add OutOfLineRefConstraint | _modify _constraint Identifier ConstraintState CascadeOrEmpty { - $$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{}} + constraint := &ast.OutOfLineConstraint{} + constraint.Name = $3.(*element.Identifier) + $$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{ + Constraint: constraint, + }} } | _modify _primary _key ConstraintState CascadeOrEmpty { - $$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{}} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypePK + $$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{ + Constraint: constraint, + }} } | _modify _unique '(' ColumnNameList ')' ConstraintState CascadeOrEmpty { - $$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{}} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypeUnique + constraint.Columns = $4.([]*element.Identifier) + $$ = []ast.AlterTableClause{&ast.ModifyConstraintClause{ + Constraint: constraint, + }} } | _rename _constraint Identifier _to Identifier { - $$ = []ast.AlterTableClause{&ast.RenameConstraintClause{}} + $$ = []ast.AlterTableClause{&ast.RenameConstraintClause{ + OldName: $3.(*element.Identifier), + NewName: $5.(*element.Identifier), + }} } | DropConstraintClauses { @@ -1203,15 +1221,28 @@ DropConstraintClauses: DropConstraintClause: _drop _primary _key CascadeOrEmpty DropConstraintProps { - $$ = &ast.DropConstraintClause{} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypePK + $$ = &ast.DropConstraintClause{ + Constraint: constraint, + } } | _drop _unique '(' ColumnNameList ')' CascadeOrEmpty DropConstraintProps { - $$ = &ast.DropConstraintClause{} + constraint := &ast.OutOfLineConstraint{} + constraint.Type = ast.ConstraintTypeUnique + constraint.Columns = $4.([]*element.Identifier) + $$ = &ast.DropConstraintClause{ + Constraint: constraint, + } } | _drop _constraint Identifier CascadeOrEmpty DropConstraintProps { - $$ = &ast.DropConstraintClause{} + constraint := &ast.OutOfLineConstraint{} + constraint.Name = $3.(*element.Identifier) + $$ = &ast.DropConstraintClause{ + Constraint: constraint, + } } CascadeOrEmpty: diff --git a/test/alter_table_constraint.sql b/test/alter_table_constraint.sql index 66bff71..29244d4 100644 --- a/test/alter_table_constraint.sql +++ b/test/alter_table_constraint.sql @@ -16,6 +16,10 @@ alter table db1.table1 modify primary key using index; alter table db1.table1 modify primary key rely using index idx_1 enable validate; +alter table db1.table1 modify constraint idx_1 using index; + +alter table db1.table1 modify unique(name,age) using index; + alter table db1.table1 drop primary key keep index; alter table db1.table1 drop primary key keep index drop unique(name,age) drop index; \ No newline at end of file