db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r360488 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql: PostgreSqlModelReader.java PostgreSqlPlatform.java
Date Sun, 01 Jan 2006 14:44:43 GMT
Author: tomdz
Date: Sun Jan  1 06:44:39 2006
New Revision: 360488

URL: http://svn.apache.org/viewcvs?rev=360488&view=rev
Log:
Finished PostgreSql platform

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java?rev=360488&r1=360487&r2=360488&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlModelReader.java
Sun Jan  1 06:44:39 2006
@@ -16,17 +16,21 @@
  * limitations under the License.
  */
 
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Index;
 import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
 import org.apache.ddlutils.platform.JdbcModelReader;
 
 /**
  * Reads a database model from a PostgreSql database.
- * 
- * TODO: Add default value parsing which are of the form '-9000000000000000000::bigint' or
- *       ''some value'::character varying' or ''2000-01-01'::date'
  *
  * @author Thomas Dudziak
  * @version $Revision: $
@@ -49,10 +53,141 @@
     /**
      * {@inheritDoc}
      */
+    protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Table table = super.readTable(metaData, values);
+
+        // PostgreSQL also returns unique indics for non-pk auto-increment columns
+        // which are of the form "[table]_[column]_key"
+        HashMap uniquesByName = new HashMap();
+
+        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
+        {
+            Index index = table.getIndex(indexIdx);
+
+            if (index.isUnique() && (index.getName() != null))
+            {
+                uniquesByName.put(index.getName(), index);
+            }
+        }
+        for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++)
+        {
+            Column column = table.getColumn(columnIdx);
+            if (column.isAutoIncrement() && !column.isPrimaryKey())
+            {
+                String indexName = table.getName() + "_" + column.getName() + "_key";
+
+                if (uniquesByName.containsKey(indexName))
+                {
+                    table.removeIndex((Index)uniquesByName.get(indexName));
+                    uniquesByName.remove(indexName);
+                }
+            }
+        }
+        return table;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+        Column column = super.readColumn(metaData, values);
+
+        if (column.getSize() != null)
+        {
+            if (column.getSizeAsInt() <= 0)
+            {
+                column.setSize(null);
+            }
+        }
+
+        String defaultValue = column.getDefaultValue();
+
+        if ((defaultValue != null) && (defaultValue.length() > 0))
+        {
+            // If the default value looks like "nextval('ROUNDTRIP_VALUE_seq'::text)"
+            // then it is an auto-increment column
+            if (defaultValue.startsWith("nextval("))
+            {
+                column.setAutoIncrement(true);
+                defaultValue = null;
+            }
+            else
+            {
+                // PostgreSQL returns default values in the forms "-9000000000000000000::bigint"
or
+                // "'some value'::character varying" or "'2000-01-01'::date"
+                switch (column.getTypeCode())
+                {
+                    case Types.INTEGER:
+                    case Types.BIGINT:
+                    case Types.DECIMAL:
+                    case Types.NUMERIC:
+                        defaultValue = extractUndelimitedDefaultValue(defaultValue);
+                        break;
+                    case Types.CHAR:
+                    case Types.VARCHAR:
+                    case Types.DATE:
+                    case Types.TIME:
+                    case Types.TIMESTAMP:
+                        defaultValue = extractDelimitedDefaultValue(defaultValue);
+                        break;
+                }
+            }
+            column.setDefaultValue(defaultValue);
+        }
+        return column;
+    }
+
+    /**
+     * Extractes the default value from a default value spec of the form
+     * "'some value'::character varying" or "'2000-01-01'::date".
+     * 
+     * @param defaultValue The default value spec
+     * @return The default value
+     */
+    private String extractDelimitedDefaultValue(String defaultValue)
+    {
+        if (defaultValue.startsWith("'"))
+        {
+            int valueEnd = defaultValue.indexOf("'::");
+
+            if (valueEnd > 0)
+            {
+                return defaultValue.substring("'".length(), valueEnd);
+            }
+        }
+        return defaultValue;
+    }
+    
+    /**
+     * Extractes the default value from a default value spec of the form
+     * "-9000000000000000000::bigint".
+     * 
+     * @param defaultValue The default value spec
+     * @return The default value
+     */
+    private String extractUndelimitedDefaultValue(String defaultValue)
+    {
+        int valueEnd = defaultValue.indexOf("::");
+
+        if (valueEnd > 0)
+        {
+            return defaultValue.substring(0, valueEnd);
+        }
+        else
+        {
+            return defaultValue;
+        }
+    }
+    
+    /**
+     * {@inheritDoc}
+     */
     protected boolean isInternalForeignKeyIndex(Table table, ForeignKey fk, Index index)
     {
-        // TODO Auto-generated method stub
-        return super.isInternalForeignKeyIndex(table, fk, index);
+        // PostgreSQL does not return an index for a foreign key
+        return false;
     }
 
     /**
@@ -60,7 +195,7 @@
      */
     protected boolean isInternalPrimaryKeyIndex(Table table, Index index)
     {
-        // PostgreSql uses the form "<tablename>_pkey"
+        // PostgreSql uses the form "[tablename]_pkey"
         return (table.getName() + "_pkey").equals(index.getName());
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java?rev=360488&r1=360487&r2=360488&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
Sun Jan  1 06:44:39 2006
@@ -18,14 +18,17 @@
 
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.sql.Types;
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.commons.beanutils.DynaBean;
 import org.apache.ddlutils.DynaSqlException;
 import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.dynabean.SqlDynaProperty;
 import org.apache.ddlutils.platform.PlatformImplBase;
 
 /**
@@ -60,8 +63,8 @@
         info.addNativeTypeMapping(Types.ARRAY,         "BYTEA",            Types.BINARY);
         info.addNativeTypeMapping(Types.BINARY,        "BYTEA",            Types.BINARY);
         info.addNativeTypeMapping(Types.BIT,           "BOOLEAN");
-        info.addNativeTypeMapping(Types.BLOB,          "BYTEA");
-        info.addNativeTypeMapping(Types.CLOB,          "TEXT");
+        info.addNativeTypeMapping(Types.BLOB,          "BYTEA",            Types.BINARY);
+        info.addNativeTypeMapping(Types.CLOB,          "TEXT",             Types.VARCHAR);
         info.addNativeTypeMapping(Types.DECIMAL,       "NUMERIC",          Types.NUMERIC);
         info.addNativeTypeMapping(Types.DISTINCT,      "BYTEA",            Types.BINARY);
         info.addNativeTypeMapping(Types.DOUBLE,        "DOUBLE PRECISION");
@@ -203,5 +206,35 @@
         // With PostgreSQL, you create a database by executing "DROP DATABASE" in an existing
database (usually 
         // the template1 database because it usually exists)
         createOrDropDatabase(jdbcDriverClassName, connectionUrl, username, password, null,
false);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean,
SqlDynaProperty property) throws SQLException
+    {
+        int     typeCode = property.getColumn().getTypeCode();
+        Object  value    = dynaBean.get(property.getName());
+
+        // PostgreSQL doesn't like setNull for BYTEA columns
+        if (value == null)
+        {
+            switch (typeCode)
+            {
+                case Types.BINARY:
+                case Types.VARBINARY:
+                case Types.LONGVARBINARY:
+                case Types.BLOB:
+                    statement.setBytes(sqlIndex, null);
+                    break;
+                default:
+                    statement.setNull(sqlIndex, typeCode);
+                    break;
+            }
+        }
+        else
+        {
+            super.setObject(statement, sqlIndex, dynaBean, property);
+        }
     }
 }



Mime
View raw message