Skip to content

Commit

Permalink
added a method that converts to slice when bytes are read crystal-lan…
Browse files Browse the repository at this point in the history
  • Loading branch information
crash2burn committed Jun 12, 2018
1 parent 4fb35a7 commit ab38f46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/db_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,21 @@ DB::DriverSpecs(MySql::Any).run do
end
end
end

it "allows VARBINARY fields to be read as slices" do |db|
db.exec %(create table if not exists btest (b varbinary(16) not null);)
db.exec %(insert into btest (b) values (X'5a5a5a');)

DB.open db.uri do |db|
begin
db.query("select b from btest") do |rs|
rs.each do
rs.read(Bytes).should eq Bytes.new(3, 90_u8)
end
end
rescue e
fail("Expected no exception, but got \"#{e.message}\"")
end
end
end
end
12 changes: 12 additions & 0 deletions src/mysql/result_set.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ class MySql::ResultSet < DB::ResultSet
end
end

# In order not to break existing functionality expecting strings, we only
# apply the binary check if bytes are requested in the read method call
def read(t : Bytes.class)
val = read

if val.is_a?(String) && @columns[@column_index - 1].character_set == 63
val.to_slice
else
val
end
end

def read(t : Bool.class)
MySql::Type.from_mysql(read.as(Int8))
end
Expand Down

0 comments on commit ab38f46

Please sign in to comment.