Permalink
Browse files

Fix handling of nil bound variable arguments in the jdbc postgres sub…

…adapter

The change made to fix stored procedure arguments on SQL Server
and Oracle appears to not work on PostgreSQL.  Leave the default
the same but override the handling for nil values in the jdbc
PostgreSQL subadapter.
  • Loading branch information...
jeremyevans committed Jun 8, 2011
1 parent e392c8b commit 2e15e94adb67118582da8316d0e94b184c9c3b83
Showing with 22 additions and 1 deletion.
  1. +1 −1 CHANGELOG
  2. +6 −0 lib/sequel/adapters/jdbc/postgresql.rb
  3. +15 −0 spec/integration/prepared_statement_test.rb
View
@@ -1,6 +1,6 @@
=== HEAD
-* Fix handling of nil values in stored procedure arguments in the jdbc adapter (wei)
+* Fix handling of nil values in bound variables and prepared statement and stored procedure arguments in the jdbc adapter (jeremyevans, wei)
* Allow treating Datasets as Expressions, e.g. DB[:table1].select(:column1) > DB[:table2].select(:column2) (jeremyevans)
@@ -60,6 +60,12 @@ def execute_insert(sql, opts={})
private
+ # Use setNull for nil arguments as the default behavior of setString
+ # with nil doesn't appear to work correctly on PostgreSQL.
+ def set_ps_arg(cps, arg, i)
+ arg.nil? ? cps.setNull(i, JavaSQL::Types::NULL) : super
+ end
+
# Extend the adapter with the JDBC PostgreSQL AdapterMethods
def setup_connection(conn)
conn = super(conn)
@@ -95,6 +95,13 @@
@ds.order(:id).map(:number).should == [10, 20]
end
+ specify "should support bound variables with NULL values" do
+ @ds.delete
+ @ds.call(:insert, {:n=>nil}, :number=>@ds.ba(:$n))
+ @ds.count.should == 1
+ @ds.map(:number).should == [nil]
+ end
+
specify "should have insert return primary key value when using bound arguments" do
@ds.call(:insert, {:n=>20}, :number=>@ds.ba(:$n)).should == 2
@ds.filter(:id=>2).first[:number].should == 20
@@ -172,6 +179,14 @@
@ds.order(:id).map(:number).should == [10, 20]
end
+ specify "should support prepared statements with NULL values" do
+ @ds.delete
+ @ds.prepare(:insert, :insert_n, :number=>@ds.ba(:$n))
+ INTEGRATION_DB.call(:insert_n, :n=>nil)
+ @ds.count.should == 1
+ @ds.map(:number).should == [nil]
+ end
+
specify "should have insert return primary key value when using prepared statements" do
@ds.prepare(:insert, :insert_n, :number=>@ds.ba(:$n))
INTEGRATION_DB.call(:insert_n, :n=>20).should == 2

0 comments on commit 2e15e94

Please sign in to comment.