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 2014/10/22 00:53:18 UTC
svn commit: r1633480 - in /db/torque/torque4/trunk/torque-runtime/src:
main/java/org/apache/torque/sql/SqlBuilder.java
main/java/org/apache/torque/sql/whereclausebuilder/EnumValueBuilder.java
test/java/org/apache/torque/sql/SqlBuilderTest.java
Author: tfischer
Date: Tue Oct 21 22:53:17 2014
New Revision: 1633480
URL: http://svn.apache.org/r1633480
Log:
TORQUE-331 - implement enum handling in Criteria
Added:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/whereclausebuilder/EnumValueBuilder.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
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=1633480&r1=1633479&r2=1633480&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 Tue Oct 21 22:53:17 2014
@@ -46,6 +46,7 @@ import org.apache.torque.sql.whereclause
import org.apache.torque.sql.whereclausebuilder.LikeBuilder;
import org.apache.torque.sql.whereclausebuilder.NullValueBuilder;
import org.apache.torque.sql.whereclausebuilder.StandardBuilder;
+import org.apache.torque.sql.whereclausebuilder.EnumValueBuilder;
import org.apache.torque.sql.whereclausebuilder.VerbatimSqlConditionBuilder;
import org.apache.torque.sql.whereclausebuilder.WhereClausePsPartBuilder;
import org.apache.torque.util.UniqueColumnList;
@@ -77,6 +78,7 @@ public final class SqlBuilder
static
{
+ whereClausePsPartBuilders.add(new EnumValueBuilder());
whereClausePsPartBuilders.add(new VerbatimSqlConditionBuilder());
whereClausePsPartBuilders.add(new CurrentDateTimePsPartBuilder());
whereClausePsPartBuilders.add(new NullValueBuilder());
Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/whereclausebuilder/EnumValueBuilder.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/whereclausebuilder/EnumValueBuilder.java?rev=1633480&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/whereclausebuilder/EnumValueBuilder.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/whereclausebuilder/EnumValueBuilder.java Tue Oct 21 22:53:17 2014
@@ -0,0 +1,134 @@
+package org.apache.torque.sql.whereclausebuilder;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.torque.TorqueException;
+import org.apache.torque.adapter.Adapter;
+import org.apache.torque.criteria.PreparedStatementPart;
+import org.apache.torque.sql.SqlBuilder;
+import org.apache.torque.sql.WhereClauseExpression;
+
+/**
+ * Builds a PreparedStatementPart from a WhereClauseExpression containing an enum object.
+ *
+ * @version $Id: InBuilder.java 1448414 2013-02-20 21:06:35Z tfischer $
+ */
+public class EnumValueBuilder extends AbstractWhereClausePsPartBuilder
+{
+ /**
+ * Takes a WhereClauseExpression containing a enum object and unwraps the enum value.
+ *
+ * @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.
+ */
+ public PreparedStatementPart buildPs(
+ final WhereClauseExpression whereClausePart,
+ final boolean ignoreCase,
+ final Adapter adapter)
+ throws TorqueException
+ {
+ if (whereClausePart.getLValue() instanceof Enum)
+ {
+ whereClausePart.setLValue(getWrappedValue(whereClausePart.getLValue()));
+ }
+ if (whereClausePart.getRValue() instanceof Enum)
+ {
+ whereClausePart.setRValue(getWrappedValue(whereClausePart.getRValue()));
+ }
+ for (WhereClausePsPartBuilder builder : SqlBuilder.getWhereClausePsPartBuilders())
+ {
+ if (builder.isApplicable(whereClausePart, adapter))
+ {
+ return builder.buildPs(
+ whereClausePart,
+ ignoreCase,
+ adapter);
+ }
+ }
+ // should not happen as last element in list is standardHandler
+ // which takes all
+ throw new RuntimeException("No handler found for whereClausePart " + whereClausePart);
+ }
+
+ protected Object getWrappedValue(final Object wrapped) throws TorqueException
+ {
+ Class<?> clazz = wrapped.getClass();
+ Method getValueMethod;
+ try
+ {
+ getValueMethod = clazz.getMethod("getValue");
+ }
+ catch (SecurityException e)
+ {
+ throw new TorqueException("Could not access the getValue() method of the class of an enum value, " + clazz.getName(),
+ e);
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new TorqueException("An enum is given as Criterion value but its class, " + clazz.getName()
+ + ", does not have a parameterless getValue() method");
+ }
+ if (getValueMethod.getReturnType().equals(Void.class))
+ {
+ throw new TorqueException("An enum is given as Criterion value but its class, " + clazz.getName()
+ + ", has a parameterless getValue() method which retirsns void. It should return the wrapped type instead.");
+ }
+ try
+ {
+ return getValueMethod.invoke(wrapped);
+ }
+ catch (IllegalArgumentException e)
+ {
+ // should not happen
+ throw new TorqueException("Could not invoke the getValue() method of the class of an enum value, " + clazz.getName(),
+ e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new TorqueException("Could not access the getValue() method of the class of an enum value, " + clazz.getName(),
+ e);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw new TorqueException("The getValue() method of the class of an enum value, " + clazz.getName()
+ + " threw an exception",
+ e);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isApplicable(
+ final WhereClauseExpression whereClauseExpression,
+ final Adapter adapter)
+ {
+ return (whereClauseExpression.getLValue() instanceof Enum
+ || whereClauseExpression.getRValue() instanceof Enum);
+ }
+}
Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java?rev=1633480&r1=1633479&r2=1633480&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java Tue Oct 21 22:53:17 2014
@@ -43,8 +43,8 @@ import org.apache.torque.om.NumberKey;
*/
public class SqlBuilderTest extends BaseTestCase
{
- /** Time limit in ms of builing a query from a large array. */
- private static final long LARGE_ARRAY_TIME_LIMIT = 100L;
+ /** Time limit in ms of builing a query from a large array. */
+ private static final long LARGE_ARRAY_TIME_LIMIT = 100L;
/**
* Test where condition with several ANDs compairing against Strings.
@@ -2377,4 +2377,37 @@ public class SqlBuilderTest extends Base
assertEquals(2, query.getPreparedStatementReplacements().get(2));
assertEquals(1, query.getPreparedStatementReplacements().get(3));
}
+
+ public void testValueHolder() throws Exception
+ {
+ Criteria criteria = new Criteria()
+ .where(new ColumnImpl("table.column1"), ValueHolderImpl.A)
+ .and(ValueHolderImpl.B, new ColumnImpl("table.column2"))
+ .addSelectColumn(new ColumnImpl("table.column1"));
+ Query query = SqlBuilder.buildQuery(criteria);
+ assertEquals("SELECT table.column1 FROM table "
+ + "WHERE (table.column1=? "
+ + "AND ?=table.column2)",
+ query.toString());
+ assertEquals(2, query.getPreparedStatementReplacements().size());
+ assertEquals("A", query.getPreparedStatementReplacements().get(0));
+ assertEquals("B", query.getPreparedStatementReplacements().get(1));
+ }
+
+ public static enum ValueHolderImpl
+ {
+ A,
+ B,
+ C;
+
+ public String getValue()
+ {
+ return toString();
+ }
+
+ public static ValueHolderImpl getByValue(final String arg)
+ {
+ return valueOf(arg);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org