Work around regression in jdbc-sqlite3 3.8.7 where empty blobs are returned as nil
authorJeremy Evans <code@jeremyevans.net>
Mon, 24 Nov 2014 20:07:24 +0000 (12:07 -0800)
committerJeremy Evans <code@jeremyevans.net>
Mon, 24 Nov 2014 20:12:57 +0000 (12:12 -0800)
getBytes returns nil instead of an 0 length byte[] instance in
jdbc-sqlite3 3.8.7.  Add a wasNull check to catch this an return
an empty blob in that case, idea from Karol Bucek.

Details:
https://github.com/jruby/activerecord-jdbc-adapter/issues/605
https://bitbucket.org/xerial/sqlite-jdbc/issue/155

CHANGELOG
lib/sequel/adapters/jdbc/sqlite.rb

index 748a172056539bce696dc97f858bf0c390babe20..4be9f5cf4a70b01d88dde53679b36fa709bbf142 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,7 @@
 === HEAD
 
+* Work around regression in jdbc-sqlite3 3.8.7 where empty blobs are returned as nil (jeremyevans)
+
 * Work around regression in jdbc-sqlite3 3.8.7 when using JDBC getDate method for date parsing (jeremyevans)
 
 * Make Model#update_or_create return object if existing object exists but updates are not necessary (contentfree) (#916)
index 3e9364f886aefdd8657593b9319bb50eb82ce3cc..d820a97c1a0f36964bba5d1724f0c5936cdefd76 100644 (file)
@@ -71,6 +71,7 @@ module Sequel
         end
 
         # Use getLong instead of getInt for converting integers on SQLite, since SQLite does not enforce a limit of 2**32.
+        # Work around regressions in jdbc-sqlite 3.8.7 for date and blob types.
         def setup_type_convertor_map
           super
           @type_convertor_map[Java::JavaSQL::Types::INTEGER] = @type_convertor_map[Java::JavaSQL::Types::BIGINT]
@@ -80,6 +81,13 @@ module Sequel
               Sequel.string_to_date(v)
             end
           end
+          @type_convertor_map[Java::JavaSQL::Types::BLOB] = lambda do |r, i|
+            if v = r.getBytes(i)
+              Sequel::SQL::Blob.new(String.from_java_bytes(v))
+            elsif !r.wasNull
+              Sequel::SQL::Blob.new('')
+            end
+          end
         end
       end
     end