net.sf.eodsql
Class QueryTool

java.lang.Object
  extended by net.sf.eodsql.QueryTool

public class QueryTool
extends Object

The QueryTool is used to construct an implementation of a BaseQuery interface. There are two types of BaseQuery that can be constructed with this class:

  1. A simple BaseQuery that locks a Connection to the database (obtained via getQuery(Connection, Class)). This type of query will only allow one connected DataSet at a time, the DataSet.disconnect() method, or the Select.disconnected() flag may be used to get around this limitation. Once a DataSet becomms disconnected, the queries Select methods can be used again. Attempting to call a method when the Connection is in use will result in an IllegalStateException.
  2. A pool-friendly BaseQuery. These can be obtained through the getQuery(DataSource, Class) methods. The reason these queries are pool-friendly is that they do not request a Connection from the DataSource until a method is called, and they release the connections one they are done with it. This also makes them suitable for long-term storage as class-fields instead of local variables.
The QueryTool also has a notion of a "default" DataSource, which can be set via the setDefaultDataSource(javax.sql.DataSource) method.

The classes generated by this class are loaded via the Proxy class. Effort is made to minimize the amount of time it takes to create the query objects by caching structure information, and lazy loading the actual method implementations. The ClassLoader used for the implementation classes is the ContextClassLoader.

The query classes are validated before any initialization of Proxy's is done, if the query class violates any of the rules for creating a BaseQuery the creation method will throw an InvalidQueryException

Author:
jason
See Also:
Proxy, Thread.getContextClassLoader()

Method Summary
static
<T extends BaseQuery>
T
createQueryImplementation(Class<T> query)
          Deprecated. use getQuery(Class) in preference to this method
static
<T extends BaseQuery>
T
createQueryImplementation(Connection connection, Class<T> query)
          Deprecated. use getQuery(Connection, Class) in preference to this method
static
<T extends BaseQuery>
T
createQueryImplementation(DataSource dataSource, Class<T> query)
          Deprecated. use getQuery(DataSource, Class) in preference to this method
static DataSource getDefaultDataSource()
          Returns the "default" DataSource if one has been set.
static
<T extends BaseQuery>
T
getQuery(Class<T> query)
           Returns an instance of a generated implementation for a specific BaseQuery class.
static
<T extends BaseQuery>
T
getQuery(Connection connection, Class<T> query)
          Creates a new instance of a BaseQuery class with a specific Connection.
static
<T extends BaseQuery>
T
getQuery(DataSource dataSource, Class<T> query)
          Creates a new instance of a BaseQuery class with a reference to the specified DataSource.
static Map<Class,TypeMapper> getTypeMap()
          Returns the Map that specifies how Java types should be mapped to SQL types and back again.
static void setDefaultDataSource(DataSource ds)
          Sets the "default" DataSource for the VM.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setDefaultDataSource

public static void setDefaultDataSource(DataSource ds)
Sets the "default" DataSource for the VM. This method must be invoked before createQueryImplementation(Class) can be called, the other createQueryImplementation methods are unaffected by the default DataSource. This method may not be called with null, and will throw an IllegalArgumentException if the specified DataSource is null.

Parameters:
ds - the default DataSource for the VM

getDefaultDataSource

public static DataSource getDefaultDataSource()
Returns the "default" DataSource if one has been set. If setDefaultDataSource has not been called with a valid DataSource this method will return null.

Returns:
the default DataSource for this VM
See Also:
setDefaultDataSource(javax.sql.DataSource)

createQueryImplementation

@Deprecated
public static <T extends BaseQuery> T createQueryImplementation(Class<T> query)
Deprecated. use getQuery(Class) in preference to this method

Returns an instance of a generated implementation for a specific BaseQuery class. After validating that the specified class is a valid query class, this method will return a new implementation of the requested BaseQuery with a reference to the specified DataSource. Each query will keep track of any Connections it opens, and force them closed when it's BaseQuery.close() is called.

If the default DataSource has not yet been set, this method will throw an IllegalStateException. If the query class violates any of the rules for a query, this method will result in an IllegalArgumentException.

Parameters:
query - the BaseQuery to create an implementation of
Returns:
an implementation of the specified BaseQuery

createQueryImplementation

@Deprecated
public static <T extends BaseQuery> T createQueryImplementation(DataSource dataSource,
                                                                           Class<T> query)
Deprecated. use getQuery(DataSource, Class) in preference to this method

Creates a new instance of a BaseQuery class with a reference to the specified DataSource. Each query will keep track of any Connections it opens, and force them closed when it's BaseQuery.close() is called. If the DataSource is null this method will throw an IllegalArgumentException.

Parameters:
dataSource - the dataSource to obtain Connections from
query - the BaseQuery to create an implementation of
Returns:
an implementation of the specified BaseQuery
See Also:
createQueryImplementation(Class)

createQueryImplementation

@Deprecated
public static <T extends BaseQuery> T createQueryImplementation(Connection connection,
                                                                           Class<T> query)
Deprecated. use getQuery(Connection, Class) in preference to this method

Creates a new instance of a BaseQuery class with a specific Connection. If the Connection is null this method will throw an IllegalArgumentException. The specified Connection will be closed when the BaseQuery.close() method is called.

Parameters:
connection - the connection to use for the query class
query - the BaseQuery to create an implementation of
See Also:
createQueryImplementation(Class)

getQuery

public static <T extends BaseQuery> T getQuery(Class<T> query)

Returns an instance of a generated implementation for a specific BaseQuery class. After validating that the specified class is a valid query class, this method will return a new implementation of the requested BaseQuery with a reference to the specified DataSource. Each query will keep track of any Connections it opens, and force them closed when it's BaseQuery.close() is called.

If the default DataSource has not yet been set, this method will throw an IllegalStateException. If the query class violates any of the rules for a query, this method will result in an IllegalArgumentException.

Parameters:
query - the BaseQuery to create an implementation of
Returns:
an implementation of the specified BaseQuery

getQuery

public static <T extends BaseQuery> T getQuery(DataSource dataSource,
                                               Class<T> query)
Creates a new instance of a BaseQuery class with a reference to the specified DataSource. Each query will keep track of any Connections it opens, and force them closed when it's BaseQuery.close() is called. If the DataSource is null this method will throw an IllegalArgumentException.

Parameters:
dataSource - the dataSource to obtain Connections from
query - the BaseQuery to create an implementation of
Returns:
an implementation of the specified BaseQuery
See Also:
getQuery(Class)

getQuery

public static <T extends BaseQuery> T getQuery(Connection connection,
                                               Class<T> query)
Creates a new instance of a BaseQuery class with a specific Connection. If the Connection is null this method will throw an IllegalArgumentException. The specified Connection will be closed when the BaseQuery.close() method is called.

Parameters:
connection - the connection to use for the query class
query - the BaseQuery to create an implementation of
Returns:
an implementation of the specified BaseQuery
See Also:
getQuery(Class)

getTypeMap

public static Map<Class,TypeMapper> getTypeMap()
Returns the Map that specifies how Java types should be mapped to SQL types and back again. This Map may be modified directly to add new types to the system.

Returns:
the current data-type Map