db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1056930 [2/2] - in /db/torque/torque4/branches/trunk-without-village: torque-runtime/src/main/java/org/apache/torque/adapter/ torque-runtime/src/main/java/org/apache/torque/util/ torque-runtime/src/test/java/org/apache/torque/ torque-runti...
Date Sun, 09 Jan 2011 13:43:30 GMT
Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java Sun Jan  9 13:43:29 2011
@@ -20,18 +20,14 @@ package org.apache.torque.util;
  */
 
 import java.lang.reflect.Array;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
-import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.DB;
-import org.apache.torque.om.DateKey;
 import org.apache.torque.om.ObjectKey;
-import org.apache.torque.om.StringKey;
 
 
 /**
@@ -52,8 +48,6 @@ import org.apache.torque.om.StringKey;
  */
 public final class SqlExpression
 {
-    /** escaped single quote */
-    private static final char SINGLE_QUOTE = '\'';
     /** escaped backslash */
     private static final char BACKSLASH = '\\';
 
@@ -67,134 +61,13 @@ public final class SqlExpression
     {
     }
 
-    /**
-     * Used to specify a join on two columns.
-     *
-     * @param column A column in one of the tables to be joined.
-     * @param relatedColumn The column in the other table to be joined.
-     * @return A join expression, e.g. UPPER(table_a.column_a) =
-     *         UPPER(table_b.column_b).
-     */
-    public static String buildInnerJoin(String column, String relatedColumn)
-    {
-        // 'db' can be null because 'ignoreCase' is false.
-        return buildInnerJoin(column, relatedColumn, false, null);
-    }
-
-    /**
-     * Used to specify a join on two columns.
-     *
-     * @param column A column in one of the tables to be joined.
-     * @param relatedColumn The column in the other table to be joined.
-     * @param ignoreCase If true and columns represent Strings, the appropriate
-     *        function defined for the database will be used to ignore
-     *        differences in case.
-     * @param db Represents the database in use for vendor-specific functions.
-     * @return A join expression, e.g. UPPER(table_a.column_a) =
-     *         UPPER(table_b.column_b).
-     */
-    public static String buildInnerJoin(String column,
-                                         String relatedColumn,
-                                         boolean ignoreCase,
-                                         DB db)
-    {
-        int addlength = (ignoreCase) ? 25 : 1;
-        StringBuffer sb = new StringBuffer(column.length()
-                + relatedColumn.length() + addlength);
-        buildInnerJoin(column, relatedColumn, ignoreCase, db, sb);
-        return sb.toString();
-    }
-
-    /**
-     * Used to specify a join on two columns.
-     *
-     * @param column A column in one of the tables to be joined.
-     * @param relatedColumn The column in the other table to be joined.
-     * @param ignoreCase If true and columns represent Strings, the appropriate
-     *        function defined for the database will be used to ignore
-     *        differences in case.
-     * @param db Represents the database in use for vendor-specific functions.
-     * @param whereClause A StringBuffer to which the sql expression will be
-     *        appended.
-     */
-    public static void buildInnerJoin(String column,
-                                       String relatedColumn,
-                                       boolean ignoreCase,
-                                       DB db,
-                                       StringBuffer whereClause)
-    {
-        if (ignoreCase)
-        {
-            whereClause.append(db.ignoreCase(column))
-                    .append('=')
-                    .append(db.ignoreCase(relatedColumn));
-        }
-        else
-        {
-            whereClause.append(column)
-                    .append('=')
-                    .append(relatedColumn);
-        }
-    }
-
-
-    /**
-     * Builds a simple SQL expression.
-     *
-     * @param columnName A column.
-     * @param criteria The value to compare the column against.
-     * @param comparison One of =, <, >, ^lt;=, >=, <>,
-     *        !=, LIKE, etc.
-     * @return A simple SQL expression, e.g. UPPER(table_a.column_a)
-     *         LIKE UPPER('ab%c').
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
-     */
-    public static String build(String columnName,
-                                Object criteria,
-                                SqlEnum comparison)
-        throws TorqueException
-    {
-        // 'db' can be null because 'ignoreCase' is null
-        return build(columnName, criteria, comparison, false, null);
-    }
-
-    /**
-     * Builds a simple SQL expression.
-     *
-     * @param columnName A column.
-     * @param criteria The value to compare the column against.
-     * @param comparison One of =, <, >, ^lt;=, >=, <>,
-     *        !=, LIKE, etc.
-     * @param ignoreCase If true and columns represent Strings, the appropriate
-     *        function defined for the database will be used to ignore
-     *        differences in case.
-     * @param db Represents the database in use, for vendor specific functions.
-     * @return A simple sql expression, e.g. UPPER(table_a.column_a)
-     *         LIKE UPPER('ab%c').
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
-     */
-    public static String build(String columnName,
-                                Object criteria,
-                                SqlEnum comparison,
-                                boolean ignoreCase,
-                                DB db)
-        throws TorqueException
-    {
-        int addlength = (ignoreCase ? 40 : 20);
-        StringBuffer sb = new StringBuffer(columnName.length() + addlength);
-        build(columnName, criteria, comparison, ignoreCase, db, sb);
-        return sb.toString();
-    }
 
     /**
-     * Builds a simple SQL expression.
+     * Builds an element of the where clause of a prepared statement.
      *
-     * @param columnName A column.
-     * @param criteria The value to compare the column against.
-     * @param comparison One of =, <, >, ^lt;=, >=, <>,
-     *        !=, LIKE, etc.
+     * @param columnName the name of the column.
+     * @param value The value to compare the column against.
+     * @param comparison The comparison between column and value.
      * @param ignoreCase If true and columns represent Strings, the appropriate
      *        function defined for the database will be used to ignore
      *        differences in case.
@@ -202,111 +75,114 @@ public final class SqlExpression
      * @param whereClause A StringBuffer to which the sql expression will be
      *        appended.
      */
-    public static void build(String columnName,
-                              Object criteria,
+    public static void buildPs(String columnName,
+                              Object value,
                               SqlEnum comparison,
                               boolean ignoreCase,
                               DB db,
-                              StringBuffer whereClause)
+                              StringBuilder whereClause,
+                              List<Object> params)
             throws TorqueException
     {
         // Allow null criteria
         // This will result in queries like
         // insert into table (name, parent) values ('x', null);
-        //
 
-        /* Check to see if the criteria is an ObjectKey
-         * and if the value of that ObjectKey is null.
-         * In that case, criteria should be null.
-         */
+        if (SqlEnum.CUSTOM == comparison)
+        {
+            whereClause.append(value);
+            return;
+        }
 
-        if (criteria != null && criteria instanceof ObjectKey)
+        // Handle SqlEnum.CURRENT_DATE and SqlEnum.CURRENT_TIME
+        if (value instanceof SqlEnum)
         {
-            if (((ObjectKey) criteria).getValue() == null)
-            {
-                criteria = null;
-            }
+            whereClause.append(columnName).append(comparison).append(value);
+            return;
         }
+
+        /* If the value is an ObjectKey, take the value of that ObjectKey.
+         */
+        if (value instanceof ObjectKey)
+        {
+            value = ((ObjectKey) value).getValue();
+        }
+
         /*  If the criteria is null, check to see comparison
          *  is an =, <>, or !=.  If so, replace the comparison
          *  with the proper IS or IS NOT.
          */
-
-        if (criteria == null)
+        if (value == null)
         {
-            criteria = "null";
             if (comparison.equals(Criteria.EQUAL))
             {
-                comparison = Criteria.ISNULL;
+                whereClause.append(columnName).append(Criteria.ISNULL);
+                return;
             }
-            else if (comparison.equals(Criteria.NOT_EQUAL))
+            if (comparison.equals(Criteria.NOT_EQUAL))
             {
-                comparison = Criteria.ISNOTNULL;
+                whereClause.append(columnName).append(Criteria.ISNOTNULL);
+                return;
             }
             else if (comparison.equals(Criteria.ALT_NOT_EQUAL))
             {
-                comparison = Criteria.ISNOTNULL;
+                whereClause.append(columnName).append(Criteria.ISNOTNULL);
+                return;
             }
         }
-        else
+
+        String sqlValue;
+        Object parameter;
+        if (value instanceof Criteria)
         {
-            if (criteria instanceof String || criteria instanceof StringKey)
+            Query subquery = SQLBuilder.buildQuery(
+                    (Criteria) value);
+            List<Object> preparedStatementReplacements
+                    = subquery.getPreparedStatementReplacements();
+            if (preparedStatementReplacements.size() == 0)
             {
-                criteria = quoteAndEscapeText(criteria.toString(), db);
+                // no prepared statement replacement necessary
+                whereClause.append(columnName)
+                    .append(comparison)
+                    .append("(").append(subquery.toString()).append(")");
+                return;
             }
-            else if (criteria instanceof Date)
+            if (preparedStatementReplacements.size() != 1)
             {
-                Date dt = (Date) criteria;
-                criteria = db.getDateString(dt);
+                throw new TorqueException(
+                        "could not use subquery, wrong number of parameters");
             }
-            else if (criteria instanceof DateKey)
+            parameter = preparedStatementReplacements.get(0);
+            if (comparison.equals(Criteria.IN)
+                    || comparison.equals(Criteria.NOT_IN))
             {
-                Date dt = (Date) ((DateKey) criteria).getValue();
-                criteria = db.getDateString(dt);
+                // code below takes care of adding brackets
+                sqlValue = subquery.toString();
             }
-            else if (criteria instanceof Boolean)
+            else
             {
-                criteria = db.getBooleanString((Boolean) criteria);
-            }
-            else if (criteria instanceof Criteria)
-            {
-                 Query subquery = SQLBuilder.buildQueryClause(
-                        (Criteria) criteria,
-                        null,
-                        new SQLBuilder.QueryCallback() {
-                            public String process(
-                                    Criteria.Criterion criterion,
-                                    List<Object> params)
-                            {
-                                return criterion.toString();
-                            }
-                });
-                if (comparison.equals(Criteria.IN)
-                        || comparison.equals(Criteria.NOT_IN))
-                {
-                    // code below takes care of adding brackets
-                    criteria = subquery.toString();
-                }
-                else
-                {
-                    criteria = "(" + subquery.toString() + ")";
-                }
+                sqlValue = "(" + subquery.toString() + ")";
             }
         }
+        else
+        {
+            sqlValue = "?";
+            parameter = value;
+        }
 
         if (comparison.equals(Criteria.LIKE)
                 || comparison.equals(Criteria.NOT_LIKE)
                 || comparison.equals(Criteria.ILIKE)
                 || comparison.equals(Criteria.NOT_ILIKE))
         {
-            buildLike(columnName, (String) criteria, comparison,
-                       ignoreCase, db, whereClause);
+            buildPsLike(columnName, (String) parameter, sqlValue, comparison,
+                       ignoreCase, db, whereClause, params);
         }
         else if (comparison.equals(Criteria.IN)
                 || comparison.equals(Criteria.NOT_IN))
         {
-            buildIn(columnName, criteria, comparison,
-                     ignoreCase, db, whereClause);
+            buildPsIn(columnName, parameter, comparison, sqlValue,
+                     ignoreCase, db, whereClause, params);
         }
         else
         {
@@ -320,15 +196,15 @@ public final class SqlExpression
             }
             else
             {
-                String columnValue = criteria.toString();
-                if (ignoreCase && db != null)
+                params.add(parameter);
+                if (ignoreCase)
                 {
                     columnName = db.ignoreCase(columnName);
-                    columnValue = db.ignoreCase(columnValue);
+                    sqlValue = db.ignoreCase(sqlValue);
                 }
                 whereClause.append(columnName)
                         .append(comparison)
-                        .append(columnValue);
+                        .append(sqlValue);
             }
         }
     }
@@ -344,54 +220,27 @@ public final class SqlExpression
      *                        -> UPPER(columnName) LIKE UPPER('fre%')
      *      criteria = "50\%" -> columnName = '50%'
      *
-     * @param columnName A column.
-     * @param criteria The value to compare the column against.
-     * @param comparison Whether to do a LIKE or a NOT LIKE
-     * @param ignoreCase If true and columns represent Strings, the
-     * appropriate function defined for the database will be used to
-     * ignore differences in case.
-     * @param db Represents the database in use, for vendor specific functions.
-     * @return An SQL expression.
-     */
-    static String buildLike(String columnName,
-                             String criteria,
-                             SqlEnum comparison,
-                             boolean ignoreCase,
-                             DB db)
-    {
-        StringBuffer whereClause = new StringBuffer();
-        buildLike(columnName, criteria, comparison, ignoreCase, db,
-                   whereClause);
-        return whereClause.toString();
-    }
-
-    /**
-     * Takes a columnName and criteria and builds an SQL phrase based
-     * on whether wildcards are present and the state of the
-     * ignoreCase flag.  Multicharacter wildcards % and * may be used
-     * as well as single character wildcards, _ and ?.  These
-     * characters can be escaped with \.
-     *
-     * e.g. criteria = "fre%" -> columnName LIKE 'fre%'
-     *                        -> UPPER(columnName) LIKE UPPER('fre%')
-     *      criteria = "50\%" -> columnName = '50%'
-     *
      * @param columnName A column name.
-     * @param criteria The value to compare the column against.
-     * @param comparison Whether to do a LIKE or a NOT LIKE
+     * @param value The value to compare the column against.
+     * @param sqlValue The placeholder in the prepared statement for the value.
+     * @param comparison the comparison operator.
      * @param ignoreCase If true and columns represent Strings, the
-     * appropriate function defined for the database will be used to
-     * ignore differences in case.
+     *        appropriate function defined for the database will be used to
+     *        ignore differences in case.
      * @param db Represents the database in use, for vendor specific functions.
      * @param whereClause A StringBuffer to which the sql expression
-     * will be appended.
-     */
-    static void buildLike(String columnName,
-                           String criteria,
+     *        will be appended.
+     * @param parameters the fill-ins for the placeholders in the
+     *        prepared statement.
+     */
+    static void buildPsLike(String columnName,
+                           String value,
+                           String sqlValue,
                            SqlEnum comparison,
                            boolean ignoreCase,
                            DB db,
-                           StringBuffer whereClause)
+                           StringBuilder whereClause,
+                           List<Object> parameters)
     {
         // If selection criteria contains wildcards use LIKE otherwise
         // use = (equals).  Wildcards can be escaped by prepending
@@ -404,34 +253,22 @@ public final class SqlExpression
         int position = 0;
         StringBuffer sb = new StringBuffer();
         boolean replaceWithEquals = true;
-        while (position < criteria.length())
+        while (position < value.length())
         {
-            char checkWildcard = criteria.charAt(position);
+            char checkWildcard = value.charAt(position);
 
             switch (checkWildcard)
             {
             case BACKSLASH:
-                // if text is escaped, all backslashes are already escaped,
-                // so the next character after the backslash is the doubled
-                // backslash from escaping.
-                int charsToProceed = db.escapeText() ? 2 : 1;
-                if (position + charsToProceed >= criteria.length())
-                {
-                    charsToProceed = criteria.length() - position - 1;
-                }
-                else if (criteria.charAt(position + charsToProceed) == BACKSLASH
-                        && db.escapeText())
+                if (position + 1 >= value.length())
                 {
-                    // the escaped backslash is also escaped,
-                    // so we need to proceed another character
-                    charsToProceed += 1;
+                    // ignore backslashes at end
+                    break;
                 }
-                sb.append(criteria.substring(
-                        position,
-                        position + charsToProceed));
-                position += charsToProceed;
+                sb.append(checkWildcard);
+                position ++;
                 // code below copies escaped character into sb
-                checkWildcard = criteria.charAt(position);
+                checkWildcard = value.charAt(position);
                 break;
             case '%':
             case '_':
@@ -450,7 +287,7 @@ public final class SqlExpression
             sb.append(checkWildcard);
             position++;
         }
-        criteria = sb.toString();
+        value = sb.toString();
 
         if (ignoreCase)
         {
@@ -480,50 +317,32 @@ public final class SqlExpression
             if (comparison.equals(Criteria.NOT_LIKE)
                     || comparison.equals(Criteria.NOT_ILIKE))
             {
-                whereClause.append(" ").append(Criteria.NOT_EQUAL).append(" ");
+                whereClause.append(Criteria.NOT_EQUAL);
             }
             else
             {
-                whereClause.append(" ").append(Criteria.EQUAL).append(" ");
+                whereClause.append(Criteria.EQUAL);
             }
 
             // remove escape backslashes from String
             position = 0;
             sb = new StringBuffer();
-            while (position < criteria.length())
+            while (position < value.length())
             {
-                char checkWildcard = criteria.charAt(position);
+                char checkWildcard = value.charAt(position);
 
-                if (checkWildcard == BACKSLASH)
+                if (checkWildcard == BACKSLASH
+                        && position + 1 < value.length())
                 {
-                    // if text is escaped, all backslashes are already escaped,
-                    // so the next character after the backslash is the doubled
-                    // backslash from escaping.
-                    int charsToSkip = db.escapeText() ? 2 : 1;
-                    if (position + charsToSkip >= criteria.length())
-                    {
-                        charsToSkip = criteria.length() - position - 1;
-                    }
-                    else if (criteria.charAt(position + charsToSkip)
-                                == BACKSLASH
-                            && db.escapeText())
-                    {
-                        // the escaped backslash is also escaped,
-                        // so we need to skip another character
-                        // but add the escaped backslash to sb
-                        // so that the escaping remains.
-                        sb.append(BACKSLASH);
-                        charsToSkip += 1;
-                    }
-                    position += charsToSkip;
+                    position++;
                     // code below copies escaped character into sb
-                    checkWildcard = criteria.charAt(position);
+                    checkWildcard = value.charAt(position);
                 }
                 sb.append(checkWildcard);
                 position++;
             }
-            criteria = sb.toString();
-       }
+            value = sb.toString();
+        }
         else
         {
             whereClause.append(comparison);
@@ -533,41 +352,14 @@ public final class SqlExpression
         // on criteria.
         if (ignoreCase && (!(db.useIlike()) || replaceWithEquals))
         {
-            criteria = db.ignoreCase(criteria);
+            sqlValue = db.ignoreCase(sqlValue);
         }
-        whereClause.append(criteria);
-
         if (!replaceWithEquals && db.useEscapeClauseForLike())
         {
-            whereClause.append(SqlEnum.ESCAPE)
-                       .append("'\\'");
+            sqlValue = sqlValue + SqlEnum.ESCAPE + "'\\'";
         }
-    }
-
-    /**
-     * Takes a columnName and criteria (which must be an array) and
-     * builds a SQL 'IN' expression taking into account the ignoreCase
-     * flag.
-     *
-     * @param columnName A column.
-     * @param criteria The value to compare the column against.
-     * @param comparison Either " IN " or " NOT IN ".
-     * @param ignoreCase If true and columns represent Strings, the
-     * appropriate function defined for the database will be used to
-     * ignore differences in case.
-     * @param db Represents the database in use, for vendor specific functions.
-     * @return An SQL expression.
-     */
-    static String buildIn(String columnName,
-                          Object criteria,
-                          SqlEnum comparison,
-                          boolean ignoreCase,
-                          DB db)
-    {
-        StringBuffer whereClause = new StringBuffer();
-        buildIn(columnName, criteria, comparison,
-                ignoreCase, db, whereClause);
-        return whereClause.toString();
+        parameters.add(value);
+        whereClause.append(sqlValue);
     }
 
     /**
@@ -585,12 +377,14 @@ public final class SqlExpression
      * @param whereClause A StringBuffer to which the sql expression
      * will be appended.
      */
-    static void buildIn(String columnName,
-                        Object criteria,
+    static void buildPsIn(String columnName,
+                        Object parameter,
                         SqlEnum comparison,
+                        String sqlValue,
                         boolean ignoreCase,
                         DB db,
-                        StringBuffer whereClause)
+                        StringBuilder whereClause,
+                        List<Object> params)
     {
         if (ignoreCase)
         {
@@ -602,116 +396,69 @@ public final class SqlExpression
         }
 
         whereClause.append(comparison);
-        Set<String> inClause = new HashSet<String>();
-        if (criteria instanceof List)
+        List<String> inClause = new ArrayList<String>();
+        if (parameter instanceof Iterable)
         {
-            Iterator<Object> iter = ((List<Object>) criteria).iterator();
-            while (iter.hasNext())
+            if (!ignoreCase)
             {
-                Object value = iter.next();
-
-                // The method processInValue() quotes the string
-                // and/or wraps it in UPPER().
-                inClause.add(processInValue(value, ignoreCase, db));
+                inClause.add(sqlValue);
+                params.add(parameter);
+            }
+            else
+            {
+                for (Object listValue : (Iterable<?>) parameter)
+                {
+                    params.add(listValue);
+                    if (listValue instanceof String)
+                    {
+                        inClause.add(db.ignoreCase("?"));
+                    }
+                    else
+                    {
+                        inClause.add("?");
+                    }
+                }
             }
         }
-        else if (criteria instanceof String)
+        else if (parameter instanceof String)
         {
             // subquery
-            inClause.add((String) criteria);
+            inClause.add((String) sqlValue);
+            params.add(parameter);
         }
-        else
+        else if (parameter.getClass().isArray())
         {
-            // Assume array.
-            for (int i = 0; i < Array.getLength(criteria); i++)
+            if (!ignoreCase)
             {
-                Object value = Array.get(criteria, i);
-
-                // The method processInValue() quotes the string
-                // and/or wraps it in UPPER().
-                inClause.add(processInValue(value, ignoreCase, db));
+                inClause.add(sqlValue);
+                params.add(parameter);
             }
+            else
+            {
+                for (Object listValue : (Object[]) parameter)
+                {
+                    params.add(listValue);
+                    if (listValue instanceof String)
+                    {
+                        inClause.add(db.ignoreCase("?"));
+                    }
+                    else
+                    {
+                        inClause.add("?");
+                    }
+                }
+            }
+        } 
+        else
+        {
+            throw new IllegalArgumentException(
+                    "Unknown parameter type "
+                    + parameter.getClass().getName()
+                    + ". parameter must be an instance of "
+                    + " Iterable, String or Array");
         }
         whereClause.append('(')
                    .append(StringUtils.join(inClause.iterator(), ","))
                    .append(')');
     }
-
-    /**
-     * Creates an appropriate string for an 'IN' clause from an
-     * object.  Adds quoting and/or UPPER() as appropriate.  This is
-     * broken out into a seperate method as it is used in two places
-     * in buildIn, depending on whether an array or List is being
-     * looped over.
-     *
-     * @param value The value to process.
-     * @param ignoreCase Coerce the value suitably for ignoring case.
-     * @param db Represents the database in use for vendor specific functions.
-     * @return Processed value as String.
-     */
-    static String processInValue(Object value,
-                                 boolean ignoreCase,
-                                 DB db)
-    {
-        String ret = null;
-        if (value instanceof String)
-        {
-            ret = quoteAndEscapeText((String) value, db);
-        }
-        else
-        {
-            ret = value.toString();
-        }
-        if (ignoreCase)
-        {
-            ret = db.ignoreCase(ret);
-        }
-        return ret;
-    }
-
-    /**
-     * Quotes and escapes raw text for placement in a SQL expression.
-     * For simplicity, the text is assumed to be neither quoted nor
-     * escaped.
-     *
-     * @param rawText The <i>unquoted</i>, <i>unescaped</i> text to process.
-     * @param db the db
-     * @return Quoted and escaped text.
-     */
-    public static String quoteAndEscapeText(String rawText, DB db)
-    {
-        StringBuffer buf = new StringBuffer((int) (rawText.length() * 1.1));
-
-        // Some databases do not need escaping.
-        String escapeString;
-        if (db != null && !db.escapeText())
-        {
-            escapeString = String.valueOf(BACKSLASH);
-        }
-        else
-        {
-            escapeString = String.valueOf(BACKSLASH)
-                    + String.valueOf(BACKSLASH);
-        }
-
-        char[] data = rawText.toCharArray();
-        buf.append(SINGLE_QUOTE);
-        for (int i = 0; i < data.length; i++)
-        {
-            switch (data[i])
-            {
-            case SINGLE_QUOTE:
-                buf.append(SINGLE_QUOTE).append(SINGLE_QUOTE);
-                break;
-            case BACKSLASH:
-                buf.append(escapeString);
-                break;
-            default:
-                buf.append(data[i]);
-            }
-        }
-        buf.append(SINGLE_QUOTE);
-
-        return buf.toString();
-    }
-}
+}
\ No newline at end of file

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java Sun Jan  9 13:43:29 2011
@@ -51,22 +51,17 @@ public abstract class BaseTestCase exten
     /**
      * Initialize Torque on the first setUp().  Subclasses which
      * override setUp() must call super.setUp() as their first action.
+     *
+     * @throws TorqueException if initialisation fails.
      */
-    public void setUp()
+    public void setUp() throws TorqueException
     {
         synchronized (BaseTestCase.class)
         {
             if (!hasInitialized)
             {
-                try
-                {
-                    Torque.init(CONFIG_FILE);
-                    hasInitialized = true;
-                }
-                catch (Exception e)
-                {
-                    fail("Couldn't initialize Torque: " + e.getMessage());
-                }
+                Torque.init(CONFIG_FILE);
+                hasInitialized = true;
             }
         }
     }

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java Sun Jan  9 13:43:29 2011
@@ -41,14 +41,13 @@ import org.apache.torque.util.BasePeer;
  */
 public class TorqueInstanceTest extends TestCase
 {
-    /** The name of the "default" dataSourceFactory" as used by Turbine */
+    /** The name of the "default" dataSourceFactory. */
     private static final String DEFAULT_NAME = "default";
 
     /**
-     * The name of the "turbine" dataSourceFactory"
-     * as used by the Turbine configuration
+     * The name of the "mysql" dataSourceFactory
      */
-    private static final String TURBINE_NAME = "turbine";
+    private static final String MYSQL_NAME = "mysql";
 
     /**
      * Creates a new instance.
@@ -82,7 +81,7 @@ public class TorqueInstanceTest extends 
      */
     public void testExternalAdapter() throws Exception
     {
-        DB adapter = Torque.getDatabase(TURBINE_NAME).getAdapter();
+        DB adapter = Torque.getDatabase(MYSQL_NAME).getAdapter();
         assertNotNull(adapter);
     }
 
@@ -119,15 +118,15 @@ public class TorqueInstanceTest extends 
         Map databases = Torque.getDatabases();
         // check whether all expected databases are contained in the Map
         assertEquals(
-                "Databases should contain 2 Databases, not "
+                "Databases should contain 3 Databases, not "
                     + databases.size(),
-                databases.size(),
-                2);
+                3,
+                databases.size());
 
         // check that the default database and the turbine database
         // refer to the same object
         Database defaultDatabase = Torque.getDatabase(DEFAULT_NAME);
-        Database turbineDatabase = Torque.getDatabase(TURBINE_NAME);
+        Database turbineDatabase = Torque.getDatabase(MYSQL_NAME);
 
         assertNotSame("The default database and the turbine database "
                         + "are the same object",
@@ -140,7 +139,7 @@ public class TorqueInstanceTest extends 
         // because we have not properly initialized the DataSourceFactory,
         // closing the DatasourceFactory down would result in an error.
         // So we have to remove the reference to the DatasourceFactory.
-        Torque.getDatabase(TURBINE_NAME).setDataSourceFactory(null);
+        Torque.getDatabase(MYSQL_NAME).setDataSourceFactory(null);
 
         Torque.shutdown();
         assertFalse("Torque.isInit() should return false after shutdown",
@@ -217,7 +216,7 @@ public class TorqueInstanceTest extends 
          */
         public void doBuild() throws TorqueException
         {
-            dbMap = Torque.getDatabaseMap(TURBINE_NAME);
+            dbMap = Torque.getDatabaseMap(MYSQL_NAME);
 
             dbMap.addTable(tableName);
             TableMap tMap = dbMap.getTable(tableName);

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java Sun Jan  9 13:43:29 2011
@@ -32,7 +32,6 @@ public class DBOracleTest extends TestCa
      */
     public void testSelectColumnsForLimitOffset()
     {
-        Query query = new Query();
         UniqueList input = new UniqueList();
         input.add("c1");
         input.add("c2");
@@ -41,30 +40,34 @@ public class DBOracleTest extends TestCa
         input.add("t.c1 a4");
 
         // A list with no duplicates must remain unchanged
-        query.setSelectClause(new UniqueList(input));
+        Query query = new Query();
+        query.getSelectClause().addAll(input);
         new DBOracle().generateLimits(query, 0, 1);
         assertEquals(input, query.getSelectClause());
 
         // double column names must be aliased
+        query = new Query();
         input.set(1, "t.c1");
-        query.setSelectClause(new UniqueList(input));
+        query.getSelectClause().addAll(input);
         new DBOracle().generateLimits(query, 0, 1);
         UniqueList expected = new UniqueList(input);
         expected.set(1, "t.c1 a0");
         assertEquals(expected, query.getSelectClause());
 
         // a column name which is the same as an alias name must be replaced
+        query = new Query();
         input.set(1, "c2");
         input.set(0, "t.a1");
-        query.setSelectClause(new UniqueList(input));
+        query.getSelectClause().addAll(input);
         new DBOracle().generateLimits(query, 0, 1);
         expected = new UniqueList(input);
         expected.set(0, "t.a1 a0");
         assertEquals(query.getSelectClause(), expected);
 
         // triple column names must be made unique
+        query = new Query();
         input.set(1, "t2.a1");
-        query.setSelectClause(new UniqueList(input));
+        query.getSelectClause().addAll(input);
         new DBOracle().generateLimits(query, 0, 1);
         expected = new UniqueList(input);
         expected.set(0, "t.a1 a0");

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java Sun Jan  9 13:43:29 2011
@@ -21,6 +21,7 @@ package org.apache.torque.util;
 
 import java.util.Calendar;
 import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Map;
 
@@ -64,10 +65,37 @@ public class CriteriaTest extends BaseTe
     /**
      * Initializes the criteria.
      */
-    public void setUp()
+    public void setUp() throws TorqueException
     {
         super.setUp();
         c = new Criteria();
+        DatabaseMap databaseMap = Torque.getDatabaseMap("postgresql");
+        TableMap tableMap = new TableMap("TABLE", 4, databaseMap);
+        databaseMap.addTable(tableMap);
+        {
+            ColumnMap columnMap1 = new ColumnMap("COLUMN1", tableMap);
+            columnMap1.setType(new String(""));
+            columnMap1.setJavaType("String");
+            tableMap.addColumn(columnMap1);
+        }
+        {
+            ColumnMap columnMap2 = new ColumnMap("COLUMN2", tableMap);
+            columnMap2.setType(new String(""));
+            columnMap2.setJavaType("String");
+            tableMap.addColumn(columnMap2);
+        }
+        {
+            ColumnMap columnMap3 = new ColumnMap("COLUMN3", tableMap);
+            columnMap3.setType(new String(""));
+            columnMap3.setJavaType("String");
+            tableMap.addColumn(columnMap3);
+        }
+        {
+            ColumnMap columnMap4 = new ColumnMap("COLUMN4", tableMap);
+            columnMap4.setType(new Integer(0));
+            columnMap4.setJavaType("Integer");
+            tableMap.addColumn(columnMap4);
+        }
     }
 
     /**
@@ -92,7 +120,7 @@ public class CriteriaTest extends BaseTe
     /**
      * test various properties of Criterion and nested criterion
      */
-    public void testNestedCriterion()
+    public void testNestedCriterion() throws TorqueException
     {
         final String table2 = "myTable2";
         final String column2 = "myColumn2";
@@ -120,117 +148,101 @@ public class CriteriaTest extends BaseTe
             c.getNewCriterion(table5, column5, (Object) value5, Criteria.EQUAL);
 
         crit2.and(crit3).or(crit4.and(crit5));
-        String expect =
-            "((myTable2.myColumn2='myValue2' "
-                + "AND myTable3.myColumn3='myValue3') "
-            + "OR (myTable4.myColumn4='myValue4' "
-                + "AND myTable5.myColumn5='myValue5'))";
-        String result = crit2.toString();
-        assertEquals(expect, result);
+        c.add(crit2);
+        c.addSelectColumn("*");
 
-        Criteria.Criterion crit6 =
-            c.getNewCriterion(table2, column2, (Object) value2, Criteria.EQUAL);
-        Criteria.Criterion crit7 =
-            c.getNewCriterion(table3, column3, (Object) value3, Criteria.EQUAL);
-        Criteria.Criterion crit8 =
-            c.getNewCriterion(table4, column4, (Object) value4, Criteria.EQUAL);
-        Criteria.Criterion crit9 =
-            c.getNewCriterion(table5, column5, (Object) value5, Criteria.EQUAL);
+        String expect =
+            "SELECT * FROM myTable2, myTable3, myTable4, myTable5 WHERE "
+            + "((myTable2.myColumn2=? "
+                + "AND myTable3.myColumn3=?) "
+            + "OR (myTable4.myColumn4=? "
+                + "AND myTable5.myColumn5=?))";
+        Query result = BasePeer.createQuery(c);
+        assertEquals(expect, result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(4, preparedStatementReplacements.size());
+        assertEquals("myValue2", preparedStatementReplacements.get(0));
+        assertEquals("myValue3", preparedStatementReplacements.get(1));
+        assertEquals("myValue4", preparedStatementReplacements.get(2));
+        assertEquals("myValue5", preparedStatementReplacements.get(3));
+
+        c.clear();
+        Criteria.Criterion crit6 = c.getNewCriterion(
+                table2,
+                column2,
+                (Object) value2,
+                Criteria.LESS_THAN);
+        Criteria.Criterion crit7 = c.getNewCriterion(
+                table3,
+                column3,
+                (Object) value3,
+                Criteria.LESS_EQUAL);
+        Criteria.Criterion crit8 = c.getNewCriterion(
+                table4,
+                column4,
+                (Object) value4,
+                Criteria.GREATER_THAN);
+        Criteria.Criterion crit9 = c.getNewCriterion(
+                table5,
+                column5,
+                (Object) value5,
+                Criteria.GREATER_EQUAL);
 
         crit6.and(crit7).or(crit8).and(crit9);
+        c.add(crit6);
+        c.addSelectColumn("*");
         expect =
-            "(((myTable2.myColumn2='myValue2' "
-                    + "AND myTable3.myColumn3='myValue3') "
-                + "OR myTable4.myColumn4='myValue4') "
-                    + "AND myTable5.myColumn5='myValue5')";
-        result = crit6.toString();
-        assertEquals(expect, result);
-
-        // should make sure we have tests for all possibilities
-
-        Criteria.Criterion[] crita = crit2.getAttachedCriterion();
-
-        assertEquals(crit2, crita[0]);
-        assertEquals(crit3, crita[1]);
-        assertEquals(crit4, crita[2]);
-        assertEquals(crit5, crita[3]);
-
-        List tables = crit2.getAllTables();
-
-        assertEquals(crit2.getTable(), tables.get(0));
-        assertEquals(crit3.getTable(), tables.get(1));
-        assertEquals(crit4.getTable(), tables.get(2));
-        assertEquals(crit5.getTable(), tables.get(3));
+            "SELECT * FROM myTable2, myTable3, myTable4, myTable5 WHERE "
+                + "(((myTable2.myColumn2<? "
+                    + "AND myTable3.myColumn3<=?) "
+                + "OR myTable4.myColumn4>?) "
+                    + "AND myTable5.myColumn5>=?)";
+        result = BasePeer.createQuery(c);
+        assertEquals(expect, result.toString());
+        preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(4, preparedStatementReplacements.size());
+        assertEquals("myValue2", preparedStatementReplacements.get(0));
+        assertEquals("myValue3", preparedStatementReplacements.get(1));
+        assertEquals("myValue4", preparedStatementReplacements.get(2));
+        assertEquals("myValue5", preparedStatementReplacements.get(3));
 
         // simple confirmations that equality operations work
-        assertTrue(crit2.hashCode() == crit2.hashCode());
-        assertEquals(crit2.toString(), crit2.toString());
+        Criteria.Criterion compareToCriterion = c.getNewCriterion(
+                table5,
+                column5,
+                (Object) value5,
+                Criteria.GREATER_EQUAL);
+        assertEquals(compareToCriterion.hashCode(),crit9.hashCode());
+        assertEquals("myTable5.myColumn5>=myValue5", crit9.toString());
     }
 
     /**
      * Tests &lt;= and =&gt;.
      */
-    public void testBetweenCriterion()
+    public void testBetweenCriterion() throws TorqueException
     {
         Criteria.Criterion cn1 =
             c.getNewCriterion(
                 "INVOICE.COST",
-                new Integer(1000),
+                1000,
                 Criteria.GREATER_EQUAL);
         Criteria.Criterion cn2 =
             c.getNewCriterion(
                 "INVOICE.COST",
-                new Integer(5000),
+                5000,
                 Criteria.LESS_EQUAL);
         c.add(cn1.and(cn2));
         String expect =
             "SELECT  FROM INVOICE WHERE "
-            + "(INVOICE.COST>=1000 AND INVOICE.COST<=5000)";
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
-
-        assertEquals(expect, result);
-    }
-
-    /**
-     * Verify that AND and OR criterion are nested correctly.
-     */
-    public void testPrecedence()
-    {
-        Criteria.Criterion cn1 =
-            c.getNewCriterion("INVOICE.COST", "1000", Criteria.GREATER_EQUAL);
-        Criteria.Criterion cn2 =
-            c.getNewCriterion("INVOICE.COST", "2000", Criteria.LESS_EQUAL);
-        Criteria.Criterion cn3 =
-            c.getNewCriterion("INVOICE.COST", "8000", Criteria.GREATER_EQUAL);
-        Criteria.Criterion cn4 =
-            c.getNewCriterion("INVOICE.COST", "9000", Criteria.LESS_EQUAL);
-        c.add(cn1.and(cn2));
-        c.or(cn3.and(cn4));
-
-        String expect =
-            "SELECT  FROM INVOICE WHERE "
-            + "((INVOICE.COST>='1000' AND INVOICE.COST<='2000') "
-            + "OR (INVOICE.COST>='8000' AND INVOICE.COST<='9000'))";
-
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
+            + "(INVOICE.COST>=? AND INVOICE.COST<=?)";
+        Query result = BasePeer.createQuery(c);
 
-        assertEquals(expect, result);
+        assertEquals(expect, result.toString());
+        assertEquals(2, result.getPreparedStatementReplacements().size());
+        assertEquals(1000, result.getPreparedStatementReplacements().get(0));
+        assertEquals(5000, result.getPreparedStatementReplacements().get(1));
     }
 
     /**
@@ -238,256 +250,196 @@ public class CriteriaTest extends BaseTe
      * As the output is db specific the test just prints the result to
      * System.out
      */
-    public void testCriterionIgnoreCase()
+    public void testCriterionIgnoreCase() throws TorqueException
     {
-        Criteria myCriteria = new Criteria();
-
-        Criteria.Criterion expected = myCriteria.getNewCriterion(
-                "TABLE.COLUMN", (Object)"FoObAr", Criteria.LIKE);
-        Criteria.Criterion result = expected.setIgnoreCase(true);
-        assertEquals("Criterion mis-match after calling setIgnoreCase(true)",
-                     expected.toString(), result.toString());
+        Criteria.Criterion criterion1 = c.getNewCriterion(
+                "TABLE.COLUMN1", (Object) "FoObAr1", Criteria.LIKE);
+        criterion1.setIgnoreCase(true);
+        Criteria.Criterion criterion2 = c.getNewCriterion(
+                "TABLE.COLUMN2", (Object) "FoObAr2", Criteria.EQUAL);
+        criterion2.setIgnoreCase(true);
+        Criteria.Criterion criterion3 = c.getNewCriterion(
+                "TABLE.COLUMN3", (Object) "FoObAr3", Criteria.EQUAL);
+        Criteria.Criterion criterion4 = c.getNewCriterion(
+                "TABLE.COLUMN4", (Object) new Integer(1), Criteria.EQUAL);
+        criterion4.setIgnoreCase(true);
+        c.add(criterion1.and(criterion2).and(criterion3).and(criterion4));
+        c.addSelectColumn("*");
+        c.setDbName("postgresql");
+        Query result = BasePeer.createQuery(c);
+
+        String expect = "SELECT * FROM TABLE WHERE "
+            + "(((UPPER(TABLE.COLUMN1)=UPPER(?)"
+            + " AND UPPER(TABLE.COLUMN2)=UPPER(?))"
+            + " AND TABLE.COLUMN3=?) AND TABLE.COLUMN4=?)";
+        assertEquals(expect, result.toString());
+        List<Object> replacements = result.getPreparedStatementReplacements();
+        assertEquals(4, replacements.size());
+        assertEquals("FoObAr1", replacements.get(0));
+        assertEquals("FoObAr2", replacements.get(1));
+        assertEquals("FoObAr3", replacements.get(2));
+        assertEquals(new Integer(1), replacements.get(3));
     }
 
     /**
      * Test that true is evaluated correctly.
      */
-    public void testBoolean()
+    public void testBoolean() throws TorqueException
     {
-        Criteria c = new Criteria().add("TABLE.COLUMN", true);
-
-        String expect = "SELECT  FROM TABLE WHERE TABLE.COLUMN=1";
+        c.add("TABLE.COLUMN", true);
 
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
+        Query result = BasePeer.createQuery(c);
 
-        assertEquals(expect, result);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE TABLE.COLUMN=?",
+                result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(Boolean.TRUE, preparedStatementReplacements.get(0));
 
         // test the postgresql variation
         c = new Criteria();
-        Criteria.Criterion cc =
-            c.getNewCriterion("TABLE.COLUMN", Boolean.TRUE, Criteria.EQUAL);
+        c.add("TABLE.COLUMN", true);
+        c.setDbName("postgresql");
 
-        Configuration conf = new BaseConfiguration();
-        conf.addProperty("driver", "org.postgresql.Driver");
-        try
-        {
-            cc.setDB(DBFactory.create("org.postgresql.Driver"));
-        }
-        catch (Exception e)
-        {
-            fail("Exception thrown in DBFactory");
-        }
+        result = BasePeer.createQuery(c);
 
-        assertEquals("TABLE.COLUMN=TRUE", cc.toString());
+        assertEquals(
+                "SELECT  FROM TABLE WHERE TABLE.COLUMN=?",
+                result.toString());
+        preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(Boolean.TRUE, preparedStatementReplacements.get(0));
     }
 
     /**
      * testcase for addDate()
      */
-    public void testAddDate()
+    public void testAddDate() throws TorqueException
     {
-        Criteria c = new Criteria();
-        c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22);
+         c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22);
 
-        String expect = "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN='20030122000000'";
+        Query result = BasePeer.createQuery(c);
 
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            e.printStackTrace();
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
-        assertEquals(expect, result);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN=?",
+                result.toString());
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(
+                new GregorianCalendar(2003, 0, 22).getTime(),
+                preparedStatementReplacements.get(0));
     }
 
     /**
      * testcase for andDate()
      * issue TORQUE-42
      */
-    public void testAndDate()
+    public void testAndDate() throws TorqueException
     {
-        Criteria c = new Criteria();
         c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22, Criteria.GREATER_THAN);
-        c.andDate("TABLE.DATE_COLUMN", 2004, 0, 22, Criteria.LESS_THAN);
-
-        String expect = "SELECT  FROM TABLE WHERE (TABLE.DATE_COLUMN>'20030122000000' AND TABLE.DATE_COLUMN<'20040122000000')";
+        c.andDate("TABLE.DATE_COLUMN", 2004, 2, 24, Criteria.LESS_THAN);
 
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            e.printStackTrace();
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
-        assertEquals(expect, result);
+        Query result = BasePeer.createQuery(c);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE "
+                    + "(TABLE.DATE_COLUMN>? AND TABLE.DATE_COLUMN<?)",
+                result.toString());
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(2, preparedStatementReplacements.size());
+        assertEquals(
+                new GregorianCalendar(2003, 0, 22).getTime(),
+                preparedStatementReplacements.get(0));
+        assertEquals(
+                new GregorianCalendar(2004, 2, 24).getTime(),
+                preparedStatementReplacements.get(1));
     }
 
     /**
      * testcase for add(Date)
      */
-    public void testDateAdd()
+    public void testDateAdd() throws TorqueException
     {
-        Calendar cal = Calendar.getInstance();
-        cal.set(2003, 0, 22, 0, 0, 0);
+        Calendar cal = new GregorianCalendar(2003, 0, 22);
         Date date = cal.getTime();
-        Criteria c = new Criteria();
         c.add("TABLE.DATE_COLUMN", date);
 
-        String expect = "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN='20030122000000'";
-
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            e.printStackTrace();
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
-        assertEquals(expect, result);
+        Query result = BasePeer.createQuery(c);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE "
+                    + "TABLE.DATE_COLUMN=?",
+                result.toString());
+
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(
+                new GregorianCalendar(2003, 0, 22).getTime(),
+                preparedStatementReplacements.get(0));
     }
 
-    public void testCurrentDate()
+    public void testCurrentDate() throws TorqueException
     {
-        Criteria c = new Criteria()
-                .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE)
+        c.add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE)
                 .add("TABLE.TIME_COLUMN", Criteria.CURRENT_TIME);
+        c.addSelectColumn("COUNT(*)");
 
-        String expect = "SELECT  FROM TABLE WHERE TABLE.TIME_COLUMN=CURRENT_TIME AND TABLE.DATE_COLUMN=CURRENT_DATE";
-
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            e.printStackTrace();
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
+        Query result = BasePeer.createQuery(c);
+        assertEquals(
+                "SELECT COUNT(*) FROM TABLE WHERE "
+                    + "TABLE.TIME_COLUMN=CURRENT_TIME "
+                    + "AND TABLE.DATE_COLUMN=CURRENT_DATE",
+                result.toString());
 
-        assertEquals(expect,result);
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(0, preparedStatementReplacements.size());
     }
 
-    public void testCountAster()
+    public void testCriteriaOffsetLimit() throws TorqueException
     {
-        Criteria c = new Criteria()
-                .addSelectColumn("COUNT(*)")
-                .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE)
-                .add("TABLE.TIME_COLUMN", Criteria.CURRENT_TIME);
-
-        String expect = "SELECT COUNT(*) FROM TABLE WHERE TABLE.TIME_COLUMN=CURRENT_TIME AND TABLE.DATE_COLUMN=CURRENT_DATE";
-
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            e.printStackTrace();
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
-
-        assertEquals(expect,result);
+        c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22);
+        c.setOffset(3).setLimit(5);
+        c.addSelectColumn("COUNT(*)");
 
+        Query result = BasePeer.createQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " LIMIT 5 OFFSET 3";
+        assertEquals(expect, result.toString());
     }
 
     /**
-     * This test case has been written to try out the fix applied to resolve
-     * TRQS73 - i.e. ensuring that Criteria.toString() does not alter any limit
-     * or offset that may be stored in the Criteria object.  This testcase
-     * could actually pass without the fix if the database in use does not
-     * support native limits and offsets.
+     * TORQUE-87
      */
-    public void testCriteriaToStringOffset()
+    public void testCriteriaWithOffsetNoLimit() throws TorqueException
     {
-        Criteria c = new Criteria()
-                .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE)
-                .setOffset(3)
-                .setLimit(5);
-
-        String toStringExpect = "Criteria:: TABLE.DATE_COLUMN<=>TABLE.DATE_COLUMN=CURRENT_DATE:  "
-                + "\nCurrent Query SQL (may not be complete or applicable): "
-                + "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN=CURRENT_DATE LIMIT 5 OFFSET 3";
-
-        String cString = c.toString();
-        //System.out.println(cString);
-        assertEquals(toStringExpect, cString);
+        c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22);
+        c.setOffset(3);
+        c.addSelectColumn("COUNT(*)");
 
-        // Note that this is intentionally the same as above as the behaviour is
-        // only observed on subsequent invocations of toString().
-        cString = c.toString();
-        //System.out.println(cString);
-        assertEquals(toStringExpect, cString);
+        Query result = BasePeer.createQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " LIMIT 18446744073709551615 OFFSET 3";
+        assertEquals(expect, result.toString());
     }
 
-    /**
-     * TORQUE-87
-     */
-    public void testCriteriaWithOffsetNoLimit()
+    public void testCriteriaToStringLimit() throws TorqueException
     {
-        Criteria c = new Criteria()
-        .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE)
-        .setOffset(3);
-        
-        String toStringExpect = "Criteria:: TABLE.DATE_COLUMN<=>TABLE.DATE_COLUMN=CURRENT_DATE:  "
-            + "\nCurrent Query SQL (may not be complete or applicable): "
-            + "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN=CURRENT_DATE LIMIT 18446744073709551615 OFFSET 3";
-        
-        String cString = c.toString();
-        //System.out.println(cString);
-        assertEquals(toStringExpect, cString);
-        
-        // Note that this is intentionally the same as above as the behaviour is
-        // only observed on subsequent invocations of toString().
-        cString = c.toString();
-        //System.out.println(cString);
-        assertEquals(toStringExpect, cString);
-    }
-    
-    /**
-     * This test case has been written to try out the fix applied to resolve
-     * TRQS73 - i.e. ensuring that Criteria.toString() does not alter any limit
-     * or offset that may be stored in the Criteria object.  This testcase
-     * could actually pass without the fix if the database in use does not
-     * support native limits and offsets.
-     */
-    public void testCriteriaToStringLimit()
-    {
-        Criteria c = new Criteria()
-                .add("TABLE.DATE_COLUMN", Criteria.CURRENT_DATE)
-                .setLimit(5);
-
-        String toStringExpect = "Criteria:: TABLE.DATE_COLUMN<=>TABLE.DATE_COLUMN=CURRENT_DATE:  "
-                + "\nCurrent Query SQL (may not be complete or applicable): "
-                + "SELECT  FROM TABLE WHERE TABLE.DATE_COLUMN=CURRENT_DATE LIMIT 5";
-
-        String cString = c.toString();
-        //System.out.println(cString);
-        assertEquals(toStringExpect, cString);
-
-        // Note that this is intentionally the same as above as the behaviour is
-        // only observed on subsequent invocations of toString().
-        cString = c.toString();
-        //System.out.println(cString);
-        assertEquals(toStringExpect, cString);
+        c.addDate("TABLE.DATE_COLUMN", 2003, 0, 22);
+        c.setLimit(5);
+        c.addSelectColumn("COUNT(*)");
+
+        Query result = BasePeer.createQuery(c);
+        String expect
+            = "SELECT COUNT(*) FROM TABLE WHERE TABLE.DATE_COLUMN=?"
+                + " LIMIT 5";
+        assertEquals(expect, result.toString());
     }
 
     /**
@@ -495,25 +447,22 @@ public class CriteriaTest extends BaseTe
      * get replaced through Criteria.EQUAL if there are no SQL wildcards
      * in the given value.
      */
-    public void testLikeWithoutWildcards()
+    public void testLikeWithoutWildcards() throws TorqueException
     {
-        Criteria c = new Criteria();
         c.add("TABLE.COLUMN", (Object) "no wildcards", Criteria.LIKE);
 
-        String expect = "SELECT  FROM TABLE WHERE TABLE.COLUMN = 'no wildcards'";
-
-        String result = null;
-        try
-        {
-            result = BasePeer.createQueryString(c);
-        }
-        catch (TorqueException e)
-        {
-            e.printStackTrace();
-            fail("TorqueException thrown in BasePeer.createQueryString()");
-        }
-
-        assertEquals(expect, result);
+        Query result = BasePeer.createQuery(c);
+        assertEquals(
+                "SELECT  FROM TABLE WHERE "
+                    + "TABLE.COLUMN=?",
+                result.toString());
+
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals(
+                "no wildcards",
+                preparedStatementReplacements.get(0));
     }
 
     /**
@@ -523,16 +472,15 @@ public class CriteriaTest extends BaseTe
      */
     public void testNotLikeWithoutWildcards()
     {
-        Criteria c = new Criteria();
         c.add("TABLE.COLUMN", (Object) "no wildcards", Criteria.NOT_LIKE);
 
-        String firstExpect = "SELECT  FROM TABLE WHERE TABLE.COLUMN != 'no wildcards'";
-        String secondExpect = "SELECT  FROM TABLE WHERE TABLE.COLUMN <> 'no wildcards'";
+        String firstExpect = "SELECT  FROM TABLE WHERE TABLE.COLUMN!=?";
+        String secondExpect = "SELECT  FROM TABLE WHERE TABLE.COLUMN<>?";
 
-        String result = null;
+        Query result = null;
         try
         {
-            result = BasePeer.createQueryString(c);
+            result = BasePeer.createQuery(c);
         }
         catch (TorqueException e)
         {
@@ -540,7 +488,12 @@ public class CriteriaTest extends BaseTe
             fail("TorqueException thrown in BasePeer.createQueryString()");
         }
 
-        assertTrue(result.equals(firstExpect) || result.equals(secondExpect));
+        assertTrue(result.toString().equals(firstExpect)
+                || result.toString().equals(secondExpect));
+        List<Object> preparedStatementReplacements
+                = result.getPreparedStatementReplacements();
+        assertEquals(1, preparedStatementReplacements.size());
+        assertEquals("no wildcards", preparedStatementReplacements.get(0));
     }
 
     /**
@@ -667,21 +620,22 @@ public class CriteriaTest extends BaseTe
 
         // check that alias'ed tables are referenced by their alias
         // name when added to the select clause.
-        Criteria criteria = new Criteria();
-        criteria.addSelectColumn("AUTHOR.NAME");
-        criteria.addAlias("a", "AUTHOR");
-        criteria.addJoin(
+        c.addSelectColumn("AUTHOR.NAME");
+        c.addAlias("a", "AUTHOR");
+        c.addJoin(
                 "AUTHOR.AUTHOR_ID",
                 "a." + "AUTHOR_ID");
-        criteria.addAscendingOrderByColumn(
+        c.addAscendingOrderByColumn(
                 "a.NAME");
 
-        String result = BasePeer.createQueryString(criteria);
+        Query result = BasePeer.createQuery(c);
         assertEquals("SELECT AUTHOR.NAME, a.NAME "
                     + "FROM AUTHOR, AUTHOR a "
                     + "WHERE AUTHOR.AUTHOR_ID=a.AUTHOR_ID "
                     + "ORDER BY a.NAME ASC",
-                result);
+                result.toString());
+        List<Object> preparedStatementReplacements
+            = result.getPreparedStatementReplacements();
+        assertEquals(0, preparedStatementReplacements.size());
     }
-
 }

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java Sun Jan  9 13:43:29 2011
@@ -45,17 +45,16 @@ public class QueryTest extends TestCase
      */
     public void testColumns()
     {
-        String expected
-                = "SELECT tableA.column1, tableA.column2, tableB.column1 FROM ";
         Query query = new Query();
 
-        UniqueList columns = new UniqueList();
+        UniqueList columns = query.getSelectClause();
         columns.add("tableA.column1");
         columns.add("tableA.column2");
         columns.add("tableB.column1");
-        query.setSelectClause(columns);
 
-        assertEquals(expected, query.toString());
+        assertEquals(
+                "SELECT tableA.column1, tableA.column2, tableB.column1 FROM ",
+                query.toString());
     }
 
     /**
@@ -68,21 +67,20 @@ public class QueryTest extends TestCase
                 + " AND tableA.B = 1234";
         Query query = new Query();
 
-        UniqueList columns = new UniqueList();
+        UniqueList columns = query.getSelectClause();
         columns.add("tableA.column1");
         columns.add("tableA.column2");
         columns.add("tableB.column1");
-        query.setSelectClause(columns);
 
-        UniqueList tables = new UniqueList();
+        UniqueList<Query.FromElement> tables = query.getFromClause();
+        tables.clear();
         tables.add(new Query.FromElement("tableA", null, null));
         tables.add(new Query.FromElement("tableB", null, null));
-        query.setFromClause(tables);
 
-        UniqueList where = new UniqueList();
+        UniqueList<String> where = query.getWhereClause();
+        where.clear();
         where.add("tableA.A = tableB.A");
         where.add("tableA.B = 1234");
-        query.setWhereClause(where);
 
         assertEquals(expected, query.toString());
     }

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java Sun Jan  9 13:43:29 2011
@@ -20,6 +20,8 @@ package org.apache.torque.util;
  */
 
 import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
 
 import junit.framework.TestCase;
 
@@ -62,50 +64,36 @@ public class SqlExpressionTest extends T
         }
     }
 
-        /**
-         * Test for String buildInnerJoin(String, String)
-         */
-        public void testBuildInnerJoinStringString()
-        {
-        String result = SqlExpression.buildInnerJoin("TA.COLA", "TB.COLB");
-        assertEquals(result, "TA.COLA=TB.COLB");
-        }
-
-        /**
-         * Test for String buildInnerJoin(String, String, boolean, DB)
-         */
-        public void testBuildInnerJoinStringStringbooleanDB()
+    /**
+     * Test for buildPsIn.
+     */
+    public void testBuildPsIn()
         {
-        String result = SqlExpression.buildInnerJoin("TA.COLA", "TB.COLB",
-                true, db);
-        assertEquals(result, "TA.COLA=TB.COLB");
-        }
+        String[] values = new String[] { "42", "43", "44" };
+        StringBuilder result = new StringBuilder();
+        List<Object> replacements = new ArrayList<Object>();
+        SqlExpression.buildPsIn("COL", values, SqlEnum.IN, "?",
+                false, db, result, replacements);
+        assertEquals("COL IN (?)", result.toString());
+        assertEquals(1, replacements.size());
+        assertEquals(values, replacements.get(0));
+    }
 
-        /**
-         * Test for String buildIn(String, Object, SqlEnum, boolean, DB)
-         */
-        public void testBuildInStringObjectSqlEnumbooleanDB()
+    /**
+     * Test for buildPsIn with ignore case
+     */
+    public void testBuildPsInIgnoreCase()
         {
         String[] values = new String[] { "42", "43", "44" };
-        String result = SqlExpression.buildIn("COL", values, SqlEnum.IN,
-                true, db);
-        // It seems the order of the values is different for jdk1.3 vs 1.4, etc.
-        // In any case, the order is not significant.
-        if (result.equals("COL IN ('43','42','44')"))
-        {
-            // jdk 1.6
-            assertEquals(result, "COL IN ('43','42','44')");
-        }
-        else if (result.equals("COL IN ('42','43','44')"))
-        {
-            // jdk 1.4
-            assertEquals(result, "COL IN ('42','43','44')");
-        }
-        else
-        {
-            // jdk 1.3
-            assertEquals(result, "COL IN ('43','44','42')");
-        }
+        StringBuilder result = new StringBuilder();
+        List<Object> replacements = new ArrayList<Object>();
+        SqlExpression.buildPsIn("COL", values, SqlEnum.IN, "?",
+                true, db, result, replacements);
+        assertEquals("COL IN (?,?,?)", result.toString());
+        assertEquals(3, replacements.size());
+        assertEquals(values[0], replacements.get(0));
+        assertEquals(values[1], replacements.get(1));
+        assertEquals(values[2], replacements.get(2));
     }
 
     public void testLargeBuildInStringObjectSqlEnumbooleanDB()
@@ -117,8 +105,10 @@ public class SqlExpressionTest extends T
             Array.set(values, i, String.valueOf(i));
         }
         long start = System.currentTimeMillis();
-        String result = SqlExpression.buildIn("COL", values, SqlEnum.IN,
-                true, db);
+        StringBuilder result = new StringBuilder();
+        List<Object> replacements = new ArrayList<Object>();
+        SqlExpression.buildPsIn("COL", values, SqlEnum.IN, "?",
+                true, db, result, replacements);
         long end =  System.currentTimeMillis();
         System.out.println("large buildIn took " + (end - start) + " milliseconds");
     }
@@ -126,14 +116,36 @@ public class SqlExpressionTest extends T
     /**
      * Test whether LIKE clauses are built correctly.
      */
-    public void testBuildLike()
+    public void testBuildPsLike()
     {
-        String result = SqlExpression.buildLike(
-                "COL", "fre%", SqlEnum.LIKE, false, db);
-        assertEquals("COL LIKE fre%", result);
-
-        result = SqlExpression.buildLike(
-                "COL", "50\\\\%", SqlEnum.LIKE, false, db);
-        assertEquals("COL = 50%", result);
+        StringBuilder whereClause = new StringBuilder();
+        List<Object> replacements = new ArrayList<Object>();
+        SqlExpression.buildPsLike(
+                "COL",
+                "fre%",
+                "?",
+                SqlEnum.LIKE,
+                false,
+                db,
+                whereClause,
+                replacements);
+        assertEquals("COL LIKE ?", whereClause.toString());
+        assertEquals(1, replacements.size());
+        assertEquals("fre%", replacements.get(0));
+
+        whereClause = new StringBuilder();
+        replacements = new ArrayList<Object>();
+        SqlExpression.buildPsLike(
+                "COL",
+                "50\\%",
+                "?",
+                SqlEnum.LIKE,
+                false,
+                db,
+                whereClause,
+                replacements);
+        assertEquals("COL=?", whereClause.toString());
+        assertEquals(1, replacements.size());
+        assertEquals("50%", replacements.get(0));
     }
 }

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/resources/Torque.properties
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/resources/Torque.properties?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/resources/Torque.properties (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/test/resources/Torque.properties Sun Jan  9 13:43:29 2011
@@ -16,10 +16,11 @@
 # under the License.
 
 
-torque.database.default = turbine
-torque.database.turbine.adapter=mymysql
-torque.database.turbine.adapter.mymysql.className=org.apache.torque.adapter.DBMM
-torque.dsfactory.turbine.factory= org.apache.torque.dsfactory.SharedPoolDataSourceFactory
+torque.database.default = mysql
+torque.database.mysql.adapter=mysql
+torque.dsfactory.mysql.factory= org.apache.torque.dsfactory.SharedPoolDataSourceFactory
+torque.database.postgresql.adapter=postgresql
+torque.dsfactory.postgresql.factory= org.apache.torque.dsfactory.SharedPoolDataSourceFactory
 torque.idbroker.prefetch=false
 
 

Modified: db/torque/torque4/branches/trunk-without-village/torque-test/src/main/schema/test-schema.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-test/src/main/schema/test-schema.xml?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-test/src/main/schema/test-schema.xml (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-test/src/main/schema/test-schema.xml Sun Jan  9 13:43:29 2011
@@ -186,7 +186,7 @@
 
   <!-- BOOLEANCHAR not working? -->
   <table name="boolean_check" idMethod="none">
-    <column name="test_key" required="true" primaryKey="true" type="CHAR" size="10" />
+    <column name="test_key" required="true" primaryKey="true" type="CHAR" size="2" />
     <column name="bint_value" required="true" type="BOOLEANINT" />
     <column name="bchar_value" required="true" type="BOOLEANCHAR" />
   </table>

Modified: db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1056930&r1=1056929&r2=1056930&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java Sun Jan  9 13:43:29 2011
@@ -101,6 +101,7 @@ import org.apache.torque.test.RAbPeer;
 import org.apache.torque.util.BasePeer;
 import org.apache.torque.util.CountHelper;
 import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Query;
 import org.apache.torque.util.Transaction;
 
 /**
@@ -176,7 +177,6 @@ public class DataTest extends BaseRuntim
         }
         // clean booleancheck table (because insert uses fixed keys)
         Criteria criteria = new Criteria();
-        criteria.add(BooleanCheckPeer.TEST_KEY, (Object) null, Criteria.NOT_EQUAL);
         BooleanCheckPeer.doDelete(criteria);
 
         BooleanCheck bc = new BooleanCheck();
@@ -294,7 +294,7 @@ public class DataTest extends BaseRuntim
         criteria.setSingleRecord(true);
         criteria.setLimit(1);
         criteria.setOffset(5);
-        List books = BookPeer.doSelect(criteria);
+        List<Book> books = BookPeer.doSelect(criteria);
         assertTrue("List should have 1 books, not " + books.size(),
                 books.size() == 1);
 
@@ -350,8 +350,8 @@ public class DataTest extends BaseRuntim
         assertEquals("testNullSelects()" 
                 + "Result size should be 1 but is " 
                 + result.size(),
-                result.size(),
-                1);
+                1,
+                result.size());
         nvt = (NullValueTable) result.get(0);
         assertEquals("testNullSelects()"
                 + "Text1 should be \"text\" but is"
@@ -372,8 +372,8 @@ public class DataTest extends BaseRuntim
         assertEquals("testNullSelects()" 
                 + "Result size should be 1 but is " 
                 + result.size(),
-                result.size(),
-                1);
+                1,
+                result.size());
         nvt = (NullValueTable) result.get(0);
         assertEquals("testNullSelects()"
                 + "Text2 should be \"text\" but is"
@@ -563,11 +563,12 @@ public class DataTest extends BaseRuntim
         Criteria criteria = new Criteria();
         criteria.add(BooleanCheckPeer.BCHAR_VALUE, new Boolean(true));
         criteria.add(BooleanCheckPeer.BINT_VALUE, new Boolean(true));
-        List booleanCheckList = BooleanCheckPeer.doSelect(criteria);
+        List<BooleanCheck> booleanCheckList
+            = BooleanCheckPeer.doSelect(criteria);
         assertTrue("Should have read 1 dataset with both values true "
                 + "but read " + booleanCheckList.size(),
                 booleanCheckList.size() == 1);
-        BooleanCheck booleanCheck = (BooleanCheck) booleanCheckList.get(0);
+        BooleanCheck booleanCheck = booleanCheckList.get(0);
         // use trim() for testkey because some databases will return the
         // testkey filled up with blanks, as it is defined as char(10)
         assertTrue("Primary key of data set should be t1 but is "
@@ -581,7 +582,7 @@ public class DataTest extends BaseRuntim
         assertTrue("Should have read 1 dataset with both values false "
                 + "but read " + booleanCheckList.size(),
                 booleanCheckList.size() == 1);
-        booleanCheck = (BooleanCheck) booleanCheckList.get(0);
+        booleanCheck = booleanCheckList.get(0);
         assertTrue("Primary key of data set should be f1 but is "
                 + booleanCheck.getTestKey().trim(),
                 "f1".equals(booleanCheck.getTestKey().trim()));
@@ -1396,7 +1397,7 @@ public class DataTest extends BaseRuntim
         Criteria criteria = new Criteria();
         criteria.add(AuthorPeer.NAME, author.getName().toLowerCase());
         criteria.setIgnoreCase(true);
-        List result = AuthorPeer.doSelect(criteria);
+        List<Author> result = AuthorPeer.doSelect(criteria);
         assertTrue("Size of result is not 1, but " + result.size(), 
                 result.size() == 1);
 
@@ -1411,6 +1412,49 @@ public class DataTest extends BaseRuntim
         assertTrue("Size of result is not 1, but " + result.size(), 
                 result.size() == 1);
         
+        // Test ignore case in criterion
+        criteria = new Criteria();
+        Criteria.Criterion criterion1 = criteria.getNewCriterion(
+                AuthorPeer.NAME,
+                author.getName().toLowerCase(),
+                Criteria.EQUAL);
+        criterion1.setIgnoreCase(true);
+        Criteria.Criterion criterion2 = criteria.getNewCriterion(
+                AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
+        criterion1.and(criterion2);
+
+        result = AuthorPeer.doSelect(criteria);
+
+        // ignore case should not be set either in Criteria
+        // nor in other criterions
+        assertFalse(criteria.isIgnoreCase());
+        assertFalse(criterion2.isIgnoreCase());
+        assertTrue("Size of result is not 1, but " + result.size(), 
+                result.size() == 1);
+
+        
+        // Test ignore case in attached criterion
+        criteria = new Criteria();
+        criterion1 = criteria.getNewCriterion(
+                AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
+        criterion2 = criteria.getNewCriterion(
+                AuthorPeer.NAME,
+                author.getName().toLowerCase(),
+                Criteria.EQUAL);
+        criterion2.setIgnoreCase(true);
+        criterion1.and(criterion2);
+
+        result = AuthorPeer.doSelect(criteria);
+
+        // ignore case should not be set either in Criteria
+        // nor in other criterions
+        assertFalse(criteria.isIgnoreCase());
+        assertFalse(criterion1.isIgnoreCase());
+
+        assertTrue("Size of result is not 1, but " + result.size(), 
+                result.size() == 1);
+
+
         // Check that case is not ignored if ignoreCase is not set
         // This is known not to work for mysql
         author = new Author();
@@ -1450,6 +1494,24 @@ public class DataTest extends BaseRuntim
             result = AuthorPeer.doSelect(criteria);
             assertTrue("Size of result is not 1, but " + result.size(), 
                     result.size() == 1);
+
+            // Test different ignore cases in criterions
+            criteria = new Criteria();
+            criterion1 = criteria.getNewCriterion(
+                    AuthorPeer.NAME, 
+                    author.getName().toLowerCase(),
+                    Criteria.NOT_EQUAL);
+            criterion2 = criteria.getNewCriterion(
+                    AuthorPeer.NAME,
+                    author.getName().toLowerCase(),
+                    Criteria.EQUAL);
+            criterion2.setIgnoreCase(true);
+            criterion1.and(criterion2);
+            criteria.add(criterion1);
+
+            result = AuthorPeer.doSelect(criteria);
+            assertTrue("Size of result is not 1, but " + result.size(), 
+                    result.size() == 1);
         }
 
         // check ignore case in order by
@@ -1622,6 +1684,21 @@ public class DataTest extends BaseRuntim
     }
 
     /**
+     * Checks that Criteria.CURRENT_DATE and Criteria.CURRENT_TIME work
+     * (i.e. do not throw an exception).
+     * Other behaviour is difficult to test as database implementations differ.
+     *
+     * @throws TorqueException if a problem occurs.
+     */
+    public void testCurrentDate() throws TorqueException
+    {
+        Criteria c = new Criteria();
+        c.add(DateTestPeer.DATE_VALUE, Criteria.CURRENT_DATE);
+        c.add(DateTestPeer.TIME_VALUE, Criteria.CURRENT_TIME);
+        List<DateTest> result = DateTestPeer.doSelect(c);
+    }
+
+    /**
      * tests whether large primary keys are inserted and read correctly
      * @throws Exception if the test fails
      */
@@ -1905,42 +1982,6 @@ public class DataTest extends BaseRuntim
                 clobTest.getClobValue().equals(readClobTest.getClobValue()));
     }
 
-
-    /**
-     * Test whether we can execute queries as prepared statements
-     * @throws Exception
-     */
-    public void testPreparedStatements() throws Exception
-    {
-        // clean LargePk table
-        Criteria criteria = new Criteria();
-        criteria.add(
-                LargePkPeer.LARGE_PK_ID,
-                (Long) null,
-                Criteria.NOT_EQUAL);
-        LargePkPeer.doDelete(criteria);
-
-        LargePk largePk = new LargePk();
-        largePk.setLargePkId(1);
-        largePk.setName("testLargePk");
-        largePk.save();
-
-        largePk = new LargePk();
-        largePk.setLargePkId(2);
-        largePk.setName("testLargePk");
-        largePk.save();
-
-        criteria = new Criteria();
-        criteria.add(LargePkPeer.LARGE_PK_ID, 2, Criteria.LESS_THAN);
-        LargePkPeer.addSelectColumns(criteria);
-        List<LargePk> result = BasePeer.doPSSelect(
-                criteria, new LargePkPeer.LargePkRecordMapper(),
-                LargePkPeer.getTableMap());
-        assertTrue("Size of largePk list should be 1 but is "
-                + result.size(),
-                result.size() == 1);
-    }
-
     /**
      * Test whether equals() is working correctly
      * @throws Exception
@@ -2151,12 +2192,31 @@ public class DataTest extends BaseRuntim
                 inheritanceObjects.get(3).getClass());
     }
 
+    /**
+     * Checks whether selects with unqualified column names work.
+     *
+     * @throws Exception if a problem occurs.
+     */
+    public void testUnqualifiedColumnNames() throws Exception
+    {
+        cleanBookstore();
+        Author author = new Author();
+        author.setName("Joshua Bloch");
+        author.save();
+        
+        Criteria criteria = new Criteria();
+        criteria.add(AuthorPeer.AUTHOR_ID, (Object) null, Criteria.NOT_EQUAL);
+        criteria.add("name", (Object) "Joshua Bloch", Criteria.EQUAL);
+        List<Author> authors = AuthorPeer.doSelect(criteria);
+        assertEquals(1, authors.size());
+    }
+    
     public void testEscaping() throws Exception
     {
         String[] authorNames 
                 = {"abc", "bbc", "a_c", "a%c", "a\\c", "a\"c", "a'c"};
 
-        Map likeResults = new HashMap();
+        Map<String, String> likeResults = new HashMap<String, String>();
 
         likeResults.put("a\\_c", "a_c");
         likeResults.put("a\\_%", "a_c");
@@ -2202,16 +2262,14 @@ public class DataTest extends BaseRuntim
                     author.getName());
         }
 
-        for (Iterator likeResultIt = likeResults.entrySet().iterator(); 
-                likeResultIt.hasNext(); )
+        for (Map.Entry<String, String> likeResult : likeResults.entrySet())
         {
-            Map.Entry likeResult = (Map.Entry) likeResultIt.next();
             Criteria criteria = new Criteria();
             criteria.add(
                     AuthorPeer.NAME, 
-                    likeResult.getKey(), 
+                    (Object) likeResult.getKey(), 
                     Criteria.LIKE);
-            List authorList = AuthorPeer.doSelect(criteria);
+            List<Author> authorList = AuthorPeer.doSelect(criteria);
             assertEquals(
                     "AuthorList should contain one author"
                         + " when querying for " + likeResult.getKey(), 



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


Mime
View raw message