Skip to content

add support for index hints in Update statement for MySQL #1662

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

Merged
Merged
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
13 changes: 12 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ Update Update( List<WithItem> with ):
<K_UPDATE> { update.setOracleHint(getOracleHint()); }
[ LOOKAHEAD(2) <K_LOW_PRIORITY> { modifierPriority = UpdateModifierPriority.LOW_PRIORITY; }]
[ LOOKAHEAD(2) <K_IGNORE> { modifierIgnore = true; }]
table=TableWithAlias() startJoins=JoinsList()
table=TableWithAliasAndMysqlIndexHint() startJoins=JoinsList()
<K_SET>
(
LOOKAHEAD(3) tableColumn=Column() "=" valueExpression=SimpleExpression() { update.addUpdateSet(tableColumn, valueExpression); }
Expand Down Expand Up @@ -1914,6 +1914,17 @@ Table TableWithAlias():
{ return table; }
}

Table TableWithAliasAndMysqlIndexHint():
{
Table table = null;
Alias alias = null;
MySQLIndexHint indexHint = null;
}
{
table=Table() [alias=Alias() { table.setAlias(alias); }] [indexHint=MySQLIndexHint() {table.setHint(indexHint);}]
{ return table; }
}

Select SelectWithWithItems( ):
{
Select select;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public void testOracleHint() throws JSQLParserException {
// assertOracleHintExists("UPDATE mytable /*+ SOMEHINT */ set col1='as', col2=?, col3=565 Where o >= 3", true, "SOMEHINT");
}

@Test
public void testMysqlHint() throws JSQLParserException {
assertUpdateMysqlHintExists("UPDATE demo FORCE INDEX (idx_demo) SET col1 = NULL WHERE col2 = 1", true, "FORCE", "INDEX", "idx_demo");
}

@Test
public void testWith() throws JSQLParserException {
String statement
Expand Down
22 changes: 19 additions & 3 deletions src/test/java/net/sf/jsqlparser/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.stream.Stream;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.MySQLIndexHint;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.parser.CCJSqlParser;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
Expand All @@ -37,11 +38,11 @@
import org.apache.commons.lang3.builder.MultilineRecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

/**
*
* @author toben
Expand Down Expand Up @@ -342,4 +343,19 @@ public static void assertOracleHintExists(String sql, boolean assertDeparser, St
assertEquals(hints[0], hint.getValue());
}
}

public static void assertUpdateMysqlHintExists(String sql, boolean assertDeparser, String action, String qualifier, String... indexNames)
throws JSQLParserException {
if (assertDeparser) {
assertSqlCanBeParsedAndDeparsed(sql, true);
}
Statement statement = CCJSqlParserUtil.parse(sql);
assertInstanceOf(Update.class, statement);
Update updateStmt = (Update) statement;
final MySQLIndexHint indexHint = updateStmt.getTable().getIndexHint();
assertNotNull(indexHint);
assertEquals(indexHint.getAction(), action);
assertEquals(indexHint.getIndexQualifier(), qualifier);
assertArrayEquals(indexHint.getIndexNames().toArray(), indexNames);
}
}