db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1100619 - in /db/torque/torque4/trunk/torque-runtime/src: main/java/org/apache/torque/adapter/ main/java/org/apache/torque/oid/ main/java/org/apache/torque/sql/ main/java/org/apache/torque/util/ test/java/org/apache/torque/adapter/ test/ja...
Date Sat, 07 May 2011 20:37:20 GMT
Author: tfischer
Date: Sat May  7 20:37:19 2011
New Revision: 1100619

URL: http://svn.apache.org/viewvc?rev=1100619&view=rev
Log:
Move SQL generation classes from util package into their own sql package.

Added:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java
      - copied, changed from r1070039, db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/JoinBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/PreparedStatementPart.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
      - copied, changed from r1056939, db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Query.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
      - copied, changed from r1083546, db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SQLBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/WhereClauseExpression.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/package.html
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java
      - copied, changed from r1056939, db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
      - copied, changed from r1070058, db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java
Removed:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/JoinBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Query.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SQLBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/SqlBuilderTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java
Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractDBAdapter.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBDB2App.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBMM.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBOracle.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBPostgres.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBSybase.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractDBAdapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractDBAdapter.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractDBAdapter.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractDBAdapter.java Sat May  7 20:37:19 2011
@@ -27,7 +27,7 @@ import java.util.Hashtable;
 import java.util.Map;
 
 import org.apache.torque.TorqueException;
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 import org.apache.torque.util.functions.Aggregate;
 import org.apache.torque.util.functions.FunctionEnum;
 

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java Sat May  7 20:37:19 2011
@@ -25,7 +25,7 @@ import java.sql.SQLException;
 import java.util.Date;
 
 import org.apache.torque.TorqueException;
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 import org.apache.torque.util.functions.FunctionEnum;
 
 /**

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBDB2App.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBDB2App.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBDB2App.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBDB2App.java Sat May  7 20:37:19 2011
@@ -22,7 +22,7 @@ package org.apache.torque.adapter;
 import java.sql.Connection;
 import java.sql.SQLException;
 
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 
 /**
  * This is used to connect via the Application-Driver to DB2

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBMM.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBMM.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBMM.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBMM.java Sat May  7 20:37:19 2011
@@ -25,7 +25,7 @@ import java.sql.Statement;
 import java.util.Date;
 import java.text.SimpleDateFormat;
 
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 
 /**
  * This is used in order to connect to a MySQL database using the MM
@@ -144,7 +144,7 @@ public class DBMM extends AbstractDBAdap
     {
         if (offset > 0)
         {
-            if (limit >=0 )
+            if (limit >= 0)
             {
                 query.setLimit(Integer.toString(limit));
             }

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBOracle.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBOracle.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBOracle.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBOracle.java Sat May  7 20:37:19 2011
@@ -28,7 +28,7 @@ import java.util.HashSet;
 import java.util.ListIterator;
 import java.util.Set;
 
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 import org.apache.torque.util.UniqueList;
 
 /**

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBPostgres.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBPostgres.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBPostgres.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBPostgres.java Sat May  7 20:37:19 2011
@@ -25,7 +25,7 @@ import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 
 /**
  * This is used to connect to PostgresQL databases.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBSybase.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBSybase.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBSybase.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBSybase.java Sat May  7 20:37:19 2011
@@ -26,9 +26,7 @@ import java.text.SimpleDateFormat;
 import java.util.Date;
 
 import org.apache.torque.TorqueException;
-import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Query;
-import org.apache.torque.util.SqlExpression;
+import org.apache.torque.sql.Query;
 
 /**
  * This is used to connect to a Sybase database using Sybase's

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java Sat May  7 20:37:19 2011
@@ -30,8 +30,8 @@ import org.apache.torque.om.mapper.Integ
 import org.apache.torque.om.mapper.LongMapper;
 import org.apache.torque.om.mapper.RecordMapper;
 import org.apache.torque.om.mapper.StringMapper;
+import org.apache.torque.sql.SqlBuilder;
 import org.apache.torque.util.BasePeer;
-import org.apache.torque.util.SQLBuilder;
 
 /**
  * This generator works with databases that have an sql syntax that
@@ -178,7 +178,7 @@ public class AutoIncrementIdGenerator im
                 RecordMapper<T> mapper)
             throws TorqueException
     {
-        String tableName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
+        String tableName = SqlBuilder.getFullTableName(String.valueOf(keyInfo), name);
         String idSql = dbAdapter.getIDMethodSQL(tableName);
 
         List<T> result = BasePeer.doSelect(idSql, mapper, null, connection);

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java Sat May  7 20:37:19 2011
@@ -30,8 +30,8 @@ import org.apache.torque.om.mapper.Integ
 import org.apache.torque.om.mapper.LongMapper;
 import org.apache.torque.om.mapper.RecordMapper;
 import org.apache.torque.om.mapper.StringMapper;
+import org.apache.torque.sql.SqlBuilder;
 import org.apache.torque.util.BasePeer;
-import org.apache.torque.util.SQLBuilder;
 
 /**
  * This generator works with databases that have an sql syntax for
@@ -173,7 +173,7 @@ public class SequenceIdGenerator impleme
             throws TorqueException
     {
         String sequenceName
-                = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
+                = SqlBuilder.getFullTableName(String.valueOf(keyInfo), name);
         String idSql = dbAdapter.getIDMethodSQL(sequenceName);
 
         List<T> result = BasePeer.doSelect(idSql, mapper, null, connection);

Copied: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java (from r1070039, db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/JoinBuilder.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java?p2=db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java&p1=db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/JoinBuilder.java&r1=1070039&r2=1100619&rev=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/JoinBuilder.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java Sat May  7 20:37:19 2011
@@ -1,4 +1,4 @@
-package org.apache.torque.util;
+package org.apache.torque.sql;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -24,11 +24,12 @@ import java.util.List;
 import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.DB;
 import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.util.Criteria;
+import org.apache.torque.util.SqlEnum;
+import org.apache.torque.util.UniqueList;
 
 /**
- * Factored out code that is used to generate Join Code. This code comes
- * from BasePeer and is put here to reduce complexity in the BasePeer class.
- * You should not use the methods here directly!
+ * Generates SQL for joins.
  *
  * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
@@ -79,11 +80,11 @@ public final class JoinBuilder
             // check if the column names make sense
             if (leftColumn.indexOf('.') == -1)
             {
-                SQLBuilder.throwMalformedColumnNameException("join", leftColumn);
+                SqlBuilder.throwMalformedColumnNameException("join", leftColumn);
             }
             if (rightColumn.indexOf('.') == -1)
             {
-                SQLBuilder.throwMalformedColumnNameException("join", rightColumn);
+                SqlBuilder.throwMalformedColumnNameException("join", rightColumn);
             }
 
             // get the table names
@@ -93,8 +94,8 @@ public final class JoinBuilder
             String leftTableName = leftColumn.substring(0, dot);
 
             leftTableName =
-                    SQLBuilder.getTableNameForFromClause(leftTableName, criteria);
-            leftTableName = SQLBuilder.getFullTableName(
+                    SqlBuilder.getTableNameForFromClause(leftTableName, criteria);
+            leftTableName = SqlBuilder.getFullTableName(
                     leftTableName,
                     criteria.getDbName());
 
@@ -108,9 +109,9 @@ public final class JoinBuilder
                 dbTableName = rightTableName;
             }
 
-            rightTableName = SQLBuilder.getTableNameForFromClause(
+            rightTableName = SqlBuilder.getTableNameForFromClause(
                     rightTableName, criteria);
-            rightTableName = SQLBuilder.getFullTableName(
+            rightTableName = SqlBuilder.getFullTableName(
                     rightTableName,
                     criteria.getDbName());
 
@@ -122,7 +123,7 @@ public final class JoinBuilder
             {
                 // Do not treat join as explicit join, but add
                 // the join condition to the where clauses
-                if (!SQLBuilder.fromClauseContainsTableName(
+                if (!SqlBuilder.fromClauseContainsTableName(
                             queryFromClause,
                             leftTableName))
                 {
@@ -131,7 +132,7 @@ public final class JoinBuilder
                                     leftTableName, null, null);
                     queryFromClause.add(fromElement);
                 }
-                if (!SQLBuilder.fromClauseContainsTableName(
+                if (!SqlBuilder.fromClauseContainsTableName(
                             queryFromClause,
                             rightTableName))
                 {
@@ -149,11 +150,11 @@ public final class JoinBuilder
                 // This if the case if the fromClause already contains
                 // rightTableName
 
-                if (!SQLBuilder.fromClauseContainsTableName(
+                if (!SqlBuilder.fromClauseContainsTableName(
                             queryFromClause,
                             rightTableName))
                 {
-                    if (!SQLBuilder.fromClauseContainsTableName(
+                    if (!SqlBuilder.fromClauseContainsTableName(
                                 queryFromClause,
                                 leftTableName))
                     {
@@ -172,7 +173,7 @@ public final class JoinBuilder
                 }
                 else
                 {
-                    if (SQLBuilder.fromClauseContainsTableName(
+                    if (SqlBuilder.fromClauseContainsTableName(
                                 queryFromClause,
                                 leftTableName))
                     {

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/PreparedStatementPart.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/PreparedStatementPart.java?rev=1100619&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/PreparedStatementPart.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/PreparedStatementPart.java Sat May  7 20:37:19 2011
@@ -0,0 +1,104 @@
+package org.apache.torque.sql;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+/**
+ * The rendered SQL for a part of a prepared statement.
+ *
+ * @version $Id: $
+ */
+public class PreparedStatementPart
+{
+    /**
+     * The SQL for the part, not null.
+     */
+    private StringBuilder sql = new StringBuilder();
+
+    /**
+     * The replacements for the prepared statement, not null.
+     */
+    private List<Object> preparedStatementReplacements
+        = new ArrayList<Object>();
+
+    /**
+     * Returns the SQL of the part.
+     *
+     * @return the SQL as mutable StringBuilder, not null.
+     */
+    public StringBuilder getSql()
+    {
+        return sql;
+    }
+
+    /**
+     * Returns the list of prepared statement replacements.
+     *
+     * @return the modifiable list of prepared statement replacements, not null.
+     */
+    public List<Object> getPreparedStatementReplacements()
+    {
+        return preparedStatementReplacements;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        int result = sql.hashCode();
+        result = 31 * result + preparedStatementReplacements.hashCode();
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        PreparedStatementPart other = (PreparedStatementPart) obj;
+        if (!sql.equals(other.sql))
+        {
+            return false;
+        }
+        if (!preparedStatementReplacements.equals(
+                other.preparedStatementReplacements))
+        {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public String toString()
+    {
+        return sql + ", preparedStatementReplacements="
+                + preparedStatementReplacements;
+    }
+}

Copied: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java (from r1056939, db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Query.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java?p2=db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java&p1=db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Query.java&r1=1056939&r2=1100619&rev=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Query.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java Sat May  7 20:37:19 2011
@@ -1,4 +1,4 @@
-package org.apache.torque.util;
+package org.apache.torque.sql;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -25,6 +25,8 @@ import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.torque.TorqueException;
+import org.apache.torque.util.SqlEnum;
+import org.apache.torque.util.UniqueList;
 
 /**
  * Contains the various parts of a SQL statement (select, update or delete).

Copied: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java (from r1083546, db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SQLBuilder.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java?p2=db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java&p1=db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SQLBuilder.java&r1=1083546&r2=1100619&rev=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SQLBuilder.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java Sat May  7 20:37:19 2011
@@ -1,4 +1,4 @@
-package org.apache.torque.util;
+package org.apache.torque.sql;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -19,7 +19,9 @@ package org.apache.torque.util;
  * under the License.
  */
 
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
@@ -30,7 +32,12 @@ import org.apache.torque.TorqueException
 import org.apache.torque.adapter.DB;
 import org.apache.torque.map.ColumnMap;
 import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.TableMap;
+import org.apache.torque.om.ObjectKey;
+import org.apache.torque.util.Criteria;
 import org.apache.torque.util.Criteria.Criterion;
+import org.apache.torque.util.SqlEnum;
+import org.apache.torque.util.UniqueList;
 
 /**
  * Factored out code that is used to process SQL tables. This code comes
@@ -41,14 +48,17 @@ import org.apache.torque.util.Criteria.C
  * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
  * @version $Id$
  */
-public final class SQLBuilder
+public final class SqlBuilder
 {
     /** Logging */
-    protected static final Log log = LogFactory.getLog(SQLBuilder.class);
+    protected static final Log log = LogFactory.getLog(SqlBuilder.class);
 
-    /** Function Characters */
-    public static final String[] COLUMN_CHARS = {".", "*"};
-    public static final String[] DELIMITERS = {" ", ",", "(", ")", "<", ">"};
+    /** Delimiters for SQL functions. */
+    public static final String[] FUNCTION_DELIMITERS
+            = {" ", ",", "(", ")", "<", ">"};
+
+    /** The backslash character*/
+    private static final char BACKSLASH = '\\';
 
     /**
      * Private constructor to prevent instantiation.
@@ -56,7 +66,7 @@ public final class SQLBuilder
      * Class contains only static method and should therefore not be
      * instantiated.
      */
-    private SQLBuilder()
+    private SqlBuilder()
     {
     }
 
@@ -159,8 +169,8 @@ public final class SQLBuilder
         }
         String pre = name.substring(0, dotIndex);
         String post = name.substring(dotIndex + 1, name.length());
-        int startIndex = StringUtils.lastIndexOfAny(pre, DELIMITERS);
-        int endIndex = StringUtils.indexOfAny(post, DELIMITERS);
+        int startIndex = StringUtils.lastIndexOfAny(pre, FUNCTION_DELIMITERS);
+        int endIndex = StringUtils.indexOfAny(post, FUNCTION_DELIMITERS);
         if (startIndex < 0 && endIndex < 0)
         {
             return name;
@@ -256,7 +266,7 @@ public final class SQLBuilder
      * @param criteria the criteria from which the select columns are taken.
      * @param query the query to which the select columns should be added.
      * @param dbName the name of the database to use.
-     * 
+     *
      * @throws TorqueException if the select columns can not be processed.
      */
     private static void processSelectColumns(
@@ -377,38 +387,42 @@ public final class SQLBuilder
                 Query query)
             throws TorqueException
     {
-        {
-            criterion.setDB(db);
-            String tableName = criterion.getTable();
+        criterion.setDB(db);
+        String tableName = criterion.getTable();
 
-            if (tableName != null)
-            {
-                // add the table to the from clause, if it is not already
-                // contained there
-                // it is important that this piece of code is executed AFTER
-                // the joins are processed
-                addTableToFromClause(getFullTableName(
-                        tableName,
-                        databaseMap.getName()),
-                        criteria,
-                        query);
-            }
+        if (tableName != null)
+        {
+            // add the table to the from clause, if it is not already
+            // contained there
+            // it is important that this piece of code is executed AFTER
+            // the joins are processed
+            addTableToFromClause(getFullTableName(
+                    tableName,
+                    databaseMap.getName()),
+                    criteria,
+                    query);
+        }
 
+        boolean ignoreCase
+                = criteria.isIgnoreCase() || criterion.isIgnoreCase();
+        {
             String table = criteria.getTableForAlias(tableName);
             if (table == null)
             {
                 table = tableName;
             }
 
-            boolean ignoreCase
-                = ((criteria.isIgnoreCase()
-                        || criterion.isIgnoreCase())
-                    && (databaseMap.getTable(table)
+            TableMap tableMap = databaseMap.getTable(table);
+            if (tableMap != null)
+            {
+                // do not use ignoreCase on columns
+                // which do not contain String values
+                ignoreCase = ignoreCase
+                    && databaseMap.getTable(table)
                             .getColumn(criterion.getColumn())
                             .getType()
-                        instanceof String));
-
-            criterion.setIgnoreCase(ignoreCase);
+                        instanceof String;
+            }
         }
 
         for (int j = 0; j < criterion.getClauses().size(); j++)
@@ -416,14 +430,19 @@ public final class SQLBuilder
             sb.append('(');
         }
         String columnName = criterion.getFullyQualifiedColumnName();
-        SqlExpression.buildPs(
-                columnName,
-                criterion.getValue(),
-                criterion.getComparison(),
-                criterion.isIgnoreCase() || criteria.isIgnoreCase(),
-                criterion.getDb(),
-                sb,
-                query.getPreparedStatementReplacements());
+        WhereClauseExpression whereClausePartInput
+                = new WhereClauseExpression(
+                        columnName,
+                        criterion.getComparison(),
+                        criterion.getValue());
+        PreparedStatementPart whereClausePartOutput
+            = buildPs(
+                whereClausePartInput,
+                ignoreCase,
+                criterion.getDb());
+        sb.append(whereClausePartOutput.getSql());
+        query.getPreparedStatementReplacements().addAll(
+                whereClausePartOutput.getPreparedStatementReplacements());
 
         for (int i = 0; i < criterion.getClauses().size(); i++)
         {
@@ -756,4 +775,365 @@ public final class SQLBuilder
         }
         return tableNameForFromClause;
     }
+
+    /**
+     * Builds an element of the where clause of a prepared statement.
+     *
+     * @param whereClausePart the part of the where clause to build.
+     *        Can be modified during this method.
+     * @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 The database for which the SQL should be created, not null.
+     */
+    public static PreparedStatementPart buildPs(
+                WhereClauseExpression whereClausePart,
+                boolean ignoreCase,
+                DB db)
+            throws TorqueException
+    {
+        PreparedStatementPart result = new PreparedStatementPart();
+
+        // Handle SqlEnum.Custom
+        if (SqlEnum.CUSTOM == whereClausePart.getOperator())
+        {
+            result.getSql().append(whereClausePart.getRValue());
+            return result;
+        }
+
+        // Handle SqlEnum.CURRENT_DATE and SqlEnum.CURRENT_TIME
+        if (whereClausePart.getRValue() instanceof SqlEnum)
+        {
+            result.getSql().append(whereClausePart.getLValue())
+                .append(whereClausePart.getOperator())
+                .append(whereClausePart.getRValue());
+            return result;
+        }
+
+        // If rValue is an ObjectKey, take the value of that ObjectKey.
+        if (whereClausePart.getRValue() instanceof ObjectKey)
+        {
+            whereClausePart.setRValue(
+                    ((ObjectKey) whereClausePart.getRValue()).getValue());
+        }
+
+        /*  If rValue is null, check to see if the operator
+         *  is an =, <>, or !=.  If so, replace the comparison
+         *  with SqlEnum.ISNULL or SqlEnum.ISNOTNULL.
+         */
+        if (whereClausePart.getRValue() == null)
+        {
+            if (whereClausePart.getOperator().equals(SqlEnum.EQUAL))
+            {
+                result.getSql().append(whereClausePart.getLValue())
+                        .append(SqlEnum.ISNULL);
+                return result;
+            }
+            if (whereClausePart.getOperator().equals(SqlEnum.NOT_EQUAL)
+                || whereClausePart.getOperator().equals(
+                        SqlEnum.ALT_NOT_EQUAL))
+            {
+                result.getSql().append(whereClausePart.getLValue())
+                    .append(SqlEnum.ISNOTNULL);
+                return result;
+            }
+        }
+
+        // Handle SqlEnum.ISNULL and SqlEnum.ISNOTNULL
+        if (whereClausePart.getOperator().equals(SqlEnum.ISNULL)
+            || whereClausePart.getOperator().equals(SqlEnum.ISNOTNULL))
+        {
+            result.getSql().append(whereClausePart.getLValue())
+                    .append(whereClausePart.getOperator());
+            return result;
+        }
+
+        // handle Subqueries
+        if (whereClausePart.getRValue() instanceof Criteria)
+        {
+            Query subquery = SqlBuilder.buildQuery(
+                    (Criteria) whereClausePart.getRValue());
+            result.getPreparedStatementReplacements().addAll(
+                    subquery.getPreparedStatementReplacements());
+            result.getSql().append(whereClausePart.getLValue())
+                    .append(whereClausePart.getOperator())
+                    .append("(").append(subquery.toString()).append(")");
+                return result;
+        }
+
+        // handle LIKE and similar
+        if (whereClausePart.getOperator().equals(Criteria.LIKE)
+            || whereClausePart.getOperator().equals(Criteria.NOT_LIKE)
+            || whereClausePart.getOperator().equals(Criteria.ILIKE)
+            || whereClausePart.getOperator().equals(Criteria.NOT_ILIKE))
+        {
+            return buildPsLike(whereClausePart, ignoreCase, db);
+        }
+
+        // handle IN and similar
+        if (whereClausePart.getOperator().equals(Criteria.IN)
+                || whereClausePart.getOperator().equals(Criteria.NOT_IN))
+        {
+            return buildPsIn(whereClausePart, ignoreCase, db);
+        }
+
+        // Standard case
+        result.getPreparedStatementReplacements().add(
+                whereClausePart.getRValue());
+        if (ignoreCase
+            && whereClausePart.getRValue() instanceof String)
+        {
+            result.getSql().append(
+                    db.ignoreCase(whereClausePart.getLValue()))
+                .append(whereClausePart.getOperator())
+                .append(db.ignoreCase("?"));
+        }
+        else
+        {
+            result.getSql().append(whereClausePart.getLValue())
+                .append(whereClausePart.getOperator())
+                .append("?");
+        }
+        return result;
+    }
+
+    /**
+     * Takes a WhereClauseExpression with a LIKE operator
+     * and builds an SQL phrase based on whether wildcards are present
+     * and the state of theignoreCase 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 whereClausePart the part of the where clause to build.
+     *        Can be modified in this method.
+     * @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 The database for which the SQL should be created, not null.
+     *
+     * @return the rendered SQL for the WhereClauseExpression
+     */
+    static PreparedStatementPart buildPsLike(
+                WhereClauseExpression whereClausePart,
+                boolean ignoreCase,
+                DB db)
+            throws TorqueException
+    {
+        if (!(whereClausePart.getRValue() instanceof String))
+        {
+            throw new TorqueException(
+                "rValue must be a String for the operator "
+                    + whereClausePart.getOperator());
+        }
+        String value = (String) whereClausePart.getRValue();
+        // If selection criteria contains wildcards use LIKE otherwise
+        // use = (equals).  Wildcards can be escaped by prepending
+        // them with \ (backslash). However, if we switch from
+        // like to equals, we need to remove the escape characters.
+        // from the wildcards.
+        // So we need two passes: The first replaces * and ? by % and _,
+        // and checks whether we switch to equals,
+        // the second removes escapes if we have switched to equals.
+        int position = 0;
+        StringBuffer sb = new StringBuffer();
+        boolean replaceWithEquals = true;
+        while (position < value.length())
+        {
+            char checkWildcard = value.charAt(position);
+
+            switch (checkWildcard)
+            {
+            case BACKSLASH:
+                if (position + 1 >= value.length())
+                {
+                    // ignore backslashes at end
+                    break;
+                }
+                sb.append(checkWildcard);
+                position++;
+                // code below copies escaped character into sb
+                checkWildcard = value.charAt(position);
+                break;
+            case '%':
+            case '_':
+                replaceWithEquals = false;
+                break;
+            case '*':
+                replaceWithEquals = false;
+                checkWildcard = '%';
+                break;
+            case '?':
+                replaceWithEquals = false;
+                checkWildcard = '_';
+                break;
+            default:
+                break;
+            }
+
+            sb.append(checkWildcard);
+            position++;
+        }
+        value = sb.toString();
+
+        if (ignoreCase)
+        {
+            if (db.useIlike() && !replaceWithEquals)
+            {
+                if (SqlEnum.LIKE.equals(whereClausePart.getOperator()))
+                {
+                    whereClausePart.setOperator(SqlEnum.ILIKE);
+                }
+                else if (SqlEnum.NOT_LIKE.equals(whereClausePart.getOperator()))
+                {
+                    whereClausePart.setOperator(SqlEnum.NOT_ILIKE);
+                }
+            }
+            else
+            {
+                // no native case insensitive like is offered by the DB,
+                // or the LIKE was replaced with equals.
+                // need to ignore case manually.
+                whereClausePart.setLValue(
+                        db.ignoreCase(whereClausePart.getLValue()));
+            }
+        }
+
+        PreparedStatementPart result = new PreparedStatementPart();
+        result.getSql().append(whereClausePart.getLValue());
+
+        if (replaceWithEquals)
+        {
+            if (whereClausePart.getOperator().equals(SqlEnum.NOT_LIKE)
+                    || whereClausePart.getOperator().equals(SqlEnum.NOT_ILIKE))
+            {
+                result.getSql().append(SqlEnum.NOT_EQUAL);
+            }
+            else
+            {
+                result.getSql().append(SqlEnum.EQUAL);
+            }
+
+            // remove escape backslashes from String
+            position = 0;
+            sb = new StringBuffer();
+            while (position < value.length())
+            {
+                char checkWildcard = value.charAt(position);
+
+                if (checkWildcard == BACKSLASH
+                        && position + 1 < value.length())
+                {
+                    position++;
+                    // code below copies escaped character into sb
+                    checkWildcard = value.charAt(position);
+                }
+                sb.append(checkWildcard);
+                position++;
+            }
+            value = sb.toString();
+        }
+        else
+        {
+            result.getSql().append(whereClausePart.getOperator());
+        }
+
+        String rValueSql = "?";
+        // handle ignoreCase if necessary
+        if (ignoreCase && (!(db.useIlike()) || replaceWithEquals))
+        {
+            rValueSql = db.ignoreCase(rValueSql);
+        }
+        // handle escape clause if necessary
+        if (!replaceWithEquals && db.useEscapeClauseForLike())
+        {
+            rValueSql = rValueSql + SqlEnum.ESCAPE + "'\\'";
+        }
+
+        result.getPreparedStatementReplacements().add(value);
+        result.getSql().append(rValueSql);
+        return result;
+    }
+
+    /**
+     * Takes a columnName and criteria and
+     * builds a SQL 'IN' expression taking into account the ignoreCase
+     * flag.
+     *
+     * @param whereClausePart the part of the where clause to build.
+     *        Can be modified in this method.
+     * @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 The database for which the SQL should be created, not null.
+     */
+    static PreparedStatementPart buildPsIn(
+            WhereClauseExpression whereClausePart,
+            boolean ignoreCase,
+            DB db)
+    {
+        PreparedStatementPart result = new PreparedStatementPart();
+
+        boolean ignoreCaseApplied = false;
+        List<String> inClause = new ArrayList<String>();
+        if (whereClausePart.getRValue() instanceof Iterable)
+        {
+            for (Object listValue : (Iterable<?>) whereClausePart.getRValue())
+            {
+                result.getPreparedStatementReplacements().add(listValue);
+                if (ignoreCase && listValue instanceof String)
+                {
+                    inClause.add(db.ignoreCase("?"));
+                    ignoreCaseApplied = true;
+                }
+                else
+                {
+                    inClause.add("?");
+                }
+            }
+        }
+        else if (whereClausePart.getRValue().getClass().isArray())
+        {
+            for (Object arrayValue : (Object[]) whereClausePart.getRValue())
+            {
+                result.getPreparedStatementReplacements().add(arrayValue);
+                if (ignoreCase && arrayValue instanceof String)
+                {
+                    inClause.add(db.ignoreCase("?"));
+                    ignoreCaseApplied = true;
+                }
+                else
+                {
+                    inClause.add("?");
+                }
+            }
+        }
+        else
+        {
+            throw new IllegalArgumentException(
+                    "Unknown rValue type "
+                    + whereClausePart.getRValue().getClass().getName()
+                    + ". rValue must be an instance of "
+                    + " Iterable or Array");
+        }
+
+        if (ignoreCaseApplied)
+        {
+            result.getSql().append(db.ignoreCase(whereClausePart.getLValue()));
+        }
+        else
+        {
+            result.getSql().append(whereClausePart.getLValue());
+        }
+
+        result.getSql().append(whereClausePart.getOperator())
+                .append('(')
+                .append(StringUtils.join(inClause.iterator(), ","))
+                .append(')');
+        return result;
+    }
 }

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/WhereClauseExpression.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/WhereClauseExpression.java?rev=1100619&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/WhereClauseExpression.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/WhereClauseExpression.java Sat May  7 20:37:19 2011
@@ -0,0 +1,192 @@
+package org.apache.torque.sql;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.torque.util.SqlEnum;
+
+/**
+ * The raw values for a part of the where clause of a SQL statement,
+ * typically of the form lValue operator rValue, e.g. author.author_id = 1.
+ * The lValue always contains a column name
+ * or a function applied to a column name.
+ *
+ * @version $Id: $
+ */
+public class WhereClauseExpression
+{
+    /**
+     * The value on the left hand side of the operator,
+     * representing the name of a column, not null.
+     */
+    private String lValue = "";
+
+    /**
+     * The operator.
+     */
+    private SqlEnum operator;
+
+    /**
+     * The value on the right hand side of the operator.
+     */
+    private Object rValue = "";
+
+    /**
+     * Constructor.
+     *
+     * @param lValue The value on the left hand side of the operator of the
+     *        expression. The value represents the name of a database column.
+     * @param operator the operator.
+     * @param rValue
+     */
+    public WhereClauseExpression(
+            String lValue,
+            SqlEnum operator,
+            Object rValue)
+    {
+        setLValue(lValue);
+        setOperator(operator);
+        setRValue(rValue);
+    }
+
+    /**
+     * Returns the value on the left hand side of the operator of the
+     * expression. The value represents the name of a database column.
+     *
+     * @return the lValue, not null.
+     */
+    public String getLValue()
+    {
+        return lValue;
+    }
+
+    /**
+     * Sets the value on the left hand side of the operator of the
+     * expression. The value represents the name of a database column.
+     *
+     * @param lValue the value to set, or null for the empty String.
+     */
+    public void setLValue(String lValue)
+    {
+        if (lValue == null)
+        {
+            this.lValue = "";
+        }
+        else
+        {
+            this.lValue = lValue;
+        }
+    }
+
+    /**
+     * Returns the value on the operator of the expression.
+     *
+     * @return the operator, or null for no operator.
+     */
+    public SqlEnum getOperator()
+    {
+        return operator;
+    }
+
+    /**
+     * Sets the value on the operator of the expression.
+     *
+     * @param operator the value to set, or null fo no operator.
+     */
+    public void setOperator(SqlEnum operator)
+    {
+        this.operator = operator;
+    }
+
+    /**
+     * Returns the value on the right hand side of the operator of the
+     * expression.
+     *
+     * @return the rValue, or null.
+     */
+    public Object getRValue()
+    {
+        return rValue;
+    }
+
+    /**
+     * Sets the value on the right hand side of the operator of the
+     * expression.
+     *
+     * @param rValue the value to set, or null for the empty String.
+     */
+    public void setRValue(Object rValue)
+    {
+        this.rValue = rValue;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + lValue.hashCode();
+        result = prime * result + operator.hashCode();
+        result = prime * result + rValue.hashCode();
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        WhereClauseExpression other = (WhereClauseExpression) obj;
+        if (!lValue.equals(other.lValue))
+        {
+            return false;
+        }
+        if (!operator.equals(other.operator))
+        {
+            return false;
+        }
+        if (!rValue.equals(other.rValue))
+        {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder result = new StringBuilder(lValue);
+        if (operator != null)
+        {
+            result.append(operator);
+        }
+        result.append(rValue);
+        return result.toString();
+    }
+}

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/package.html
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/package.html?rev=1100619&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/package.html (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/package.html Sat May  7 20:37:19 2011
@@ -0,0 +1,28 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<html>
+  <head>
+  </head>
+  <body>
+    This package contains classes which are mainly used in the SQL generation
+    process.
+    These classes are NOT considered part of the public API of Torque
+    and may change without notice.
+  </body>
+</html>

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java Sat May  7 20:37:19 2011
@@ -48,6 +48,8 @@ import org.apache.torque.om.ObjectKey;
 import org.apache.torque.om.SimpleKey;
 import org.apache.torque.om.StringKey;
 import org.apache.torque.om.mapper.RecordMapper;
+import org.apache.torque.sql.Query;
+import org.apache.torque.sql.SqlBuilder;
 
 /**
  * This is the base class for all Peer classes in the system.  Peer
@@ -87,7 +89,7 @@ public abstract class BasePeer
     public static final String TABLE_NAME = "TABLE_NAME";
 
     /** the log */
-    protected static final Log log = LogFactory.getLog(BasePeer.class);
+    private static final Log log = LogFactory.getLog(BasePeer.class);
 
     private static void throwTorqueException(Exception e)
         throws TorqueException
@@ -333,7 +335,7 @@ public abstract class BasePeer
 
         query.setType(Query.Type.DELETE);
         query.getFromClause().clear();
-        String fullTableName = SQLBuilder.getFullTableName(
+        String fullTableName = SqlBuilder.getFullTableName(
                 tableName,
                 criteria.getDbName());
         query.getFromClause().add(
@@ -511,7 +513,7 @@ public abstract class BasePeer
             replacementObjects.add(value);
         }
 
-        String fullTableName = SQLBuilder.getFullTableName(
+        String fullTableName = SqlBuilder.getFullTableName(
                 tableMap.getName(), 
                 dbMap.getName());
         StringBuilder query = new StringBuilder("INSERT INTO ")
@@ -656,7 +658,7 @@ public abstract class BasePeer
     static String createQueryDisplayString(Criteria criteria)
         throws TorqueException
     {
-        Query query = SQLBuilder.buildQuery(criteria);
+        Query query = SqlBuilder.buildQuery(criteria);
         return query.getDisplayString();
     }
 
@@ -673,7 +675,7 @@ public abstract class BasePeer
     static Query createQuery(Criteria criteria)
         throws TorqueException
     {
-        return SQLBuilder.buildQuery(criteria);
+        return SqlBuilder.buildQuery(criteria);
     }
 
     /**
@@ -1284,7 +1286,7 @@ public abstract class BasePeer
         query.setType(Query.Type.UPDATE);
 
         query.getFromClause().clear();
-        String fullTableName = SQLBuilder.getFullTableName(
+        String fullTableName = SqlBuilder.getFullTableName(
                 updateValues.getTable().getName(),
                 criteria.getDbName());
         query.getFromClause().add(
@@ -1489,10 +1491,10 @@ public abstract class BasePeer
     {
         correctBooleans(criteria, defaultTableMap);
 
-        Query query = SQLBuilder.buildQuery(criteria);
+        Query query = SqlBuilder.buildQuery(criteria);
         if (query.getFromClause().isEmpty() && defaultTableMap != null)
         {
-            String tableName = SQLBuilder.getFullTableName(
+            String tableName = SqlBuilder.getFullTableName(
                     defaultTableMap.getName(),
                     criteria.getDbName());
             query.getFromClause().add(

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/adapter/DBOracleTest.java Sat May  7 20:37:19 2011
@@ -21,7 +21,7 @@ package org.apache.torque.adapter;
 
 import junit.framework.TestCase;
 
-import org.apache.torque.util.Query;
+import org.apache.torque.sql.Query;
 import org.apache.torque.util.UniqueList;
 
 public class DBOracleTest extends TestCase

Copied: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java (from r1056939, db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java?p2=db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java&p1=db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java&r1=1056939&r2=1100619&rev=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/QueryTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java Sat May  7 20:37:19 2011
@@ -1,4 +1,4 @@
-package org.apache.torque.util;
+package org.apache.torque.sql;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -19,6 +19,9 @@ package org.apache.torque.util;
  * under the License.
  */
 
+import org.apache.torque.sql.Query;
+import org.apache.torque.util.UniqueList;
+
 import junit.framework.TestCase;
 
 /**

Copied: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java (from r1070058, db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java)
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java?p2=db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java&p1=db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java&r1=1070058&r2=1100619&rev=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/SqlExpressionTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java Sat May  7 20:37:19 2011
@@ -1,4 +1,4 @@
-package org.apache.torque.util;
+package org.apache.torque.sql;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -20,13 +20,14 @@ package org.apache.torque.util;
  */
 
 import java.lang.reflect.Array;
-import java.util.ArrayList;
 import java.util.List;
 
 import junit.framework.TestCase;
 
+import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.DB;
 import org.apache.torque.adapter.DBFactory;
+import org.apache.torque.util.SqlEnum;
 
 /**
  * Tests for SqlExpression
@@ -35,19 +36,18 @@ import org.apache.torque.adapter.DBFacto
  * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
  * @version $Id$
  */
-public class SqlExpressionTest extends TestCase
+public class SqlBuilderTest extends TestCase
 {
     private DB db = null;
 
-
-        /**
-         * Constructor for SqlExpressionTest.
-         * @param arg0
-         */
-        public SqlExpressionTest(String arg0)
-        {
-                super(arg0);
-        }
+    /**
+     * Constructor for SqlExpressionTest.
+     * @param arg0
+     */
+    public SqlBuilderTest(String arg0)
+    {
+        super(arg0);
+    }
 
     /**
      * set up environment
@@ -69,12 +69,15 @@ public class SqlExpressionTest extends T
      */
     public void testBuildPsIn()
         {
-        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());
+        WhereClauseExpression whereClauseExpression
+                = new WhereClauseExpression(
+                        "COL",
+                        SqlEnum.IN,
+                        new String[] {"42", "43", "44"});
+        PreparedStatementPart result
+            = SqlBuilder.buildPsIn(whereClauseExpression, false, db);
+        assertEquals("COL IN (?,?,?)", result.getSql().toString());
+        List<Object> replacements = result.getPreparedStatementReplacements();
         assertEquals(3, replacements.size());
         assertEquals("42", replacements.get(0));
         assertEquals("43", replacements.get(1));
@@ -86,16 +89,20 @@ public class SqlExpressionTest extends T
      */
     public void testBuildPsInIgnoreCase()
         {
-        String[] values = new String[] { "42", "43", "44" };
-        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());
+        WhereClauseExpression whereClauseExpression
+            = new WhereClauseExpression(
+                    "COL",
+                    SqlEnum.IN,
+                    new String[] {"42", "43", "44"});
+        PreparedStatementPart result
+            = SqlBuilder.buildPsIn(whereClauseExpression, true, db);
+
+        assertEquals("COL IN (?,?,?)", result.getSql().toString());
+        List<Object> replacements = result.getPreparedStatementReplacements();
         assertEquals(3, replacements.size());
-        assertEquals(values[0], replacements.get(0));
-        assertEquals(values[1], replacements.get(1));
-        assertEquals(values[2], replacements.get(2));
+        assertEquals("42", replacements.get(0));
+        assertEquals("43", replacements.get(1));
+        assertEquals("44", replacements.get(2));
     }
 
     public void testLargeBuildInStringObjectSqlEnumbooleanDB()
@@ -106,48 +113,144 @@ public class SqlExpressionTest extends T
         {
             Array.set(values, i, String.valueOf(i));
         }
+        WhereClauseExpression whereClauseExpression
+            = new WhereClauseExpression(
+                    "COL",
+                    SqlEnum.IN,
+                    values);
         long start = System.currentTimeMillis();
-        StringBuilder result = new StringBuilder();
-        List<Object> replacements = new ArrayList<Object>();
-        SqlExpression.buildPsIn("COL", values, SqlEnum.IN, "?",
-                true, db, result, replacements);
+        PreparedStatementPart result
+                = SqlBuilder.buildPsIn(whereClauseExpression, true, db);
         long end =  System.currentTimeMillis();
+        List<Object> replacements = result.getPreparedStatementReplacements();
+        assertEquals(size, replacements.size());
         System.out.println("large buildIn took " + (end - start) + " milliseconds");
     }
 
     /**
-     * Test whether LIKE clauses are built correctly.
+     * Test whether LIKE clauses without Escapes are built correctly.
      */
-    public void testBuildPsLike()
+    public void testBuildPsLikeWithoutEscape() throws TorqueException
     {
-        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());
+        WhereClauseExpression whereClauseExpression
+            = new WhereClauseExpression(
+                    "COL",
+                    SqlEnum.LIKE,
+                    "fre%");
+        PreparedStatementPart result
+            = SqlBuilder.buildPsLike(whereClauseExpression, false, db);
+        assertEquals("COL LIKE ?", result.getSql().toString());
+        List<Object> replacements = result.getPreparedStatementReplacements();
         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());
+    /**
+     * Test whether LIKE clauses with Escapes are built correctly.
+     */
+    public void testBuildPsLikeWithEscape() throws TorqueException
+    {
+        WhereClauseExpression whereClauseExpression
+            = new WhereClauseExpression(
+                    "COL",
+                    SqlEnum.LIKE,
+                    "50\\%");
+        PreparedStatementPart result
+                = SqlBuilder.buildPsLike(whereClauseExpression, false, db);
+        assertEquals("COL=?", result.getSql().toString());
+        List<Object> replacements = result.getPreparedStatementReplacements();
         assertEquals(1, replacements.size());
         assertEquals("50%", replacements.get(0));
     }
+    
+    public void testExtractTableName() throws TorqueException
+    {
+        // standard cases with / without schema
+        String columnName = "table.column";
+        String tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        columnName = "schema.table.column";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("schema.table", tableName);
+
+        // functions
+        columnName = "function(table.column)";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        columnName = "function(1,table.column,2)";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        // comparisons
+        columnName = "table.column < 10";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        columnName = "table.column<10";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        columnName = "10 > table.column";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        columnName = "10>table.column";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        columnName = "10>table.column";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        // in clause
+        columnName = "table.column in (1,2,3)";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals("table", tableName);
+
+        // wildcard
+        columnName = "*";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals(null, tableName);
+
+        // function with wildcard
+        columnName = "count(*)";
+        tableName = SqlBuilder.getTableName(columnName, null);
+        assertEquals(null, tableName);
+
+        // empty String and null
+        columnName = "";
+        try
+        {
+            tableName = SqlBuilder.getTableName(columnName, null);
+            fail("getTableName() should fail for empty column name");
+        }
+        catch (TorqueException e)
+        {
+        }
+
+        columnName = null;
+        try
+        {
+            tableName = SqlBuilder.getTableName(columnName, null);
+            fail("getTableName() should fail for null as column name");
+        }
+        catch (TorqueException e)
+        {
+        }
+
+        // failure: no dot or wildcard
+        columnName = "column";
+        try
+        {
+            tableName = SqlBuilder.getTableName(columnName, null);
+            fail("getTableName() should fail for column name "
+                    + "without a dot or wildcard");
+        }
+        catch (TorqueException e)
+        {
+        }
+
+    }
 }

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java?rev=1100619&r1=1100618&r2=1100619&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java Sat May  7 20:37:19 2011
@@ -25,16 +25,14 @@ import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.configuration.BaseConfiguration;
-import org.apache.commons.configuration.Configuration;
 import org.apache.commons.lang.SerializationUtils;
 import org.apache.torque.BaseTestCase;
 import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
-import org.apache.torque.adapter.DBFactory;
 import org.apache.torque.map.ColumnMap;
 import org.apache.torque.map.DatabaseMap;
 import org.apache.torque.map.TableMap;
+import org.apache.torque.sql.Query;
 import org.apache.torque.util.Criteria.Criterion;
 import org.apache.torque.util.Criteria.Join;
 



---------------------------------------------------------------------
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