Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ boolean isDdlStatement(String sql) {
* @return <code>true</code> if the statement is a SELECT statement (i.e. starts with 'SELECT').
*/
boolean isQuery(String sql) {
// Skip any query hints at the beginning of the query.
if (sql.startsWith("@")) {
sql = removeStatementHint(sql);
}
return statementStartsWith(sql, selectStatements);
}

Expand Down Expand Up @@ -378,4 +382,22 @@ static String removeCommentsAndTrim(String sql) {
}
return res.toString().trim();
}

/** Removes any statement hints at the beginning of the statement. */
static String removeStatementHint(String sql) {
// Valid statement hints at the beginning of a SQL statement can only contain a fixed set of
// possible values. Although it is possible to add a @{FORCE_INDEX=...} as a statement hint, the
// only allowed value is _BASE_TABLE. This means that we can safely assume that the statement
// hint will not contain any special characters, for example a closing curly brace, and
// that we can keep the check simple by just searching for the first occurrence of a closing
// curly brace at the end of the statement hint.
int startStatementHintIndex = sql.indexOf('{');
int endStatementHintIndex = sql.indexOf('}');
if (startStatementHintIndex == -1 || startStatementHintIndex > endStatementHintIndex) {
// Looks like an invalid statement hint. Just ignore at this point and let the caller handle
// the invalid query.
return sql;
}
return removeCommentsAndTrim(sql.substring(endStatementHintIndex + 1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,28 @@ public void testIsQuery() {
.isQuery());
}

@Test
public void testQueryHints() {
// Valid query hints.
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@ {JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{ JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN } SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN}\nSELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{\nJOIN_METHOD = HASH_JOIN \t}\n\t SELECT * FROM PersonsTable"));
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN}\n -- Single line comment\nSELECT * FROM PersonsTable"));
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN}\n /* Multi line comment\n with more comments\n */SELECT * FROM PersonsTable"));

// Invalid query hints.
assertFalse(parser.isQuery("@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
assertFalse(parser.isQuery("@JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertFalse(parser.isQuery("@JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
}

@Test
public void testIsUpdate_InsertStatements() {
assertFalse(parser.isUpdateStatement(""));
Expand Down