org.eigenbase.sql.parser
Class SqlParserUtil

java.lang.Object
  extended by org.eigenbase.sql.parser.SqlParserUtil

public final class SqlParserUtil
extends Object

Utility methods relating to parsing SQL.

Since:
Oct 7, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/parser/SqlParserUtil.java#28 $
Author:
jhyde

Nested Class Summary
static class SqlParserUtil.ParsedCollation
           
static class SqlParserUtil.StringAndPos
          Contains a string, the offset of a token within the string, and a parser position containing the beginning and end line number.
static class SqlParserUtil.ToTreeListItem
          Class that holds a SqlOperator and a SqlParserPos.
 
Field Summary
static String DateFormatStr
           
static List emptyList
           
static String[] emptyStringArray
           
static String PrecisionTimeFormatStr
           
static String PrecisionTimestampFormatStr
           
static String TimeFormatStr
           
static String TimestampFormatStr
           
(package private) static Logger tracer
           
 
Constructor Summary
private SqlParserUtil()
           
 
Method Summary
static String addCarets(String sql, int line, int col, int endLine, int endCol)
          Converts a string to a string with one or two carets in it.
static void checkDateFormat(String pattern)
          Checks if the date/time format is valid
static char checkUnicodeEscapeChar(String s)
          Checks a UESCAPE string for validity, and returns the escape character if no exception is thrown.
static SqlParserUtil.StringAndPos findPos(String sql)
          Looks for one or two carets in a SQL string, and if present, converts them into a parser position.
static String getCharacterSet(String s)
           
static String getTokenVal(String token)
           
static int[] indexToLineCol(String sql, int i)
          Returns the (1-based) line and column corresponding to a particular (0-based) offset in a string.
static long intervalToMillis(SqlIntervalLiteral.IntervalValue interval)
          Converts the interval value into a millisecond representation.
static long intervalToMillis(String literal, SqlIntervalQualifier intervalQualifier)
           
static long intervalToMonths(SqlIntervalLiteral.IntervalValue interval)
          Converts the interval value into a months representation.
static long intervalToMonths(String literal, SqlIntervalQualifier intervalQualifier)
           
static int lineColToIndex(String sql, int line, int column)
          Finds the position (0-based) in a string which corresponds to a given line and column (1-based).
static byte[] parseBinaryString(String s)
          Parses a Binary string.
static SqlParserUtil.ParsedCollation parseCollation(String in)
          Extracts the values from a collation name.
static Date parseDate(String s)
          Deprecated. this method is not localized for Farrago standards
static BigDecimal parseDecimal(String s)
           
static BigDecimal parseInteger(String s)
           
static int parsePositiveInt(String value)
          Parses a positive int.
static String parseString(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).
static Time parseTime(String s)
          Deprecated. Does not parse SQL:99 milliseconds
static Timestamp parseTimestamp(String s)
          Deprecated. this method is not localized for Farrago standards
static
<T> void
replaceSublist(List<T> list, int start, int end, T o)
          Replaces a range of elements in a list with a single element.
static String rightTrim(String s, char c)
           
static String strip(String s, String quote)
          Unquotes a quoted string.
static SqlNode[] toNodeArray(List<SqlNode> list)
           
static SqlNode[] toNodeArray(SqlNodeList list)
           
static String[] toStringArray(List<String> list)
           
static SqlNode toTree(List list)
          Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.
static SqlNode toTreeEx(List<Object> list, int start, int minPrec, SqlKind stopperKind)
          Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.
static String trim(String s, String chars)
          Trims a string for given characters from left and right.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tracer

static final Logger tracer

emptyStringArray

public static final String[] emptyStringArray

emptyList

public static final List emptyList

DateFormatStr

public static final String DateFormatStr
See Also:
Constant Field Values

TimeFormatStr

public static final String TimeFormatStr
See Also:
Constant Field Values

PrecisionTimeFormatStr

public static final String PrecisionTimeFormatStr
See Also:
Constant Field Values

TimestampFormatStr

public static final String TimestampFormatStr
See Also:
Constant Field Values

PrecisionTimestampFormatStr

public static final String PrecisionTimestampFormatStr
See Also:
Constant Field Values
Constructor Detail

SqlParserUtil

private SqlParserUtil()
Method Detail

getCharacterSet

public static String getCharacterSet(String s)
Returns:
the character-set prefix of an sql string literal; returns null if there is none

parseString

public static String parseString(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 BigDecimal parseDecimal(String s)

parseInteger

public static BigDecimal parseInteger(String s)

parseDate

public static Date parseDate(String s)
Deprecated. this method is not localized for Farrago standards


parseTime

public static Time parseTime(String s)
Deprecated. Does not parse SQL:99 milliseconds


parseTimestamp

public static Timestamp parseTimestamp(String s)
Deprecated. this method is not localized for Farrago standards


checkDateFormat

public static void checkDateFormat(String pattern)
Checks if the date/time format is valid

Parameters:
pattern - SimpleDateFormat pattern

intervalToMillis

public static long intervalToMillis(SqlIntervalLiteral.IntervalValue interval)
Converts the interval value into a millisecond representation.

Parameters:
interval - Interval
Returns:
a long value that represents millisecond equivalent of the interval value.

intervalToMillis

public static long intervalToMillis(String literal,
                                    SqlIntervalQualifier intervalQualifier)

intervalToMonths

public static long intervalToMonths(SqlIntervalLiteral.IntervalValue interval)
Converts the interval value into a months representation.

Parameters:
interval - Interval
Returns:
a long value that represents months equivalent of the interval value.

intervalToMonths

public static long intervalToMonths(String literal,
                                    SqlIntervalQualifier intervalQualifier)

parsePositiveInt

public static int parsePositiveInt(String value)
                            throws NumberFormatException
Parses a positive int. All characters have to be digits.

Throws:
NumberFormatException
See Also:
Integer.parseInt(String)

parseBinaryString

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


strip

public static String strip(String s,
                           String quote)
Unquotes a quoted string. For example, strip("'it''s got quotes'") returns "it's got quotes".


trim

public static String trim(String s,
                          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(String sql)
Looks for one or two carets in a SQL string, and if present, converts them into a parser position.

Examples:


indexToLineCol

public static int[] indexToLineCol(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).


lineColToIndex

public static int lineColToIndex(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 String addCarets(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 String getTokenVal(String token)

parseCollation

public static SqlParserUtil.ParsedCollation parseCollation(String in)
Extracts the values from a collation name.

Collation names are on the form charset$locale$strength.

Parameters:
in - The collation name
Returns:
A link SqlParserUtil.ParsedCollation

toStringArray

public static String[] toStringArray(List<String> list)

toNodeArray

public static SqlNode[] toNodeArray(List<SqlNode> list)

toNodeArray

public static SqlNode[] toNodeArray(SqlNodeList list)

rightTrim

public static String rightTrim(String s,
                               char c)

replaceSublist

public static <T> void replaceSublist(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(List list)
Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.

"Precondition:"
list.size() % 2 == 1

toTreeEx

public static SqlNode toTreeEx(List<Object> list,
                               int start,
                               int minPrec,
                               SqlKind stopperKind)
Converts a list of {expression, operator, expression, ...} into a tree, taking operator precedence and associativity into account.

Parameters:
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.
Returns:
the root node of the tree which the list condenses into

checkUnicodeEscapeChar

public static char checkUnicodeEscapeChar(String s)
Checks a UESCAPE string for validity, and returns the escape character if no exception is thrown.

Parameters:
s - UESCAPE string to check
Returns:
validated escape character