org.eigenbase.sql
Class SqlUtil

java.lang.Object
  extended by org.eigenbase.sql.SqlUtil

public abstract class SqlUtil
extends Object

Contains utility functions related to SQL parsing, all static.

Since:
Nov 26, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/SqlUtil.java#35 $
Author:
jhyde

Nested Class Summary
static class SqlUtil.DatabaseMetaDataInvocationHandler
          Handles particular DatabaseMetaData methods; invocations of other methods will fall through to the base class, BarfingInvocationHandler, which will throw an error.
 
Field Summary
static SqlDialect dummyDialect
          A SqlDialect useful for generating generic SQL.
static SqlDialect eigenbaseDialect
          A SqlDialect useful for generating SQL which can be parsed by the Eigenbase parser, but not much else.
 
Constructor Summary
SqlUtil()
           
 
Method Summary
(package private) static SqlNode andExpressions(SqlNode node1, SqlNode node2)
           
static SqlLiteral concatenateLiterals(SqlLiteral[] lits)
          Concatenates string literals.
static RelDataType createNlsStringType(RelDataTypeFactory typeFactory, NlsString str)
          Creates the type of an NlsString.
static String deriveAliasFromOrdinal(int ordinal)
           
private static DatabaseMetaData dummyDatabaseMetaData(String databaseProductName, String identifierQuoteString)
          Creates a DatabaseMetaData object good enough to create a SqlDialect object with, but not good for much else.
private static void filterRoutinesByParameterCount(List<SqlFunction> routines, RelDataType[] argTypes)
           
private static void filterRoutinesByParameterType(List<SqlFunction> routines, RelDataType[] argTypes)
           
private static void filterRoutinesByTypePrecedence(List<SqlFunction> routines, RelDataType[] argTypes)
           
(package private) static ArrayList<SqlNode> flatten(SqlNode node)
           
private static void flatten(SqlNode node, ArrayList<SqlNode> list)
           
static String getAliasedSignature(SqlOperator op, String opName, List<? extends Object> typeList)
          Constructs an operator signature from a type list, substituting an alias for the operator name.
static SqlNode getFromNode(SqlSelect query, int ordinal)
          Returns the nth (0-based) input to a join expression.
static String getOperatorSignature(SqlOperator op, List<? extends Object> typeList)
          Constructs an operator signature from a type list.
static SqlNode getSelectListItem(SqlNode query, int i)
          Returns the ith select-list item of a query.
static boolean isCallTo(SqlNode node, SqlOperator operator)
          Returns whether a node is a call to a given operator.
static boolean isLiteral(SqlNode node)
          Returns whether a node is a literal.
static boolean isLiteralChain(SqlNode node)
          Returns whether a node is a literal chain which is used to represent a continued string literal.
static boolean isNull(SqlNode node)
          Returns whether a node represents the NULL value or a series of nested CAST(NULL as ) calls
For Example:
isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1))) returns true
static boolean isNullLiteral(SqlNode node, boolean allowCast)
          Returns whether a node represents the NULL value.
static SqlFunction lookupRoutine(SqlOperatorTable opTab, SqlIdentifier funcName, RelDataType[] argTypes, SqlFunctionCategory category)
          Looks up a (possibly overloaded) routine based on name and argument types.
static List<SqlFunction> lookupSubjectRoutines(SqlOperatorTable opTab, SqlIdentifier funcName, RelDataType[] argTypes, SqlFunctionCategory category)
          Looks up all subject routines matching the given name and argument types.
private static List<SqlFunction> lookupSubjectRoutinesByName(SqlOperatorTable opTab, SqlIdentifier funcName, SqlFunctionCategory category)
           
static SqlCall makeCall(SqlOperatorTable opTab, SqlIdentifier id)
          If an identifier is a legitimate call to a function which has no arguments and requires no parentheses (for example "CURRENT_USER"), returns a call to that function, otherwise returns null.
static boolean matchRoutinesByParameterCount(SqlOperatorTable opTab, SqlIdentifier funcName, RelDataType[] argTypes, SqlFunctionCategory category)
          Determine if there is a routine matching the given name and number of arguments.
static EigenbaseException newContextException(int line, int col, int endLine, int endCol, Throwable e)
          Wraps an exception with context.
static EigenbaseException newContextException(SqlParserPos pos, Throwable e)
          Wraps an exception with context.
static EigenbaseException newContextException(SqlParserPos pos, Throwable e, String inputText)
          Wraps an exception with context.
static SqlNodeList toNodeList(SqlNode[] operands)
          Converts an SqlNode array to a SqlNodeList
static String translateCharacterSetName(String name)
          Translates a character set name from a SQL-level name into a Java-level name.
static void unparseBinarySyntax(SqlOperator operator, SqlNode[] operands, SqlWriter writer, int leftPrec, int rightPrec)
           
static void unparseFunctionSyntax(SqlOperator operator, SqlWriter writer, SqlNode[] operands, boolean emptyParens, SqlLiteral quantifier)
          Unparses a call to an operator which has function syntax.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dummyDialect

public static final SqlDialect dummyDialect
A SqlDialect useful for generating generic SQL. If you need to do something database-specific like quoting identifiers, don't rely on this dialect to do what you want.


eigenbaseDialect

public static final SqlDialect eigenbaseDialect
A SqlDialect useful for generating SQL which can be parsed by the Eigenbase parser, but not much else.

Constructor Detail

SqlUtil

public SqlUtil()
Method Detail

andExpressions

static SqlNode andExpressions(SqlNode node1,
                              SqlNode node2)

flatten

static ArrayList<SqlNode> flatten(SqlNode node)

getFromNode

public static SqlNode getFromNode(SqlSelect query,
                                  int ordinal)
Returns the nth (0-based) input to a join expression.


flatten

private static void flatten(SqlNode node,
                            ArrayList<SqlNode> list)

toNodeList

public static SqlNodeList toNodeList(SqlNode[] operands)
Converts an SqlNode array to a SqlNodeList


isNullLiteral

public static boolean isNullLiteral(SqlNode node,
                                    boolean allowCast)
Returns whether a node represents the NULL value.

Examples:


isNull

public static boolean isNull(SqlNode node)
Returns whether a node represents the NULL value or a series of nested CAST(NULL as ) calls
For Example:
isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1))) returns true


isLiteral

public static boolean isLiteral(SqlNode node)
Returns whether a node is a literal.

Many constructs which require literals also accept CAST(NULL AS type). This method does not accept casts, so you should call isNullLiteral(org.eigenbase.sql.SqlNode, boolean) first.

Parameters:
node - The node, never null.
Returns:
Whether the node is a literal
"Precondition:"
node != null

isLiteralChain

public static boolean isLiteralChain(SqlNode node)
Returns whether a node is a literal chain which is used to represent a continued string literal.

Parameters:
node - The node, never null.
Returns:
Whether the node is a literal chain
"Precondition:"
node != null

unparseFunctionSyntax

public static void unparseFunctionSyntax(SqlOperator operator,
                                         SqlWriter writer,
                                         SqlNode[] operands,
                                         boolean emptyParens,
                                         SqlLiteral quantifier)
Unparses a call to an operator which has function syntax.

Parameters:
operator - The operator
writer - Writer
operands - List of 0 or more operands
emptyParens - Whether to print parentheses if there are 0 operands
quantifier -

unparseBinarySyntax

public static void unparseBinarySyntax(SqlOperator operator,
                                       SqlNode[] operands,
                                       SqlWriter writer,
                                       int leftPrec,
                                       int rightPrec)

dummyDatabaseMetaData

private static DatabaseMetaData dummyDatabaseMetaData(String databaseProductName,
                                                      String identifierQuoteString)
Creates a DatabaseMetaData object good enough to create a SqlDialect object with, but not good for much else.

Parameters:
databaseProductName - Database product name
identifierQuoteString - Identifier quote string

concatenateLiterals

public static SqlLiteral concatenateLiterals(SqlLiteral[] lits)
Concatenates string literals.

This method takes an array of arguments, since pairwise concatenation means too much string copying.

Parameters:
lits - an array of SqlLiteral, not empty, all of the same class
Returns:
a new SqlLiteral, of that same class, whose value is the string concatenation of the values of the literals
Throws:
ClassCastException - if the lits are not homogeneous.
ArrayIndexOutOfBoundsException - if lits is an empty array.

lookupRoutine

public static SqlFunction lookupRoutine(SqlOperatorTable opTab,
                                        SqlIdentifier funcName,
                                        RelDataType[] argTypes,
                                        SqlFunctionCategory category)
Looks up a (possibly overloaded) routine based on name and argument types.

Parameters:
opTab - operator table to search
funcName - name of function being invoked
argTypes - argument types
category - whether a function or a procedure. (If a procedure is being invoked, the overload rules are simpler.)
Returns:
matching routine, or null if none found
Reference to SQL:1999 standard:
Part 2 Section 10.4

lookupSubjectRoutines

public static List<SqlFunction> lookupSubjectRoutines(SqlOperatorTable opTab,
                                                      SqlIdentifier funcName,
                                                      RelDataType[] argTypes,
                                                      SqlFunctionCategory category)
Looks up all subject routines matching the given name and argument types.

Parameters:
opTab - operator table to search
funcName - name of function being invoked
argTypes - argument types
category - category of routine to look up
Returns:
list of matching routines
Reference to SQL:1999 standard:
Part 2 Section 10.4

matchRoutinesByParameterCount

public static boolean matchRoutinesByParameterCount(SqlOperatorTable opTab,
                                                    SqlIdentifier funcName,
                                                    RelDataType[] argTypes,
                                                    SqlFunctionCategory category)
Determine if there is a routine matching the given name and number of arguments.

Parameters:
opTab - operator table to search
funcName - name of function being invoked
argTypes - argument types
category - category of routine to look up
Returns:
true if match found

lookupSubjectRoutinesByName

private static List<SqlFunction> lookupSubjectRoutinesByName(SqlOperatorTable opTab,
                                                             SqlIdentifier funcName,
                                                             SqlFunctionCategory category)

filterRoutinesByParameterCount

private static void filterRoutinesByParameterCount(List<SqlFunction> routines,
                                                   RelDataType[] argTypes)

filterRoutinesByParameterType

private static void filterRoutinesByParameterType(List<SqlFunction> routines,
                                                  RelDataType[] argTypes)
Reference to SQL:1999 standard:
Part 2 Section 10.4 Syntax Rule 6.b.iii.2.B

filterRoutinesByTypePrecedence

private static void filterRoutinesByTypePrecedence(List<SqlFunction> routines,
                                                   RelDataType[] argTypes)
Reference to SQL:1999 standard:
Part 2 Section 9.4

getSelectListItem

public static SqlNode getSelectListItem(SqlNode query,
                                        int i)
Returns the ith select-list item of a query.


makeCall

public static SqlCall makeCall(SqlOperatorTable opTab,
                               SqlIdentifier id)
If an identifier is a legitimate call to a function which has no arguments and requires no parentheses (for example "CURRENT_USER"), returns a call to that function, otherwise returns null.


deriveAliasFromOrdinal

public static String deriveAliasFromOrdinal(int ordinal)

getOperatorSignature

public static String getOperatorSignature(SqlOperator op,
                                          List<? extends Object> typeList)
Constructs an operator signature from a type list.

Parameters:
op - operator
typeList - list of types to use for operands. Types may be represented as String, SqlTypeFamily, or any object with a valid Object.toString() method.
Returns:
constructed signature

getAliasedSignature

public static String getAliasedSignature(SqlOperator op,
                                         String opName,
                                         List<? extends Object> typeList)
Constructs an operator signature from a type list, substituting an alias for the operator name.

Parameters:
op - operator
opName - name to use for operator
typeList - list of SqlTypeName or String to use for operands
Returns:
constructed signature

newContextException

public static EigenbaseException newContextException(SqlParserPos pos,
                                                     Throwable e,
                                                     String inputText)
Wraps an exception with context.


newContextException

public static EigenbaseException newContextException(SqlParserPos pos,
                                                     Throwable e)
Wraps an exception with context.


newContextException

public static EigenbaseException newContextException(int line,
                                                     int col,
                                                     int endLine,
                                                     int endCol,
                                                     Throwable e)
Wraps an exception with context.


isCallTo

public static boolean isCallTo(SqlNode node,
                               SqlOperator operator)
Returns whether a node is a call to a given operator.


createNlsStringType

public static RelDataType createNlsStringType(RelDataTypeFactory typeFactory,
                                              NlsString str)
Creates the type of an NlsString.

The type inherits the The NlsString's Charset and SqlCollation, if they are set, otherwise it gets the system defaults.

Parameters:
typeFactory - Type factory
str - String
Returns:
Type, including collation and charset

translateCharacterSetName

public static String translateCharacterSetName(String name)
Translates a character set name from a SQL-level name into a Java-level name.

Parameters:
name - SQL-level name
Returns:
Java-level name, or null if SQL-level name is unknown