r2169 - trunk/topia-persistence/src/main/java/org/nuiton/topia/framework
Author: tchemit Date: 2010-12-27 11:07:33 +0100 (Mon, 27 Dec 2010) New Revision: 2169 Url: http://nuiton.org/repositories/revision/topia/2169 Log: Evolution #1169: Introduce a TopiaSQLQuery Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java 2010-12-27 10:07:33 UTC (rev 2169) @@ -0,0 +1,114 @@ +package org.nuiton.topia.framework; + +import org.hibernate.jdbc.Work; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaRuntimeException; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * An executor of sql query which permits to obtain a single result via + * the method {@link #findSingleResult(TopiaContextImplementor)} + * or a multiple result with method {@link #findMultipleResult(TopiaContextImplementor)}. + * + * @param <O> the type of result data + * @since 2.5 + */ +public abstract class TopiaSQLQuery<O> { + + /** + * Prepare the statement used to do the sql query. + * + * @param connection jdbc connection to use + * @return the statement containing the query to execute + * @throws SQLException if any problem + */ + protected abstract PreparedStatement prepareQuery(Connection connection) throws SQLException; + + /** + * given a result set, extract the data. + * + * @param set the result set + * @return the data extracted from the current set, or {@code null} + * @throws SQLException if any prob + */ + protected abstract O prepareResult(ResultSet set) throws SQLException; + + /** + * Obtain a single result from the builded sql query. + * + * @param tx the transaction used to execute the query. + * @return the single result or {@code null} if none found. + * @throws TopiaException for any pb + */ + public O findSingleResult(TopiaContextImplementor tx) throws TopiaException { + final List<O> result = new ArrayList<O>(); + + tx.getHibernate().doWork(new Work() { + + @Override + public void execute(Connection connection) throws SQLException { + + PreparedStatement ps = prepareQuery(connection); + + try { + ResultSet set = ps.executeQuery(); + + if (set.next()) { + O singleResult = prepareResult(set); + if (singleResult != null) { + result.add(singleResult); + } + } + } catch (Exception e) { + throw new TopiaRuntimeException("Could not execute query", e); + } finally { + ps.close(); + } + } + }); + return result.isEmpty() ? null : result.get(0); + } + + /** + * Obtain a multiple results fro the builded sql query. + * + * @param tx the transaction used to execute the query. + * @return the list of results (the list is empty if non result is found). + * @throws TopiaException for any pb + */ + public List<O> findMultipleResult(TopiaContextImplementor tx) throws TopiaException { + final List<O> result = new ArrayList<O>(); + + tx.getHibernate().doWork(new Work() { + + @Override + public void execute(Connection connection) throws SQLException { + + PreparedStatement ps = prepareQuery(connection); + try { + ResultSet set = ps.executeQuery(); + + while (set.next()) { + O singleResult = prepareResult(set); + + if (singleResult != null) { + result.add(singleResult); + } + } + } catch (Exception e) { + throw new TopiaRuntimeException("Could not execute query", e); + } finally { + ps.close(); + } + } + }); + return result; + } + +} Property changes on: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSQLQuery.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native
participants (1)
-
tchemit@users.nuiton.org