net.sf.farrago.db
Class FarragoDbStmtContextBase

java.lang.Object
  extended by net.sf.farrago.db.FarragoDbStmtContextBase
All Implemented Interfaces:
FarragoSessionStmtContext, FarragoAllocation, ClosableAllocation
Direct Known Subclasses:
FarragoDbStmtContext

public abstract class FarragoDbStmtContextBase
extends Object
implements FarragoSessionStmtContext

FarragoDbStmtContextBase provides a partial implementation of FarragoSessionStmtContext in terms of FarragoDbSession.

The basic funtionality provided here may be extended to implement statements in a way specific to extension projects.

See individual methods for assistance in determining when they may be called.

Most non-trivial public methods on this class must be synchronized on the parent session, since closeAllocation may be called from a thread shutting down the database. The exception is cancel, which must NOT be synchronized, since it needs to return immediately. (We synchronize on the parent session to avoid deadlocks from session/stmt vs. stmt/session lock order; see http://issues.eigenbase.org/browse/LDB-150 for an example.)

Author:
Stephan Zuercher

Field Summary
protected  CancelFlag cancelFlag
           
protected  List<FarragoSessionStmtContext> childrenStmtContexts
          The children statement contexts associated with a root statement context.
protected  boolean daemon
           
private  FarragoDdlLockManager ddlLockManager
           
protected  FarragoSessionStmtParamDef[] dynamicParamDefs
          Definitions of dynamic parameters.
protected  Object[] dynamicParamValues
          Current dynamic parameter bindings.
protected  boolean[] dynamicParamValuesSet
          Indicates if dynamic parameter is set Used to validate that all dynamic parameters have been set
private  FarragoSessionExecutingStmtInfo info
           
protected  FarragoSessionStmtParamDefFactory paramDefFactory
           
protected  FarragoSessionStmtContext rootStmtContext
           
protected  boolean saveFirstCsn
          Indicates whether the csn associated with the first txn initiated from a root context or one of its children contexts needs to be saved.
protected  FarragoDbSession session
           
protected  Long snapshotCsn
          If non-null, the commit sequence number to be used for all transactions associated with a root context as well as children contexts associated with that root context.
protected  String sql
           
private  long stmtCurrentTime
           
protected static Logger tracer
           
 
Constructor Summary
protected FarragoDbStmtContextBase(FarragoDbSession session, FarragoSessionStmtParamDefFactory paramDefFactory, FarragoDdlLockManager ddlLockManager)
          Creates a new FarragoDbStmtContextBase object.
protected FarragoDbStmtContextBase(FarragoDbSession session, FarragoSessionStmtParamDefFactory paramDefFactory, FarragoDdlLockManager ddlLockManager, FarragoSessionStmtContext rootStmtContext)
          Creates a new FarragoDbStmtContextBase object.
 
Method Summary
protected  void accessTable(List<String> table, TableAccessMap.Mode mode)
          Acquires locks (or whatever transaction manager wants) on a single table.
protected  void accessTables(FarragoSessionExecutableStmt executableStmt)
          Acquires locks (or whatever transaction manager wants) on all tables accessed by this statement.
 void addChildStmtContext(FarragoSessionStmtContext childStmtContext)
          Adds a child statement context to the list of children context for a statement.
protected  void checkDynamicParamsSet()
          Checks that all dynamic parameters have been set.
protected  void clearExecutingStmtInfo()
          Clears session's FarragoSessionExecutingStmtInfo.
 void clearParameters()
          Clears any settings for all dynamic parameters.
 void closeAllocation()
          Closes this object.
 void daemonize()
          Turns this context into a daemon so that it will be deallocated as soon as its current result set is closed.
 CancelFlag getCancelFlag()
          Gets the cancellation flag for this context.
 FarragoSessionExecutingStmtInfo getExecutingStmtInfo()
          Returns an object which contains information about this executing statement.
 FarragoSession getSession()
           
private  FarragoDbSessionInfo getSessionInfo()
           
 String getSql()
           
 long getStmtCurrentTime()
           
protected  void initDynamicParams(RelDataType dynamicParamRowType)
          Initializes the dynamicParamDefs and dynamicParamValues arrays based on the dynamic param row type,
protected  void initExecutingStmtInfo(FarragoSessionExecutableStmt executableStmt)
          Initializes the session's FarragoSessionExecutingStmtInfo.
protected  void lockObjectInUse(String mofId)
          Marks a single object, represented by its mofId, as in-use.
protected  void lockObjectsInUse(FarragoSessionExecutableStmt newExecutableStmt)
          See FarragoDbSession.
private  void lockTables(TableAccessMap accessMap)
          Calls the transaction manager to access a set of tables.
 boolean needToSaveFirstTxnCsn()
           
 void saveFirstTxnCsn(long csn)
          Saves the commit sequence number associated with the first transaction initiated by a stmt associated with a root context.
 void setDynamicParam(int parameterIndex, Object x)
          Sets an input parameter.
 void setDynamicParam(int parameterIndex, Object x, Calendar cal)
          Sets an input parameter.
 void setSaveFirstTxnCsn()
          Indicates that the context needs to retrieve and save the commit sequence number for the very first transaction initiated by a stmt context associated with a root context.
protected  void startAutocommitTxn(boolean readOnly)
          Starts an auto commit transaction.
protected  void traceExecute()
          Traces execution.
 void unprepare()
          Releases any resources (including result sets) associated with this statement context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.farrago.session.FarragoSessionStmtContext
cancel, closeResultSet, execute, getPreparedParamType, getPreparedRowType, getQueryTimeout, getResultSet, getUpdateCount, getWarningQueue, isPrepared, isPreparedDml, kill, prepare, prepare, setQueryTimeout
 

Field Detail

tracer

protected static final Logger tracer

session

protected final FarragoDbSession session

paramDefFactory

protected final FarragoSessionStmtParamDefFactory paramDefFactory

dynamicParamDefs

protected FarragoSessionStmtParamDef[] dynamicParamDefs
Definitions of dynamic parameters.


dynamicParamValues

protected Object[] dynamicParamValues
Current dynamic parameter bindings.


dynamicParamValuesSet

protected boolean[] dynamicParamValuesSet
Indicates if dynamic parameter is set Used to validate that all dynamic parameters have been set


daemon

protected boolean daemon

sql

protected String sql

ddlLockManager

private FarragoDdlLockManager ddlLockManager

info

private FarragoSessionExecutingStmtInfo info

stmtCurrentTime

private final long stmtCurrentTime

rootStmtContext

protected final FarragoSessionStmtContext rootStmtContext

childrenStmtContexts

protected List<FarragoSessionStmtContext> childrenStmtContexts
The children statement contexts associated with a root statement context.


snapshotCsn

protected Long snapshotCsn
If non-null, the commit sequence number to be used for all transactions associated with a root context as well as children contexts associated with that root context. Only used if the personality supports snapshots.


saveFirstCsn

protected boolean saveFirstCsn
Indicates whether the csn associated with the first txn initiated from a root context or one of its children contexts needs to be saved. Only used if the personality supports snapshots.


cancelFlag

protected final CancelFlag cancelFlag
Constructor Detail

FarragoDbStmtContextBase

protected FarragoDbStmtContextBase(FarragoDbSession session,
                                   FarragoSessionStmtParamDefFactory paramDefFactory,
                                   FarragoDdlLockManager ddlLockManager)
Creates a new FarragoDbStmtContextBase object.

Parameters:
session - the session creating this statement
paramDefFactory - dynamic parameter definition factory
ddlLockManager - ddl object lock manager

FarragoDbStmtContextBase

protected FarragoDbStmtContextBase(FarragoDbSession session,
                                   FarragoSessionStmtParamDefFactory paramDefFactory,
                                   FarragoDdlLockManager ddlLockManager,
                                   FarragoSessionStmtContext rootStmtContext)
Creates a new FarragoDbStmtContextBase object.

Parameters:
session - the session creating this statement
paramDefFactory - dynamic parameter definition factory
ddlLockManager - ddl object lock manager
rootStmtContext - the root statement context for an internally prepared statement; for an externally prepared statement, this will be null
Method Detail

closeAllocation

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

Specified by:
closeAllocation in interface ClosableAllocation

getSession

public FarragoSession getSession()
Specified by:
getSession in interface FarragoSessionStmtContext
Returns:
the session from which this statement context was created

getExecutingStmtInfo

public FarragoSessionExecutingStmtInfo getExecutingStmtInfo()
Description copied from interface: FarragoSessionStmtContext
Returns an object which contains information about this executing statement.

Specified by:
getExecutingStmtInfo in interface FarragoSessionStmtContext
Returns:
FarragoSessionExecutingStmtInfo

daemonize

public void daemonize()
Description copied from interface: FarragoSessionStmtContext
Turns this context into a daemon so that it will be deallocated as soon as its current result set is closed.

Specified by:
daemonize in interface FarragoSessionStmtContext

unprepare

public void unprepare()
Description copied from interface: FarragoSessionStmtContext
Releases any resources (including result sets) associated with this statement context.

Specified by:
unprepare in interface FarragoSessionStmtContext

setDynamicParam

public void setDynamicParam(int parameterIndex,
                            Object x)
Description copied from interface: FarragoSessionStmtContext
Sets an input parameter.

Specified by:
setDynamicParam in interface FarragoSessionStmtContext
Parameters:
parameterIndex - 0-based index of parameter to set
x - value to set

setDynamicParam

public void setDynamicParam(int parameterIndex,
                            Object x,
                            Calendar cal)
Description copied from interface: FarragoSessionStmtContext
Sets an input parameter.

Specified by:
setDynamicParam in interface FarragoSessionStmtContext
Parameters:
parameterIndex - 0-based index of parameter to set
x - value to set

clearParameters

public void clearParameters()
Description copied from interface: FarragoSessionStmtContext
Clears any settings for all dynamic parameters.

Specified by:
clearParameters in interface FarragoSessionStmtContext

getSql

public String getSql()
Specified by:
getSql in interface FarragoSessionStmtContext

getStmtCurrentTime

public long getStmtCurrentTime()
Specified by:
getStmtCurrentTime in interface FarragoSessionStmtContext
Returns:
the current time for this statement

setSaveFirstTxnCsn

public void setSaveFirstTxnCsn()
Description copied from interface: FarragoSessionStmtContext
Indicates that the context needs to retrieve and save the commit sequence number for the very first transaction initiated by a stmt context associated with a root context. Can only be called on the root context.

Specified by:
setSaveFirstTxnCsn in interface FarragoSessionStmtContext

needToSaveFirstTxnCsn

public boolean needToSaveFirstTxnCsn()
Specified by:
needToSaveFirstTxnCsn in interface FarragoSessionStmtContext
Returns:
whether the context needs to retrieve and save the commit sequence number for the very first transaction initiated by a stmt context associated with a root context; can only be called on the root context

saveFirstTxnCsn

public void saveFirstTxnCsn(long csn)
Description copied from interface: FarragoSessionStmtContext
Saves the commit sequence number associated with the first transaction initiated by a stmt associated with a root context. Can only be called on the root context.

Specified by:
saveFirstTxnCsn in interface FarragoSessionStmtContext
Parameters:
csn - the commit sequence number

addChildStmtContext

public void addChildStmtContext(FarragoSessionStmtContext childStmtContext)
Description copied from interface: FarragoSessionStmtContext
Adds a child statement context to the list of children context for a statement. Can only be called on the root context.

Specified by:
addChildStmtContext in interface FarragoSessionStmtContext

startAutocommitTxn

protected void startAutocommitTxn(boolean readOnly)
Starts an auto commit transaction.

Call near the beginning of FarragoSessionStmtContext.execute().

Parameters:
readOnly - true if statement is read only (e.g. not DML)

initDynamicParams

protected void initDynamicParams(RelDataType dynamicParamRowType)
Initializes the dynamicParamDefs and dynamicParamValues arrays based on the dynamic param row type,

Call after statement preparation, when the dynamic param row type is known, but before FarragoSessionStmtContext.setDynamicParam(int, Object) or FarragoSessionStmtContext.clearParameters().

Parameters:
dynamicParamRowType - dynamic param row type (e.g., FarragoSessionExecutableStmt.getDynamicParamRowType()

checkDynamicParamsSet

protected void checkDynamicParamsSet()
Checks that all dynamic parameters have been set.


accessTables

protected void accessTables(FarragoSessionExecutableStmt executableStmt)
Acquires locks (or whatever transaction manager wants) on all tables accessed by this statement.

Call during FarragoSessionStmtContext.execute() to lock tables used by your FarragoSessionExecutableStmt.

Parameters:
executableStmt - executable statement

accessTable

protected void accessTable(List<String> table,
                           TableAccessMap.Mode mode)
Acquires locks (or whatever transaction manager wants) on a single table.

Parameters:
table - fully qualified table name, represented as a list
mode - access mode for the table

lockTables

private void lockTables(TableAccessMap accessMap)
Calls the transaction manager to access a set of tables.

Parameters:
accessMap - map containing the tables being accessed and their access modes

lockObjectsInUse

protected void lockObjectsInUse(FarragoSessionExecutableStmt newExecutableStmt)
See FarragoDbSession.

Call from FarragoSessionStmtContext.prepare(RelNode, SqlKind, boolean, FarragoSessionPreparingStmt) after preparation is complete.

Parameters:
newExecutableStmt -

lockObjectInUse

protected void lockObjectInUse(String mofId)
Marks a single object, represented by its mofId, as in-use.

Parameters:
mofId - mofId of the object being marked as in-use

initExecutingStmtInfo

protected void initExecutingStmtInfo(FarragoSessionExecutableStmt executableStmt)
Initializes the session's FarragoSessionExecutingStmtInfo.

Call before FarragoSessionExecutableStmt.execute( FarragoSessionRuntimeContext).

Parameters:
executableStmt - executable statement

clearExecutingStmtInfo

protected void clearExecutingStmtInfo()
Clears session's FarragoSessionExecutingStmtInfo.

Call on FarragoSessionStmtContext.cancel(), FarragoSessionStmtContext.closeResultSet(), or whenever the statement is known to have stopped executing.


getCancelFlag

public CancelFlag getCancelFlag()
Description copied from interface: FarragoSessionStmtContext
Gets the cancellation flag for this context. This is part of the implementation of cancel-checking; callers who actually want to request cancellation should use the FarragoSessionStmtContext.cancel() method instead.

Specified by:
getCancelFlag in interface FarragoSessionStmtContext
Returns:
the cancellation flag for this context

traceExecute

protected void traceExecute()
Traces execution.

Optionally, call from FarragoSessionStmtContext.execute().


getSessionInfo

private FarragoDbSessionInfo getSessionInfo()