Skip to content

Parse all parameters of an index #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/main/java/net/sf/jsqlparser/statement/create/table/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package net.sf.jsqlparser.statement.create.table;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.jsqlparser.statement.select.PlainSelect;
Expand All @@ -30,7 +32,7 @@
*/
public class Index {

private String type;
private List<String> types;
private List<String> columnsNames;
private String name;
private List<String> idxSpec;
Expand All @@ -50,7 +52,14 @@ public String getName() {
* The type of this index: "PRIMARY KEY", "UNIQUE", "INDEX"
*/
public String getType() {
return type;
return types == null || types.isEmpty() ? null : PlainSelect.getStringList(types, false, false);
}

/**
* All types of this index: "PRIMARY KEY", "UNIQUE", "UNIQUE NULL_FILTERED", "INDEX"
*/
public List<String> getTypes() {
return types;
}

public void setColumnsNames(List<String> list) {
Expand All @@ -60,9 +69,20 @@ public void setColumnsNames(List<String> list) {
public void setName(String string) {
name = string;
}

public void setType(String string) {
type = string;
if(types == null) {
types = new ArrayList<>();
}
if(string == null) {
types.clear();
} else {
types.addAll(Arrays.asList(string.split("\\s+")));
}
}

public void setTypes(List<String> list) {
types = list;
}

public List<String> getIndexSpec() {
Expand All @@ -76,7 +96,7 @@ public void setIndexSpec(List<String> idxSpec) {
@Override
public String toString() {
String idxSpecText = PlainSelect.getStringList(idxSpec, false, false);
return type + (name != null ? " " + name : "") + " " + PlainSelect.
return (types == null || types.isEmpty() ? "" : PlainSelect.getStringList(types, false, false)) + (name != null ? " " + name : "") + " " + PlainSelect.
getStringList(columnsNames, true, true) + (!"".equals(idxSpecText) ? " " + idxSpecText : "");
}
}
14 changes: 10 additions & 4 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2966,16 +2966,19 @@ CreateIndex CreateIndex():
Index index = null;
String name = null;
List<String> parameter = new ArrayList<String>();
List<String> types = new ArrayList<String>();
List<String> indexSpecs = new ArrayList<String>();
Token ascDesc = null;
}
{
<K_CREATE>
( parameter=CreateParameter() )*
(parameter = CreateParameter() { types.addAll(parameter); })*

<K_INDEX> name=RelObjectName()
{
index = new Index();
index.setName(name);
index.setType(parameter.isEmpty()?null:parameter.get(0));
index.setTypes(types);
}

<K_ON> table=Table()
Expand All @@ -2985,9 +2988,10 @@ CreateIndex CreateIndex():
|
columnName=<S_QUOTED_IDENTIFIER>)

(CreateParameter() | <K_ASC> | <K_DESC>)*
(CreateParameter() | ascDesc=<K_ASC> | ascDesc=<K_DESC>)*
{
colNames.add(columnName.image);
indexSpecs.add(ascDesc == null ? null : ascDesc.image);
}

(
Expand All @@ -2996,9 +3000,10 @@ CreateIndex CreateIndex():
|
columnName=<S_QUOTED_IDENTIFIER>)

(CreateParameter() | <K_ASC> | <K_DESC>)*
(CreateParameter() | ascDesc=<K_ASC> | ascDesc=<K_DESC>)*
{
colNames.add(columnName.image);
indexSpecs.add(ascDesc == null ? null : ascDesc.image);
}
)*

Expand All @@ -3007,6 +3012,7 @@ CreateIndex CreateIndex():

{
index.setColumnsNames(colNames);
index.setIndexSpec(indexSpecs);
createIndex.setIndex(index);
createIndex.setTable(table);
return createIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,27 @@ public void testCreateIndex6() throws JSQLParserException {
String stmt = "CREATE INDEX myindex ON mytab (mycol, mycol2)";
assertSqlCanBeParsedAndDeparsed(stmt);
}

@Test
public void testCreateIndexDesc() throws JSQLParserException {
String statement = "CREATE INDEX myindex ON mytab (mycol desc, mycol2 asc)";
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
assertEquals(2, createIndex.getIndex().getColumnsNames().size());
assertEquals(2, createIndex.getIndex().getIndexSpec().size());
assertEquals("desc", createIndex.getIndex().getIndexSpec().get(0));
assertEquals("asc", createIndex.getIndex().getIndexSpec().get(1));
}

@Test
public void testCreateUniqueNullFilteredIndex() throws JSQLParserException {
String statement = "CREATE UNIQUE NULL_FILTERED INDEX myindex ON mytab (mycol)";
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
assertEquals(1, createIndex.getIndex().getColumnsNames().size());
assertEquals(1, createIndex.getIndex().getIndexSpec().size());
assertEquals(2, createIndex.getIndex().getTypes().size());
assertEquals("UNIQUE", createIndex.getIndex().getTypes().get(0));
assertEquals("NULL_FILTERED", createIndex.getIndex().getTypes().get(1));
assertEquals("UNIQUE NULL_FILTERED", createIndex.getIndex().getType());
assertEquals("CREATE UNIQUE NULL_FILTERED INDEX myindex ON mytab (mycol)", createIndex.toString());
}
}