You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2010/05/25 16:01:34 UTC
svn commit: r948045 - in /db/derby/code/trunk/java:
engine/org/apache/derby/impl/sql/compile/ResultSetNode.java
testing/org/apache/derbyTesting/functionTests/tests/lang/InsertTest.java
Author: kahatlen
Date: Tue May 25 14:01:33 2010
New Revision: 948045
URL: http://svn.apache.org/viewvc?rev=948045&view=rev
Log:
DERBY-4671: Embedded driver does not work with jbossCache
Set the type of the dynamic parameters in statements like
INSERT INTO T SELECT ? FROM T
to prevent a NullPointerException and to revert to the pre-10.6
behaviour.
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/InsertTest.java
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java?rev=948045&r1=948044&r2=948045&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java Tue May 25 14:01:33 2010
@@ -304,12 +304,8 @@ public abstract class ResultSetNode exte
}
/**
- * Set the type of each parameter in the result column list if this node
- * represents a table constructor (aka VALUES clause). Table constructors
- * are represented either by a {@code RowResultSetNode} or by a
- * {@code UnionNode} with multiple {@code RowResultSetNode} children and
- * whose {@code tableConstructor()} method returns {@code true}. For all
- * other nodes, this method should be a no-op.
+ * Set the type of each parameter in the result column list for this
+ * table constructor.
*
* @param typeColumns The ResultColumnList containing the desired result
* types.
@@ -319,9 +315,17 @@ public abstract class ResultSetNode exte
void setTableConstructorTypes(ResultColumnList typeColumns)
throws StandardException
{
- // Nothing to be done unless this node represents a VALUES clause, in
- // which case the overrides in RowResultSetNode or UnionNode will do
- // the necessary work.
+ // VALUES clause needs special handling that's taken care of in a
+ // sub-class. For all other nodes, just go through the result columns
+ // and set the type for dynamic parameters.
+ for (int i = 0; i < resultColumns.size(); i++) {
+ ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
+ ValueNode re = rc.getExpression();
+ if (re != null && re.requiresTypeFromContext()) {
+ ResultColumn typeCol = (ResultColumn) typeColumns.elementAt(i);
+ re.setType(typeCol.getTypeServices());
+ }
+ }
}
/**
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/InsertTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/InsertTest.java?rev=948045&r1=948044&r2=948045&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/InsertTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/InsertTest.java Tue May 25 14:01:33 2010
@@ -34,6 +34,8 @@ import org.apache.derbyTesting.junit.Tes
*/
public class InsertTest extends BaseJDBCTestCase {
+ private static final String PARAMETER_IN_SELECT_LIST = "42X34";
+
public InsertTest(String name) {
super(name);
}
@@ -134,4 +136,55 @@ public class InsertTest extends BaseJDBC
"1");
s.execute("delete from t3");
}
+
+ /**
+ * Regression test for DERBY-4671. Verify that dynamic parameters can be
+ * used in the select list in an INSERT INTO ... SELECT FROM statement.
+ * This used to work, but the fix for DERBY-4420 made it throw a
+ * NullPointerException.
+ */
+ public void testInsertFromSelectWithParameters() throws SQLException {
+ Statement s = createStatement();
+ s.execute("create table derby4671(x int)");
+ s.execute("insert into derby4671 values (1), (2)");
+
+ // This call failed with a NullPointerException
+ PreparedStatement ins1 = prepareStatement(
+ "insert into derby4671 select ? from derby4671");
+
+ ins1.setInt(1, 7);
+ assertUpdateCount(ins1, 2);
+
+ JDBC.assertFullResultSet(
+ s.executeQuery("select * from derby4671 order by x"),
+ new String[][] {{"1"}, {"2"}, {"7"}, {"7"}});
+
+ // Also verify that it works when the ? is in an expression
+ PreparedStatement ins2 = prepareStatement(
+ "insert into derby4671 select (x+?)*10 from derby4671");
+
+ ins2.setInt(1, 77);
+ assertUpdateCount(ins2, 4);
+
+ JDBC.assertFullResultSet(
+ s.executeQuery("select * from derby4671 order by x"),
+ new String[][] {
+ {"1"}, {"2"}, {"7"}, {"7"},
+ {"780"}, {"790"}, {"840"}, {"840"}});
+
+ // We only accept ? in the top level select list, so these should
+ // still fail
+ assertCompileError(
+ PARAMETER_IN_SELECT_LIST,
+ "insert into derby4671 select ? from derby4671 "
+ + "union select ? from derby4671");
+ assertCompileError(
+ PARAMETER_IN_SELECT_LIST,
+ "insert into derby4671 select ? from derby4671 "
+ + "except select ? from derby4671");
+ assertCompileError(
+ PARAMETER_IN_SELECT_LIST,
+ "insert into derby4671 select ? from derby4671 "
+ + "intersect select ? from derby4671");
+ }
}