Skip to content

Commit

Permalink
Support ON DELETE clause in table foreign constraint (cloudspannereco…
Browse files Browse the repository at this point in the history
…system#59)

* Support ON DELETE clause in table foreign constraint

* Ran mvn spotless:apply
  • Loading branch information
gurminder71 authored Jan 25, 2024
1 parent f7004c9 commit d098c60
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public List<String> getReferencedColumnNames() {
(ASTidentifier_list) ((ASTreferenced_columns) children[child]).children[0]);
}

public String getDeleteOption() {
ASTon_delete deleteOption = ASTTreeUtils.getOptionalChildByType(children, ASTon_delete.class);
if (deleteOption != null) {
return " " + deleteOption;
} else {
return "";
}
}

public String toString() {

return "CONSTRAINT "
Expand All @@ -79,7 +88,8 @@ public String toString() {
+ getReferencedTableName()
+ " ("
+ Joiner.on(", ").join(getReferencedColumnNames())
+ ")";
+ ")"
+ getDeleteOption();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.solutions.spannerddl.parser;

public class ASTon_delete extends SimpleNode {
public ASTon_delete(int id) {
super(id);
}

public ASTon_delete(DdlParser p, int id) {
super(p, id);
}

@Override
public String toString() {
if (children[0] instanceof ASTreferential_action) {
return "ON DELETE " + children[0];
} else {
throw new UnsupportedOperationException("Not Implemented");
}
}

@Override
public boolean equals(Object other) {
return (other instanceof ASTon_delete && this.toString().equals(other.toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.solutions.spannerddl.parser;

import com.google.cloud.solutions.spannerddl.diff.ASTTreeUtils;

public class ASTreferential_action extends SimpleNode {
public ASTreferential_action(int id) {
super(id);
}

public ASTreferential_action(DdlParser p, int id) {
super(p, id);
}

@Override
public String toString() {
return ASTTreeUtils.tokensToString(this);
}

@Override
public boolean equals(Object other) {
return (other instanceof ASTreferential_action && this.toString().equals(other.toString()));
}
}
7 changes: 7 additions & 0 deletions src/main/jjtree-sources/ddl_parser.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,15 @@ void foreign_key() :
"(" identifier_list() #referencing_columns ")"
<REFERENCES> qualified_identifier() #referenced_table
"(" identifier_list() #referenced_columns ")"
[ <ON> <DELETE> referential_action() #on_delete ]
}

void referential_action() :
{}
{
<NO> <ACTION> #no_action
| <CASCADE> #cascade
}

void statement_token_no_paren() :
{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void parseCreateTable() throws ParseException {
+ "jsoncol json, "
+ "pgcolumn pg.something, "
+ "generatedcol string(max) as (sizedstring+ strstr(maxstring,strpos(maxstring,'xxx'),length(maxstring)) +2.0) STORED, "
+ "constraint fk_col_remote FOREIGN KEY(col1, col2) REFERENCES test.other_table(other_col1, other_col2), "
+ "constraint fk_col_remote FOREIGN KEY(col1, col2) REFERENCES test.other_table(other_col1, other_col2) on delete cascade, "
+ "constraint fk_col_remote2 FOREIGN KEY(col1) REFERENCES test.other_table(other_col1) on delete no action, "
+ "constraint check_some_value CHECK ((length(sizedstring)>100 or sizedstring= \"xxx\") AND boolcol= true and intcol > -123.4 and numericcol < 1.5)"
+ ") "
+ "primary key (intcol ASC, floatcol desc, boolcol), "
Expand All @@ -70,7 +71,8 @@ public void parseCreateTable() throws ParseException {
+ "jsoncol JSON, "
+ "pgcolumn PG.SOMETHING, "
+ "generatedcol STRING(MAX) AS ( sizedstring + strstr ( maxstring, strpos ( maxstring, 'xxx' ), length ( maxstring ) ) + 2.0 ) STORED, "
+ "CONSTRAINT fk_col_remote FOREIGN KEY (col1, col2) REFERENCES test.other_table (other_col1, other_col2), "
+ "CONSTRAINT fk_col_remote FOREIGN KEY (col1, col2) REFERENCES test.other_table (other_col1, other_col2) ON DELETE CASCADE, "
+ "CONSTRAINT fk_col_remote2 FOREIGN KEY (col1) REFERENCES test.other_table (other_col1) ON DELETE NO ACTION, "
+ "CONSTRAINT check_some_value CHECK (( length ( sizedstring ) > 100 OR sizedstring = \"xxx\" ) AND boolcol = TRUE AND intcol > -123.4 AND numericcol < 1.5)"
+ ") PRIMARY KEY (intcol ASC, floatcol DESC, boolcol ASC), "
+ "INTERLEAVE IN PARENT `other_table` ON DELETE CASCADE, "
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/expectedDdlDiff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ ALTER DATABASE dbname SET OPTIONS (nothing=NULL,othervalue=456)

ALTER DATABASE dbname SET OPTIONS (othervalue=456,removeme=NULL)

== TEST 42 add foreign key in table with delete cascade

ALTER TABLE test1 ADD CONSTRAINT fk_in_table FOREIGN KEY (col2) REFERENCES othertable (othercol) ON DELETE CASCADE

== TEST 43 add foreign key in table with delete no action

ALTER TABLE test1 ADD CONSTRAINT fk_in_table FOREIGN KEY (col2) REFERENCES othertable (othercol) ON DELETE NO ACTION

==


Expand Down
16 changes: 15 additions & 1 deletion src/test/resources/newDdl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,22 @@ ALTER DATABASE dbname SET OPTIONS(hello='world');

ALTER DATABASE dbname SET OPTIONS(hello='world', othervalue=456);

==
== TEST 42 add foreign key in table with delete cascade

create table test1 (
col1 int64,
col2 int64 NOT NULL,
constraint fk_in_table foreign key (col2) references othertable(othercol) ON DELETE CASCADE
)
primary key (col1);

== TEST 43 add foreign key in table with delete no action

create table test1 (
col1 int64,
col2 int64 NOT NULL,
constraint fk_in_table foreign key (col2) references othertable(othercol) ON DELETE NO ACTION
)
primary key (col1);

==
16 changes: 16 additions & 0 deletions src/test/resources/originalDdl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,22 @@ ALTER DATABASE dbname SET OPTIONS(hello='world');

ALTER DATABASE dbname SET OPTIONS(hello='world', othervalue=123, removeme='please');

== TEST 42 add foreign key in table with delete cascade

create table test1 (
col1 int64,
col2 int64 NOT NULL
)
primary key (col1);

== TEST 43 add foreign key in table with delete no action

create table test1 (
col1 int64,
col2 int64 NOT NULL
)
primary key (col1);

==


Expand Down

0 comments on commit d098c60

Please sign in to comment.