Class SqlParserUtil

  • public final class SqlParserUtil
    extends java.lang.Object
    Utility methods relating to parsing SQL.
    • Field Detail

      • LOGGER

        static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • SqlParserUtil

        private SqlParserUtil()
    • Method Detail

      • getCharacterSet

        public static java.lang.String getCharacterSet​(java.lang.String s)
        the character-set prefix of an sql string literal; returns null if there is none
      • parseString

        public static java.lang.String parseString​(java.lang.String s)
        Converts the contents of an sql quoted string literal into the corresponding Java string representation (removing leading and trailing quotes and unescaping internal doubled quotes).
      • parseDecimal

        public static java.math.BigDecimal parseDecimal​(java.lang.String s)
      • parseInteger

        public static java.math.BigDecimal parseInteger​(java.lang.String s)
      • parseDate

        public static java.sql.Date parseDate​(java.lang.String s)
        this method is not localized for Farrago standards
      • parseTime

        public static java.sql.Time parseTime​(java.lang.String s)
        Does not parse SQL:99 milliseconds
      • parseTimestamp

        public static java.sql.Timestamp parseTimestamp​(java.lang.String s)
        this method is not localized for Farrago standards
      • checkDateFormat

        public static void checkDateFormat​(java.lang.String pattern)
        Checks if the date/time format is valid
        pattern - SimpleDateFormat pattern
      • intervalToMillis

        public static long intervalToMillis​(SqlIntervalLiteral.IntervalValue interval)
        Converts the interval value into a millisecond representation.
        interval - Interval
        a long value that represents millisecond equivalent of the interval value.
      • intervalToMillis

        public static long intervalToMillis​(java.lang.String literal,
                                            SqlIntervalQualifier intervalQualifier)
      • intervalToMonths

        public static long intervalToMonths​(SqlIntervalLiteral.IntervalValue interval)
        Converts the interval value into a months representation.
        interval - Interval
        a long value that represents months equivalent of the interval value.
      • intervalToMonths

        public static long intervalToMonths​(java.lang.String literal,
                                            SqlIntervalQualifier intervalQualifier)
      • parsePositiveInt

        public static int parsePositiveInt​(java.lang.String value)
        Parses a positive int. All characters have to be digits.
        java.lang.NumberFormatException - if invalid number or leading '-'
        See Also:
      • parseBinaryString

        public static byte[] parseBinaryString​(java.lang.String s)
        Parses a Binary string. SQL:99 defines a binary string as a hexstring with EVEN nbr of hex digits.
      • strip

        public static java.lang.String strip​(java.lang.String s,
                                             java.lang.String startQuote,
                                             java.lang.String endQuote,
                                             java.lang.String escape,
                                             org.apache.calcite.avatica.util.Casing casing)
        Unquotes a quoted string, using different quotes for beginning and end.
      • trim

        public static java.lang.String trim​(java.lang.String s,
                                            java.lang.String chars)
        Trims a string for given characters from left and right. E.g. trim("aBaac123AabC","abBcC") returns "123A".
      • findPos

        public static SqlParserUtil.StringAndPos findPos​(java.lang.String sql)
        Looks for one or two carets in a SQL string, and if present, converts them into a parser position.


        • findPos("xxx^yyy") yields {"xxxyyy", position 3, line 1 column 4}
        • findPos("xxxyyy") yields {"xxxyyy", null}
        • findPos("xxx^yy^y") yields {"xxxyyy", position 3, line 4 column 4 through line 1 column 6}
      • indexToLineCol

        public static int[] indexToLineCol​(java.lang.String sql,
                                           int i)
        Returns the (1-based) line and column corresponding to a particular (0-based) offset in a string.

        Converse of lineColToIndex(String, int, int).

      • nextLine

        public static int nextLine​(java.lang.String sql,
                                   int j)
      • lineColToIndex

        public static int lineColToIndex​(java.lang.String sql,
                                         int line,
                                         int column)
        Finds the position (0-based) in a string which corresponds to a given line and column (1-based).

        Converse of indexToLineCol(String, int).

      • addCarets

        public static java.lang.String addCarets​(java.lang.String sql,
                                                 int line,
                                                 int col,
                                                 int endLine,
                                                 int endCol)
        Converts a string to a string with one or two carets in it. For example, addCarets("values (foo)", 1, 9, 1, 12) yields "values (^foo^)".
      • getTokenVal

        public static java.lang.String getTokenVal​(java.lang.String token)
      • toStringArray

        public static java.lang.String[] toStringArray​(java.util.List<java.lang.String> list)
      • toNodeArray

        public static SqlNode[] toNodeArray​(java.util.List<SqlNode> list)
      • rightTrim

        public static java.lang.String rightTrim​(java.lang.String s,
                                                 char c)
      • replaceSublist

        public static <T> void replaceSublist​(java.util.List<T> list,
                                              int start,
                                              int end,
                                              T o)
        Replaces a range of elements in a list with a single element. For example, if list contains {A, B, C, D, E} then replaceSublist(list, X, 1, 4) returns {A, X, E}.
      • toTree

        public static SqlNode toTree​(java.util.List<java.lang.Object> list)
        Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.
      • toTreeEx

        public static SqlNode toTreeEx​(SqlSpecialOperator.TokenSequence list,
                                       int start,
                                       int minPrec,
                                       SqlKind stopperKind)
        Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.
        list - List of operands and operators. This list is modified as expressions are reduced.
        start - Position of first operand in the list. Anything to the left of this (besides the immediately preceding operand) is ignored. Generally use value 1.
        minPrec - Minimum precedence to consider. If the method encounters an operator of lower precedence, it doesn't reduce any further.
        stopperKind - If not SqlKind.OTHER, stop reading the list if we encounter a token of this kind.
        the root node of the tree which the list condenses into
      • checkUnicodeEscapeChar

        public static char checkUnicodeEscapeChar​(java.lang.String s)
        Checks a UESCAPE string for validity, and returns the escape character if no exception is thrown.
        s - UESCAPE string to check
        validated escape character