Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/active_record/type/oracle_enhanced/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,31 @@ def type
:raw
end

def deserialize(value)
value.is_a?(HEXData) ? value.raw_binary_string : super
end

def serialize(value)
# Encode a string or byte array as string of hex codes
if value.nil?
super
else
value = value.unpack("C*")
value.map { |x| "%02X" % x }.join
HEXData.from_binary_string(value)
end
end

class HEXData < ::String
def self.from_binary_string(str)
new(str.unpack1("H*"))
end

def raw_binary_string
(0..length - 2).step(2).reduce(::String.new(capacity: length / 2, encoding: Encoding::BINARY)) do |data, i|
data << self[i, 2].hex
end
end

OCI8::BindType::Mapping[self] = OCI8::BindType::String
end
end
end
Expand Down
38 changes: 36 additions & 2 deletions spec/active_record/oracle_enhanced/type/raw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ::TestEmployee < ActiveRecord::Base
last_name: "Last",
binary_data: @binary_data
)
expect(@employee.binary_data).to eq(@binary_data)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end
Expand All @@ -51,6 +52,7 @@ class ::TestEmployee < ActiveRecord::Base
expect(@employee.binary_data).to be_nil
@employee.binary_data = @binary_data
@employee.save!
expect(@employee.binary_data).to eq(@binary_data)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end
Expand All @@ -77,6 +79,7 @@ class ::TestEmployee < ActiveRecord::Base
@employee.reload
@employee.binary_data = @binary_data2
@employee.save!
expect(@employee.binary_data).to eq(@binary_data2)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data2)
end
Expand Down Expand Up @@ -116,13 +119,14 @@ class ::TestEmployee < ActiveRecord::Base
@employee.reload
@employee.binary_data = @binary_data
@employee.save!
expect(@employee.binary_data).to eq(@binary_data)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end

it "should allow equality on select" do
TestEmployee.delete_all
TestEmployee.create!(
employee = TestEmployee.create!(
first_name: "First",
last_name: "Last",
binary_data: @binary_data,
Expand All @@ -132,6 +136,36 @@ class ::TestEmployee < ActiveRecord::Base
last_name: "Last1",
binary_data: @binary_data2,
)
expect(TestEmployee.where(binary_data: @binary_data)).to have_attributes(count: 1)
expect(TestEmployee.where(binary_data: @binary_data).to_a).to eq([employee])
end

it "should allow equality on select with NULL value" do
TestEmployee.delete_all
employee = TestEmployee.create!(
first_name: "First",
last_name: "Last",
)
TestEmployee.create!(
first_name: "First1",
last_name: "Last1",
binary_data: @binary_data2,
)
expect(TestEmployee.where(binary_data: nil).to_a).to eq([employee])
end

it "should report changed when changed in place" do
employee = TestEmployee.create!(
first_name: "First",
last_name: "Last",
binary_data: @binary_data,
)
expect(employee.changed?).to be_falsey

employee.binary_data << "a"
expect(employee.changed?).to be(true)
expect(employee.changes).to eq({ "binary_data" => [@binary_data, @binary_data + "a"] })

employee.reload.binary_data << "b"
expect(employee.changes).to eq({ "binary_data" => [@binary_data, @binary_data + "b"] })
end
end