Skip to content

Commit 7557524

Browse files
committed
start alter statement
1 parent e5e488d commit 7557524

File tree

6 files changed

+150
-3
lines changed

6 files changed

+150
-3
lines changed

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

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

24+
import net.sf.jsqlparser.statement.alter.Alter;
2425
import net.sf.jsqlparser.statement.create.index.CreateIndex;
2526
import net.sf.jsqlparser.statement.create.table.CreateTable;
2627
import net.sf.jsqlparser.statement.create.view.CreateView;
@@ -53,4 +54,6 @@ public interface StatementVisitor {
5354
void visit(CreateTable createTable);
5455

5556
void visit(CreateView createView);
57+
58+
void visit(Alter alter);
5659
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 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) 2013 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+
41+
package net.sf.jsqlparser.statement.alter;
42+
43+
import net.sf.jsqlparser.schema.Table;
44+
import net.sf.jsqlparser.statement.Statement;
45+
import net.sf.jsqlparser.statement.StatementVisitor;
46+
import net.sf.jsqlparser.statement.create.table.ColDataType;
47+
48+
/**
49+
*
50+
* @author toben
51+
*/
52+
public class Alter implements Statement {
53+
private Table table;
54+
private String columnName;
55+
private ColDataType dataType;
56+
57+
public Table getTable() {
58+
return table;
59+
}
60+
61+
public void setTable(Table table) {
62+
this.table = table;
63+
}
64+
65+
public String getColumnName() {
66+
return columnName;
67+
}
68+
69+
public void setColumnName(String columnName) {
70+
this.columnName = columnName;
71+
}
72+
73+
public ColDataType getDataType() {
74+
return dataType;
75+
}
76+
77+
public void setDataType(ColDataType dataType) {
78+
this.dataType = dataType;
79+
}
80+
81+
@Override
82+
public void accept(StatementVisitor statementVisitor) {
83+
statementVisitor.visit(this);
84+
}
85+
86+
public String toString() {
87+
return "ALTER TABLE " + table.getWholeTableName() + " ADD COLUMN " + columnName + " " + dataType.toString();
88+
}
89+
}

src/main/java/net/sf/jsqlparser/util/deparser/StatementDeParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Iterator;
2525

2626
import net.sf.jsqlparser.statement.StatementVisitor;
27+
import net.sf.jsqlparser.statement.alter.Alter;
2728
import net.sf.jsqlparser.statement.create.index.CreateIndex;
2829
import net.sf.jsqlparser.statement.create.table.CreateTable;
2930
import net.sf.jsqlparser.statement.create.view.CreateView;
@@ -142,4 +143,9 @@ public StringBuilder getBuffer() {
142143
public void setBuffer(StringBuilder buffer) {
143144
this.buffer = buffer;
144145
}
146+
147+
@Override
148+
public void visit(Alter alter) {
149+
150+
}
145151
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import net.sf.jsqlparser.statement.create.table.Index;
123123
import net.sf.jsqlparser.statement.create.table.ForeignKeyIndex;
124124
import net.sf.jsqlparser.statement.delete.Delete;
125125
import net.sf.jsqlparser.statement.drop.Drop;
126+
import net.sf.jsqlparser.statement.alter.Alter;
126127
import net.sf.jsqlparser.statement.insert.Insert;
127128
import net.sf.jsqlparser.statement.replace.Replace;
128129
import net.sf.jsqlparser.statement.select.AllColumns;
@@ -264,6 +265,9 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
264265
| <K_PRIOR:"PRIOR">
265266
| <K_NOCYCLE:"NOCYCLE">
266267
| <K_SIBLINGS:"SIBLINGS">
268+
| <K_ALTER:"ALTER">
269+
| <K_ADD:"ADD">
270+
| <K_COLUMN:"COLUMN">
267271
}
268272

269273

@@ -323,6 +327,8 @@ Statement Statement() :
323327
|
324328
stm = Replace()
325329
|
330+
stm = Alter()
331+
|
326332
LOOKAHEAD(3)
327333
stm = CreateIndex()
328334
|
@@ -2305,3 +2311,22 @@ Truncate Truncate():
23052311
return truncate;
23062312
}
23072313
}
2314+
2315+
Alter Alter():
2316+
{
2317+
Alter alter = new Alter();
2318+
Table table;
2319+
Token tk;
2320+
ColDataType dataType;
2321+
}
2322+
{
2323+
<K_ALTER> <K_TABLE> table=Table() <K_ADD> <K_COLUMN>
2324+
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) dataType=ColDataType()
2325+
2326+
{
2327+
alter.setTable(table);
2328+
alter.setColumnName(tk.image);
2329+
alter.setDataType(dataType);
2330+
return alter;
2331+
}
2332+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.sf.jsqlparser.test.alter;
2+
3+
4+
import junit.framework.TestCase;
5+
import net.sf.jsqlparser.JSQLParserException;
6+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
7+
import net.sf.jsqlparser.statement.Statement;
8+
import net.sf.jsqlparser.statement.alter.Alter;
9+
10+
public class AlterTest extends TestCase {
11+
12+
public AlterTest(String arg0) {
13+
super(arg0);
14+
}
15+
16+
public void testAlterTableAddColumn() throws JSQLParserException {
17+
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE mytable ADD COLUMN mycolumn varchar (255)");
18+
assertTrue(stmt instanceof Alter);
19+
Alter alter = (Alter)stmt;
20+
assertEquals("mytable",alter.getTable().getWholeTableName());
21+
assertEquals("mycolumn", alter.getColumnName());
22+
assertEquals("varchar (255)", alter.getDataType().toString());
23+
}
24+
}

src/test/java/net/sf/jsqlparser/test/update/UpdateTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public void testUpdate() throws JSQLParserException {
3838
}
3939

4040
public void testUpdateWAlias() throws JSQLParserException {
41-
String statement = "UPDATE table1 A SET A.column = 'XXX' WHERE A.cod_table = 'YYY'";
41+
String statement = "UPDATE table1 A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'";
4242
Update update = (Update) parserManager.parse(new StringReader(statement));
4343
}
4444

4545
public void testUpdateWithDeparser() throws JSQLParserException {
46-
assertSqlCanBeParsedAndDeparsed("UPDATE table1 AS A SET A.column = 'XXX' WHERE A.cod_table = 'YYY'");
46+
assertSqlCanBeParsedAndDeparsed("UPDATE table1 AS A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'");
4747
}
4848

4949
public void testUpdateWithFrom() throws JSQLParserException {
50-
assertSqlCanBeParsedAndDeparsed("UPDATE table1 SET column = 5 FROM table1 LEFT JOIN table2 ON col1 = col2");
50+
assertSqlCanBeParsedAndDeparsed("UPDATE table1 SET columna = 5 FROM table1 LEFT JOIN table2 ON col1 = col2");
5151
}
5252
}

0 commit comments

Comments
 (0)