db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r358997 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils: ./ model/ platform/
Date Sun, 25 Dec 2005 18:30:31 GMT
Author: tomdz
Date: Sun Dec 25 10:30:18 2005
New Revision: 358997

URL: http://svn.apache.org/viewcvs?rev=358997&view=rev
Log:
Simplified builders by giving the ability to specify default sizes per datatype in the platform
info, and by allowing for a placeholder for the size in the native type spec
Added workaround for Derby which doesn't like BigDecimal objects in setObject
Added ability for the platforms to postprocess the model read from the database; is used right
now to remove the quotation marks from the default value for text-type columns

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapeBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapePlatform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Builder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Platform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbasePlatform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/JdbcModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformInfo.java Sun Dec 25 10:30:18 2005
@@ -93,6 +93,9 @@
     /** Contains those JDBC types whose corresponding native types are types that have a
size on this platform. */
     private HashSet _typesWithSize = new HashSet();
 
+    /** Contains the default sizes for those JDBC types whose corresponding native types
require a size. */
+    private HashMap _typesDefaultSizes = new HashMap();
+
     /** Contains those JDBC types whose corresponding native types are types that have precision
and scale on this platform. */
     private HashSet _typesWithPrecisionAndScale = new HashSet();
 
@@ -553,6 +556,52 @@
         return targetJdbcType == null ? typeCode : targetJdbcType.intValue(); 
     }
 
+    /**
+     * Adds a default size for the given jdbc type.
+     * 
+     * @param jdbcTypeCode The jdbc type code
+     * @param defaultSize  The default size
+     */
+    public void addDefaultSize(int jdbcTypeCode, int defaultSize)
+    {
+        _typesDefaultSizes.put(new Integer(jdbcTypeCode), new Integer(defaultSize));
+    }
+
+    /**
+     * Returns the default size value for the given type, if any.
+     * 
+     * @param jdbcTypeCode The jdbc type code
+     * @return The default size or <code>null</code> if none is defined
+     */
+    public Integer getDefaultSize(int jdbcTypeCode)
+    {
+        return (Integer)_typesDefaultSizes.get(new Integer(jdbcTypeCode));
+    }
+    
+    /**
+     * Adds a default size for the given jdbc type.
+     * 
+     * @param jdbcTypeName The name of the jdbc type, one of the {@link Types} constants
+     * @param defaultSize  The default size
+     */
+    public void addDefaultSize(String jdbcTypeName, int defaultSize)
+    {
+        try
+        {
+            Field constant = Types.class.getField(jdbcTypeName);
+
+            if (constant != null)
+            {
+                addDefaultSize(constant.getInt(null), defaultSize);
+            }
+        }
+        catch (Exception ex)
+        {
+            // ignore -> won't be defined
+            _log.warn("Cannot add default size for undefined jdbc type "+jdbcTypeName, ex);
+        }
+    }
+    
     /**
      * Specifies whether the native type for the given sql type code (one of the
      * {@link java.sql.Types} constants) has a null default value on this platform.

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java Sun Dec 25 10:30:18 2005
@@ -53,7 +53,7 @@
     /** The size of the column for JDBC types that require/support this. */
     private Integer _sizeAsInt;
     /** The scale of the column for JDBC types that require/support this. */
-    private int _scale = 0;
+    private int _scale;
     /** The default value. */
     private String _defaultValue;
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapeBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapeBuilder.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapeBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapeBuilder.java Sun Dec
25 10:30:18 2005
@@ -17,7 +17,6 @@
  */
 
 import java.io.IOException;
-import java.sql.Types;
 
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.model.Column;
@@ -39,34 +38,6 @@
     public CloudscapeBuilder(PlatformInfo info)
     {
         super(info);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    protected String getSqlType(Column column)
-    {
-        switch (column.getTypeCode())
-        {
-            case Types.BINARY:
-            case Types.VARBINARY:
-                StringBuffer sqlType = new StringBuffer();
-                
-                sqlType.append(getNativeType(column));
-                sqlType.append(" (");
-                if (column.getSize() == null)
-                {
-                    sqlType.append("254");
-                }
-                else
-                {
-                    sqlType.append(column.getSize());
-                }
-                sqlType.append(") FOR BIT DATA");
-                return sqlType.toString();
-            default:
-                return super.getSqlType(column);
-        }
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapePlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapePlatform.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapePlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/CloudscapePlatform.java Sun Dec
25 10:30:18 2005
@@ -49,7 +49,7 @@
         info.setMaxIdentifierLength(128);
         // BINARY and VARBINARY will also be handled by CloudscapeBuilder.getSqlType
         info.addNativeTypeMapping(Types.ARRAY,         "BLOB",                     Types.BLOB);
-        info.addNativeTypeMapping(Types.BINARY,        "CHAR");
+        info.addNativeTypeMapping(Types.BINARY,        "CHAR {0} FOR BIT DATA");
         info.addNativeTypeMapping(Types.BIT,           "SMALLINT",                 Types.SMALLINT);
         info.addNativeTypeMapping(Types.DISTINCT,      "BLOB",                     Types.BLOB);
         info.addNativeTypeMapping(Types.DOUBLE,        "DOUBLE PRECISION");
@@ -62,10 +62,14 @@
         info.addNativeTypeMapping(Types.REF,           "LONG VARCHAR FOR BIT DATA", Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.STRUCT,        "BLOB",                      Types.BLOB);
         info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT",                  Types.SMALLINT);
-        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR");
+        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR {0} FOR BIT DATA");
         info.addNativeTypeMapping("BOOLEAN",  "SMALLINT",                  "SMALLINT");
         info.addNativeTypeMapping("DATALINK", "LONG VARCHAR FOR BIT DATA", "LONGVARBINARY");
 
+        info.addDefaultSize(Types.BINARY,    254);
+        info.addDefaultSize(Types.CHAR,      254);
+        info.addDefaultSize(Types.VARBINARY, 254);
+        info.addDefaultSize(Types.VARCHAR,   254);
         setSqlBuilder(new CloudscapeBuilder(info));
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Builder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Builder.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Builder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Builder.java Sun Dec 25 10:30:18
2005
@@ -17,7 +17,6 @@
  */
 
 import java.io.IOException;
-import java.sql.Types;
 
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.model.Column;
@@ -40,34 +39,6 @@
     public Db2Builder(PlatformInfo info)
     {
         super(info);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    protected String getSqlType(Column column)
-    {
-        switch (column.getTypeCode())
-        {
-            case Types.BINARY:
-            case Types.VARBINARY:
-                StringBuffer sqlType = new StringBuffer();
-
-                sqlType.append(getNativeType(column));
-                sqlType.append(" (");
-                if (column.getSize() == null)
-                {
-                    sqlType.append("254");
-                }
-                else
-                {
-                    sqlType.append(column.getSize());
-                }
-                sqlType.append(") FOR BIT DATA");
-                return sqlType.toString();
-            default:
-                return super.getSqlType(column);
-        }
     }
     
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Platform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Platform.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Platform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Db2Platform.java Sun Dec 25 10:30:18
2005
@@ -57,7 +57,7 @@
         info.setIndicesEmbedded(false);
         // the BINARY types are also handled by Db2Builder.getSqlType(Column)
         info.addNativeTypeMapping(Types.ARRAY,         "BLOB");
-        info.addNativeTypeMapping(Types.BINARY,        "CHAR");
+        info.addNativeTypeMapping(Types.BINARY,        "CHAR {0} FOR BIT DATA");
         info.addNativeTypeMapping(Types.BIT,           "SMALLINT");
         info.addNativeTypeMapping(Types.FLOAT,         "REAL");
         info.addNativeTypeMapping(Types.JAVA_OBJECT,   "BLOB");
@@ -67,8 +67,11 @@
         info.addNativeTypeMapping(Types.OTHER,         "BLOB");
         info.addNativeTypeMapping(Types.STRUCT,        "BLOB");
         info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT");
-        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR");
+        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR {0} FOR BIT DATA");
         info.addNativeTypeMapping("BOOLEAN", "SMALLINT");
+
+        info.addDefaultSize(Types.BINARY,    254);
+        info.addDefaultSize(Types.VARBINARY, 254);
 
         setSqlBuilder(new Db2Builder(info));
     }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbaseBuilder.java Sun Dec
25 10:30:18 2005
@@ -17,7 +17,6 @@
  */
 
 import java.io.IOException;
-import java.sql.Types;
 import java.util.Map;
 
 import org.apache.ddlutils.PlatformInfo;
@@ -108,21 +107,6 @@
             println(" !!");
             print("COMMIT");
             printEndOfStatement();
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    protected String getSqlType(Column column)
-    {
-        switch (column.getTypeCode())
-        {
-            case Types.BINARY:
-            case Types.VARBINARY:
-                return super.getSqlType(column) + " CHARACTER SET OCTETS";
-            default:
-                return super.getSqlType(column);
         }
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbasePlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbasePlatform.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbasePlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/InterbasePlatform.java Sun Dec
25 10:30:18 2005
@@ -53,7 +53,7 @@
         // BINARY and VARBINARY are also handled by the InterbaseBuilder.getSqlType method
         info.addNativeTypeMapping(Types.ARRAY,         "BLOB");
         info.addNativeTypeMapping(Types.BIGINT,        "DECIMAL(38,0)");
-        info.addNativeTypeMapping(Types.BINARY,        "CHAR");
+        info.addNativeTypeMapping(Types.BINARY,        "CHAR {0} CHARACTER SET OCTETS");
         info.addNativeTypeMapping(Types.BIT,           "DECIMAL(1,0)");
         info.addNativeTypeMapping(Types.CLOB,          "BLOB SUB_TYPE TEXT");
         info.addNativeTypeMapping(Types.DISTINCT,      "BLOB");
@@ -68,7 +68,7 @@
         info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT");
         info.addNativeTypeMapping(Types.REF,           "BLOB");
         info.addNativeTypeMapping(Types.STRUCT,        "BLOB");
-        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR");
+        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR {0} CHARACTER SET OCTETS");
         info.addNativeTypeMapping("BOOLEAN",  "DECIMAL(1,0)");
         info.addNativeTypeMapping("DATALINK", "BLOB");
 

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=358997&r1=358996&r2=358997&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 Sun Dec 25
10:30:18 2005
@@ -316,10 +316,18 @@
                 col.setName(getValueAsString(columnData, "COLUMN_NAME", availableColumns,
"UNKNOWN"));
                 col.setTypeCode(getValueAsInt(columnData, "DATA_TYPE", availableColumns,
java.sql.Types.OTHER));
                 col.setPrecisionRadix(getValueAsInt(columnData, "NUM_PREC_RADIX", availableColumns,
10));
-                col.setScale(getValueAsInt(columnData, "DECIMAL_DIGITS", availableColumns,
0));
+
+                int scale = 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()))));
+                if (scale != 0)
+                {
+                    // if there is a scale value, set it after the size (which probably did
not contain
+                    // a scale specification)
+                    col.setScale(scale);
+                }
                 col.setRequired("NO".equalsIgnoreCase(getValueAsString(columnData, "IS_NULLABLE",
availableColumns, "YES").trim()));
                 col.setDescription(getValueAsString(columnData, "REMARKS", availableColumns,
null));                
                 if (primaryKeys.contains(col.getName()))

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlBuilder.java Sun Dec 25 10:30:18
2005
@@ -17,7 +17,6 @@
  */
 
 import java.io.IOException;
-import java.sql.Types;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -53,34 +52,6 @@
         print("DROP TABLE IF EXISTS ");
         printIdentifier(getTableName(table));
         printEndOfStatement();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    protected String getSqlType(Column column)
-    {
-        switch (column.getTypeCode())
-        {
-            case Types.BINARY:
-            case Types.VARBINARY:
-                StringBuffer sqlType = new StringBuffer();
-
-                sqlType.append(getNativeType(column));
-                sqlType.append("(");
-                if (column.getSize() == null)
-                {
-                    sqlType.append("254");
-                }
-                else
-                {
-                    sqlType.append(column.getSize());
-                }
-                sqlType.append(") BINARY");
-                return sqlType.toString();
-            default:
-                return super.getSqlType(column);
-        }
     }
 
     /**

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java Sun Dec 25
10:30:18 2005
@@ -54,7 +54,7 @@
         info.setDelimiterToken("`");
         // the BINARY types are also handled by MySqlBuilder.getSqlType(Column)
         info.addNativeTypeMapping(Types.ARRAY,         "LONGBLOB");
-        info.addNativeTypeMapping(Types.BINARY,        "CHAR");
+        info.addNativeTypeMapping(Types.BINARY,        "CHAR {0} BINARY");
         info.addNativeTypeMapping(Types.BIT,           "TINYINT(1)");
         info.addNativeTypeMapping(Types.BLOB,          "LONGBLOB");
         info.addNativeTypeMapping(Types.CLOB,          "LONGTEXT");
@@ -73,10 +73,13 @@
         // that DATETIME (year to seconds) as of MySQL 5, we map the JDBC type here to DATETIME
         // TODO: Make this configurable
         info.addNativeTypeMapping(Types.TIMESTAMP,     "DATETIME");
-        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR");
+        info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR {0} BINARY");
         info.addNativeTypeMapping("BOOLEAN",  "TINYINT(1)");
         info.addNativeTypeMapping("DATALINK", "MEDIUMBLOB");
 
+        info.addDefaultSize(Types.BINARY,    254);
+        info.addDefaultSize(Types.VARBINARY, 254);
+        
         setSqlBuilder(new MySqlBuilder(info));
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java Sun Dec 25
10:30:18 2005
@@ -82,6 +82,9 @@
         info.addNativeTypeMapping("BOOLEAN",  "NUMBER(1,0)");
         info.addNativeTypeMapping("DATALINK", "BLOB");
 
+        info.addDefaultSize(Types.BINARY,  254);
+        info.addDefaultSize(Types.VARCHAR, 254);
+
         setSqlBuilder(new OracleBuilder(info));
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/OracleBuilder.java Sun Dec 25
10:30:18 2005
@@ -17,7 +17,6 @@
  */
 
 import java.io.IOException;
-import java.sql.Types;
 import java.util.Map;
 
 import org.apache.ddlutils.PlatformInfo;
@@ -107,35 +106,6 @@
             println(" FROM dual;");
             print("END");
             printEndOfStatement();
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    protected String getSqlType(Column column)
-    {
-        switch (column.getTypeCode())
-        {
-            // we need to always specify a size for these types
-            case Types.BINARY:
-            case Types.VARCHAR:
-                StringBuffer sqlType = new StringBuffer();
-
-                sqlType.append(getNativeType(column));
-                sqlType.append("(");
-                if (column.getSize() == null)
-                {
-                    sqlType.append("254");
-                }
-                else
-                {
-                    sqlType.append(column.getSize());
-                }
-                sqlType.append(")");
-                return sqlType.toString();
-            default:
-                return super.getSqlType(column);
         }
     }
 

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=358997&r1=358996&r2=358997&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 Sun Dec
25 10:30:18 2005
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.StringWriter;
 import java.lang.reflect.InvocationTargetException;
+import java.math.BigDecimal;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -49,6 +50,7 @@
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Database;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.TypeMap;
 import org.apache.ddlutils.util.JdbcSupport;
 
 /**
@@ -1372,7 +1374,10 @@
     {
         try
         {
-            return getModelReader().getDatabase(connection, name);
+            Database model = getModelReader().getDatabase(connection, name);
+
+            postprocessModelFromDatabase(model);
+            return model;
         }
         catch (SQLException ex)
         {
@@ -1396,8 +1401,10 @@
         try
         {
             JdbcModelReader reader = getModelReader();
-            
-            return reader.getDatabase(connection, name, catalog, schema, tableTypes);
+            Database        model  = reader.getDatabase(connection, name, catalog, schema,
tableTypes);
+
+            postprocessModelFromDatabase(model);
+            return model;
         }
         catch (SQLException ex)
         {
@@ -1406,6 +1413,38 @@
     }
 
     /**
+     * Allows the platform to postprocess the model just read from the database.
+     * 
+     * @param model The model
+     */
+    protected void postprocessModelFromDatabase(Database model)
+    {
+        // Default values for CHAR/VARCHAR/LONGVARCHAR columns have quotation marks
+        // around them which we'll remove now
+        for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
+        {
+            Table table = model.getTable(tableIdx);
+
+            for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
+            {
+                Column column = table.getColumn(columnIdx);
+
+                if (TypeMap.isTextType(column.getTypeCode()))
+                {
+                    String defaultValue = column.getDefaultValue();
+
+                    if ((defaultValue != null) &&
+                        defaultValue.startsWith("'") && defaultValue.endsWith("'"))
+                    {
+                        defaultValue = defaultValue.substring(1, defaultValue.length() -
1);
+                        column.setDefaultValue(defaultValue);
+                    }
+                }
+            }
+        }
+    }
+    
+    /**
      * Derives the column values for the given dyna properties from the dyna bean.
      * 
      * @param properties The properties
@@ -1444,6 +1483,11 @@
         else if (value instanceof String)
         {
             statement.setString(sqlIndex, (String)value);
+        }
+        else if (value instanceof BigDecimal)
+        {
+            // Derby doesn't like BigDecimal's in setObject
+            statement.setBigDecimal(sqlIndex, (BigDecimal)value);
         }
         else
         {

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=358997&r1=358996&r2=358997&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Sun Dec 25 10:30:18
2005
@@ -59,6 +59,8 @@
 {
     /** The line separator for in between sql commands. */
     private static final String LINE_SEPERATOR = System.getProperty("line.separator", "\n");
+    /** The placeholder for the size value in the native type spec. */
+    protected static final String SIZE_PLACEHOLDER = "{0}";
 
     /** The Log to which logging calls will be made. */
     protected final Log _log = LogFactory.getLog(SqlBuilder.class);
@@ -1171,25 +1173,37 @@
      */
     protected String getSqlType(Column column)
     {
-        StringBuffer sqlType = new StringBuffer(getNativeType(column));
+        String       nativeType = getNativeType(column);
+        int          sizePos    = nativeType.indexOf(SIZE_PLACEHOLDER);
+        StringBuffer sqlType    = new StringBuffer();
 
-        if (column.getSize() != null)
+        sqlType.append(sizePos >= 0 ? nativeType.substring(0, sizePos) : nativeType);
+
+        Object sizeSpec = column.getSize();
+        
+        if (sizeSpec == null)
+        {
+            sizeSpec = getPlatformInfo().getDefaultSize(column.getTypeCode());
+        }
+        if (sizeSpec != null)
         {
             if (getPlatformInfo().hasSize(column.getTypeCode()))
             {
                 sqlType.append("(");
-                sqlType.append(column.getSize());
+                sqlType.append(sizeSpec.toString());
                 sqlType.append(")");
             }
             else if (getPlatformInfo().hasPrecisionAndScale(column.getTypeCode()))
             {
                 sqlType.append("(");
-                sqlType.append(column.getSize());
+                sqlType.append(sizeSpec.toString());
                 sqlType.append(",");
                 sqlType.append(column.getScale());
                 sqlType.append(")");
             }
         }
+        sqlType.append(sizePos >= 0 ? nativeType.substring(sizePos + SIZE_PLACEHOLDER.length())
: "");
+
         return sqlType.toString();
     }
 



Mime
View raw message