Skip to content

Commit 822bbbe

Browse files
committed
fixes #246
1 parent 4263651 commit 822bbbe

File tree

4 files changed

+127
-34
lines changed

4 files changed

+127
-34
lines changed

src/main/java/net/sf/jsqlparser/statement/alter/Alter.java

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
package net.sf.jsqlparser.statement.alter;
2323

24+
import java.util.ArrayList;
2425
import java.util.List;
2526
import net.sf.jsqlparser.schema.Table;
2627
import net.sf.jsqlparser.statement.Statement;
@@ -37,12 +38,15 @@ public class Alter implements Statement {
3738

3839
private Table table;
3940
private String columnName;
40-
private ColDataType dataType;
41+
//private ColDataType dataType;
42+
43+
private List<ColumnDataType> colDataTypeList;
44+
4145
private List<String> pkColumns;
4246
private List<String> ukColumns;
4347
private String ukName;
4448
private Index index = null;
45-
private String operation;
49+
private AlterOperation operation;
4650
private String constraintName;
4751
private boolean onDeleteRestrict;
4852
private boolean onDeleteSetNull;
@@ -91,11 +95,22 @@ public void setFkSourceTable(String fkSourceTable) {
9195
this.fkSourceTable = fkSourceTable;
9296
}
9397

94-
public String getOperation() {
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() {
95110
return operation;
96111
}
97112

98-
public void setOperation(String operation) {
113+
public void setOperation(AlterOperation operation) {
99114
this.operation = operation;
100115
}
101116

@@ -131,14 +146,6 @@ public void setConstraintName(final String constraintName) {
131146
this.constraintName = constraintName;
132147
}
133148

134-
public ColDataType getDataType() {
135-
return dataType;
136-
}
137-
138-
public void setDataType(ColDataType dataType) {
139-
this.dataType = dataType;
140-
}
141-
142149
public List<String> getPkColumns() {
143150
return pkColumns;
144151
}
@@ -182,8 +189,13 @@ public String toString() {
182189
b.append("ALTER TABLE ").append(table.getFullyQualifiedName()).append(" ").append(operation).append(" ");
183190
if (columnName != null) {
184191
b.append("COLUMN ").append(columnName);
185-
if (dataType != null) {
186-
b.append(" ").append(dataType.toString());
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(")");
187199
}
188200
} else if (constraintName != null) {
189201
b.append("CONSTRAINT ").append(constraintName);
@@ -193,17 +205,41 @@ public String toString() {
193205
b.append("UNIQUE KEY ").append(ukName).append(" (").append(PlainSelect.getStringList(ukColumns)).append(")");
194206
} else if (fkColumns != null) {
195207
b.append("FOREIGN KEY (").append(PlainSelect.getStringList(fkColumns)).append(") REFERENCES ").append(fkSourceTable).append(" (").append(
196-
PlainSelect.getStringList(fkSourceColumns)).append(")");
208+
PlainSelect.getStringList(fkSourceColumns)).append(")");
197209
if (isOnDeleteCascade()) {
198210
b.append(" ON DELETE CASCADE");
199211
} else if (isOnDeleteRestrict()) {
200212
b.append(" ON DELETE RESTRICT");
201213
} else if (isOnDeleteSetNull()) {
202214
b.append(" ON DELETE SET NULL");
203215
}
204-
} else if (index != null) {
205-
b.append(index);
206-
}
207-
return b.toString();
216+
} else if (index != null) {
217+
b.append(index);
218+
}
219+
return b.toString();
220+
}
221+
222+
public class ColumnDataType {
223+
224+
private final String columnName;
225+
private final ColDataType colDataType;
226+
227+
public ColumnDataType(String columnName, ColDataType colDataType) {
228+
this.columnName = columnName;
229+
this.colDataType = colDataType;
230+
}
231+
232+
public String getColumnName() {
233+
return columnName;
234+
}
235+
236+
public ColDataType getColDataType() {
237+
return colDataType;
238+
}
239+
240+
@Override
241+
public String toString() {
242+
return columnName + " " + colDataType;
243+
}
208244
}
209245
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2016 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
/*
23+
* Copyright (C) 2016 JSQLParser.
24+
*
25+
* This library is free software; you can redistribute it and/or
26+
* modify it under the terms of the GNU Lesser General Public
27+
* License as published by the Free Software Foundation; either
28+
* version 2.1 of the License, or (at your option) any later version.
29+
*
30+
* This library is distributed in the hope that it will be useful,
31+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33+
* Lesser General Public License for more details.
34+
*
35+
* You should have received a copy of the GNU Lesser General Public
36+
* License along with this library; if not, write to the Free Software
37+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38+
* MA 02110-1301 USA
39+
*/
40+
package net.sf.jsqlparser.statement.alter;
41+
42+
/**
43+
*
44+
* @author toben
45+
*/
46+
public enum AlterOperation {
47+
ADD, DROP;
48+
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,22 +3000,23 @@ Alter AlterTable():
30003000
ForeignKeyIndex fkIndex = null;
30013001
NamedConstraint index = null;
30023002
Table fkTable = null;
3003-
Token alterDrop = null;
30043003
}
30053004
{
30063005
<K_ALTER> <K_TABLE> table=Table() { alter.setTable(table); }
30073006
(
3008-
(alterDrop = <K_ADD>
3007+
(<K_ADD>
30093008
{
3010-
alter.setOperation(alterDrop.image);
3009+
alter.setOperation(AlterOperation.ADD);
30113010
}
30123011
(
30133012
( <K_COLUMN>
3014-
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) dataType=ColDataType()
3015-
{
3016-
alter.setColumnName(tk.image);
3017-
alter.setDataType(dataType);
3018-
}
3013+
sk3 = RelObjectName() dataType=ColDataType()
3014+
{ alter.addColDataType(sk3, dataType); }
3015+
)
3016+
|
3017+
(
3018+
"(" sk3 = RelObjectName() dataType = ColDataType() { alter.addColDataType(sk3, dataType); }
3019+
("," sk3 = RelObjectName() dataType = ColDataType() { alter.addColDataType(sk3, dataType); } )* ")"
30193020
)
30203021
|
30213022
( <K_PRIMARY> <K_KEY> columnNames=ColumnsNamesList() { alter.setPkColumns(columnNames); } )
@@ -3074,9 +3075,9 @@ Alter AlterTable():
30743075
)
30753076
)
30763077
|
3077-
(alterDrop = <K_DROP>
3078+
(<K_DROP>
30783079
{
3079-
alter.setOperation(alterDrop.image);
3080+
alter.setOperation(AlterOperation.DROP);
30803081
}
30813082
(
30823083
( <K_COLUMN>

src/test/java/net/sf/jsqlparser/test/alter/AlterTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package net.sf.jsqlparser.test.alter;
22

33

4+
import java.util.List;
45
import junit.framework.TestCase;
56
import net.sf.jsqlparser.JSQLParserException;
67
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
78
import net.sf.jsqlparser.statement.Statement;
89
import net.sf.jsqlparser.statement.alter.Alter;
9-
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
10-
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
11-
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
10+
import net.sf.jsqlparser.statement.alter.Alter.ColumnDataType;
1211
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1312

1413
public class AlterTest extends TestCase {
@@ -22,8 +21,10 @@ public void testAlterTableAddColumn() throws JSQLParserException {
2221
assertTrue(stmt instanceof Alter);
2322
Alter alter = (Alter)stmt;
2423
assertEquals("mytable",alter.getTable().getFullyQualifiedName());
25-
assertEquals("mycolumn", alter.getColumnName());
26-
assertEquals("varchar (255)", alter.getDataType().toString());
24+
List<ColumnDataType> list = alter.getColDataTypeList();
25+
assertNotNull(list);
26+
assertEquals("mycolumn", list.get(0).getColumnName());
27+
assertEquals("varchar (255)", list.get(0).getColDataType().toString());
2728
}
2829

2930
public void testAlterTablePrimaryKey() throws JSQLParserException {
@@ -70,4 +71,11 @@ public void testAlterTableCheckConstraint() throws JSQLParserException {
7071
assertSqlCanBeParsedAndDeparsed("ALTER TABLE `Author` ADD CONSTRAINT name_not_empty CHECK (`NAME` <> '')");
7172
}
7273

74+
public void testAlterTableAddColumn2() throws JSQLParserException {
75+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE animals ADD (col1 integer, col2 integer)");
76+
}
77+
78+
public void testAlterTableAddColumn3() throws JSQLParserException {
79+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN mycolumn varchar (255)");
80+
}
7381
}

0 commit comments

Comments
 (0)