jodd.db.orm.mapper
Class DefaultResultSetMapper

java.lang.Object
  extended by jodd.db.orm.mapper.DefaultResultSetMapper
All Implemented Interfaces:
ResultSetMapper

public class DefaultResultSetMapper
extends java.lang.Object
implements ResultSetMapper

Maps all columns of database result set (RS) row to objects. It does it in two steps: preparation (reading table and column names) and parsing (parsing one result set row to resulting objects).

Preparation
Default mapper reads RS column and table names from RS meta-data and external maps, if provided. Since column name is always available in RS meta-data, it may be used to hold table name information. Column names may contain table code separator (DbOrm.getColumnAliasSeparator() that divides column name to table reference and column name. Here, table reference may be either table name or table alias. When it is table alias, external alias-to-name map must be provided. Hence, this defines the table name, and there is no need to read it from RS meta-data.

When column name doesn't contain a separator, it may be either an actual column name, or a column code. For column codes, both table and column name is lookup-ed from external map. If column name is an actual column name, table information is read from the RS meta data. Unfortunately, some DBs (such Oracle) doesn't implements this simple JDBC feature. Therefore, it must be expected that column table name is not available.

Table name is also not available for columns which are not directly table columns: e.g. some calculations, counts etc.

Parsing
Parser takes types array and tries to populate their instances in best possible way. It assumes that provided types list matches selected columns. That is very important, and yet very easy and natural to follow. So, parser will try to inject columns value into the one result instance. Now, there are two types of instances: simple types (numbers and strings) and entities (pojo objects). Simple types are always mapped to one and only one column. Entities will be mapped to all possible columns that can be matched starting from current column. So, simple types are not column-hungry, entity types are column-hungry:)

A column can be injected in one entities property only once. If one column is already mapped to current result, RS mapper will assume that current result is finished with mapping and will proceed to the next one. Similarly, if property name is not found for a column, RS mapper will proceed to the next result. Therefore, entity types are column precise and hungry;) - all listed columns must be mapped somewhere.

Results that are not used during parsing will be set to null.


Field Summary
protected  int cachedColumnNdx
           
protected  java.lang.Object cachedColumnValue
           
protected  java.lang.String[] cachedTypesTableNames
           
protected  java.lang.Class[] cachedUsedTypes
           
protected  java.lang.String[] columnNames
           
protected  DbOrm dbOrm
           
protected  java.sql.ResultSet rs
           
protected  java.lang.String[] tableNames
           
protected  int totalColumns
           
 
Constructor Summary
DefaultResultSetMapper(java.sql.ResultSet rs)
           
DefaultResultSetMapper(java.sql.ResultSet rs, DbOrm orm)
           
DefaultResultSetMapper(java.sql.ResultSet rs, java.util.Map<java.lang.String,ColumnData> columnAliases)
           
DefaultResultSetMapper(java.sql.ResultSet rs, java.util.Map<java.lang.String,ColumnData> columnAliases, DbOrm orm)
          Reads RS meta-data for column and table names.
 
Method Summary
 void close()
          Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
protected  java.lang.String[] createTypesTableNames(java.lang.Class[] types)
          Creates table names for all specified types.
 java.sql.ResultSet getResultSet()
          Return JDBC result set.
protected  java.lang.Object newInstance(java.lang.Class types)
          Creates new instances of a types.
 boolean next()
          Moves the cursor down one row from its current position.
 java.lang.Object[] parseObjects(java.lang.Class... types)
          Parse objects from result set row to specified types.
 java.lang.Object parseOneObject(java.lang.Class... types)
          Parse single object from result set row to specified type.
protected  java.lang.Object readColumnValue(int colNdx)
          Reads column value from result set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dbOrm

protected DbOrm dbOrm

rs

protected java.sql.ResultSet rs

totalColumns

protected int totalColumns

columnNames

protected java.lang.String[] columnNames

tableNames

protected java.lang.String[] tableNames

cachedUsedTypes

protected java.lang.Class[] cachedUsedTypes

cachedTypesTableNames

protected java.lang.String[] cachedTypesTableNames

cachedColumnNdx

protected int cachedColumnNdx

cachedColumnValue

protected java.lang.Object cachedColumnValue
Constructor Detail

DefaultResultSetMapper

public DefaultResultSetMapper(java.sql.ResultSet rs)

DefaultResultSetMapper

public DefaultResultSetMapper(java.sql.ResultSet rs,
                              DbOrm orm)

DefaultResultSetMapper

public DefaultResultSetMapper(java.sql.ResultSet rs,
                              java.util.Map<java.lang.String,ColumnData> columnAliases)

DefaultResultSetMapper

public DefaultResultSetMapper(java.sql.ResultSet rs,
                              java.util.Map<java.lang.String,ColumnData> columnAliases,
                              DbOrm orm)
Reads RS meta-data for column and table names.

Method Detail

next

public boolean next()
Moves the cursor down one row from its current position.

Specified by:
next in interface ResultSetMapper

close

public void close()
Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Specified by:
close in interface ResultSetMapper

getResultSet

public java.sql.ResultSet getResultSet()
Return JDBC result set.

Specified by:
getResultSet in interface ResultSetMapper

newInstance

protected java.lang.Object newInstance(java.lang.Class types)
Creates new instances of a types.


createTypesTableNames

protected java.lang.String[] createTypesTableNames(java.lang.Class[] types)
Creates table names for all specified types. Since this is usually done once per result set, these names are cached. Type name will be null for simple names, i.e. for all those types that returns null when used by DbOrm.lookupType(Class).


readColumnValue

protected java.lang.Object readColumnValue(int colNdx)
Reads column value from result set. Since this method may be called more then once for the same column, it caches column value.


parseObjects

public java.lang.Object[] parseObjects(java.lang.Class... types)
Description copied from interface: ResultSetMapper
Parse objects from result set row to specified types.

Specified by:
parseObjects in interface ResultSetMapper

parseOneObject

public java.lang.Object parseOneObject(java.lang.Class... types)
Description copied from interface: ResultSetMapper
Parse single object from result set row to specified type.

Specified by:
parseOneObject in interface ResultSetMapper
See Also:
ResultSetMapper.parseObjects(Class[])


Copyright ©2008 Jodd Team