Permalink
Browse files

Minor cleanups for db2 blob commit

Use fewer String#<< calls in literal_blob_append.

When use_clob_as_blob is true, don't cast to blob.

Don't use let in the specs.

Remove jdbc/db2 guard for blob escaping.
  • Loading branch information...
jeremyevans committed Dec 18, 2012
1 parent f586c8a commit cd445948c72a427357a99912546e2f63762e481f
Showing with 19 additions and 14 deletions.
  1. +2 −0 CHANGELOG
  2. +7 −4 lib/sequel/adapters/shared/db2.rb
  3. +9 −9 spec/adapters/db2_spec.rb
  4. +1 −1 spec/integration/dataset_test.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Cast blobs correctly on DB2 when use_clob_as_blob is false (mluu, jeremyevans) (#594)
+
* Add date_arithmetic extension for database-independent date calculations (jeremyevans)
* Make Database#schema handle [host.]database.schema.table qualified tables on Microsoft SQL Server (jeremyevans)
@@ -204,7 +204,6 @@ def uses_clob_for_text?
module DatasetMethods
include EmulateOffsetWithRowNumber
- APOS = Dataset::APOS
PAREN_CLOSE = Dataset::PAREN_CLOSE
PAREN_OPEN = Dataset::PAREN_OPEN
BITWISE_METHOD_MAP = {:& =>:BITAND, :| => :BITOR, :^ => :BITXOR, :'B~'=>:BITNOT}
@@ -217,9 +216,9 @@ module DatasetMethods
FETCH_FIRST = " FETCH FIRST ".freeze
ROWS_ONLY = " ROWS ONLY".freeze
EMPTY_FROM_TABLE = ' FROM "SYSIBM"."SYSDUMMY1"'.freeze
- HEX_START = 'X'.freeze
HSTAR = "H*".freeze
- CAST_BLOB_OPEN = "BLOB(".freeze
+ BLOB_OPEN = "BLOB(X'".freeze
+ BLOB_CLOSE = "')".freeze
# DB2 casts strings using RTRIM and CHAR instead of VARCHAR.
def cast_sql_append(sql, expr, type)
@@ -322,7 +321,11 @@ def literal_true
# DB2 uses a literal hexidecimal number for blob strings
def literal_blob_append(sql, v)
- sql << CAST_BLOB_OPEN << HEX_START << APOS << v.unpack(HSTAR).first << APOS << PAREN_CLOSE
+ if ::Sequel::DB2.use_clob_as_blob
+ super
+ else
+ sql << BLOB_OPEN << v.unpack(HSTAR).first << BLOB_CLOSE
+ end
end
# Add a fallback table for empty from situation
View
@@ -39,32 +39,32 @@
describe "Simple Dataset operations" do
before(:all) do
+ Sequel::DB2.use_clob_as_blob = false
DB2_DB.create_table!(:items) do
Integer :id, :primary_key => true
Integer :number
column :bin_string, 'varchar(20) for bit data'
column :bin_blob, 'blob'
end
+ @ds = DB2_DB[:items]
end
- let(:ds) { DB2_DB[:items] }
-
after(:each) do
- ds.delete
+ @ds.delete
end
-
after(:all) do
+ Sequel::DB2.use_clob_as_blob = true
DB2_DB.drop_table(:items)
end
specify "should insert with a primary key specified" do
- ds.insert(:id => 1, :number => 10)
- ds.insert(:id => 100, :number => 20)
- ds.select_hash(:id, :number).should == {1 => 10, 100 => 20}
+ @ds.insert(:id => 1, :number => 10)
+ @ds.insert(:id => 100, :number => 20)
+ @ds.select_hash(:id, :number).should == {1 => 10, 100 => 20}
end
specify "should insert into binary columns" do
- ds.insert(:id => 1, :bin_string => Sequel.blob("\1"), :bin_blob => Sequel.blob("\2"))
- ds.select(:bin_string, :bin_blob).first.should == {:bin_string => "\1", :bin_blob => "\2"}
+ @ds.insert(:id => 1, :bin_string => Sequel.blob("\1"), :bin_blob => Sequel.blob("\2"))
+ @ds.select(:bin_string, :bin_blob).first.should == {:bin_string => "\1", :bin_blob => "\2"}
end
end
@@ -309,7 +309,7 @@
end
end
- cspecify "should properly escape binary data", [:odbc], [:jdbc, :hsqldb], [:jdbc, :db2], :oracle do
+ cspecify "should properly escape binary data", [:odbc], [:jdbc, :hsqldb], :oracle do
INTEGRATION_DB.get(Sequel.cast(Sequel.blob("\1\2\3"), File).as(:a)).should == "\1\2\3"
end

0 comments on commit cd44594

Please sign in to comment.