@connection.more_results &&@connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped

rows

end

# Clears the prepared statements cache.

defclear_cache!

@statements.clear

end

if"<3".respond_to?(:encode)

# Taken from here:

# https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb

# Author: TOMITA Masahiro <tommy@tmtm.org>

ENCODINGS= {

"armscii8" => nil,

"ascii" => Encoding::US_ASCII,

"big5" => Encoding::Big5,

"binary" => Encoding::ASCII_8BIT,

"cp1250" => Encoding::Windows_1250,

"cp1251" => Encoding::Windows_1251,

"cp1256" => Encoding::Windows_1256,

"cp1257" => Encoding::Windows_1257,

"cp850" => Encoding::CP850,

"cp852" => Encoding::CP852,

"cp866" => Encoding::IBM866,

"cp932" => Encoding::Windows_31J,

"dec8" => nil,

"eucjpms" => Encoding::EucJP_ms,

"euckr" => Encoding::EUC_KR,

"gb2312" => Encoding::EUC_CN,

"gbk" => Encoding::GBK,

"geostd8" => nil,

"greek" => Encoding::ISO_8859_7,

"hebrew" => Encoding::ISO_8859_8,

"hp8" => nil,

"keybcs2" => nil,

"koi8r" => Encoding::KOI8_R,

"koi8u" => Encoding::KOI8_U,

"latin1" => Encoding::ISO_8859_1,

"latin2" => Encoding::ISO_8859_2,

"latin5" => Encoding::ISO_8859_9,

"latin7" => Encoding::ISO_8859_13,

"macce" => Encoding::MacCentEuro,

"macroman" => Encoding::MacRoman,

"sjis" => Encoding::SHIFT_JIS,

"swe7" => nil,

"tis620" => Encoding::TIS_620,

"ucs2" => Encoding::UTF_16BE,

"ujis" => Encoding::EucJP_ms,

"utf8" => Encoding::UTF_8,

"utf8mb4" => Encoding::UTF_8,

}

else

ENCODINGS=Hash.new { |h,k| h[k] = k }

end

# Get the client encoding for this database

defclient_encoding

return@client_encodingif@client_encoding

result = exec_query(

"SHOW VARIABLES WHERE Variable_name = 'character_set_client'",

'SCHEMA')

@client_encoding=ENCODINGS[result.rows.last.last]

end

defexec_query(sql, name='SQL', binds= [])

log(sql, name, binds) do

exec_stmt(sql, name, binds) do |cols, stmt|

ActiveRecord::Result.new(cols, stmt.to_a) if cols

end

end

end

deflast_inserted_id(result)

@connection.insert_id

end

defexec_without_stmt(sql, name='SQL') # :nodoc:

# Some queries, like SHOW CREATE TABLE don't work through the prepared

# statement API. For those queries, we need to use this method. :'(

log(sql, name) do

result =@connection.query(sql)

cols = []

rows = []

if result

cols = result.fetch_fields.map { |field| field.name }

rows = result.to_a

result.free

end

ActiveRecord::Result.new(cols, rows)

end

end

# Executes an SQL query and returns a MySQL::Result object. Note that you have to free

# the Result object after you're done using it.

defexecute(sql, name=nil) #:nodoc:

if name ==:skip_logging

@connection.query(sql)

else

log(sql, name) { @connection.query(sql) }

end

rescueActiveRecord::StatementInvalid => exception

if exception.message.split(":").first =~/Packets out of order/

raiseActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."