org.teiid.connector.jdbc.translator
Class Translator

java.lang.Object
  extended by org.teiid.connector.jdbc.translator.Translator
Direct Known Subclasses:
DB2SQLTranslator, InformixSQLTranslator, MySQLTranslator, OracleSQLTranslator, PostgreSQLTranslator, SybaseSQLTranslator

public class Translator
extends java.lang.Object

Base class for creating source SQL queries and retrieving results. Specific databases should override as necessary.


Nested Class Summary
static class Translator.NullOrder
           
 
Field Summary
static java.util.TimeZone DEFAULT_TIME_ZONE
           
 
Constructor Summary
Translator()
           
 
Method Summary
 boolean addSourceComment()
          Returns true to indicate that SQL should include a comment indicating the session and request ids.
 void afterConnectionCreation(java.sql.Connection connection)
          Provides a hook to call source specific logic when a connection is created.
protected  void afterInitialConnectionCreation(java.sql.Connection connection)
          Called exactly once for this source.
 void bindValue(java.sql.PreparedStatement stmt, java.lang.Object param, java.lang.Class<?> paramType, int i)
          Sets prepared statement parameter i with param.
 java.sql.ResultSet executeStoredProcedure(java.sql.CallableStatement statement, TranslatedCommand command)
          This is a generic implementation.
 java.lang.String formatDateValue(java.util.Date dateObject)
          Format the dateObject (of type date, time, or timestamp) into a string using the DatabaseTimeZone format.
 java.lang.String getConnectionTestQuery()
           
 ConnectorCapabilities getConnectorCapabilities()
          Get the capabilties for the source.
 java.util.Calendar getDatabaseCalendar()
          Gets the database calendar.
 java.lang.Class<? extends ConnectorCapabilities> getDefaultCapabilities()
          Get the default capabilities class.
 java.lang.String getDefaultConnectionTestQuery()
           
 Translator.NullOrder getDefaultNullOrder()
          get the default null ordering
 ConnectorEnvironment getEnvironment()
          Gets the ConnectorEnvironment used to initialize this Translator
 java.util.Map<java.lang.String,FunctionModifier> getFunctionModifiers()
          Return a map of function name in lower case to FunctionModifier.
 int getIsValidTimeout()
          Returns a positive number if query testing should use the JDBC 4.0 isValid check.
 ILanguageFactory getLanguageFactory()
          Gets the ILanguageFactory
 java.lang.String getSetOperationString(ISetQuery.Operation operation)
          Returns the name for a given ISetQuery.Operation
 java.lang.String getSourceComment(ExecutionContext context, ICommand command)
          Returns the source comment for
 SQLConversionVisitor getSQLConversionVisitor()
          Create the SQLConversionVisitor that will perform translation.
 int getTimestampNanoPrecision()
          Return the precision of timestamp literals.
 TypeFacility getTypeFacility()
           
 boolean hasTimeType()
          Return false to indicate that time support should be emulated with timestamps.
 void initialize(ConnectorEnvironment env)
          Initialize the SQLTranslator.
 void registerFunctionModifier(java.lang.String name, FunctionModifier modifier)
          Add the FunctionModifier to the set of known modifiers.
protected  void registerSpecificTypeOfOutParameter(java.sql.CallableStatement statement, IParameter param, int index)
          For registering specific output parameter types we need to translate these into the appropriate java.sql.Types output parameters We will need to match these up with the appropriate standard sql types
 java.lang.String replaceElementName(java.lang.String group, java.lang.String element)
          Override to return a name other than the default [group.]element
 java.lang.Object retrieveValue(java.sql.CallableStatement results, int parameterIndex, java.lang.Class expectedType)
          Retrieve the value for the given parameter index
 java.lang.Object retrieveValue(java.sql.ResultSet results, int columnIndex, java.lang.Class<?> expectedType)
          Retrieve the value on the current resultset row for the given column index.
 boolean supportsExplicitNullOrdering()
           
 java.util.List<?> translate(ILanguageObject obj, ExecutionContext context)
          Return a List of translated parts (ILanguageObjects and Objects), or null if to rely on the default translation.
 java.util.List<?> translateCommand(ICommand command, ExecutionContext context)
          Return a List of translated parts (ILanguageObjects and Objects), or null if to rely on the default translation.
 java.util.List<?> translateLimit(ILimit limit, ExecutionContext context)
          Return a List of translated parts (ILanguageObjects and Objects), or null if to rely on the default translation.
 java.lang.String translateLiteralBoolean(java.lang.Boolean booleanValue)
          Subclasses should override this method to provide a different sql translation of the literal boolean value.
 java.lang.String translateLiteralDate(java.sql.Date dateValue)
          Subclasses should override this method to provide a different sql translation of the literal date value.
 java.lang.String translateLiteralTime(java.sql.Time timeValue)
          Subclasses should override this method to provide a different sql translation of the literal time value.
 java.lang.String translateLiteralTimestamp(java.sql.Timestamp timestampValue)
          Subclasses should override this method to provide a different sql translation of the literal timestamp value.
 boolean useAsInGroupAlias()
          Indicates whether group alias should be of the form "...FROM groupA AS X" or "...FROM groupA X".
 boolean useParensForJoins()
          Set to true to indicate that every branch of a join should have parenthesis.
 boolean useParensForSetQueries()
          Set to true to indicate that every branch of a set query should have parenthesis, i.e.
 boolean usePreparedStatements()
          Use PreparedStatements (or CallableStatements) as appropriate for all commands.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_TIME_ZONE

public static final java.util.TimeZone DEFAULT_TIME_ZONE
Constructor Detail

Translator

public Translator()
Method Detail

initialize

public void initialize(ConnectorEnvironment env)
                throws ConnectorException
Initialize the SQLTranslator.

Parameters:
env -
metadata -
Throws:
ConnectorException

getDatabaseCalendar

public java.util.Calendar getDatabaseCalendar()
Gets the database calendar. This will be set to the time zone specified by the property JDBCPropertyNames.DATABASE_TIME_ZONE, or the local time zone if none is specified.

Returns:

getEnvironment

public final ConnectorEnvironment getEnvironment()
Gets the ConnectorEnvironment used to initialize this Translator


getLanguageFactory

public final ILanguageFactory getLanguageFactory()
Gets the ILanguageFactory


translate

public java.util.List<?> translate(ILanguageObject obj,
                                   ExecutionContext context)
Return a List of translated parts (ILanguageObjects and Objects), or null if to rely on the default translation. Override with care.

Parameters:
command -
context -
Returns:

translateCommand

public java.util.List<?> translateCommand(ICommand command,
                                          ExecutionContext context)
Return a List of translated parts (ILanguageObjects and Objects), or null if to rely on the default translation.

Parameters:
command -
context -
Returns:

translateLimit

public java.util.List<?> translateLimit(ILimit limit,
                                        ExecutionContext context)
Return a List of translated parts (ILanguageObjects and Objects), or null if to rely on the default translation.

Parameters:
limit -
context -
Returns:

getFunctionModifiers

public java.util.Map<java.lang.String,FunctionModifier> getFunctionModifiers()
Return a map of function name in lower case to FunctionModifier.

Returns:
Map of function name to FunctionModifier.

registerFunctionModifier

public void registerFunctionModifier(java.lang.String name,
                                     FunctionModifier modifier)
Add the FunctionModifier to the set of known modifiers.

Parameters:
name -
modifier -

translateLiteralBoolean

public java.lang.String translateLiteralBoolean(java.lang.Boolean booleanValue)
Subclasses should override this method to provide a different sql translation of the literal boolean value. By default, a boolean literal is represented as: '0' or '1'.

Parameters:
booleanValue - Boolean value, never null
Returns:
Translated string

translateLiteralDate

public java.lang.String translateLiteralDate(java.sql.Date dateValue)
Subclasses should override this method to provide a different sql translation of the literal date value. By default, a date literal is represented as: {d'2002-12-31'}

Parameters:
dateValue - Date value, never null
Returns:
Translated string

translateLiteralTime

public java.lang.String translateLiteralTime(java.sql.Time timeValue)
Subclasses should override this method to provide a different sql translation of the literal time value. By default, a time literal is represented as: {t'23:59:59'} See hasTimeType() to represent literal times as timestamps.

Parameters:
timeValue - Time value, never null
Returns:
Translated string

translateLiteralTimestamp

public java.lang.String translateLiteralTimestamp(java.sql.Timestamp timestampValue)
Subclasses should override this method to provide a different sql translation of the literal timestamp value. By default, a timestamp literal is represented as: {ts'2002-12-31 23:59:59'}. See getTimestampNanoPrecision() to control the literal precision.

Parameters:
timestampValue - Timestamp value, never null
Returns:
Translated string

formatDateValue

public java.lang.String formatDateValue(java.util.Date dateObject)
Format the dateObject (of type date, time, or timestamp) into a string using the DatabaseTimeZone format.

Parameters:
dateObject -
Returns:
Formatted string

addSourceComment

public boolean addSourceComment()
Returns true to indicate that SQL should include a comment indicating the session and request ids.


useAsInGroupAlias

public boolean useAsInGroupAlias()
Indicates whether group alias should be of the form "...FROM groupA AS X" or "...FROM groupA X". Certain data sources (such as Oracle) may not support the first form.

Returns:
boolean

usePreparedStatements

public boolean usePreparedStatements()
Use PreparedStatements (or CallableStatements) as appropriate for all commands. Bind values will be determined by the BindValueVisitor. ILiteral.setBindValue(boolean) can be used to force a literal to be a bind value.


useParensForSetQueries

public boolean useParensForSetQueries()
Set to true to indicate that every branch of a set query should have parenthesis, i.e. (query) union (query)

Returns:

hasTimeType

public boolean hasTimeType()
Return false to indicate that time support should be emulated with timestamps.

Returns:

getSetOperationString

public java.lang.String getSetOperationString(ISetQuery.Operation operation)
Returns the name for a given ISetQuery.Operation

Parameters:
operation -
Returns:

getSourceComment

public java.lang.String getSourceComment(ExecutionContext context,
                                         ICommand command)
Returns the source comment for

Parameters:
context -
command -
Returns:

replaceElementName

public java.lang.String replaceElementName(java.lang.String group,
                                           java.lang.String element)
Override to return a name other than the default [group.]element

Parameters:
group -
element -
Returns:

getTimestampNanoPrecision

public int getTimestampNanoPrecision()
Return the precision of timestamp literals. Defaults to 9

Returns:

getConnectionTestQuery

public java.lang.String getConnectionTestQuery()

getDefaultConnectionTestQuery

public java.lang.String getDefaultConnectionTestQuery()

getTypeFacility

public TypeFacility getTypeFacility()

executeStoredProcedure

public java.sql.ResultSet executeStoredProcedure(java.sql.CallableStatement statement,
                                                 TranslatedCommand command)
                                          throws java.sql.SQLException
This is a generic implementation. Because different databases handle stored procedures differently, subclasses should override this method if necessary.

Throws:
java.sql.SQLException

registerSpecificTypeOfOutParameter

protected void registerSpecificTypeOfOutParameter(java.sql.CallableStatement statement,
                                                  IParameter param,
                                                  int index)
                                           throws java.sql.SQLException
For registering specific output parameter types we need to translate these into the appropriate java.sql.Types output parameters We will need to match these up with the appropriate standard sql types

Parameters:
cstmt -
parameter -
Throws:
java.sql.SQLException

bindValue

public void bindValue(java.sql.PreparedStatement stmt,
                      java.lang.Object param,
                      java.lang.Class<?> paramType,
                      int i)
               throws java.sql.SQLException
Sets prepared statement parameter i with param. Performs special handling to translate dates using the database time zone and to translate biginteger, float, and char to JDBC safe objects.

Parameters:
stmt -
param -
paramType -
i -
cal -
Throws:
java.sql.SQLException

retrieveValue

public java.lang.Object retrieveValue(java.sql.ResultSet results,
                                      int columnIndex,
                                      java.lang.Class<?> expectedType)
                               throws java.sql.SQLException
Retrieve the value on the current resultset row for the given column index.

Parameters:
results -
columnIndex -
expectedType -
Returns:
Throws:
java.sql.SQLException

retrieveValue

public java.lang.Object retrieveValue(java.sql.CallableStatement results,
                                      int parameterIndex,
                                      java.lang.Class expectedType)
                               throws java.sql.SQLException
Retrieve the value for the given parameter index

Parameters:
results -
parameterIndex -
expectedType -
Returns:
Throws:
java.sql.SQLException

afterInitialConnectionCreation

protected void afterInitialConnectionCreation(java.sql.Connection connection)
Called exactly once for this source.

Parameters:
connection -

afterConnectionCreation

public void afterConnectionCreation(java.sql.Connection connection)
Provides a hook to call source specific logic when a connection is created. defect request 13979 & 13978


getIsValidTimeout

public int getIsValidTimeout()
Returns a positive number if query testing should use the JDBC 4.0 isValid check. Can be set via the JDBCPropertyNames.IS_VALID_TIMEOUT property

Returns:

getSQLConversionVisitor

public SQLConversionVisitor getSQLConversionVisitor()
Create the SQLConversionVisitor that will perform translation. Typical custom JDBC connectors will not need to create custom conversion visitors, rather implementors should override existing Translator methods.

Returns:

getDefaultCapabilities

public java.lang.Class<? extends ConnectorCapabilities> getDefaultCapabilities()
Get the default capabilities class. Will be used by getConnectorCapabilities() to return a capabilities instance.

Returns:

getConnectorCapabilities

public ConnectorCapabilities getConnectorCapabilities()
                                               throws ConnectorException
Get the capabilties for the source.

Returns:
Throws:
ConnectorException

useParensForJoins

public boolean useParensForJoins()
Set to true to indicate that every branch of a join should have parenthesis.

Returns:

getDefaultNullOrder

public Translator.NullOrder getDefaultNullOrder()
get the default null ordering

Returns:

supportsExplicitNullOrdering

public boolean supportsExplicitNullOrdering()


Copyright © 2009. All Rights Reserved.