Skip to content

Commit 9d74c6d

Browse files
committed
support postgresql create index syntax
1 parent dd80699 commit 9d74c6d

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

src/main/java/net/sf/jsqlparser/statement/create/index/CreateIndex.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public String toString() {
5757
buffer.append(" ON ");
5858
buffer.append(table.getFullyQualifiedName());
5959

60+
if (index.getUsing() != null){
61+
buffer.append(" USING ");
62+
buffer.append(index.getUsing());
63+
}
64+
6065
if (index.getColumnsNames() != null) {
6166
buffer.append(" (");
6267

src/main/java/net/sf/jsqlparser/statement/create/table/Index.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
public class Index {
1717

1818
private String type;
19+
private String using;
1920
private List<String> columnsNames;
2021
private String name;
2122
private List<String> idxSpec;
@@ -32,6 +33,17 @@ public String getType() {
3233
return type;
3334
}
3435

36+
/**
37+
* In postgresql, the index type (Btree, GIST, etc.) is indicated
38+
* with a USING clause.
39+
* Please note that:
40+
* Oracle - the type might be BITMAP, indicating a bitmap kind of index
41+
* MySQL - the type might be FULLTEXT or SPATIAL
42+
*/
43+
public void setUsing(String string) {
44+
using = string;
45+
}
46+
3547
public void setColumnsNames(List<String> list) {
3648
columnsNames = list;
3749
}
@@ -44,6 +56,10 @@ public void setType(String string) {
4456
type = string;
4557
}
4658

59+
public String getUsing() {
60+
return using;
61+
}
62+
4763
public List<String> getIndexSpec() {
4864
return idxSpec;
4965
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public void deParse(CreateIndex createIndex) {
3737
buffer.append(" ON ");
3838
buffer.append(createIndex.getTable().getFullyQualifiedName());
3939

40+
String using = index.getUsing();
41+
if (using != null){
42+
buffer.append(" USING ");
43+
buffer.append(using);
44+
}
45+
4046
if (index.getColumnsNames() != null) {
4147
buffer.append(" (");
4248
for (Iterator iter = index.getColumnsNames().iterator(); iter.hasNext();) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,6 +3290,7 @@ CreateIndex CreateIndex():
32903290
Table table = null;
32913291
List<String> colNames = new ArrayList<String>();
32923292
Token columnName;
3293+
Token using;
32933294
Index index = null;
32943295
String name = null;
32953296
List<String> parameter = new ArrayList<String>();
@@ -3307,6 +3308,8 @@ CreateIndex CreateIndex():
33073308

33083309
<K_ON> table=Table()
33093310

3311+
[ <K_USING> using=<S_IDENTIFIER> {index.setUsing(using.image);} ]
3312+
33103313
"("
33113314
(columnName=<S_IDENTIFIER>
33123315
|

src/test/java/net/sf/jsqlparser/statement/create/CreateIndexTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ public void testCreateIndex6() throws JSQLParserException {
9191
assertSqlCanBeParsedAndDeparsed(stmt);
9292
}
9393

94+
@Test
95+
public void testCreateIndex7() throws JSQLParserException {
96+
String statement
97+
= "CREATE INDEX myindex1 ON mytab USING GIST (mycol)";
98+
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
99+
assertEquals(1, createIndex.getIndex().getColumnsNames().size());
100+
assertEquals("myindex1", createIndex.getIndex().getName());
101+
assertNull(createIndex.getIndex().getType());
102+
assertEquals("mytab", createIndex.getTable().getFullyQualifiedName());
103+
assertEquals("mycol", createIndex.getIndex().getColumnsNames().get(0));
104+
assertEquals("GIST", createIndex.getIndex().getUsing());
105+
assertEquals(statement, "" + createIndex);
106+
assertSqlCanBeParsedAndDeparsed(statement);
107+
}
108+
94109
@Test
95110
@Ignore
96111
public void testCreateIndexIssue633() throws JSQLParserException {

0 commit comments

Comments
 (0)