db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r420747 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase: InterbaseModelReader.java InterbasePlatform.java
Date Tue, 11 Jul 2006 07:28:34 GMT
Author: tomdz
Date: Tue Jul 11 00:28:33 2006
New Revision: 420747

URL: http://svn.apache.org/viewvc?rev=420747&view=rev
Log:
Enhancements to the Interbase platform

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java?rev=420747&r1=420746&r2=420747&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbaseModelReader.java
Tue Jul 11 00:28:33 2006
@@ -66,8 +66,7 @@
 
         if (table != null)
         {
-            determineColumnSizes(table);
-            determineColumnDefaultValues(table);
+            determineExtraColumnInfo(table);
             determineAutoIncrementColumns(table);
             adjustColumns(table);
         }
@@ -127,13 +126,19 @@
     }
 
     /**
-     * Helper method that determines the column default values using Interbase's system tables.
+     * Helper method that determines extra column info from the system tables: default value,
precision, scale.
      *
      * @param table The table
      */
-    protected void determineColumnDefaultValues(Table table) throws SQLException
+    protected void determineExtraColumnInfo(Table table) throws SQLException
     {
-        PreparedStatement prepStmt = getConnection().prepareStatement("SELECT RDB$FIELD_NAME,
RDB$DEFAULT_SOURCE FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME=?");
+        StringBuffer query   = new StringBuffer();
+        
+        query.append("SELECT a.RDB$FIELD_NAME, a.RDB$DEFAULT_SOURCE, b.RDB$FIELD_PRECISION,
b.RDB$FIELD_SCALE,");
+        query.append(" b.RDB$FIELD_TYPE, b.RDB$FIELD_SUB_TYPE FROM RDB$RELATION_FIELDS a,
RDB$FIELDS b");
+        query.append(" WHERE a.RDB$RELATION_NAME=? AND a.RDB$FIELD_SOURCE=b.RDB$FIELD_NAME");
+
+        PreparedStatement prepStmt = getConnection().prepareStatement(query.toString());
 
         try
         {
@@ -159,47 +164,25 @@
                         }
                         column.setDefaultValue(defaultValue);
                     }
-                }
-            }
-            rs.close();
-        }
-        finally
-        {
-            prepStmt.close();
-        }
-    }
-
-    /**
-     * Helper method that determines the column sizes (precision, scale) using Interbase's
system tables.
-     *
-     * @param table The table
-     */
-    protected void determineColumnSizes(Table table) throws SQLException
-    {
-        PreparedStatement prepStmt = getConnection().prepareStatement("SELECT a.RDB$FIELD_NAME,
b.RDB$FIELD_PRECISION, b.RDB$FIELD_SCALE FROM RDB$RELATION_FIELDS a, RDB$FIELDS b WHERE a.RDB$RELATION_NAME=?
AND a.RDB$FIELD_SOURCE=b.RDB$FIELD_NAME");
-
-        try
-        {
-            prepStmt.setString(1, getPlatform().isDelimitedIdentifierModeOn() ? table.getName()
: table.getName().toUpperCase());
-
-            ResultSet rs = prepStmt.executeQuery();
-
-            while (rs.next())
-            {
-                String columnName = rs.getString(1).trim();
-                Column column     = table.findColumn(columnName, getPlatform().isDelimitedIdentifierModeOn());
-
-                if (column != null)
-                {
-                    short   precision          = rs.getShort(2);
+                    
+                    short   precision          = rs.getShort(3);
                     boolean precisionSpecified = !rs.wasNull();
-                    short   scale              = rs.getShort(3);
+                    short   scale              = rs.getShort(4);
                     boolean scaleSpecified     = !rs.wasNull();
 
                     if (precisionSpecified)
                     {
                         // for some reason, Interbase stores the negative scale
                         column.setSizeAndScale(precision, scaleSpecified ? -scale : 0);
+                    }
+
+                    short dbType      = rs.getShort(5);
+                    short blobSubType = rs.getShort(6);
+
+                    // CLOBs are returned by the driver as VARCHAR
+                    if (!rs.wasNull() && (dbType == 261) && (blobSubType
== 1))
+                    {
+                        column.setTypeCode(Types.CLOB);
                     }
                 }
             }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java?rev=420747&r1=420746&r2=420747&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/interbase/InterbasePlatform.java
Tue Jul 11 00:28:33 2006
@@ -16,8 +16,16 @@
  * limitations under the License.
  */
 
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.sql.Types;
 
+import org.apache.ddlutils.DdlUtilsException;
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.platform.PlatformImplBase;
 
@@ -54,16 +62,18 @@
         // BINARY and VARBINARY are also handled by the InterbaseBuilder.getSqlType method
         info.addNativeTypeMapping(Types.ARRAY,         "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.BIGINT,        "NUMERIC(18,0)");
+        // Theoretically we could use (VAR)CHAR CHARACTER SET OCTETS but the JDBC driver
is not
+        // able to handle that properly (the byte[]/BinaryStream accessors do not work)
         info.addNativeTypeMapping(Types.BINARY,        "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.BIT,           "SMALLINT",           Types.SMALLINT);
         info.addNativeTypeMapping(Types.BLOB,          "BLOB",               Types.LONGVARBINARY);
-        info.addNativeTypeMapping(Types.CLOB,          "BLOB SUB_TYPE TEXT", Types.VARCHAR);
+        info.addNativeTypeMapping(Types.CLOB,          "BLOB SUB_TYPE TEXT");
         info.addNativeTypeMapping(Types.DISTINCT,      "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.DOUBLE,        "DOUBLE PRECISION");
         info.addNativeTypeMapping(Types.FLOAT,         "DOUBLE PRECISION",   Types.DOUBLE);
         info.addNativeTypeMapping(Types.JAVA_OBJECT,   "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.LONGVARBINARY, "BLOB",               Types.LONGVARBINARY);
-        info.addNativeTypeMapping(Types.LONGVARCHAR,   "BLOB SUB_TYPE TEXT", Types.VARCHAR);
+        info.addNativeTypeMapping(Types.LONGVARCHAR,   "BLOB SUB_TYPE TEXT", Types.CLOB);
         info.addNativeTypeMapping(Types.NULL,          "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.OTHER,         "BLOB",               Types.LONGVARBINARY);
         info.addNativeTypeMapping(Types.REAL,          "FLOAT");
@@ -74,6 +84,11 @@
         info.addNativeTypeMapping("BOOLEAN",  "SMALLINT", "SMALLINT");
         info.addNativeTypeMapping("DATALINK", "BLOB",     "LONGVARBINARY");
 
+        info.setDefaultSize(Types.CHAR,      254);
+        info.setDefaultSize(Types.VARCHAR,   254);
+        info.setDefaultSize(Types.BINARY,    254);
+        info.setDefaultSize(Types.VARBINARY, 254);
+        
         setSqlBuilder(new InterbaseBuilder(this));
         setModelReader(new InterbaseModelReader(this));
     }
@@ -85,4 +100,76 @@
     {
         return DATABASENAME;
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void setStatementParameterValue(PreparedStatement statement, int sqlIndex,
int typeCode, Object value) throws SQLException
+    {
+        if (value != null)
+        {
+            if ((value instanceof byte[]) &&
+                ((typeCode == Types.BINARY) || (typeCode == Types.VARBINARY) || (typeCode
== Types.BLOB)))
+            {
+                byte[]               bytes  = (byte[])value;
+                ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
+
+                statement.setBinaryStream(sqlIndex, stream, bytes.length);
+                return;
+            }
+            else if ((value instanceof String) && ((typeCode == Types.CLOB) || (typeCode
== Types.LONGVARCHAR)))
+            {
+                // Clob is not supported directly
+                statement.setString(sqlIndex, (String)value);
+            }
+        }
+        super.setStatementParameterValue(statement, sqlIndex, typeCode, value);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Object extractColumnValue(ResultSet resultSet, String columnName, int columnIdx,
int jdbcType) throws SQLException
+    {
+        boolean useIdx = (columnName == null);
+
+        switch (jdbcType)
+        {
+            case Types.BINARY:
+            case Types.VARBINARY:
+            case Types.BLOB:
+                try
+                {
+                    BufferedInputStream input = new BufferedInputStream(useIdx ? resultSet.getBinaryStream(columnIdx)
: resultSet.getBinaryStream(columnName));
+        
+                    if (resultSet.wasNull())
+                    {
+                        return null;
+                    }
+        
+                    ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
+                    byte[]                data   = new byte[1024];
+                    int                   numRead;
+        
+                    while ((numRead = input.read(data, 0, data.length)) != -1)
+                    {
+                        buffer.write(data, 0, numRead);
+                    }
+                    input.close();
+                    return buffer.toByteArray();
+                }
+                catch (IOException ex)
+                {
+                    throw new DdlUtilsException(ex);
+                }
+            case Types.LONGVARCHAR:
+            case Types.CLOB:
+                String value = useIdx ? resultSet.getString(columnIdx) : resultSet.getString(columnName);
+
+                return resultSet.wasNull() ? null : value;
+            default:
+                return super.extractColumnValue(resultSet, columnName, columnIdx, jdbcType);
+        }
+    }
+
 }



Mime
View raw message