iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💎

ruby-mysql and ruby-mysql2

に公開
2023/04/13
1件

This is an article for Day 3 of the MySQL Advent Calendar and the Ruby Advent Calendar.

https://qiita.com/advent-calendar/2022/mysql
https://qiita.com/advent-calendar/2022/ruby


ruby-mysql

I've released ruby-mysql 4.0, though I doubt anyone will use it.

ruby-mysql | RubyGems.org | Community Gem hosting service

ruby-mysql is a MySQL client library written in Ruby.

Major Changes from 3.0

Class of Value Objects in Mysql#query Results

Previously, result values for normal queries (Mysql#query) that were not prepared statements (Mysql#prepare) were always returned as String. In the case of prepared statements, objects corresponding to the MySQL types were returned. In 4.0, normal queries now return results using the same types of objects as prepared statements. Additionally, the types for DECIMAL and DATE have been changed.

MySQL type Ruby class
NULL NilClass
INT Integer
DECIMAL BigDecimal (String up to 3.0)
FLOAT, DOUBLE Float
DATE Date (Time up to 3.0)
DATETIME, TIMESTAMP Time
TIME Float (in seconds)
YEAR Integer
CHAR, VARCHAR String
BIT String
TEXT, BLOB, JSON String

3.0:

pp my.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
#=> [["123", String],
#    ["123.45", String],
#    ["2022-11-15 00:17:11", String],
#    ["2022-11-15", String]]

4.0:

pp my.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
#=> [[123, Integer],
#    [0.12345e3, BigDecimal],
#    [2022-11-15 00:17:17 +0900, Time],
#    [#<Date: 2022-11-15 ((2459899j,0s,0n),+0s,2299161j)>, Date]]

To return values as String as before, specify cast: false in Mysql.new, Mysql.connect, Mysql#query, Mysql#each, or Mysql#fetch.

Mysql.new(cast: false).connect.query('select 123').fetch   #=> ["123"]
Mysql.new.connect(cast: false).query('select 123').fetch   #=> ["123"]
Mysql.connect(cast: false).query('select 123').fetch       #=> ["123"]
Mysql.connect.query('select 123', cast: false).fetch       #=> ["123"]
Mysql.connect.query('select 123').each(cast: false).first  #=> ["123"]
Mysql.connect.query('select 123').fetch(cast: false)       #=> ["123"]

Alternatively, changing Mysql.default_options will modify the behavior of Mysql objects created later in that process.

m1 = Mysql.connect
Mysql.default_options[:cast] = false
m2 = Mysql.connect
m1.query('select 123').fetch  #=> [123]
m2.query('select 123').fetch  #=> ["123"]

Mysql::Result#each Repeats from the Beginning Every Time

Up to 3.0, each returned results starting from the previous position, but in 4.0, it repeats from the beginning.

3.0:

res = my.query('select 1 union select 2 union select 3')
res.entries  #=> [["1"], ["2"], ["3"]]
res.entries  #=> []

res = my.query('select 1 union select 2 union select 3')
res.each.first  #=> ["1"]
res.each.first  #=> ["2"]
res.each.first  #=> ["3"]
res.each.first  #=> nil

4.0:

res = my.query('select 1 union select 2 union select 3')
res.entries  #=> [[1], [2], [3]]
res.entries  #=> [[1], [2], [3]]

res = my.query('select 1 union select 2 union select 3')
res.each.first  #=> [1]
res.each.first  #=> [1]
res.each.first  #=> [1]

I don't think you'd ever want the same behavior as 3.0, but if you do, you can do it like this:

require 'mysql'
class Mysql::Result
  def each(**opts)
    while (r = fetch(**opts))
      yield r
    end
  end
end
my = Mysql.connect
res = my.query('select 1 union select 2 union select 3')
pp res.entries  #=> [[1], [2], [3]]
pp res.entries  #=> []

Miscellaneous

RSpec

I had been writing test code with test-unit, but I've switched to RSpec because I'm more familiar with it.

Migration from GitHub to GitLab

I migrated from https://github.com/tmtm/ruby-mysql to https://gitlab.com/tmtms/ruby-mysql. Just because.

ruby-mysql2

mysql2 uses the libmysqlclient C library, but I thought it would be interesting to use ruby-mysql instead, so I created ruby-mysql2 based on mysql2.

https://gitlab.com/tmtms/ruby-mysql2

mysql2's test code is very comprehensive, and it was helpful for the development of ruby-mysql as well.

Since the test cases mostly passed, I think it can be used as a regular replacement for mysql2.

The incompatibilities with mysql2 are roughly as follows:

  • It does not read my.cnf, etc.
  • It does not support authentication methods other than caching_sha2_password, mysql_native_password, and sha256_password.
  • Mysql2::EM is not available.

I tried using it with ActiveRecord, but mysql2_adapter.rb had gem "mysql2" written in it, so it didn't work.

It worked normally with Sequel.

Well, but there's no particular reason to use ruby-mysql2. You should just use mysql2 anyway. It's much slower compared to mysql2.


Once again, I've made something that won't be useful to anyone...

Discussion

shuichishuichi

ruby-mysql, Sequelのアダプタとしてかなりお世話になってます!
最近だと、AWS Lambdaで依存ライブラリごとzipパッケージアップロードする方法をとるときに、pure rubyなのでとても手軽に同梱できて、助かってます♪

2