11
11
12
12
import java .util .ArrayList ;
13
13
import java .util .Arrays ;
14
+ import java .util .LinkedHashSet ;
14
15
import 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 ;
15
21
import net .sf .jsqlparser .statement .create .table .ColDataType ;
16
22
import net .sf .jsqlparser .statement .create .table .ColumnDefinition ;
17
23
import net .sf .jsqlparser .statement .create .table .Index ;
@@ -23,7 +29,7 @@ public class AlterExpression {
23
29
private String optionalSpecifier ;
24
30
private String columnName ;
25
31
private String columnOldName ;
26
- //private ColDataType dataType;
32
+ // private ColDataType dataType;
27
33
28
34
private List <ColumnDataType > colDataTypeList ;
29
35
private List <ColumnDropNotNull > columnDropNotNullList ;
@@ -34,9 +40,9 @@ public class AlterExpression {
34
40
private Index index = null ;
35
41
private String constraintName ;
36
42
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
+
40
46
private List <String > fkColumns ;
41
47
private String fkSourceTable ;
42
48
private List <String > fkSourceColumns ;
@@ -71,28 +77,96 @@ public void setOptionalSpecifier(String optionalSpecifier) {
71
77
this .optionalSpecifier = optionalSpecifier ;
72
78
}
73
79
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
74
120
public boolean isOnDeleteCascade () {
75
- return onDeleteCascade ;
121
+ ReferentialAction found = getReferentialAction (Type .DELETE );
122
+ return found != null && Action .CASCADE .equals (found .getAction ());
76
123
}
77
124
125
+ /**
126
+ * @return
127
+ * @deprecated use {@link #setOnDeleteReferentialAction(Action)
128
+ */
129
+ @ Deprecated
78
130
public void setOnDeleteCascade (boolean onDeleteCascade ) {
79
- this . onDeleteCascade = onDeleteCascade ;
131
+ setReferentialAction ( Type . DELETE , Action . CASCADE , onDeleteCascade ) ;
80
132
}
81
133
134
+ /**
135
+ * @return
136
+ * @deprecated use {@link #getOnDeleteReferentialAction()}
137
+ */
138
+ @ Deprecated
82
139
public boolean isOnDeleteRestrict () {
83
- return onDeleteRestrict ;
140
+ ReferentialAction found = getReferentialAction (Type .DELETE );
141
+ return found != null && Action .RESTRICT .equals (found .getAction ());
84
142
}
85
143
144
+ /**
145
+ * @return
146
+ * @deprecated use {@link #setOnDeleteReferentialAction(Action)
147
+ */
148
+ @ Deprecated
86
149
public void setOnDeleteRestrict (boolean onDeleteRestrict ) {
87
- this . onDeleteRestrict = onDeleteRestrict ;
150
+ setReferentialAction ( Type . DELETE , Action . RESTRICT , onDeleteRestrict ) ;
88
151
}
89
152
153
+ /**
154
+ * @return
155
+ * @deprecated use {@link #getOnDeleteReferentialAction()}
156
+ */
157
+ @ Deprecated
90
158
public boolean isOnDeleteSetNull () {
91
- return onDeleteSetNull ;
159
+ ReferentialAction found = getReferentialAction (Type .DELETE );
160
+ return found != null && Action .SET_NULL .equals (found .getAction ());
92
161
}
93
162
163
+ /**
164
+ * @return
165
+ * @deprecated use {@link #setOnDeleteReferentialAction(Action)
166
+ */
167
+ @ Deprecated
94
168
public void setOnDeleteSetNull (boolean onDeleteSetNull ) {
95
- this . onDeleteSetNull = onDeleteSetNull ;
169
+ setReferentialAction ( Type . DELETE , Action . SET_NULL , onDeleteSetNull ) ;
96
170
}
97
171
98
172
public List <String > getFkColumns () {
@@ -121,14 +195,14 @@ public void addColDataType(String columnName, ColDataType colDataType) {
121
195
122
196
public void addColDataType (ColumnDataType columnDataType ) {
123
197
if (colDataTypeList == null ) {
124
- colDataTypeList = new ArrayList <ColumnDataType >();
198
+ colDataTypeList = new ArrayList <>();
125
199
}
126
200
colDataTypeList .add (columnDataType );
127
201
}
128
202
129
203
public void addColDropNotNull (ColumnDropNotNull columnDropNotNull ) {
130
204
if (columnDropNotNullList == null ) {
131
- columnDropNotNullList = new ArrayList <ColumnDropNotNull >();
205
+ columnDropNotNullList = new ArrayList <>();
132
206
}
133
207
columnDropNotNullList .add (columnDropNotNull );
134
208
}
@@ -219,7 +293,7 @@ public List<ColumnDropNotNull> getColumnDropNotNullList() {
219
293
220
294
public void addParameters (String ... params ) {
221
295
if (parameters == null ) {
222
- parameters = new ArrayList <String >();
296
+ parameters = new ArrayList <>();
223
297
}
224
298
parameters .addAll (Arrays .asList (params ));
225
299
}
@@ -312,16 +386,11 @@ public String toString() {
312
386
}
313
387
b .append (" (" ).append (PlainSelect .getStringList (ukColumns )).append (")" );
314
388
} 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 );
325
394
} else if (index != null ) {
326
395
b .append (index );
327
396
}
0 commit comments