You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2014/10/26 09:19:22 UTC

svn commit: r1634277 - in /db/torque/torque4/trunk/torque-runtime/src: main/java/org/apache/torque/criteria/Criteria.java main/java/org/apache/torque/sql/JoinBuilder.java test/java/org/apache/torque/sql/SqlBuilderTest.java

Author: tfischer
Date: Sun Oct 26 08:19:22 2014
New Revision: 1634277

URL: http://svn.apache.org/r1634277
Log:
TORQUE-332 Criteria.addJoin(String, String, Criterion, JoinType) does not honor default schema

Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java?rev=1634277&r1=1634276&r2=1634277&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java Sun Oct 26 08:19:22 2014
@@ -654,6 +654,10 @@ public class Criteria implements Seriali
      *         FooPeer.PROJECT"_ID))
      *     Criteria.LEFT_JOIN);
      * </code>
+     * If a default schema name is set for the used database
+     * and leftTable or rightTable are a simple unqualified table names,
+     * the default schema name is prepended to the table name.
+     * For more complicated "table names", no schema resolution is done.
      *
      * @param leftTable the left table of the join, or null to determine
      *        the left table from the join condition.
@@ -696,7 +700,11 @@ public class Criteria implements Seriali
      *         FooPeer.PROJECT_ID,
      *         Criteria.NOT_EQUAL)
      *     Criteria.LEFT_JOIN);
-     * </code>
+     * </code>.
+     * If a default schema name is set for the used database
+     * and leftTable or rightTable are a simple unqualified table names,
+     * the default schema name is prepended to the table name.
+     * For more complicated "table names", no schema resolution is done.
      *
      * @param leftTable the left table of the join, might contain an alias name,
      *        or null to be determined from the join clause.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java?rev=1634277&r1=1634276&r2=1634277&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/JoinBuilder.java Sun Oct 26 08:19:22 2014
@@ -21,6 +21,8 @@ package org.apache.torque.sql;
 
 import java.util.List;
 
+import org.apache.torque.Database;
+import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
 import org.apache.torque.criteria.Criteria;
 import org.apache.torque.criteria.Criterion;
@@ -145,6 +147,8 @@ public final class JoinBuilder
                             criteria);
 
                 }
+                addSchema(leftExpression, criteria);
+                addSchema(rightExpression, criteria);
 
                 // check whether the order of the join must be "reversed"
                 // This if the case if the fromClause already contains
@@ -290,4 +294,35 @@ public final class JoinBuilder
                 = SqlBuilder.processCriterion(joinCondition, criteria);
         joinPart.append(joinConditionStatementPart);
     }
+
+    /**
+     * Adds the default schema to a table name if necessary.
+     *
+     * @param tableNamePart the table name to add the schema name to, not null.
+     * @param criteria the criteria from which the tableNamePart was created, not null.
+     */
+    private static void addSchema(final PreparedStatementPart tableNamePart, final Criteria criteria)
+            throws TorqueException
+    {
+        String tableName = tableNamePart.getSql().toString();
+        if (tableName.indexOf('.') != -1 // table name is already qualified
+            || tableName.indexOf(' ') != -1 // table name is no simple table name
+            || tableName.indexOf('(') != -1) // table name is no simple table name
+        {
+            return;
+        }
+        Object resolvedAlias = criteria.getAliases().get(tableName);
+        if (resolvedAlias != null)
+        {
+            return;
+        }
+        final String dbName = criteria.getDbName();
+        final Database database = Torque.getDatabase(dbName);
+        String resolvedSchemaName = database.getSchema();
+        if (resolvedSchemaName != null)
+        {
+            tableNamePart.getSql().insert(0, '.');
+            tableNamePart.getSql().insert(0, resolvedSchemaName);
+        }
+    }
 }

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java?rev=1634277&r1=1634276&r2=1634277&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java Sun Oct 26 08:19:22 2014
@@ -1119,6 +1119,33 @@ public class SqlBuilderTest extends Base
         assertEquals("y", query.getPreparedStatementReplacements().get(1));
     }
 
+    public void testInnerJoinWithJoinCriteriaAndDefaultSchema()
+            throws Exception
+    {
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("TestSchema");
+            Column otherTableJoinColumn = new ColumnImpl("table2.column2");
+            Criterion joinCriterion = new Criterion(stringColumnMap, otherTableJoinColumn);
+
+            Criteria criteria = new Criteria()
+                    .addSelectColumn(stringColumnMap)
+                    .addJoin(stringColumnMap.getTableName(), "table2", joinCriterion, Criteria.INNER_JOIN);
+
+            Query query = SqlBuilder.buildQuery(criteria);
+
+            assertEquals(
+                    "SELECT TABLE.COLUMN1 FROM TestSchema.TABLE INNER JOIN TestSchema.table2 ON TABLE.COLUMN1=table2.column2",
+                    query.toString());
+            assertEquals(0, query.getPreparedStatementReplacements().size());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
+    }
+
     public void testLeftJoin()
             throws Exception
     {



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