1111
1212import java .util .ArrayList ;
1313import java .util .Arrays ;
14+ import java .util .LinkedHashSet ;
1415import java .util .List ;
16+ import java .util .Set ;
17+
18+ import net .sf .jsqlparser .statement .ReferentialAction ;
19+ import net .sf .jsqlparser .statement .ReferentialAction .Action ;
20+ import net .sf .jsqlparser .statement .ReferentialAction .Type ;
1521import net .sf .jsqlparser .statement .create .table .ColDataType ;
1622import net .sf .jsqlparser .statement .create .table .ColumnDefinition ;
1723import net .sf .jsqlparser .statement .create .table .Index ;
@@ -23,7 +29,7 @@ public class AlterExpression {
2329 private String optionalSpecifier ;
2430 private String columnName ;
2531 private String columnOldName ;
26- //private ColDataType dataType;
32+ // private ColDataType dataType;
2733
2834 private List <ColumnDataType > colDataTypeList ;
2935 private List <ColumnDropNotNull > columnDropNotNullList ;
@@ -34,9 +40,9 @@ public class AlterExpression {
3440 private Index index = null ;
3541 private String constraintName ;
3642 private boolean constraintIfExists ;
37- private boolean onDeleteRestrict ;
38- private boolean onDeleteSetNull ;
39- private boolean onDeleteCascade ;
43+
44+ private Set < ReferentialAction > referentialActions = new LinkedHashSet <>( 2 ) ;
45+
4046 private List <String > fkColumns ;
4147 private String fkSourceTable ;
4248 private List <String > fkSourceColumns ;
@@ -71,28 +77,96 @@ public void setOptionalSpecifier(String optionalSpecifier) {
7177 this .optionalSpecifier = optionalSpecifier ;
7278 }
7379
80+ /**
81+ * @param type
82+ * @param action
83+ */
84+ public void setReferentialAction (Type type , Action action ) {
85+ setReferentialAction (type , action , true );
86+ }
87+
88+ /**
89+ * @param type
90+ */
91+ public void removeReferentialAction (Type type ) {
92+ setReferentialAction (type , null , false );
93+ }
94+
95+ /**
96+ * @param type
97+ * @return
98+ */
99+ public ReferentialAction getReferentialAction (Type type ) {
100+ return referentialActions .stream ().filter (ra -> type .equals (ra .getType ())).findFirst ().orElse (null );
101+ }
102+
103+ private void setReferentialAction (Type type , Action action , boolean set ) {
104+ ReferentialAction found = getReferentialAction (type );
105+ if (set ) {
106+ if (found == null ) {
107+ referentialActions .add (new ReferentialAction (type , action ));
108+ } else {
109+ found .setAction (action );
110+ }
111+ } else if (found != null ) {
112+ referentialActions .remove (found );
113+ }
114+ }
115+ /**
116+ * @return
117+ * @deprecated use {@link #getOnDeleteReferentialAction()}
118+ */
119+ @ Deprecated
74120 public boolean isOnDeleteCascade () {
75- return onDeleteCascade ;
121+ ReferentialAction found = getReferentialAction (Type .DELETE );
122+ return found != null && Action .CASCADE .equals (found .getAction ());
76123 }
77124
125+ /**
126+ * @return
127+ * @deprecated use {@link #setOnDeleteReferentialAction(Action)
128+ */
129+ @ Deprecated
78130 public void setOnDeleteCascade (boolean onDeleteCascade ) {
79- this . onDeleteCascade = onDeleteCascade ;
131+ setReferentialAction ( Type . DELETE , Action . CASCADE , onDeleteCascade ) ;
80132 }
81133
134+ /**
135+ * @return
136+ * @deprecated use {@link #getOnDeleteReferentialAction()}
137+ */
138+ @ Deprecated
82139 public boolean isOnDeleteRestrict () {
83- return onDeleteRestrict ;
140+ ReferentialAction found = getReferentialAction (Type .DELETE );
141+ return found != null && Action .RESTRICT .equals (found .getAction ());
84142 }
85143
144+ /**
145+ * @return
146+ * @deprecated use {@link #setOnDeleteReferentialAction(Action)
147+ */
148+ @ Deprecated
86149 public void setOnDeleteRestrict (boolean onDeleteRestrict ) {
87- this . onDeleteRestrict = onDeleteRestrict ;
150+ setReferentialAction ( Type . DELETE , Action . RESTRICT , onDeleteRestrict ) ;
88151 }
89152
153+ /**
154+ * @return
155+ * @deprecated use {@link #getOnDeleteReferentialAction()}
156+ */
157+ @ Deprecated
90158 public boolean isOnDeleteSetNull () {
91- return onDeleteSetNull ;
159+ ReferentialAction found = getReferentialAction (Type .DELETE );
160+ return found != null && Action .SET_NULL .equals (found .getAction ());
92161 }
93162
163+ /**
164+ * @return
165+ * @deprecated use {@link #setOnDeleteReferentialAction(Action)
166+ */
167+ @ Deprecated
94168 public void setOnDeleteSetNull (boolean onDeleteSetNull ) {
95- this . onDeleteSetNull = onDeleteSetNull ;
169+ setReferentialAction ( Type . DELETE , Action . SET_NULL , onDeleteSetNull ) ;
96170 }
97171
98172 public List <String > getFkColumns () {
@@ -121,14 +195,14 @@ public void addColDataType(String columnName, ColDataType colDataType) {
121195
122196 public void addColDataType (ColumnDataType columnDataType ) {
123197 if (colDataTypeList == null ) {
124- colDataTypeList = new ArrayList <ColumnDataType >();
198+ colDataTypeList = new ArrayList <>();
125199 }
126200 colDataTypeList .add (columnDataType );
127201 }
128202
129203 public void addColDropNotNull (ColumnDropNotNull columnDropNotNull ) {
130204 if (columnDropNotNullList == null ) {
131- columnDropNotNullList = new ArrayList <ColumnDropNotNull >();
205+ columnDropNotNullList = new ArrayList <>();
132206 }
133207 columnDropNotNullList .add (columnDropNotNull );
134208 }
@@ -219,7 +293,7 @@ public List<ColumnDropNotNull> getColumnDropNotNullList() {
219293
220294 public void addParameters (String ... params ) {
221295 if (parameters == null ) {
222- parameters = new ArrayList <String >();
296+ parameters = new ArrayList <>();
223297 }
224298 parameters .addAll (Arrays .asList (params ));
225299 }
@@ -312,16 +386,11 @@ public String toString() {
312386 }
313387 b .append (" (" ).append (PlainSelect .getStringList (ukColumns )).append (")" );
314388 } else if (fkColumns != null ) {
315- b .append ("FOREIGN KEY (" ).append (PlainSelect .getStringList (fkColumns )).
316- append (") REFERENCES " ).append (fkSourceTable ).append (" (" ).append (
317- PlainSelect .getStringList (fkSourceColumns )).append (")" );
318- if (isOnDeleteCascade ()) {
319- b .append (" ON DELETE CASCADE" );
320- } else if (isOnDeleteRestrict ()) {
321- b .append (" ON DELETE RESTRICT" );
322- } else if (isOnDeleteSetNull ()) {
323- b .append (" ON DELETE SET NULL" );
324- }
389+ b .append ("FOREIGN KEY (" ).append (PlainSelect .getStringList (fkColumns )).append (") REFERENCES " )
390+ .append (fkSourceTable ).append (" (" ).append (
391+ PlainSelect .getStringList (fkSourceColumns ))
392+ .append (")" );
393+ referentialActions .forEach (b ::append );
325394 } else if (index != null ) {
326395 b .append (index );
327396 }
0 commit comments