Return-Path: Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: (qmail 29017 invoked from network); 29 Oct 2010 19:49:44 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 29 Oct 2010 19:49:44 -0000 Received: (qmail 5116 invoked by uid 500); 29 Oct 2010 19:49:44 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 5071 invoked by uid 500); 29 Oct 2010 19:49:44 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 5063 invoked by uid 500); 29 Oct 2010 19:49:43 -0000 Received: (qmail 5059 invoked by uid 99); 29 Oct 2010 19:49:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Oct 2010 19:49:43 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Oct 2010 19:49:42 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 77ACF23888EA; Fri, 29 Oct 2010 19:48:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1028884 - in /db/torque/torque4/branches/trunk-without-village: torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java torque-test/src/test/java/org/apache/torque/DataTest.java Date: Fri, 29 Oct 2010 19:48:46 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101029194846.77ACF23888EA@eris.apache.org> Author: tfischer Date: Fri Oct 29 19:48:46 2010 New Revision: 1028884 URL: http://svn.apache.org/viewvc?rev=1028884&view=rev Log: reactivate BasePeer.doPSSelect and re-add test for this method Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java?rev=1028884&r1=1028883&r2=1028884&view=diff ============================================================================== --- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java (original) +++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java Fri Oct 29 19:48:46 2010 @@ -1791,6 +1791,212 @@ public abstract class BasePeer } /** + * Performs a SQL select using a PreparedStatement. + * + * @param criteria A Criteria specifying the records to select, not null. + * @param mapper The mapper creating the objects from the resultSet, + * not null. + * @param defaultTableMap The table map used for the + * unqualified columns in the query, not null. + * @param connection the database connection for selecting records, + * not null. + * + * @return The results of the query, not null. + * @throws TorqueException Error performing database query. + */ + public static List doPSSelect( + Criteria criteria, + RecordMapper mapper, + TableMap defaultTableMap, + Connection connection) + throws TorqueException + { + correctBooleans(criteria, defaultTableMap); + + StringBuffer query = new StringBuffer(); + List params = new ArrayList(criteria.size()); + + createPreparedStatement(criteria, query, params); + + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + statement = connection.prepareStatement(query.toString()); + + for (int i = 0; i < params.size(); i++) + { + Object param = params.get(i); + if (param instanceof java.sql.Date) + { + statement.setDate(i + 1, (java.sql.Date) param); + } + else if (param instanceof NumberKey) + { + statement.setBigDecimal(i + 1, + ((NumberKey) param).getBigDecimal()); + } + else if (param instanceof Integer) + { + statement.setInt(i + 1, ((Integer) param).intValue()); + } + else + { + statement.setString(i + 1, param.toString()); + } + } + + long startTime = System.currentTimeMillis(); + log.debug("Executing query " + query + ", parameters = " + params); + + resultSet = statement.executeQuery(); + long queryEndTime = System.currentTimeMillis(); + log.trace("query took " + (queryEndTime - startTime) + + " milliseconds"); + + int offset; + Database database = Torque.getDatabase(criteria.getDbName()); + if (database.getAdapter().supportsNativeOffset()) + { + offset = 0; //database takes care of offset + } + else + { + offset = criteria.getOffset(); + } + + int limit; + if (database.getAdapter().supportsNativeLimit()) + { + limit = -1; //database takes care of offset + } + else + { + if (database.getAdapter().supportsNativeOffset()) + { + limit = criteria.getLimit(); + } + else + { + if (criteria.getLimit() == -1 ) + { + limit = criteria.getLimit(); + } + else + { + limit = offset + criteria.getLimit(); + } + } + } + + List result = new ArrayList(); + int rowNumber = 0; + while (resultSet.next()) + { + if (rowNumber < offset) + { + rowNumber++; + continue; + } + if (limit >= 0 && rowNumber >= limit) + { + break; + } + + T rowResult = mapper.processRow(resultSet, 0); + result.add(rowResult); + + rowNumber++; + } + long mappingEndTime = System.currentTimeMillis(); + log.trace("mapping took " + (mappingEndTime - queryEndTime) + + " milliseconds"); + + if (criteria.isSingleRecord() && result.size() > 1) + { + throw new TooManyRowsException( + "Criteria expected single Record and " + + "Multiple Records were selected"); + } + return result; + } + catch (SQLException e) + { + throw new TorqueException(e); + } + finally + { + if (resultSet != null) + { + try + { + resultSet.close(); + } + catch (SQLException e) + { + log.warn("error closing resultSet", e); + } + } + if (statement != null) + { + try + { + statement.close(); + } + catch (SQLException e) + { + log.warn("error closing statement", e); + } + } + } + } + + /** + * Do a Prepared Statement select according to the given criteria. + * + * @param criteria A Criteria specifying the records to select, not null. + * @param mapper The mapper creating the objects from the resultSet, + * not null. + * @param defaultTableMap The table map used for the + * unqualified columns in the query, not null. + * + * @return The results of the query, not null. + * + * @throws TorqueException if a database error occurs. + */ + public static List doPSSelect( + Criteria criteria, + RecordMapper mapper, + TableMap defaultTableMap) + throws TorqueException + { + Connection connection = null; + try + { + connection = Transaction.beginOptional( + criteria.getDbName(), + criteria.isUseTransaction()); + + List result = doPSSelect( + criteria, + mapper, + defaultTableMap, + connection); + + Transaction.commit(connection); + connection = null; + return result; + } + finally + { + if (connection != null) + { + Transaction.safeRollback(connection); + } + } + } + + /** * Create a new PreparedStatement. It builds a string representation * of a query and a list of PreparedStatement parameters. * @@ -1806,8 +2012,11 @@ public abstract class BasePeer List params) throws TorqueException { - Query query = SQLBuilder.buildQueryClause(criteria, params, new SQLBuilder.QueryCallback() { - public String process(Criteria.Criterion criterion, List params) + Query query = SQLBuilder.buildQueryClause( + criteria, params, new SQLBuilder.QueryCallback() { + public String process( + Criteria.Criterion criterion, + List params) { StringBuffer sb = new StringBuffer(); criterion.appendPsTo(sb, params); Modified: db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1028884&r1=1028883&r2=1028884&view=diff ============================================================================== --- db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java (original) +++ db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java Fri Oct 29 19:48:46 2010 @@ -1907,6 +1907,41 @@ public class DataTest extends BaseRuntim /** + * Test whether we can execute queries as prepared statements + * @throws Exception + */ + public void testPreparedStatements() throws Exception + { + // clean LargePk table + Criteria criteria = new Criteria(); + criteria.add( + LargePkPeer.LARGE_PK_ID, + (Long) null, + Criteria.NOT_EQUAL); + LargePkPeer.doDelete(criteria); + + LargePk largePk = new LargePk(); + largePk.setLargePkId(1); + largePk.setName("testLargePk"); + largePk.save(); + + largePk = new LargePk(); + largePk.setLargePkId(2); + largePk.setName("testLargePk"); + largePk.save(); + + criteria = new Criteria(); + criteria.add(LargePkPeer.LARGE_PK_ID, 2, Criteria.LESS_THAN); + LargePkPeer.addSelectColumns(criteria); + List result = BasePeer.doPSSelect( + criteria, new LargePkPeer.LargePkRecordMapper(), + LargePkPeer.getTableMap()); + assertTrue("Size of largePk list should be 1 but is " + + result.size(), + result.size() == 1); + } + + /** * Test whether equals() is working correctly * @throws Exception */ --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org For additional commands, e-mail: torque-dev-help@db.apache.org