Class SqlUtils
- java.lang.Object
-
- com.blazebit.persistence.impl.util.SqlUtils
-
public class SqlUtils extends Object
Utility class to extract information from SQL queries.- Since:
- 1.2.0
- Author:
- Christian Beikov
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSqlUtils.SelectItemExtractor
-
Field Summary
Fields Modifier and Type Field Description static StringASstatic PatternFinderAS_FINDERstatic StringFETCH_FIRSTstatic PatternFinderFETCH_FIRST_FINDERstatic StringFROMstatic StringFROM_FINAL_TABLEstatic PatternFinderFROM_FINAL_TABLE_FINDERstatic PatternFinderFROM_FINDERstatic StringJOINstatic PatternFinderJOIN_FINDERstatic StringLIMITstatic PatternFinderLIMIT_FINDERstatic StringNEXT_VALUE_FORstatic PatternFinderNEXT_VALUE_FOR_FINDERstatic StringORDER_BYstatic PatternFinderORDER_BY_FINDERstatic StringSELECTstatic PatternFinderSELECT_FINDERstatic StringSETstatic PatternFinderSET_FINDERstatic StringWHEREstatic PatternFinderWHERE_FINDERstatic StringWITHstatic PatternFinderWITH_FINDER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidapplyTableNameRemapping(StringBuilder sb, String sqlAlias, String newCteName, String aliasExtension, String newSqlAlias, boolean useApply)static StringextractAlias(CharSequence sb, int index)Extracts the next alias from the given expression starting the given index.static StringextractAlias(StringBuilder sb)Extracts the alias part of a select item expression.static intfindEndOfOnClause(CharSequence sqlSb, int predicateStartIndex, int whereIndex)static intfindJoinStartIndex(CharSequence sqlSb, int aliasIndex)static intfindJoinStartIndex(CharSequence sqlSb, int tokenEnd, Set<com.blazebit.persistence.impl.util.SqlUtils.JoinToken> allowedTokens)static List<String>getExpressionItems(CharSequence sql)static List<String>getExpressionItems(CharSequence sql, int i, int end)static List<String>getExpressionItems(CharSequence sql, int i, int end, SqlUtils.SelectItemExtractor extractor)static String[]getSelectItemAliases(CharSequence sql, int start)Extracts the select item aliases of an arbitrary SELECT query.static String[]getSelectItemColumns(CharSequence sql, int start)static String[]getSelectItemExpressions(CharSequence sql, int start)static String[]getSelectItems(CharSequence sql, int start, SqlUtils.SelectItemExtractor extractor)static CharSequencegetSetElementSequence(CharSequence sql)static intindexOfFetchFirst(CharSequence sql, int start)static int[]indexOfFinalTableSubquery(CharSequence sql, int selectIndex)Finds the final table clause in an arbitrary SELECT query.static intindexOfFrom(CharSequence sql)static intindexOfFrom(CharSequence sql, int start)static int[]indexOfFullJoin(CharSequence sql, String tableAlias)static int[]indexOfFullJoin(CharSequence sql, String tableAlias, int whereIndex)static int[]indexOfFullJoin(CharSequence sql, String tableAlias, int aliasIndex, int whereIndex)static intindexOfJoinTableAlias(CharSequence sql, String tableName)static intindexOfLimit(CharSequence sql)Finds the toplevel LIMIT keyword in an arbitrary query.static intindexOfLimit(CharSequence sql, int start)static intindexOfOrderBy(CharSequence sql)Finds the toplevel ORDER BY keyword in an arbitrary query.static intindexOfOrderBy(CharSequence sql, int start)static intindexOfSelect(CharSequence sql)Finds the toplevel SELECT keyword in an arbitrary SELECT query.static intindexOfTableName(CharSequence sql, String tableName)Finds the table name within a FROM clause of an arbitrary SELECT query.static intindexOfWhere(CharSequence sql)Finds the toplevel WHERE keyword in an arbitrary query.static intindexOfWhere(CharSequence sql, int start)static booleanisIdentifier(char c)static booleanisIdentifierStart(char c)static int[]rtrimBackwardsToFirstWhitespace(CharSequence sb, int startIndex)
-
-
-
Field Detail
-
SELECT
public static final String SELECT
- See Also:
- Constant Field Values
-
SET
public static final String SET
- See Also:
- Constant Field Values
-
FROM
public static final String FROM
- See Also:
- Constant Field Values
-
JOIN
public static final String JOIN
- See Also:
- Constant Field Values
-
WITH
public static final String WITH
- See Also:
- Constant Field Values
-
WHERE
public static final String WHERE
- See Also:
- Constant Field Values
-
ORDER_BY
public static final String ORDER_BY
- See Also:
- Constant Field Values
-
LIMIT
public static final String LIMIT
- See Also:
- Constant Field Values
-
FETCH_FIRST
public static final String FETCH_FIRST
- See Also:
- Constant Field Values
-
AS
public static final String AS
- See Also:
- Constant Field Values
-
FROM_FINAL_TABLE
public static final String FROM_FINAL_TABLE
- See Also:
- Constant Field Values
-
NEXT_VALUE_FOR
public static final String NEXT_VALUE_FOR
- See Also:
- Constant Field Values
-
SELECT_FINDER
public static final PatternFinder SELECT_FINDER
-
SET_FINDER
public static final PatternFinder SET_FINDER
-
FROM_FINDER
public static final PatternFinder FROM_FINDER
-
JOIN_FINDER
public static final PatternFinder JOIN_FINDER
-
WITH_FINDER
public static final PatternFinder WITH_FINDER
-
WHERE_FINDER
public static final PatternFinder WHERE_FINDER
-
ORDER_BY_FINDER
public static final PatternFinder ORDER_BY_FINDER
-
LIMIT_FINDER
public static final PatternFinder LIMIT_FINDER
-
FETCH_FIRST_FINDER
public static final PatternFinder FETCH_FIRST_FINDER
-
AS_FINDER
public static final PatternFinder AS_FINDER
-
FROM_FINAL_TABLE_FINDER
public static final PatternFinder FROM_FINAL_TABLE_FINDER
-
NEXT_VALUE_FOR_FINDER
public static final PatternFinder NEXT_VALUE_FOR_FINDER
-
-
Method Detail
-
applyTableNameRemapping
public static void applyTableNameRemapping(StringBuilder sb, String sqlAlias, String newCteName, String aliasExtension, String newSqlAlias, boolean useApply)
-
rtrimBackwardsToFirstWhitespace
public static int[] rtrimBackwardsToFirstWhitespace(CharSequence sb, int startIndex)
-
isIdentifierStart
public static boolean isIdentifierStart(char c)
-
isIdentifier
public static boolean isIdentifier(char c)
-
getSelectItemAliases
public static String[] getSelectItemAliases(CharSequence sql, int start)
Extracts the select item aliases of an arbitrary SELECT query.- Parameters:
sql- The SQL querystart- The start index from which to look for select items- Returns:
- The select item aliases
-
getSelectItemExpressions
public static String[] getSelectItemExpressions(CharSequence sql, int start)
-
getSelectItemColumns
public static String[] getSelectItemColumns(CharSequence sql, int start)
-
getSelectItems
public static String[] getSelectItems(CharSequence sql, int start, SqlUtils.SelectItemExtractor extractor)
-
getExpressionItems
public static List<String> getExpressionItems(CharSequence sql)
-
getExpressionItems
public static List<String> getExpressionItems(CharSequence sql, int i, int end)
-
getExpressionItems
public static List<String> getExpressionItems(CharSequence sql, int i, int end, SqlUtils.SelectItemExtractor extractor)
-
getSetElementSequence
public static CharSequence getSetElementSequence(CharSequence sql)
-
indexOfSelect
public static int indexOfSelect(CharSequence sql)
Finds the toplevel SELECT keyword in an arbitrary SELECT query.- Parameters:
sql- The SQL query- Returns:
- The index of the SELECT keyword if found, or -1
-
indexOfFrom
public static int indexOfFrom(CharSequence sql)
-
indexOfFrom
public static int indexOfFrom(CharSequence sql, int start)
-
indexOfWhere
public static int indexOfWhere(CharSequence sql)
Finds the toplevel WHERE keyword in an arbitrary query.- Parameters:
sql- The SQL query- Returns:
- The index of the SELECT keyword if found, or -1
-
indexOfWhere
public static int indexOfWhere(CharSequence sql, int start)
-
indexOfOrderBy
public static int indexOfOrderBy(CharSequence sql)
Finds the toplevel ORDER BY keyword in an arbitrary query.- Parameters:
sql- The SQL query- Returns:
- The index of the ORDER BY keyword if found, or -1
-
indexOfOrderBy
public static int indexOfOrderBy(CharSequence sql, int start)
-
indexOfLimit
public static int indexOfLimit(CharSequence sql)
Finds the toplevel LIMIT keyword in an arbitrary query.- Parameters:
sql- The SQL query- Returns:
- The index of the LIMIT keyword if found, or -1
-
indexOfLimit
public static int indexOfLimit(CharSequence sql, int start)
-
indexOfFetchFirst
public static int indexOfFetchFirst(CharSequence sql, int start)
-
indexOfFinalTableSubquery
public static int[] indexOfFinalTableSubquery(CharSequence sql, int selectIndex)
Finds the final table clause in an arbitrary SELECT query.- Parameters:
sql- The SQL queryselectIndex- The start index or the index of the toplevel SELECT keyword in the query- Returns:
- The start and end index of the final table subquery if found, or 0 and the length of the query
-
indexOfTableName
public static int indexOfTableName(CharSequence sql, String tableName)
Finds the table name within a FROM clause of an arbitrary SELECT query.- Parameters:
sql- The SQL querytableName- The table name to look for- Returns:
- The index of the table name or -1 if it couldn't be found
-
indexOfJoinTableAlias
public static int indexOfJoinTableAlias(CharSequence sql, String tableName)
-
indexOfFullJoin
public static int[] indexOfFullJoin(CharSequence sql, String tableAlias)
-
indexOfFullJoin
public static int[] indexOfFullJoin(CharSequence sql, String tableAlias, int whereIndex)
-
indexOfFullJoin
public static int[] indexOfFullJoin(CharSequence sql, String tableAlias, int aliasIndex, int whereIndex)
-
findJoinStartIndex
public static int findJoinStartIndex(CharSequence sqlSb, int aliasIndex)
-
findJoinStartIndex
public static int findJoinStartIndex(CharSequence sqlSb, int tokenEnd, Set<com.blazebit.persistence.impl.util.SqlUtils.JoinToken> allowedTokens)
-
findEndOfOnClause
public static int findEndOfOnClause(CharSequence sqlSb, int predicateStartIndex, int whereIndex)
-
extractAlias
public static String extractAlias(StringBuilder sb)
Extracts the alias part of a select item expression.- Parameters:
sb- The string builder containing the select item expression- Returns:
- The alias of the select item expression
-
extractAlias
public static String extractAlias(CharSequence sb, int index)
Extracts the next alias from the given expression starting the given index.- Parameters:
sb- The char sequence containing the aliasindex- The start index- Returns:
- The next alias of the char sequence
-
-