db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1206426 [1/7] - in /db/torque/torque4/trunk: torque-runtime/src/main/java/org/apache/torque/ torque-runtime/src/main/java/org/apache/torque/avalon/ torque-runtime/src/main/java/org/apache/torque/dsfactory/ torque-runtime/src/main/java/org/...
Date Sat, 26 Nov 2011 10:59:52 GMT
Author: tfischer
Date: Sat Nov 26 10:59:15 2011
New Revision: 1206426

URL: http://svn.apache.org/viewvc?rev=1206426&view=rev
Log:
TORQUE-168:
Use Objects instead of Strings for Column constants in Peers and Criteria

Added:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Column.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/ColumnImpl.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/OrderBy.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/UniqueColumnList.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/ColumnImplTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/map/
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/map/ColumnMapTest.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/DatabaseMapInitTableTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/DatabaseMapInitTransformer.java
      - copied, changed from r1145732, db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/columnConstant.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/columnConstantInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/foreignKeysInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/getTableMap.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/initDatabaseMap.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/staticInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/tableConstant.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/tableConstantInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/transformer/om/mapInit/DatabaseMapInitTableTransformer.properties
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/transformer/om/mapInit/DatabaseMapInitTransformer.properties
      - copied unchanged from r1103197, db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.properties
    db/torque/torque4/trunk/torque-test/src/main/schema/databaseMapInit-schema.xml
Removed:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/MapBuilder.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/classNameConstant.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/dbMapField.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/doBuild.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/getDatabaseMap.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/isBuilt.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/columnConstantInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/mapBuilderGetter.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/mapBuilderInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/mapBuilderGetter.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/mapBuilderInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.properties
Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/Torque.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.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/IDBroker.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/sql/JoinBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/ColumnValues.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/CountHelper.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/Criteria.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SummaryHelper.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/AbstractFunction.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/Aggregate.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/FunctionFactory.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/functions/SQLFunction.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueRuntimeExceptionTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/avalon/AvalonTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/QueryTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/CriteriaTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/resources/Torque.properties
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TemplateOptionName.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/DatabaseAttributeName.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/LoadExternalSchemaTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTableTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/control.xml
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/conf/options.properties
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/mapBuilder.xml
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/peer.xml
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/baseDbObject.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getByNameMethods.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/dbObject.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/imports.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapBuilder/mapBuilder.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapInit/baseMapInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/mapInit/mapInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/columnConstant.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/imports.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/staticInit.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/imports.vm
    db/torque/torque4/trunk/torque-test/src/main/schema/bookstore-schema.xml
    db/torque/torque4/trunk/torque-test/src/main/schema/foreign-key-schema.xml
    db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/BaseRuntimeTestCase.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/ForeignKeySchemaData.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/InitShutdownTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/avalon/AvalonMapBuildersTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DeleteTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/FillerTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/map/DatabaseMapTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/om/OMByNameMethodsTest.java

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Column.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Column.java?rev=1206426&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Column.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Column.java Sat Nov 26 10:59:15 2011
@@ -0,0 +1,70 @@
+package org.apache.torque;
+
+/*
+ * 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.
+ */
+
+/**
+ * The metadata of a column (or pseudocolumn) in a database.
+ *
+ * @version $Id: $
+ */
+public interface Column
+{
+    /**
+     * Returns the name of the column (not prefixed by the table name).
+     *
+     * @return the name of the column, may be null.
+     *         (e.g. for the pseudocoulumn count(*)).
+     */
+    String getColumnName();
+
+    /**
+     * Returns the name of the associated table
+     * (not prefixed by the schema name).
+     *
+     * @return the name of the table, not null.
+     */
+    String getTableName();
+
+    /**
+     * Returns the name of any fixed schema prefix for the column's table
+     * (if any).
+     *
+     * @return the schema name, or null if the schema is not known.
+     */
+    String getSchemaName();
+
+    /**
+     * Returns the table name prefixed with the schema name if it exists.
+     * I.e. if a schema name exists, the result will be schemaName.tableName,
+     * and otherwise it will just be tableName.
+     *
+     * @return the fully qualified table name of the column.
+     */
+    String getFullTableName();
+
+    /**
+     * Returns the SQL expression for the column name qualified by the
+     * table name but not by the schema name.
+     * This can also be a pseudocolumn (e.g. count(*)).
+     *
+     * @return the SQL expression for the column, not null.
+     */
+    String getSqlExpression();
+}

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/ColumnImpl.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/ColumnImpl.java?rev=1206426&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/ColumnImpl.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/ColumnImpl.java Sat Nov 26 10:59:15 2011
@@ -0,0 +1,414 @@
+package org.apache.torque;
+
+/*
+ * 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.io.Serializable;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+
+/**
+ * An implementation of the column interface.
+ * This class is immutable, i.e cannot be cchanged once constructed.
+ *
+ * @version $Id: $
+ */
+public class ColumnImpl implements Column, Serializable
+{
+    /** Serial version uid. **/
+    private static final long serialVersionUID = 1L;
+
+    /** Delimiters for SQL functions. */
+    private static final String[] FUNCTION_DELIMITERS
+            = {" ", ",", "(", ")", "<", ">"};
+
+    /** Constant for the dot. */
+    private static final String DOT = ".";
+
+    /** The column name, may be null but not empty. */
+    private String columnName;
+
+    /** The table name, may be null but not empty. */
+    private String tableName;
+
+    /** The schema name, may be null bit not empty. */
+    private String schemaName;
+
+    /** The SQL expression for the column, not null. */
+    private String sqlExpression;
+
+    /**
+     * Constructor.
+     *
+     * @param tableName the table name, not null or blank.
+     * @param columnName the column name, not null or blank.
+     *
+     * @throws NullPointerException if columnName or tableName are null.
+     * @throws IllegalArgumentException if columnName or tableName are blank.
+     */
+    public ColumnImpl(String tableName, String columnName)
+    {
+        if (columnName == null)
+        {
+            throw new NullPointerException("columnName must not be null");
+        }
+        if (tableName == null)
+        {
+            throw new NullPointerException("tableName must not be null");
+        }
+        setColumnName(columnName);
+        setTableName(tableName);
+        this.sqlExpression = tableName + DOT + columnName;
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param schemaName the schema name, may be null but not blank.
+     * @param tableName the table name, may be null but not blank.
+     *        If it contains a dot, then only the portion after the dot
+     *        will be taken as table name.
+     *        If it contains a dot and schemaName is null, then the schema
+     *        name will be set as the portion before the dot.
+     * @param columnName the column name, not null or blank.
+     *
+     * @throws NullPointerException if columnName or tableName are null.
+     * @throws IllegalArgumentException if columnName or tableName are blank.
+     */
+    public ColumnImpl(String schemaName, String tableName, String columnName)
+    {
+        if (columnName == null)
+        {
+            throw new NullPointerException("columnName must not be null");
+        }
+        setColumnName(columnName);
+        setTableName(tableName);
+        setSchemaName(schemaName);
+        if (this.tableName == null)
+        {
+            this.sqlExpression = this.columnName;
+        }
+        else
+        {
+            this.sqlExpression = this.tableName + DOT + this.columnName;
+        }
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param schemaName the schema name, may be null but not blank.
+     * @param tableName the table name, may be null but not blank.
+     *        If it contains a dot, then only the portion after the dot
+     *        will be taken as stable name.
+     *        If it contains a dot and schemaName is null, then the schema
+     *        name will be set as the portion before the dot.
+     * @param columnName the column name, may be null but not blank.
+     * @param sqlExpression the SQL expression for the column,
+     *        not null or blank.
+     *
+     * @throws NullPointerException if tableName or sqlExpression are null.
+     * @throws IllegalArgumentException if tableName or sqlExpression are blank.
+     */
+    public ColumnImpl(
+            String schemaName,
+            String tableName,
+            String columnName,
+            String sqlExpression)
+    {
+        setColumnName(columnName);
+        setTableName(tableName);
+        setSchemaName(schemaName);
+        setSqlExpression(sqlExpression);
+    }
+
+
+    /**
+     * Constructor which tries to guess schema, table and column names from
+     * an SQL expression. If a schema name can be identified in the
+     * sql expression, it is removed from the sql expression in the column.
+     *
+     * @param sqlExpression the SQL expression, not null, not blank.
+     *
+     * @throws NullPointerException if sqlExpression is null.
+     * @throws IllegalArgumentException if table or column name cannot be
+     *         guessed from sqlExpression.
+     */
+    public ColumnImpl(String sqlExpression)
+    {
+        setSqlExpression(sqlExpression);
+
+        // Find Table.Column
+        int dotIndex = sqlExpression.lastIndexOf(DOT);
+        if (dotIndex == -1)
+        {
+            if (StringUtils.contains(sqlExpression, "*"))
+            {
+                setColumnName("*");
+                return;
+            }
+            if (StringUtils.indexOfAny(sqlExpression, FUNCTION_DELIMITERS)
+                    != -1)
+            {
+                throw new IllegalArgumentException("sqlExpression "
+                    + sqlExpression
+                    + " is unparseable, it does not contain a dot (.) "
+                    + " but function delimiters.");
+            }
+            setColumnName(sqlExpression);
+            return;
+        }
+        String pre = sqlExpression.substring(0, dotIndex);
+        String post = sqlExpression.substring(
+                dotIndex + 1,
+                sqlExpression.length());
+        if (StringUtils.isBlank(pre))
+        {
+            throw new IllegalArgumentException("sqlExpression "
+                    + sqlExpression
+                    + " is blank before the dot (.)");
+        }
+        int startIndex = StringUtils.lastIndexOfAny(pre, FUNCTION_DELIMITERS);
+        int endIndex = StringUtils.indexOfAny(post, FUNCTION_DELIMITERS);
+        if (endIndex < 0)
+        {
+            endIndex = sqlExpression.length();
+        }
+        else
+        {
+            // relative to sqlExpression not to post
+            endIndex += dotIndex + 1;
+        }
+
+        if (startIndex + 1 == dotIndex)
+        {
+            throw new IllegalArgumentException("sqlExpression "
+                    + sqlExpression
+                    + " is blank between the last function delimiter ("
+                    + StringUtils.join(FUNCTION_DELIMITERS)
+                    + ") and the dot");
+        }
+        setColumnName(sqlExpression.substring(dotIndex + 1, endIndex));
+        // if startIndex == -1 the formula is correct
+        String fullTableName
+                = sqlExpression.substring(startIndex + 1, dotIndex);
+        setTableName(fullTableName);
+        if (fullTableName.contains(DOT))
+        {
+            int fullTableNameDotIndex = fullTableName.lastIndexOf(DOT);
+            String extractedSchemaName
+                = fullTableName.substring(0, fullTableNameDotIndex);
+            setSchemaName(extractedSchemaName);
+            StringBuilder sqlExpressionBuilder = new StringBuilder();
+            if (startIndex != -1)
+            {
+                sqlExpressionBuilder.append(
+                        sqlExpression.substring(0, startIndex + 1));
+            }
+            sqlExpressionBuilder.append(getTableName())
+                    .append(DOT)
+                    .append(post);
+            setSqlExpression(sqlExpressionBuilder.toString());
+        }
+    }
+
+    /**
+     * Returns the column name.
+     *
+     * @return the column name, may be null.
+     */
+    public String getColumnName()
+    {
+        return columnName;
+    }
+
+    /**
+     * Sets the column name, using null if * is passed.
+     *
+     * @param columnName the column name, not blank.
+     */
+    private void setColumnName(String columnName)
+    {
+        if (columnName != null && StringUtils.isBlank(columnName))
+        {
+            throw new IllegalArgumentException("columnName must not be blank");
+        }
+        if ("*".equals(columnName))
+        {
+            this.columnName = null;
+        }
+        else
+        {
+            this.columnName = columnName;
+        }
+    }
+
+    /**
+     * Returns the table name.
+     *
+     * @return the table name, may be null.
+     */
+    public String getTableName()
+    {
+        return tableName;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getFullTableName()
+    {
+        if (schemaName != null)
+        {
+            return schemaName + '.' + tableName;
+        }
+        return tableName;
+    }
+
+    /**
+     * Sets the table name.
+     *
+     * @param tableName the table name, not blank or null.
+     *
+     * @throws IllegalArgumentException if tableName is blank or null.
+     */
+    private void setTableName(String tableName)
+    {
+        if (tableName != null && StringUtils.isBlank(tableName))
+        {
+            throw new IllegalArgumentException("tableName must not be blank");
+        }
+        if (StringUtils.contains(tableName, DOT))
+        {
+            int dotIndex = tableName.lastIndexOf(DOT);
+            this.schemaName = tableName.substring(0, dotIndex);
+            this.tableName = tableName.substring(dotIndex + 1);
+            return;
+        }
+        this.tableName = tableName;
+    }
+
+    /**
+     * Returns the name of any fixed schema prefix for the column's table
+     * (if any).
+     *
+     * @return the schema name, or null if the schema is not known.
+     */
+    public String getSchemaName()
+    {
+        return schemaName;
+    }
+
+    /**
+     * Sets the schema name, if a non-null value is passed.
+     *
+     * @param schemaName the schema name, or null.
+     *
+     * @throws IllegalArgumentException if schemaName is blank.
+     */
+    private void setSchemaName(String schemaName)
+    {
+        if (schemaName == null)
+        {
+            return;
+        }
+        if (StringUtils.isBlank(schemaName))
+        {
+            throw new IllegalArgumentException("schemaName must not be blank");
+        }
+        this.schemaName = schemaName;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getSqlExpression()
+    {
+        return sqlExpression;
+    }
+
+    /**
+     * Sets the sql expression.
+     *
+     * @param sqlExpression the sql expression, not null.
+     *
+     * @throws NullPointerException if sqlExpression is null.
+     * @throws IllegalArgumentException if sqlExpression is blank.
+     */
+    private void setSqlExpression(String sqlExpression)
+    {
+        if (sqlExpression == null)
+        {
+            throw new IllegalArgumentException(
+                    "sqlExpression must not be null");
+        }
+        if (StringUtils.isBlank(sqlExpression))
+        {
+            throw new IllegalArgumentException(
+                    "sqlExpression must not be blank");
+        }
+        this.sqlExpression = sqlExpression;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return new HashCodeBuilder()
+            .append(sqlExpression)
+            .append(columnName)
+            .append(tableName)
+            .append(schemaName)
+            .toHashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        ColumnImpl other = (ColumnImpl) obj;
+        return new EqualsBuilder()
+            .append(sqlExpression, other.sqlExpression)
+            .append(columnName, other.columnName)
+            .append(tableName, other.tableName)
+            .append(schemaName, other.schemaName)
+            .isEquals();
+    }
+
+    @Override
+    public String toString()
+    {
+        return "ColumnImpl [columnName=" + columnName
+                + ", tableName=" + tableName
+                + ", schemaName=" + schemaName
+                + ", sqlExpression=" + sqlExpression + "]";
+    }
+}

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java Sat Nov 26 10:59:15 2011
@@ -172,7 +172,7 @@ public class Database
     public void setIdTable(TableMap idTable)
     {
         this.idTable = idTable;
-        getDatabaseMap().addTable(idTable);
+        getDatabaseMap().setIdTable(idTable);
     }
 
     /**
@@ -226,7 +226,7 @@ public class Database
         ColumnMap quantity = new ColumnMap("QUANTITY", tMap);
         quantity.setType(Integer.valueOf(0));
         tMap.addColumn(quantity);
-        idBroker = new IDBroker(idTable);
+        idBroker = new IDBroker(this);
         addIdGenerator(IDMethod.ID_BROKER, idBroker);
         return true;
     }
@@ -241,7 +241,7 @@ public class Database
      */
     public IdGenerator getIdGenerator(String type)
     {
-        return (IdGenerator) idGenerators.get(type);
+        return idGenerators.get(type);
     }
 
     /**

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java Sat Nov 26 10:59:15 2011
@@ -26,7 +26,7 @@ import org.apache.commons.configuration.
 import org.apache.torque.adapter.DB;
 import org.apache.torque.manager.AbstractBaseManager;
 import org.apache.torque.map.DatabaseMap;
-import org.apache.torque.map.MapBuilder;
+import org.apache.torque.oid.IDBroker;
 
 /**
  * A static facade wrapper around the Torque implementation (which is in
@@ -243,50 +243,29 @@ public final class Torque
      * of the connection pool to associate with the map.
      *
      * @param name The name of the database corresponding to the
-     *        <code>DatabaseMap</code> to retrieve.
-     * @return The named <code>DatabaseMap</code>.
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
-     */
-    public static DatabaseMap getDatabaseMap(String name)
-        throws TorqueException
-    {
-        return getInstance().getDatabaseMap(name);
-    }
-
-    /**
-     * Register a MapBuilder
+     *        <code>DatabaseMap</code> to retrieve, not null.
      *
-     * @param className the MapBuilder
-     */
-    public static void registerMapBuilder(String className)
-    {
-        getInstance().registerMapBuilder(className);
-    }
-
-    /**
-     * Register a MapBuilder
-     *
-     * @param builder the instance of the MapBuilder
+     * @return The named <code>DatabaseMap</code>, not null.
      *
+     * @throws NullPointerException if name is null.
      */
-    public static void registerMapBuilder(MapBuilder builder)
+    public static DatabaseMap getDatabaseMap(String name)
     {
-        getInstance().registerMapBuilder(builder);
+        return getInstance().getDatabaseMap(name);
     }
 
     /**
-     * Get a MapBuilder
+     * Registers an id broker. If Torque is already initialized,
+     * the id broker is started. If Torque is not initialized,
+     * the id broker will be started on initialization.
      *
-     * @param className of the MapBuilder
-     * @return A MapBuilder, not null
-     * @throws TorqueException if the Map Builder cannot be instantiated
+     * @param idBroker the id broker to register, not null.
      *
+     * @throws NullPointerException if idBroker is null.
      */
-    public static MapBuilder getMapBuilder(String className)
-        throws TorqueException
+    public static void registerIDBroker(IDBroker idBroker)
     {
-        return getInstance().getMapBuilder(className);
+        getInstance().registerIDBroker(idBroker);
     }
 
     /**
@@ -334,10 +313,11 @@ public final class Torque
     {
         return getInstance().getConnection(name, username, password);
     }
+
     /**
-     * Returns database adapter for a specific connection pool.
+     * Returns the database adapter for a specific database name.
      *
-     * @param name A pool name.
+     * @param name the database name.
      * @return The corresponding database adapter.
      * @throws TorqueException Any exceptions caught during processing will be
      *         rethrown wrapped into a TorqueException.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java Sat Nov 26 10:59:15 2011
@@ -23,8 +23,10 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationException;
@@ -37,7 +39,6 @@ import org.apache.torque.adapter.DBFacto
 import org.apache.torque.dsfactory.DataSourceFactory;
 import org.apache.torque.manager.AbstractBaseManager;
 import org.apache.torque.map.DatabaseMap;
-import org.apache.torque.map.MapBuilder;
 import org.apache.torque.oid.IDBroker;
 import org.apache.torque.oid.IDGeneratorFactory;
 
@@ -80,6 +81,9 @@ public class TorqueInstance
     /** A repository of Manager instances. */
     private Map<String, AbstractBaseManager> managers;
 
+    /** A repository of idBroker instances. */
+    private Set<IDBroker> idBrokers = new HashSet<IDBroker>();
+
     /** Torque-specific configuration. */
     private Configuration conf;
 
@@ -95,15 +99,6 @@ public class TorqueInstance
     private boolean defaultDsfIsReference = false;
 
     /**
-     * Store mapbuilder classnames for peers that have been referenced prior
-     * to Torque being initialized.  This can happen if torque om/peer objects
-     * are serialized then unserialized prior to Torque being reinitialized.
-     * This condition exists in a normal catalina restart.
-     */
-    private Map<String, MapBuilder> mapBuilderCache
-            = Collections.synchronizedMap(new HashMap<String, MapBuilder>());
-
-    /**
      * Creates a new instance with default configuration.
      *
      * @see #resetConfiguration()
@@ -181,36 +176,9 @@ public class TorqueInstance
 
         // setup manager mappings
         initManagerMappings(conf);
+        startIdBrokers();
 
         isInit = true;
-
-        // re-build any MapBuilders that may have gone lost during serialization
-        synchronized (mapBuilderCache)
-        {
-            for (Map.Entry<String, MapBuilder> entry
-                    : mapBuilderCache.entrySet())
-            {
-                if (null == entry.getValue())
-                {
-                    try
-                    {
-                        // create and build the MapBuilder
-                        MapBuilder builder = (MapBuilder) Class.forName(entry.getKey()).newInstance();
-
-                        if (!builder.isBuilt())
-                        {
-                            builder.doBuild();
-                        }
-
-                        entry.setValue(builder);
-                    }
-                    catch (Exception e)
-                    {
-                        throw new TorqueException(e);
-                    }
-                }
-            }
-        }
     }
 
 
@@ -578,6 +546,17 @@ public class TorqueInstance
     }
 
     /**
+     * Starts all registered IdBrokers.
+     */
+    private void startIdBrokers()
+    {
+        for (IDBroker idBroker : idBrokers)
+        {
+            idBroker.start();
+        }
+    }
+
+    /**
      * Determine whether Torque has already been initialized.
      *
      * @return true if Torque is already initialized
@@ -687,17 +666,11 @@ public class TorqueInstance
     public synchronized void shutdown()
         throws TorqueException
     {
-        // stop the idbrokers
-        synchronized (databases)
+        for (IDBroker idBroker : idBrokers)
         {
-            for (Database database : databases.values())
-            {
-                IDBroker idBroker = database.getIdBroker();
-                if (idBroker != null)
-                {
-                    idBroker.stop();
-                }
-            }
+            idBroker.stop();
+            // do not remove idbrokers because they will not be
+            // re-registered on a new startup.
         }
 
         // shut down the cache managers
@@ -791,17 +764,17 @@ public class TorqueInstance
      * of the connection pool to associate with the map.
      *
      * @param name The name of the database corresponding to the
-     *        <code>DatabaseMap</code> to retrieve.
-     * @return The named <code>DatabaseMap</code>.
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     *        <code>DatabaseMap</code> to retrieve, not null.
+     *
+     * @return The named <code>DatabaseMap</code>, not null.
+     *
+     * @throws NullPointerException if name is null.
      */
     public DatabaseMap getDatabaseMap(String name)
-            throws TorqueException
     {
         if (name == null)
         {
-            throw new TorqueException ("DatabaseMap name was null!");
+            throw new NullPointerException ("name was null!");
         }
 
         Database database = getOrCreateDatabase(name);
@@ -809,82 +782,20 @@ public class TorqueInstance
     }
 
     /**
-     * Get the registered MapBuilders
-     *
-     * @return the MapBuilder cache
-     *
-     */
-    public Map<String, MapBuilder> getMapBuilders()
-    {
-        return mapBuilderCache;
-    }
-
-    /**
-     * Register a MapBuilder
-     *
-     * @param className the MapBuilder
-     */
-    public void registerMapBuilder(String className)
-    {
-        mapBuilderCache.put(className, null);
-    }
-
-    /**
-     * Register a MapBuilder
-     *
-     * @param builder the instance of the MapBuilder
-     *
-     */
-    public void registerMapBuilder(MapBuilder builder)
-    {
-        mapBuilderCache.put(builder.getClass().getName(), builder);
-    }
-
-    /**
-     * Get a MapBuilder
+     * Registers an id broker. If Torque is already initialized,
+     * the id broker is started. If Torque is not initialized,
+     * the id broker will be started on initialization.
      *
-     * @param className of the MapBuilder
-     * @return A MapBuilder, not null
-     * @throws TorqueException if the Map Builder cannot be instantiated
+     * @param idBroker the id broker to register, not null.
      *
+     * @throws NullPointerException if idBroker is null.
      */
-    public MapBuilder getMapBuilder(String className)
-        throws TorqueException
+    public void registerIDBroker(IDBroker idBroker)
     {
-        try
+        idBrokers.add(idBroker);
+        if (isInit())
         {
-            MapBuilder mb = mapBuilderCache.get(className);
-
-            if (mb == null)
-            {
-                mb = (MapBuilder) Class.forName(className).newInstance();
-                // Cache the MapBuilder before it is built.
-                mapBuilderCache.put(className, mb);
-            }
-
-            if (mb.isBuilt())
-            {
-                return mb;
-            }
-
-            try
-            {
-                mb.doBuild();
-            }
-            catch (Exception e)
-            {
-                // remove the MapBuilder from the cache if it can't be built correctly
-                mapBuilderCache.remove(className);
-                throw e;
-            }
-
-            return mb;
-        }
-        catch (Exception e)
-        {
-            log.error("getMapBuilder failed trying to instantiate: "
-                    + className, e);
-            throw new TorqueException(e);
+            idBroker.start();
         }
     }
 

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/Torque.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/Torque.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/Torque.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/Torque.java Sat Nov 26 10:59:15 2011
@@ -28,7 +28,6 @@ import org.apache.torque.TorqueException
 import org.apache.torque.adapter.DB;
 import org.apache.torque.manager.AbstractBaseManager;
 import org.apache.torque.map.DatabaseMap;
-import org.apache.torque.map.MapBuilder;
 
 /**
  * Avalon role interface for Torque.
@@ -107,32 +106,6 @@ public interface Torque
     DatabaseMap getDatabaseMap(String name) throws TorqueException;
 
     /**
-     * Register a MapBuilder
-     *
-     * @param className the MapBuilder
-     */
-    void registerMapBuilder(String className);
-
-    /**
-     * Register a MapBuilder
-     *
-     * @param builder the instance of the MapBuilder
-     *
-     */
-    void registerMapBuilder(MapBuilder builder);
-
-    /**
-     * Get a MapBuilder
-     *
-     * @param className of the MapBuilder
-     * @return A MapBuilder, not null
-     * @throws TorqueException if the Map Builder cannot be instantiated
-     *
-     */
-    MapBuilder getMapBuilder(String className)
-        throws TorqueException;
-
-    /**
      * This method returns a Connection from the default pool.
      *
      * @return The requested connection.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java Sat Nov 26 10:59:15 2011
@@ -34,8 +34,8 @@ import org.apache.avalon.framework.logge
 import org.apache.avalon.framework.logger.Logger;
 import org.apache.avalon.framework.thread.ThreadSafe;
 import org.apache.commons.lang.StringUtils;
+import org.apache.torque.Database;
 import org.apache.torque.TorqueInstance;
-import org.apache.torque.map.MapBuilder;
 
 /**
  * Avalon component for Torque.
@@ -164,14 +164,15 @@ public class TorqueComponent
 
         TorqueInstance instance = org.apache.torque.Torque.getInstance();
 
-        Map<String, MapBuilder> mapBuilders = instance.getMapBuilders();
-
-        if (mapBuilders != null)
+        // Copy the database maps
+        Map<String, Database> databases = null;
+        if (instance.isInit())
         {
-            // Copy the registered MapBuilders and take care that they will be built again
-            for (String className : mapBuilders.keySet())
+            databases = instance.getDatabases();
+            for (Database otherDatabase : databases.values())
             {
-                registerMapBuilder(className);
+                getDatabaseMap(otherDatabase.getName()).copyFrom(
+                        otherDatabase.getDatabaseMap());
             }
         }
 
@@ -179,6 +180,19 @@ public class TorqueComponent
         org.apache.torque.Torque.setInstance(this);
 
         init(configFile);
+
+        // start the id brokers
+        if (instance.isInit())
+        {
+            for (Database otherDatabase : databases.values())
+            {
+                Database database = getDatabase(otherDatabase.getName());
+                if (otherDatabase.getIdBroker() != null)
+                {
+                    database.startIdBroker();
+                }
+            }
+        }
     }
 
     /**

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java Sat Nov 26 10:59:15 2011
@@ -20,6 +20,7 @@ package org.apache.torque.dsfactory;
  */
 
 import javax.sql.DataSource;
+
 import org.apache.commons.configuration.Configuration;
 import org.apache.torque.TorqueException;
 
@@ -63,7 +64,7 @@ public interface DataSourceFactory
      * Sets the current schema for the database connection
      *
      * @param schema The current schema name
-     * @deprecated use DatabaseInfo.setSchema() instead. Will be removed
+     * @deprecated use Database.setSchema() instead. Will be removed
      *             in a future version of Torque.
      */
     void setSchema(String schema);
@@ -74,7 +75,7 @@ public interface DataSourceFactory
      * @return The current schema name. Null means, no schema has been set.
      * @throws TorqueException Any exceptions caught during processing will be
      *         rethrown wrapped into a TorqueException.
-     * @deprecated use DatabaseInfo.getSchema() instead. Will be removed
+     * @deprecated use Database.getSchema() instead. Will be removed
      *             in a future version of Torque.
      */
     String getSchema();

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/ColumnMap.java Sat Nov 26 10:59:15 2011
@@ -24,6 +24,8 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.apache.torque.Column;
+
 /**
  * ColumnMap is used to model a column of a table in a database.
  * <p>
@@ -35,7 +37,7 @@ import java.util.Map;
  * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
  * @version $Id$
  */
-public class ColumnMap implements java.io.Serializable
+public class ColumnMap implements java.io.Serializable, Column
 {
     /** The serialVersionUID for this class. */
     private static final long serialVersionUID = -5971184507395399165L;
@@ -158,6 +160,8 @@ public class ColumnMap implements java.i
      * Get the table name + column name.
      *
      * @return A String with the full column name.
+     *
+     * @deprecated use getSqlExpression() instead.
      */
     public String getFullyQualifiedName()
     {
@@ -175,6 +179,31 @@ public class ColumnMap implements java.i
     }
 
     /**
+     * Get the name of the table this column is in.
+     *
+     * @return A String with the table name.
+     */
+    public String getFullTableName()
+    {
+        String schema = table.getSchemaName();
+        if (schema != null)
+        {
+            return schema + '.' + table.getName();
+        }
+        return table.getName();
+    }
+
+    /**
+     * Get the name of the schema of the table this column is in.
+     *
+     * @return A String with the schema name, or null if no schema is given.
+     */
+    public String getSchemaName()
+    {
+        return table.getSchemaName();
+    }
+
+    /**
      * Set the type of this column.
      *
      * @param type An Object specifying the type.
@@ -638,4 +667,24 @@ public class ColumnMap implements java.i
     {
         return optionsMap.get(key);
     }
+
+    /**
+     * Returns the SQL expression for the fully qualified column name.
+     * This is tableName.columnName if the database for this column has no
+     * schema and defaultSchema is null,
+     * or schemaName.tableName.columnName with a schema
+     * (the database schema overrides the default schema if both are given).
+     *
+     * @return the SQL expression for the column, not null.
+     */
+    public String getSqlExpression()
+    {
+        return table.getName() + '.' + columnName;
+    }
+
+    @Override
+    public String toString()
+    {
+        return getSqlExpression();
+    }
 }

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java Sat Nov 26 10:59:15 2011
@@ -57,7 +57,7 @@ public class DatabaseMap implements java
      * specific map
      */
     public static final String INIT_CLASS_NAME_FORMAT =
-                                "org.apache.torque.linkage.{0}MapInit";
+                                "org.apache.torque.linkage.{0}DatabaseMapInit";
 
     /**
      * Error Messages for initialisation.
@@ -86,6 +86,9 @@ public class DatabaseMap implements java
     private Map<String, TableMap> tables
         = Collections.synchronizedMap(new LinkedHashMap<String, TableMap>());
 
+    /** The id Table. */
+    private TableMap idTable = null;
+
     /** Flag indicating that all tables have been loaded via initialize() */
     private boolean isInitialized = false;
 
@@ -128,7 +131,12 @@ public class DatabaseMap implements java
         {
             name = name.substring(0, name.indexOf('.'));
         }
-        return tables.containsKey(name);
+        boolean found = tables.containsKey(name);
+        if (!found && idTable != null)
+        {
+            return idTable.getName().equals(name);
+        }
+        return found;
     }
 
     /**
@@ -154,7 +162,16 @@ public class DatabaseMap implements java
      */
     public TableMap getTable(String name)
     {
-        return (TableMap) tables.get(name);
+        TableMap result = tables.get(name);
+        if (result != null)
+        {
+            return result;
+        }
+        if (idTable != null && idTable.getName().equals(name))
+        {
+            return idTable;
+        }
+        return null;
     }
 
     /**
@@ -169,7 +186,12 @@ public class DatabaseMap implements java
      */
     public TableMap[] getTables()
     {
-        TableMap[] dbTables = new TableMap[tables.size()];
+        int size = tables.size();
+        if (idTable != null)
+        {
+            size++;
+        }
+        TableMap[] dbTables = new TableMap[size];
         synchronized (tables)
         {
             Iterator<TableMap> it = tables.values().iterator();
@@ -179,6 +201,10 @@ public class DatabaseMap implements java
                 dbTables[i++] = it.next();
             }
         }
+        if (idTable != null)
+        {
+            dbTables[size - 1] = idTable;
+        }
         return dbTables;
     }
 
@@ -187,24 +213,14 @@ public class DatabaseMap implements java
      * TableMap that you need to populate.
      *
      * @param tableName The name of the table.
-     */
-    public void addTable(String tableName)
-    {
-        TableMap tmap = new TableMap(tableName, this);
-        tables.put(tableName, tmap);
-    }
-
-    /**
-     * Add a new table to the database by name.  It creates an empty
-     * TableMap that you need to populate.
      *
-     * @param tableName The name of the table.
-     * @param numberOfColumns The number of columns in the table.
+     * @return the new table map.
      */
-    public void addTable(String tableName, int numberOfColumns)
+    public TableMap addTable(String tableName)
     {
-        TableMap tmap = new TableMap(tableName, numberOfColumns, this);
+        TableMap tmap = new TableMap(tableName, this);
         tables.put(tableName, tmap);
+        return tmap;
     }
 
     /**
@@ -212,9 +228,9 @@ public class DatabaseMap implements java
      *
      * @param map The TableMap representation.
      */
-    public void addTable(TableMap map)
+    public void setIdTable(TableMap idTableMap)
     {
-        tables.put(map.getName(), map);
+        this.idTable = idTableMap;
     }
 
     /**
@@ -247,7 +263,7 @@ public class DatabaseMap implements java
      */
     public String getOption(String key)
     {
-        return (String) optionsMap.get(key);
+        return optionsMap.get(key);
     }
 
     /**
@@ -381,4 +397,39 @@ public class DatabaseMap implements java
         }
         return result.toString();
     }
+
+    /**
+     * Copy all settings except the database from another database map.
+     *
+     * @param databaseMap the database map to copy from, not null.
+     */
+    public void copyFrom(DatabaseMap databaseMap)
+    {
+        this.isInitialized = databaseMap.isInitialized;
+        this.optionsMap.clear();
+        this.optionsMap.putAll(databaseMap.optionsMap);
+        this.tables.clear();
+        this.tables.putAll(databaseMap.tables);
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder result = new StringBuilder();
+        result.append("DatabaseMap[database.name=")
+             .append(database.getName())
+             .append(", tables=(");
+        boolean first = true;
+        for (TableMap table : tables.values())
+        {
+            if (!first)
+            {
+                result.append(',');
+            }
+            result.append(table.getName());
+            first = false;
+        }
+        result.append(")]");
+        return result.toString();
+    }
 }

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java Sat Nov 26 10:59:15 2011
@@ -21,6 +21,7 @@ package org.apache.torque.map;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
@@ -66,6 +67,12 @@ public class TableMap implements IDMetho
     /** The name of the table. */
     private String tableName;
 
+    /**
+     * The name of the schema to which this table belongs,
+     * or null for the default schema.
+     */
+    private String schemaName;
+
     /** The JavaName of the table as defined in XML */
     private String javaName;
 
@@ -93,67 +100,78 @@ public class TableMap implements IDMetho
     /** The associated cache manager class. */
     private Class<?> managerClass;
 
+    /** Overrides the information stored in the pkInfoMap for all id methods. */
+    private Object pkInfoOverride;
+
     /**
-     * Object to store information that is needed if the
-     * for generating primary keys.
+     * Stores information that is needed for generating primary keys.
+     * The information is keyed by the idMethodType because it might be
+     * different for different id methods.
      */
-    private Object pkInfo = null;
+    private Map<String, Object> pkInfoMap = new HashMap<String, Object>();
 
     /** Associated options. */
     private Map<String, String> optionsMap
             = Collections.synchronizedMap(new LinkedHashMap<String, String>());
 
     /**
-     * Required by proxy. Not used.
-     */
-    public TableMap()
-    {
-    }
-
-    /**
-     * Constructor.
-     *
-     * @param tableName The name of the table.
-     * @param numberOfColumns The number of columns in the table.
-     * @param containingDB A DatabaseMap that this table belongs to.
-     */
-    public TableMap(String tableName,
-                    int numberOfColumns,
-                    DatabaseMap containingDB)
-    {
-        this.tableName = tableName;
-        dbMap = containingDB;
-    }
-
-    /**
      * Constructor.
      *
-     * @param tableName The name of the table.
+     * @param tableName The name of the table, may be prefixed with a
+     *        schema name, not null.
      * @param containingDB A DatabaseMap that this table belongs to.
      */
     public TableMap(String tableName, DatabaseMap containingDB)
     {
-        this.tableName = tableName;
+        setTableName(tableName);
         dbMap = containingDB;
     }
 
     /**
      * Constructor.
      *
-     * @param tableName The name of the table.
+     * @param tableName The name of the table, may be prefixed with a
+     *        schema name, not null.
      * @param prefix The prefix for the table name (ie: SCARAB for
-     * SCARAB_PROJECT).
+     *        SCARAB_PROJECT).
      * @param containingDB A DatabaseMap that this table belongs to.
      */
     public TableMap(String tableName,
                     String prefix,
                     DatabaseMap containingDB)
     {
-        this.tableName = tableName;
+        setTableName(tableName);
         this.prefix = prefix;
         dbMap = containingDB;
     }
 
+    private void setTableName(String tableName)
+    {
+        if (tableName == null)
+        {
+            throw new NullPointerException("tableName must not be null");
+        }
+        int dotIndex = tableName.indexOf(".");
+        if (dotIndex != -1)
+        {
+            this.schemaName = tableName.substring(0, dotIndex);
+            this.tableName = tableName.substring(dotIndex + 1);
+        }
+        else
+        {
+            this.tableName = tableName;
+        }
+    }
+
+    /**
+     * Sets the database map this table belongs to.
+     * @param databaseMap
+     */
+    void setDatabaseMap(DatabaseMap databaseMap)
+    {
+        dbMap = databaseMap;
+    }
+
     /**
      * Does this table contain the specified column?
      *
@@ -204,7 +222,7 @@ public class TableMap implements IDMetho
             Iterator<ColumnMap> it = columns.values().iterator();
             while (it.hasNext())
             {
-                Object theType = ((ColumnMap) it.next()).getType();
+                Object theType = it.next().getType();
                 if (!(theType instanceof String || theType instanceof Number
                         || theType instanceof java.util.Date))
                 {
@@ -216,9 +234,9 @@ public class TableMap implements IDMetho
     }
 
     /**
-     * Get the name of the Table.
+     * Get the name of the Table, not prefixed by a possible schema name
      *
-     * @return A String with the name of the table.
+     * @return A String with the name of the table, not null.
      */
     public String getName()
     {
@@ -226,6 +244,16 @@ public class TableMap implements IDMetho
     }
 
     /**
+     * Get the schema to which the table belongs to.
+     *
+     * @return the schema name, or null if the default schema should be used.
+     */
+    public String getSchemaName()
+    {
+        return schemaName;
+    }
+
+    /**
      * Get the Java name of the table as defined in XML.
      *
      * @return A String with the Java name of the table.
@@ -281,9 +309,13 @@ public class TableMap implements IDMetho
      *
      * @return An Object.
      */
-    public Object getPrimaryKeyMethodInfo()
+    public Object getPrimaryKeyMethodInfo(String idMethod)
     {
-        return pkInfo;
+        if (pkInfoOverride != null)
+        {
+            return pkInfoOverride;
+        }
+        return pkInfoMap.get(idMethod);
     }
 
     /**
@@ -382,13 +414,26 @@ public class TableMap implements IDMetho
     }
 
     /**
-     * Sets the pk information needed to generate a key
+     * Sets the pk information needed to generate a key.
+     * This overrides all information set by
+     * <code>setPrimaryKeyMethodInfo(String, Object)</code>.
      *
      * @param pkInfo information needed to generate a key
      */
     public void setPrimaryKeyMethodInfo(Object pkInfo)
     {
-        this.pkInfo = pkInfo;
+        pkInfoOverride = pkInfo;
+    }
+
+    /**
+     * Sets the pk information needed to generate a key.
+     *
+     * @param idMethod the id method for which this information is stored.
+     * @param pkInfo information needed to generate a key.
+     */
+    public void setPrimaryKeyMethodInfo(String idMethod, Object pkInfo)
+    {
+        pkInfoMap.put(idMethod, pkInfo);
     }
 
     //---Utility methods for doing intelligent lookup of table names
@@ -642,4 +687,9 @@ public class TableMap implements IDMetho
         return result.iterator().next();
     }
 
+    @Override
+    public String toString()
+    {
+        return "TableMap[" + tableName + "]";
+    }
 }

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=1206426&r1=1206425&r2=1206426&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 Nov 26 10:59:15 2011
@@ -43,22 +43,25 @@ import org.apache.torque.util.BasePeer;
  */
 public class AutoIncrementIdGenerator implements IdGenerator
 {
-    /** the adapter that knows the correct sql syntax */
+    /** The adapter that knows the correct sql syntax */
     private DB dbAdapter;
 
-    /** The internal name of the Database that this Generator is connected to */
-    private String name = null;
+    /**
+     * The internal name of the Database that this Generator is connected to.
+     */
+    private String databaseName = null;
 
     /**
      * Creates an IdGenerator which will work with the specified database.
      *
      * @param dbAdapter the adapter that knows the correct sql syntax.
-     * @param name The name of the datasource to find the correct schema
+     * @param databaseName The name of the databaseName to find the correct
+     *        schema.
      */
-    public AutoIncrementIdGenerator(final DB dbAdapter, final String name)
+    public AutoIncrementIdGenerator(final DB dbAdapter, final String databaseName)
     {
         this.dbAdapter = dbAdapter;
-        this.name = name;
+        this.databaseName = databaseName;
     }
 
     /**
@@ -178,7 +181,9 @@ public class AutoIncrementIdGenerator im
                 RecordMapper<T> mapper)
             throws TorqueException
     {
-        String tableName = SqlBuilder.getFullTableName(String.valueOf(keyInfo), name);
+        String tableName = SqlBuilder.getFullTableName(
+                String.valueOf(keyInfo),
+                databaseName);
         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/IDBroker.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java Sat Nov 26 10:59:15 2011
@@ -34,7 +34,6 @@ import org.apache.commons.logging.LogFac
 import org.apache.torque.Database;
 import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
-import org.apache.torque.map.TableMap;
 import org.apache.torque.util.Transaction;
 
 //!!
@@ -106,6 +105,9 @@ public class IDBroker implements Runnabl
     /** Fully qualified Quantity column name */
     public static final String QUANTITY = ID_TABLE + "." + COL_QUANTITY;
 
+    /** The backup quantity which is used if an error occurs. */
+    private static final double PREFETCH_BACKUP_QUANTITY = 10d;
+
     /** the name of the database in which this IdBroker is running. */
     private String databaseName;
 
@@ -186,36 +188,21 @@ public class IDBroker implements Runnabl
     private Log log = LogFactory.getLog(IDBroker.class);
 
     /**
-     * constructs an IdBroker for the given Database.
+     * Constructs an IdBroker for the given Database.
+     *
      * @param database the database where this IdBroker is running in.
      */
     public IDBroker(Database database)
     {
-        this(database.getName());
-    }
-
-    /**
-     * Creates an IDBroker for the ID table.
-     *
-     * @param tMap A TableMap.
-     * @deprecated Use IDBroker(DatabaseInfo) instead. Will be removed
-     *             in a future version of Torque.
-     */
-    public IDBroker(TableMap tMap)
-    {
-        this(tMap.getDatabaseMap().getDatabase().getName());
+        this.databaseName = database.getName();
+        Torque.registerIDBroker(this);
     }
 
     /**
-     * Constructor.
-     * Provided as long as both Constructors, IDBroker(DatabaseInfo) and
-     * IDBroker(TableMap), are around.
-     * @param databaseName the name of the database for which this IdBroker
-     *        provides Ids.
+     * Starts the idBroker.
      */
-    private IDBroker(String databaseName)
+    public void start()
     {
-        this.databaseName = databaseName;
         configuration = Torque.getConfiguration();
 
         // Start the housekeeper thread only if prefetch has not been disabled
@@ -493,7 +480,7 @@ public class IDBroker implements Runnabl
     public boolean exists(String tableName)
         throws Exception
     {
-        String query = new StringBuffer(100)
+        String query = new StringBuilder()
             .append("select ")
             .append(TABLE_NAME)
             .append(" where ")
@@ -584,8 +571,7 @@ public class IDBroker implements Runnabl
      * Shuts down the IDBroker thread.
      *
      * Calling this method stops the thread that was started for this
-     * instance of the IDBroker. This method should be called during
-     * MapBroker Service shutdown.
+     * instance of the IDBroker.
      */
     public void stop()
     {
@@ -612,7 +598,7 @@ public class IDBroker implements Runnabl
         }
 
         // Get the last id request for this table.
-        java.util.Date lastTime = (java.util.Date) lastQueryTime.get(tableName);
+        java.util.Date lastTime = lastQueryTime.get(tableName);
         java.util.Date now = new java.util.Date();
 
         if (lastTime != null)
@@ -630,7 +616,7 @@ public class IDBroker implements Runnabl
                 // Increase quantity, so that hopefully this does not
                 // happen again.
                 float rate = getQuantity(tableName, null).floatValue()
-                    / (float) timeLapse;
+                    / timeLapse;
                 quantityStore.put(tableName, new BigDecimal(
                     Math.ceil(SLEEP_PERIOD * rate * SAFETY_MARGIN)));
             }
@@ -750,7 +736,7 @@ public class IDBroker implements Runnabl
         // Initialize quantity, if necessary.
         else if (quantityStore.containsKey(tableName))
         {
-            quantity = (BigDecimal) quantityStore.get(tableName);
+            quantity = quantityStore.get(tableName);
         }
         else
         {
@@ -773,7 +759,7 @@ public class IDBroker implements Runnabl
             }
             catch (Exception e)
             {
-                quantity = new BigDecimal((double) 10);
+                quantity = new BigDecimal(PREFETCH_BACKUP_QUANTITY);
             }
             finally
             {
@@ -863,8 +849,7 @@ public class IDBroker implements Runnabl
     {
 
 
-        StringBuffer stmt = new StringBuffer(id.length()
-                                             + tableName.length() + 50);
+        StringBuilder stmt = new StringBuilder();
         stmt.append("UPDATE " + ID_TABLE)
             .append(" SET ")
             .append(COL_NEXT_ID)
@@ -910,8 +895,7 @@ public class IDBroker implements Runnabl
                                 BigDecimal quantity)
         throws Exception
     {
-        StringBuffer stmt = new StringBuffer(quantity.toString().length()
-                                             + tableName.length() + 50);
+        StringBuilder stmt = new StringBuilder();
         stmt.append("UPDATE ")
             .append(ID_TABLE)
             .append(" SET ")

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=1206426&r1=1206425&r2=1206426&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 Nov 26 10:59:15 2011
@@ -46,18 +46,19 @@ public class SequenceIdGenerator impleme
     private DB dbAdapter;
 
     /** The internal name of the Database that this Generator is connected to */
-    private String name = null;
+    private String databaseName = null;
 
     /**
      * Creates an IdGenerator which will work with the specified database.
      *
      * @param dbAdapter the adapter that knows the correct sql syntax.
-     * @param name The name of the datasource to find the correct schema
+     * @param databaseName The name of the database to find the
+     *        correct schema.
      */
-    public SequenceIdGenerator(final DB dbAdapter, final String name)
+    public SequenceIdGenerator(final DB dbAdapter, final String databaseName)
     {
         this.dbAdapter = dbAdapter;
-        this.name = name;
+        this.databaseName = databaseName;
     }
 
     /**
@@ -173,7 +174,9 @@ public class SequenceIdGenerator impleme
             throws TorqueException
     {
         String sequenceName
-                = SqlBuilder.getFullTableName(String.valueOf(keyInfo), name);
+                = SqlBuilder.getFullTableName(
+                        String.valueOf(keyInfo),
+                        databaseName);
         String idSql = dbAdapter.getIDMethodSQL(sequenceName);
 
         List<T> result = BasePeer.doSelect(idSql, mapper, null, connection);

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=1206426&r1=1206425&r2=1206426&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 Sat Nov 26 10:59:15 2011
@@ -21,9 +21,9 @@ package org.apache.torque.sql;
 
 import java.util.List;
 
+import org.apache.torque.Column;
+import org.apache.torque.Database;
 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;
@@ -50,13 +50,13 @@ public final class JoinBuilder
     /**
      * Adds the Joins from the criteria to the query.
      *
+     * @param database the database for which the query should be built.
      * @param criteria the criteria from which the Joins are taken
      * @param query the query to which the Joins should be added
      * @throws TorqueException if the Joins can not be processed
      */
     public static void processJoins(
-            final DB db,
-            final DatabaseMap dbMap,
+            final Database database,
             final Criteria criteria,
             final Query query)
             throws TorqueException
@@ -73,49 +73,11 @@ public final class JoinBuilder
 
         for (int i = 0; i < criteriaJoins.size(); i++)
         {
-            Criteria.Join join = (Criteria.Join) criteriaJoins.get(i);
-            String leftColumn = join.getLeftColumn();
-            String rightColumn = join.getRightColumn();
+            Criteria.Join join = criteriaJoins.get(i);
+            Column leftColumn = join.getLeftColumn();
+            Column rightColumn = join.getRightColumn();
 
-            // check if the column names make sense
-            if (leftColumn.indexOf('.') == -1)
-            {
-                SqlBuilder.throwMalformedColumnNameException("join", leftColumn);
-            }
-            if (rightColumn.indexOf('.') == -1)
-            {
-                SqlBuilder.throwMalformedColumnNameException("join", rightColumn);
-            }
-
-            // get the table names
-            // (and the alias names for them if necessary))
-            // Also check whether a case insensitive comparison is needed
-            int dot = leftColumn.lastIndexOf('.');
-            String leftTableName = leftColumn.substring(0, dot);
-
-            leftTableName =
-                    SqlBuilder.getTableNameForFromClause(leftTableName, criteria);
-            leftTableName = SqlBuilder.getFullTableName(
-                    leftTableName,
-                    criteria.getDbName());
-
-            dot = rightColumn.lastIndexOf('.');
-            String rightTableName = rightColumn.substring(0, dot);
-            String dbTableName
-                    = criteria.getTableForAlias(rightTableName);
-
-            if (dbTableName == null)
-            {
-                dbTableName = rightTableName;
-            }
-
-            rightTableName = SqlBuilder.getTableNameForFromClause(
-                    rightTableName, criteria);
-            rightTableName = SqlBuilder.getFullTableName(
-                    rightTableName,
-                    criteria.getDbName());
-
-            // now check the join type and add the join to the
+            // Check the join type and add the join to the
             // appropriate places in the query
             SqlEnum joinType  = join.getJoinType();
 
@@ -123,29 +85,33 @@ public final class JoinBuilder
             {
                 // Do not treat join as explicit join, but add
                 // the join condition to the where clauses
-                if (!SqlBuilder.fromClauseContainsTableName(
-                            queryFromClause,
-                            leftTableName))
-                {
-                    Query.FromElement fromElement
-                            = new Query.FromElement(
-                                    leftTableName, null, null);
-                    queryFromClause.add(fromElement);
-                }
-                if (!SqlBuilder.fromClauseContainsTableName(
-                            queryFromClause,
-                            rightTableName))
-                {
-                    Query.FromElement fromElement
-                            = new Query.FromElement(
-                                    rightTableName, null, null);
-                    queryFromClause.add(fromElement);
-                }
+                SqlBuilder.addTableToFromClause(
+                        leftColumn,
+                        database,
+                        criteria,
+                        query);
+                SqlBuilder.addTableToFromClause(
+                        rightColumn,
+                        database,
+                        criteria,
+                        query);
                 queryWhereClause.add(
-                        buildInnerJoin(leftColumn, rightColumn));
+                        buildJoinCondition(leftColumn, rightColumn));
             }
             else
             {
+                // get the table names
+                // (and the alias names for them if necessary))
+                String leftTableName = SqlBuilder.getTableNameForFromClause(
+                        leftColumn,
+                        database,
+                        criteria);
+
+                String rightTableName = SqlBuilder.getTableNameForFromClause(
+                        rightColumn,
+                        database,
+                        criteria);
+
                 // check whether the order of the join must be "reversed"
                 // This if the case if the fromClause already contains
                 // rightTableName
@@ -166,8 +132,9 @@ public final class JoinBuilder
 
                     Query.FromElement fromElement
                             = new Query.FromElement(
-                                    rightTableName, joinType,
-                                    buildInnerJoin(
+                                    rightTableName,
+                                    joinType,
+                                    buildJoinCondition(
                                             leftColumn, rightColumn));
                     queryFromClause.add(fromElement);
                 }
@@ -191,8 +158,9 @@ public final class JoinBuilder
                     // because it is already present
                     Query.FromElement fromElement
                             = new Query.FromElement(
-                                    leftTableName, reverseJoinType(joinType),
-                                    buildInnerJoin(
+                                    leftTableName,
+                                    reverseJoinType(joinType),
+                                    buildJoinCondition(
                                             rightColumn, leftColumn));
                     queryFromClause.add(fromElement);
                 }
@@ -237,9 +205,12 @@ public final class JoinBuilder
 
      * @return A join expression, e.g. table_a.column_a=table_b.column_b.
      */
-    private static String buildInnerJoin(String column,
-                                         String relatedColumn)
+    private static String buildJoinCondition(
+            Column column,
+            Column relatedColumn)
     {
-        return column + '=' + relatedColumn;
+        return column.getSqlExpression()
+            + '='
+            + relatedColumn.getSqlExpression();
     }
 }

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/OrderBy.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/OrderBy.java?rev=1206426&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/OrderBy.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/OrderBy.java Sat Nov 26 10:59:15 2011
@@ -0,0 +1,157 @@
+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.io.Serializable;
+
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.torque.Column;
+import org.apache.torque.util.SqlEnum;
+
+/**
+ * An order by clause.
+ * @version $Id: $
+ *
+ */
+public class OrderBy implements Serializable
+{
+    /** The column to order by. */
+    private Column column;
+
+    /** The order to order by (ascending or descending). */
+    private SqlEnum order;
+
+    /** Whether case should be ignored for String columns. */
+    private boolean ignoreCase = true;
+
+    /**
+     * Constructor.
+     *
+     * @param column the column to order by, not null.
+     * @param order the order, either SqlEnum.DESC or SqlEnum.ASC, not null.
+     * @param ignoreCase whether case should be ignored for String columns
+     *
+     * @throws NullPointerException if null is passed.
+     * @throws IllegalArgumentException if an unknown order is passed.
+     */
+    public OrderBy(Column column, SqlEnum order, boolean ignoreCase)
+    {
+        if (column == null)
+        {
+            throw new NullPointerException("column is null");
+        }
+        if (order == null)
+        {
+            throw new NullPointerException("order is null");
+        }
+        if (SqlEnum.DESC != order && SqlEnum.ASC != order)
+        {
+            throw new IllegalArgumentException("unknown order: " + order);
+        }
+        this.column = column;
+        this.order = order;
+        this.ignoreCase = ignoreCase;
+    }
+
+    /**
+     * Returns the column to order by.
+     *
+     * @return the column to order by, not null.
+     */
+    public Column getColumn()
+    {
+        return column;
+    }
+
+    /**
+     * Returns the order to order by (ASC or DESC).
+     *
+     * @return the order, either SqlEnum.DESC or SqlEnum.ASC, not null.
+     */
+    public SqlEnum getOrder()
+    {
+        return order;
+    }
+
+    /**
+     * Returns whether case should be ignored for String columns.
+     *
+     * @return true if case should be ignored for String columns,
+     *         false otherwise.
+     */
+    public boolean isIgnoreCase()
+    {
+        return ignoreCase;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return new HashCodeBuilder()
+            .append(this.column.getSqlExpression())
+            .append(this.column.getSchemaName())
+            .append(this.column.getTableName())
+            .append(this.column.getColumnName())
+            .append(this.order)
+            .append(this.ignoreCase)
+            .toHashCode();
+    }
+
+    /**
+     * Checks whether two orderBy are equal.
+     * This is true if and only if the orders are equal and if the contained
+     * columns have the same schema name, table name, column name
+     * and sql expression.
+     *
+     * @param obj the object to compare to.
+     *
+     * @return true if this object is equal to obj, false otherwise.
+     */
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null)
+        {
+            return false;
+        }
+        if (getClass() != obj.getClass())
+        {
+            return false;
+        }
+        OrderBy other = (OrderBy) obj;
+        return new EqualsBuilder()
+            .append(this.column.getSqlExpression(),
+                    other.column.getSqlExpression())
+            .append(this.column.getSchemaName(),
+                    other.column.getSchemaName())
+            .append(this.column.getTableName(),
+                    other.column.getTableName())
+            .append(this.column.getColumnName(),
+                    other.column.getColumnName())
+            .append(this.order, other.order)
+            .append(this.ignoreCase, other.ignoreCase)
+            .isEquals();
+    }
+}

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java?rev=1206426&r1=1206425&r2=1206426&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java Sat Nov 26 10:59:15 2011
@@ -24,6 +24,8 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
 import org.apache.torque.TorqueException;
 import org.apache.torque.util.SqlEnum;
 import org.apache.torque.util.UniqueList;
@@ -635,5 +637,42 @@ public class Query
             }
             return result.toString();
         }
+
+
+        @Override
+        public int hashCode()
+        {
+            return new HashCodeBuilder()
+                    .append(joinCondition)
+                    .append(joinType)
+                    .append(tableName)
+                    .toHashCode();
+        }
+
+
+        @Override
+        public boolean equals(Object obj)
+        {
+            if (this == obj)
+            {
+                return true;
+            }
+            if (obj == null)
+            {
+                return false;
+            }
+            if (getClass() != obj.getClass())
+            {
+                return false;
+            }
+            FromElement other = (FromElement) obj;
+            return new EqualsBuilder()
+                .append(joinCondition, other.joinCondition)
+                .append(joinType, other.joinType)
+                .append(tableName, other.tableName)
+                .isEquals();
+        }
+
+
     } // end of inner class FromElement
 }



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