Skip to content

Commit

Permalink
Merge pull request #29 from sjjian/alter_table_constraint_ast
Browse files Browse the repository at this point in the history
support alter table constraint ast
  • Loading branch information
sjjian authored Dec 24, 2021
2 parents b5a4d36 + a04f759 commit 793d6d1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:|
Expand Down
3 changes: 2 additions & 1 deletion ast/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type ColumnDefault struct {
type ConstraintType int

const (
ConstraintTypeNull ConstraintType = iota
ConstraintTypeDefault ConstraintType = iota
ConstraintTypeNull
ConstraintTypeNotNull
ConstraintTypeUnique
ConstraintTypePK
Expand Down
7 changes: 6 additions & 1 deletion ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/*
Expand Down Expand Up @@ -132,4 +137,4 @@ type DropTableStmt struct {
type DropIndexStmt struct {
node
IndexName *IndexName
}
}
47 changes: 39 additions & 8 deletions sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 39 additions & 8 deletions sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions test/alter_table_constraint.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 793d6d1

Please sign in to comment.