cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r653788 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/trans/ main/java/org/apache/cayenne/dba/db2/ main/java/org/apache/cayenne/dba/openbase/ main/java/org/apache/cayenne/dba/postgres...
Date Tue, 06 May 2008 14:33:23 GMT
Author: aadamchik
Date: Tue May  6 07:33:22 2008
New Revision: 653788

URL: http://svn.apache.org/viewvc?rev=653788&view=rev
Log:
CAY-1049 Refactor QueryAssembler/SelectTranslator

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/OrderingTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/TrimmingQualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/db2/DB2QualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/openbase/OpenBaseQualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresQualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/OrderingTranslatorTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/QualifierTranslatorTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java Tue May  6 07:33:22 2008
@@ -61,9 +61,10 @@
         queryBuf.append(dbEnt.getFullyQualifiedName());
 
         // 2. build qualifier
-        String qualifierStr = adapter.getQualifierTranslator(this).doTranslation();
-        if (qualifierStr != null)
-            queryBuf.append(" WHERE ").append(qualifierStr);
+        StringBuilder qualifier = new StringBuilder();
+        adapter.getQualifierTranslator(this).appendPart(qualifier);
+        if (qualifier.length() > 0)
+            queryBuf.append(" WHERE ").append(qualifier);
 
         return queryBuf.toString();
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/OrderingTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/OrderingTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/OrderingTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/OrderingTranslator.java Tue May  6 07:33:22 2008
@@ -19,7 +19,9 @@
 
 package org.apache.cayenne.access.trans;
 
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 import org.apache.cayenne.CayenneRuntimeException;
@@ -28,82 +30,80 @@
 import org.apache.cayenne.query.Query;
 import org.apache.cayenne.query.SelectQuery;
 
-/** 
- * Translates query ordering to SQL. 
- * 
- * @author Andrus Adamchik
- * @author Craig Miskell
+/**
+ * Translates query ordering to SQL.
  */
 public class OrderingTranslator extends QueryAssemblerHelper {
 
     protected List<String> orderByColumnList = new ArrayList<String>();
-    
+
     public OrderingTranslator(QueryAssembler queryAssembler) {
         super(queryAssembler);
     }
 
-    /** Translates query Ordering list to SQL ORDER BY clause. 
-     *  Ordering list is obtained from <code>queryAssembler</code>'s query object. 
-     *  In a process of building of ORDER BY clause, <code>queryAssembler</code> 
-     *  is notified when a join needs to be added. */
+    /**
+     * Translates query Ordering list to SQL ORDER BY clause. Ordering list is obtained
+     * from <code>queryAssembler</code>'s query object. In a process of building of
+     * ORDER BY clause, <code>queryAssembler</code> is notified when a join needs to be
+     * added.
+     * 
+     * @since 3.0
+     */
     @Override
-    public String doTranslation() {
+    protected void doAppendPart() throws IOException {
+
         Query q = queryAssembler.getQuery();
 
         // only select queries can have ordering...
-        if (q == null || !(q instanceof SelectQuery))
-            return null;
+        if (q == null || !(q instanceof SelectQuery)) {
+            return;
+        }
+
+        Iterator<Ordering> it = ((SelectQuery) q).getOrderings().iterator();
 
-        StringBuilder buf = new StringBuilder();
+        while (it.hasNext()) {
+            Ordering ord = it.next();
 
-        for (Ordering ord : ((SelectQuery) q).getOrderings()) {
-            if (buf.length() > 0)
-                buf.append(", ");
-
-            StringBuffer ordComp = new StringBuffer();
-            
-            //UPPER is (I think) part of the SQL99 standard, and I'm not convinced it's universally available
-            // - should the syntax used here be defined by the Db specific adaptor perhaps, or at least
-            // possibly specified by the db adaptor (a DB specific OrderingTranslator hook)?
             if (ord.isCaseInsensitive()) {
-                ordComp.append("UPPER(");
+                out.append("UPPER(");
             }
 
             Expression exp = ord.getSortSpec();
 
             if (exp.getType() == Expression.OBJ_PATH) {
-                appendObjPath(ordComp, exp);
-            } else if (exp.getType() == Expression.DB_PATH) {
-                appendDbPath(ordComp, exp);
-            } else {
-                throw new CayenneRuntimeException(
-                    "Unsupported ordering expression: " + exp);
+                appendObjPath(exp);
+            }
+            else if (exp.getType() == Expression.DB_PATH) {
+                appendDbPath(exp);
+            }
+            else {
+                throw new CayenneRuntimeException("Unsupported ordering expression: "
+                        + exp);
             }
 
-            //Close UPPER() modifier
+            // Close UPPER() modifier
             if (ord.isCaseInsensitive()) {
-                ordComp.append(")");
+                out.append(")");
             }
 
-            orderByColumnList.add(ordComp.toString());
-            
-            buf.append(ordComp.toString());
+            orderByColumnList.add(out.toString());
 
-            // "ASC" is a noop, omit it from the query 
+            // "ASC" is a noop, omit it from the query
             if (!ord.isAscending()) {
-                buf.append(" DESC");
+                out.append(" DESC");
             }
-        }
 
-        return buf.length() > 0 ? buf.toString() : null;
+            if (it.hasNext()) {
+                out.append(", ");
+            }
+        }
     }
 
     /**
-     * Returns the column expressions (not Expressions) used in
-     * the order by clause.  E.g., in the case of an case-insensitive 
-     * order by, an element of the list would be 
+     * Returns the column expressions (not Expressions) used in the order by clause. E.g.,
+     * in the case of an case-insensitive order by, an element of the list would be
      * <code>UPPER(&lt;column reference&gt;)</code>
-     */    
+     */
     public List<String> getOrderByColumnList() {
         return orderByColumnList;
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java Tue May  6 07:33:22 2008
@@ -19,9 +19,11 @@
 
 package org.apache.cayenne.access.trans;
 
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.ObjectId;
 import org.apache.cayenne.Persistent;
 import org.apache.cayenne.exp.Expression;
@@ -42,36 +44,27 @@
  */
 public class QualifierTranslator extends QueryAssemblerHelper implements TraversalHandler {
 
-    protected StringBuffer qualBuf = new StringBuffer();
-
     protected DataObjectMatchTranslator objectMatchTranslator;
     protected boolean matchingObject;
 
-    public QualifierTranslator() {
-        this(null);
-    }
-
     public QualifierTranslator(QueryAssembler queryAssembler) {
         super(queryAssembler);
     }
 
     /**
-     * Translates query qualifier to SQL WHERE clause. Qualifier is obtained from
-     * <code>queryAssembler</code> object.
+     * Translates query qualifier to SQL WHERE clause. Qualifier is obtained from the
+     * parent queryAssembler.
+     * 
+     * @since 3.0
      */
     @Override
-    public String doTranslation() {
-        qualBuf.setLength(0);
-
+    protected void doAppendPart() throws IOException {
         Expression rootNode = extractQualifier();
         if (rootNode == null) {
-            return null;
+            return;
         }
 
-        // build SQL where clause string based on expression
-        // (using '?' for object values)
         rootNode.traverse(this);
-        return qualBuf.length() > 0 ? qualBuf.toString() : null;
     }
 
     protected Expression extractQualifier() {
@@ -132,7 +125,7 @@
         }
     }
 
-    protected void appendObjectMatch() {
+    protected void appendObjectMatch() throws IOException {
         if (!matchingObject || objectMatchTranslator == null) {
             throw new IllegalStateException("An invalid attempt to append object match.");
         }
@@ -154,117 +147,121 @@
                 first = false;
             }
             else {
-                qualBuf.append(" AND ");
+                out.append(" AND ");
             }
 
             String key = it.next();
             DbAttribute attr = objectMatchTranslator.getAttribute(key);
             Object val = objectMatchTranslator.getValue(key);
 
-            processColumn(qualBuf, attr);
-            qualBuf.append(objectMatchTranslator.getOperation());
-            appendLiteral(qualBuf, val, attr, objectMatchTranslator.getExpression());
+            processColumn(attr);
+            out.append(objectMatchTranslator.getOperation());
+            appendLiteral(val, attr, objectMatchTranslator.getExpression());
         }
 
         objectMatchTranslator.reset();
     }
 
-    /** Opportunity to insert an operation */
     public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) {
 
         if (!hasMoreChildren) {
             return;
         }
 
-        StringBuffer buf = (matchingObject) ? new StringBuffer() : qualBuf;
+        Appendable out = (matchingObject) ? new StringBuilder() : this.out;
 
-        switch (node.getType()) {
-            case Expression.AND:
-                buf.append(" AND ");
-                break;
-            case Expression.OR:
-                buf.append(" OR ");
-                break;
-            case Expression.EQUAL_TO:
-                // translate NULL as IS NULL
-                if (childIndex == 0
-                        && node.getOperandCount() == 2
-                        && node.getOperand(1) == null) {
-                    buf.append(" IS ");
-                }
-                else {
-                    buf.append(" = ");
-                }
-                break;
-            case Expression.NOT_EQUAL_TO:
-                // translate NULL as IS NOT NULL
-                if (childIndex == 0
-                        && node.getOperandCount() == 2
-                        && node.getOperand(1) == null) {
-                    buf.append(" IS NOT ");
-                }
-                else {
-                    buf.append(" <> ");
-                }
-                break;
-            case Expression.LESS_THAN:
-                buf.append(" < ");
-                break;
-            case Expression.GREATER_THAN:
-                buf.append(" > ");
-                break;
-            case Expression.LESS_THAN_EQUAL_TO:
-                buf.append(" <= ");
-                break;
-            case Expression.GREATER_THAN_EQUAL_TO:
-                buf.append(" >= ");
-                break;
-            case Expression.IN:
-                buf.append(" IN ");
-                break;
-            case Expression.NOT_IN:
-                buf.append(" NOT IN ");
-                break;
-            case Expression.LIKE:
-                buf.append(" LIKE ");
-                break;
-            case Expression.NOT_LIKE:
-                buf.append(" NOT LIKE ");
-                break;
-            case Expression.LIKE_IGNORE_CASE:
-                buf.append(") LIKE UPPER(");
-                break;
-            case Expression.NOT_LIKE_IGNORE_CASE:
-                buf.append(") NOT LIKE UPPER(");
-                break;
-            case Expression.ADD:
-                buf.append(" + ");
-                break;
-            case Expression.SUBTRACT:
-                buf.append(" - ");
-                break;
-            case Expression.MULTIPLY:
-                buf.append(" * ");
-                break;
-            case Expression.DIVIDE:
-                buf.append(" / ");
-                break;
-            case Expression.BETWEEN:
-                if (childIndex == 0)
-                    buf.append(" BETWEEN ");
-                else if (childIndex == 1)
-                    buf.append(" AND ");
-                break;
-            case Expression.NOT_BETWEEN:
-                if (childIndex == 0)
-                    buf.append(" NOT BETWEEN ");
-                else if (childIndex == 1)
-                    buf.append(" AND ");
-                break;
+        try {
+            switch (node.getType()) {
+                case Expression.AND:
+                    out.append(" AND ");
+                    break;
+                case Expression.OR:
+                    out.append(" OR ");
+                    break;
+                case Expression.EQUAL_TO:
+                    // translate NULL as IS NULL
+                    if (childIndex == 0
+                            && node.getOperandCount() == 2
+                            && node.getOperand(1) == null) {
+                        out.append(" IS ");
+                    }
+                    else {
+                        out.append(" = ");
+                    }
+                    break;
+                case Expression.NOT_EQUAL_TO:
+                    // translate NULL as IS NOT NULL
+                    if (childIndex == 0
+                            && node.getOperandCount() == 2
+                            && node.getOperand(1) == null) {
+                        out.append(" IS NOT ");
+                    }
+                    else {
+                        out.append(" <> ");
+                    }
+                    break;
+                case Expression.LESS_THAN:
+                    out.append(" < ");
+                    break;
+                case Expression.GREATER_THAN:
+                    out.append(" > ");
+                    break;
+                case Expression.LESS_THAN_EQUAL_TO:
+                    out.append(" <= ");
+                    break;
+                case Expression.GREATER_THAN_EQUAL_TO:
+                    out.append(" >= ");
+                    break;
+                case Expression.IN:
+                    out.append(" IN ");
+                    break;
+                case Expression.NOT_IN:
+                    out.append(" NOT IN ");
+                    break;
+                case Expression.LIKE:
+                    out.append(" LIKE ");
+                    break;
+                case Expression.NOT_LIKE:
+                    out.append(" NOT LIKE ");
+                    break;
+                case Expression.LIKE_IGNORE_CASE:
+                    out.append(") LIKE UPPER(");
+                    break;
+                case Expression.NOT_LIKE_IGNORE_CASE:
+                    out.append(") NOT LIKE UPPER(");
+                    break;
+                case Expression.ADD:
+                    out.append(" + ");
+                    break;
+                case Expression.SUBTRACT:
+                    out.append(" - ");
+                    break;
+                case Expression.MULTIPLY:
+                    out.append(" * ");
+                    break;
+                case Expression.DIVIDE:
+                    out.append(" / ");
+                    break;
+                case Expression.BETWEEN:
+                    if (childIndex == 0)
+                        out.append(" BETWEEN ");
+                    else if (childIndex == 1)
+                        out.append(" AND ");
+                    break;
+                case Expression.NOT_BETWEEN:
+                    if (childIndex == 0)
+                        out.append(" NOT BETWEEN ");
+                    else if (childIndex == 1)
+                        out.append(" AND ");
+                    break;
+            }
+        }
+        catch (IOException ioex) {
+            throw new CayenneRuntimeException("Error appending content", ioex);
         }
 
         if (matchingObject) {
-            objectMatchTranslator.setOperation(buf.toString());
+            objectMatchTranslator.setOperation(out.toString());
             objectMatchTranslator.setExpression(node);
         }
     }
@@ -277,32 +274,38 @@
             detectObjectMatch(node);
         }
 
-        if (parenthesisNeeded(node, parentNode)) {
-            qualBuf.append('(');
-        }
+        try {
 
-        if (count == 0) {
-            // not all databases handle true/false
-            if (node.getType() == Expression.TRUE) {
-                qualBuf.append("1 = 1");
+            if (parenthesisNeeded(node, parentNode)) {
+                out.append('(');
             }
-            if (node.getType() == Expression.FALSE) {
-                qualBuf.append("1 = 0");
+
+            if (count == 0) {
+                // not all databases handle true/false
+                if (node.getType() == Expression.TRUE) {
+                    out.append("1 = 1");
+                }
+                if (node.getType() == Expression.FALSE) {
+                    out.append("1 = 0");
+                }
             }
-        }
 
-        if (count == 1) {
-            if (node.getType() == Expression.NEGATIVE)
-                qualBuf.append('-');
-            // ignore POSITIVE - it is a NOOP
-            // else if(node.getType() == Expression.POSITIVE)
-            // qualBuf.append('+');
-            else if (node.getType() == Expression.NOT)
-                qualBuf.append("NOT ");
+            if (count == 1) {
+                if (node.getType() == Expression.NEGATIVE)
+                    out.append('-');
+                // ignore POSITIVE - it is a NOOP
+                // else if(node.getType() == Expression.POSITIVE)
+                // qualBuf.append('+');
+                else if (node.getType() == Expression.NOT)
+                    out.append("NOT ");
+            }
+            else if (node.getType() == Expression.LIKE_IGNORE_CASE
+                    || node.getType() == Expression.NOT_LIKE_IGNORE_CASE) {
+                out.append("UPPER(");
+            }
         }
-        else if (node.getType() == Expression.LIKE_IGNORE_CASE
-                || node.getType() == Expression.NOT_LIKE_IGNORE_CASE) {
-            qualBuf.append("UPPER(");
+        catch (IOException ioex) {
+            throw new CayenneRuntimeException("Error appending content", ioex);
         }
     }
 
@@ -311,33 +314,45 @@
      */
     public void endNode(Expression node, Expression parentNode) {
 
-        // check if we need to use objectMatchTranslator to finish building the expression
-        if (node.getOperandCount() == 2 && matchingObject) {
-            appendObjectMatch();
-        }
+        try {
+            // check if we need to use objectMatchTranslator to finish building the
+            // expression
+            if (node.getOperandCount() == 2 && matchingObject) {
+                appendObjectMatch();
+            }
 
-        if (parenthesisNeeded(node, parentNode)) {
-            qualBuf.append(')');
-        }
+            if (parenthesisNeeded(node, parentNode)) {
+                out.append(')');
+            }
 
-        if (node.getType() == Expression.LIKE_IGNORE_CASE
-                || node.getType() == Expression.NOT_LIKE_IGNORE_CASE) {
-            qualBuf.append(')');
+            if (node.getType() == Expression.LIKE_IGNORE_CASE
+                    || node.getType() == Expression.NOT_LIKE_IGNORE_CASE) {
+                out.append(')');
+            }
+        }
+        catch (IOException ioex) {
+            throw new CayenneRuntimeException("Error appending content", ioex);
         }
     }
 
     public void objectNode(Object leaf, Expression parentNode) {
-        if (parentNode.getType() == Expression.OBJ_PATH) {
-            appendObjPath(qualBuf, parentNode);
-        }
-        else if (parentNode.getType() == Expression.DB_PATH) {
-            appendDbPath(qualBuf, parentNode);
-        }
-        else if (parentNode.getType() == Expression.LIST) {
-            appendList(parentNode, paramsDbType(parentNode));
+
+        try {
+            if (parentNode.getType() == Expression.OBJ_PATH) {
+                appendObjPath(parentNode);
+            }
+            else if (parentNode.getType() == Expression.DB_PATH) {
+                appendDbPath(parentNode);
+            }
+            else if (parentNode.getType() == Expression.LIST) {
+                appendList(parentNode, paramsDbType(parentNode));
+            }
+            else {
+                appendLiteral(leaf, paramsDbType(parentNode), parentNode);
+            }
         }
-        else {
-            appendLiteral(qualBuf, leaf, paramsDbType(parentNode), parentNode);
+        catch (IOException ioex) {
+            throw new CayenneRuntimeException("Error appending content", ioex);
         }
     }
 
@@ -358,7 +373,8 @@
         return true;
     }
 
-    private final void appendList(Expression listExpr, DbAttribute paramDesc) {
+    private final void appendList(Expression listExpr, DbAttribute paramDesc)
+            throws IOException {
         Iterator<?> it = null;
         Object list = listExpr.getOperand(0);
         if (list instanceof List) {
@@ -376,25 +392,22 @@
         // process first element outside the loop
         // (unroll loop to avoid condition checking
         if (it.hasNext())
-            appendLiteral(qualBuf, it.next(), paramDesc, listExpr);
+            appendLiteral(it.next(), paramDesc, listExpr);
         else
             return;
 
         while (it.hasNext()) {
-            qualBuf.append(", ");
-            appendLiteral(qualBuf, it.next(), paramDesc, listExpr);
+            out.append(", ");
+            appendLiteral(it.next(), paramDesc, listExpr);
         }
     }
 
     @Override
-    protected void appendLiteral(
-            StringBuffer buf,
-            Object val,
-            DbAttribute attr,
-            Expression parentExpression) {
+    protected void appendLiteral(Object val, DbAttribute attr, Expression parentExpression)
+            throws IOException {
 
         if (!matchingObject) {
-            super.appendLiteral(buf, val, attr, parentExpression);
+            super.appendLiteral(val, attr, parentExpression);
         }
         else if (val == null || (val instanceof Persistent)) {
             objectMatchTranslator.setDataObject((Persistent) val);
@@ -409,13 +422,11 @@
     }
 
     @Override
-    protected void processRelTermination(
-            StringBuffer buf,
-            DbRelationship rel,
-            JoinType joinType) {
+    protected void processRelTermination(DbRelationship rel, JoinType joinType)
+            throws IOException {
 
         if (!matchingObject) {
-            super.processRelTermination(buf, rel, joinType);
+            super.processRelTermination(rel, joinType);
         }
         else {
             if (rel.isToMany()) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java Tue May  6 07:33:22 2008
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne.access.trans;
 
+import java.io.IOException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -48,46 +49,35 @@
 public abstract class QueryAssemblerHelper {
 
     protected QueryAssembler queryAssembler;
+    protected Appendable out;
 
-    public QueryAssemblerHelper() {
-    }
-
-    /** Creates QueryAssemblerHelper. Sets queryAssembler property. */
+    /**
+     * Creates QueryAssemblerHelper initializaing with parent {@link QueryAssembler} and
+     * output buffer object.
+     */
     public QueryAssemblerHelper(QueryAssembler queryAssembler) {
         this.queryAssembler = queryAssembler;
     }
 
-    /** Returns parent QueryAssembler that uses this helper. */
-    public QueryAssembler getQueryAssembler() {
-        return queryAssembler;
-    }
-
-    public void setQueryAssembler(QueryAssembler queryAssembler) {
-        this.queryAssembler = queryAssembler;
-    }
-
-    /**
-     * Translates the part of parent translator's query that is supported by this
-     * PartTranslator. For example, QualifierTranslator will process qualifier expression,
-     * OrderingTranslator - ordering of the query. In the process of translation parent
-     * translator is notified of any join tables added (so that it can update its "FROM"
-     * clause). Also parent translator is consulted about table aliases to use when
-     * translating columns.
-     */
-    public abstract String doTranslation();
-
     public ObjEntity getObjEntity() {
-        return getQueryAssembler().getRootEntity();
+        return queryAssembler.getRootEntity();
     }
 
     public DbEntity getDbEntity() {
-        return getQueryAssembler().getRootDbEntity();
+        return queryAssembler.getRootDbEntity();
+    }
+    
+    public void appendPart(Appendable out) throws IOException {
+        this.out = out;
+        doAppendPart();
     }
+    
+    protected abstract void doAppendPart() throws IOException;
 
     /**
      * Processes parts of the OBJ_PATH expression.
      */
-    protected void appendObjPath(StringBuffer buf, Expression pathExp) {
+    protected void appendObjPath(Expression pathExp) throws IOException {
 
         queryAssembler.resetJoinStack();
 
@@ -102,7 +92,7 @@
                 // if this is a last relationship in the path,
                 // it needs special handling
                 if (component.isLast()) {
-                    processRelTermination(buf, relationship, component.getJoinType());
+                    processRelTermination(relationship, component.getJoinType());
                 }
                 else {
                     // find and add joins ....
@@ -127,7 +117,7 @@
                                 JoinType.INNER);
                     }
                     else if (pathPart instanceof DbAttribute) {
-                        processColumn(buf, (DbAttribute) pathPart);
+                        processColumn((DbAttribute) pathPart);
                     }
                 }
 
@@ -135,7 +125,7 @@
         }
     }
 
-    protected void appendDbPath(StringBuffer buf, Expression pathExp) {
+    protected void appendDbPath(Expression pathExp) throws IOException {
 
         queryAssembler.resetJoinStack();
 
@@ -149,7 +139,7 @@
                 // if this is a last relationship in the path,
                 // it needs special handling
                 if (component.isLast()) {
-                    processRelTermination(buf, relationship, component.getJoinType());
+                    processRelTermination(relationship, component.getJoinType());
                 }
                 else {
                     // find and add joins ....
@@ -158,16 +148,16 @@
                 }
             }
             else {
-                processColumn(buf, component.getAttribute());
+                processColumn(component.getAttribute());
             }
         }
     }
 
-    protected void processColumn(StringBuffer buf, DbAttribute dbAttr) {
+    protected void processColumn(DbAttribute dbAttr) throws IOException {
         String alias = (queryAssembler.supportsTableAliases()) ? queryAssembler
                 .getCurrentAlias() : null;
 
-        buf.append(dbAttr.getAliasedName(alias));
+        out.append(dbAttr.getAliasedName(alias));
     }
 
     /**
@@ -181,19 +171,16 @@
      * If <code>val</code> is a DataObject, its primary key value is used as a
      * parameter. <i>Only objects with a single column primary key can be used.</i>
      * 
-     * @param buf query buffer.
      * @param val object that should be appended as a literal to the query. Must be of one
      *            of "standard JDBC" types, null or a DataObject.
      * @param attr DbAttribute that has information on what type of parameter is being
      *            appended.
      */
-    protected void appendLiteral(
-            StringBuffer buf,
-            Object val,
-            DbAttribute attr,
-            Expression parentExpression) {
+    protected void appendLiteral(Object val, DbAttribute attr, Expression parentExpression)
+            throws IOException {
+
         if (val == null) {
-            buf.append("NULL");
+            out.append("NULL");
         }
         else if (val instanceof Persistent) {
             ObjectId id = ((Persistent) val).getObjectId();
@@ -225,13 +212,12 @@
 
             // checks have been passed, use id value
             appendLiteralDirect(
-                    buf,
                     snap.get(snap.keySet().iterator().next()),
                     attr,
                     parentExpression);
         }
         else {
-            appendLiteralDirect(buf, val, attr, parentExpression);
+            appendLiteralDirect(val, attr, parentExpression);
         }
     }
 
@@ -245,11 +231,10 @@
      *            of "standard JDBC" types. Can not be null.
      */
     protected void appendLiteralDirect(
-            StringBuffer buf,
             Object val,
             DbAttribute attr,
-            Expression parentExpression) {
-        buf.append('?');
+            Expression parentExpression) throws IOException {
+        out.append('?');
 
         // we are hoping that when processing parameter list,
         // the correct type will be
@@ -355,10 +340,9 @@
      * 
      * @since 3.0
      */
-    protected void processRelTermination(
-            StringBuffer buf,
-            ObjRelationship rel,
-            JoinType joinType) {
+    protected void processRelTermination(ObjRelationship rel, JoinType joinType)
+            throws IOException {
+
         Iterator<DbRelationship> dbRels = rel.getDbRelationships().iterator();
 
         // scan DbRelationships
@@ -368,7 +352,7 @@
             // if this is a last relationship in the path,
             // it needs special handling
             if (!dbRels.hasNext()) {
-                processRelTermination(buf, dbRel, joinType);
+                processRelTermination(dbRel, joinType);
             }
             else {
                 // find and add joins ....
@@ -385,10 +369,8 @@
      * 
      * @since 3.0
      */
-    protected void processRelTermination(
-            StringBuffer buf,
-            DbRelationship rel,
-            JoinType joinType) {
+    protected void processRelTermination(DbRelationship rel, JoinType joinType)
+            throws IOException {
         if (rel.isToMany()) {
             // append joins
             queryAssembler.dbRelationshipAdded(rel, joinType);
@@ -433,6 +415,6 @@
             attribute = join.getSource();
         }
 
-        processColumn(buf, attribute);
+        processColumn(attribute);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java Tue May  6 07:33:22 2008
@@ -102,14 +102,14 @@
         // build column list
         this.resultColumns = buildResultColumns();
 
-        QualifierTranslator tr = adapter.getQualifierTranslator(this);
-
         // build qualifier
-        String qualifierStr = tr.doTranslation();
+        StringBuilder qualifierBuffer = new StringBuilder();
+        adapter.getQualifierTranslator(this).appendPart(qualifierBuffer);
 
         // build ORDER BY
         OrderingTranslator orderingTranslator = new OrderingTranslator(this);
-        String orderByStr = orderingTranslator.doTranslation();
+        StringBuilder orderingBuffer = new StringBuilder();
+        orderingTranslator.appendPart(orderingBuffer);
 
         // assemble
         StringBuilder queryBuf = new StringBuilder();
@@ -168,14 +168,14 @@
         joinStack.appendJoins(queryBuf);
 
         // append qualifier
-        if (qualifierStr != null) {
+        if (qualifierBuffer.length() > 0) {
             queryBuf.append(" WHERE ");
-            queryBuf.append(qualifierStr);
+            queryBuf.append(qualifierBuffer);
         }
 
         // append prebuilt ordering
-        if (orderByStr != null) {
-            queryBuf.append(" ORDER BY ").append(orderByStr);
+        if (orderingBuffer.length() > 0) {
+            queryBuf.append(" ORDER BY ").append(orderingBuffer);
         }
 
         return queryBuf.toString();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/TrimmingQualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/TrimmingQualifierTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/TrimmingQualifierTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/TrimmingQualifierTranslator.java Tue May  6 07:33:22 2008
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne.access.trans;
 
+import java.io.IOException;
 import java.sql.Types;
 
 import org.apache.cayenne.map.DbAttribute;
@@ -36,13 +37,6 @@
     /**
      * Constructor for TrimmingQualifierTranslator.
      */
-    protected TrimmingQualifierTranslator() {
-        super();
-    }
-
-    /**
-     * Constructor for TrimmingQualifierTranslator.
-     */
     public TrimmingQualifierTranslator(QueryAssembler queryAssembler, String trimFunction) {
         super(queryAssembler);
         this.trimFunction = trimFunction;
@@ -52,14 +46,14 @@
      * Adds special handling of CHAR columns.
      */
     @Override
-    protected void processColumn(StringBuffer buf, DbAttribute dbAttr) {
+    protected void processColumn(DbAttribute dbAttr) throws IOException {
         if (dbAttr.getType() == Types.CHAR) {
-            buf.append(trimFunction).append("(");
-            super.processColumn(buf, dbAttr);
-            buf.append(')');
+            out.append(trimFunction).append("(");
+            super.processColumn(dbAttr);
+            out.append(')');
         }
         else {
-            super.processColumn(buf, dbAttr);
+            super.processColumn(dbAttr);
         }
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java Tue May  6 07:33:22 2008
@@ -67,9 +67,10 @@
         buildSetClause(queryBuf, (UpdateQuery) query);
 
         // 3. build qualifier
-        String qualifierStr = adapter.getQualifierTranslator(this).doTranslation();
-        if (qualifierStr != null)
-            queryBuf.append(" WHERE ").append(qualifierStr);
+        StringBuilder qualifier = new StringBuilder();
+        adapter.getQualifierTranslator(this).appendPart(qualifier);
+        if (qualifier.length() > 0)
+            queryBuf.append(" WHERE ").append(qualifier);
 
         return queryBuf.toString();
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/db2/DB2QualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/db2/DB2QualifierTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/db2/DB2QualifierTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/db2/DB2QualifierTranslator.java Tue May  6 07:33:22 2008
@@ -17,9 +17,9 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.dba.db2;
 
+import java.io.IOException;
 import java.sql.Types;
 
 import org.apache.cayenne.CayenneRuntimeException;
@@ -34,41 +34,32 @@
  */
 public class DB2QualifierTranslator extends TrimmingQualifierTranslator {
 
-    public DB2QualifierTranslator() {
-        super();
-    }
-
-    public DB2QualifierTranslator(
-        QueryAssembler queryAssembler,
-        String trimFunction) {
+    public DB2QualifierTranslator(QueryAssembler queryAssembler, String trimFunction) {
         super(queryAssembler, trimFunction);
     }
 
     @Override
     protected void appendLiteralDirect(
-        StringBuffer buf,
-        Object val,
-        DbAttribute attr,
-        Expression parentExpression) {
+            Object val,
+            DbAttribute attr,
+            Expression parentExpression) throws IOException {
 
         boolean castNeeded = false;
 
         if (parentExpression != null) {
             int type = parentExpression.getType();
 
-            castNeeded =
-                attr != null
+            castNeeded = attr != null
                     && (type == Expression.LIKE
-                        || type == Expression.LIKE_IGNORE_CASE
-                        || type == Expression.NOT_LIKE
-                        || type == Expression.NOT_LIKE_IGNORE_CASE);
+                            || type == Expression.LIKE_IGNORE_CASE
+                            || type == Expression.NOT_LIKE || type == Expression.NOT_LIKE_IGNORE_CASE);
         }
 
         if (castNeeded) {
-            buf.append("CAST (");
+            out.append("CAST (");
         }
 
-        super.appendLiteralDirect(buf, val, attr, parentExpression);
+        super.appendLiteralDirect(val, attr, parentExpression);
 
         if (castNeeded) {
             int jdbcType = attr.getType();
@@ -77,7 +68,7 @@
             // determine CAST type
 
             // LIKE on CHAR may produce unpredictible results
-            // LIKE on LONVARCHAR doesn't seem to be supported 
+            // LIKE on LONVARCHAR doesn't seem to be supported
             if (jdbcType == Types.CHAR || jdbcType == Types.LONGVARCHAR) {
                 jdbcType = Types.VARCHAR;
 
@@ -87,23 +78,24 @@
                 }
             }
 
-            buf.append(" AS ");
-            String[] types =
-                getQueryAssembler().getAdapter().externalTypesForJdbcType(
+            out.append(" AS ");
+            String[] types = queryAssembler.getAdapter().externalTypesForJdbcType(
                     jdbcType);
 
             if (types == null || types.length == 0) {
                 throw new CayenneRuntimeException(
-                    "Can't find database type for JDBC type '"
-                        + TypesMapping.getSqlNameByType(jdbcType));
+                        "Can't find database type for JDBC type '"
+                                + TypesMapping.getSqlNameByType(jdbcType));
             }
 
-            buf.append(types[0]);
+            out.append(types[0]);
             if (len > 0 && TypesMapping.supportsLength(jdbcType)) {
-                buf.append("(").append(len).append(")");
+                out.append("(");
+                out.append(String.valueOf(len));
+                out.append(")");
             }
 
-            buf.append(")");
+            out.append(")");
         }
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/openbase/OpenBaseQualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/openbase/OpenBaseQualifierTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/openbase/OpenBaseQualifierTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/openbase/OpenBaseQualifierTranslator.java Tue May  6 07:33:22 2008
@@ -19,25 +19,23 @@
 
 package org.apache.cayenne.dba.openbase;
 
+import java.io.IOException;
+
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.access.trans.QualifierTranslator;
 import org.apache.cayenne.access.trans.QueryAssembler;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.map.DbAttribute;
 
-/** 
+/**
  * Translates query qualifier to SQL. Used as a helper class by query translators.
  * 
  * @author <a href="mailto:mkienenb@alaska.net">Mike Kienenberger</a>
  * @author Andrus Adamchik
- * 
  * @since 1.1
  */
 public class OpenBaseQualifierTranslator extends QualifierTranslator {
 
-    public OpenBaseQualifierTranslator() {
-        this(null);
-    }
-
     public OpenBaseQualifierTranslator(QueryAssembler queryAssembler) {
         super(queryAssembler);
     }
@@ -49,14 +47,19 @@
             // binary nodes are the only ones that currently require this
             detectObjectMatch(node);
 
-            if (parenthesisNeeded(node, parentNode)) {
-                qualBuf.append('(');
+            try {
+                if (parenthesisNeeded(node, parentNode)) {
+                    out.append('(');
+                }
+
+                // super implementation has special handling
+                // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
+                // OpenBase is case-insensitive by default
+                // ...
+            }
+            catch (IOException ioex) {
+                throw new CayenneRuntimeException("Error appending content", ioex);
             }
-
-            // super implementation has special handling 
-            // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
-            // OpenBase is case-insensitive by default
-            // ...
         }
         else {
             super.startNode(node, parentNode);
@@ -66,18 +69,25 @@
     @Override
     public void endNode(Expression node, Expression parentNode) {
         if (node.getOperandCount() == 2) {
-            // check if we need to use objectMatchTranslator to finish building the expression
-            if (matchingObject) {
-                appendObjectMatch();
-            }
 
-            if (parenthesisNeeded(node, parentNode))
-                qualBuf.append(')');
-
-            // super implementation has special handling 
-            // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
-            // OpenBase is case-insensitive by default
-            // ...
+            try {
+                // check if we need to use objectMatchTranslator to finish building the
+                // expression
+                if (matchingObject) {
+                    appendObjectMatch();
+                }
+
+                if (parenthesisNeeded(node, parentNode))
+                    out.append(')');
+
+                // super implementation has special handling
+                // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
+                // OpenBase is case-insensitive by default
+                // ...
+            }
+            catch (IOException ioex) {
+                throw new CayenneRuntimeException("Error appending content", ioex);
+            }
         }
         else {
             super.endNode(node, parentNode);
@@ -86,21 +96,20 @@
 
     @Override
     protected void appendLiteralDirect(
-        StringBuffer buf,
-        Object val,
-        DbAttribute attr,
-        Expression parentExpression) {
+            Object val,
+            DbAttribute attr,
+            Expression parentExpression) throws IOException {
 
         // Special handling of string matching is needed:
         // Case-sensitive LIKE must be converted to [x][Y][z] format
         if (val instanceof String
-            && (parentExpression.getType() == Expression.LIKE
-                || parentExpression.getType() == Expression.NOT_LIKE)) {
+                && (parentExpression.getType() == Expression.LIKE || parentExpression
+                        .getType() == Expression.NOT_LIKE)) {
 
             val = caseSensitiveLikePattern((String) val);
         }
 
-        super.appendLiteralDirect(buf, val, attr, parentExpression);
+        super.appendLiteralDirect(val, attr, parentExpression);
     }
 
     private String caseSensitiveLikePattern(String pattern) {
@@ -126,29 +135,35 @@
             return;
         }
 
-        // super implementation has special handling 
+        // super implementation has special handling
         // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
         // OpenBase is case-insensitive by default
         // ...
 
-        switch (node.getType()) {
+        try {
+            switch (node.getType()) {
 
-            case Expression.LIKE_IGNORE_CASE :
-                finishedChildNodeAppendExpression(node, " LIKE ");
-                break;
-            case Expression.NOT_LIKE_IGNORE_CASE :
-                finishedChildNodeAppendExpression(node, " NOT LIKE ");
-                break;
-            default :
-                super.finishedChild(node, childIndex, hasMoreChildren);
+                case Expression.LIKE_IGNORE_CASE:
+                    finishedChildNodeAppendExpression(node, " LIKE ");
+                    break;
+                case Expression.NOT_LIKE_IGNORE_CASE:
+                    finishedChildNodeAppendExpression(node, " NOT LIKE ");
+                    break;
+                default:
+                    super.finishedChild(node, childIndex, hasMoreChildren);
+            }
+        }
+        catch (IOException ioex) {
+            throw new CayenneRuntimeException("Error appending content", ioex);
         }
     }
 
-    private void finishedChildNodeAppendExpression(Expression node, String operation) {
-        StringBuffer buf = (matchingObject) ? new StringBuffer() : qualBuf;
-        buf.append(operation);
+    private void finishedChildNodeAppendExpression(Expression node, String operation)
+            throws IOException {
+        Appendable out = (matchingObject) ? new StringBuilder() : this.out;
+        out.append(operation);
         if (matchingObject) {
-            objectMatchTranslator.setOperation(buf.toString());
+            objectMatchTranslator.setOperation(out.toString());
             objectMatchTranslator.setExpression(node);
         }
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresQualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresQualifierTranslator.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresQualifierTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresQualifierTranslator.java Tue May  6 07:33:22 2008
@@ -19,12 +19,15 @@
 
 package org.apache.cayenne.dba.postgres;
 
+import java.io.IOException;
+
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.access.trans.QueryAssembler;
 import org.apache.cayenne.access.trans.TrimmingQualifierTranslator;
 import org.apache.cayenne.exp.Expression;
 
-/** 
- * Uses Postgres extensions to optimize various translations. 
+/**
+ * Uses Postgres extensions to optimize various translations.
  * 
  * @author Andrus Adamchik
  * @since 1.1
@@ -42,14 +45,19 @@
             // binary nodes are the only ones that currently require this
             detectObjectMatch(node);
 
-            if (parenthesisNeeded(node, parentNode)) {
-                qualBuf.append('(');
+            try {
+                if (parenthesisNeeded(node, parentNode)) {
+                    out.append('(');
+                }
+
+                // super implementation has special handling
+                // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
+                // Postgres uses ILIKE
+                // ...
+            }
+            catch (IOException ioex) {
+                throw new CayenneRuntimeException("Error appending content", ioex);
             }
-
-            // super implementation has special handling 
-            // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
-            // Postgres uses ILIKE
-            // ...
         }
         else {
             super.startNode(node, parentNode);
@@ -59,18 +67,25 @@
     @Override
     public void endNode(Expression node, Expression parentNode) {
         if (node.getOperandCount() == 2) {
-            // check if we need to use objectMatchTranslator to finish building the expression
-            if (matchingObject) {
-                appendObjectMatch();
-            }
-
-            if (parenthesisNeeded(node, parentNode))
-                qualBuf.append(')');
 
-            // super implementation has special handling 
-            // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
-            // Postgres uses ILIKE
-            // ...
+            try {
+                // check if we need to use objectMatchTranslator to finish building the
+                // expression
+                if (matchingObject) {
+                    appendObjectMatch();
+                }
+
+                if (parenthesisNeeded(node, parentNode))
+                    out.append(')');
+
+                // super implementation has special handling
+                // of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
+                // Postgres uses ILIKE
+                // ...
+            }
+            catch (IOException ioex) {
+                throw new CayenneRuntimeException("Error appending content", ioex);
+            }
         }
         else {
             super.endNode(node, parentNode);
@@ -83,23 +98,29 @@
             return;
         }
 
-        // use ILIKE
+        try {
+            // use ILIKE
 
-        switch (node.getType()) {
+            switch (node.getType()) {
 
-            case Expression.LIKE_IGNORE_CASE :
-                finishedChildNodeAppendExpression(node, " ILIKE ");
-                break;
-            case Expression.NOT_LIKE_IGNORE_CASE :
-                finishedChildNodeAppendExpression(node, " NOT ILIKE ");
-                break;
-            default :
-                super.finishedChild(node, childIndex, hasMoreChildren);
+                case Expression.LIKE_IGNORE_CASE:
+                    finishedChildNodeAppendExpression(node, " ILIKE ");
+                    break;
+                case Expression.NOT_LIKE_IGNORE_CASE:
+                    finishedChildNodeAppendExpression(node, " NOT ILIKE ");
+                    break;
+                default:
+                    super.finishedChild(node, childIndex, hasMoreChildren);
+            }
+        }
+        catch (IOException ioex) {
+            throw new CayenneRuntimeException("Error appending content", ioex);
         }
     }
 
-    private void finishedChildNodeAppendExpression(Expression node, String operation) {
-        StringBuffer buf = (matchingObject) ? new StringBuffer() : qualBuf;
+    private void finishedChildNodeAppendExpression(Expression node, String operation)
+            throws IOException {
+        Appendable buf = (matchingObject) ? new StringBuilder() : this.out;
         buf.append(operation);
         if (matchingObject) {
             objectMatchTranslator.setOperation(buf.toString());

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/OrderingTranslatorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/OrderingTranslatorTest.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/OrderingTranslatorTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/OrderingTranslatorTest.java Tue May  6 07:33:22 2008
@@ -41,14 +41,18 @@
      */
     public void testDoTranslation1() throws Exception {
         try {
-            TranslationCase tstCase =
-                new TranslationCase("Artist", null, "ta.ARTIST_NAME");
+            TranslationCase tstCase = new TranslationCase(
+                    "Artist",
+                    null,
+                    "ta.ARTIST_NAME");
             q.setRoot(Artist.class);
             q.addOrdering("artistName", Ordering.ASC);
-            String orderBySql = new OrderingTranslator(qa).doTranslation();
 
-            assertNotNull(orderBySql);
-            tstCase.assertTranslatedWell(orderBySql);
+            StringBuilder out = new StringBuilder();
+            new OrderingTranslator(qa).appendPart(out);
+
+            assertTrue(out.length() > 0);
+            tstCase.assertTranslatedWell(out.toString());
         }
         finally {
             qa.dispose();
@@ -60,14 +64,18 @@
      */
     public void testDoTranslation2() throws Exception {
         try {
-            TranslationCase tstCase =
-                new TranslationCase("Artist", null, "ta.ARTIST_NAME DESC");
+            TranslationCase tstCase = new TranslationCase(
+                    "Artist",
+                    null,
+                    "ta.ARTIST_NAME DESC");
             q.setRoot(Artist.class);
             q.addOrdering("artistName", Ordering.DESC);
-            String orderBySql = new OrderingTranslator(qa).doTranslation();
 
-            assertNotNull(orderBySql);
-            tstCase.assertTranslatedWell(orderBySql);
+            StringBuilder out = new StringBuilder();
+            new OrderingTranslator(qa).appendPart(out);
+
+            assertTrue(out.length() > 0);
+            tstCase.assertTranslatedWell(out.toString());
         }
         finally {
             qa.dispose();
@@ -79,13 +87,17 @@
      */
     public void testDoTranslation4() throws Exception {
         try {
-            TranslationCase tstCase =
-                new TranslationCase("Artist", null, "UPPER(ta.ARTIST_NAME)");
+            TranslationCase tstCase = new TranslationCase(
+                    "Artist",
+                    null,
+                    "UPPER(ta.ARTIST_NAME)");
             q.setRoot(Artist.class);
             q.addOrdering("artistName", Ordering.ASC, true);
-            String orderBySql = new OrderingTranslator(qa).doTranslation();
+            StringBuilder out = new StringBuilder();
+            new OrderingTranslator(qa).appendPart(out);
 
-            assertNotNull(orderBySql);
+            assertTrue(out.length() > 0);
+            String orderBySql = out.toString();
             assertTrue(orderBySql.contains("UPPER("));
             tstCase.assertTranslatedWell(orderBySql);
         }
@@ -96,18 +108,20 @@
 
     public void testDoTranslation5() throws Exception {
         try {
-            TranslationCase tstCase =
-                new TranslationCase(
+            TranslationCase tstCase = new TranslationCase(
                     "Artist",
                     null,
                     "UPPER(ta.ARTIST_NAME) DESC, ta.ESTIMATED_PRICE");
             q.setRoot(Artist.class);
             q.addOrdering("artistName", Ordering.DESC, true);
             q.addOrdering("paintingArray.estimatedPrice", Ordering.ASC);
-            String orderBySql = new OrderingTranslator(qa).doTranslation();
+            StringBuilder out = new StringBuilder();
+            new OrderingTranslator(qa).appendPart(out);
+
+            assertTrue(out.length() > 0);
+            String orderBySql = out.toString();
 
-            assertNotNull(orderBySql);
-            //Check there is an UPPER modifier
+            // Check there is an UPPER modifier
             int indexOfUpper = orderBySql.indexOf("UPPER(");
             assertTrue(indexOfUpper != -1);
 
@@ -122,18 +136,20 @@
 
     public void testDoTranslation6() throws Exception {
         try {
-            TranslationCase tstCase =
-                new TranslationCase(
+            TranslationCase tstCase = new TranslationCase(
                     "Artist",
                     null,
                     "UPPER(ta.ARTIST_NAME), UPPER(ta.ESTIMATED_PRICE)");
             q.setRoot(Artist.class);
             q.addOrdering("artistName", Ordering.ASC, true);
             q.addOrdering("paintingArray.estimatedPrice", Ordering.ASC, true);
-            String orderBySql = new OrderingTranslator(qa).doTranslation();
+            StringBuilder out = new StringBuilder();
+            new OrderingTranslator(qa).appendPart(out);
 
-            assertNotNull(orderBySql);
-            //Check there is at least one UPPER modifier
+            assertTrue(out.length() > 0);
+            String orderBySql = out.toString();
+            
+            // Check there is at least one UPPER modifier
             int indexOfUpper = orderBySql.indexOf("UPPER(");
             assertTrue(indexOfUpper != -1);
 
@@ -149,17 +165,18 @@
 
     public void testDoTranslation3() throws Exception {
         try {
-            TranslationCase tstCase =
-                new TranslationCase(
+            TranslationCase tstCase = new TranslationCase(
                     "Artist",
                     null,
                     "ta.ARTIST_NAME DESC, ta.ESTIMATED_PRICE");
             q.setRoot(Artist.class);
             q.addOrdering("artistName", Ordering.DESC);
             q.addOrdering("paintingArray.estimatedPrice", Ordering.ASC);
-            String orderBySql = new OrderingTranslator(qa).doTranslation();
+            StringBuilder out = new StringBuilder();
+            new OrderingTranslator(qa).appendPart(out);
 
-            assertNotNull(orderBySql);
+            assertTrue(out.length() > 0);
+            String orderBySql = out.toString();
             tstCase.assertTranslatedWell(orderBySql);
         }
         finally {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/QualifierTranslatorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/QualifierTranslatorTest.java?rev=653788&r1=653787&r2=653788&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/QualifierTranslatorTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/QualifierTranslatorTest.java Tue May  6 07:33:22 2008
@@ -49,7 +49,7 @@
         qa = new TstQueryAssembler(getNode(), new MockQuery());
 
         try {
-            new QualifierTranslator(qa).doTranslation();
+            new QualifierTranslator(qa).appendPart(new StringBuilder());
             fail();
         }
         catch (ClassCastException ccex) {
@@ -61,12 +61,15 @@
     }
 
     public void testNullQualifier() throws Exception {
+        StringBuilder out = new StringBuilder();
         try {
-            assertNull(new QualifierTranslator(qa).doTranslation());
+            new QualifierTranslator(qa).appendPart(out);
         }
         finally {
             qa.dispose();
         }
+
+        assertEquals(0, out.length());
     }
 
     public void testUnary() throws Exception {
@@ -118,11 +121,15 @@
                     ObjEntity ent = getObjEntity(cases[i].getRootEntity());
                     assertNotNull(ent);
                     q.setRoot(ent);
-                    String translated = new QualifierTranslator(qa).doTranslation();
-                    cases[i].assertTranslatedWell(translated);
+                    StringBuilder out = new StringBuilder();
+                    new QualifierTranslator(qa).appendPart(out);
+                    cases[i].assertTranslatedWell(out.toString());
                 }
                 catch (Exception ex) {
-                    throw new CayenneRuntimeException("Failed case: [" + i + "]: " + cases[i], ex);
+                    throw new CayenneRuntimeException("Failed case: ["
+                            + i
+                            + "]: "
+                            + cases[i], ex);
                 }
             }
         }



Mime
View raw message