Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ruby-mysql

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ruby-mysql

  • 4.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

ruby-mysql

Description

ruby-mysql is a MySQL client library. It is written entirely in Ruby. Therefore libmysqlclient is not required and no compilation is required during installation.

Installation

gem install ruby-mysql

Synopsis

require 'mysql'

my = Mysql.connect('mysql://username:password@hostname:port/dbname?charset=utf8mb4')
my.query("select col1, col2 from tblname").each do |col1, col2|
  p col1, col2
end
stmt = my.prepare('insert into tblname (col1,col2) values (?,?)')
stmt.execute 123, 'abc'

Major incompatibility with 3.0

Result values are now converted by default

MySQL typeRuby class
NULLNilClass
INTInteger
DECIMALBigDecimal
FLOAT, DOUBLEFloat
DATEDate
DATETIME, TIMESTAMPTime
TIMEFloat (as seconds)
YEARInteger
CHAR, VARCHARString
BITString
TEXT, BLOB, JSONString

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 specify cast: false, you get the same behavior as in 3.0.

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

It can also be specified during Mysql.new and Mysql.connect.

my = Mysql.connect('mysql://user:pass@localhost/', cast: false)

Changing mysql.default_options will affect the behavior of subsequently created instances.

my1 = Mysql.connect('mysql://user:pass@localhost/')
Mysql.default_options[:cast] = false
my2 = Mysql.connect('mysql://user:pass@localhost/')
pp my1.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
#=> [[123, Integer],
#    [0.12345e3, BigDecimal],
#    [2022-11-15 00:26:09 +0900, Time],
#    [#<Date: 2022-11-15 ((2459899j,0s,0n),+0s,2299161j)>, Date]]
pp my2.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
#=> [["123", String],
#    ["123.45", String],
#    ["2022-11-15 00:26:09", String],
#    ["2022-11-15", String]]

Mysql::Result#each now always return records from the beginning

3.0:

res = my.query('select 123 union select 456')
res.entries
#=> [["123"], ["456"]]
res.entries
#=> []

4.0:

res = my.query('select 123 union select 456')
res.entries
#=> [[123], [456]]
res.entries
#=> [[123], [456]]
  • Author: TOMITA Masahiro tommy@tmtm.org
  • Copyright: Copyright 2008 TOMITA Masahiro
  • License: MIT

FAQs

Package last updated on 08 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc