Skip to content

Commit aebe814

Browse files
committed
Merge support-create-index
2 parents 5e10695 + b9d0049 commit aebe814

File tree

6 files changed

+332
-0
lines changed

6 files changed

+332
-0
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.create.index.CreateIndex;
2425
import net.sf.jsqlparser.statement.create.table.CreateTable;
2526
import net.sf.jsqlparser.statement.create.view.CreateView;
2627
import net.sf.jsqlparser.statement.delete.Delete;
@@ -47,6 +48,8 @@ public interface StatementVisitor {
4748

4849
public void visit(Truncate truncate);
4950

51+
public void visit(CreateIndex createIndex);
52+
5053
public void visit(CreateTable createTable);
5154

5255
public void visit(CreateView createView);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
package net.sf.jsqlparser.statement.create.index;
23+
24+
import java.util.Iterator;
25+
26+
import net.sf.jsqlparser.schema.Table;
27+
import net.sf.jsqlparser.statement.Statement;
28+
import net.sf.jsqlparser.statement.StatementVisitor;
29+
import net.sf.jsqlparser.statement.create.table.Index;
30+
31+
/**
32+
* A "CREATE INDEX" statement
33+
*
34+
* @author Raymond Augé
35+
*/
36+
public class CreateIndex implements Statement {
37+
38+
private Table table;
39+
private Index index;
40+
41+
public void accept(StatementVisitor statementVisitor) {
42+
statementVisitor.visit(this);
43+
}
44+
45+
/**
46+
* The index to be created
47+
*/
48+
public Index getIndex() {
49+
return index;
50+
}
51+
52+
public void setIndex(Index index) {
53+
this.index = index;
54+
}
55+
56+
/**
57+
* The table on which the index is to be created
58+
*/
59+
public Table getTable() {
60+
return table;
61+
}
62+
63+
public void setTable(Table table) {
64+
this.table = table;
65+
}
66+
67+
public String toString() {
68+
StringBuffer buffer = new StringBuffer();
69+
70+
buffer.append("CREATE ");
71+
72+
if (index.getType() != null) {
73+
buffer.append(index.getType());
74+
buffer.append(" ");
75+
}
76+
77+
buffer.append("INDEX ");
78+
buffer.append(index.getName());
79+
buffer.append(" ON ");
80+
buffer.append(table.getWholeTableName());
81+
82+
if (index.getColumnsNames() != null) {
83+
buffer.append(" (");
84+
85+
for (Iterator iter = index.getColumnsNames().iterator(); iter.hasNext() ;) {
86+
String columnName = (String)iter.next();
87+
88+
buffer.append(columnName);
89+
90+
if (iter.hasNext()) {
91+
buffer.append(", ");
92+
}
93+
}
94+
95+
buffer.append(")");
96+
}
97+
98+
return buffer.toString();
99+
}
100+
101+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
package net.sf.jsqlparser.util.deparser;
23+
24+
import java.util.Iterator;
25+
26+
import net.sf.jsqlparser.statement.create.index.CreateIndex;
27+
import net.sf.jsqlparser.statement.create.table.Index;
28+
29+
/**
30+
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a string)
31+
* a {@link net.sf.jsqlparser.statement.create.index.CreateIndex}
32+
*
33+
* @author Raymond Augé
34+
*/
35+
public class CreateIndexDeParser {
36+
protected StringBuilder buffer;
37+
38+
/**
39+
* @param buffer the buffer that will be filled with the create
40+
*/
41+
public CreateIndexDeParser(StringBuilder buffer) {
42+
this.buffer = buffer;
43+
}
44+
45+
public void deParse(CreateIndex createIndex) {
46+
Index index = createIndex.getIndex();
47+
48+
buffer.append("CREATE ");
49+
50+
if (index.getType() != null) {
51+
buffer.append(index.getType());
52+
buffer.append(" ");
53+
}
54+
55+
buffer.append("INDEX ");
56+
buffer.append(index.getName());
57+
buffer.append(" ON ");
58+
buffer.append(createIndex.getTable().getWholeTableName());
59+
60+
if (index.getColumnsNames() != null) {
61+
buffer.append(" (");
62+
for (Iterator iter = index.getColumnsNames().iterator(); iter.hasNext();) {
63+
String columnName = (String)iter.next();
64+
buffer.append(columnName);
65+
66+
if (iter.hasNext()) {
67+
buffer.append(", ");
68+
}
69+
}
70+
buffer.append(")");
71+
}
72+
}
73+
74+
public StringBuilder getBuffer() {
75+
return buffer;
76+
}
77+
78+
public void setBuffer(StringBuilder buffer) {
79+
this.buffer = buffer;
80+
}
81+
82+
}

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

Lines changed: 7 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.create.index.CreateIndex;
2728
import net.sf.jsqlparser.statement.create.table.CreateTable;
2829
import net.sf.jsqlparser.statement.create.view.CreateView;
2930
import net.sf.jsqlparser.statement.delete.Delete;
@@ -43,6 +44,12 @@ public StatementDeParser(StringBuilder buffer) {
4344
this.buffer = buffer;
4445
}
4546

47+
@Override
48+
public void visit(CreateIndex createIndex) {
49+
CreateIndexDeParser createIndexDeParser = new CreateIndexDeParser(buffer);
50+
createIndexDeParser.deParse(createIndex);
51+
}
52+
4653
@Override
4754
public void visit(CreateTable createTable) {
4855
CreateTableDeParser createTableDeParser = new CreateTableDeParser(buffer);

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
108108
import net.sf.jsqlparser.schema.Column;
109109
import net.sf.jsqlparser.schema.Table;
110110
import net.sf.jsqlparser.statement.Statement;
111+
import net.sf.jsqlparser.statement.create.index.CreateIndex;
111112
import net.sf.jsqlparser.statement.create.table.ColDataType;
112113
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
113114
import net.sf.jsqlparser.statement.create.table.CreateTable;
@@ -296,6 +297,9 @@ Statement Statement() :
296297
stm = Delete()
297298
|
298299
stm = Replace()
300+
|
301+
LOOKAHEAD(3)
302+
stm = CreateIndex()
299303
|
300304
LOOKAHEAD(2)
301305
stm = CreateTable()
@@ -1707,6 +1711,62 @@ SubSelect SubSelect():
17071711
}
17081712
}
17091713

1714+
CreateIndex CreateIndex():
1715+
{
1716+
CreateIndex createIndex = new CreateIndex();
1717+
Table table = null;
1718+
List colNames = new ArrayList();
1719+
Token columnName;
1720+
Index index = null;
1721+
String name = null;
1722+
String parameter = null;
1723+
}
1724+
{
1725+
<K_CREATE>
1726+
( parameter=CreateParameter() )*
1727+
1728+
<K_INDEX> name=RelObjectName()
1729+
{
1730+
index = new Index();
1731+
index.setName(name);
1732+
index.setType(parameter);
1733+
}
1734+
1735+
<K_ON> table=Table()
1736+
1737+
"("
1738+
(columnName=<S_IDENTIFIER>
1739+
|
1740+
columnName=<S_QUOTED_IDENTIFIER>)
1741+
1742+
(CreateParameter() | <K_ASC> | <K_DESC>)*
1743+
{
1744+
colNames.add(columnName.image);
1745+
}
1746+
1747+
(
1748+
","
1749+
(columnName=<S_IDENTIFIER>
1750+
|
1751+
columnName=<S_QUOTED_IDENTIFIER>)
1752+
1753+
(CreateParameter() | <K_ASC> | <K_DESC>)*
1754+
{
1755+
colNames.add(columnName.image);
1756+
}
1757+
)*
1758+
1759+
")"
1760+
(CreateParameter() {})*
1761+
1762+
{
1763+
index.setColumnsNames(colNames);
1764+
createIndex.setIndex(index);
1765+
createIndex.setTable(table);
1766+
return createIndex;
1767+
}
1768+
}
1769+
17101770
CreateTable CreateTable():
17111771
{
17121772
CreateTable createTable = new CreateTable();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package net.sf.jsqlparser.test.create;
2+
3+
import java.io.StringReader;
4+
5+
import junit.framework.TestCase;
6+
import net.sf.jsqlparser.JSQLParserException;
7+
import net.sf.jsqlparser.parser.CCJSqlParserManager;
8+
import net.sf.jsqlparser.statement.create.index.CreateIndex;
9+
10+
11+
/**
12+
* @author Raymond Augé
13+
*/
14+
public class CreateIndexTest extends TestCase {
15+
16+
CCJSqlParserManager parserManager = new CCJSqlParserManager();
17+
18+
public CreateIndexTest(String arg0) {
19+
super(arg0);
20+
}
21+
22+
public void testCreateIndex() throws JSQLParserException {
23+
String statement =
24+
"CREATE INDEX myindex ON mytab (mycol, mycol2)";
25+
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
26+
assertEquals(2, createIndex.getIndex().getColumnsNames().size());
27+
assertEquals("myindex", createIndex.getIndex().getName());
28+
assertNull(createIndex.getIndex().getType());
29+
assertEquals("mytab", createIndex.getTable().getWholeTableName());
30+
assertEquals("mycol", createIndex.getIndex().getColumnsNames().get(0));
31+
assertEquals(statement, ""+createIndex);
32+
}
33+
34+
public void testCreateIndex2() throws JSQLParserException {
35+
String statement =
36+
"CREATE mytype INDEX myindex ON mytab (mycol, mycol2)";
37+
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
38+
assertEquals(2, createIndex.getIndex().getColumnsNames().size());
39+
assertEquals("myindex", createIndex.getIndex().getName());
40+
assertEquals("mytype", createIndex.getIndex().getType());
41+
assertEquals("mytab", createIndex.getTable().getWholeTableName());
42+
assertEquals("mycol2", createIndex.getIndex().getColumnsNames().get(1));
43+
assertEquals(statement, ""+createIndex);
44+
}
45+
46+
public void testCreateIndex3() throws JSQLParserException {
47+
String statement =
48+
"CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2, mycol3)";
49+
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
50+
assertEquals(3, createIndex.getIndex().getColumnsNames().size());
51+
assertEquals("myindex", createIndex.getIndex().getName());
52+
assertEquals("mytype", createIndex.getIndex().getType());
53+
assertEquals("mytab", createIndex.getTable().getWholeTableName());
54+
assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2));
55+
}
56+
57+
public void testCreateIndex4() throws JSQLParserException {
58+
String statement =
59+
"CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2 (75), mycol3)";
60+
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
61+
assertEquals(3, createIndex.getIndex().getColumnsNames().size());
62+
assertEquals("myindex", createIndex.getIndex().getName());
63+
assertEquals("mytype", createIndex.getIndex().getType());
64+
assertEquals("mytab", createIndex.getTable().getWholeTableName());
65+
assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2));
66+
}
67+
68+
public void testCreateIndex5() throws JSQLParserException {
69+
String statement =
70+
"CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2 (75), mycol3) mymodifiers";
71+
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
72+
assertEquals(3, createIndex.getIndex().getColumnsNames().size());
73+
assertEquals("myindex", createIndex.getIndex().getName());
74+
assertEquals("mytype", createIndex.getIndex().getType());
75+
assertEquals("mytab", createIndex.getTable().getWholeTableName());
76+
assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2));
77+
}
78+
79+
}

0 commit comments

Comments
 (0)