org.brains2b.data.sql.cursor
Class SqlCursor

java.lang.Object
  extended by org.brains2b.data.cursor.Cursor
      extended by org.brains2b.data.sql.cursor.SqlCursor
Direct Known Subclasses:
OracleCursor

public class SqlCursor
extends Cursor

Implementation of the Cursor object for Sql queries and add functionality for having queries with parameters.

The sql statement can contain placeholder characters ? signifying a parameter which can be set at runtime by calling addParameter(index, value). The index corresponds with the count of the placeholder character in the SQL statement starting at 1

Version:
0.25 [21-03-2008]
Author:
dennis@brains2b.nl

Field Summary
protected  java.sql.Connection m_con
           
protected  java.sql.PreparedStatement m_prep
           
protected  org.brains2b.sql.meta.SelectDescriptor m_select
           
 
Fields inherited from class org.brains2b.data.cursor.Cursor
m_prototype
 
Constructor Summary
SqlCursor(java.sql.Connection con, java.lang.String name, java.lang.String sql)
          Constructor for SqlCursor.
SqlCursor(java.lang.String name, java.lang.String sql)
          Constructor for SqlCursor
 
Method Summary
 void addParameter(int idx, double d)
           
 void addParameter(int idx, int i)
          adds a integer value for the given placeholder position in the prepared statement.
 void addParameter(int idx, long l)
           
 void addParameter(int idx, java.lang.Object o)
          adds a value for the given placeholder position in the prepared statement.
 int delete(CursorData cdata)
          delete an object of type o from the datastore
 java.sql.Connection getConnection()
           
protected  CursorData getObject(CursorData prototype, java.sql.ResultSet rs)
          get a CursorData object filled from the current position in the ResultSet.
 java.sql.PreparedStatement getPreparedStatement(java.sql.Connection con, java.lang.String select)
          returns a prepared statement build from the prepared query set, the parameters added, the conditions set and the order by supplied.
 CursorData getProtoType()
          get a prototype for this cursor, which is basically a defintion of the columns and types, but without the values
protected  CursorData getProtoType(java.sql.ResultSetMetaData rsMd)
          get a prototype on the basis of the SQL-statement for this Cursor, which is defined in the ResultSetMetaData.
 org.brains2b.sql.meta.SelectDescriptor getSelect()
           
 int insert(CursorData cdata)
          insert the cursor data in the table for this cursor
 void reset()
          reset this Cursor by removing all previously set parameter values
 CursorData retrieve(Condition con)
          select an Object of the type of the Object o which match the supplied condition.
 java.util.List retrieveList()
          retrieve a list of Objects of the type of the Object
 java.util.List retrieveList(Condition condition)
          retrieve a list of Objects of the type of the Object o which match the supplied condition.
 void setConnection(java.sql.Connection con)
          set the java.sql.Connection to use for this Cursor
 java.lang.String toString()
          Overwritten to return the SQL statement that defines the Cursor
 int update(CursorData cdata)
          update an object of type o to the datastore
 
Methods inherited from class org.brains2b.data.cursor.Cursor
getName, getNewCursorData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_select

protected org.brains2b.sql.meta.SelectDescriptor m_select

m_prep

protected java.sql.PreparedStatement m_prep

m_con

protected java.sql.Connection m_con
Constructor Detail

SqlCursor

public SqlCursor(java.lang.String name,
                 java.lang.String sql)
Constructor for SqlCursor

Parameters:
name - String, the name of the cursor
sql - String, the SQL-statement to use for this cursor

SqlCursor

public SqlCursor(java.sql.Connection con,
                 java.lang.String name,
                 java.lang.String sql)
Constructor for SqlCursor.

Usually the Connection does not have to be set at initialization, you can use setConnection(Connection)

Parameters:
con - java.sql.Connection, the JDBC connection to use for this Cursor
name - String, the name of the cursor
sql - String, the SQL-statement to use for this cursor
Method Detail

retrieve

public CursorData retrieve(Condition con)
                    throws DataException
Description copied from class: Cursor
select an Object of the type of the Object o which match the supplied condition.

Specified by:
retrieve in class Cursor
Parameters:
con - Condition the condition which should be met by the retrieved objects
Returns:
An object which meets the condition. If more then one object meets the criteria implementations should only return the first one. Should return null if no object meets the condition.
Throws:
DataException
See Also:
DataController.retrieve(Object, Condition), Condition

getObject

protected CursorData getObject(CursorData prototype,
                               java.sql.ResultSet rs)
                        throws java.sql.SQLException
Description copied from class: Cursor
get a CursorData object filled from the current position in the ResultSet.

Overrides:
getObject in class Cursor
Parameters:
prototype - CursorData, object which functions as a prototype, will be cloned to function as return type.
rs - ResultSet, a resultset with a valid position
Returns:
CursorData, an object with all fields and values set.
Throws:
java.sql.SQLException

getProtoType

public CursorData getProtoType()
                        throws DataException
Description copied from class: Cursor
get a prototype for this cursor, which is basically a defintion of the columns and types, but without the values

Specified by:
getProtoType in class Cursor
Returns:
CursorData
Throws:
DataException

retrieveList

public java.util.List retrieveList()
                            throws DataException
Description copied from class: Cursor
retrieve a list of Objects of the type of the Object

Specified by:
retrieveList in class Cursor
Returns:
A list of CursorData which meet the defined cursor condition.
Throws:
DataException

retrieveList

public java.util.List retrieveList(Condition condition)
                            throws DataException
Description copied from class: Cursor
retrieve a list of Objects of the type of the Object o which match the supplied condition.

Specified by:
retrieveList in class Cursor
Parameters:
condition - Condition the condition which should be met by the retrieved objects
Returns:
A Vector of Objects which meet the condition. Should return null if no object meets the condition.
Throws:
DataException
See Also:
DataController.retrieveList(Object, Condition), Condition

update

public int update(CursorData cdata)
           throws DataException
Description copied from class: Cursor
update an object of type o to the datastore

Specified by:
update in class Cursor
Parameters:
cdata - CursorData, the object to update
Returns:
int, the number or records updated, should always return one
Throws:
DataException
See Also:
DataController.update(Object)

delete

public int delete(CursorData cdata)
           throws DataException
Description copied from class: Cursor
delete an object of type o from the datastore

Specified by:
delete in class Cursor
Parameters:
cdata - CursorData, the object to delete
Returns:
int, the number or records deleted, should always return one
Throws:
DataException
See Also:
DataController.delete(Object)

insert

public int insert(CursorData cdata)
           throws DataException
insert the cursor data in the table for this cursor

Specified by:
insert in class Cursor
Parameters:
cdata - CursorData, data to insert
Returns:
int, the number of records inserted should always be one
Throws:
java.lang.Exception
DataException
Since:
DataC 0.31
See Also:
DataController.insert(Object)

addParameter

public void addParameter(int idx,
                         int i)
adds a integer value for the given placeholder position in the prepared statement.

Parameters:
idx - int, the placeholder position
i - int, The value to set in the prepared statement

addParameter

public void addParameter(int idx,
                         long l)

addParameter

public void addParameter(int idx,
                         double d)

addParameter

public void addParameter(int idx,
                         java.lang.Object o)
adds a value for the given placeholder position in the prepared statement.

Parameters:
idx - int, the placeholder position
o - Object, the value to set in the prepared statement.

reset

public void reset()
reset this Cursor by removing all previously set parameter values

See Also:
addParameter(int, int), addParameter(int, Object)

getPreparedStatement

public java.sql.PreparedStatement getPreparedStatement(java.sql.Connection con,
                                                       java.lang.String select)
                                                throws java.sql.SQLException
returns a prepared statement build from the prepared query set, the parameters added, the conditions set and the order by supplied.

If a prepared query has placeholders, the addParameters must be called for every placeholder supplied.

Parameters:
con - Connection, the connection for which to prepare the query
select - String, the select containing the fields to retrieve and the table

This should be a valid ANSI-SQL `SELECT` statement without any conditions.

Returns:
PreparedStatement, the PreparedStatement created for execution
Throws:
SQLException, - throws the Exception if generated by the prepareStatement command in java.sql.Connection()
java.sql.SQLException
See Also:
Connection, PreparedStatement

setConnection

public void setConnection(java.sql.Connection con)
set the java.sql.Connection to use for this Cursor

Parameters:
con - Connection

getProtoType

protected CursorData getProtoType(java.sql.ResultSetMetaData rsMd)
                           throws java.sql.SQLException
get a prototype on the basis of the SQL-statement for this Cursor, which is defined in the ResultSetMetaData.

Parameters:
rsMd - java.sql.ResultSetMetaData, created on the basis of this cursor SQL-statement
Returns:
CursorData, with all the fields defined but no values.
Throws:
SQLException, - thrown if the database delivers an SQLException.
java.sql.SQLException

toString

public java.lang.String toString()
Overwritten to return the SQL statement that defines the Cursor

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

getConnection

public java.sql.Connection getConnection()

getSelect

public org.brains2b.sql.meta.SelectDescriptor getSelect()