Skip to content

Commit

Permalink
Closed #1814, mysql and mariadb can use index type before ON (#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin authored Dec 15, 2023
1 parent 2ce72a3 commit b0aff31
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@
*/
package net.sf.jsqlparser.statement.create.index;

import static java.util.stream.Collectors.joining;

import java.util.*;
import net.sf.jsqlparser.schema.*;
import net.sf.jsqlparser.statement.*;
import net.sf.jsqlparser.statement.create.table.*;

import java.util.*;
import static java.util.stream.Collectors.joining;

public class CreateIndex implements Statement {

private Table table;
private Index index;
private List<String> tailParameters;
private boolean indexTypeBeforeOn = false;

public boolean isIndexTypeBeforeOn() {
return indexTypeBeforeOn;
}

public void setIndexTypeBeforeOn(boolean indexTypeBeforeOn) {
this.indexTypeBeforeOn = indexTypeBeforeOn;
}

public boolean isUsingIfNotExists() {
return usingIfNotExists;
Expand Down Expand Up @@ -78,10 +87,16 @@ public String toString() {
buffer.append("IF NOT EXISTS ");
}
buffer.append(index.getName());

if (index.getUsing() != null && isIndexTypeBeforeOn()) {
buffer.append(" USING ");
buffer.append(index.getUsing());
}

buffer.append(" ON ");
buffer.append(table.getFullyQualifiedName());

if (index.getUsing() != null) {
if (index.getUsing() != null && !isIndexTypeBeforeOn()) {
buffer.append(" USING ");
buffer.append(index.getUsing());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/
package net.sf.jsqlparser.util.deparser;

import static java.util.stream.Collectors.joining;

import net.sf.jsqlparser.statement.create.index.CreateIndex;
import net.sf.jsqlparser.statement.create.table.Index;

import static java.util.stream.Collectors.joining;

public class CreateIndexDeParser extends AbstractDeParser<CreateIndex> {

public CreateIndexDeParser(StringBuilder buffer) {
Expand All @@ -36,11 +36,17 @@ public void deParse(CreateIndex createIndex) {
buffer.append("IF NOT EXISTS ");
}
buffer.append(index.getName());

String using = index.getUsing();
if (using != null && createIndex.isIndexTypeBeforeOn()) {
buffer.append(" USING ");
buffer.append(using);
}

buffer.append(" ON ");
buffer.append(createIndex.getTable().getFullyQualifiedName());

String using = index.getUsing();
if (using != null) {
if (using != null && !createIndex.isIndexTypeBeforeOn()) {
buffer.append(" USING ");
buffer.append(using);
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5302,9 +5302,21 @@ CreateIndex CreateIndex():
<K_INDEX>
[ LOOKAHEAD(2) <K_IF> <K_NOT> <K_EXISTS> { createIndex.setUsingIfNotExists(true);} ]
index = Index() { index.setType(parameter.isEmpty() ? null : parameter.get(0)); }

<K_ON> table=Table()
[ <K_USING> using=<S_IDENTIFIER> {index.setUsing(using.image);} ]
(
LOOKAHEAD(3)(
<K_ON> table=Table()
[ <K_USING> using=<S_IDENTIFIER> { index.setUsing(using.image); } ]
)
|
(
[ <K_USING> using=<S_IDENTIFIER> {
index.setUsing(using.image);
createIndex.setIndexTypeBeforeOn(true);
}
]
<K_ON> table=Table()
)
)
colNames = ColumnNamesWithParamsList()
( parameter=CreateParameter() { tailParameters.addAll(parameter); } )*
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
*/
package net.sf.jsqlparser.statement.create;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.StringReader;
import java.util.List;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
import org.junit.jupiter.api.Test;

import java.io.StringReader;
import java.util.List;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class CreateIndexTest {

private final CCJSqlParserManager parserManager = new CCJSqlParserManager();
Expand Down Expand Up @@ -142,4 +141,11 @@ void testIfNotExistsIssue1861() throws JSQLParserException {
"CREATE INDEX IF NOT EXISTS test_test_idx ON test.test USING btree (\"time\")";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testCreateIndexIssue1814() throws JSQLParserException {
String sqlStr =
"CREATE INDEX idx_operationlog_operatetime_regioncode USING BTREE ON operation_log (operate_time,region_biz_code)";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
}

0 comments on commit b0aff31

Please sign in to comment.