db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r329452 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils: ./ platform/ task/
Date Sat, 29 Oct 2005 17:32:16 GMT
Author: tomdz
Date: Sat Oct 29 10:32:00 2005
New Revision: 329452

URL: http://svn.apache.org/viewcvs?rev=329452&view=rev
Log:
Integrated the model reader into the platform
Adapted the Ant tasks to use the platform rather than the model reader directly

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DatabaseMetaDataWrapper.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/PlatformConfiguration.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java Sat Oct 29 10:32:00 2005
@@ -27,6 +27,7 @@
 import org.apache.commons.beanutils.DynaBean;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.platform.JdbcModelReader;
 import org.apache.ddlutils.platform.SqlBuilder;
 
 /**
@@ -53,13 +54,20 @@
     public PlatformInfo getPlatformInfo();
     
     /**
-     * Returns the sql builder for the this database.
+     * Returns the sql builder for the this platform.
      * 
      * @return The sql builder
      */
     public SqlBuilder getSqlBuilder();
 
     /**
+     * Returns the model reader (which reads a database model from a live database) for this
platform.
+     * 
+     * @return The model reader
+     */
+    public JdbcModelReader getModelReader();
+
+    /**
      * Returns the data source that this platform uses to access the database.
      * 
      * @return The data source
@@ -466,4 +474,46 @@
      * @param connection The database connection
      */
     public void delete(Database model, DynaBean dynaBean, Connection connection) throws DynaSqlException;
+
+    /**
+     * Reads the database model from the live database as specified by the data source set
for
+     * this platform.
+     * 
+     * @return The database model
+     * @throws DynaSqlException If an error occurred during reading the model
+     */
+    public Database readModelFromDatabase() throws DynaSqlException;
+
+    /**
+     * Reads the database model from the live database as specified by the data source set
for
+     * this platform.
+     * 
+     * @param catalog    The catalog to acess in the database; use <code>null</code>
for the default value
+     * @param schema     The schema to acess in the database; use <code>null</code>
for the default value
+     * @param tableTypes The table types to process; use <code>null</code> or
an empty list for the default ones
+     * @return The database model
+     * @throws DynaSqlException If an error occurred during reading the model
+     */
+    public Database readModelFromDatabase(String catalog, String schema, String[] tableTypes)
throws DynaSqlException;
+
+    /**
+     * Reads the database model from the live database to which the given connection is pointing.
+     * 
+     * @param connection The connection to the database
+     * @return The database model
+     * @throws DynaSqlException If an error occurred during reading the model
+     */
+    public Database readModelFromDatabase(Connection connection) throws DynaSqlException;
+
+    /**
+     * Reads the database model from the live database to which the given connection is pointing.
+     * 
+     * @param connection The connection to the database
+     * @param catalog    The catalog to acess in the database; use <code>null</code>
for the default value
+     * @param schema     The schema to acess in the database; use <code>null</code>
for the default value
+     * @param tableTypes The table types to process; use <code>null</code> or
an empty list for the default ones
+     * @return The database model
+     * @throws DynaSqlException If an error occurred during reading the model
+     */
+    public Database readModelFromDatabase(Connection connection, String catalog, String schema,
String[] tableTypes) throws DynaSqlException;
 }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DatabaseMetaDataWrapper.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DatabaseMetaDataWrapper.java?rev=329452&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DatabaseMetaDataWrapper.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DatabaseMetaDataWrapper.java Sat
Oct 29 10:32:00 2005
@@ -0,0 +1,192 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed 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.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Wrapper class for database meta data that stores additional info.
+ * 
+ * @author Thomas Dudziak
+ * @version $Revision: 329426 $
+ */
+public class DatabaseMetaDataWrapper
+{
+    /** The database meta data. */
+    private DatabaseMetaData _metaData;
+    /** The catalog to acess in the database. */
+    private String _catalog;
+    /** The schema(s) to acess in the database. */
+    private String _schemaPattern;
+    /** The table types to process. */
+    private String[] _tableTypes;
+
+    /**
+     * Returns the database meta data.
+     *
+     * @return The meta data
+     */
+    public DatabaseMetaData getMetaData()
+    {
+        return _metaData;
+    }
+
+    /**
+     * Sets the database meta data.
+     *
+     * @param metaData The meta data
+     */
+    public void setMetaData(DatabaseMetaData metaData)
+    {
+        _metaData = metaData;
+    }
+
+    /**
+     * Returns the catalog in the database to read.
+     *
+     * @return The catalog
+     */
+    public String getCatalog()
+    {
+        return _catalog;
+    }
+
+    /**
+     * Sets the catalog in the database to read.
+     * 
+     * @param catalog The catalog
+     */
+    public void setCatalog(String catalog)
+    {
+        _catalog = catalog;
+    }
+
+    /**
+     * Returns the schema in the database to read.
+     *
+     * @return The schema
+     */
+    public String getSchemaPattern()
+    {
+        return _schemaPattern;
+    }
+
+    /**
+     * Sets the schema in the database to read.
+     * 
+     * @param schema The schema
+     */
+    public void setSchemaPattern(String schema)
+    {
+        _schemaPattern = schema;
+    }
+
+    /**
+     * Returns the table types to recognize.
+     *
+     * @return The table types
+     */
+    public String[] getTableTypes()
+    {
+        return _tableTypes;
+    }
+
+    /**
+     * Sets the table types to recognize.
+     * 
+     * @param types The table types
+     */
+    public void setTableTypes(String[] types)
+    {
+        _tableTypes = types;
+    }
+
+    /**
+     * Convenience method to return the table meta data using the configured catalog,
+     * schema pattern and table types.
+     * 
+     * @param tableNamePattern The pattern identifying for which tables to return info
+     * @return The table meta data
+     * @throws SQLException If an error occurred retrieving the meta data
+     * @see DatabaseMetaData#getTables(java.lang.String, java.lang.String, java.lang.String,
java.lang.String[])
+     */
+    public ResultSet getTables(String tableNamePattern) throws SQLException
+    {
+        return getMetaData().getTables(getCatalog(), getSchemaPattern(), tableNamePattern,
getTableTypes());
+    }
+
+    /**
+     * Convenience method to return the column meta data using the configured catalog and
+     * schema pattern.
+     * 
+     * @param tableNamePattern  The pattern identifying for which tables to return info
+     * @param columnNamePattern The pattern identifying for which columns to return info
+     * @return The column meta data
+     * @throws SQLException If an error occurred retrieving the meta data
+     * @see DatabaseMetaData#getColumns(java.lang.String, java.lang.String, java.lang.String,
java.lang.String)
+     */
+    public ResultSet getColumns(String tableNamePattern, String columnNamePattern) throws
SQLException
+    {
+        return getMetaData().getColumns(getCatalog(), getSchemaPattern(), tableNamePattern,
columnNamePattern);
+    }
+
+    /**
+     * Convenience method to return the primary key meta data using the configured catalog
and
+     * schema pattern.
+     * 
+     * @param tableNamePattern The pattern identifying for which tables to return info
+     * @return The primary key meta data
+     * @throws SQLException If an error occurred retrieving the meta data
+     * @see DatabaseMetaData#getPrimaryKeys(java.lang.String, java.lang.String, java.lang.String)
+     */
+    public ResultSet getPrimaryKeys(String tableNamePattern) throws SQLException
+    {
+        return getMetaData().getPrimaryKeys(getCatalog(), getSchemaPattern(), tableNamePattern);
+    }
+
+    /**
+     * Convenience method to return the foreign key meta data using the configured catalog
and
+     * schema pattern.
+     * 
+     * @param tableNamePattern The pattern identifying for which tables to return info
+     * @return The foreign key meta data
+     * @throws SQLException If an error occurred retrieving the meta data
+     * @see DatabaseMetaData#getImportedKeys(java.lang.String, java.lang.String, java.lang.String)
+     */
+    public ResultSet getForeignKeys(String tableNamePattern) throws SQLException
+    {
+        return getMetaData().getImportedKeys(getCatalog(), getSchemaPattern(), tableNamePattern);
+    }
+
+    /**
+     * Convenience method to return the index meta data using the configured catalog and
+     * schema pattern.
+     * 
+     * @param tableNamePattern The pattern identifying for which tables to return info
+     * @param unique           Whether to return only indices for unique values
+     * @param approximate      Whether the result is allowed to reflect approximate or out
of data values
+     * @return The index meta data
+     * @throws SQLException If an error occurred retrieving the meta data
+     * @see DatabaseMetaData#getIndexInfo(java.lang.String, java.lang.String, java.lang.String,
boolean, boolean)
+     */
+    public ResultSet getIndices(String tableNamePattern, boolean unique, boolean approximate)
throws SQLException
+    {
+        return getMetaData().getImportedKeys(getCatalog(), getSchemaPattern(), tableNamePattern);
+    }
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java Sat Oct 29
10:32:00 2005
@@ -17,7 +17,6 @@
  */
 
 import java.sql.Connection;
-import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
@@ -39,11 +38,11 @@
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Index;
-import org.apache.ddlutils.model.UniqueIndex;
-import org.apache.ddlutils.model.NonUniqueIndex;
 import org.apache.ddlutils.model.IndexColumn;
+import org.apache.ddlutils.model.NonUniqueIndex;
 import org.apache.ddlutils.model.Reference;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.UniqueIndex;
 
 /**
  * An utility class to create a Database model from a live database.
@@ -55,85 +54,125 @@
 public class JdbcModelReader
 {
     /** The Log to which logging calls will be made. */
-    private final Log log = LogFactory.getLog(JdbcModelReader.class);
+    private final Log _log = LogFactory.getLog(JdbcModelReader.class);
 
     /** Contains default column sizes (minimum sizes that a JDBC-compliant db must support).
*/
-    private HashMap defaultSizes = new HashMap();
-    /** The database connection. */
-    private Connection connection = null;
-    /** The database catalog to read. */
-    private String catalog = "%";
-    /** The database schema to read. */
-    private String schema = "%";
-    /** The table types to recognize. */
-    private String[] tableTypes = { "TABLE" };
+    private HashMap _defaultSizes = new HashMap();
+    /** The default database catalog to read. */
+    private String _defaultCatalog = "%";
+    /** The sefault database schema(s) to read. */
+    private String _defaultSchemaPattern = "%";
+    /** The table types to recognize per default. */
+    private String[] _defaultTableTypes = { "TABLE" };
     /** The pattern to recognize when parsing a default value. */
-    private Pattern defaultPattern = Pattern.compile("\\(\\'?(.*?)\\'?\\)");
+    private Pattern _defaultPattern = Pattern.compile("\\(\\'?(.*?)\\'?\\)");
 
     /**
-     * Creates a new model reader instance for the given connection.
-     * 
-     * @param conn The database connection
+     * Creates a new model reader instance.
+     */
+    public JdbcModelReader()
+    {
+        _defaultSizes.put(new Integer(Types.CHAR),          "254");
+        _defaultSizes.put(new Integer(Types.VARCHAR),       "254");
+        _defaultSizes.put(new Integer(Types.LONGVARCHAR),   "254");
+        _defaultSizes.put(new Integer(Types.BINARY),        "254");
+        _defaultSizes.put(new Integer(Types.VARBINARY),     "254");
+        _defaultSizes.put(new Integer(Types.LONGVARBINARY), "254");
+        _defaultSizes.put(new Integer(Types.INTEGER),       "32");
+        _defaultSizes.put(new Integer(Types.BIGINT),        "64");
+        _defaultSizes.put(new Integer(Types.REAL),          "7,0");
+        _defaultSizes.put(new Integer(Types.FLOAT),         "15,0");
+        _defaultSizes.put(new Integer(Types.DOUBLE),        "15,0");
+        _defaultSizes.put(new Integer(Types.DECIMAL),       "15,15");
+        _defaultSizes.put(new Integer(Types.NUMERIC),       "15,15");
+    }
+
+    /**
+     * Returns the catalog in the database to read per default.
+     *
+     * @return The default catalog
      */
-    public JdbcModelReader(Connection conn)
+    public String getDefaultCatalog()
     {
-        connection = conn;
-        defaultSizes.put(new Integer(Types.CHAR),          "254");
-        defaultSizes.put(new Integer(Types.VARCHAR),       "254");
-        defaultSizes.put(new Integer(Types.LONGVARCHAR),   "254");
-        defaultSizes.put(new Integer(Types.BINARY),        "254");
-        defaultSizes.put(new Integer(Types.VARBINARY),     "254");
-        defaultSizes.put(new Integer(Types.LONGVARBINARY), "254");
-        defaultSizes.put(new Integer(Types.INTEGER),       "32");
-        defaultSizes.put(new Integer(Types.BIGINT),        "64");
-        defaultSizes.put(new Integer(Types.REAL),          "7,0");
-        defaultSizes.put(new Integer(Types.FLOAT),         "15,0");
-        defaultSizes.put(new Integer(Types.DOUBLE),        "15,0");
-        defaultSizes.put(new Integer(Types.DECIMAL),       "15,15");
-        defaultSizes.put(new Integer(Types.NUMERIC),       "15,15");
+        return _defaultCatalog;
     }
 
     /**
-     * Sets the catalog in the database to read.
+     * Sets the catalog in the database to read per default.
      * 
      * @param catalog The catalog
      */
-    public void setCatalog(String catalog)
+    public void setDefaultCatalog(String catalog)
     {
-        this.catalog = catalog;
+        _defaultCatalog = catalog;
     }
 
     /**
-     * Sets the schema in the database to read.
+     * Returns the schema in the database to read per default.
+     *
+     * @return The default schema
+     */
+    public String getDefaultSchemaPattern()
+    {
+        return _defaultSchemaPattern;
+    }
+
+    /**
+     * Sets the schema in the database to read per default.
      * 
-     * @param schema The schema
+     * @param schemaPattern The schema
+     */
+    public void setDefaultSchemaPattern(String schemaPattern)
+    {
+        _defaultSchemaPattern = schemaPattern;
+    }
+
+    /**
+     * Returns the table types to recognize per default.
+     *
+     * @return The default table types
      */
-    public void setSchema(String schema)
+    public String[] getDefaultTableTypes()
     {
-        this.schema = schema;
+        return _defaultTableTypes;
     }
 
     /**
-     * Sets the table types to recognize. Typical types are "TABLE", "VIEW", "SYSTEM TABLE",
-     * "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
+     * Sets the table types to recognize per default. Typical types are "TABLE", "VIEW",
+     * "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
      * 
      * @param types The table types
      */
-    public void setTableTypes(String[] types)
+    public void setDefaultTableTypes(String[] types)
     {
-        this.tableTypes = types;
+        _defaultTableTypes = types;
     }
 
     /**
-     * Creates the database model.
+     * Reads the database model from the given connection.
      * 
+     * @param connection The connection
      * @return The database model
      */
-    public Database getDatabase() throws SQLException
+    public Database getDatabase(Connection connection) throws SQLException
+    {
+        return getDatabase(connection, null, null, null);
+    }
+
+    /**
+     * Reads the database model from the given connection.
+     * 
+     * @param connection The connection
+     * @param catalog    The catalog to acess in the database; use <code>null</code>
for the default value
+     * @param schema     The schema to acess in the database; use <code>null</code>
for the default value
+     * @param tableTypes The table types to process; use <code>null</code> or
an empty list for the default ones
+     * @return The database model
+     */
+    public Database getDatabase(Connection connection, String catalog, String schema, String[]
tableTypes) throws SQLException
     {
         Database db = new Database();
 
-        for (Iterator it = getTables().iterator(); it.hasNext();)
+        for (Iterator it = getTables(connection, catalog, schema, tableTypes).iterator();
it.hasNext();)
         {
             db.addTable((Table)it.next());
         }
@@ -143,18 +182,27 @@
     /**
      * Returns a list of {@link Table} instances for the tables in the database.
      * 
+     * @param connection    The connection
+     * @param catalog       The catalog to acess in the database; use <code>null</code>
for the default value
+     * @param schemaPattern The schema(s) to acess in the database; use <code>null</code>
for the default value
+     * @param tableTypes    The table types to process; use <code>null</code>
or an empty list for the default ones
      * @return The list of tables
      */
-    private List getTables() throws SQLException
+    private List getTables(Connection connection, String catalog, String schemaPattern, String[]
tableTypes) throws SQLException
     {
         ResultSet tableData = null;
 
         try
         {
-            DatabaseMetaData metaData = connection.getMetaData();
+            DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();
 
-            tableData = metaData.getTables(catalog, schema, "%", tableTypes);
+            metaData.setMetaData(connection.getMetaData());
+            metaData.setCatalog(catalog == null ? getDefaultCatalog() : catalog);
+            metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern() :
schemaPattern);
+            metaData.setTableTypes((tableTypes == null) || (tableTypes.length == 0) ? getDefaultTableTypes()
: tableTypes);
             
+            tableData = metaData.getTables("%");
+
             Set  availableColumns = determineAvailableColumns(tableData);
             List tables           = new ArrayList();
 
@@ -210,13 +258,13 @@
      * @param tableName The name of the table
      * @return The list of columns
      */
-    private List getColumnsForTable(DatabaseMetaData metaData, String tableName) throws SQLException
+    private List getColumnsForTable(DatabaseMetaDataWrapper metaData, String tableName) throws
SQLException
     {
         ResultSet columnData = null;
 
         try
         {
-            columnData = metaData.getColumns(catalog, schema, tableName, null);
+            columnData = metaData.getColumns(tableName, null);
 
             Set  availableColumns = determineAvailableColumns(columnData);
             List columns          = new ArrayList();
@@ -232,7 +280,7 @@
                 col.setScale(getValueAsInt(columnData, "DECIMAL_DIGITS", availableColumns,
0));
                 // we're setting the size after the precision and radix in case
                 // the database prefers to return them in the size value 
-                col.setSize(getValueAsString(columnData, "COLUMN_SIZE", availableColumns,
(String)defaultSizes.get(new Integer(col.getTypeCode()))));
+                col.setSize(getValueAsString(columnData, "COLUMN_SIZE", availableColumns,
(String)_defaultSizes.get(new Integer(col.getTypeCode()))));
                 col.setRequired("NO".equalsIgnoreCase(getValueAsString(columnData, "IS_NULLABLE",
availableColumns, "YES").trim()));
                 if (primaryKeys.contains(col.getName()))
                 {
@@ -248,7 +296,7 @@
 
                 if (columnDefaultValue != null)
                 {
-                    Matcher m = defaultPattern.matcher(columnDefaultValue);
+                    Matcher m = _defaultPattern.matcher(columnDefaultValue);
 
                     if (m.matches())
                     {
@@ -277,14 +325,14 @@
      * @param tableName The name of the table from which to retrieve PK information
      * @return The list of the primary key column names
      */
-    private List getPrimaryKeysForTable(DatabaseMetaData metaData, String tableName) throws
SQLException
+    private List getPrimaryKeysForTable(DatabaseMetaDataWrapper metaData, String tableName)
throws SQLException
     {
         List      pks   = new ArrayList();
         ResultSet pkData = null;
 
         try
         {
-            pkData = metaData.getPrimaryKeys(catalog, schema, tableName);
+            pkData = metaData.getPrimaryKeys(tableName);
             while (pkData.next())
             {
                 pks.add(pkData.getString("COLUMN_NAME"));
@@ -292,7 +340,7 @@
         }
         catch (SQLException ex)
         {
-            log.warn("Could not determine the primary keys of table "+tableName, ex);
+            _log.warn("Could not determine the primary keys of table "+tableName, ex);
         }
         finally
         {
@@ -311,14 +359,14 @@
      * @param tableName The name of the table from which to retrieve FK information
      * @return The list of foreign keys
      */
-    private List getForeignKeysForTable(DatabaseMetaData metaData, String tableName) throws
SQLException
+    private List getForeignKeysForTable(DatabaseMetaDataWrapper metaData, String tableName)
throws SQLException
     {
         List      fks    = new ArrayList();
         ResultSet fkData = null;
 
         try
         {
-            fkData = metaData.getImportedKeys(catalog, schema, tableName);
+            fkData = metaData.getForeignKeys(tableName);
 
             Set        availableColumns = determineAvailableColumns(fkData);
             String     prevPkTable      = null;
@@ -354,7 +402,7 @@
         }
         catch (SQLException ex)
         {
-            log.warn("Could not determine the foreignkeys of table "+tableName, ex);
+            _log.warn("Could not determine the foreignkeys of table "+tableName, ex);
         }
         finally
         {
@@ -373,14 +421,14 @@
      * @param tableName The name of the table
      * @return The list of indices
      */
-    private List getIndicesForTable(DatabaseMetaData metaData, String tableName) throws SQLException
+    private List getIndicesForTable(DatabaseMetaDataWrapper metaData, String tableName) throws
SQLException
     {
         ResultSet indexData = null;
         List      indices   = new ArrayList();
 
         try 
         {
-            indexData = metaData.getIndexInfo(catalog, schema, tableName, false, false);
+            indexData = metaData.getIndices(tableName, false, false);
 
             Set availableColumns = determineAvailableColumns(indexData);
             Map indicesByName    = new LinkedMap();
@@ -417,7 +465,7 @@
         }
         catch (SQLException ex)
         {
-            log.trace("Could determine the indices for the table "+tableName, ex);
+            _log.trace("Could determine the indices for the table "+tableName, ex);
         }
         finally
         {

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java Sat Oct
29 10:32:00 2005
@@ -62,6 +62,8 @@
 
     /** The sql builder for this platform. */
     private SqlBuilder _builder;
+    /** The model reader for this platform. */
+    private JdbcModelReader _modelReader = new JdbcModelReader();
 
     /**
      * {@inheritDoc}
@@ -84,6 +86,24 @@
     /**
      * {@inheritDoc}
      */
+    public JdbcModelReader getModelReader()
+    {
+        return _modelReader;
+    }
+
+    /**
+     * Sets the model reader for this platform.
+     * 
+     * @param modelReader The model reader
+     */
+    protected void setModelReader(JdbcModelReader modelReader)
+    {
+        _modelReader = modelReader;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     public PlatformInfo getPlatformInfo()
     {
         return _builder.getPlatformInfo();
@@ -297,16 +317,7 @@
     public void alterTables(Connection connection, Database desiredModel, boolean doDrops,
boolean modifyColumns, boolean continueOnError) throws DynaSqlException
     {
         String   sql          = null;
-        Database currentModel = null;
-
-        try
-        {
-            currentModel = new JdbcModelReader(connection).getDatabase();
-        }
-        catch (SQLException ex)
-        {
-            throw new DynaSqlException("Error while reading the model from the database",
ex);
-        }
+        Database currentModel = readModelFromDatabase(connection);
 
         try
         {
@@ -1036,6 +1047,54 @@
         finally
         {
             closeStatement(statement);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */    
+    public Database readModelFromDatabase() throws DynaSqlException
+    {
+        return readModelFromDatabase(borrowConnection());
+    }
+
+    /**
+     * {@inheritDoc}
+     */    
+    public Database readModelFromDatabase(Connection connection) throws DynaSqlException
+    {
+        try
+        {
+            return getModelReader().getDatabase(connection);
+        }
+        catch (SQLException ex)
+        {
+            throw new DynaSqlException(ex);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Database readModelFromDatabase(String catalog, String schema, String[] tableTypes)
throws DynaSqlException
+    {
+        return readModelFromDatabase(borrowConnection(), catalog, schema, tableTypes);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Database readModelFromDatabase(Connection connection, String catalog, String schema,
String[] tableTypes) throws DynaSqlException
+    {
+        try
+        {
+            JdbcModelReader reader = getModelReader();
+            
+            return reader.getDatabase(connection, catalog, schema, tableTypes);
+        }
+        catch (SQLException ex)
+        {
+            throw new DynaSqlException(ex);
         }
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java Sat Oct 29 10:32:00
2005
@@ -18,8 +18,6 @@
 
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.ddlutils.Platform;
-import org.apache.ddlutils.PlatformFactory;
-import org.apache.ddlutils.PlatformUtils;
 import org.apache.tools.ant.BuildException;
 
 /**
@@ -30,14 +28,10 @@
  */
 public abstract class DatabaseCommand implements Command
 {
-    /** The type of the database. */
-    private String _databaseType;
-    /** The data source to use for accessing the database. */
-    private BasicDataSource _dataSource;
+    /** The platform configuration. */
+    private PlatformConfiguration _platformConf = new PlatformConfiguration();
     /** Whether to stop execution upon an error. */
     private boolean _failOnError = true;
-    /** Whether to use delimited SQL identifiers. */
-    private boolean _useDelimitedSqlIdentifiers = true;
 
     /**
      * Returns the database type.
@@ -46,7 +40,7 @@
      */
     protected String getDatabaseType()
     {
-        return _databaseType;
+        return _platformConf.getDatabaseType();
     }
 
     /**
@@ -56,19 +50,17 @@
      */
     protected BasicDataSource getDataSource()
     {
-        return _dataSource;
+        return _platformConf.getDataSource();
     }
 
     /**
-     * Sets the database info.
+     * Sets the platform configuration.
      * 
-     * @param dataSource The data source pointing to the database
-     * @param type       The database type
+     * @param platformConf The platform configuration
      */
-    protected void setDatabaseInfo(BasicDataSource dataSource, String type)
+    protected void setPlatformConfiguration(PlatformConfiguration platformConf)
     {
-        _dataSource   = dataSource;
-        _databaseType = type;
+        _platformConf = platformConf;
     }
 
     /**
@@ -93,65 +85,12 @@
     }
 
     /**
-     * Determines whether delimited SQL identifiers shall be used (the default).
-     *
-     * @return <code>true</code> if delimited SQL identifiers shall be used
-     */
-    public boolean isUseDelimitedSqlIdentifiers()
-    {
-        return _useDelimitedSqlIdentifiers;
-    }
-
-    /**
-     * Specifies whether delimited SQL identifiers shall be used.
-     *
-     * @param useDelimitedSqlIdentifiers <code>true</code> if delimited SQL identifiers
shall be used
-     */
-    public void setUseDelimitedSqlIdentifiers(boolean useDelimitedSqlIdentifiers)
-    {
-        _useDelimitedSqlIdentifiers = useDelimitedSqlIdentifiers;
-    }
-
-    /**
      * Creates the platform for the configured database.
      * 
      * @return The platform
      */
     protected Platform getPlatform() throws BuildException
     {
-        Platform platform = null;
-
-        if (_databaseType == null)
-        {
-            if (_dataSource == null)
-            {
-                throw new BuildException("No database specified.");
-            }
-            if (_databaseType == null)
-            {
-                _databaseType = new PlatformUtils().determineDatabaseType(_dataSource.getDriverClassName(),
-                                                                          _dataSource.getUrl());
-            }
-            if (_databaseType == null)
-            {
-                _databaseType = new PlatformUtils().determineDatabaseType(_dataSource);
-            }
-        }
-        try
-        {
-            platform = PlatformFactory.createNewPlatformInstance(_databaseType);
-        }
-        catch (Exception ex)
-        {
-            throw new BuildException("Database type "+_databaseType+" is not supported.",
ex);
-        }
-        if (platform == null)
-        {
-            throw new BuildException("Database type "+_databaseType+" is not supported.");
-        }
-        platform.setDataSource(_dataSource);
-        platform.getPlatformInfo().setUseDelimitedIdentifiers(isUseDelimitedSqlIdentifiers());
-
-        return platform;
+        return _platformConf.getPlatform();
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java Sat Oct 29 10:32:00
2005
@@ -20,6 +20,7 @@
 import java.util.Iterator;
 
 import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.ddlutils.Platform;
 import org.apache.ddlutils.model.Database;
 import org.apache.tools.ant.Task;
 
@@ -31,10 +32,8 @@
  */
 public abstract class DatabaseTaskBase extends Task
 {
-    /** The type of the database. */
-    private String _databaseType;
-    /** The data source to use for accessing the database. */
-    private BasicDataSource _dataSource;
+    /** The platform configuration. */
+    private PlatformConfiguration _platformConf = new PlatformConfiguration();
     /** The sub tasks to execute. */
     private ArrayList _commands = new ArrayList();
 
@@ -45,7 +44,7 @@
      */
     public String getDatabaseType()
     {
-        return _databaseType;
+        return _platformConf.getDatabaseType();
     }
 
     /**
@@ -55,7 +54,7 @@
      */
     public void setDatabaseType(String type)
     {
-        _databaseType = type;
+        _platformConf.setDatabaseType(type);
     }
 
     /**
@@ -65,7 +64,7 @@
      */
     public BasicDataSource getDataSource()
     {
-        return _dataSource;
+        return _platformConf.getDataSource();
     }
 
     /**
@@ -75,7 +74,27 @@
      */
     public void addConfiguredDatabase(BasicDataSource dataSource)
     {
-        _dataSource = dataSource;
+        _platformConf.setDataSource(dataSource);
+    }
+
+    /**
+     * Determines whether delimited SQL identifiers shall be used (the default).
+     *
+     * @return <code>true</code> if delimited SQL identifiers shall be used
+     */
+    public boolean isUseDelimitedSqlIdentifiers()
+    {
+        return _platformConf.isUseDelimitedSqlIdentifiers();
+    }
+
+    /**
+     * Specifies whether delimited SQL identifiers shall be used.
+     *
+     * @param useDelimitedSqlIdentifiers <code>true</code> if delimited SQL identifiers
shall be used
+     */
+    public void setUseDelimitedSqlIdentifiers(boolean useDelimitedSqlIdentifiers)
+    {
+        _platformConf.setUseDelimitedSqlIdentifiers(useDelimitedSqlIdentifiers);
     }
 
     /**
@@ -99,6 +118,16 @@
     }
 
     /**
+     * Creates the platform for the configured database.
+     * 
+     * @return The platform
+     */
+    protected Platform getPlatform()
+    {
+        return _platformConf.getPlatform();
+    }
+    
+    /**
      * Executes the commands.
      * 
      * @param model The database model
@@ -111,7 +140,7 @@
 
             if (cmd instanceof DatabaseCommand)
             {
-                ((DatabaseCommand)cmd).setDatabaseInfo(getDataSource(), getDatabaseType());
+                ((DatabaseCommand)cmd).setPlatformConfiguration(_platformConf);
             }
             cmd.execute(this, model);
         }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java Sat Oct 29
10:32:00 2005
@@ -20,7 +20,6 @@
 import java.util.StringTokenizer;
 
 import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.platform.JdbcModelReader;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 
@@ -151,24 +150,7 @@
 
         try
         {
-            JdbcModelReader reader = new JdbcModelReader(getDataSource().getConnection());
-
-            if ((_catalog != null) && (_catalog.length() > 0))
-            {
-                reader.setCatalog(_catalog);
-            }
-            if ((_schema != null) && (_schema.length() > 0))
-            {
-                reader.setSchema(_schema);
-            }
-
-            String[] tableTypes = getTableTypes();
-
-            if (tableTypes.length > 0)
-            {
-                reader.setTableTypes(tableTypes);
-            }
-            return reader.getDatabase();
+            return getPlatform().readModelFromDatabase(_catalog, _schema, getTableTypes());
         }
         catch (Exception ex)
         {

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/PlatformConfiguration.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/PlatformConfiguration.java?rev=329452&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/PlatformConfiguration.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/PlatformConfiguration.java Sat Oct
29 10:32:00 2005
@@ -0,0 +1,142 @@
+package org.apache.ddlutils.task;
+
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
+import org.apache.ddlutils.PlatformUtils;
+import org.apache.tools.ant.BuildException;
+
+/**
+ * Encloses the platform configuration for the Ant tasks.
+ * 
+ * @author Thomas Dudziak
+ * @version $Revision: 329426 $
+ */
+public class PlatformConfiguration
+{
+    /** The type of the database. */
+    private String _databaseType;
+    /** The data source to use for accessing the database. */
+    private BasicDataSource _dataSource;
+    /** Whether to use delimited SQL identifiers. */
+    private boolean _useDelimitedSqlIdentifiers = true;
+
+    /**
+     * Returns the database type.
+     * 
+     * @return The database type
+     */
+    public String getDatabaseType()
+    {
+        return _databaseType;
+    }
+
+    /**
+     * Sets the database type.
+     * 
+     * @param type The database type
+     */
+    public void setDatabaseType(String type)
+    {
+        _databaseType = type;
+    }
+
+    /**
+     * Returns the data source to use for accessing the database.
+     * 
+     * @return The data source
+     */
+    public BasicDataSource getDataSource()
+    {
+        return _dataSource;
+    }
+
+    /**
+     * Sets the data source to use for accessing the database.
+     * 
+     * @param dataSource The data source pointing to the database
+     */
+    public void setDataSource(BasicDataSource dataSource)
+    {
+        _dataSource = dataSource;
+    }
+
+    /**
+     * Determines whether delimited SQL identifiers shall be used (the default).
+     *
+     * @return <code>true</code> if delimited SQL identifiers shall be used
+     */
+    public boolean isUseDelimitedSqlIdentifiers()
+    {
+        return _useDelimitedSqlIdentifiers;
+    }
+
+    /**
+     * Specifies whether delimited SQL identifiers shall be used.
+     *
+     * @param useDelimitedSqlIdentifiers <code>true</code> if delimited SQL identifiers
shall be used
+     */
+    public void setUseDelimitedSqlIdentifiers(boolean useDelimitedSqlIdentifiers)
+    {
+        _useDelimitedSqlIdentifiers = useDelimitedSqlIdentifiers;
+    }
+
+    /**
+     * Creates the platform for the configured database.
+     * 
+     * @return The platform
+     */
+    public Platform getPlatform() throws BuildException
+    {
+        Platform platform = null;
+
+        if (_databaseType == null)
+        {
+            if (_dataSource == null)
+            {
+                throw new BuildException("No database specified.");
+            }
+            if (_databaseType == null)
+            {
+                _databaseType = new PlatformUtils().determineDatabaseType(_dataSource.getDriverClassName(),
+                                                                          _dataSource.getUrl());
+            }
+            if (_databaseType == null)
+            {
+                _databaseType = new PlatformUtils().determineDatabaseType(_dataSource);
+            }
+        }
+        try
+        {
+            platform = PlatformFactory.createNewPlatformInstance(_databaseType);
+        }
+        catch (Exception ex)
+        {
+            throw new BuildException("Database type "+_databaseType+" is not supported.",
ex);
+        }
+        if (platform == null)
+        {
+            throw new BuildException("Database type "+_databaseType+" is not supported.");
+        }
+        platform.setDataSource(_dataSource);
+        platform.getPlatformInfo().setUseDelimitedIdentifiers(isUseDelimitedSqlIdentifiers());
+
+        return platform;
+    }
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java?rev=329452&r1=329451&r2=329452&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java Sat
Oct 29 10:32:00 2005
@@ -23,7 +23,6 @@
 
 import org.apache.ddlutils.Platform;
 import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.platform.JdbcModelReader;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.Task;
@@ -121,9 +120,8 @@
                 {
                     throw new BuildException("No database specified.");
                 }
-                connection = getDataSource().getConnection();
 
-                Database currentModel = new JdbcModelReader(connection).getDatabase();
+                Database currentModel = platform.readModelFromDatabase();
 
                 platform.getSqlBuilder().alterDatabase(currentModel, model, _doDrops, true);
             }



Mime
View raw message