db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r375128 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/platform/db2/Db2Builder.java java/org/apache/ddlutils/platform/db2/Db2ModelReader.java java/org/apache/ddlutils/platform/db2/Db2Platform.java test/jdbc.properties.db2
Date Sun, 05 Feb 2006 23:08:20 GMT
Author: tomdz
Date: Sun Feb  5 15:08:18 2006
New Revision: 375128

URL: http://svn.apache.org/viewcvs?rev=375128&view=rev
Log:
Fixed and enhanced Db2 platform

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2ModelReader.java
    db/ddlutils/trunk/src/test/jdbc.properties.db2
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Platform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java?rev=375128&r1=375127&r2=375128&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Builder.java Sun Feb  5
15:08:18 2006
@@ -17,11 +17,13 @@
  */
 
 import java.io.IOException;
+import java.sql.Types;
 
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.model.Column;
 import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.platform.SqlBuilder;
+import org.apache.ddlutils.util.Jdbc3Utils;
 
 /**
  * The SQL Builder for DB2.
@@ -41,7 +43,24 @@
     {
         super(info);
     }
-    
+
+
+    /**
+     * {@inheritDoc}
+     */
+    protected String getNativeDefaultValue(Column column)
+    {
+        if ((column.getTypeCode() == Types.BIT) ||
+            (Jdbc3Utils.supportsJava14JdbcTypes() && (column.getTypeCode() == Jdbc3Utils.determineBooleanTypeCode())))
+        {
+            return getDefaultValueHelper().convert(column.getDefaultValue(), column.getTypeCode(),
Types.SMALLINT).toString();
+        }
+        else
+        {
+            return super.getNativeDefaultValue(column);
+        }
+    }
+
     /**
      * {@inheritDoc}
      */

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2ModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2ModelReader.java?rev=375128&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2ModelReader.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2ModelReader.java Sun Feb
 5 15:08:18 2006
@@ -0,0 +1,184 @@
+package org.apache.ddlutils.platform.db2;
+
+/*
+ * Copyright 2006 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.SQLException;
+import java.sql.Types;
+import java.util.Map;
+
+import org.apache.ddlutils.DdlUtilsException;
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.model.Column;
+import org.apache.ddlutils.model.Index;
+import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
+import org.apache.ddlutils.platform.JdbcModelReader;
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.PatternCompiler;
+import org.apache.oro.text.regex.PatternMatcher;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
+
+/**
+ * Reads a database model from a Db2 UDB database.
+ *
+ * @author Thomas Dudziak
+ * @version $Revision: $
+ */
+public class Db2ModelReader extends JdbcModelReader
+{
+	/** Known system tables that Db2 creates (e.g. automatic maintenance). */
+	private static final String[] KNOWN_SYSTEM_TABLES = { "STMG_DBSIZE_INFO", "HMON_ATM_INFO",
"HMON_COLLECTION", "POLICY" };
+	/** The regular expression pattern for the time values that Db2 returns. */
+	private Pattern _db2TimePattern;
+	/** The regular expression pattern for the timestamp values that Db2 returns. */
+	private Pattern _db2TimestampPattern;
+
+	/**
+     * Creates a new model reader for Db2 databases.
+     * 
+     * @param platformInfo The platform specific settings
+     */
+    public Db2ModelReader(PlatformInfo platformInfo)
+    {
+        super(platformInfo);
+        setDefaultCatalogPattern(null);
+        setDefaultSchemaPattern(null);
+
+        PatternCompiler compiler = new Perl5Compiler();
+
+    	try
+    	{
+    		_db2TimePattern      = compiler.compile("'(\\d{2}).(\\d{2}).(\\d{2})'");
+    		_db2TimestampPattern = compiler.compile("'(\\d{4}\\-\\d{2}\\-\\d{2})\\-(\\d{2}).(\\d{2}).(\\d{2})(\\.\\d{1,8})?'");
+        }
+    	catch (MalformedPatternException ex)
+        {
+        	throw new DdlUtilsException(ex);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+	protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+	{
+        String tableName = (String)values.get("TABLE_NAME");
+
+        for (int idx = 0; idx < KNOWN_SYSTEM_TABLES.length; idx++)
+        {
+        	if (KNOWN_SYSTEM_TABLES[idx].equals(tableName))
+        	{
+        		return null;
+        	}
+        }
+
+        Table table = super.readTable(metaData, values);
+
+        // Db2 does not return the auto-increment status via the database metadata
+        determineAutoIncrementFromResultSetMetaData(table, table.getColumns());
+        return table;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+    protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
+    {
+		Column column = super.readColumn(metaData, values);
+
+		if (column.getDefaultValue() != null)
+		{
+			if (column.getTypeCode() == Types.TIME)
+			{
+				PatternMatcher matcher = new Perl5Matcher();
+
+				// Db2 returns "HH24.MI.SS"
+				if (matcher.matches(column.getDefaultValue(), _db2TimePattern))
+				{
+					StringBuffer newDefault = new StringBuffer();
+
+					newDefault.append("'");
+					// the hour
+					newDefault.append(matcher.getMatch().group(1));
+					newDefault.append(":");
+					// the minute
+					newDefault.append(matcher.getMatch().group(2));
+					newDefault.append(":");
+					// the second
+					newDefault.append(matcher.getMatch().group(3));
+					newDefault.append("'");
+
+					column.setDefaultValue(newDefault.toString());
+				}
+			}
+			else if (column.getTypeCode() == Types.TIMESTAMP)
+			{
+				PatternMatcher matcher = new Perl5Matcher();
+
+				// Db2 returns "YYYY-MM-DD-HH24.MI.SS.FF"
+				if (matcher.matches(column.getDefaultValue(), _db2TimestampPattern))
+				{
+					StringBuffer newDefault = new StringBuffer();
+
+					newDefault.append("'");
+					// group 1 is the date which has the correct format
+					newDefault.append(matcher.getMatch().group(1));
+					newDefault.append(" ");
+					// the hour
+					newDefault.append(matcher.getMatch().group(2));
+					newDefault.append(":");
+					// the minute
+					newDefault.append(matcher.getMatch().group(3));
+					newDefault.append(":");
+					// the second
+					newDefault.append(matcher.getMatch().group(4));
+					// optionally, the fraction
+					if ((matcher.getMatch().groups() > 4) && (matcher.getMatch().group(4) !=
null))
+					{
+						newDefault.append(matcher.getMatch().group(5));
+					}
+					newDefault.append("'");
+
+					column.setDefaultValue(newDefault.toString());
+				}
+			}
+		}
+		return column;
+	}
+
+	/**
+     * {@inheritDoc}
+     */
+    protected boolean isInternalPrimaryKeyIndex(Table table, Index index)
+    {
+    	if (index.getName().startsWith("SQL"))
+    	{
+            // Db2 uses the form "SQL060205225246220"
+    		try
+    		{
+    			Long.parseLong(index.getName().substring(3));
+    			return true;
+    		}
+    		catch (NumberFormatException ex)
+    		{}
+    		
+    	}
+		return false;
+    }
+}

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Platform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Platform.java?rev=375128&r1=375127&r2=375128&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Platform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/db2/Db2Platform.java Sun Feb 
5 15:08:18 2006
@@ -57,24 +57,28 @@
         info.setForeignKeysEmbedded(false);
         info.setIndicesEmbedded(false);
         // the BINARY types are also handled by Db2Builder.getSqlType(Column)
-        info.addNativeTypeMapping(Types.ARRAY,         "BLOB");
+        info.addNativeTypeMapping(Types.ARRAY,         "BLOB",                     Types.BLOB);
         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");
+        info.addNativeTypeMapping(Types.BIT,           "SMALLINT",                 Types.SMALLINT);
+        info.addNativeTypeMapping(Types.FLOAT,         "DOUBLE",                   Types.DOUBLE);
+        info.addNativeTypeMapping(Types.JAVA_OBJECT,   "BLOB",                     Types.BLOB);
         info.addNativeTypeMapping(Types.LONGVARBINARY, "LONG VARCHAR FOR BIT DATA");
         info.addNativeTypeMapping(Types.LONGVARCHAR,   "LONG VARCHAR");
         info.addNativeTypeMapping(Types.NULL,          "LONG VARCHAR FOR BIT DATA");
-        info.addNativeTypeMapping(Types.OTHER,         "BLOB");
-        info.addNativeTypeMapping(Types.STRUCT,        "BLOB");
-        info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT");
+        info.addNativeTypeMapping(Types.NUMERIC,       "DECIMAL",                  Types.DECIMAL);
+        info.addNativeTypeMapping(Types.OTHER,         "BLOB",                     Types.BLOB);
+        info.addNativeTypeMapping(Types.STRUCT,        "BLOB",                     Types.BLOB);
+        info.addNativeTypeMapping(Types.TINYINT,       "SMALLINT",                 Types.SMALLINT);
         info.addNativeTypeMapping(Types.VARBINARY,     "VARCHAR {0} FOR BIT DATA");
-        info.addNativeTypeMapping("BOOLEAN", "SMALLINT");
+        info.addNativeTypeMapping("BOOLEAN", "SMALLINT", "SMALLINT");
 
+        info.addDefaultSize(Types.CHAR,      254);
+        info.addDefaultSize(Types.VARCHAR,   254);
         info.addDefaultSize(Types.BINARY,    254);
         info.addDefaultSize(Types.VARBINARY, 254);
 
         setSqlBuilder(new Db2Builder(info));
+        setModelReader(new Db2ModelReader(info));
     }
 
     /**

Added: db/ddlutils/trunk/src/test/jdbc.properties.db2
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/jdbc.properties.db2?rev=375128&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/jdbc.properties.db2 (added)
+++ db/ddlutils/trunk/src/test/jdbc.properties.db2 Sun Feb  5 15:08:18 2006
@@ -0,0 +1,14 @@
+# JDBC properties for Db2 UDB
+
+# Use this property if ddlutils does not recognize the platform from the settings
+#ddlutils.platform=Db2
+
+# Properties starting with "datasource." will be fed into the datasource instance of the
+# class configured via the datasource.class property
+
+datasource.class=org.apache.commons.dbcp.BasicDataSource
+
+datasource.driverClassName=COM.ibm.db2.jdbc.net.DB2Driver
+datasource.url=jdbc:db2://localhost:6789/ddlutils
+datasource.username=ddlutils
+datasource.password=ddlutils



Mime
View raw message