The problem in a nutshell:
This is what happens when trying to insert a row with a few null columns using a prepared statement and groovy.sql.Sql:
groovy:000> val
===> [123123123, 2, null, 0, 0, , null, , 1213020112511801, 1283425009158952, 1, 2, null, 0, 0, , null, , 1213020112511801, 1283425009158952]
groovy:000> destSql.execute "insert into my_table values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", val
ERROR java.sql.SQLException: Type is not supported.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException (Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException (Unknown Source)
at org.apache.derby.client.am.PreparedStatement.setObject (Unknown Source)
at groovysh_evaluate.run (groovysh_evaluate:3)
...
groovy:000>
I get a similar result with destSql.dataSet("my_table").add valueMap
.
This is what happens when trying to insert the same row as a GString
:
groovy:000> destSql.execute "insert into my_table values (${val[0]}, ${val[1]}, ${val[2]}, ${val[3]}, ${val[4]}, ${val[5]}, ${val[6]}, ${val[7]}, ${val[8]}, ${val[9]})"
===> false
groovy:000>
Now, I understand that there are problems inserting null values using a prepared statement (as documented e.g. here and here).
What I would like to understand is the following:
1) I find it hard to believe that inserting nulls with prepared statements is impossible, so why does it sometimes cause problems? Less than perfect JDBC drivers, database engines?
2) Why does the example with the GString work? Following the same logic, doesn't it still type information even about the null columns, so how come it works?