to.etc.webapp.qsql
Class JdbcDataContext

java.lang.Object
  extended by to.etc.webapp.qsql.JdbcDataContext
All Implemented Interfaces:
QDataContext

public class JdbcDataContext
extends java.lang.Object
implements QDataContext

WATCH OUT- THIS DOES NOT OBEY OBJECT IDENTITY RULES!! Records loaded through this code are NOT mapped by identity, so multiple queries for the same object WILL return different copies!!

This is a poor-man's datacontext that can be used to do JDBC queries using the QCriteria interface.

Author:
Frits Jalvingh Created on Mar 10, 2010

Constructor Summary
JdbcDataContext(QDataContextFactory factory, java.sql.Connection dbc)
           
 
Method Summary
 void addCommitAction(IRunnable cx)
          Add an action which should be executed after a succesful commit on this context.
 void attach(java.lang.Object o)
          DOES NOTHING.
 void close()
          This will close and fully discard all resources belonging to this context, provided ignoreClose is not true.
 void commit()
           
 void delete(java.lang.Object o)
          Unsupported for JDBC code.
<T> T
find(java.lang.Class<T> clz, java.lang.Object pk)
          Locate the specified record by PK.
<T> T
find(ICriteriaTableDef<T> metatable, java.lang.Object pk)
           
 java.sql.Connection getConnection()
          Return the underlying connection verbatim.
 QDataContextFactory getFactory()
          Returns the context source which created this DataContext.
<T> T
getInstance(java.lang.Class<T> clz, java.lang.Object pk)
          Get an instance; this will return an instance by first trying to load it; if that fails it will create one but only fill the PK.
<T> T
getInstance(ICriteriaTableDef<T> clz, java.lang.Object pk)
           
protected  java.sql.Connection internalGetConnection()
           
protected  void internalSetConnection(java.sql.Connection dbc)
           
 boolean inTransaction()
           
<T> java.util.List<T>
query(QCriteria<T> q)
          Execute the query specified by q and return a list of results.
 java.util.List<java.lang.Object[]> query(QSelection<?> sel)
          Issue a selection query, where multiple fields or projections on fields are selected from a base class.
<T> T
queryOne(QCriteria<T> q)
          Execute the query specified by q, and expect and return at most 1 result.
 java.lang.Object[] queryOne(QSelection<?> q)
          Execute the selection query specified by q, and expect and return at most 1 result.
 void refresh(java.lang.Object o)
          Not suppore
 void rollback()
           
 void save(java.lang.Object o)
          Not supported
 void setIgnoreClose(boolean on)
          When ignoreClose is set to T the close call must be silently ignored.
 void startTransaction()
           
protected  void unclosed()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcDataContext

public JdbcDataContext(QDataContextFactory factory,
                       java.sql.Connection dbc)
Method Detail

internalSetConnection

protected void internalSetConnection(java.sql.Connection dbc)

internalGetConnection

protected java.sql.Connection internalGetConnection()
                                             throws java.lang.Exception
Throws:
java.lang.Exception

unclosed

protected void unclosed()
                 throws java.lang.Exception
Throws:
java.lang.Exception

attach

public void attach(java.lang.Object o)
            throws java.lang.Exception
DOES NOTHING.

Specified by:
attach in interface QDataContext
Throws:
java.lang.Exception
See Also:
QDataContext.attach(java.lang.Object)

close

public void close()
Description copied from interface: QDataContext
This will close and fully discard all resources belonging to this context, provided ignoreClose is not true. A closed context cannot be reused anymore and should be discarded.

Specified by:
close in interface QDataContext

commit

public void commit()
            throws java.lang.Exception
Specified by:
commit in interface QDataContext
Throws:
java.lang.Exception

delete

public void delete(java.lang.Object o)
            throws java.lang.Exception
Unsupported for JDBC code.

Specified by:
delete in interface QDataContext
Throws:
java.lang.Exception
See Also:
QDataContext.delete(java.lang.Object)

find

public <T> T find(java.lang.Class<T> clz,
                  java.lang.Object pk)
       throws java.lang.Exception
Locate the specified record by PK.

Specified by:
find in interface QDataContext
Type Parameters:
T - The object type.
Parameters:
clz - The persistent class for which an instance is being sought.
pk - The PK for the instance required.
Returns:
Null if the instance does not exist, the actual and fully initialized instance (or proxy) otherwise.
Throws:
java.lang.Exception
See Also:
QDataContext.find(java.lang.Class, java.lang.Object)

getInstance

public <T> T getInstance(java.lang.Class<T> clz,
                         java.lang.Object pk)
              throws java.lang.Exception
Get an instance; this will return an instance by first trying to load it; if that fails it will create one but only fill the PK. Use is questionable though.

Specified by:
getInstance in interface QDataContext
Type Parameters:
T - The object type
Parameters:
clz - The persistent class for which an instance is being sought.
pk - The PK for the instance required.
Returns:
Always returns an instance; it can be invalid when it does not really exist on the backing store.
Throws:
java.lang.Exception
See Also:
QDataContext.getInstance(java.lang.Class, java.lang.Object)

getConnection

public java.sql.Connection getConnection()
                                  throws java.lang.Exception
Return the underlying connection verbatim.

Specified by:
getConnection in interface QDataContext
Throws:
java.lang.Exception
See Also:
QDataContext.getConnection()

getFactory

public QDataContextFactory getFactory()
Description copied from interface: QDataContext
Returns the context source which created this DataContext. This context source is used to get query listeners to execute when a query is done.

Specified by:
getFactory in interface QDataContext
Returns:

inTransaction

public boolean inTransaction()
                      throws java.lang.Exception
Specified by:
inTransaction in interface QDataContext
Throws:
java.lang.Exception

query

public <T> java.util.List<T> query(QCriteria<T> q)
                        throws java.lang.Exception
Description copied from interface: QDataContext
Execute the query specified by q and return a list of results. Before and after the query execution all registered listeners will be called.

Specified by:
query in interface QDataContext
Type Parameters:
T - The return type for this query, a persistent class type
Parameters:
q - The selection criteria
Returns:
Throws:
java.lang.Exception

query

public java.util.List<java.lang.Object[]> query(QSelection<?> sel)
                                         throws java.lang.Exception
Description copied from interface: QDataContext
Issue a selection query, where multiple fields or projections on fields are selected from a base class.

Specified by:
query in interface QDataContext
Returns:
Throws:
java.lang.Exception

queryOne

public <T> T queryOne(QCriteria<T> q)
           throws java.lang.Exception
Description copied from interface: QDataContext
Execute the query specified by q, and expect and return at most 1 result. If the query has no result this will return null. If more than one result is obtained this will throw an IllegalStateException.

Specified by:
queryOne in interface QDataContext
Returns:
Throws:
java.lang.Exception

queryOne

public java.lang.Object[] queryOne(QSelection<?> q)
                            throws java.lang.Exception
Description copied from interface: QDataContext
Execute the selection query specified by q, and expect and return at most 1 result. If the query has no result this will return null. If more than one result is obtained this will throw an IllegalStateException.

Specified by:
queryOne in interface QDataContext
Returns:
Throws:
java.lang.Exception

find

public <T> T find(ICriteriaTableDef<T> metatable,
                  java.lang.Object pk)
       throws java.lang.Exception
Specified by:
find in interface QDataContext
Throws:
java.lang.Exception

getInstance

public <T> T getInstance(ICriteriaTableDef<T> clz,
                         java.lang.Object pk)
              throws java.lang.Exception
Specified by:
getInstance in interface QDataContext
Throws:
java.lang.Exception

refresh

public void refresh(java.lang.Object o)
             throws java.lang.Exception
Not suppore

Specified by:
refresh in interface QDataContext
Throws:
java.lang.Exception
See Also:
QDataContext.refresh(java.lang.Object)

rollback

public void rollback()
              throws java.lang.Exception
Specified by:
rollback in interface QDataContext
Throws:
java.lang.Exception

save

public void save(java.lang.Object o)
          throws java.lang.Exception
Not supported

Specified by:
save in interface QDataContext
Throws:
java.lang.Exception
See Also:
QDataContext.save(java.lang.Object)

setIgnoreClose

public void setIgnoreClose(boolean on)
Description copied from interface: QDataContext
When ignoreClose is set to T the close call must be silently ignored. Ugly, but for a lot of reasons (all having to do with the very strained object model around the ViewPoint database code) this is the least invasive method to allow for per-conversation shared contexts. Please do not replace this with any kind of wrapper/proxy based solution; it will not work.

Specified by:
setIgnoreClose in interface QDataContext

startTransaction

public void startTransaction()
                      throws java.lang.Exception
Specified by:
startTransaction in interface QDataContext
Throws:
java.lang.Exception

addCommitAction

public void addCommitAction(IRunnable cx)
Description copied from interface: QDataContext
Add an action which should be executed after a succesful commit on this context.

Specified by:
addCommitAction in interface QDataContext