com.zentense.step2.model
Class ResultSetModel

java.lang.Object
  extended by com.zentense.step2.model.ResultSetModel
Direct Known Subclasses:
AccessMaster, AutoModel, ResultSetFilterModel, ResultSetI18NModel

public class ResultSetModel
extends java.lang.Object

A model represent a table, that is, a collection of rows, where each row is a collection of columns, where one or more of the columns are used to identify each row from the other ones.

A ResultSetModel is a model that represents a relational database table and allows to execute SQL queries. The resulting JDBC Resultsets are encapsulated into ResultSetViews.

A ResultSetModel supports forward only, scrollable and updatable resultsets as well as prepared statements. The type of resultset is selected using rowMode, loopMode and updateMode.

Some methods have two versions: one including a RequestRuntime instance as an argument and other without it. It is safer use always the first ones, while is slightly faster using the second ones. The RequestRuntime object contains request information and it is the same during the entire request. Each request locks on a database connection and releases it when it finishes to ensure the code to be thread safe. If ResultSetModel queries a database with a database connection of an older request results may be unexpected. It is safer to use always the method that passes a RequestRuntime instance as an argument.

Version:
$Revision: 1.9 $
Author:
kurt

Field Summary
protected  java.lang.String idName
          Name of identifier column
static int logLevel
           
protected  java.util.Hashtable prepared
          Hashtable of prepared stataments
protected  java.lang.String preparedTableId
          Prepared table id
protected  int RSConcur
          Concurrency of newxt resultset
protected  int RSType
          Type of next resultset
protected  DBTableData.Table table
          Table metadata
 
Constructor Summary
ResultSetModel()
           
ResultSetModel(java.lang.String tableName, java.lang.String idName)
           
 
Method Summary
 void addPrepared(java.lang.String name, java.lang.String sql)
          Creates a prepared statement and leaves it ready to be executed with doPrepared.
 void clearPrepared()
          Clears prepared statements table
 void delete(java.lang.Object id)
          Executes a delete query (delete from table where id= ...)
 void deleteAll()
          Deletes all rows in this table
 void deleteBy(java.lang.String by)
          Deletes a group of rows (delete from table where ...)
 View doPrepared(java.lang.String name, java.lang.Object[] obj)
          Executes a prepared statement.
 View doQuery(java.lang.String sql)
          Executes a SQL query using JDBC.
 void doUpdate(java.lang.String sql)
          Executes a SQL statement using JDBC.
 boolean exists(java.lang.Object id)
          Exists a given row?
 View find(java.lang.String cols, java.lang.String where)
          Select a group of rows.
 View find(java.lang.String cols, java.lang.String where, java.lang.String order)
          Selects a group of rows with order.
 View findById(java.lang.Object id)
          Selects a row.
 java.lang.String genSQL(java.lang.String cols)
          Generates an SQL statement (select ... from table)
 java.lang.String genSQL(java.lang.String cols, java.lang.String where)
          Generates an SQL statement (select ... from table where ...)
protected  java.lang.String genSQL(java.lang.String cols, java.lang.String where, java.lang.String order)
          Generates an SQL statement (select ... from table where ... order by ...)
 View getAll()
          Selects all rows in table.
 View getAllOrderedBy(java.lang.String orderBy)
          Selects all rows in table ordered.
static java.sql.Connection getConnection()
           
 int getCount()
          Gets number of rows in table (select count(*) from table)
 int getMaxId()
          Gets maximum identifier in table.
 java.lang.String getPK()
          Returns identifier column name
 java.sql.PreparedStatement getPrepared(java.lang.String name)
          Returns a prepared statement
 java.lang.String getTableName()
          Returns table name
 ResultSetView insertMode()
          Returns an updatable view with one row that can be updated and then inserted into table with insertRow
 ResultSetView insertMode(java.lang.Object id)
          Returns an updatable view with one row that can be updated and then inserted into table with insertRow
 ResultSetModel loopMode()
          Prepares this model for generating a scrollable, read only resultset the next time a query will be executed.
 ResultSetModel prepare(int type, int concur)
          Sets the features for the next resultset generated by this model
 void prepareStatements()
          Creates prepared statements if any.
 ResultSetModel rowMode()
          Prepares this model for generating a forward only, read only resultset the next time a query will be executed.
 void update(java.lang.String set, java.lang.Object id)
          Executes a update query (update table set ... where id = ...)
 ResultSetModel updateMode()
          Prepares this model for generating a scrollable, updatale resultset the next time a query will be executed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logLevel

public static int logLevel

RSType

protected int RSType
Type of next resultset

See Also:
ResultSetModel#rowMode(RequestRuntime), ResultSetModel#loopMode(RequestRuntime)

RSConcur

protected int RSConcur
Concurrency of newxt resultset

See Also:
ResultSetModel#updateMode(RequestRuntime)

idName

protected java.lang.String idName
Name of identifier column


table

protected DBTableData.Table table
Table metadata


prepared

protected java.util.Hashtable prepared
Hashtable of prepared stataments


preparedTableId

protected java.lang.String preparedTableId
Prepared table id

Constructor Detail

ResultSetModel

public ResultSetModel()

ResultSetModel

public ResultSetModel(java.lang.String tableName,
                      java.lang.String idName)
Parameters:
tableName - Name of the table
idName - Name of identifier column
Method Detail

getPK

public final java.lang.String getPK()
Returns identifier column name


getTableName

public final java.lang.String getTableName()
Returns table name


getConnection

public static final java.sql.Connection getConnection()

rowMode

public final ResultSetModel rowMode()
Prepares this model for generating a forward only, read only resultset the next time a query will be executed. It is usually used for resultsets that contain only one row.


loopMode

public final ResultSetModel loopMode()
Prepares this model for generating a scrollable, read only resultset the next time a query will be executed. It is usually used for resultsets that may contain multiple rows.


updateMode

public final ResultSetModel updateMode()
Prepares this model for generating a scrollable, updatale resultset the next time a query will be executed. It is usually used for resultsets with one or more rows that can be update

See Also:
ResultSetView.updateRow()

prepare

public ResultSetModel prepare(int type,
                              int concur)
Sets the features for the next resultset generated by this model

Parameters:
vm - Runtime
type - JDBC Resultset scroll type
concur - JDBC Resultset concurrency
Returns:
The model itself

insertMode

public final ResultSetView insertMode()
                               throws java.sql.SQLException
Returns an updatable view with one row that can be updated and then inserted into table with insertRow

Throws:
java.sql.SQLException
See Also:
ResultSetView.insertRow()

insertMode

public final ResultSetView insertMode(java.lang.Object id)
                               throws java.lang.Exception
Returns an updatable view with one row that can be updated and then inserted into table with insertRow

Parameters:
id - Identifier for the new row
Throws:
java.lang.Exception
See Also:
ResultSetView.insertRow()

doQuery

public View doQuery(java.lang.String sql)
Executes a SQL query using JDBC. The query must be a select or other query that generate results. Use doUpdate for inserts, updates, deletes and alter tables. If rowMode was invoked before this method, the returned view is open and ready to be accessed. Otherwise view is positiones before the first row and a call to next or first must be done in order to position the view at the first available row.

Parameters:
sql - SQL statement to execute
Returns:
An instance of ResultSetView containing the ResultSet

doUpdate

public void doUpdate(java.lang.String sql)
Executes a SQL statement using JDBC. The statement must be a insert, delete, updates, or alter table because this methods does not return results.

Parameters:
sql - SQL statement to execute

update

public final void update(java.lang.String set,
                         java.lang.Object id)
Executes a update query (update table set ... where id = ...)

Parameters:
set - update list of sets (column1=val1, columns2=val2 ...)
id - Identifier value of row to be updated

genSQL

public final java.lang.String genSQL(java.lang.String cols)
Generates an SQL statement (select ... from table)

Parameters:
cols - Colums to be selected
Returns:
SQL statement

genSQL

public final java.lang.String genSQL(java.lang.String cols,
                                     java.lang.String where)
Generates an SQL statement (select ... from table where ...)

Parameters:
cols - Colums to be selected
where - Where clauses
Returns:
SQL statement

genSQL

protected final java.lang.String genSQL(java.lang.String cols,
                                        java.lang.String where,
                                        java.lang.String order)
Generates an SQL statement (select ... from table where ... order by ...)

Parameters:
cols - Colums to be selected
where - Where clauses
order - Order by part of SQL statement
Returns:
SQL statement

delete

public void delete(java.lang.Object id)
Executes a delete query (delete from table where id= ...)

Parameters:
id - Identifier value of row to be deleted

deleteBy

public final void deleteBy(java.lang.String by)
Deletes a group of rows (delete from table where ...)

Parameters:
by - Where clause that selects rows to be deleted

deleteAll

public final void deleteAll()
Deletes all rows in this table


findById

public View findById(java.lang.Object id)
Selects a row. This method relies on previous calls to rowMode or updateMode for working properly.

Parameters:
id - Identifier of row to be selected
Returns:
A ResultSetView

find

public final View find(java.lang.String cols,
                       java.lang.String where)
Select a group of rows. This method relies on previous calls to loopMode or updateMode for working properly.

Parameters:
cols - Cols to be selected
where - Where clause that defines rows to be selected
Returns:
A scrollable ResultSetView

find

public final View find(java.lang.String cols,
                       java.lang.String where,
                       java.lang.String order)
Selects a group of rows with order. This method relies on previous calls to loopMode or updateMode for working properly.

Parameters:
cols - Cols to be selected
where - Where clause that defines rows to be selected
order - Order part of SQL statement
Returns:
A scrollable ResultSetView

getAll

public final View getAll()
Selects all rows in table. This method relies on previous calls to loopMode or updateMode for working properly.

Returns:
A scrollable ResultSetView

getAllOrderedBy

public final View getAllOrderedBy(java.lang.String orderBy)
Selects all rows in table ordered. This method relies on previous calls to loopMode or updateMode for working properly.

Returns:
A scrollable ResultSetView

getCount

public final int getCount()
                   throws java.lang.Exception
Gets number of rows in table (select count(*) from table)

Parameters:
vm - A RequestRuntime instance
Returns:
Number of rows
Throws:
java.lang.Exception

getMaxId

public final int getMaxId()
                   throws java.lang.Exception
Gets maximum identifier in table. (select max(id) from table).

Parameters:
vm - A RequestRuntime instance
Returns:
Maximum identifier value in table
Throws:
java.lang.Exception

exists

public final boolean exists(java.lang.Object id)
                     throws java.lang.Exception
Exists a given row?

Parameters:
id - Value of identifier column
Returns:
true if a row with given value in identifier column exists, false otherwise
Throws:
java.lang.Exception

prepareStatements

public void prepareStatements()
Creates prepared statements if any. This method is called the first time that prepared statements are used on this model and must be implemented by subclasses that use prepared statements. It is expected that this method will call to addPrepared for creating the prepared statements


addPrepared

public final void addPrepared(java.lang.String name,
                              java.lang.String sql)
Creates a prepared statement and leaves it ready to be executed with doPrepared. Notice that the type and concurrency of resultset generated in the future by this prepared statement will be the current ones.

Parameters:
name - Name for this prepared statement
sql - SQL Statement to prepare

getPrepared

public final java.sql.PreparedStatement getPrepared(java.lang.String name)
Returns a prepared statement

Parameters:
name - Name of prepared statement
Returns:
A prepared statement

clearPrepared

public final void clearPrepared()
Clears prepared statements table


doPrepared

public View doPrepared(java.lang.String name,
                       java.lang.Object[] obj)
Executes a prepared statement.

Parameters:
name - Name of prepared statement previosly crated with addPrepared
obj - Array of objects to be set in statement. Use ResultSetView.NULL for setting a null value into the statement.
Returns:
Resultset with results (if any)

 

© Zentense 2008