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 2012/10/12 00:52:33 UTC

svn commit: r1397355 - in /db/torque/torque4/trunk/torque-runtime/src: main/java/org/apache/torque/criteria/Criteria.java main/java/org/apache/torque/criteria/SqlEnum.java test/java/org/apache/torque/criteria/CriteriaTest.java

Author: tfischer
Date: Thu Oct 11 22:52:33 2012
New Revision: 1397355

URL: http://svn.apache.org/viewvc?rev=1397355&view=rev
Log:
TORQUE-234 Intuitive way of using one-arg operators in Criteria 

Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/SqlEnum.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java?rev=1397355&r1=1397354&r2=1397355&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java Thu Oct 11 22:52:33 2012
@@ -998,25 +998,40 @@ public class Criteria
 
     /**
      * "AND"s a new condition with the conditions in this Criteria.
-     * An EQUAL comparison is used for comparing rValue and lValue.
+     * Depending on rValue, the condition is constructed differently:
+     * Either rValue is a unary comparison operator (i.e. is a SqlEnum and
+     * getNumberOfCompareOperands() == 1) (e.g. Criteria.ISNULL),
+     * then lValue is taken as single operand of the operator
+     * ant the passed operator is used for comparison.
+     * Otherwise, an EQUAL comparison is used for comparing rValue and lValue.
      *
      * @param lValue The left hand side of the comparison, not null.
      *        If this object implements the Column interface, it is interpreted
      *        as a (pseudo)column.
      *        If this object is c Criteria, it is interpreted as a subselect.
-     *        In all other cases, (e.G. string object), it is interpreted as
+     *        In all other cases, (e.g. string object), it is interpreted as
      *        literal value.
      * @param rValue The right hand side of the comparison, may be null.
+     *        If this object is a unary comparison operator, it is taken as
+     *        comparison operator of the condition to add.
      *        If this object implements the Column interface, it is interpreted
      *        as a (pseudo)column.
      *        If this object is c Criteria, it is interpreted as a subselect.
-     *        In all other cases, (e.G. string object), it is interpreted as
+     *        In all other cases, (e.g. string object), it is interpreted as
      *        literal value.
      *
      * @return A modified Criteria object.
      */
     public Criteria and(Object lValue, Object rValue)
     {
+        if (rValue instanceof SqlEnum)
+        {
+            SqlEnum sqlEnum = (SqlEnum) rValue;
+            if (sqlEnum.getNumberOfCompareOperands() == 1)
+            {
+                return and(lValue, null, sqlEnum);
+            }
+        }
         return and(lValue, rValue, EQUAL);
     }
 
@@ -1312,25 +1327,40 @@ public class Criteria
 
     /**
      * "OR"s a new condition with the conditions in this Criteria.
-     * An EQUAL comparison is used for column and value.
+     * Depending on rValue, the condition is constructed differently:
+     * Either rValue is a unary comparison operator (i.e. is a SqlEnum and
+     * getNumberOfCompareOperands() == 1) (e.g. Criteria.ISNULL),
+     * then lValue is taken as single operand of the operator
+     * ant the passed operator is used for comparison.
+     * Otherwise, an EQUAL comparison is used for comparing rValue and lValue.
      *
      * @param lValue The left hand side of the comparison, not null.
      *        If this object implements the Column interface, it is interpreted
      *        as a (pseudo)column.
      *        If this object is c Criteria, it is interpreted as a subselect.
-     *        In all other cases, (e.G. string object), it is interpreted as
+     *        In all other cases, (e.g. string object), it is interpreted as
      *        literal value.
      * @param rValue The right hand side of the comparison, may be null.
+     *        If this object is a unary comparison operator, it is taken as
+     *        comparison operator of the condition to add.
      *        If this object implements the Column interface, it is interpreted
      *        as a (pseudo)column.
      *        If this object is c Criteria, it is interpreted as a subselect.
-     *        In all other cases, (e.G. string object), it is interpreted as
+     *        In all other cases, (e.g. string object), it is interpreted as
      *        literal value.
      *
      * @return A modified Criteria object.
      */
     public Criteria or(Object lValue, Object rValue)
     {
+        if (rValue instanceof SqlEnum)
+        {
+            SqlEnum sqlEnum = (SqlEnum) rValue;
+            if (sqlEnum.getNumberOfCompareOperands() == 1)
+            {
+                return or(lValue, null, sqlEnum);
+            }
+        }
         return or(lValue, rValue, EQUAL);
     }
 
@@ -1612,20 +1642,28 @@ public class Criteria
 
     /**
      * "AND"s a new condition with the conditions in this Criteria.
-     * Equivalent to <code>#and(Column, Object)</code> but better to read
+     * Equivalent to <code>#and(Object, Object)</code> but better to read
      * if this is the first condition to be added to the Criteria.
+     * Depending on rValue, the condition is constructed differently:
+     * Either rValue is a unary comparison operator (i.e. is a SqlEnum and
+     * getNumberOfCompareOperands() == 1) (e.g. Criteria.ISNULL),
+     * then lValue is taken as single operand of the operator
+     * ant the passed operator is used for comparison.
+     * Otherwise, an EQUAL comparison is used for comparing rValue and lValue.
      *
      * @param lValue The left hand side of the comparison, not null.
      *        If this object implements the Column interface, it is interpreted
      *        as a (pseudo)column.
      *        If this object is c Criteria, it is interpreted as a subselect.
-     *        In all other cases, (e.G. string object), it is interpreted as
+     *        In all other cases, (e.g. string object), it is interpreted as
      *        literal value.
      * @param rValue The right hand side of the comparison, may be null.
+     *        If this object is a unary comparison operator, it is taken as
+     *        comparison operator of the condition to add.
      *        If this object implements the Column interface, it is interpreted
      *        as a (pseudo)column.
      *        If this object is c Criteria, it is interpreted as a subselect.
-     *        In all other cases, (e.G. string object), it is interpreted as
+     *        In all other cases, (e.g. string object), it is interpreted as
      *        literal value.
      *
      * @return A modified Criteria object.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/SqlEnum.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/SqlEnum.java?rev=1397355&r1=1397354&r2=1397355&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/SqlEnum.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/SqlEnum.java Thu Oct 11 22:52:33 2012
@@ -39,9 +39,17 @@ public final class SqlEnum implements ja
     /** The SQL expression. */
     private final String s;
 
-    private SqlEnum(String s)
+    /**
+     * The number of operands, if the SqlEnum is a comparison operator.
+     * -1 if the SqlEnum is no comparison operator.
+     * A Set operand (e.g. in IN) is counted as one operand.
+     */
+    private final int numberOfCompareOperands;
+
+    private SqlEnum(String s, int numberOfCompareOperands)
     {
         this.s = s;
+        this.numberOfCompareOperands = numberOfCompareOperands;
     }
 
     @Override
@@ -50,45 +58,57 @@ public final class SqlEnum implements ja
         return s;
     }
 
+    /**
+     * Returns the number of operands, if the SqlEnum is a comparison operator.
+     * A Set operand (e.g. in IN) is counted as one operand.
+     *
+     * @return the number of compare operands, or -1 if the SqlEnum
+     *         is no comparison operator.
+     */
+    public int getNumberOfCompareOperands()
+    {
+        return numberOfCompareOperands;
+    }
+
     /** SQL Expression "=". */
     public static final SqlEnum EQUAL =
-        new SqlEnum("=");
+        new SqlEnum("=", 2);
     /** SQL Expression "<>". */
     public static final SqlEnum NOT_EQUAL =
-            new SqlEnum("<>");
+            new SqlEnum("<>", 2);
     /** SQL Expression "!=". */
     public static final SqlEnum ALT_NOT_EQUAL =
-        new SqlEnum("!=");
+        new SqlEnum("!=", 2);
     /** SQL Expression ">". */
     public static final SqlEnum GREATER_THAN =
-        new SqlEnum(">");
+        new SqlEnum(">", 2);
     /** SQL Expression "<". */
     public static final SqlEnum LESS_THAN =
-        new SqlEnum("<");
+        new SqlEnum("<", 2);
     /** SQL Expression ">=". */
     public static final SqlEnum GREATER_EQUAL =
-        new SqlEnum(">=");
+        new SqlEnum(">=", 2);
     /** SQL Expression "<=". */
     public static final SqlEnum LESS_EQUAL =
-        new SqlEnum("<=");
+        new SqlEnum("<=", 2);
     /** SQL Expression " LIKE ". */
     public static final SqlEnum LIKE =
-        new SqlEnum(" LIKE ");
+        new SqlEnum(" LIKE ", 2);
     /** SQL Expression " NOT LIKE ". */
     public static final SqlEnum NOT_LIKE =
-        new SqlEnum(" NOT LIKE ");
+        new SqlEnum(" NOT LIKE ", 2);
     /** SQL Expression " ILIKE ". */
     public static final SqlEnum ILIKE =
-        new SqlEnum(" ILIKE ");
+        new SqlEnum(" ILIKE ", 2);
     /** SQL Expression " NOT ILIKE ". */
     public static final SqlEnum NOT_ILIKE =
-        new SqlEnum(" NOT ILIKE ");
+        new SqlEnum(" NOT ILIKE ", 2);
     /** SQL Expression " IN ". */
     public static final SqlEnum IN =
-        new SqlEnum(" IN ");
+        new SqlEnum(" IN ", 2);
     /** SQL Expression " NOT IN ". */
     public static final SqlEnum NOT_IN =
-        new SqlEnum(" NOT IN ");
+        new SqlEnum(" NOT IN ", 2);
     /**
      * Constant for "CUSTOM".
      *
@@ -101,46 +121,46 @@ public final class SqlEnum implements ja
      */
     @Deprecated
     public static final SqlEnum CUSTOM =
-        new SqlEnum("CUSTOM");
+        new SqlEnum("CUSTOM", -1);
     /** SQL Expression "JOIN". */
     public static final SqlEnum JOIN =
-        new SqlEnum("JOIN");
+        new SqlEnum("JOIN", -1);
     /** SQL Expression "DISTINCT ". */
     public static final SqlEnum DISTINCT =
-        new SqlEnum("DISTINCT ");
+        new SqlEnum("DISTINCT ", -1);
     /** SQL Expression "ALL ". */
     public static final SqlEnum ALL =
-        new SqlEnum("ALL ");
+        new SqlEnum("ALL ", -1);
     /** SQL Expression "ASC". */
     public static final SqlEnum ASC =
-        new SqlEnum("ASC");
+        new SqlEnum("ASC", -1);
     /** SQL Expression "DESC". */
     public static final SqlEnum DESC =
-        new SqlEnum("DESC");
+        new SqlEnum("DESC", -1);
     /** SQL Expression " IS NULL ". */
     public static final SqlEnum ISNULL =
-        new SqlEnum(" IS NULL");
+        new SqlEnum(" IS NULL", 1);
     /** SQL Expression " IS NOT NULL ". */
     public static final SqlEnum ISNOTNULL =
-        new SqlEnum(" IS NOT NULL");
+        new SqlEnum(" IS NOT NULL", 1);
     /** SQL Expression "CURRENT_DATE". */
     public static final SqlEnum CURRENT_DATE =
-        new SqlEnum("CURRENT_DATE");
+        new SqlEnum("CURRENT_DATE", -1);
     /** SQL Expression "CURRENT_TIME". */
     public static final SqlEnum CURRENT_TIME =
-        new SqlEnum("CURRENT_TIME");
+        new SqlEnum("CURRENT_TIME", -1);
     /** SQL Expression "CURRENT_TIMESTAMP". */
     public static final SqlEnum CURRENT_TIMESTAMP =
-            new SqlEnum("CURRENT_TIMESTAMP");
+            new SqlEnum("CURRENT_TIMESTAMP", -1);
     /** SQL Expression " ON ". */
     public static final SqlEnum ON =
-        new SqlEnum(" ON ");
+        new SqlEnum(" ON ", -1);
     /** SQL Expression " AS ". */
     public static final SqlEnum AS =
-        new SqlEnum(" AS ");
+        new SqlEnum(" AS ", -1);
     /** SQL Expression " ESCAPE ". */
     public static final SqlEnum ESCAPE =
-        new SqlEnum(" ESCAPE ");
+        new SqlEnum(" ESCAPE ", -1);
 
     /**
      * returns whether o is the same SqlEnum as this object.

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java?rev=1397355&r1=1397354&r2=1397355&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java Thu Oct 11 22:52:33 2012
@@ -100,7 +100,6 @@ public class CriteriaTest extends BaseTe
         final Column column = new ColumnImpl("myTable", "myColumn");
         final String value = "myValue";
 
-        // Add the string
         c.where(column, value);
 
         // Verify that the Criterion is not composite
@@ -113,6 +112,85 @@ public class CriteriaTest extends BaseTe
     }
 
     /**
+     * Test basic where condition on a string. The condition is reversed, i.e
+     * the String preceeds the column.
+     */
+    public void testWhereStringReversed()
+    {
+        final Column column = new ColumnImpl("myTable", "myColumn");
+        final String value = "myValue";
+
+        c.where(value, column);
+
+        // Verify that the Criterion is not composite
+        assertFalse(c.getTopLevelCriterion().isComposite());
+
+        // Verify that what we get out is what we put in
+        assertEquals(value, c.getTopLevelCriterion().getLValue());
+        assertEquals(column, c.getTopLevelCriterion().getRValue());
+        assertEquals(Criteria.EQUAL, c.getTopLevelCriterion().getComparison());
+    }
+
+    /**
+     * Test basic where condition on a string with a non-equal.
+     * comparison operator.
+     */
+    public void testWhereNotEqual()
+    {
+        final Column column = new ColumnImpl("myTable", "myColumn");
+        final String value = "myValue";
+
+        // Add the string
+        c.where(column, value, Criteria.NOT_EQUAL);
+
+        // Verify that the Criterion is not composite
+        assertFalse(c.getTopLevelCriterion().isComposite());
+
+        // Verify that what we get out is what we put in
+        assertEquals(column, c.getTopLevelCriterion().getLValue());
+        assertEquals(value, c.getTopLevelCriterion().getRValue());
+        assertEquals(Criteria.NOT_EQUAL, c.getTopLevelCriterion().getComparison());
+    }
+
+    /**
+     * Tests that unary operators as rValue are interpreted
+     * as comparison operator in the two-arg where method.
+     */
+    public void testWhereUnaryOperator()
+    {
+        // prepare
+        final Column column = new ColumnImpl("myTable", "myColumn");
+        final SqlEnum operator = SqlEnum.ISNOTNULL;
+
+        // execute
+        c.where(column, operator);
+
+        // Verify that what we get out is what we put in
+        assertEquals(column, c.getTopLevelCriterion().getLValue());
+        assertEquals(null, c.getTopLevelCriterion().getRValue());
+        assertEquals(operator, c.getTopLevelCriterion().getComparison());
+    }
+
+    /**
+     * Tests that unary operators as rValue are interpreted
+     * as comparison operator in the two-arg or method.
+     */
+    public void testOrUnaryOperator()
+    {
+        // prepare
+        final Column column = new ColumnImpl("myTable", "myColumn");
+        final SqlEnum operator = SqlEnum.ISNOTNULL;
+
+        // execute
+        c.or(column, operator);
+
+        // Verify that what we get out is what we put in
+        assertEquals(column, c.getTopLevelCriterion().getLValue());
+        assertEquals(null, c.getTopLevelCriterion().getRValue());
+        assertEquals(operator, c.getTopLevelCriterion().getComparison());
+    }
+
+    /**
      * Test where condition with several ANDs compairing against Strings.
      */
     public void testAndString()
@@ -448,7 +526,7 @@ public class CriteriaTest extends BaseTe
                 preparedStatementReplacements.get(0));
     }
 
-    public void testCurrentDate() throws TorqueException
+    public void testAndCurrentDate() throws TorqueException
     {
         c.where(new ColumnImpl("TABLE", "DATE_COLUMN"), Criteria.CURRENT_DATE);
         c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));
@@ -464,7 +542,7 @@ public class CriteriaTest extends BaseTe
         assertEquals(0, preparedStatementReplacements.size());
     }
 
-    public void testCurrentTime() throws TorqueException
+    public void testAndCurrentTime() throws TorqueException
     {
         c.where(new ColumnImpl("TABLE", "TIME_COLUMN"), Criteria.CURRENT_TIME);
         c.addSelectColumn(new ColumnImpl(null, "TABLE", null, "COUNT(*)"));



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org