Restore DBD::MySQL::Database#quote method

For whatever reason, Ruby DBI and DBD have axed the quote method from their midst, breaking compatibility with previous versions. Sadly I couldn’t find any clues as to why it was removed (actually, bluntly commented out).

Fortunately I was able to unearth some hints how to restore this method and with an added fix to NilClass, the result seems to be working fine:

# For some unknown reason quote method is commented out at:
#   https://github.com/raphaelcosta/rails-dbd-mysql/blob/master/lib/dbd/mysql/database.rb
# 
# Restoring it for b/w compliance
module DBI::DBD::Mysql
  class Database < DBI::BaseDatabase
    def quote(value)
        case value
        when String
          "'#{@handle.quote(value)}'"
        when DBI::Binary
          "'#{@handle.quote(value.to_s)}'"
        when TrueClass
          "'1'"
        when FalseClass
          "'0'"
        when Array
          value.collect { |v| quote(v) }.join(", ")
        when DBI::Date, DBI::Time, DBI::Timestamp, ::Date
          "'#{value.to_s}'"
        when ::Time
          "'#{value.rfc2822}'"
        when NilClass
          "NULL"
        else
          value.to_s
        end
    end
  end
end

Just place this in some file and require it in your code.

 

Be Sociable, Share!

No related posts.

Leave a Comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>