Class SqlUtils
java.lang.Object
com.blazebit.persistence.impl.util.SqlUtils
Utility class to extract information from SQL queries.
- Since:
- 1.2.0
- Author:
- Christian Beikov
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final String
static final PatternFinder
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
static final String
static final PatternFinder
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
applyTableNameRemapping
(StringBuilder sb, ExtendedQuerySupport.SqlFromInfo sqlFromInfo, String newCteName, String aliasExtension, String newSqlAlias, boolean useApply) static void
applyTableNameRemapping
(StringBuilder sb, String sqlAlias, String newCteName, String aliasExtension, String newSqlAlias, boolean useApply) static void
buildAliasMappingForTopLevelSelects
(CharSequence sql, String alias, Map<String, String> aliasMapping) static String
extractAlias
(CharSequence sb, int index) Extracts the next alias from the given expression starting the given index.static String
Extracts the alias part of a select item expression.static int
findEndOfOnClause
(CharSequence sqlSb, int predicateStartIndex, int whereIndex) static int
findJoinStartIndex
(CharSequence sqlSb, int aliasIndex) static int
findJoinStartIndex
(CharSequence sqlSb, int tokenEnd, Set<com.blazebit.persistence.impl.util.SqlUtils.JoinToken> allowedTokens) getExpressionItems
(CharSequence sql, int i, int end) 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 int
indexOfFetchFirst
(CharSequence sql, int start) static int[]
indexOfFinalTableSubquery
(CharSequence sql, int selectIndex) Finds the final table clause in an arbitrary SELECT query.static int
indexOfFrom
(CharSequence sql) static int
indexOfFrom
(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 int
indexOfGroupBy
(CharSequence sql, int start) static int
indexOfHaving
(CharSequence sql, int start) static int
indexOfJoinTableAlias
(CharSequence sql, String tableAlias) static int
indexOfLimit
(CharSequence sql) Finds the toplevel LIMIT keyword in an arbitrary query.static int
indexOfLimit
(CharSequence sql, int start) static int
indexOfOn
(CharSequence sql, int start) static int
Finds the toplevel ORDER BY keyword in an arbitrary query.static int
indexOfOrderBy
(CharSequence sql, int start) static int
Finds the toplevel SELECT keyword in an arbitrary SELECT query.static int
indexOfSet
(CharSequence sql) static int
indexOfTableName
(CharSequence sql, String tableName) Finds the table name within a FROM clause of an arbitrary SELECT query.static int
indexOfWhere
(CharSequence sql) Finds the toplevel WHERE keyword in an arbitrary query.static int
indexOfWhere
(CharSequence sql, int start) static boolean
isIdentifier
(char c) static boolean
isIdentifierStart
(char c) static void
remapColumnExpressions
(StringBuilder sqlSb, Map<String, String> columnExpressionRemappings) static int
remapColumnExpressions
(StringBuilder sqlSb, Map<String, String> columnExpressionRemappings, int startIndex, int endIndex) static int[]
rtrimBackwardsToFirstWhitespace
(CharSequence sb, int startIndex)
-
Field Details
-
SELECT
- See Also:
-
SET
- See Also:
-
FROM
- See Also:
-
JOIN
- See Also:
-
ON
- See Also:
-
WITH
- See Also:
-
WHERE
- See Also:
-
GROUP_BY
- See Also:
-
HAVING
- See Also:
-
ORDER_BY
- See Also:
-
LIMIT
- See Also:
-
FETCH_FIRST
- See Also:
-
AS
- See Also:
-
FROM_FINAL_TABLE
- See Also:
-
NEXT_VALUE_FOR
- See Also:
-
SELECT_FINDER
-
SET_FINDER
-
FROM_FINDER
-
JOIN_FINDER
-
ON_FINDER
-
WITH_FINDER
-
WHERE_FINDER
-
GROUP_BY_FINDER
-
HAVING_FINDER
-
ORDER_BY_FINDER
-
LIMIT_FINDER
-
FETCH_FIRST_FINDER
-
AS_FINDER
-
FROM_FINAL_TABLE_FINDER
-
NEXT_VALUE_FOR_FINDER
-
-
Method Details
-
applyTableNameRemapping
public static void applyTableNameRemapping(StringBuilder sb, ExtendedQuerySupport.SqlFromInfo sqlFromInfo, String newCteName, String aliasExtension, String newSqlAlias, boolean useApply) -
applyTableNameRemapping
public static void applyTableNameRemapping(StringBuilder sb, String sqlAlias, String newCteName, String aliasExtension, String newSqlAlias, boolean useApply) -
remapColumnExpressions
public static void remapColumnExpressions(StringBuilder sqlSb, Map<String, String> columnExpressionRemappings) -
remapColumnExpressions
public static int remapColumnExpressions(StringBuilder sqlSb, Map<String, String> columnExpressionRemappings, int startIndex, int endIndex) -
rtrimBackwardsToFirstWhitespace
-
isIdentifierStart
public static boolean isIdentifierStart(char c) -
isIdentifier
public static boolean isIdentifier(char c) -
getSelectItemAliases
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
-
getSelectItemColumns
-
getSelectItems
public static String[] getSelectItems(CharSequence sql, int start, SqlUtils.SelectItemExtractor extractor) -
getExpressionItems
-
getExpressionItems
-
getExpressionItems
public static List<String> getExpressionItems(CharSequence sql, int i, int end, SqlUtils.SelectItemExtractor extractor) -
buildAliasMappingForTopLevelSelects
public static void buildAliasMappingForTopLevelSelects(CharSequence sql, String alias, Map<String, String> aliasMapping) -
indexOfSelect
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
-
indexOfSet
-
indexOfFrom
-
indexOfFrom
-
indexOfWhere
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
-
indexOfGroupBy
-
indexOfHaving
-
indexOfOrderBy
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
-
indexOfLimit
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
-
indexOfFetchFirst
-
indexOfOn
-
indexOfFinalTableSubquery
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
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
-
indexOfFullJoin
-
indexOfFullJoin
-
indexOfFullJoin
public static int[] indexOfFullJoin(CharSequence sql, String tableAlias, int aliasIndex, int whereIndex) -
findJoinStartIndex
-
findJoinStartIndex
public static int findJoinStartIndex(CharSequence sqlSb, int tokenEnd, Set<com.blazebit.persistence.impl.util.SqlUtils.JoinToken> allowedTokens) -
findEndOfOnClause
-
extractAlias
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
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
-