Skip to content

Commit

Permalink
Refactor SQLHintUtils logic for method reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu committed Nov 23, 2023
1 parent 3aff42f commit 799507b
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ public final class SQLHintUtils {
*/
public static Properties getSQLHintProps(final String comment) {
Properties result = new Properties();
String lowerCaseComment = comment.toLowerCase();
int startIndex = lowerCaseComment.startsWith(SQLHintTokenEnum.SQL_START_HINT_TOKEN.getAlias().toLowerCase())
? lowerCaseComment.indexOf(SQLHintTokenEnum.SQL_HINT_TOKEN.getAlias())
: lowerCaseComment.indexOf(SQLHintTokenEnum.SQL_HINT_TOKEN.getKey());
int startIndex = getStartIndex(comment, SQLHintTokenEnum.SQL_START_HINT_TOKEN, SQLHintTokenEnum.SQL_HINT_TOKEN);
if (startIndex < 0) {
return result;
}
startIndex = startIndex + SQLHintTokenEnum.SQL_HINT_TOKEN.getKey().length();
int endIndex = comment.endsWith(SQL_COMMENT_SUFFIX) ? comment.indexOf(SQL_COMMENT_SUFFIX) : comment.length();
Collection<String> sqlHints = Splitter.on(SQL_HINT_SPLIT).trimResults().splitToList(comment.substring(startIndex, endIndex).trim());
for (String each : sqlHints) {
Expand All @@ -74,6 +70,17 @@ public static Properties getSQLHintProps(final String comment) {
return result;
}

private static int getStartIndex(final String comment, final SQLHintTokenEnum sqlStartHintToken, final SQLHintTokenEnum sqlHintToken) {
String lowerCaseComment = comment.toLowerCase();
int result = lowerCaseComment.startsWith(sqlStartHintToken.getAlias().toLowerCase())
? lowerCaseComment.indexOf(sqlHintToken.getAlias())
: lowerCaseComment.indexOf(sqlHintToken.getKey());
if (result >= 0) {
return result + sqlHintToken.getKey().length();
}
return result;
}

private static Object convert(final String value) {
try {
return new BigInteger(value);
Expand All @@ -99,7 +106,7 @@ public static Collection<String> getSplitterSQLHintValue(final String value) {
* @return Hint value context
*/
public static Optional<HintValueContext> extractHint(final String sql) {
if (!startWithHint(sql)) {
if (!startWithHint(sql, SQLHintTokenEnum.SQL_START_HINT_TOKEN)) {
return Optional.empty();
}
HintValueContext result = new HintValueContext();
Expand Down Expand Up @@ -135,8 +142,8 @@ public static Optional<HintValueContext> extractHint(final String sql) {
return Optional.of(result);
}

private static boolean startWithHint(final String sql) {
return null != sql && (sql.startsWith(SQLHintTokenEnum.SQL_START_HINT_TOKEN.getKey()) || sql.startsWith(SQLHintTokenEnum.SQL_START_HINT_TOKEN.getAlias()));
private static boolean startWithHint(final String sql, final SQLHintTokenEnum sqlStartHintToken) {
return null != sql && (sql.startsWith(sqlStartHintToken.getKey()) || sql.startsWith(sqlStartHintToken.getAlias()));
}

private static boolean containsPropertyKey(final Properties hintProps, final SQLHintPropertiesKey sqlHintPropsKey) {
Expand All @@ -159,6 +166,6 @@ private static String getProperty(final Properties hintProps, final SQLHintPrope
* @return SQL after remove hint
*/
public static String removeHint(final String sql) {
return startWithHint(sql) ? sql.substring(sql.indexOf(SQL_COMMENT_SUFFIX) + 2) : sql;
return startWithHint(sql, SQLHintTokenEnum.SQL_START_HINT_TOKEN) ? sql.substring(sql.indexOf(SQL_COMMENT_SUFFIX) + 2) : sql;
}
}

0 comments on commit 799507b

Please sign in to comment.