Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BINARY and VARBINARY columns can only be read as string #58

Open
crash2burn opened this issue Jun 11, 2018 · 0 comments
Open

BINARY and VARBINARY columns can only be read as string #58

crash2burn opened this issue Jun 11, 2018 · 0 comments

Comments

@crash2burn
Copy link

Currently we are able to insert Bytes but when reading must specify read(String) not read(Bytes) as there will be an error otherwise. I believe it might be better to support reading a slice for these fields.

DB.open "mysql://root@localhost/test" do |db|
  db.exec "drop table if exists contacts"
  db.exec "create table contacts (name varchar(30), age int, btest VARBINARY(16))"
  db.exec "insert into contacts values (?, ?, ?)", "John Doe", 30, Bytes.new(3, 90_u8)

  args = [] of DB::Any
  args << "Sarah"
  args << 33
  args << Bytes.new(3, 90_u8)
  db.exec "insert into contacts values (?, ?, ?)", args

  puts "max age:"
  puts db.scalar "select max(age) from contacts" # => 33

  puts "contacts:"
  db.query "select name, age, btest from contacts order by age desc" do |rs|
    puts "#{rs.column_name(0)} (#{rs.column_name(1)}) (#{rs.column_name(2)})"
    # => name (age)
    rs.each do
      puts "#{rs.read(String)} (#{rs.read(Int32)}) (#{rs.read(Bytes)})"
      # => Sarah (33) (ZZZ)
      # => John Doe (30) (ZZZ)
    end
  end
end

max age:
33
contacts:
name (age) (btest)
MySql::ResultSet#read returned a String. A Slice(UInt8) was expected. (Exception)
from lib/db/src/db/result_set.cr:0:9 in 'read'
from src/dbtest.cr:28:57 in '__crystal_main'
from /usr/local/Cellar/crystal-lang/0.24.2_1/src/crystal/main.cr:11:3 in '_crystal_main'
from /usr/local/Cellar/crystal-lang/0.24.2_1/src/crystal/main.cr:112:5 in 'main_user_code'
from /usr/local/Cellar/crystal-lang/0.24.2_1/src/crystal/main.cr:101:7 in 'main'
from /usr/local/Cellar/crystal-lang/0.24.2_1/src/crystal/main.cr:135:3 in 'main'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant