org.tentackle.db
Class StatementWrapper

java.lang.Object
  extended by org.tentackle.db.StatementWrapper
Direct Known Subclasses:
PreparedStatementWrapper

public class StatementWrapper
extends Object

A wrapper for sql statements.
Will catch and report SQLExceptions and keep track of being used only once after Db.createStatement().

Author:
harald

Field Summary
protected  ManagedConnection con
          the managed connection
protected  boolean ready
          flag if statement is marked ready for being consumed
protected  Statement stmt
          the sql statement
 
Constructor Summary
StatementWrapper(ManagedConnection con, Statement stmt)
          Creates a wrapper for an sql statement.
 
Method Summary
 void close()
          Closes this statement.
protected  void detachDb()
          Detach the db from the connection.
 ResultSetWrapper executeQuery(String sql)
          Executes a query.
 ResultSetWrapper executeQuery(String sql, boolean withinTx)
          Executes a query.
 int executeUpdate(String sql)
          Executes the given SQL statement.
 ManagedConnection getConnection()
          Gets the connection.
 Db getDb()
          Gets the currently attached Db.
 int getFetchDirection()
          Retrieves the direction for fetching rows from database tables that is the default for result sets generated from this Statement object.
 int getFetchSize()
          Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object.
 int getMaxRows()
          Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain.
 Statement getStatement()
          Gets the wrapped statement.
 boolean isClosed()
          Determines whether this statement is closed.
 boolean isMarkedReady()
          Returns whether this statement is marked ready.
Used to determine unused pending statements in servers when closing dead sessions.
 void markReady()
          Marks the statement to be ready for being consumed by a Db attached to a ConnectionManager.
This is an additional measure to enforce the programming rule that a statement is being used only once after Db.createStatement() (for non-prepared statements) or after Db.getPreparedStatement(int) for prepared statements.
 void setFetchDirection(int direction)
          Gives the driver a hint as to the direction in which rows will be processed in ResultSet objects created using this Statement object.
 void setFetchSize(int rows)
          Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement.
 void setMaxRows(int max)
          Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number.
 String toString()
          Returns the text representation of the wrapped statement.
 void unmarkReady()
          Unmarks (consumes) this statement previously marked ready.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

con

protected ManagedConnection con
the managed connection


stmt

protected Statement stmt
the sql statement


ready

protected boolean ready
flag if statement is marked ready for being consumed

Constructor Detail

StatementWrapper

public StatementWrapper(ManagedConnection con,
                        Statement stmt)
Creates a wrapper for an sql statement.

Parameters:
con - the connection
stmt - the sql statement
Method Detail

getConnection

public ManagedConnection getConnection()
Gets the connection.

Returns:
the connection

getStatement

public Statement getStatement()
Gets the wrapped statement.

Returns:
the statement, null if closed

getDb

public Db getDb()
Gets the currently attached Db.

Returns:
the db

markReady

public void markReady()
Marks the statement to be ready for being consumed by a Db attached to a ConnectionManager.
This is an additional measure to enforce the programming rule that a statement is being used only once after Db.createStatement() (for non-prepared statements) or after Db.getPreparedStatement(int) for prepared statements.

Notice: if a statement may be marked ready more than once, i.e. an open result exists (which would be closed according to the JDBC specs), a DbRuntimeException is thrown. The specs in Statement say:

 By default, only one ResultSet object per Statement object can be open at the same time. 
 Therefore, if the reading of one ResultSet object is interleaved with the reading of another, 
 each must have been generated by different Statement objects. 
 All execution methods in the Statement interface implicitly close a statment's current 
 ResultSet object if an open one exists.
 
Without this additional measure a "ResultSet closed" exception will be thrown by the JDBC-driver on the next usage of the first resultset and you wouldn't have any clue which resultset forced the closing.


unmarkReady

public void unmarkReady()
Unmarks (consumes) this statement previously marked ready.


isMarkedReady

public boolean isMarkedReady()
Returns whether this statement is marked ready.
Used to determine unused pending statements in servers when closing dead sessions.

Returns:
true if pending

toString

public String toString()
Returns the text representation of the wrapped statement.

Overrides:
toString in class Object
Returns:
a string representation of the object.

detachDb

protected void detachDb()
Detach the db from the connection. Statements detach the db on executeUpdate or on close() in the resultsetwrapper after executeQuery.


executeUpdate

public int executeUpdate(String sql)
Executes the given SQL statement.

Parameters:
sql - an sql-statement
Returns:
the row count

executeQuery

public ResultSetWrapper executeQuery(String sql,
                                     boolean withinTx)
Executes a query.

Parameters:
sql - is the query sql string
withinTx - is true if start a transaction for this query.
Returns:
the result set as a ResultSetWrapper

executeQuery

public ResultSetWrapper executeQuery(String sql)
Executes a query.

Parameters:
sql - is the query sql string
Returns:
the result set as a ResultSetWrapper

close

public void close()
Closes this statement.


isClosed

public boolean isClosed()
Determines whether this statement is closed.

Returns:
true if statement closed

setFetchSize

public void setFetchSize(int rows)
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.

Parameters:
rows - the number of rows to fetch
See Also:
getFetchSize()

getFetchSize

public int getFetchSize()
Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object. If this Statement object has not set a fetch size by calling the method setFetchSize, the return value is implementation-specific.

Returns:
the default fetch size for result sets generated from this Statement object
See Also:
setFetchSize(int)

setMaxRows

public void setMaxRows(int max)
Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number. If the limit is exceeded, the excess rows are silently dropped.

Parameters:
max - the new max rows limit; zero means there is no limit
See Also:
getMaxRows()

getMaxRows

public int getMaxRows()
Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain. If this limit is exceeded, the excess rows are silently dropped.

Returns:
the current maximum number of rows for a ResultSet object produced by this Statement object; zero means there is no limit
See Also:
setMaxRows(int)

setFetchDirection

public void setFetchDirection(int direction)
Gives the driver a hint as to the direction in which rows will be processed in ResultSet objects created using this Statement object. The default value is ResultSet.FETCH_FORWARD.

Note that this method sets the default fetch direction for result sets generated by this Statement object. Each result set has its own methods for getting and setting its own fetch direction.

Parameters:
direction - the initial direction for processing rows
See Also:
getFetchDirection()

getFetchDirection

public int getFetchDirection()
Retrieves the direction for fetching rows from database tables that is the default for result sets generated from this Statement object. If this Statement object has not set a fetch direction by calling the method setFetchDirection, the return value is implementation-specific.

Returns:
the default fetch direction for result sets generated from this Statement object
See Also:
setFetchDirection(int)


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de