cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r703705 - /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindDirective.java
Date Sat, 11 Oct 2008 18:39:09 GMT
Author: aadamchik
Date: Sat Oct 11 11:39:09 2008
New Revision: 703705

URL: http://svn.apache.org/viewvc?rev=703705&view=rev
Log:
CAY-1121 EJBQL collection binding

(fixed, tested on SQLServer)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindDirective.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindDirective.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindDirective.java?rev=703705&r1=703704&r2=703705&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindDirective.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/BindDirective.java
Sat Oct 11 11:39:09 2008
@@ -24,38 +24,44 @@
 import java.util.Collection;
 import java.util.Iterator;
 
+import org.apache.cayenne.dba.TypesMapping;
+import org.apache.cayenne.util.ConversionUtil;
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.ParseErrorException;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.runtime.directive.Directive;
 import org.apache.velocity.runtime.parser.node.Node;
-import org.apache.cayenne.dba.TypesMapping;
-import org.apache.cayenne.util.ConversionUtil;
 
 /**
- * A custom Velocity directive to create a PreparedStatement parameter text.
- * There are the following possible invocation formats inside the template:
+ * A custom Velocity directive to create a PreparedStatement parameter text. There are the
+ * following possible invocation formats inside the template:
  * 
  * <pre>
  * #bind(value) - e.g. #bind($xyz)
  * #bind(value jdbc_type_name) - e.g. #bind($xyz 'VARCHAR'). This is the most common and
useful form.
- * #bind(value jdbc_type_name, precision) - e.g. #bind($xyz 'VARCHAR' 2)</pre>
- * 
- * 
- * <p>Other examples:</p>
- * 
- * <p><strong>Binding literal parameter value:</strong></p>
- * <p><code>"WHERE SOME_COLUMN > #bind($xyz)"</code> produces 
- * <code>"WHERE SOME_COLUMN > ?"</code>
- * and also places the value of the "xyz" parameter in the context "bindings" collection.</p>
- * 
- * <p><strong>Binding ID column of a DataObject value:</strong></p>
- * <p><code>"WHERE ID_COL1 = #bind($helper.cayenneExp($xyz, 'db:ID_COL2')) 
- * AND ID_COL2 = #bind($helper.cayenneExp($xyz, 'db:ID_COL2'))"</code> 
- * produces <code>"WHERE ID_COL1 = ? AND ID_COL2 = ?"</code>
- * and also places the values of id columns of the DataObject parameter  "xyz" in the context

- * "bindings" collection.</p>
+ * #bind(value jdbc_type_name, precision) - e.g. #bind($xyz 'VARCHAR' 2)
+ * </pre>
+ * <p>
+ * Other examples:
+ * </p>
+ * <p>
+ * <strong>Binding literal parameter value:</strong>
+ * </p>
+ * <p>
+ * <code>"WHERE SOME_COLUMN > #bind($xyz)"</code> produces
+ * <code>"WHERE SOME_COLUMN > ?"</code> and also places the value of the "xyz"
parameter
+ * in the context "bindings" collection.
+ * </p>
+ * <p>
+ * <strong>Binding ID column of a DataObject value:</strong>
+ * </p>
+ * <p>
+ * <code>"WHERE ID_COL1 = #bind($helper.cayenneExp($xyz, 'db:ID_COL2')) 
+ * AND ID_COL2 = #bind($helper.cayenneExp($xyz, 'db:ID_COL2'))"</code> produces <code>"WHERE
ID_COL1 = ? AND ID_COL2 = ?"</code> and also places the
+ * values of id columns of the DataObject parameter "xyz" in the context "bindings"
+ * collection.
+ * </p>
  * 
  * @since 1.1
  * @author Andrus Adamchik
@@ -73,99 +79,95 @@
     }
 
     /**
-     * Extracts the value of the object property to render and passes
-     * control to {@link #render(InternalContextAdapter, Writer, ParameterBinding)} 
-     * to do the actual rendering.
+     * Extracts the value of the object property to render and passes control to
+     * {@link #render(InternalContextAdapter, Writer, ParameterBinding)} to do the actual
+     * rendering.
      */
     @Override
     public boolean render(InternalContextAdapter context, Writer writer, Node node)
-        throws
-            IOException,
-            ResourceNotFoundException,
-            ParseErrorException,
+            throws IOException, ResourceNotFoundException, ParseErrorException,
             MethodInvocationException {
 
         Object value = getChild(context, node, 0);
         Object type = getChild(context, node, 1);
-        Object precision = getChild(context, node, 2);
+        int precision = ConversionUtil.toInt(getChild(context, node, 2), -1);
+        String typeString = type != null ? type.toString() : null;
+
+        if (value instanceof Collection) {
+            Iterator<?> it = ((Collection) value).iterator();
+            while (it.hasNext()) {
+                render(context, writer, node, it.next(), typeString, precision);
+
+                if (it.hasNext()) {
+                    writer.write(',');
+                }
+            }
+        }
+        else {
+            render(context, writer, node, value, typeString, precision);
+        }
+
+        return true;
+    }
+
+    /**
+     * @since 3.0
+     */
+    protected void render(
+            InternalContextAdapter context,
+            Writer writer,
+            Node node,
+            Object value,
+            String typeString,
+            int precision) throws IOException, ParseErrorException {
 
         int jdbcType = TypesMapping.NOT_DEFINED;
-        if (type != null) {
-            jdbcType = TypesMapping.getSqlTypeByName(type.toString());
+        if (typeString != null) {
+            jdbcType = TypesMapping.getSqlTypeByName(typeString);
         }
         else if (value != null) {
             jdbcType = TypesMapping.getSqlTypeByJava(value.getClass());
         }
 
         if (jdbcType == TypesMapping.NOT_DEFINED) {
-            throw new ParseErrorException(
-                "Can't determine JDBC type of binding ("
+            throw new ParseErrorException("Can't determine JDBC type of binding ("
                     + value
                     + ", "
-                    + type
+                    + typeString
                     + ") at line "
                     + node.getLine()
                     + ", column "
                     + node.getColumn());
         }
 
-        ParameterBinding binding =
-            new ParameterBinding(value, jdbcType, ConversionUtil.toInt(precision, -1));
-
-        render(context, writer, binding);
-        return true;
+        render(context, writer, new ParameterBinding(value, jdbcType, precision));
     }
 
     protected void render(
-        InternalContextAdapter context,
-        Writer writer,
-        ParameterBinding binding)
-        throws IOException {
+            InternalContextAdapter context,
+            Writer writer,
+            ParameterBinding binding) throws IOException {
 
         bind(context, binding);
-        
-        if (binding.getValue() instanceof Collection) {
-            Collection bindingList = (Collection) binding.getValue();
-            for (Iterator bindingIter = bindingList.iterator(); bindingIter.hasNext(); )
{
-                
-                bindingIter.next();
-                writer.write('?');
-                
-                if (bindingIter.hasNext()) {
-                    writer.write(',');
-                }
-                
-            }
-        } else {
-            writer.write('?');
-        }
+        writer.write('?');
     }
 
     protected Object getChild(InternalContextAdapter context, Node node, int i)
-        throws MethodInvocationException {
-        return (i >= 0 && i < node.jjtGetNumChildren())
-            ? node.jjtGetChild(i).value(context)
-            : null;
+            throws MethodInvocationException {
+        return (i >= 0 && i < node.jjtGetNumChildren()) ? node.jjtGetChild(i).value(
+                context) : null;
     }
 
     /**
      * Adds value to the list of bindings in the context.
      */
     protected void bind(InternalContextAdapter context, ParameterBinding binding) {
-        Collection bindings =
-            (Collection) context.getInternalUserContext().get(
+
+        Collection bindings = (Collection) context.getInternalUserContext().get(
                 SQLTemplateProcessor.BINDINGS_LIST_KEY);
 
         if (bindings != null) {
-            
-            if (binding.getValue() instanceof Collection) {
-                Collection bindingList = (Collection) binding.getValue();
-                for (Iterator bindingIter = bindingList.iterator(); bindingIter.hasNext();
) {
-                    bindings.add(new ParameterBinding(bindingIter.next(), binding.getJdbcType(),
binding.getPrecision()));
-                }
-            } else {
-                bindings.add(binding);
-            }
+            bindings.add(binding);
         }
     }
 }



Mime
View raw message