db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r423912 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/types/DataTypeDescriptor.java impl/jdbc/EmbedPreparedStatement.java impl/jdbc/EmbedResultSet.java
Date Thu, 20 Jul 2006 11:54:36 GMT
Author: kahatlen
Date: Thu Jul 20 04:54:35 2006
New Revision: 423912

URL: http://svn.apache.org/viewvc?rev=423912&view=rev
Log:
DERBY-1527: Factor out type checks in EmbedResultSet and EmbedPreparedStatement

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataTypeDescriptor.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataTypeDescriptor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataTypeDescriptor.java?rev=423912&r1=423911&r2=423912&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataTypeDescriptor.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/DataTypeDescriptor.java Thu
Jul 20 04:54:35 2006
@@ -1070,6 +1070,44 @@
 		}
 	}
 
+	/**
+	 * Determine if an ASCII stream can be inserted into a column or parameter
+	 * of type <code>jdbcType</code>.
+	 *
+	 * @param jdbcType JDBC type of column or parameter
+	 * @return <code>true</code> if an ASCII stream can be inserted;
+	 *         <code>false</code> otherwise
+	 */
+	public static boolean isAsciiStreamAssignable(int jdbcType) {
+		return jdbcType == Types.CLOB || isCharacterType(jdbcType);
+	}
+
+	/**
+	 * Determine if a binary stream can be inserted into a column or parameter
+	 * of type <code>jdbcType</code>.
+	 *
+	 * @param jdbcType JDBC type of column or parameter
+	 * @return <code>true</code> if a binary stream can be inserted;
+	 *         <code>false</code> otherwise
+	 */
+	public static boolean isBinaryStreamAssignable(int jdbcType) {
+		return jdbcType == Types.BLOB || isBinaryType(jdbcType);
+	}
+
+	/**
+	 * Determine if a character stream can be inserted into a column or
+	 * parameter of type <code>jdbcType</code>.
+	 *
+	 * @param jdbcType JDBC type of column or parameter
+	 * @return <code>true</code> if a character stream can be inserted;
+	 *         <code>false</code> otherwise
+	 */
+	public static boolean isCharacterStreamAssignable(int jdbcType) {
+		// currently, we support the same types for ASCII streams and
+		// character streams
+		return isAsciiStreamAssignable(jdbcType);
+	}
+
 	public String	toString()
 	{
 		return typeDescriptor.toString();

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java?rev=423912&r1=423911&r2=423912&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement.java
Thu Jul 20 04:54:35 2006
@@ -567,8 +567,7 @@
      */
     public final void setAsciiStream(int parameterIndex, InputStream x, long length)
 	    throws SQLException {
-        checkCharacterStreamConditions(parameterIndex,
-                                        "java.io.InputStream(ASCII)");
+        checkAsciiStreamConditions(parameterIndex);
 		java.io.Reader r = null;
 
 		if (x != null)
@@ -635,7 +634,7 @@
     public final void setCharacterStream(int parameterIndex,
        			  java.io.Reader reader,
 			  long length) throws SQLException {
-        checkCharacterStreamConditions(parameterIndex, "java.io.Reader");
+        checkCharacterStreamConditions(parameterIndex);
         setCharacterStreamInternal(parameterIndex, reader, false, length);
 	}
 
@@ -662,26 +661,31 @@
     }
 
     /**
-     * Check general (pre)conditions for setXXXStream methods operating on
-     * character streams.
+     * Check general preconditions for setCharacterStream methods.
      *
      * @param parameterIndex 1-based index of the parameter.
-     * @param srcDataTypeDesc type description of the source data. Used in
-     *          error message for data type conversion failure.
      */
-    private final void checkCharacterStreamConditions(int parameterIndex,
-                                                       String srcDataTypeDesc)
+    private final void checkCharacterStreamConditions(int parameterIndex)
             throws SQLException {
         checkStatus();
         int jdbcTypeId = getParameterJDBCType(parameterIndex);
-        switch (jdbcTypeId) {
-            case Types.CHAR:
-            case Types.VARCHAR:
-            case Types.LONGVARCHAR:
-            case Types.CLOB:
-                break;
-            default:
-                throw dataTypeConversion(parameterIndex, srcDataTypeDesc);
+        if (!DataTypeDescriptor.isCharacterStreamAssignable(jdbcTypeId)) {
+            throw dataTypeConversion(parameterIndex, "java.io.Reader");
+        }
+    }
+
+    /**
+     * Check general preconditions for setAsciiStream methods.
+     *
+     * @param parameterIndex 1-based index of the parameter.
+     */
+    private final void checkAsciiStreamConditions(int parameterIndex)
+            throws SQLException {
+        checkStatus();
+        int jdbcTypeId = getParameterJDBCType(parameterIndex);
+        if (!DataTypeDescriptor.isAsciiStreamAssignable(jdbcTypeId)) {
+            throw dataTypeConversion(parameterIndex,
+                                     "java.io.InputStream(ASCII)");
         }
     }
 
@@ -907,8 +911,7 @@
 	}
 
     /**
-     * Check general (pre)conditions for setXXXStream methods operating on
-     * binary streams.
+     * Check general preconditions for setBinaryStream methods.
      *
      * @param parameterIndex 1-based index of the parameter.
      */
@@ -916,13 +919,7 @@
             throws SQLException {
         checkStatus();
         int jdbcTypeId = getParameterJDBCType(parameterIndex);
-        switch (jdbcTypeId) {
-        case Types.BINARY:
-        case Types.VARBINARY:
-        case Types.LONGVARBINARY:
-        case Types.BLOB:
-            break;
-        default:
+        if (!DataTypeDescriptor.isBinaryStreamAssignable(jdbcTypeId)) {
             throw dataTypeConversion(parameterIndex, "java.io.InputStream");
         }
     }
@@ -1723,8 +1720,7 @@
      */
     public void setAsciiStream(int parameterIndex, InputStream x)
             throws SQLException {
-        checkCharacterStreamConditions(parameterIndex,
-                                        "java.io.InputStream(ASCII)");
+        checkAsciiStreamConditions(parameterIndex);
         java.io.Reader asciiStream = null;
 
         if (x != null) {
@@ -1763,7 +1759,7 @@
      */
     public void setCharacterStream(int parameterIndex, Reader reader)
             throws SQLException {
-        checkCharacterStreamConditions(parameterIndex, "java.io.Reader");
+        checkCharacterStreamConditions(parameterIndex);
         setCharacterStreamInternal(parameterIndex, reader,
                                    true, -1);
     }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java?rev=423912&r1=423911&r2=423912&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java Thu Jul
20 04:54:35 2006
@@ -39,6 +39,7 @@
 import org.apache.derby.iapi.sql.Activation;
 import org.apache.derby.iapi.sql.execute.CursorActivation;
 
+import org.apache.derby.iapi.types.DataTypeDescriptor;
 import org.apache.derby.iapi.types.DataValueDescriptor;
 import org.apache.derby.iapi.types.RawToBinaryFormatStream;
 import org.apache.derby.iapi.types.ReaderToUTF8Stream;
@@ -2316,6 +2317,59 @@
         }
     }
 
+    /**
+     * Check whether it is OK to update a column using
+     * <code>updateAsciiStream()</code>.
+     *
+     * @param columnIndex the column index (first column is 1)
+     * @exception SQLException if the column could not be updated with
+     * <code>updateAsciiStream()</code>
+     */
+    private void checksBeforeUpdateAsciiStream(int columnIndex)
+        throws SQLException
+    {
+        checksBeforeUpdateXXX("updateAsciiStream", columnIndex);
+        int colType = getColumnType(columnIndex);
+        if (!DataTypeDescriptor.isAsciiStreamAssignable(colType)) {
+            throw dataTypeConversion(columnIndex, "java.io.InputStream");
+        }
+    }
+
+    /**
+     * Check whether it is OK to update a column using
+     * <code>updateBinaryStream()</code>.
+     *
+     * @param columnIndex the column index (first column is 1)
+     * @exception SQLException if the column could not be updated with
+     * <code>updateBinaryStream()</code>
+     */
+    private void checksBeforeUpdateBinaryStream(int columnIndex)
+        throws SQLException
+    {
+        checksBeforeUpdateXXX("updateBinaryStream", columnIndex);
+        int colType = getColumnType(columnIndex);
+        if (!DataTypeDescriptor.isBinaryStreamAssignable(colType)) {
+            throw dataTypeConversion(columnIndex, "java.io.InputStream");
+        }
+    }
+
+    /**
+     * Check whether it is OK to update a column using
+     * <code>updateCharacterStream()</code>.
+     *
+     * @param columnIndex the column index (first column is 1)
+     * @exception SQLException if the column could not be updated with
+     * <code>updateCharacterStream()</code>
+     */
+    private void checksBeforeUpdateCharacterStream(int columnIndex)
+        throws SQLException
+    {
+        checksBeforeUpdateXXX("updateCharacterStream", columnIndex);
+        int colType = getColumnType(columnIndex);
+        if (!DataTypeDescriptor.isCharacterStreamAssignable(colType)) {
+            throw dataTypeConversion(columnIndex, "java.io.Reader");
+        }
+    }
     
     /**
 	 * JDBC 2.0
@@ -2663,18 +2717,7 @@
 	 */
 	public void updateAsciiStream(int columnIndex, java.io.InputStream x,
 			long length) throws SQLException {
-		checksBeforeUpdateXXX("updateAsciiStream", columnIndex);
-
-		int colType = getColumnType(columnIndex);
-		switch (colType) {
-			case Types.CHAR:
-			case Types.VARCHAR:
-			case Types.LONGVARCHAR:
-			case Types.CLOB:
-				break;
-			default:
-				throw dataTypeConversion(columnIndex, "java.io.InputStream");
-		}
+		checksBeforeUpdateAsciiStream(columnIndex);
 
 		java.io.Reader r = null;
 		if (x != null)
@@ -2708,18 +2751,7 @@
      */
     public void updateAsciiStream(int columnIndex, InputStream x)
             throws SQLException {
-        checksBeforeUpdateXXX("updateAsciiStream", columnIndex);
-
-        int colType = getColumnType(columnIndex);
-        switch (colType) {
-            case Types.CHAR:
-            case Types.VARCHAR:
-            case Types.LONGVARCHAR:
-            case Types.CLOB:
-                break;
-            default:
-                throw dataTypeConversion(columnIndex, "java.io.InputStream");
-        }
+        checksBeforeUpdateAsciiStream(columnIndex);
 
         java.io.Reader r = null;
         if (x != null) {
@@ -2753,17 +2785,7 @@
 	 */
 	public void updateBinaryStream(int columnIndex, java.io.InputStream x,
 			long length) throws SQLException {
-		checksBeforeUpdateXXX("updateBinaryStream", columnIndex);
-		int colType = getColumnType(columnIndex);
-		switch (colType) {
-			case Types.BINARY:
-			case Types.VARBINARY:
-			case Types.LONGVARBINARY:
-			case Types.BLOB:
-				break;
-			default:
-				throw dataTypeConversion(columnIndex, "java.io.InputStream");
-		}
+		checksBeforeUpdateBinaryStream(columnIndex);
 
 		if (x == null)
 		{
@@ -2794,17 +2816,7 @@
      */
     public void updateBinaryStream(int columnIndex, InputStream x)
             throws SQLException {
-        checksBeforeUpdateXXX("updateBinaryStream", columnIndex);
-        int colType = getColumnType(columnIndex);
-        switch (colType) {
-            case Types.BINARY:
-            case Types.VARBINARY:
-            case Types.LONGVARBINARY:
-            case Types.BLOB:
-                break;
-            default:
-                throw dataTypeConversion(columnIndex, "java.io.InputStream");
-        }
+        checksBeforeUpdateBinaryStream(columnIndex);
         updateBinaryStreamInternal(columnIndex, x, true, -1,
                                    "updateBinaryStream");
     }
@@ -2878,20 +2890,7 @@
 	 */
 	public void updateCharacterStream(int columnIndex, java.io.Reader x,
 			long length) throws SQLException {
-		//If the column type is the right datatype, this method will eventually call getDVDforColumnToBeUpdated
which will check for
-		//the read only resultset. But for other datatypes, we want to catch if this updateCharacterStream
is being issued
-		//against a read only resultset. And that is the reason for call to checksBeforeUpdateXXX
here.
-		checksBeforeUpdateXXX("updateCharacterStream", columnIndex);
-		int colType = getColumnType(columnIndex);
-		switch (colType) {
-			case Types.CHAR:
-			case Types.VARCHAR:
-			case Types.LONGVARCHAR:
-			case Types.CLOB:
-				break;
-			default:
-				throw dataTypeConversion(columnIndex, "java.io.Reader");
-		}
+		checksBeforeUpdateCharacterStream(columnIndex);
 		updateCharacterStreamInternal(columnIndex, x, false, length,
                                       "updateCharacterStream");
 	}
@@ -2915,17 +2914,7 @@
      */
     public void updateCharacterStream(int columnIndex, Reader x)
             throws SQLException {
-        checksBeforeUpdateXXX("updateCharacterStream", columnIndex);
-        int colType = getColumnType(columnIndex);
-        switch (colType) {
-            case Types.CHAR:
-            case Types.VARCHAR:
-            case Types.LONGVARCHAR:
-            case Types.CLOB:
-                break;
-            default:
-                throw dataTypeConversion(columnIndex, "java.io.Reader");
-        }
+        checksBeforeUpdateCharacterStream(columnIndex);
         updateCharacterStreamInternal(columnIndex, x, true, -1,
                                       "updateCharacterStream");
     }



Mime
View raw message