org.d2rq.db
Class SQLConnection

java.lang.Object
  extended by org.d2rq.db.SQLConnection

public class SQLConnection
extends Object

TODO: Move all engine-specific code from here to Vendor and its implementations TODO: Move keepalive agent into a separate class

Author:
Richard Cyganiak (richard@cyganiak.de), kurtjx (http://github.com/kurtjx)

Field Summary
static int DEFAULT_KEEP_ALIVE_INTERVAL
           
static String DEFAULT_KEEP_ALIVE_QUERY
           
static String KEEP_ALIVE_PROPERTY
           
static String KEEP_ALIVE_QUERY_PROPERTY
           
 
Constructor Summary
SQLConnection(String jdbcURL, String jdbcDriver, String username, String password)
           
SQLConnection(String jdbcURL, String jdbcDriver, String username, String password, Properties connectionProperties)
           
 
Method Summary
 void addOverriddenColumnTypes(Map<ColumnName,DataType.GenericType> types)
           
 boolean areCompatibleFormats(DatabaseOp table1, ColumnName column1, DatabaseOp table2, ColumnName column2)
          Checks if two columns are formatted by the database in a compatible fashion.
 void close()
          Closes the database connection and shuts down the keep alive agent.
 Connection connection()
           
 boolean equals(Object other)
           
 int fetchSize()
           
protected  String getDatabaseProductType()
           
 String getJdbcDriverClass()
           
 String getJdbcURL()
           
 String getParseError(String sql)
           
 String getPassword()
           
 SQLOp getSelectStatement(String sql)
           
 TableOp getTable(TableName table)
           
 Collection<TableName> getTableNames(String searchInSchema)
          Lists available table names.
 String getUsername()
           
static String guessJDBCDriverClass(String jdbcURL)
          Tries to guess the class name of a suitable JDBC driver from a JDBC URL.
 int hashCode()
           
 boolean isReferencedByForeignKey(TableName table)
           
 boolean isTable(TableName table)
           
 int limit()
           
static void registerJDBCDriverIfPresent(String driverClassName)
          Pre-registers a JDBC driver if its class can be found on the classpath.
 void setDefaultFetchSize(int value)
           
 void setFetchSize(int fetchSize)
           
 void setLimit(int resultSizeLimit)
           
 Vendor vendor()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEEP_ALIVE_PROPERTY

public static final String KEEP_ALIVE_PROPERTY
See Also:
Constant Field Values

DEFAULT_KEEP_ALIVE_INTERVAL

public static final int DEFAULT_KEEP_ALIVE_INTERVAL
See Also:
Constant Field Values

KEEP_ALIVE_QUERY_PROPERTY

public static final String KEEP_ALIVE_QUERY_PROPERTY
See Also:
Constant Field Values

DEFAULT_KEEP_ALIVE_QUERY

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

SQLConnection

public SQLConnection(String jdbcURL,
                     String jdbcDriver,
                     String username,
                     String password)

SQLConnection

public SQLConnection(String jdbcURL,
                     String jdbcDriver,
                     String username,
                     String password,
                     Properties connectionProperties)
Method Detail

getJdbcURL

public String getJdbcURL()

getJdbcDriverClass

public String getJdbcDriverClass()

getUsername

public String getUsername()

getPassword

public String getPassword()

connection

public Connection connection()

getParseError

public String getParseError(String sql)

getSelectStatement

public SQLOp getSelectStatement(String sql)

getTableNames

public Collection<TableName> getTableNames(String searchInSchema)
Lists available table names. Caches results.

Parameters:
searchInSchema - Schema to list tables from; null to list tables from all schemas
Returns:
A list of table names

isTable

public boolean isTable(TableName table)

getTable

public TableOp getTable(TableName table)
Parameters:
table - A table name
Returns:
Metadata about the table, or null if it doesn't exist

isReferencedByForeignKey

public boolean isReferencedByForeignKey(TableName table)
Returns:
true if another table has a foreign key referencing this table's primary key

limit

public int limit()

setLimit

public void setLimit(int resultSizeLimit)

setFetchSize

public void setFetchSize(int fetchSize)
Parameters:
fetchSize - Value specified in user config or mapping file

setDefaultFetchSize

public void setDefaultFetchSize(int value)
Parameters:
value - Default value for the current operation mode (e.g., increase for dumps)

fetchSize

public int fetchSize()

addOverriddenColumnTypes

public void addOverriddenColumnTypes(Map<ColumnName,DataType.GenericType> types)

vendor

public Vendor vendor()
Returns:
A helper for generating SQL statements conforming to the syntax of the database engine used in this connection

getDatabaseProductType

protected String getDatabaseProductType()
                                 throws SQLException
Throws:
SQLException

areCompatibleFormats

public boolean areCompatibleFormats(DatabaseOp table1,
                                    ColumnName column1,
                                    DatabaseOp table2,
                                    ColumnName column2)

Checks if two columns are formatted by the database in a compatible fashion.

Assuming v1 is a value from column1, and v2 a value from column2, and v1 = v2 evaluates to true within the database, then we call the values have compatible formatting if SELECTing them results in character-for-character identical strings. As an example, a TINYINT and a BIGINT are compatible because equal values will be formatted in the same way when SELECTed, e.g. 1 = 1. But if one of them is ZEROFILL, then SELECTing will result in a different character string, e.g. 1 = 0000000001. The two columns wouldn't be compatible.

This is used by the engine when removing unnecessary joins. If two columns have compatible formatting, then we can sometimes use one in place of the other when they are known to have equal values. But not if they are incompatible, because e.g. http://example.org/id/1 is different from http://example.org/id/0000000001.

Returns:
true if both arguments have compatible formatting

close

public void close()
Closes the database connection and shuts down the keep alive agent.


equals

public boolean equals(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

registerJDBCDriverIfPresent

public static void registerJDBCDriverIfPresent(String driverClassName)
Pre-registers a JDBC driver if its class can be found on the classpath. If the class is not found, nothing will happen.

Parameters:
driverClassName - Fully qualified class name of a JDBC driver

guessJDBCDriverClass

public static String guessJDBCDriverClass(String jdbcURL)
Tries to guess the class name of a suitable JDBC driver from a JDBC URL. This only works in the unlikely case that the driver has been registered earlier using Class.forName(classname).

Parameters:
jdbcURL - A JDBC URL
Returns:
The corresponding JDBC driver class name, or null if not known