net.sf.farrago.namespace.jdbc
Class MedJdbcDataServer

java.lang.Object
  extended by net.sf.farrago.plugin.FarragoAbstractPluginBase
      extended by net.sf.farrago.namespace.impl.MedAbstractBase
          extended by net.sf.farrago.namespace.impl.MedAbstractDataServer
              extended by net.sf.farrago.namespace.jdbc.MedJdbcDataServer
All Implemented Interfaces:
FarragoMedDataServer, FarragoAllocation, ClosableAllocation

public class MedJdbcDataServer
extends MedAbstractDataServer

MedJdbcDataServer implements the FarragoMedDataServer interface for JDBC data.

MedJdbcDataServer provides three modes of operation:

  1. When a JNDI resource name is provided, it obtains a DataSource object from JNDI and obtains database connections from it. The DataSource is assumed to represent a connection pool. Validation queries and login timeouts are the responsibility of the data source. When getConnection() (or getDatabaseMetaData()) are invoked, a single Connection is borrowed from the pool and held until releaseResources() or closeAllocation() is invoked. A separate Connection is borrowed from the pool for each call to getRuntimeSupport(Object) and is returned when the associated FarragoStatementAllocation object is closed.
  2. When JDBC connection information is given (e.g., driver, URL, username, password), MedJdbcDataServer uses Apache Commons-DBCP to create a DataSource backed by a connection pool. If a validation query is specified,the DataSource is configured to execute it. When getConnection() (or getDatabaseMetaData()) are invoked, a single Connection is borrowed from the pool and held until releaseResources() or closeAllocation() is invoked. A separate Connection is borrowed from the pool for each call to getRuntimeSupport(Object) and is returned when the associated FarragoStatementAllocation object is closed.
  3. When JDBC connection information is given and connection pooling is disabled, MedJdbcDataServer behaves as it did before the introduction of connection pooling. A single Connection is obtained using the DriverManager. Validation queries are executed when the Connection is next used after a call to releaseResources(). The same Connection is also used for getRuntimeSupport(Object). The Connection is held until closeAllocation() is invoked.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/namespace/jdbc/MedJdbcDataServer.java#45 $
Author:
John V. Sichi

Nested Class Summary
private static class MedJdbcDataServer.CustomPoolableConnection
           
private  class MedJdbcDataServer.CustomPoolableConnectionFactory
          CustomPoolableConnectionFactory is similar to DBCP's PoolableConnectionFactory, but allows us to better control when Connection.setAutoCommit(boolean) and Connection.setReadOnly(boolean) are called.
static class MedJdbcDataServer.Source
           
static class MedJdbcDataServer.WildcardMapping
           
 
Field Summary
private  boolean autocommit
           
protected  String catalogName
           
private  Connection connection
           
private  org.apache.commons.pool.impl.GenericObjectPool connectionPool
           
protected  Properties connectProps
           
private  DatabaseMetaData databaseMetaData
           
protected  DataSource dataSource
           
static boolean DEFAULT_AUTOCOMMIT
           
static boolean DEFAULT_DISABLE_CONNECTION_POOL
           
static String DEFAULT_DISABLED_PUSHDOWN_REL_PATTERN
           
static long DEFAULT_EVICTION_TIMER_PERIOD
           
static int DEFAULT_FETCH_SIZE
           
static boolean DEFAULT_LENIENT
           
static int DEFAULT_MAX_IDLE_CONNECTIONS
           
static long DEFAULT_MIN_EVICTION_IDLE_MILLIS
           
static boolean DEFAULT_SKIP_TYPE_CHECK
           
static boolean DEFAULT_USE_SCHEMA_NAME_AS_FOREIGN_QUALIFIER
           
static String DEFAULT_VALIDATION_TIMING
           
private  boolean disableConnectionPool
           
protected  Pattern disabledPushdownPattern
           
private  long evictionTimerPeriodMillis
           
private  int fetchSize
           
protected  String jndiName
           
protected  boolean lenient
           
private static Logger logger
           
protected  String loginTimeout
           
private  int maxIdleConnections
           
private  long minEvictionIdleMillis
           
protected  String password
           
static String PROP_AUTOCOMMIT
           
static String PROP_CATALOG_NAME
           
static String PROP_DISABLE_CONNECTION_POOL
           
static String PROP_DISABLED_PUSHDOWN_REL_PATTERN
           
static String PROP_DRIVER_CLASS
           
static String PROP_EVICTION_TIMER_PERIOD_MILLIS
           
static String PROP_EXT_OPTIONS
           
static String PROP_FETCH_SIZE
           
static String PROP_JNDI_NAME
           
static String PROP_LENIENT
           
static String PROP_LOGIN_TIMEOUT
           
static String PROP_MAX_IDLE_CONNECTIONS
           
static String PROP_MIN_EVICTION_IDLE_MILLIS
           
static String PROP_NAME
           
static String PROP_OBJECT
           
static String PROP_PASSWORD
           
static String PROP_SCHEMA_MAPPING
           
static String PROP_SCHEMA_NAME
           
static String PROP_SKIP_TYPE_CHECK
           
static String PROP_TABLE_MAPPING
           
static String PROP_TABLE_NAME
           
static String PROP_TABLE_PREFIX_MAPPING
           
static String PROP_TABLE_TYPES
           
static String PROP_TYPE
           
static String PROP_TYPE_MAPPING
           
static String PROP_TYPE_SUBSTITUTION
           
static String PROP_URL
           
static String PROP_USE_SCHEMA_NAME_AS_FOREIGN_QUALIFIER
           
static String PROP_USER_NAME
           
static String PROP_VALIDATION_QUERY
           
static String PROP_VALIDATION_TIMING
           
static String PROP_VALIDATION_TIMING_ON_BORROW
           
static String PROP_VALIDATION_TIMING_ON_RETURN
           
static String PROP_VALIDATION_TIMING_WHILE_IDLE
           
static String PROP_VERSION
           
protected  HashMap<String,Map<String,String>> schemaMaps
           
protected  String schemaName
           
protected  boolean skipTypeCheck
           
protected  boolean supportsMetaData
           
protected  HashMap<String,Map<String,MedJdbcDataServer.Source>> tableMaps
           
protected  Map<String,List<MedJdbcDataServer.WildcardMapping>> tablePrefixMaps
           
protected  String[] tableTypes
           
protected  String url
           
protected  String userName
           
protected  boolean useSchemaNameAsForeignQualifier
           
private  boolean validateConnection
          If disableConnectionPool is true, used to determine when to re-validate the connection.
private  boolean validateOnBorrow
           
private  boolean validateOnReturn
           
private  boolean validateWhileIdle
           
protected  String validationQuery
           
 
Fields inherited from class net.sf.farrago.plugin.FarragoAbstractPluginBase
BOOLEAN_CHOICES_DEFAULT_FALSE, BOOLEAN_CHOICES_DEFAULT_TRUE, EMPTY_DRIVER_PROPERTIES
 
Fields inherited from interface net.sf.farrago.namespace.FarragoMedDataServer
PROP_SERVER_NAME, PROP_SERVER_TYPE, PROP_SERVER_VERSION
 
Constructor Summary
protected MedJdbcDataServer(String serverMofId, Properties props)
           
 
Method Summary
 void closeAllocation()
          Closes this object.
private  void closeConnection()
           
private  void createSchemaMaps(DatabaseMetaData databaseMetaData, String key, String value)
           
private  void createTableMaps(String srcSchema, String srcTable, String targetSchema, String targetTable)
           
private  void createTablePrefixMaps(String srcSchema, String srcTablePrefix, String targetSchema, String targetTablePrefix)
           
 String getCatalogName()
           
protected  Connection getConnection()
          Retrieves a Connection to this data server's configured database.
protected  DatabaseMetaData getDatabaseMetaData()
          Retrieves database metadata for this data server's configured database.
 FarragoMedNameDirectory getNameDirectory()
          Gets a FarragoMedNameDirectory corresponding to this server.
protected  String getPassword()
          Retrieves the configured password for this data server.
 Object getRuntimeSupport(Object param)
          Gets an object needed for runtime support.
protected  MedJdbcNameDirectory getSchemaNameDirectory()
           
protected  String getUserName()
          Retrieves the configured user name for this data server.
 void initialize()
           
private  void initializeDataSource()
           
private  void initMetaData()
           
private  boolean isQuoteChar(String mapping, int index)
           
private  void markLoopbackConnection(Connection conn)
           
 FarragoMedColumnSet newColumnSet(String[] localName, Properties tableProps, FarragoTypeFactory typeFactory, RelDataType rowType, Map<String,Properties> columnPropMap)
          Creates an instance of a FarragoMedColumnSet corresponding to row data identified by properties rather than container name.
private  Connection newConnection()
          Retrieves a Connection object from the DataSource and set auto-commit mode if necessary.
private  void parseMapping(DatabaseMetaData databaseMetaData, String mapping, boolean isTableMapping, boolean isTablePrefixMapping)
           
 void registerRelMetadataProviders(ChainedRelMetadataProvider chain)
          Gives this wrapper a chance to register one or more RelMetadataProviders in the chain which will be used to answer relational expression metadata queries during optimization.
 void registerRules(RelOptPlanner planner)
          Gives this wrapper a chance to register any special optimization rules.
 void releaseResources()
          Gives this wrapper a chance to release any resources.
protected static void removeNonDriverProps(Properties props)
           
 
Methods inherited from class net.sf.farrago.namespace.impl.MedAbstractDataServer
getLoopbackDataSource, getProperties, getServerMofId, setLoopbackDataSource
 
Methods inherited from class net.sf.farrago.plugin.FarragoAbstractPluginBase
getBooleanProperty, getByteProperty, getDoubleProperty, getFloatProperty, getIntProperty, getLongProperty, getShortProperty, requireProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_URL

public static final String PROP_URL
See Also:
Constant Field Values

PROP_DRIVER_CLASS

public static final String PROP_DRIVER_CLASS
See Also:
Constant Field Values

PROP_USER_NAME

public static final String PROP_USER_NAME
See Also:
Constant Field Values

PROP_PASSWORD

public static final String PROP_PASSWORD
See Also:
Constant Field Values

PROP_JNDI_NAME

public static final String PROP_JNDI_NAME
See Also:
Constant Field Values

PROP_CATALOG_NAME

public static final String PROP_CATALOG_NAME
See Also:
Constant Field Values

PROP_SCHEMA_NAME

public static final String PROP_SCHEMA_NAME
See Also:
Constant Field Values

PROP_TABLE_NAME

public static final String PROP_TABLE_NAME
See Also:
Constant Field Values

PROP_OBJECT

public static final String PROP_OBJECT
See Also:
Constant Field Values

PROP_TABLE_TYPES

public static final String PROP_TABLE_TYPES
See Also:
Constant Field Values

PROP_EXT_OPTIONS

public static final String PROP_EXT_OPTIONS
See Also:
Constant Field Values

PROP_TYPE_SUBSTITUTION

public static final String PROP_TYPE_SUBSTITUTION
See Also:
Constant Field Values

PROP_TYPE_MAPPING

public static final String PROP_TYPE_MAPPING
See Also:
Constant Field Values

PROP_LOGIN_TIMEOUT

public static final String PROP_LOGIN_TIMEOUT
See Also:
Constant Field Values

PROP_VALIDATION_QUERY

public static final String PROP_VALIDATION_QUERY
See Also:
Constant Field Values

PROP_FETCH_SIZE

public static final String PROP_FETCH_SIZE
See Also:
Constant Field Values

PROP_AUTOCOMMIT

public static final String PROP_AUTOCOMMIT
See Also:
Constant Field Values

PROP_USE_SCHEMA_NAME_AS_FOREIGN_QUALIFIER

public static final String PROP_USE_SCHEMA_NAME_AS_FOREIGN_QUALIFIER
See Also:
Constant Field Values

PROP_LENIENT

public static final String PROP_LENIENT
See Also:
Constant Field Values

PROP_SKIP_TYPE_CHECK

public static final String PROP_SKIP_TYPE_CHECK
See Also:
Constant Field Values

PROP_DISABLED_PUSHDOWN_REL_PATTERN

public static final String PROP_DISABLED_PUSHDOWN_REL_PATTERN
See Also:
Constant Field Values

PROP_SCHEMA_MAPPING

public static final String PROP_SCHEMA_MAPPING
See Also:
Constant Field Values

PROP_TABLE_MAPPING

public static final String PROP_TABLE_MAPPING
See Also:
Constant Field Values

PROP_TABLE_PREFIX_MAPPING

public static final String PROP_TABLE_PREFIX_MAPPING
See Also:
Constant Field Values

PROP_MAX_IDLE_CONNECTIONS

public static final String PROP_MAX_IDLE_CONNECTIONS
See Also:
Constant Field Values

PROP_EVICTION_TIMER_PERIOD_MILLIS

public static final String PROP_EVICTION_TIMER_PERIOD_MILLIS
See Also:
Constant Field Values

PROP_MIN_EVICTION_IDLE_MILLIS

public static final String PROP_MIN_EVICTION_IDLE_MILLIS
See Also:
Constant Field Values

PROP_VALIDATION_TIMING

public static final String PROP_VALIDATION_TIMING
See Also:
Constant Field Values

PROP_VALIDATION_TIMING_ON_BORROW

public static final String PROP_VALIDATION_TIMING_ON_BORROW
See Also:
Constant Field Values

PROP_VALIDATION_TIMING_ON_RETURN

public static final String PROP_VALIDATION_TIMING_ON_RETURN
See Also:
Constant Field Values

PROP_VALIDATION_TIMING_WHILE_IDLE

public static final String PROP_VALIDATION_TIMING_WHILE_IDLE
See Also:
Constant Field Values

PROP_DISABLE_CONNECTION_POOL

public static final String PROP_DISABLE_CONNECTION_POOL
See Also:
Constant Field Values

PROP_VERSION

public static final String PROP_VERSION
See Also:
Constant Field Values

PROP_NAME

public static final String PROP_NAME
See Also:
Constant Field Values

PROP_TYPE

public static final String PROP_TYPE
See Also:
Constant Field Values

DEFAULT_USE_SCHEMA_NAME_AS_FOREIGN_QUALIFIER

public static final boolean DEFAULT_USE_SCHEMA_NAME_AS_FOREIGN_QUALIFIER
See Also:
Constant Field Values

DEFAULT_LENIENT

public static final boolean DEFAULT_LENIENT
See Also:
Constant Field Values

DEFAULT_SKIP_TYPE_CHECK

public static final boolean DEFAULT_SKIP_TYPE_CHECK
See Also:
Constant Field Values

DEFAULT_DISABLED_PUSHDOWN_REL_PATTERN

public static final String DEFAULT_DISABLED_PUSHDOWN_REL_PATTERN
See Also:
Constant Field Values

DEFAULT_FETCH_SIZE

public static final int DEFAULT_FETCH_SIZE
See Also:
Constant Field Values

DEFAULT_AUTOCOMMIT

public static final boolean DEFAULT_AUTOCOMMIT
See Also:
Constant Field Values

DEFAULT_MAX_IDLE_CONNECTIONS

public static final int DEFAULT_MAX_IDLE_CONNECTIONS
See Also:
Constant Field Values

DEFAULT_EVICTION_TIMER_PERIOD

public static final long DEFAULT_EVICTION_TIMER_PERIOD
See Also:
Constant Field Values

DEFAULT_MIN_EVICTION_IDLE_MILLIS

public static final long DEFAULT_MIN_EVICTION_IDLE_MILLIS
See Also:
Constant Field Values

DEFAULT_VALIDATION_TIMING

public static final String DEFAULT_VALIDATION_TIMING
See Also:
Constant Field Values

DEFAULT_DISABLE_CONNECTION_POOL

public static final boolean DEFAULT_DISABLE_CONNECTION_POOL
See Also:
Constant Field Values

logger

private static final Logger logger

dataSource

protected DataSource dataSource

connectProps

protected Properties connectProps

userName

protected String userName

password

protected String password

url

protected String url

connectionPool

private org.apache.commons.pool.impl.GenericObjectPool connectionPool

maxIdleConnections

private int maxIdleConnections

evictionTimerPeriodMillis

private long evictionTimerPeriodMillis

minEvictionIdleMillis

private long minEvictionIdleMillis

jndiName

protected String jndiName

connection

private Connection connection

supportsMetaData

protected boolean supportsMetaData

databaseMetaData

private DatabaseMetaData databaseMetaData

disableConnectionPool

private boolean disableConnectionPool

validateConnection

private boolean validateConnection
If disableConnectionPool is true, used to determine when to re-validate the connection.


catalogName

protected String catalogName

schemaName

protected String schemaName

tableTypes

protected String[] tableTypes

loginTimeout

protected String loginTimeout

validationQuery

protected String validationQuery

validateOnBorrow

private boolean validateOnBorrow

validateOnReturn

private boolean validateOnReturn

validateWhileIdle

private boolean validateWhileIdle

useSchemaNameAsForeignQualifier

protected boolean useSchemaNameAsForeignQualifier

lenient

protected boolean lenient

skipTypeCheck

protected boolean skipTypeCheck

disabledPushdownPattern

protected Pattern disabledPushdownPattern

fetchSize

private int fetchSize

autocommit

private boolean autocommit

schemaMaps

protected HashMap<String,Map<String,String>> schemaMaps

tableMaps

protected HashMap<String,Map<String,MedJdbcDataServer.Source>> tableMaps

tablePrefixMaps

protected Map<String,List<MedJdbcDataServer.WildcardMapping>> tablePrefixMaps
Constructor Detail

MedJdbcDataServer

protected MedJdbcDataServer(String serverMofId,
                            Properties props)
Method Detail

initialize

public void initialize()
                throws SQLException
Throws:
SQLException

getCatalogName

public String getCatalogName()
Returns:
name of the catalog accessed by this server

initMetaData

private void initMetaData()

initializeDataSource

private void initializeDataSource()
                           throws SQLException
Throws:
SQLException

getUserName

protected String getUserName()
Retrieves the configured user name for this data server. Subclasses may override this method to obtain the user name from an alternate source.

Returns:
user name for this data server

getPassword

protected String getPassword()
Retrieves the configured password for this data server. Subclasses may override this method to obtain the password from an alternate source.

Returns:
password for this data server

getConnection

protected Connection getConnection()
                            throws SQLException
Retrieves a Connection to this data server's configured database. The Connection returned by the first call to this method will continue to be returned until releaseResources() is invoked.

This Connection is not to be used for runtime query support, although DDL (such as IMPORT FOREIGN SCHEMA) may use it.

NOTE: if connection pooling is disabled, the Connection returned by this method will be re-used for runtime support and will be returned even after a call to releaseResources().

Returns:
Connection to the database
Throws:
SQLException - if there's an error obtaining a connection

newConnection

private Connection newConnection()
                          throws SQLException
Retrieves a Connection object from the DataSource and set auto-commit mode if necessary.

Returns:
a connection from the datasource
Throws:
SQLException

markLoopbackConnection

private void markLoopbackConnection(Connection conn)

getDatabaseMetaData

protected DatabaseMetaData getDatabaseMetaData()
                                        throws SQLException
Retrieves database metadata for this data server's configured database. This method automatically invoked getConnection() to obtain a Connection to the database. The same DatabaseMetaData object will be returned for each call to this method until releaseResources() is invoked.

This DatabaseMetaData object is not to be used for runtime query support, although DDL (such as IMPORT FOREIGN SCHEMA) may use it.

NOTE: if connection pooling is disabled, the DatabaseMetaData object returned by this method will be re-used even after a call to releaseResources().

Returns:
database metadata
Throws:
SQLException - if there's an error obtaining a connection or metadata

releaseResources

public void releaseResources()
Description copied from interface: FarragoMedDataServer
Gives this wrapper a chance to release any resources.

Specified by:
releaseResources in interface FarragoMedDataServer
Overrides:
releaseResources in class MedAbstractDataServer

closeConnection

private void closeConnection()

removeNonDriverProps

protected static void removeNonDriverProps(Properties props)

getNameDirectory

public FarragoMedNameDirectory getNameDirectory()
                                         throws SQLException
Description copied from interface: FarragoMedDataServer
Gets a FarragoMedNameDirectory corresponding to this server.

Specified by:
getNameDirectory in interface FarragoMedDataServer
Overrides:
getNameDirectory in class MedAbstractDataServer
Returns:
directory, or null if this server does not have the required metadata capability
Throws:
SQLException - if directory access is unsuccessful (but not if directory access is unsupported)

getSchemaNameDirectory

protected MedJdbcNameDirectory getSchemaNameDirectory()

newColumnSet

public FarragoMedColumnSet newColumnSet(String[] localName,
                                        Properties tableProps,
                                        FarragoTypeFactory typeFactory,
                                        RelDataType rowType,
                                        Map<String,Properties> columnPropMap)
                                 throws SQLException
Description copied from interface: FarragoMedDataServer
Creates an instance of a FarragoMedColumnSet corresponding to row data identified by properties rather than container name. This supports the SQL/MED CREATE FOREIGN TABLE statement. As much validation as possible should be performed, including accessing representative data.

Parameters:
localName - the qualified name to assign to the column set within Farrago; this should NOT be used for finding the actual data, since it can be set arbitrarily by the caller; instead, it should be used to implement the RelOptTable.getQualifiedName() method, and can be useful for correlation during debugging
tableProps - properties to use for data location and access
typeFactory - FarragoTypeFactory to use for defining types
rowType - type to impose on the rows of this column set (including column names and types), or null to infer row type; if this is non-null, it must be saved for use by the getRowType() returned from FarragoMedColumnSet
columnPropMap - map from column name to column-specific Properties; this is optional and may only be specified when rowType is also specified (the field names in rowType are used as the keys for columnPropMap)
Returns:
new FarragoMedColumnSet
Throws:
SQLException - if data access is unsuccessful

getRuntimeSupport

public Object getRuntimeSupport(Object param)
                         throws SQLException
Description copied from interface: FarragoMedDataServer
Gets an object needed for runtime support. Typically, this will be called from code generated by this server. The meaning of this is entirely dependent on the server implementation. If the returned object implements FarragoAllocation, its closeAllocation() method will be called automatically as soon as it is no longer needed.

Specified by:
getRuntimeSupport in interface FarragoMedDataServer
Overrides:
getRuntimeSupport in class MedAbstractDataServer
Parameters:
param - parameter supplied at runtime
Returns:
support object
Throws:
SQLException

registerRelMetadataProviders

public void registerRelMetadataProviders(ChainedRelMetadataProvider chain)
Description copied from interface: FarragoMedDataServer
Gives this wrapper a chance to register one or more RelMetadataProviders in the chain which will be used to answer relational expression metadata queries during optimization. Wrappers which define their own relational expressions will generally need to supply corresponding metadata providers.

Specified by:
registerRelMetadataProviders in interface FarragoMedDataServer
Overrides:
registerRelMetadataProviders in class MedAbstractDataServer
Parameters:
chain - receives wrappers's custom providers, if any

registerRules

public void registerRules(RelOptPlanner planner)
Description copied from interface: FarragoMedDataServer
Gives this wrapper a chance to register any special optimization rules. This method may be called more than once, each time with a different planner instance.

Specified by:
registerRules in interface FarragoMedDataServer
Overrides:
registerRules in class MedAbstractDataServer
Parameters:
planner - the planner in which the rules should be registered

closeAllocation

public void closeAllocation()
Description copied from interface: ClosableAllocation
Closes this object.

Specified by:
closeAllocation in interface ClosableAllocation
Overrides:
closeAllocation in class MedAbstractDataServer

parseMapping

private void parseMapping(DatabaseMetaData databaseMetaData,
                          String mapping,
                          boolean isTableMapping,
                          boolean isTablePrefixMapping)
                   throws SQLException
Throws:
SQLException

createSchemaMaps

private void createSchemaMaps(DatabaseMetaData databaseMetaData,
                              String key,
                              String value)
                       throws SQLException
Throws:
SQLException

createTableMaps

private void createTableMaps(String srcSchema,
                             String srcTable,
                             String targetSchema,
                             String targetTable)
                      throws SQLException
Throws:
SQLException

createTablePrefixMaps

private void createTablePrefixMaps(String srcSchema,
                                   String srcTablePrefix,
                                   String targetSchema,
                                   String targetTablePrefix)
                            throws SQLException
Throws:
SQLException

isQuoteChar

private boolean isQuoteChar(String mapping,
                            int index)