You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2013/05/19 11:58:01 UTC
svn commit: r1484252 [2/3] - in /db/torque/torque4/trunk/torque-runtime/src:
main/java/org/apache/torque/ main/java/org/apache/torque/avalon/
main/java/org/apache/torque/criteria/ main/java/org/apache/torque/map/
main/java/org/apache/torque/om/mapper/ ...
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java?rev=1484252&r1=1484251&r2=1484252&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java Sun May 19 09:58:00 2013
@@ -33,7 +33,6 @@ import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.torque.adapter.Adapter;
import org.apache.torque.criteria.Criteria;
-import org.apache.torque.criteria.CriteriaInterface;
import org.apache.torque.criteria.Criterion;
import org.apache.torque.criteria.FromElement;
import org.apache.torque.criteria.PreparedStatementPart;
@@ -42,9 +41,7 @@ import org.apache.torque.map.ColumnMap;
import org.apache.torque.map.DatabaseMap;
import org.apache.torque.map.MapHelper;
import org.apache.torque.map.TableMap;
-import org.apache.torque.om.ObjectKey;
import org.apache.torque.sql.whereclausebuilder.CurrentDateTimePsPartBuilder;
-import org.apache.torque.sql.whereclausebuilder.CustomBuilder;
import org.apache.torque.sql.whereclausebuilder.InBuilder;
import org.apache.torque.sql.whereclausebuilder.LikeBuilder;
import org.apache.torque.sql.whereclausebuilder.NullValueBuilder;
@@ -63,7 +60,6 @@ import org.apache.torque.util.UniqueList
* @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
* @version $Id$
*/
-@SuppressWarnings("deprecation")
public final class SqlBuilder
{
/** Logging */
@@ -85,7 +81,6 @@ public final class SqlBuilder
static
{
whereClausePsPartBuilders.add(new VerbatimSqlConditionBuilder());
- whereClausePsPartBuilders.add(new CustomBuilder());
whereClausePsPartBuilders.add(new CurrentDateTimePsPartBuilder());
whereClausePsPartBuilders.add(new NullValueBuilder());
whereClausePsPartBuilders.add(new LikeBuilder());
@@ -124,36 +119,6 @@ public final class SqlBuilder
* @return the corresponding query to the criteria.
*
* @exception TorqueException if an error occurs
- * @deprecated please use org.apache.torque.criteria.Criteria
- * instead of org.apache.torque.util.Criteria.
- */
- @Deprecated
- public static Query buildQuery(final org.apache.torque.util.Criteria crit)
- throws TorqueException
- {
- Query sqlStatement = new Query();
-
- JoinBuilder.processJoins(crit, sqlStatement);
- processModifiers(crit, sqlStatement);
- processSelectColumns(crit, sqlStatement);
- processAsColumns(crit, sqlStatement);
- processCriterions(crit, sqlStatement);
- processGroupBy(crit, sqlStatement);
- processHaving(crit, sqlStatement);
- processOrderBy(crit, sqlStatement);
- processLimits(crit, sqlStatement);
-
- return sqlStatement;
- }
-
- /**
- * Builds a Query from a criteria.
- *
- * @param crit the criteria to build the query from, not null.
- *
- * @return the corresponding query to the criteria.
- *
- * @exception TorqueException if an error occurs
*/
public static Query buildQuery(final Criteria crit)
throws TorqueException
@@ -185,7 +150,7 @@ public final class SqlBuilder
* @throws TorqueException if the select columns can not be processed.
*/
private static void processSelectColumns(
- final CriteriaInterface<?> criteria,
+ final Criteria criteria,
final Query query)
throws TorqueException
{
@@ -221,7 +186,7 @@ public final class SqlBuilder
* @throws TorqueException if the as columns can not be processed.
*/
private static void processAsColumns(
- final CriteriaInterface<?> criteria,
+ final Criteria criteria,
final Query query)
throws TorqueException
{
@@ -251,7 +216,7 @@ public final class SqlBuilder
* not null.
*/
private static void processModifiers(
- final CriteriaInterface<?> criteria,
+ final Criteria criteria,
final Query query)
{
UniqueList<String> selectModifiers = query.getSelectModifiers();
@@ -289,10 +254,10 @@ public final class SqlBuilder
}
static void appendCriterion(
- Criterion criterion,
- CriteriaInterface<?> criteria,
- StringBuilder where,
- Query query)
+ final Criterion criterion,
+ final Criteria criteria,
+ final StringBuilder where,
+ final Query query)
throws TorqueException
{
if (criterion.isComposite())
@@ -337,8 +302,8 @@ public final class SqlBuilder
}
static PreparedStatementPart processCriterion(
- Criterion criterion,
- CriteriaInterface<?> criteria)
+ final Criterion criterion,
+ final Criteria criteria)
throws TorqueException
{
final String dbName = criteria.getDbName();
@@ -379,130 +344,13 @@ public final class SqlBuilder
}
/**
- * Adds the Criterions from the criteria to the query.
- *
- * @param criteria the criteria from which the Criterion-objects are taken
- * @param query the query to which the Criterion-objects should be added.
- *
- * @throws TorqueException if the Criterion-objects can not be processed
- * @deprecated please use org.apache.torque.criteria.Criteria
- * instead of org.apache.torque.util.Criteria.
- */
- @Deprecated
- private static void processCriterions(
- final org.apache.torque.util.Criteria criteria,
- final Query query)
- throws TorqueException
- {
- UniqueList<String> whereClause = query.getWhereClause();
-
- for (org.apache.torque.util.Criteria.Criterion criterion
- : criteria.values())
- {
- StringBuilder sb = new StringBuilder();
- appendCriterionToPs(
- criterion,
- criteria,
- sb,
- query);
- whereClause.add(sb.toString());
- }
- }
-
- /**
- * @deprecated please use org.apache.torque.criteria.Criteria
- * instead of org.apache.torque.util.Criteria.
- */
- @Deprecated
- private static void appendCriterionToPs(
- org.apache.torque.util.Criteria.Criterion criterion,
- org.apache.torque.util.Criteria criteria,
- StringBuilder sb,
- Query query)
- throws TorqueException
- {
- Column column = criterion.getColumn();
-
- // add the table to the from clause, if it is not already
- // contained there
- // it is important that this piece of code is executed AFTER
- // the joins are processed
- addTableToFromClause(
- column,
- criteria,
- query);
-
- boolean ignoreCase
- = criteria.isIgnoreCase() || criterion.isIgnoreCase();
- final String dbName = criteria.getDbName();
- final Adapter adapter = Torque.getAdapter(dbName);
- final Database database = Torque.getDatabase(dbName);
- {
- Column databaseColumn = resolveAliasAndAsColumnAndSchema(
- column,
- criteria);
- ColumnMap columnMap = null;
- {
- DatabaseMap databaseMap = database.getDatabaseMap();
- TableMap tableMap = databaseMap.getTable(
- databaseColumn.getTableName());
- if (tableMap != null)
- {
- columnMap = tableMap.getColumn(
- databaseColumn.getColumnName());
- }
- }
- if (columnMap != null)
- {
- // do not use ignoreCase on columns
- // which do not contain String values
- ignoreCase = ignoreCase
- && columnMap.getType() instanceof String;
- }
- }
-
- for (int j = 0; j < criterion.getClauses().size(); j++)
- {
- sb.append('(');
- }
- String columnName = criterion.getColumn().getSqlExpression();
- WhereClauseExpression whereClausePartInput
- = new WhereClauseExpression(
- columnName,
- criterion.getComparison(),
- criterion.getValue(),
- null,
- null);
- PreparedStatementPart whereClausePartOutput
- = buildPs(
- whereClausePartInput,
- ignoreCase,
- adapter);
- sb.append(whereClausePartOutput.getSql());
- query.getWhereClausePreparedStatementReplacements().addAll(
- whereClausePartOutput.getPreparedStatementReplacements());
-
- for (int i = 0; i < criterion.getClauses().size(); i++)
- {
- sb.append(criterion.getConjunctions().get(i));
- org.apache.torque.util.Criteria.Criterion clause
- = criterion.getClauses().get(i);
- appendCriterionToPs(
- clause,
- criteria,
- sb,
- query);
- sb.append(')');
- }
- }
- /**
* adds the OrderBy-Columns from the criteria to the query
* @param criteria the criteria from which the OrderBy-Columns are taken
* @param query the query to which the OrderBy-Columns should be added
* @throws TorqueException if the OrderBy-Columns can not be processed
*/
private static void processOrderBy(
- final CriteriaInterface<?> crit,
+ final Criteria crit,
final Query query)
throws TorqueException
{
@@ -571,7 +419,7 @@ public final class SqlBuilder
* @throws TorqueException if the GroupBy-Columns can not be processed
*/
private static void processGroupBy(
- final CriteriaInterface<?> criteria,
+ final Criteria criteria,
final Query query)
throws TorqueException
{
@@ -614,24 +462,6 @@ public final class SqlBuilder
}
/**
- * adds the Having-Columns from the criteria to the query
- * @param criteria the criteria from which the Having-Columns are taken
- * @param query the query to which the Having-Columns should be added
- * @throws TorqueException if the Having-Columns can not be processed
- */
- private static void processHaving(
- final org.apache.torque.util.Criteria crit,
- final Query query)
- throws TorqueException
- {
- org.apache.torque.util.Criteria.Criterion having = crit.getHaving();
- if (having != null)
- {
- query.setHaving(having.toString());
- }
- }
-
- /**
* Adds a Limit clause to the query if supported by the database.
*
* @param criteria the criteria from which the Limit and Offset values
@@ -641,7 +471,7 @@ public final class SqlBuilder
* @throws TorqueException if the Database adapter cannot be obtained
*/
private static void processLimits(
- final CriteriaInterface<?> crit,
+ final Criteria crit,
final Query query)
throws TorqueException
{
@@ -715,7 +545,7 @@ public final class SqlBuilder
*/
static PreparedStatementPart getExpressionForFromClause(
final Object toAddToFromClause,
- final CriteriaInterface<?> criteria)
+ final Criteria criteria)
throws TorqueException
{
if (!(toAddToFromClause instanceof Column))
@@ -854,7 +684,7 @@ public final class SqlBuilder
*
* @throws TorqueException if the table name cannot be determined.
*/
- public static String guessFullTableFromCriteria(Criteria criteria)
+ public static String guessFullTableFromCriteria(final Criteria criteria)
throws TorqueException
{
org.apache.torque.criteria.Criterion criterion
@@ -894,7 +724,7 @@ public final class SqlBuilder
*
* @throws TorqueException if the database or table is unknown.
*/
- public static TableMap getTableMap(String tableName, String dbName)
+ public static TableMap getTableMap(final String tableName, String dbName)
throws TorqueException
{
@@ -933,7 +763,7 @@ public final class SqlBuilder
*/
static Column resolveAliasAndAsColumnAndSchema(
final Column columnToResolve,
- final CriteriaInterface<?> criteria)
+ final Criteria criteria)
throws TorqueException
{
String columnNameToResolve = columnToResolve.getColumnName();
@@ -1030,8 +860,8 @@ public final class SqlBuilder
*/
static void addTableToFromClause(
final Object possibleColumn,
- final CriteriaInterface<?> criteria,
- Query query)
+ final Criteria criteria,
+ final Query query)
throws TorqueException
{
if (possibleColumn == null)
@@ -1082,9 +912,9 @@ public final class SqlBuilder
* @throws TorqueException in the case of an error.
*/
static boolean isIgnoreCase(
- Criterion criterion,
- CriteriaInterface<?> criteria,
- Database database)
+ final Criterion criterion,
+ final Criteria criteria,
+ final Database database)
throws TorqueException
{
boolean ignoreCase
@@ -1116,9 +946,9 @@ public final class SqlBuilder
* @throws TorqueException in the case of an error.
*/
private static boolean ignoreCaseApplicable(
- Object value,
- CriteriaInterface<?> criteria,
- Database database)
+ final Object value,
+ final Criteria criteria,
+ final Database database)
throws TorqueException
{
if (value == null)
@@ -1158,428 +988,4 @@ public final class SqlBuilder
// which do not contain String values
return columnMap.getType() instanceof String;
}
-
- /**
- * Builds an element of the where clause of a prepared statement.
- *
- * @param whereClausePart the part of the where clause to build.
- * Can be modified during this method.
- * @param ignoreCase If true and columns represent Strings, the appropriate
- * function defined for the database will be used to ignore
- * differences in case.
- * @param adapter The adapter for the database for which the SQL
- * should be created, not null.
- *
- * @deprecated remove when util.Criteria is removed
- */
- @Deprecated
- private static PreparedStatementPart buildPs(
- WhereClauseExpression whereClausePart,
- boolean ignoreCase,
- Adapter adapter)
- throws TorqueException
- {
- PreparedStatementPart result = new PreparedStatementPart();
-
- // Handle SqlEnum.Custom
- if (SqlEnum.CUSTOM == whereClausePart.getOperator())
- {
- result.getSql().append(whereClausePart.getRValue());
- return result;
- }
-
- // Handle SqlEnum.CURRENT_DATE and SqlEnum.CURRENT_TIME
- if (whereClausePart.getRValue() instanceof SqlEnum)
- {
- result.getSql().append(whereClausePart.getLValue())
- .append(whereClausePart.getOperator())
- .append(whereClausePart.getRValue());
- return result;
- }
-
- // If rValue is an ObjectKey, take the value of that ObjectKey.
- if (whereClausePart.getRValue() instanceof ObjectKey)
- {
- whereClausePart.setRValue(
- ((ObjectKey) whereClausePart.getRValue()).getValue());
- }
-
- /* If rValue is null, check to see if the operator
- * is an =, <>, or !=. If so, replace the comparison
- * with SqlEnum.ISNULL or SqlEnum.ISNOTNULL.
- */
- if (whereClausePart.getRValue() == null)
- {
- if (whereClausePart.getOperator().equals(SqlEnum.EQUAL))
- {
- result.getSql().append(whereClausePart.getLValue())
- .append(SqlEnum.ISNULL);
- return result;
- }
- if (whereClausePart.getOperator().equals(SqlEnum.NOT_EQUAL)
- || whereClausePart.getOperator().equals(
- SqlEnum.ALT_NOT_EQUAL))
- {
- result.getSql().append(whereClausePart.getLValue())
- .append(SqlEnum.ISNOTNULL);
- return result;
- }
- }
-
- // Handle SqlEnum.ISNULL and SqlEnum.ISNOTNULL
- if (whereClausePart.getOperator().equals(SqlEnum.ISNULL)
- || whereClausePart.getOperator().equals(SqlEnum.ISNOTNULL))
- {
- result.getSql().append(whereClausePart.getLValue())
- .append(whereClausePart.getOperator());
- return result;
- }
-
- // handle Subqueries
- if (whereClausePart.getRValue() instanceof Criteria)
- {
- Query subquery = SqlBuilder.buildQuery(
- (Criteria) whereClausePart.getRValue());
- result.getPreparedStatementReplacements().addAll(
- subquery.getPreparedStatementReplacements());
- result.getSql().append(whereClausePart.getLValue())
- .append(whereClausePart.getOperator())
- .append("(").append(subquery.toString()).append(")");
- return result;
- }
- if (whereClausePart.getRValue()
- instanceof org.apache.torque.util.Criteria)
- {
- Query subquery = SqlBuilder.buildQuery(
- (org.apache.torque.util.Criteria)
- whereClausePart.getRValue());
- result.getPreparedStatementReplacements().addAll(
- subquery.getPreparedStatementReplacements());
- result.getSql().append(whereClausePart.getLValue())
- .append(whereClausePart.getOperator())
- .append("(").append(subquery.toString()).append(")");
- return result;
- }
-
- // handle LIKE and similar
- if (whereClausePart.getOperator().equals(Criteria.LIKE)
- || whereClausePart.getOperator().equals(Criteria.NOT_LIKE)
- || whereClausePart.getOperator().equals(Criteria.ILIKE)
- || whereClausePart.getOperator().equals(Criteria.NOT_ILIKE))
- {
- return buildPsLike(whereClausePart, ignoreCase, adapter);
- }
-
- // handle IN and similar
- if (whereClausePart.getOperator().equals(Criteria.IN)
- || whereClausePart.getOperator().equals(Criteria.NOT_IN))
- {
- return buildPsIn(whereClausePart, ignoreCase, adapter);
- }
-
- // Standard case
- result.getPreparedStatementReplacements().add(
- whereClausePart.getRValue());
- if (ignoreCase
- && whereClausePart.getRValue() instanceof String)
- {
- result.getSql().append(
- adapter.ignoreCase((String) whereClausePart.getLValue()))
- .append(whereClausePart.getOperator())
- .append(adapter.ignoreCase("?"));
- }
- else
- {
- result.getSql().append(whereClausePart.getLValue())
- .append(whereClausePart.getOperator())
- .append("?");
- }
- return result;
- }
-
- /**
- * Takes a WhereClauseExpression with a LIKE operator
- * and builds an SQL phrase based on whether wildcards are present
- * and the state of theignoreCase flag.
- * Multicharacter wildcards % and * may be used
- * as well as single character wildcards, _ and ?. These
- * characters can be escaped with \.
- *
- * e.g. criteria = "fre%" -> columnName LIKE 'fre%'
- * -> UPPER(columnName) LIKE UPPER('fre%')
- * criteria = "50\%" -> columnName = '50%'
- *
- * @param whereClausePart the part of the where clause to build.
- * Can be modified in this method.
- * @param ignoreCase If true and columns represent Strings, the appropriate
- * function defined for the database will be used to ignore
- * differences in case.
- * @param adapter The adapter for the database for which the SQL
- * should be created, not null.
- *
- * @return the rendered SQL for the WhereClauseExpression
- *
- * @deprecated remove when util.Criteria is removed
- */
- @Deprecated
- static PreparedStatementPart buildPsLike(
- WhereClauseExpression whereClausePart,
- boolean ignoreCase,
- Adapter adapter)
- throws TorqueException
- {
- if (!(whereClausePart.getRValue() instanceof String))
- {
- throw new TorqueException(
- "rValue must be a String for the operator "
- + whereClausePart.getOperator());
- }
- String value = (String) whereClausePart.getRValue();
- // If selection criteria contains wildcards use LIKE otherwise
- // use = (equals). Wildcards can be escaped by prepending
- // them with \ (backslash). However, if we switch from
- // like to equals, we need to remove the escape characters.
- // from the wildcards.
- // So we need two passes: The first replaces * and ? by % and _,
- // and checks whether we switch to equals,
- // the second removes escapes if we have switched to equals.
- int position = 0;
- StringBuffer sb = new StringBuffer();
- boolean replaceWithEquals = true;
- while (position < value.length())
- {
- char checkWildcard = value.charAt(position);
-
- switch (checkWildcard)
- {
- case BACKSLASH:
- if (position + 1 >= value.length())
- {
- // ignore backslashes at end
- break;
- }
- position++;
- char escapedChar = value.charAt(position);
- if (escapedChar != '*' && escapedChar != '?')
- {
- sb.append(checkWildcard);
- }
- // code below copies escaped character into sb
- checkWildcard = escapedChar;
- break;
- case '%':
- case '_':
- replaceWithEquals = false;
- break;
- case '*':
- replaceWithEquals = false;
- checkWildcard = '%';
- break;
- case '?':
- replaceWithEquals = false;
- checkWildcard = '_';
- break;
- default:
- break;
- }
-
- sb.append(checkWildcard);
- position++;
- }
- value = sb.toString();
-
- if (ignoreCase)
- {
- if (adapter.useIlike() && !replaceWithEquals)
- {
- if (SqlEnum.LIKE.equals(whereClausePart.getOperator()))
- {
- whereClausePart.setOperator(SqlEnum.ILIKE);
- }
- else if (SqlEnum.NOT_LIKE.equals(whereClausePart.getOperator()))
- {
- whereClausePart.setOperator(SqlEnum.NOT_ILIKE);
- }
- }
- else
- {
- // no native case insensitive like is offered by the DB,
- // or the LIKE was replaced with equals.
- // need to ignore case manually.
- whereClausePart.setLValue(
- adapter.ignoreCase((String) whereClausePart.getLValue()));
- }
- }
-
- PreparedStatementPart result = new PreparedStatementPart();
- result.getSql().append(whereClausePart.getLValue());
-
- if (replaceWithEquals)
- {
- if (whereClausePart.getOperator().equals(SqlEnum.NOT_LIKE)
- || whereClausePart.getOperator().equals(SqlEnum.NOT_ILIKE))
- {
- result.getSql().append(SqlEnum.NOT_EQUAL);
- }
- else
- {
- result.getSql().append(SqlEnum.EQUAL);
- }
-
- // remove escape backslashes from String
- position = 0;
- sb = new StringBuffer();
- while (position < value.length())
- {
- char checkWildcard = value.charAt(position);
-
- if (checkWildcard == BACKSLASH
- && position + 1 < value.length())
- {
- position++;
- // code below copies escaped character into sb
- checkWildcard = value.charAt(position);
- }
- sb.append(checkWildcard);
- position++;
- }
- value = sb.toString();
- }
- else
- {
- result.getSql().append(whereClausePart.getOperator());
- }
-
- String rValueSql = "?";
- // handle ignoreCase if necessary
- if (ignoreCase && (!(adapter.useIlike()) || replaceWithEquals))
- {
- rValueSql = adapter.ignoreCase(rValueSql);
- }
- // handle escape clause if necessary
- if (!replaceWithEquals && adapter.useEscapeClauseForLike())
- {
- rValueSql = rValueSql + SqlEnum.ESCAPE + "'\\'";
- }
-
- result.getPreparedStatementReplacements().add(value);
- result.getSql().append(rValueSql);
- return result;
- }
-
- /**
- * Takes a columnName and criteria and
- * builds a SQL 'IN' expression taking into account the ignoreCase
- * flag.
- *
- * @param whereClausePart the part of the where clause to build.
- * Can be modified in this method.
- * @param ignoreCase If true and columns represent Strings, the appropriate
- * function defined for the database will be used to ignore
- * differences in case.
- * @param adapter The adapter for the database for which the SQL
- * should be created, not null.
- *
- * @return the built part.
- *
- * @deprecated remove when util.Criteria is removed
- */
- @Deprecated
- static PreparedStatementPart buildPsIn(
- WhereClauseExpression whereClausePart,
- boolean ignoreCase,
- Adapter adapter)
- {
- PreparedStatementPart result = new PreparedStatementPart();
-
- boolean ignoreCaseApplied = false;
- List<String> inClause = new ArrayList<String>();
- boolean nullContained = false;
- if (whereClausePart.getRValue() instanceof Iterable)
- {
- for (Object listValue : (Iterable<?>) whereClausePart.getRValue())
- {
- if (listValue == null)
- {
- nullContained = true;
- continue;
- }
- result.getPreparedStatementReplacements().add(listValue);
- if (ignoreCase && listValue instanceof String)
- {
- inClause.add(adapter.ignoreCase("?"));
- ignoreCaseApplied = true;
- }
- else
- {
- inClause.add("?");
- }
- }
- }
- else if (whereClausePart.getRValue().getClass().isArray())
- {
- for (Object arrayValue : (Object[]) whereClausePart.getRValue())
- {
- if (arrayValue == null)
- {
- nullContained = true;
- continue;
- }
- result.getPreparedStatementReplacements().add(arrayValue);
- if (ignoreCase && arrayValue instanceof String)
- {
- inClause.add(adapter.ignoreCase("?"));
- ignoreCaseApplied = true;
- }
- else
- {
- inClause.add("?");
- }
- }
- }
- else
- {
- throw new IllegalArgumentException(
- "Unknown rValue type "
- + whereClausePart.getRValue().getClass().getName()
- + ". rValue must be an instance of "
- + " Iterable or Array");
- }
-
- if (nullContained)
- {
- result.getSql().append('(');
- }
-
- if (ignoreCaseApplied)
- {
- result.getSql().append(
- adapter.ignoreCase((String) whereClausePart.getLValue()));
- }
- else
- {
- result.getSql().append(whereClausePart.getLValue());
- }
-
- result.getSql().append(whereClausePart.getOperator())
- .append('(')
- .append(StringUtils.join(inClause.iterator(), ","))
- .append(')');
- if (nullContained)
- {
- if (whereClausePart.getOperator() == SqlEnum.IN)
- {
- result.getSql().append(Criterion.OR)
- .append(whereClausePart.getLValue()).append(SqlEnum.ISNULL);
- }
- else if (whereClausePart.getOperator() == SqlEnum.NOT_IN)
- {
- result.getSql().append(Criterion.AND)
- .append(whereClausePart.getLValue()).append(
- SqlEnum.ISNOTNULL);
- }
- result.getSql().append(')');
- }
- return result;
- }
}
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/objectbuilder/ObjectOrColumnPsPartBuilder.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/objectbuilder/ObjectOrColumnPsPartBuilder.java?rev=1484252&r1=1484251&r2=1484252&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/objectbuilder/ObjectOrColumnPsPartBuilder.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/objectbuilder/ObjectOrColumnPsPartBuilder.java Sun May 19 09:58:00 2013
@@ -52,8 +52,8 @@ public class ObjectOrColumnPsPartBuilder
*/
public PreparedStatementPart buildPs(
Object toBuildFrom,
- boolean ignoreCase,
- Adapter adapter)
+ final boolean ignoreCase,
+ final Adapter adapter)
throws TorqueException
{
PreparedStatementPart result = new PreparedStatementPart();
@@ -84,16 +84,6 @@ public class ObjectOrColumnPsPartBuilder
return result;
}
- if (toBuildFrom instanceof org.apache.torque.util.Criteria)
- {
- Query subquery = SqlBuilder.buildQuery(
- (org.apache.torque.util.Criteria) toBuildFrom);
- result.getPreparedStatementReplacements().addAll(
- subquery.getPreparedStatementReplacements());
- result.getSql().append("(").append(subquery.toString()).append(")");
- return result;
- }
-
if (toBuildFrom.equals(
SqlEnum.CURRENT_DATE)
|| toBuildFrom.equals(
Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java?rev=1484252&r1=1484251&r2=1484252&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java Sun May 19 09:58:00 2013
@@ -41,6 +41,8 @@ import org.apache.torque.Torque;
import org.apache.torque.TorqueException;
import org.apache.torque.adapter.Adapter;
import org.apache.torque.adapter.IDMethod;
+import org.apache.torque.criteria.Criteria;
+import org.apache.torque.criteria.Criterion;
import org.apache.torque.criteria.FromElement;
import org.apache.torque.map.ColumnMap;
import org.apache.torque.map.MapHelper;
@@ -107,7 +109,7 @@ public class BasePeerImpl<T> implements
* @param tableMap the default table map
* @param databaseName the name of the database
*/
- public BasePeerImpl(RecordMapper<T> recordMapper, TableMap tableMap, String databaseName)
+ public BasePeerImpl(final RecordMapper<T> recordMapper, final TableMap tableMap, final String databaseName)
{
this();
setRecordMapper(recordMapper);
@@ -120,7 +122,7 @@ public class BasePeerImpl<T> implements
*
* @param recordMapper the recordMapper to set
*/
- public void setRecordMapper(RecordMapper<T> recordMapper)
+ public void setRecordMapper(final RecordMapper<T> recordMapper)
{
this.recordMapper = recordMapper;
}
@@ -145,7 +147,7 @@ public class BasePeerImpl<T> implements
*
* @param tableMap the tableMap to set
*/
- public void setTableMap(TableMap tableMap)
+ public void setTableMap(final TableMap tableMap)
{
this.tableMap = tableMap;
}
@@ -170,7 +172,7 @@ public class BasePeerImpl<T> implements
*
* @param databaseName the databaseName to set
*/
- public void setDatabaseName(String databaseName)
+ public void setDatabaseName(final String databaseName)
{
this.databaseName = databaseName;
}
@@ -191,132 +193,6 @@ public class BasePeerImpl<T> implements
}
/**
- * Convenience method to create a String array of criteria keys.
- *
- * @param tableName Name of table.
- * @param columnNames A String[].
- * @return A String[].
- *
- * @deprecated This method is not used any more and will be removed in a
- * future version of Torque.
- */
- @Deprecated
- public String[] initCriteriaKeys(
- String tableName,
- String[] columnNames)
- {
- String[] keys = new String[columnNames.length];
- for (int i = 0; i < columnNames.length; i++)
- {
- keys[i] = tableName + "." + columnNames[i].toUpperCase();
- }
- return keys;
- }
-
- /**
- * Convenience method that uses straight JDBC to delete multiple
- * rows.
- *
- * @param con A Connection.
- * @param table The table to delete records from.
- * @param column The column in the where clause.
- * @param value The value of the column.
- *
- * @return the number of deleted rows.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- *
- * @deprecated The value is not SQL escaped.
- * Better use doDelete(Criteria, String, Connection)
- * for automatic escaping and more flexibility.
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public int deleteAll(
- Connection con,
- String table,
- String column,
- int value)
- throws TorqueException
- {
- Statement statement = null;
- try
- {
- statement = con.createStatement();
-
- StringBuffer query = new StringBuffer();
- query.append("DELETE FROM ")
- .append(table)
- .append(" WHERE ")
- .append(column)
- .append(" = ")
- .append(value);
-
- return statement.executeUpdate(query.toString());
- }
- catch (SQLException e)
- {
- throw new TorqueException(e);
- }
- finally
- {
- if (statement != null)
- {
- try
- {
- statement.close();
- }
- catch (SQLException e)
- {
- throw new TorqueException(e);
- }
- }
- }
- }
-
- /**
- * Convenience method that uses straight JDBC to delete multiple
- * rows. This method attempts to get the default database from
- * the pool.
- *
- * @param table The table to delete records from.
- * @param column The column in the where clause.
- * @param value The value of the column.
- *
- * @return the number of deleted rows.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- *
- * @deprecated The value is not SQL escaped.
- * Better use doDelete(Criteria, String)
- * for automatic escaping and more flexibility.
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public int deleteAll(String table, String column, int value)
- throws TorqueException
- {
- Connection con = null;
- try
- {
- con = Transaction.begin(Torque.getDefaultDB());
- int result = deleteAll(con, table, column, value);
- Transaction.commit(con);
- con = null;
- return result;
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
- }
-
- /**
* Deletes rows from a database table.
*
* @param criteria defines the rows to be deleted, not null.
@@ -326,7 +202,7 @@ public class BasePeerImpl<T> implements
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public int doDelete(org.apache.torque.criteria.Criteria criteria)
+ public int doDelete(final Criteria criteria)
throws TorqueException
{
Connection connection = null;
@@ -361,8 +237,8 @@ public class BasePeerImpl<T> implements
* rethrown wrapped into a TorqueException.
*/
public int doDelete(
- org.apache.torque.criteria.Criteria criteria,
- Connection connection)
+ final Criteria criteria,
+ final Connection connection)
throws TorqueException
{
correctBooleans(criteria);
@@ -442,154 +318,6 @@ public class BasePeerImpl<T> implements
}
/**
- * Method to perform deletes based on conditions in a Criteria.
- *
- * @param criteria The criteria to use.
- *
- * @return the number of deleted rows.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- *
- * @deprecated This method causes unexpected results when joins are used.
- * Please use doDelete(
- * org.apache.torque.criteria.Criteria, TableMap).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- protected int doDelete(Criteria criteria) throws TorqueException
- {
- Connection con = null;
- try
- {
- con = Transaction.begin(criteria.getDbName());
- int result = doDelete(criteria, con);
- Transaction.commit(con);
- con = null;
- return result;
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
- }
-
- /**
- * Method to perform deletes based on conditions a Criteria.
- *
- * @param criteria The criteria to use.
- * @param con the Connection to be used for deleting.
- *
- * @return the number of deleted rows.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- *
- * @deprecated This method causes unexpected results when joins are used.
- * Please use doDelete(
- * org.apache.torque.criteria.Criteria, TableMap, Connection).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- protected int doDelete(Criteria criteria, Connection con)
- throws TorqueException
- {
- if (criteria.values().isEmpty())
- {
- throw new TorqueException("No conditions found in Criteria");
- }
- Criteria.Criterion criterion
- = criteria.values().iterator().next();
-
- TableMap tableMapFromCriteria = MapHelper.getTableMap(
- criterion.getColumn(), criteria, null);
- if (tableMapFromCriteria == null)
- {
- throw new TorqueException("Unqualified column name in criteria"
- + " or table name not found in database map");
- }
-
- Query query = SqlBuilder.buildQuery(criteria);
- query.setType(Query.Type.DELETE);
-
- String fullTableName = null;
- if (tableMap != null)
- {
- fullTableName = SqlBuilder.getFullTableName(
- tableMap.getFullyQualifiedTableName(),
- criteria.getDbName());
- }
- else
- {
- Column column = criteria.values().iterator().next().getColumn();
- fullTableName = SqlBuilder.getFullTableName(
- column.getFullTableName(),
- criteria.getDbName());
- }
-
- boolean ownTableAdded = false;
- for (FromElement fromElement : query.getFromClause())
- {
- // Table names are case insensitive in known databases
- // so use case-insensitive compare
- if (fullTableName.equalsIgnoreCase(fromElement.getFromExpression()))
- {
- ownTableAdded = true;
- break;
- }
- }
- if (!ownTableAdded)
- {
- query.getFromClause().add(new FromElement(fullTableName));
- }
- String sql = query.toString();
-
- PreparedStatement preparedStatement = null;
- try
- {
- preparedStatement = con.prepareStatement(sql);
- List<Object> replacements = setPreparedStatementReplacements(
- preparedStatement,
- query.getPreparedStatementReplacements(),
- 0);
- long startTime = System.currentTimeMillis();
- log.debug("Executing delete " + sql
- + ", parameters = "
- + replacements);
-
- int affectedRows = preparedStatement.executeUpdate();
- long queryEndTime = System.currentTimeMillis();
- log.trace("delete took " + (queryEndTime - startTime)
- + " milliseconds");
-
- preparedStatement.close();
- preparedStatement = null;
- return affectedRows;
- }
- catch (SQLException e)
- {
- throw ExceptionMapper.getInstance().toTorqueException(e);
- }
- finally
- {
- if (preparedStatement != null)
- {
- try
- {
- preparedStatement.close();
- }
- catch (SQLException e)
- {
- log.warn("error closing prepared statement", e);
- }
- }
- }
- }
-
- /**
* Inserts a record into a database table.
* <p>
* If the primary key is included in Criteria, then that value will
@@ -610,7 +338,7 @@ public class BasePeerImpl<T> implements
*
* @throws TorqueException if a database error occurs.
*/
- public ObjectKey doInsert(ColumnValues insertValues)
+ public ObjectKey doInsert(final ColumnValues insertValues)
throws TorqueException
{
String databaseNameFromInsertValues = insertValues.getDbName();
@@ -659,8 +387,8 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if a database error occurs.
*/
public ObjectKey doInsert(
- ColumnValues insertValues,
- Connection connection)
+ final ColumnValues insertValues,
+ final Connection connection)
throws TorqueException
{
if (insertValues == null)
@@ -850,10 +578,10 @@ public class BasePeerImpl<T> implements
* @throws TorqueException Possible errors get wrapped in here.
*/
private SimpleKey getId(
- ColumnMap pk,
- IdGenerator keyGen,
- Connection con,
- Object keyInfo)
+ final ColumnMap pk,
+ final IdGenerator keyGen,
+ final Connection con,
+ final Object keyInfo)
throws TorqueException
{
SimpleKey id = null;
@@ -880,7 +608,7 @@ public class BasePeerImpl<T> implements
* @param criteria the Criteria to which the select columns should
* be added.
*/
- public void addSelectColumns(org.apache.torque.criteria.Criteria criteria)
+ public void addSelectColumns(final Criteria criteria)
{
ColumnMap[] columns = this.tableMap.getColumns();
@@ -891,55 +619,6 @@ public class BasePeerImpl<T> implements
}
/**
- * Add all the columns needed to create a new object.
- *
- * @param criteria the Criteria to which the select columns should
- * be added.
- *
- * @deprecated Please use addSelectColumns(
- * org.apache.torque.criteria.Criteria).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public void addSelectColumns(Criteria criteria)
- {
- ColumnMap[] columns = this.tableMap.getColumns();
-
- for (ColumnMap c : columns)
- {
- criteria.addSelectColumn(c);
- }
- }
-
- /**
- * Selects objects from a database.
- *
- * @param criteria object used to create the SELECT statement.
- *
- * @return the list of selected objects, not null.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- *
- * @deprecated Please use doSelect(org.apache.torque.criteria.Criteria).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public List<T> doSelect(Criteria criteria)
- throws TorqueException
- {
- if (criteria.getSelectColumns().size() == 0)
- {
- addSelectColumns(criteria);
- }
- setDbName(criteria);
-
- return doSelect(
- criteria,
- getRecordMapper());
- }
-
- /**
* Selects objects from a database.
*
* @param criteria object used to create the SELECT statement.
@@ -949,7 +628,7 @@ public class BasePeerImpl<T> implements
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public List<T> doSelect(org.apache.torque.criteria.Criteria criteria)
+ public List<T> doSelect(final Criteria criteria)
throws TorqueException
{
if (criteria.getSelectColumns().size() == 0)
@@ -974,44 +653,10 @@ public class BasePeerImpl<T> implements
*
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
- *
- * @deprecated Please use doSelect(org.apache.torque.criteria.Criteria,
- * Connection).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public List<T> doSelect(
- Criteria criteria,
- Connection connection)
- throws TorqueException
- {
- if (criteria.getSelectColumns().size() == 0)
- {
- addSelectColumns(criteria);
- }
- setDbName(criteria);
-
- return doSelect(
- criteria,
- getRecordMapper(),
- connection);
- }
-
- /**
- * Selects objects from a database
- * within a transaction.
- *
- * @param criteria object used to create the SELECT statement.
- * @param connection the connection to use, not null.
- *
- * @return the list of selected objects, not null.
- *
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
*/
public List<T> doSelect(
- org.apache.torque.criteria.Criteria criteria,
- Connection connection)
+ final Criteria criteria,
+ final Connection connection)
throws TorqueException
{
if (criteria.getSelectColumns().size() == 0)
@@ -1036,7 +681,7 @@ public class BasePeerImpl<T> implements
* @throws TorqueException If more than one record is selected or if
* an error occurs when processing the query.
*/
- public T doSelectSingleRecord(org.apache.torque.criteria.Criteria criteria)
+ public T doSelectSingleRecord(final Criteria criteria)
throws TorqueException
{
List<T> recordList = doSelect(criteria);
@@ -1066,8 +711,8 @@ public class BasePeerImpl<T> implements
* an error occurs when processing the query.
*/
public T doSelectSingleRecord(
- org.apache.torque.criteria.Criteria criteria,
- Connection connection)
+ final Criteria criteria,
+ final Connection connection)
throws TorqueException
{
List<T> recordList = doSelect(criteria, connection);
@@ -1094,54 +739,10 @@ public class BasePeerImpl<T> implements
* @return The results of the query, not null.
*
* @throws TorqueException if querying the database fails.
- *
- * @deprecated Please use doSelect(org.apache.torque.criteria.Criteria,
- * RecordMapper).
- * This method will be removed in a future version of Torque.
*/
- @Deprecated
public <TT> List<TT> doSelect(
- Criteria criteria,
- RecordMapper<TT> mapper)
- throws TorqueException
- {
- Connection connection = null;
- try
- {
- connection = Transaction.begin(criteria.getDbName());
-
- List<TT> result = doSelect(
- criteria,
- mapper,
- connection);
-
- Transaction.commit(connection);
- connection = null;
- return result;
- }
- finally
- {
- if (connection != null)
- {
- Transaction.safeRollback(connection);
- }
- }
- }
-
- /**
- * Selects rows from a database an maps them to objects.
- *
- * @param criteria A Criteria specifying the records to select, not null.
- * @param mapper The mapper creating the objects from the resultSet,
- * not null.
- *
- * @return The results of the query, not null.
- *
- * @throws TorqueException if querying the database fails.
- */
- public <TT> List<TT> doSelect(
- org.apache.torque.criteria.Criteria criteria,
- RecordMapper<TT> mapper)
+ final Criteria criteria,
+ final RecordMapper<TT> mapper)
throws TorqueException
{
Connection connection = null;
@@ -1176,7 +777,7 @@ public class BasePeerImpl<T> implements
*
* @throws TorqueException if querying the database fails.
*/
- public List<T> doSelect(String query)
+ public List<T> doSelect(final String query)
throws TorqueException
{
return doSelect(
@@ -1196,8 +797,8 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if querying the database fails.
*/
public List<T> doSelect(
- String query,
- Connection connection)
+ final String query,
+ final Connection connection)
throws TorqueException
{
return doSelect(
@@ -1220,9 +821,9 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if querying the database fails.
*/
public <TT> List<TT> doSelect(
- String query,
- RecordMapper<TT> mapper,
- String dbName)
+ final String query,
+ final RecordMapper<TT> mapper,
+ final String dbName)
throws TorqueException
{
Connection connection = null;
@@ -1259,197 +860,43 @@ public class BasePeerImpl<T> implements
* not null.
* @param connection the database connection, not null.
*
- * @return The results of the query, not null.
- *
- * @throws TorqueException if querying the database fails.
- */
- public <TT> List<TT> doSelect(
- String query,
- RecordMapper<TT> mapper,
- Connection connection)
- throws TorqueException
- {
- if (connection == null)
- {
- throw new NullPointerException("connection is null");
- }
-
- List<TT> result = new ArrayList<TT>();
- Statement statement = null;
- ResultSet resultSet = null;
- try
- {
- statement = connection.createStatement();
- long startTime = System.currentTimeMillis();
- log.debug("Executing query " + query);
-
- resultSet = statement.executeQuery(query.toString());
- long queryEndTime = System.currentTimeMillis();
- log.trace("query took " + (queryEndTime - startTime)
- + " milliseconds");
-
- while (resultSet.next())
- {
- TT rowResult = mapper.processRow(resultSet, 0, null);
- result.add(rowResult);
- }
- long mappingEndTime = System.currentTimeMillis();
- log.trace("mapping took " + (mappingEndTime - queryEndTime)
- + " milliseconds");
- }
- catch (SQLException e)
- {
- throw ExceptionMapper.getInstance().toTorqueException(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);
- }
- }
- }
- return result;
- }
-
- /**
- * Performs a SQL <code>select</code> 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 connection the database connection for selecting records,
- * not null.
- *
- * @return The results of the query, not null.
- *
- * @throws TorqueException Error performing database query.
- *
- * @deprecated Please use doSelect(org.apache.torque.criteria.Criteria,
- * RecordMapper, Connection).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public <TT> List<TT> doSelect(
- Criteria criteria,
- RecordMapper<TT> mapper,
- Connection connection)
- throws TorqueException
- {
- correctBooleans(criteria);
-
- Query query = SqlBuilder.buildQuery(criteria);
- if (query.getFromClause().isEmpty())
- {
- String tableName = SqlBuilder.getFullTableName(
- getTableMap().getFullyQualifiedTableName(),
- criteria.getDbName());
- query.getFromClause().add(new FromElement(tableName));
- }
-
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- statement = connection.prepareStatement(query.toString());
-
- List<Object> replacements = setPreparedStatementReplacements(
- statement,
- query.getPreparedStatementReplacements(),
- 0);
-
- long startTime = System.currentTimeMillis();
- log.debug("Executing query " + query
- + ", parameters = "
- + replacements);
-
- resultSet = statement.executeQuery();
- long queryEndTime = System.currentTimeMillis();
- log.trace("query took " + (queryEndTime - startTime)
- + " milliseconds");
-
- long offset;
- Database database = Torque.getDatabase(criteria.getDbName());
- if (database.getAdapter().supportsNativeOffset())
- {
- offset = 0; //database takes care of offset
- }
- else
- {
- offset = criteria.getOffset();
- }
-
- long 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();
- }
- }
- }
+ * @return The results of the query, not null.
+ *
+ * @throws TorqueException if querying the database fails.
+ */
+ public <TT> List<TT> doSelect(
+ final String query,
+ final RecordMapper<TT> mapper,
+ final Connection connection)
+ throws TorqueException
+ {
+ if (connection == null)
+ {
+ throw new NullPointerException("connection is null");
+ }
+
+ List<TT> result = new ArrayList<TT>();
+ Statement statement = null;
+ ResultSet resultSet = null;
+ try
+ {
+ statement = connection.createStatement();
+ long startTime = System.currentTimeMillis();
+ log.debug("Executing query " + query);
+
+ resultSet = statement.executeQuery(query.toString());
+ long queryEndTime = System.currentTimeMillis();
+ log.trace("query took " + (queryEndTime - startTime)
+ + " milliseconds");
- List<TT> result = new ArrayList<TT>();
- int rowNumber = 0;
while (resultSet.next())
{
- if (rowNumber < offset)
- {
- rowNumber++;
- continue;
- }
- if (limit >= 0 && rowNumber >= limit)
- {
- break;
- }
-
- TT rowResult = mapper.processRow(resultSet, 0, criteria);
+ TT rowResult = mapper.processRow(resultSet, 0, null);
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)
{
@@ -1480,6 +927,7 @@ public class BasePeerImpl<T> implements
}
}
}
+ return result;
}
/**
@@ -1496,9 +944,9 @@ public class BasePeerImpl<T> implements
* @throws TorqueException Error performing database query.
*/
public <TT> List<TT> doSelect(
- org.apache.torque.criteria.Criteria criteria,
- RecordMapper<TT> mapper,
- Connection connection)
+ final Criteria criteria,
+ final RecordMapper<TT> mapper,
+ final Connection connection)
throws TorqueException
{
correctBooleans(criteria);
@@ -1646,8 +1094,8 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if querying the database fails.
*/
public <TT> TT doSelectSingleRecord(
- org.apache.torque.criteria.Criteria criteria,
- RecordMapper<TT> mapper)
+ final Criteria criteria,
+ final RecordMapper<TT> mapper)
throws TorqueException
{
List<TT> resultList = doSelect(criteria, mapper);
@@ -1677,9 +1125,9 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if querying the database fails.
*/
public <TT> TT doSelectSingleRecord(
- org.apache.torque.criteria.Criteria criteria,
- RecordMapper<TT> mapper,
- Connection connection)
+ final Criteria criteria,
+ final RecordMapper<TT> mapper,
+ final Connection connection)
throws TorqueException
{
List<TT> resultList = doSelect(
@@ -1722,7 +1170,7 @@ public class BasePeerImpl<T> implements
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public int doUpdate(ColumnValues updateValues)
+ public int doUpdate(final ColumnValues updateValues)
throws TorqueException
{
String databaseNameFromUpdateValues = updateValues.getDbName();
@@ -1773,16 +1221,16 @@ public class BasePeerImpl<T> implements
* rethrown wrapped into a TorqueException.
*/
public int doUpdate(
- ColumnValues updateValues,
- Connection connection)
+ final ColumnValues updateValues,
+ final Connection connection)
throws TorqueException
{
ColumnMap pk = getTableMap().getPrimaryKey();
- org.apache.torque.criteria.Criteria selectCriteria = null;
+ Criteria selectCriteria = null;
if (pk != null && updateValues.containsKey(pk.getSqlExpression()))
{
- selectCriteria = new org.apache.torque.criteria.Criteria();
+ selectCriteria = new Criteria();
selectCriteria.where(pk,
updateValues.remove(pk.getSqlExpression()));
}
@@ -1806,56 +1254,10 @@ public class BasePeerImpl<T> implements
* @return the number of affected rows.
*
* @throws TorqueException if updating fails.
- *
- * @deprecated Please use doUpdate(
- * org.apache.torque.criteria.Criteria, ColumnValues).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public int doUpdate(
- Criteria selectCriteria,
- ColumnValues updateValues)
- throws TorqueException
- {
- String databaseNameFromUpdateValues = updateValues.getDbName();
- if (databaseNameFromUpdateValues == null)
- {
- databaseNameFromUpdateValues = getDatabaseName();
- }
- Connection connection = null;
- try
- {
- connection = Transaction.begin(databaseNameFromUpdateValues);
- int result = doUpdate(selectCriteria, updateValues, connection);
- Transaction.commit(connection);
- connection = null;
- return result;
- }
- finally
- {
- if (connection != null)
- {
- Transaction.safeRollback(connection);
- }
- }
- }
-
- /**
- * Executes an update against the database. The rows to be updated
- * are selected using <code>criteria</code> and updated using the values
- * in <code>updateValues</code>.
- *
- * @param selectCriteria selects which rows of which table
- * should be updated, not null.
- * @param updateValues Which columns to update with which values, not null.
- *
- * @return the number of affected rows.
- *
- * @throws TorqueException if updating fails.
*/
public int doUpdate(
- org.apache.torque.criteria.Criteria selectCriteria,
- ColumnValues updateValues)
+ final Criteria selectCriteria,
+ final ColumnValues updateValues)
throws TorqueException
{
String databaseNameFromUpdateValues = updateValues.getDbName();
@@ -1893,113 +1295,11 @@ public class BasePeerImpl<T> implements
* @return the number of affected rows.
*
* @throws TorqueException if updating fails.
- *
- * @deprecated Please use doUpdate(org.apache.torque.criteria.Criteria,
- * ColumnValues, Connection).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public int doUpdate(
- Criteria criteria,
- ColumnValues updateValues,
- Connection connection)
- throws TorqueException
- {
- Query query = SqlBuilder.buildQuery(criteria);
- query.setType(Query.Type.UPDATE);
-
- query.getFromClause().clear();
- String fullTableName = SqlBuilder.getFullTableName(
- getTableMap().getFullyQualifiedTableName(),
- criteria.getDbName());
- query.getFromClause().add(new FromElement(fullTableName));
-
- List<JdbcTypedValue> replacementObjects
- = new ArrayList<JdbcTypedValue>();
- for (Map.Entry<Column, JdbcTypedValue> updateValue
- : updateValues.entrySet())
- {
- Column column = updateValue.getKey();
- query.getSelectClause().add(column.getColumnName());
- replacementObjects.add(updateValue.getValue());
- }
-
- PreparedStatement preparedStatement = null;
- try
- {
- preparedStatement = connection.prepareStatement(query.toString());
- int position = 1;
- for (JdbcTypedValue replacementObject : replacementObjects)
- {
- Object value = replacementObject.getValue();
- if (value != null)
- {
- preparedStatement.setObject(position, value);
- }
- else
- {
- preparedStatement.setNull(
- position,
- replacementObject.getJdbcType());
- }
- position++;
- }
- List<Object> replacements = setPreparedStatementReplacements(
- preparedStatement,
- query.getPreparedStatementReplacements(),
- position - 1);
- long startTime = System.currentTimeMillis();
- log.debug("Executing update " + query.toString()
- + " using update parameters " + replacementObjects
- + " and query parameters "
- + replacements);
-
- int affectedRows = preparedStatement.executeUpdate();
- long queryEndTime = System.currentTimeMillis();
- log.trace("update took " + (queryEndTime - startTime)
- + " milliseconds");
-
- preparedStatement.close();
- preparedStatement = null;
- return affectedRows;
- }
- catch (SQLException e)
- {
- throw ExceptionMapper.getInstance().toTorqueException(e);
- }
- finally
- {
- if (preparedStatement != null)
- {
- try
- {
- preparedStatement.close();
- }
- catch (SQLException e)
- {
- log.warn("error closing prepared statement", e);
- }
- }
- }
- }
-
- /**
- * Executes an update against the database. The rows to be updated
- * are selected using <code>criteria</code> and updated using the values
- * in <code>updateValues</code>.
- *
- * @param criteria selects which rows of which table should be updated.
- * @param updateValues Which columns to update with which values, not null.
- * @param connection the database connection to use, not null.
- *
- * @return the number of affected rows.
- *
- * @throws TorqueException if updating fails.
*/
public int doUpdate(
- org.apache.torque.criteria.Criteria criteria,
- ColumnValues updateValues,
- Connection connection)
+ final Criteria criteria,
+ final ColumnValues updateValues,
+ final Connection connection)
throws TorqueException
{
Query query = SqlBuilder.buildQuery(criteria);
@@ -2093,7 +1393,7 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if executing the statement fails
* or no database connection can be established.
*/
- public int executeStatement(String statementString) throws TorqueException
+ public int executeStatement(final String statementString) throws TorqueException
{
return executeStatement(statementString, Torque.getDefaultDB(), null);
}
@@ -2114,8 +1414,8 @@ public class BasePeerImpl<T> implements
* or no database connection can be established.
*/
public int executeStatement(
- String statementString,
- List<JdbcTypedValue> replacementValues)
+ final String statementString,
+ final List<JdbcTypedValue> replacementValues)
throws TorqueException
{
return executeStatement(
@@ -2142,9 +1442,9 @@ public class BasePeerImpl<T> implements
* or no database connection can be established.
*/
public int executeStatement(
- String statementString,
- String dbName,
- List<JdbcTypedValue> replacementValues)
+ final String statementString,
+ final String dbName,
+ final List<JdbcTypedValue> replacementValues)
throws TorqueException
{
Connection con = null;
@@ -2184,9 +1484,9 @@ public class BasePeerImpl<T> implements
* @throws TorqueException if executing the statement fails.
*/
public int executeStatement(
- String statementString,
- Connection con,
- List<JdbcTypedValue> replacementValues)
+ final String statementString,
+ final Connection con,
+ final List<JdbcTypedValue> replacementValues)
throws TorqueException
{
int rowCount = -1;
@@ -2251,9 +1551,9 @@ public class BasePeerImpl<T> implements
* @throws SQLException if setting the parameter fails.
*/
private List<Object> setPreparedStatementReplacements(
- PreparedStatement statement,
- List<Object> replacements,
- int offset)
+ final PreparedStatement statement,
+ final List<Object> replacements,
+ final int offset)
throws SQLException
{
List<Object> result = new ArrayList<Object>(replacements.size());
@@ -2314,85 +1614,6 @@ public class BasePeerImpl<T> implements
}
/**
- * Changes the boolean values in the criteria to the appropriate type,
- * whenever a booleanchar or booleanint column is involved.
- * This enables the user to create criteria using Boolean values
- * for booleanchar or booleanint columns.
- *
- * @param criteria the criteria in which the boolean values should be
- * corrected.
- * @throws TorqueException if the database map for the criteria cannot be
- * obtained.
- *
- * @deprecated Please use correctBooleans(
- * org.apache.torque.criteria.Criteria).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- public void correctBooleans(Criteria criteria)
- throws TorqueException
- {
- for (Object criterionObject : criteria.values())
- {
- Criteria.Criterion criterion = (Criteria.Criterion) criterionObject;
- correctBooleans(criteria, criterion);
- }
- }
-
- /**
- * Checks all columns in the criteria to see whether
- * booleanchar and booleanint columns are queried with a boolean.
- * If yes, the query values are mapped onto values the database
- * does understand, i.e. 0 and 1 for booleanints and N and Y for
- * booleanchar columns.
- *
- * @param criteria The criteria to which teh criterion belongs.
- * @param criterion The criterion to be checked for booleanint
- * and booleanchar columns.
- *
- * @throws TorqueException if the database map for the criteria cannot be
- * retrieved.
- *
- * @deprecated
- */
- @Deprecated
- private void correctBooleans(
- Criteria criteria,
- Criteria.Criterion criterion)
- throws TorqueException
- {
- Column column = criterion.getColumn();
- TableMap tableMapFromCriteria = MapHelper.getTableMap(
- column,
- criteria,
- tableMap);
- // if no description of table available, do not modify anything
- if (tableMapFromCriteria != null)
- {
- String columnName = column.getColumnName();
- ColumnMap columnMap = tableMapFromCriteria.getColumn(columnName);
- if (columnMap != null)
- {
- if ("BOOLEANINT".equals(columnMap.getTorqueType()))
- {
- replaceBooleanValues(
- criterion,
- Integer.valueOf(1),
- Integer.valueOf(0));
- }
- else if ("BOOLEANCHAR".equals(columnMap.getTorqueType()))
- {
- replaceBooleanValues(criterion, "Y", "N");
- }
- }
- }
- for (Criteria.Criterion attachedCriterion : criterion.getClauses())
- {
- correctBooleans(criteria, attachedCriterion);
- }
- }
-
- /**
* Checks all columns in the criteria to see whether
* booleanchar and booleanint columns are queried with a boolean.
* If yes, the query values are mapped onto values the database
@@ -2406,7 +1627,7 @@ public class BasePeerImpl<T> implements
* retrieved.
*/
public void correctBooleans(
- org.apache.torque.criteria.Criteria criteria)
+ final Criteria criteria)
throws TorqueException
{
correctBooleans(
@@ -2415,8 +1636,8 @@ public class BasePeerImpl<T> implements
}
private void correctBooleans(
- org.apache.torque.criteria.Criteria criteria,
- org.apache.torque.criteria.Criterion criterion)
+ final Criteria criteria,
+ final Criterion criterion)
throws TorqueException
{
if (criterion == null)
@@ -2425,7 +1646,7 @@ public class BasePeerImpl<T> implements
}
if (criterion.isComposite())
{
- for (org.apache.torque.criteria.Criterion part
+ for (Criterion part
: criterion.getParts())
{
correctBooleans(criteria, part);
@@ -2467,51 +1688,14 @@ public class BasePeerImpl<T> implements
* and falseValue if the Boolean equals <code>Boolean.FALSE</code>.
*
* @param criterion the criterion to replace Boolean values in.
- * @param trueValue the value by which Boolean.TRUE should be replaced.
- * @param falseValue the value by which Boolean.FALSE should be replaced.
- *
- * @deprecated
- */
- @Deprecated
- private void replaceBooleanValues(
- Criteria.Criterion criterion,
- Object trueValue,
- Object falseValue)
- {
- // attachedCriterions also contains the criterion itself,
- // so no additional treatment is needed for the criterion itself.
- Criteria.Criterion[] attachedCriterions
- = criterion.getAttachedCriterion();
- for (int i = 0; i < attachedCriterions.length; ++i)
- {
- Object criterionValue
- = attachedCriterions[i].getValue();
- if (criterionValue instanceof Boolean)
- {
- Boolean booleanValue = (Boolean) criterionValue;
- attachedCriterions[i].setValue(
- Boolean.TRUE.equals(booleanValue)
- ? trueValue
- : falseValue);
- }
-
- }
- }
-
- /**
- * Replaces any Boolean value in the criterion and its attached Criterions
- * by trueValue if the Boolean equals <code>Boolean.TRUE</code>
- * and falseValue if the Boolean equals <code>Boolean.FALSE</code>.
- *
- * @param criterion the criterion to replace Boolean values in.
* May not be a composite criterion.
* @param trueValue the value by which Boolean.TRUE should be replaced.
* @param falseValue the value by which Boolean.FALSE should be replaced.
*/
private void replaceBooleanValues(
- org.apache.torque.criteria.Criterion criterion,
- Object trueValue,
- Object falseValue)
+ final Criterion criterion,
+ final Object trueValue,
+ final Object falseValue)
{
Object rValue = criterion.getRValue();
if (rValue instanceof Boolean)
@@ -2546,7 +1730,7 @@ public class BasePeerImpl<T> implements
* retrieved.
*/
public void correctBooleans(
- ColumnValues columnValues)
+ final ColumnValues columnValues)
throws TorqueException
{
for (Map.Entry<Column, JdbcTypedValue> entry : columnValues.entrySet())
@@ -2596,7 +1780,7 @@ public class BasePeerImpl<T> implements
*
* @param crit the criteria to set the database name in, not null.
*/
- protected void setDbName(org.apache.torque.criteria.Criteria crit)
+ protected void setDbName(final Criteria crit)
throws TorqueException
{
if (crit.getDbName() == null)
@@ -2604,23 +1788,4 @@ public class BasePeerImpl<T> implements
crit.setDbName(getDatabaseName());
}
}
-
- /**
- * Sets the database name in the passed criteria to the table's default,
- * if it is not already set.
- *
- * @param crit the criteria to set the database name in, not null.
- *
- * @deprecated Please use addSelectColumns(
- * org.apache.torque.criteria.Criteria).
- * This method will be removed in a future version of Torque.
- */
- @Deprecated
- protected void setDbName(Criteria crit) throws TorqueException
- {
- if (crit.getDbName() == null)
- {
- crit.setDbName(getDatabaseName());
- }
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org