crystal-lang / crystal-mysql

MySQL connector for Crystal
MIT License
107 stars 36 forks source link

Unprepared statements reading strings is broken #36

Closed benoist closed 7 years ago

benoist commented 7 years ago

I was trying to reduce the amount of prepared statements in order to work around this issue https://github.com/crystal-lang/crystal-db/issues/60

I ran into bugs with using unprepared statements. I added the following spec and it fails. If you change db.unprepared.query to db.query it uses a prepared statement and that works fine.

  it "allows unprepared statement queries" do |db|
    db.exec %(create table if not exists a (i int not null, str text not null);)
    db.exec %(insert into a (i, str) values (23, "bai bai");)

    2.times do |i|
      DB.open db.uri do |db|
        begin
          db.unprepared.query("SELECT i, str FROM a WHERE i = 23") do |rs|
            rs.each do
              rs.read(Int32).should eq 23
              rs.read(String).should eq "bai bai"
            end
          end
        rescue e
          fail("Expected no exception, but got \"#{e.message}\"")
        end
      end
    end
  end
Failures:

  1) as a db allows unprepared statement queries
     Failure/Error: fail("Expected no exception, but got \"#{e.message}\"")

       Expected no exception, but got "Expected: "bai bai"
            got: "\u0007b""

     # spec/db_spec.cr:194