You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2007/06/13 17:20:58 UTC

svn commit: r546918 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ test/java/org/apache/cayenne/access/

Author: aadamchik
Date: Wed Jun 13 08:20:57 2007
New Revision: 546918

URL: http://svn.apache.org/viewvc?view=rev&rev=546918
Log:
CAY-805: SQLTemplate improvement: new #bindObjectEqual #bindObjectNotEqual directives
#bindObjectNotEqual - first cut

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindObjectNotEqualDirective.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateProcessor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindObjectNotEqualDirective.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindObjectNotEqualDirective.java?view=auto&rev=546918
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindObjectNotEqualDirective.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindObjectNotEqualDirective.java Wed Jun 13 08:20:57 2007
@@ -0,0 +1,67 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+package org.apache.cayenne.access.jdbc;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.velocity.context.InternalContextAdapter;
+
+/**
+ * A custom Velocity directive to create a set of SQL conditions to check unequality of an
+ * ObjectId of an object. Usage in Velocity template is "WHERE
+ * #bindObjectNotEqual($object)" or "WHERE #bindObjectNotEqual($object $columns
+ * $idValues)".
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class BindObjectNotEqualDirective extends BindObjectEqualDirective {
+
+    public String getName() {
+        return "bindObjectNotEqual";
+    }
+
+    protected void renderColumn(
+            InternalContextAdapter context,
+            Writer writer,
+            Object columnName,
+            int columnIndex) throws IOException {
+
+        if (columnIndex > 0) {
+            writer.write(" OR ");
+        }
+
+        writer.write(columnName.toString());
+    }
+
+    protected void render(
+            InternalContextAdapter context,
+            Writer writer,
+            ParameterBinding binding) throws IOException {
+
+        if (binding.getValue() != null) {
+            bind(context, binding);
+            writer.write("<> ?");
+        }
+        else {
+            writer.write("IS NOT NULL");
+        }
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateProcessor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateProcessor.java?view=diff&rev=546918&r1=546917&r2=546918
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateProcessor.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateProcessor.java Wed Jun 13 08:20:57 2007
@@ -75,6 +75,7 @@
         sharedRuntime.addProperty("userdirective", BindEqualDirective.class.getName());
         sharedRuntime.addProperty("userdirective", BindNotEqualDirective.class.getName());
         sharedRuntime.addProperty("userdirective", BindObjectEqualDirective.class.getName());
+        sharedRuntime.addProperty("userdirective", BindObjectNotEqualDirective.class.getName());
         sharedRuntime.addProperty("userdirective", ResultDirective.class.getName());
         sharedRuntime.addProperty("userdirective", ChainDirective.class.getName());
         sharedRuntime.addProperty("userdirective", ChunkDirective.class.getName());

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java?view=diff&rev=546918&r1=546917&r2=546918
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java Wed Jun 13 08:20:57 2007
@@ -134,6 +134,26 @@
         assertEquals(33002, DataObjectUtils.intPKForObject(p));
     }
 
+    public void testBindObjectNotEqualShort() throws Exception {
+        createTestData("prepare");
+
+        ObjectContext context = createDataContext();
+
+        Artist a = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33002);
+
+        String template = "SELECT * FROM PAINTING "
+                + "WHERE #bindObjectNotEqual($a) ORDER BY PAINTING_ID";
+        SQLTemplate query = new SQLTemplate(Painting.class, template);
+        query.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+        query.setParameters(Collections.singletonMap("a", a));
+
+        List objects = context.performQuery(query);
+        assertEquals(1, objects.size());
+
+        Painting p = (Painting) objects.get(0);
+        assertEquals(33001, DataObjectUtils.intPKForObject(p));
+    }
+
     public void testBindObjectEqualFull() throws Exception {
         createTestData("prepare");
 
@@ -152,6 +172,26 @@
 
         Painting p = (Painting) objects.get(0);
         assertEquals(33002, DataObjectUtils.intPKForObject(p));
+    }
+
+    public void testBindObjectNotEqualFull() throws Exception {
+        createTestData("prepare");
+
+        ObjectContext context = createDataContext();
+
+        Artist a = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33002);
+
+        String template = "SELECT * FROM PAINTING t0"
+                + " WHERE #bindObjectNotEqual($a [ 't0.ARTIST_ID' ] [ 'ARTIST_ID' ] ) ORDER BY PAINTING_ID";
+        SQLTemplate query = new SQLTemplate(Painting.class, template);
+        query.setColumnNamesCapitalization(SQLTemplate.UPPERCASE_COLUMN_NAMES);
+        query.setParameters(Collections.singletonMap("a", a));
+
+        List objects = context.performQuery(query);
+        assertEquals(1, objects.size());
+
+        Painting p = (Painting) objects.get(0);
+        assertEquals(33001, DataObjectUtils.intPKForObject(p));
     }
 
     public void testFetchLimit() throws Exception {