2222package net .sf .jsqlparser .statement .alter ;
2323
2424import java .util .ArrayList ;
25- import java .util .List ;
25+ import java .util .Iterator ;
26+
2627import net .sf .jsqlparser .schema .Table ;
2728import net .sf .jsqlparser .statement .Statement ;
2829import net .sf .jsqlparser .statement .StatementVisitor ;
3233
3334/**
3435 *
35- * @author toben
36+ * @author toben & wrobstory
3637 */
3738public class Alter implements Statement {
3839
3940 private Table table ;
40- private String columnName ;
41- //private ColDataType dataType;
42-
43- private List <ColumnDataType > colDataTypeList ;
44-
45- private List <String > pkColumns ;
46- private List <String > ukColumns ;
47- private String ukName ;
48- private Index index = null ;
49- private AlterOperation operation ;
50- private String constraintName ;
51- private boolean onDeleteRestrict ;
52- private boolean onDeleteSetNull ;
53- private boolean onDeleteCascade ;
54- private List <String > fkColumns ;
55- private String fkSourceTable ;
56- private List <String > fkSourceColumns ;
57-
58- public boolean isOnDeleteCascade () {
59- return onDeleteCascade ;
60- }
6141
62- public void setOnDeleteCascade (boolean onDeleteCascade ) {
63- this .onDeleteCascade = onDeleteCascade ;
64- }
65-
66- public boolean isOnDeleteRestrict () {
67- return onDeleteRestrict ;
68- }
69-
70- public void setOnDeleteRestrict (boolean onDeleteRestrict ) {
71- this .onDeleteRestrict = onDeleteRestrict ;
72- }
73-
74- public boolean isOnDeleteSetNull () {
75- return onDeleteSetNull ;
76- }
77-
78- public void setOnDeleteSetNull (boolean onDeleteSetNull ) {
79- this .onDeleteSetNull = onDeleteSetNull ;
80- }
81-
82- public List <String > getFkColumns () {
83- return fkColumns ;
84- }
85-
86- public void setFkColumns (List <String > fkColumns ) {
87- this .fkColumns = fkColumns ;
88- }
89-
90- public String getFkSourceTable () {
91- return fkSourceTable ;
92- }
93-
94- public void setFkSourceTable (String fkSourceTable ) {
95- this .fkSourceTable = fkSourceTable ;
96- }
97-
98- public List <ColumnDataType > getColDataTypeList () {
99- return colDataTypeList ;
100- }
101-
102- public void addColDataType (String columnName , ColDataType colDataType ) {
103- if (colDataTypeList == null ) {
104- colDataTypeList = new ArrayList <ColumnDataType >();
105- }
106- colDataTypeList .add (new ColumnDataType (columnName , colDataType ));
107- }
108-
109- public AlterOperation getOperation () {
110- return operation ;
111- }
112-
113- public void setOperation (AlterOperation operation ) {
114- this .operation = operation ;
115- }
116-
117- public List <String > getFkSourceColumns () {
118- return fkSourceColumns ;
119- }
120-
121- public void setFkSourceColumns (List <String > fkSourceColumns ) {
122- this .fkSourceColumns = fkSourceColumns ;
123- }
42+ private ArrayList <AlterExpression > alterExpressions ;
12443
12544 public Table getTable () {
12645 return table ;
@@ -130,116 +49,45 @@ public void setTable(Table table) {
13049 this .table = table ;
13150 }
13251
133- public String getColumnName () {
134- return columnName ;
135- }
136-
137- public void setColumnName (String columnName ) {
138- this .columnName = columnName ;
139- }
140-
141- public String getConstraintName () {
142- return this .constraintName ;
143- }
144-
145- public void setConstraintName (final String constraintName ) {
146- this .constraintName = constraintName ;
147- }
148-
149- public List <String > getPkColumns () {
150- return pkColumns ;
151- }
152-
153- public void setPkColumns (List <String > pkColumns ) {
154- this .pkColumns = pkColumns ;
155- }
156-
157- public List <String > getUkColumns () {
158- return ukColumns ;
159- }
160-
161- public void setUkColumns (List <String > ukColumns ) {
162- this .ukColumns = ukColumns ;
52+ public void addAlterExpression (AlterExpression alterExpression ){
53+ if (alterExpressions == null ) {
54+ alterExpressions = new ArrayList <AlterExpression >();
55+ }
56+ alterExpressions .add (alterExpression );
16357 }
16458
165- public String getUkName () {
166- return ukName ;
59+ public ArrayList < AlterExpression > getAlterExpressions () {
60+ return alterExpressions ;
16761 }
16862
169- public void setUkName ( String ukName ) {
170- this .ukName = ukName ;
63+ public void setAlterExpressions ( ArrayList < AlterExpression > alterExpressions ) {
64+ this .alterExpressions = alterExpressions ;
17165 }
17266
17367 @ Override
17468 public void accept (StatementVisitor statementVisitor ) {
17569 statementVisitor .visit (this );
17670 }
17771
178- public Index getIndex () {
179- return index ;
180- }
181-
182- public void setIndex (Index index ) {
183- this .index = index ;
184- }
185-
18672 @ Override
18773 public String toString () {
188- StringBuilder b = new StringBuilder ();
189- b .append ("ALTER TABLE " ).append (table .getFullyQualifiedName ()).append (" " ).append (operation ).append (" " );
190- if (columnName != null ) {
191- b .append ("COLUMN " ).append (columnName );
192- } else if (colDataTypeList != null ) {
193- if (colDataTypeList .size () > 1 ) {
194- b .append ("(" );
195- } else b .append ("COLUMN " );
196- b .append (PlainSelect .getStringList (colDataTypeList ));
197- if (colDataTypeList .size () > 1 ) {
198- b .append (")" );
199- }
200- } else if (constraintName != null ) {
201- b .append ("CONSTRAINT " ).append (constraintName );
202- } else if (pkColumns != null ) {
203- b .append ("PRIMARY KEY (" ).append (PlainSelect .getStringList (pkColumns )).append (")" );
204- } else if (ukColumns != null ) {
205- b .append ("UNIQUE KEY " ).append (ukName ).append (" (" ).append (PlainSelect .getStringList (ukColumns )).append (")" );
206- } else if (fkColumns != null ) {
207- b .append ("FOREIGN KEY (" ).append (PlainSelect .getStringList (fkColumns )).append (") REFERENCES " ).append (fkSourceTable ).append (" (" ).append (
208- PlainSelect .getStringList (fkSourceColumns )).append (")" );
209- if (isOnDeleteCascade ()) {
210- b .append (" ON DELETE CASCADE" );
211- } else if (isOnDeleteRestrict ()) {
212- b .append (" ON DELETE RESTRICT" );
213- } else if (isOnDeleteSetNull ()) {
214- b .append (" ON DELETE SET NULL" );
215- }
216- } else if (index != null ) {
217- b .append (index );
218- }
219- return b .toString ();
220- }
22174
222- public class ColumnDataType {
223-
224- private final String columnName ;
225- private final ColDataType colDataType ;
75+ StringBuilder b = new StringBuilder ();
76+ b .append ("ALTER TABLE " ).append (table .getFullyQualifiedName ()).append (" " );
22677
227- public ColumnDataType (String columnName , ColDataType colDataType ) {
228- this .columnName = columnName ;
229- this .colDataType = colDataType ;
230- }
78+ Iterator <AlterExpression > altIter = alterExpressions .iterator ();
23179
232- public String getColumnName () {
233- return columnName ;
234- }
80+ while (altIter .hasNext ()){
81+ b .append (altIter .next ().toString ());
23582
236- public ColDataType getColDataType () {
237- return colDataType ;
83+ // Need to append whitespace after each ADD or DROP statement
84+ // but not the last one
85+ if (altIter .hasNext ()){
86+ b .append (", " );
87+ }
23888 }
23989
240- @ Override
241- public String toString () {
242- return columnName + " " + colDataType ;
243- }
90+ return b .toString ();
24491 }
92+
24593}
0 commit comments