org.nuiton.topia.framework
Class TopiaSQLQuery<O>

java.lang.Object
  extended by org.nuiton.topia.framework.TopiaSQLQuery<O>
Type Parameters:
O - the type of result data

public abstract class TopiaSQLQuery<O>
extends Object

An executor of sql query which permits to obtain a single result via the method findSingleResult(TopiaContextImplementor) or a multiple result with method findMultipleResult(TopiaContextImplementor).

Since:
2.5

Constructor Summary
TopiaSQLQuery()
           
 
Method Summary
protected  void afterExecuteQuery(ResultSet set)
          A hook to obtain the result set just after the query execute.
protected  void findMultipleResult(List<O> result, ResultSet set)
          Obtain a multi result given the result set and push in in the result list.
 List<O> findMultipleResult(TopiaContextImplementor tx)
          Obtain a multiple results fro the builded sql query.
protected  void findSingleResult(List<O> result, ResultSet set)
          Obtain a single result given the result set and push in in the result list.
 O findSingleResult(TopiaContextImplementor tx)
          Obtain a single result from the builded sql query.
protected  String[] getColumnNames(ResultSet set)
          Obtain the column names of a given result set using his metadata.
protected  int getNbRows(ResultSet set)
          From a given result set, let's count his number of row.
protected  Map<String,Object> getRowAsMap(String[] columnNames, ResultSet set)
          Given the column names of the result set, transform the row of the result set to a map with column name as key.
protected abstract  PreparedStatement prepareQuery(Connection connection)
          Prepare the statement used to do the sql query.
protected abstract  O prepareResult(ResultSet set)
          given a result set, extract the data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TopiaSQLQuery

public TopiaSQLQuery()
Method Detail

prepareQuery

protected abstract PreparedStatement prepareQuery(Connection connection)
                                           throws SQLException
Prepare the statement used to do the sql query.

Parameters:
connection - jdbc connection to use
Returns:
the statement containing the query to execute
Throws:
SQLException - if any problem

prepareResult

protected abstract O prepareResult(ResultSet set)
                            throws SQLException
given a result set, extract the data.

Parameters:
set - the result set
Returns:
the data extracted from the current set, or null
Throws:
SQLException - if any prob

findSingleResult

public O findSingleResult(TopiaContextImplementor tx)
                   throws TopiaException
Obtain a single result from the builded sql query.

Parameters:
tx - the transaction used to execute the query.
Returns:
the single result or null if none found.
Throws:
TopiaException - for any pb

findMultipleResult

public List<O> findMultipleResult(TopiaContextImplementor tx)
                           throws TopiaException
Obtain a multiple results fro the builded sql query.

Parameters:
tx - the transaction used to execute the query.
Returns:
the list of results (the list is empty if non result is found).
Throws:
TopiaException - for any pb

afterExecuteQuery

protected void afterExecuteQuery(ResultSet set)
                          throws SQLException
A hook to obtain the result set just after the query execute.

Parameters:
set - the result set just obtained
Throws:
SQLException - if any prob
Since:
2.6.4

findSingleResult

protected void findSingleResult(List<O> result,
                                ResultSet set)
                         throws SQLException
Obtain a single result given the result set and push in in the result list.

Parameters:
result - the result list
set - the set of the executed sql query
Throws:
SQLException - if any pb
Since:
2.6.4

findMultipleResult

protected void findMultipleResult(List<O> result,
                                  ResultSet set)
                           throws SQLException
Obtain a multi result given the result set and push in in the result list.

Parameters:
result - the result list
set - the set of the executed sql query
Throws:
SQLException - if any pb
Since:
2.6.4

getColumnNames

protected String[] getColumnNames(ResultSet set)
                           throws SQLException
Obtain the column names of a given result set using his metadata.

Parameters:
set - the result set to inspect
Returns:
the column names of the result set
Throws:
SQLException - if any pb
Since:
2.6.4

getNbRows

protected int getNbRows(ResultSet set)
                 throws SQLException
From a given result set, let's count his number of row.

Note: the result set must be scrollable to go back to before first row.

Parameters:
set - the result set to inspect
Returns:
the number of row of the given result set
Throws:
SQLException - if any pb
Since:
2.6.4

getRowAsMap

protected Map<String,Object> getRowAsMap(String[] columnNames,
                                         ResultSet set)
                                  throws SQLException
Given the column names of the result set, transform the row of the result set to a map with column name as key.

Parameters:
columnNames - column names of the result set
set - the set to inspect
Returns:
the map for the given row of the result set
Throws:
SQLException - if any pb
Since:
2.6.4


Copyright © 2004-2012 CodeLutin. All Rights Reserved.