File tree Expand file tree Collapse file tree 3 files changed +42
-1
lines changed
java/net/sf/jsqlparser/statement/alter
jjtree/net/sf/jsqlparser/parser
test/java/net/sf/jsqlparser/test/alter Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,26 @@ public void setOnDeleteCascade(boolean onDeleteCascade) {
51
51
this .onDeleteCascade = onDeleteCascade ;
52
52
}
53
53
54
+ private boolean onDeleteRestrict ;
55
+
56
+ public boolean isOnDeleteRestrict () {
57
+ return onDeleteRestrict ;
58
+ }
59
+
60
+ public void setOnDeleteRestrict (boolean onDeleteRestrict ) {
61
+ this .onDeleteRestrict = onDeleteRestrict ;
62
+ }
63
+
64
+ private boolean onDeleteSetNull ;
65
+
66
+ public boolean isOnDeleteSetNull () {
67
+ return onDeleteSetNull ;
68
+ }
69
+
70
+ public void setOnDeleteSetNull (boolean onDeleteSetNull ) {
71
+ this .onDeleteSetNull = onDeleteSetNull ;
72
+ }
73
+
54
74
private List <String > fkColumns ;
55
75
56
76
public List <String > getFkColumns () {
@@ -148,6 +168,10 @@ public String toString() {
148
168
b .append ("FOREIGN KEY (" ).append (PlainSelect .getStringList (fkColumns )).append (") REFERENCES " ).append (fkSourceTable ).append (" (" ).append (PlainSelect .getStringList (fkSourceColumns )).append (")" );
149
169
if (isOnDeleteCascade ()) {
150
170
b .append (" ON DELETE CASCADE" );
171
+ } else if (isOnDeleteRestrict ()) {
172
+ b .append (" ON DELETE RESTRICT" );
173
+ } else if (isOnDeleteSetNull ()) {
174
+ b .append (" ON DELETE SET NULL" );
151
175
}
152
176
}
153
177
return b .toString ();
Original file line number Diff line number Diff line change @@ -226,6 +226,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
226
226
| <K_MERGE: "MERGE">
227
227
| <K_MATCHED: "MATCHED">
228
228
| <K_CASCADE: "CASCADE">
229
+ | <K_RESTRICT: "RESTRICT">
229
230
}
230
231
231
232
TOKEN : /* Numeric Constants */
@@ -2771,7 +2772,11 @@ Alter Alter():
2771
2772
( <K_FOREIGN> <K_KEY> columnNames=ColumnsNamesList() { alter.setFkColumns(columnNames); }
2772
2773
<K_REFERENCES> tk=<S_IDENTIFIER> columnNames=ColumnsNamesList()
2773
2774
{ alter.setFkSourceTable(tk.image); alter.setFkSourceColumns(columnNames); }
2774
- [<K_ON> <K_DELETE> <K_CASCADE> { alter.setOnDeleteCascade(true); } ] )
2775
+ [<K_ON> <K_DELETE>
2776
+ (<K_CASCADE> { alter.setOnDeleteCascade(true); }
2777
+ | <K_RESTRICT> { alter.setOnDeleteRestrict(true); }
2778
+ | <K_SET> <K_NULL> { alter.setOnDeleteSetNull(true); } ) ]
2779
+ )
2775
2780
)
2776
2781
2777
2782
{
Original file line number Diff line number Diff line change @@ -34,4 +34,16 @@ public void testAlterTableUniqueKey() throws JSQLParserException {
34
34
public void testAlterTableForgeignKey () throws JSQLParserException {
35
35
assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id) ON DELETE CASCADE" );
36
36
}
37
+
38
+ public void testAlterTableForgeignKey2 () throws JSQLParserException {
39
+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id)" );
40
+ }
41
+
42
+ public void testAlterTableForgeignKey3 () throws JSQLParserException {
43
+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id) ON DELETE RESTRICT" );
44
+ }
45
+
46
+ public void testAlterTableForgeignKey4 () throws JSQLParserException {
47
+ assertSqlCanBeParsedAndDeparsed ("ALTER TABLE test ADD FOREIGN KEY (user_id) REFERENCES ra_user (id) ON DELETE SET NULL" );
48
+ }
37
49
}
You can’t perform that action at this time.
0 commit comments