Class SqlUtils


  • public class SqlUtils
    extends Object
    Utility class to extract information from SQL queries.
    Since:
    1.2.0
    Author:
    Christian Beikov
    • 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 query
        start - 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)
      • 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 query
        selectIndex - 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 query
        tableName - 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 alias
        index - The start index
        Returns:
        The next alias of the char sequence