Giter Site home page Giter Site logo

brianmario / mysql2 Goto Github PK

View Code? Open in Web Editor NEW
2.2K 72.0 550.0 2.03 MB

A modern, simple and very fast Mysql library for Ruby - binding to libmysql

Home Page: http://github.com/brianmario/mysql2

License: MIT License

Shell 2.81% Ruby 50.75% C 46.43%

mysql2's Introduction

Mysql2 - A modern, simple and very fast MySQL library for Ruby - binding to libmysql

GitHub Actions GitHub Actions Status: Build GitHub Actions Status: Container Travis CI Travis CI Status Appveyor CI Appveyor CI Status

The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results. Some database libraries out there serve as direct 1:1 mappings of the already complex C APIs available. This one is not.

It also forces the use of UTF-8 [or binary] for the connection and uses encoding-aware MySQL API calls where it can.

The API consists of three classes:

Mysql2::Client - your connection to the database.

Mysql2::Result - returned from issuing a #query on the connection. It includes Enumerable.

Mysql2::Statement - returned from issuing a #prepare on the connection. Execute the statement to get a Result.

Installing

General Instructions

gem install mysql2

This gem links against MySQL's libmysqlclient library or Connector/C library, and compatible alternatives such as MariaDB. You may need to install a package such as libmariadb-dev, libmysqlclient-dev, mysql-devel, or other appropriate package for your system. See below for system-specific instructions.

By default, the mysql2 gem will try to find a copy of MySQL in this order:

  • Option --with-mysql-dir, if provided (see below).
  • Option --with-mysql-config, if provided (see below).
  • Several typical paths for mysql_config (default for the majority of users).
  • The directory /usr/local.

Configuration options

Use these options by gem install mysql2 -- [--optionA] [--optionB=argument].

  • --with-mysql-dir[=/path/to/mysqldir] - Specify the directory where MySQL is installed. The mysql2 gem will not use mysql_config, but will instead look at mysqldir/lib and mysqldir/include for the library and header files. This option is mutually exclusive with --with-mysql-config.

  • --with-mysql-config[=/path/to/mysql_config] - Specify a path to the mysql_config binary provided by your copy of MySQL. The mysql2 gem will ask this mysql_config binary about the compiler and linker arguments needed. This option is mutually exclusive with --with-mysql-dir.

  • --with-mysql-rpath=/path/to/mysql/lib / --without-mysql-rpath - Override the runtime path used to find the MySQL libraries. This may be needed if you deploy to a system where these libraries are located somewhere different than on your build system. This overrides any rpath calculated by default or by the options above.

  • --with-openssl-dir[=/path/to/openssl] - Specify the directory where OpenSSL is installed. In most cases, the Ruby runtime and MySQL client libraries will link against a system-installed OpenSSL library and this option is not needed. Use this option when non-default library paths are needed.

  • --with-sanitize[=address,cfi,integer,memory,thread,undefined] - Enable sanitizers for Clang / GCC. If no argument is given, try to enable all sanitizers or fail if none are available. If a command-separated list of specific sanitizers is given, configure will fail unless they all are available. Note that the some sanitizers may incur a performance penalty, and the Address Sanitizer may require a runtime library. To see line numbers in backtraces, declare these environment variables (adjust the llvm-symbolizer path as needed for your system):

  export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer-3.4
  export ASAN_OPTIONS=symbolize=1

Linux and other Unixes

You may need to install a package such as libmariadb-dev, libmysqlclient-dev, mysql-devel, or default-libmysqlclient-dev; refer to your distribution's package guide to find the particular package. The most common issue we see is a user who has the library file libmysqlclient.so but is missing the header file mysql.h -- double check that you have the -dev packages installed.

Mac OS X

You may use Homebrew, MacPorts, or a native MySQL installer package. The most common paths will be automatically searched. If you want to select a specific MySQL directory, use the --with-mysql-dir or --with-mysql-config options above.

If you have not done so already, you will need to install the XCode select tools by running xcode-select --install.

Later versions of MacOS no longer distribute a linkable OpenSSL library. It is common to use Homebrew or MacPorts to install OpenSSL. Make sure that both the Ruby runtime and MySQL client libraries are compiled with the same OpenSSL family, 1.0 or 1.1 or 3.0, since only one can be loaded at runtime.

$ brew install [email protected]
$ gem install mysql2 -- --with-openssl-dir=$(brew --prefix [email protected])

or

$ sudo port install openssl11

Since most Ruby projects use Bundler, you can set build options in the Bundler config rather than manually installing a global mysql2 gem. This example shows how to set build arguments with Bundler config:

$ bundle config --local build.mysql2 -- --with-openssl-dir=$(brew --prefix [email protected])

Another helpful trick is to use the same OpenSSL library that your Ruby was built with, if it was built with an alternate OpenSSL path. This example finds the argument --with-openssl-dir=/some/path from the Ruby build and adds that to the Bundler config:

$ bundle config --local build.mysql2 -- $(ruby -r rbconfig -e 'puts RbConfig::CONFIG["configure_args"]' | xargs -n1 | grep with-openssl-dir)

Note the additional double dashes (--) these separate command-line arguments that gem or bundler interpret from the additional arguments that are passed to the mysql2 build process.

Windows

Make sure that you have Ruby and the DevKit compilers installed. We recommend the Ruby Installer distribution.

By default, the mysql2 gem will download and use MySQL Connector/C from mysql.com. If you prefer to use a local installation of Connector/C, add the flag --with-mysql-dir=c:/mysql-connector-c-x-y-z (this path may use forward slashes).

By default, the libmysql.dll library will be copied into the mysql2 gem directory. To prevent this, add the flag --no-vendor-libmysql. The mysql2 gem will search for libmysql.dll in the following paths, in order:

  • Environment variable RUBY_MYSQL2_LIBMYSQL_DLL=C:\path\to\libmysql.dll (note the Windows-style backslashes).
  • In the mysql2 gem's own directory vendor/libmysql.dll
  • In the system's default library search paths.

Usage

Connect to a database:

# this takes a hash of options, almost all of which map directly
# to the familiar database.yml in rails
# See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Mysql2Adapter.html
client = Mysql2::Client.new(:host => "localhost", :username => "root")

Then query it:

results = client.query("SELECT * FROM users WHERE group='githubbers'")

Need to escape something first?

escaped = client.escape("gi'thu\"bbe\0r's")
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")

You can get a count of your results with results.count.

Finally, iterate over the results:

results.each do |row|
  # conveniently, row is a hash
  # the keys are the fields, as you'd expect
  # the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
  puts row["id"] # row["id"].is_a? Integer
  if row["dne"]  # non-existent hash entry is nil
    puts row["dne"]
  end
end

Or, you might just keep it simple:

client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
  # do something with row, it's ready to rock
end

How about with symbolized keys?

client.query("SELECT * FROM users WHERE group='githubbers'", :symbolize_keys => true).each do |row|
  # do something with row, it's ready to rock
end

You can get the headers, columns, and the field types in the order that they were returned by the query like this:

headers = results.fields # <= that's an array of field names, in order
types = results.field_types # <= that's an array of field types, in order
results.each(:as => :array) do |row|
  # Each row is an array, ordered the same as the query results
  # An otter's den is called a "holt" or "couch"
end

Prepared statements are supported, as well. In a prepared statement, use a ? in place of each value and then execute the statement to retrieve a result set. Pass your arguments to the execute method in the same number and order as the question marks in the statement. Query options can be passed as keyword arguments to the execute method.

Be sure to read about the known limitations of prepared statements at https://dev.mysql.com/doc/refman/5.6/en/c-api-prepared-statement-problems.html

statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
result1 = statement.execute(1)
result2 = statement.execute(2)

statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
result = statement.execute(1, "CA")

statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
result = statement.execute(1, "CA", :as => :array)

Session Tracking information can be accessed with

c = Mysql2::Client.new(
  host: "127.0.0.1",
  username: "root",
  flags: "SESSION_TRACK",
  init_command: "SET @@SESSION.session_track_schema=ON"
)
c.query("INSERT INTO test VALUES (1)")
session_track_type = Mysql2::Client::SESSION_TRACK_SCHEMA
session_track_data = c.session_track(session_track_type)

The types of session track types can be found at https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html

Connection options

You may set the following connection options in Mysql2::Client.new(...):

Mysql2::Client.new(
  :host,
  :username,
  :password,
  :port,
  :database,
  :socket = '/path/to/mysql.sock',
  :flags = REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION | MULTI_STATEMENTS,
  :encoding = 'utf8',
  :read_timeout = seconds,
  :write_timeout = seconds,
  :connect_timeout = seconds,
  :connect_attrs = {:program_name => $PROGRAM_NAME, ...},
  :reconnect = true/false,
  :local_infile = true/false,
  :secure_auth = true/false,
  :get_server_public_key = true/false,
  :default_file = '/path/to/my.cfg',
  :default_group = 'my.cfg section',
  :default_auth = 'authentication_windows_client'
  :init_command => sql
  )

Connecting to MySQL on localhost and elsewhere

The underlying MySQL client library uses the :host parameter to determine the type of connection to make, with special interpretation you should be aware of:

  • An empty value or "localhost" will attempt a local connection:
    • On Unix, connect to the default local socket path. (To set a custom socket path, use the :socket parameter).
    • On Windows, connect using a shared-memory connection, if enabled, or TCP.
  • A value of "." on Windows specifies a named-pipe connection.
  • An IPv4 or IPv6 address will result in a TCP connection.
  • Any other value will be looked up as a hostname for a TCP connection.

SSL/TLS options

Setting any of the following options will enable an SSL/TLS connection, but only if your MySQL client library and server have been compiled with SSL support. MySQL client library defaults will be used for any parameters that are left out or set to nil. Relative paths are allowed, and may be required by managed hosting providers such as Heroku.

Mysql2::Client.new(
  # ...options as above...,
  :sslkey => '/path/to/client-key.pem',
  :sslcert => '/path/to/client-cert.pem',
  :sslca => '/path/to/ca-cert.pem',
  :sslcapath => '/path/to/cacerts',
  :sslcipher => 'DHE-RSA-AES256-SHA',
  :sslverify => true, # Removed in MySQL 8.0
  :ssl_mode => :disabled / :preferred / :required / :verify_ca / :verify_identity,
  )

For MySQL versions 5.7.11 and higher, use :ssl_mode to prefer or require an SSL connection and certificate validation. For earlier versions of MySQL, use the :sslverify boolean. For details on each of the :ssl_mode options, see https://dev.mysql.com/doc/refman/8.0/en/connection-options.html.

The :ssl_mode option will also set the appropriate MariaDB connection flags:

:ssl_mode MariaDB option value
:disabled MYSQL_OPT_SSL_ENFORCE = 0
:required MYSQL_OPT_SSL_ENFORCE = 1
:verify_identity MYSQL_OPT_SSL_VERIFY_SERVER_CERT = 1

MariaDB does not support the :preferred or :verify_ca options. For more information about SSL/TLS in MariaDB, see https://mariadb.com/kb/en/securing-connections-for-client-and-server/ and https://mariadb.com/kb/en/mysql_optionsv/#tls-options

Secure auth

Starting with MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this). When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format. The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password. To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new().

Flags option parsing

The :flags parameter accepts an integer, a string, or an array. The integer form allows the client to assemble flags from constants defined under Mysql2::Client such as Mysql2::Client::FOUND_ROWS. Use a bitwise | (OR) to specify several flags.

The string form will be split on whitespace and parsed as with the array form: Plain flags are added to the default flags, while flags prefixed with - (minus) are removed from the default flags.

Using Active Record's database.yml

Active Record typically reads its configuration from a file named database.yml or an environment variable DATABASE_URL. Use the value mysql2 as the adapter name. For example:

development:
  adapter: mysql2
  encoding: utf8
  database: my_db_name
  username: root
  password: my_password
  host: 127.0.0.1
  port: 3306
  flags:
    - -COMPRESS
    - FOUND_ROWS
    - MULTI_STATEMENTS
  secure_auth: false

In this example, the compression flag is negated with -COMPRESS.

Using Active Record's DATABASE_URL

Active Record typically reads its configuration from a file named database.yml or an environment variable DATABASE_URL. Use the value mysql2 as the protocol name. For example:

DATABASE_URL=mysql2://sql_user:sql_pass@sql_host_name:port/sql_db_name?option1=value1&option2=value2

Reading a MySQL config file

You may read configuration options from a MySQL configuration file by passing the :default_file and :default_group parameters. For example:

Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group => 'client')

Initial command on connect and reconnect

If you specify the :init_command option, the SQL string you provide will be executed after the connection is established. If :reconnect is set to true, init_command will also be executed after a successful reconnect. It is useful if you want to provide session options which survive reconnection.

Mysql2::Client.new(:init_command => "SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'")

Multiple result sets

You can also retrieve multiple result sets. For this to work you need to connect with flags Mysql2::Client::MULTI_STATEMENTS. Multiple result sets can be used with stored procedures that return more than one result set, and for bundling several SQL statements into a single call to client.query.

client = Mysql2::Client.new(:host => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS)
result = client.query('CALL sp_customer_list( 25, 10 )')
# result now contains the first result set
while client.next_result
  result = client.store_result
  # result now contains the next result set
end

Repeated calls to client.next_result will return true, false, or raise an exception if the respective query erred. When client.next_result returns true, call client.store_result to retrieve a result object. Exceptions are not raised until client.next_result is called to find the status of the respective query. Subsequent queries are not executed if an earlier query raised an exception. Subsequent calls to client.next_result will return false.

result = client.query('SELECT 1; SELECT 2; SELECT A; SELECT 3')
p result.first

while client.next_result
  result = client.store_result
  p result.first
end

Yields:

{"1"=>1}
{"2"=>2}
next_result: Unknown column 'A' in 'field list' (Mysql2::Error)

Cascading config

The default config hash is at:

Mysql2::Client.default_query_options

which defaults to:

{:async => false, :as => :hash, :symbolize_keys => false}

that can be used as so:

# these are the defaults all Mysql2::Client instances inherit
Mysql2::Client.default_query_options.merge!(:as => :array)

or

# this will change the defaults for all future results returned by the #query method _for this connection only_
c = Mysql2::Client.new
c.query_options.merge!(:symbolize_keys => true)

or

# this will set the options for the Mysql2::Result instance returned from the #query method
c = Mysql2::Client.new
c.query(sql, :symbolize_keys => true)

or

# this will set the options for the Mysql2::Result instance returned from the #execute method
c = Mysql2::Client.new
s = c.prepare(sql)
s.execute(arg1, args2, :symbolize_keys => true)

Result types

Array of Arrays

Pass the :as => :array option to any of the above methods of configuration

Array of Hashes

The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash

Timezones

Mysql2 now supports two timezone options:

:database_timezone # this is the timezone Mysql2 will assume fields are already stored as, and will use this when creating the initial Time objects in ruby
:application_timezone # this is the timezone Mysql2 will convert to before finally handing back to the caller

In other words, if :database_timezone is set to :utc - Mysql2 will create the Time objects using Time.utc(...) from the raw value libmysql hands over initially. Then, if :application_timezone is set to say - :local - Mysql2 will then convert the just-created UTC Time object to local time.

Both options only allow two values - :local or :utc - with the exception that :application_timezone can be [and defaults to] nil

Casting "boolean" columns

You can now tell Mysql2 to cast tinyint(1) fields to boolean values in Ruby with the :cast_booleans option.

client = Mysql2::Client.new
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)

Keep in mind that this works only with fields and not with computed values, e.g. this result will contain 1, not true:

client = Mysql2::Client.new
result = client.query("SELECT true", :cast_booleans => true)

CAST function wouldn't help here as there's no way to cast to TINYINT(1). Apparently the only way to solve this is to use a stored procedure with return type set to TINYINT(1).

Skipping casting

Mysql2 casting is fast, but not as fast as not casting data. In rare cases where typecasting is not needed, it will be faster to disable it by providing :cast => false. (Note that :cast => false overrides :cast_booleans => true.)

client = Mysql2::Client.new
result = client.query("SELECT * FROM table", :cast => false)

Here are the results from the query_without_mysql_casting.rb script in the benchmarks folder:

                           user     system      total        real
Mysql2 (cast: true)    0.340000   0.000000   0.340000 (  0.405018)
Mysql2 (cast: false)   0.160000   0.010000   0.170000 (  0.209937)
Mysql                  0.080000   0.000000   0.080000 (  0.129355)
do_mysql               0.520000   0.010000   0.530000 (  0.574619)

Although Mysql2 performs reasonably well at retrieving uncasted data, it (currently) is not as fast as the Mysql gem. In spite of this small disadvantage, Mysql2 still sports a friendlier interface and doesn't block the entire ruby process when querying.

Async

NOTE: Not supported on Windows.

Mysql2::Client takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for all queries. But, in order to take full advantage of it in your Ruby code, you can do:

client.query("SELECT sleep(5)", :async => true)

Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism like EventMachine or even IO.select. Once the socket becomes readable, you can do:

# result will be a Mysql2::Result instance
result = client.async_result

NOTE: Because of the way MySQL's query API works, this method will block until the result is ready. So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine. If you need multiple query concurrency take a look at using a connection pool.

Row Caching

By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily). This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.

If you only plan on using each row once, then it's much more efficient to disable this behavior by setting the :cache_rows option to false. This would be helpful if you wanted to iterate over the results in a streaming manner. Meaning the GC would cleanup rows you don't need anymore as you're iterating over the result set.

Streaming

Mysql2::Client can optionally only fetch rows from the server on demand by setting :stream => true. This is handy when handling very large result sets which might not fit in memory on the client.

result = client.query("SELECT * FROM really_big_Table", :stream => true)

There are a few things that need to be kept in mind while using streaming:

  • :cache_rows is ignored currently. (if you want to use :cache_rows you probably don't want to be using :stream)
  • You must fetch all rows in the result set of your query before you can make new queries. (i.e. with Mysql2::Result#each)

Read more about the consequences of using mysql_use_result (what streaming is implemented with) here: http://dev.mysql.com/doc/refman/5.0/en/mysql-use-result.html.

Lazy Everything

Well... almost ;)

Field name strings/symbols are shared across all the rows so only one object is ever created to represent the field name for an entire dataset.

Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each. For example, if you were to yield 4 rows from a 100 row dataset, only 4 hashes will be created. The rest will sit and wait in C-land until you want them (or when the GC goes to cleanup your Mysql2::Result instance). Now say you were to iterate over that same collection again, this time yielding 15 rows - the 4 previous rows that had already been turned into ruby hashes would be pulled from an internal cache, then 11 more would be created and stored in that cache. Once the entire dataset has been converted into ruby objects, Mysql2::Result will free the Mysql C result object as it's no longer needed.

This caching behavior can be disabled by setting the :cache_rows option to false.

As for field values themselves, I'm workin on it - but expect that soon.

Compatibility

This gem is tested with the following Ruby versions on Linux and Mac OS X:

  • Ruby MRI 2.0 through 2.7 (all versions to date)
  • Ruby MRI 3.0, 3.1, 3.2 (all versions to date)
  • Rubinius 2.x and 3.x do work but may fail under some workloads

This gem is tested with the following MySQL and MariaDB versions:

  • MySQL 5.5, 5.6, 5.7, 8.0
  • MySQL Connector/C 6.0, 6.1, 8.0 (primarily on Windows)
  • MariaDB 5.5, 10.x, with a focus on 10.6 LTS and 10.11 LTS
  • MariaDB Connector/C 2.x, 3.x

Ruby on Rails / Active Record

  • mysql2 0.5.x works with Rails / Active Record 4.2.11, 5.0.7, 5.1.6, and higher.
  • mysql2 0.4.x works with Rails / Active Record 4.2.5 - 5.0 and higher.
  • mysql2 0.3.x works with Rails / Active Record 3.1, 3.2, 4.x, 5.0.
  • mysql2 0.2.x works with Rails / Active Record 2.3 - 3.0.

Asynchronous Active Record

Please see the em-synchrony project for details about using EventMachine with mysql2 and Rails.

Sequel

Sequel includes a mysql2 adapter in all releases since 3.15 (2010-09-01). Use the prefix "mysql2://" in your connection specification.

EventMachine

The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine, while specifying callbacks for success for failure. Here's a simple example:

require 'mysql2/em'

EM.run do
  client1 = Mysql2::EM::Client.new
  defer1 = client1.query "SELECT sleep(3) as first_query"
  defer1.callback do |result|
    puts "Result: #{result.to_a.inspect}"
  end

  client2 = Mysql2::EM::Client.new
  defer2 = client2.query "SELECT sleep(1) second_query"
  defer2.callback do |result|
    puts "Result: #{result.to_a.inspect}"
  end
end

Benchmarks and Comparison

The mysql2 gem converts MySQL field types to Ruby data types in C code, providing a serious speed benefit.

The do_mysql gem also converts MySQL fields types, but has a considerably more complex API and is still ~2x slower than mysql2.

The mysql gem returns only nil or string data types, leaving you to convert field values to Ruby types in Ruby-land, which is much slower than mysql2's C code.

For a comparative benchmark, the script below performs a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type, then iterating over every row using an #each like method yielding a block:

         user       system     total       real
Mysql2   0.750000   0.180000   0.930000   (1.821655)
do_mysql 1.650000   0.200000   1.850000   (2.811357)
Mysql    7.500000   0.210000   7.710000   (8.065871)

These results are from the query_with_mysql_casting.rb script in the benchmarks folder.

Development

Use 'bundle install' to install the necessary development and testing gems:

bundle install
rake

The tests require the "test" database to exist, and expect to connect both as root and the running user, both with a blank password:

CREATE DATABASE test;
CREATE USER '<user>'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON test.* TO '<user>'@'localhost';

You can change these defaults in the spec/configuration.yml which is generated automatically when you run rake (or explicitly rake spec/configuration.yml).

For a normal installation on a Mac, you most likely do not need to do anything, though.

Special Thanks

  • Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
  • Yury Korolev - for TONS of help testing the Active Record adapter
  • Aaron Patterson - tons of contributions, suggestions and general badassness
  • Mike Perham - Async Active Record adapter (uses Fibers and EventMachine)
  • Aaron Stone - additional client settings, local files, microsecond time, maintenance support
  • Kouhei Ueno - for the original work on Prepared Statements way back in 2012
  • John Cant - polishing and updating Prepared Statements support
  • Justin Case - polishing and updating Prepared Statements support and getting it merged
  • Tamir Duberstein - for help with timeouts and all around updates and cleanups
  • Jun Aruga - for migrating CI tests to GitHub Actions and other improvements

mysql2's People

Contributors

ajaska avatar benmcredmond avatar brianmario avatar byroot avatar casperisfine avatar cbandy avatar dbussink avatar dj2 avatar dylanahsmith avatar felixbuenemann avatar jeremy avatar junaruga avatar justincase avatar kamipo avatar kronn avatar loe avatar luislavena avatar methodmissing avatar mishina2228 avatar mperham avatar nyaxt avatar radsaq avatar simi avatar sodabrew avatar tadd avatar tamird avatar tenderlove avatar timcharper avatar wok avatar yahonda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mysql2's Issues

symbol not found

$ which gem
/Users/ddonnell/.rvm/rubies/ruby-1.8.7-p302/bin/gem

$ ls /usr/local/
git             mysql               mysql-5.1.50-osx10.6-x86

$ gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.2.3
1 gem installed
Installing ri documentation for mysql2-0.2.3...

Enclosing class/module 'mMysql2' for class Client not known

Enclosing class/module 'mMysql2' for class Result not known
Installing RDoc documentation for mysql2-0.2.3...

Enclosing class/module 'mMysql2' for class Client not known

Enclosing class/module 'mMysql2' for class Result not known

$ rake db:migrate
rake aborted!
dlsym(0x102846160, Init_mysql2): symbol not found - /Users/ddonnell/.rvm/gems/ruby-1.8.7-p302/gems/mysql2-0.2.3/lib/mysql2/mysql2.bundle

(See full trace by running task with --trace)

AR adaptor: forces non-numeric attributes to be 0

I'm assuming you're planning for the mysql2 AR adaptor to be a drop in replacement for the standard mysql one? Let me know if that isn't the case and I'll stop filing these bugs.

In the standard mysql adaptor if I have a model with a decimal attribute and validates_numericality_of, trying to set that attribute to something silly like an array causes a validation error.

class Supplier < ActiveRecord::Base
  ...
  validates_numericality_of :margin, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100, :allow_blank => true
  ...
end

>> Supplier.new(:margin => []).valid?
=> false
>> Supplier.new(:margin => 0).valid?
=> true

With the mysql2 adaptor, using an array is accepted:

>> Supplier.new(:margin => []).valid?
=> true
>> Supplier.new(:margin => 0).valid?
=> true

select_rows incompatible with Rails API

mysql2 0.1.8 ActiveRecord ConnectionAdapter is incompatible with the Rails API specification (and thus can't be blindly used as a drop-in replacement for the mysql gem):

Check the current implementation of mysql2 - the select_rows returns array of hashes, the specification at http://api.rubyonrails.org/ (ActiveRecord::ConnectionAdapters::DatabaseStatements -> select_rows) requires it to return "an array of arrays containing the field values. Order is the same as that returned by columns."

(I have not verified the compatibility of the other methods, so there may be other issues left.)

Ruby 1.9.1 is about half the speed of 1.8.7 when selecting integers

If i do this:
I'm using Mysql
Benchmark.bmbm do|b|
b.report("SEQUEL / MYSQL2") do
1000.times do
DB3.fetch("SELECT video_id FROM stats_videos_daily WHERE banner_internal_views = :views LIMIT 1000", {:views => 26}).all
end
end
end

where video_id is an INT in the database (InnoDB) (utf8 / utf8_unicode_ci)

With ruby 1.8.7 => 1.40 sec
With ruby 1.9.1 => 2.80 sec

System:
Snow Leopard:
mysql Ver 14.14 Distrib 5.1.47, for apple-darwin10.3.0 (i386) using readline 5.1
Using:
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.4.0]
and
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

Installing does not work SL 10.6.4 rvm ruby 1.9.3 dev branch and rails 3.0.0 rc

While all other gems are installing properly I'm getting stuck with this error message when trying to install mysql2:

mkmf.rb:368:in try_do': The complier failed to generate an executable file. (RuntimeError) You have to install development tools first. from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:435:intry_link0'
from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:440:in try_link' from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:552:intry_func'
from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:797:in block in have_func' from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:693:inblock in checking_for'
from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:280:in block (2 levels) in postpone' from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:254:inopen'
from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:280:in block in postpone' from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:254:inopen'
from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:276:in postpone' from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:692:inchecking_for'
from /Users/spaquet/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/mkmf.rb:796:in have_func' from extconf.rb:9:in

'

Gem files will remain installed in /Users/spaquet/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/mysql2-0.1.9 for inspection.
Results logged to /Users/spaquet/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/mysql2-0.1.9/ext/mysql2/gem_make.out

Configuration: SL 10.6.4 rvm ruby 1.9.3 dev branch and rails 3.0.0 rc

"can't modify frozen object" error in 1.9.2

getting this issue from a very recent commit:

can't modify frozen object
/Users/james/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/mysql2-6142336/lib/active_record/connection_adapters/mysql2_adapter.rb:299:in query' /Users/james/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/mysql2-6142336/lib/active_record/connection_adapters/mysql2_adapter.rb:299:inexecute'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/mysql2-6142336/lib/active_record/connection_adapters/mysql2_adapter.rb:602:in configure_connection' /Users/james/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/mysql2-6142336/lib/active_record/connection_adapters/mysql2_adapter.rb:164:ininitialize'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/mysql2-6142336/lib/active_record/connection_adapters/mysql2_adapter.rb:11:in new' /Users/james/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/mysql2-6142336/lib/active_record/connection_adapters/mysql2_adapter.rb:11:inmysql2_connection'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in new_connection' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:incheckout_new_connection'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:in block (2 levels) in checkout' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:inloop'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in block in checkout' /Users/james/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/monitor.rb:201:inmon_synchronize'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:in checkout' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:inconnection'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:in retrieve_connection' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:inretrieve_connection'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in connection' /Users/james/projects/music-social/vendor/plugins/set_names_utf8/init.rb:3:inblock (2 levels) in evaluate_init_rb'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:54:in suppress' /Users/james/projects/music-social/vendor/plugins/set_names_utf8/init.rb:2:inblock in evaluate_init_rb'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin.rb:158:in eval' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin.rb:158:inblock in evaluate_init_rb'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:11:in silence_warnings' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin.rb:154:inevaluate_init_rb'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin.rb:48:in load' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin/loader.rb:38:inblock in load_plugins'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin/loader.rb:37:in each' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/rails/plugin/loader.rb:37:inload_plugins'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/initializer.rb:369:in load_plugins' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/initializer.rb:165:inprocess'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/initializer.rb:113:in run' /Users/james/projects/music-social/config/environment.rb:7:in<top (required)>'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:in require' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:inblock in require'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:521:in new_constants_in' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:156:inrequire'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rails-2.3.8/lib/tasks/misc.rake:4:in block in <top (required)>' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:636:incall'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:636:in block in execute' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:631:ineach'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:631:in execute' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:597:inblock in invoke_with_call_chain'
/Users/james/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/monitor.rb:201:in mon_synchronize' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:607:in block in invoke_prerequisites' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:604:ineach'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:604:in invoke_prerequisites' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:596:inblock in invoke_with_call_chain'
/Users/james/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/monitor.rb:201:in mon_synchronize' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:607:in block in invoke_prerequisites' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:604:ineach'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:604:in invoke_prerequisites' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:596:inblock in invoke_with_call_chain'
/Users/james/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/monitor.rb:201:in mon_synchronize' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:583:in invoke' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2051:ininvoke_task'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2029:in block (2 levels) in top_level' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2029:ineach'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2029:in block in top_level' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2023:in top_level' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2001:inblock in run'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:1998:inrun'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/bin/rake:31:in <top (required)>' /Users/james/.rvm/gems/ruby-1.9.2-rc2/bin/rake:19:inload'
/Users/james/.rvm/gems/ruby-1.9.2-rc2/bin/rake:19:in `

'

I see a whole heap of stuff has changed. Any ideas?

LOAD DATA LOCAL command

I have a rake task that load data in text files to tables, but got the following error:

Mysql2::Error: The used command is not allowed with this MySQL version: LOAD DATA LOCAL INFILE '/path/to/articles.dat' INTO TABLE articles

Will this command be added in the future or never by design?

undefined method `truncate_table'

I'm using technoweenie's masochism plugin and recently switched to the mysql2 adapter in a Rails project to give her a whirl. I'm getting the following error in my stack trace:

vendor/plugins/masochism/lib/active_reload/connection_proxy.rb:120:in 'send': undefined method 'truncate_table' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x2bfae1c> (NoMethodError)

Any plans on adding that? Workarounds? Suggestions?

For now, I'm switching back to the old adapter, but plan on using this when it gets some more mileage. Thanks for your time and contributions!

Finish docs

After API has solidified (only a few tweaks left), do a full documentation pass

using rails connect hangs when initialized from script/server, but not from script/console

Hi,

A certain action in a rails 2.3.5 app stalls when using the mysql2 gem, after investigation we have narrowed this to mysql2 establishing the connection. The loop is on client.c in the function nogvl_connect. When stalled, the script/server process cannot be interrupted and only a kill -KILL will stop it.

The same connection arguments work just fine if the connection is established from script/console (or from a stand-alone ruby process, if that matters). Also the old mysql gem works fine from rails, and the mysql native client can connect perfectly to the server.

To make it clearer, this code hangs forever:

class TestController < ApplicationController
  def test
    Mysql2::Client.new(:host => "our_host", :username => "xxx", :password => "xxx", :port => 3306, :database => "database", :socket => nil, :flags => 2)
  end
end

but this works perfectly:

>script/console
>>  Mysql2::Client.new(:host => "our_host", :username => "xxx", :password => "xxx", :port => 3306, :database => "database", :socket => nil, :flags => 2)

This has been replicated in two machines (Mac OS X, Snow Leopard, 64 bits). The target server is

mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.1.30, for suse-linux-gnu (i686) using readline 5.2
Server version:     5.1.30-log SUSE MySQL RPM
..

What information can we provide for a useful bug report? We have the C and ruby call stack but that doesn't seem very useful.

handle invalid datetime values somehow

irb(main):020:0> client.query("SELECT cast('0000-00-00 00:00:01' as datetime) as d").to_a
ArgumentError: argument out of range
    from (irb):20:in `local'
    from (irb):20:in `each'
    from (irb):20:in `to_a'
    from (irb):20
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:168

rake db:drop don't work with rails 3 and mysql2 gem

Ruby: 1.8.7 and 1.9.3dev (2010-05-08 trunk 27674) [x86_64-linux]
Rails: 3.0.0.beta3
mysql2: (0.1.8)

rake-svn db:drop && rake-svn db:create
(in /home/petrushka/webdev/test)
(in /home/petrushka/webdev/test)
test_test already exists
test_development already exists

I.e. rake db:drop do nothing... But it works fine with common mysql gem.

Add Mysql2::Result#fields

Currently, a result will only yield a hash for every row. The problem with hashes is that the order of the fields in the result is not preserved. In some cases, the order is important however.

Maybe it is a good idea to be able to inspect the fields of a result, e.g. a Mysql2::Result#fields method that will return an (ordered) array with all fields in the result, using the mysql_fetch_field and mysql_num_fields functions from the MySQL client API.

Exception during UTC conversion of datetime values

I'm getting an exception whenever I'm accessing a non-null datetime value. The error seems to happen during the UTC conversion:

Rails 2.3.5, ruby 1.8.7-174, OSX 10.6

>> post = Post.first
>> post.created_at

    NoMethodError: undefined method `period_for_utc' for nil:NilClass
  from /Library/WebServer/railsapps/sparkle/vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing'
  from /Library/WebServer/railsapps/sparkle/vendor/rails/activesupport/lib/active_support/time_with_zone.rb:58:in `period'
  from /Library/WebServer/railsapps/sparkle/vendor/rails/activesupport/lib/active_support/time_with_zone.rb:44:in `time'
  from /Library/WebServer/railsapps/sparkle/vendor/rails/activesupport/lib/active_support/time_with_zone.rb:99:in `inspect'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:302:in `output_value'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:151:in `eval_input'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:263:in `signal_status'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:147:in `eval_input'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:146:in `eval_input'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:70:in `start'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `catch'
  from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `start'
  from /usr/bin/irb:13

I do not seem to have this issue with Rails 3.

Add EventMachine support

Because you are using mysql's native query API and select()'ing the socket yourself, mysql2 won't play nicely in an event-driven system afaik. I'd like to see Mysql2 somehow outsource its socket management to EM when EM is running. Perhaps tmm1 could give us some advice (cause I'm not sure what the right thing to do here is either).

Fixnum to String

When MySQL password (presumably the same with login) consists only of digits (eg. 123456) there is a "Cannot convert from Fixnum to String" error. After changing password to, say, q123456 - it works.

mysql2 (0.2.3)
ruby 1.9.3dev (2010-08-21 trunk 29063) [i686-linux]
rails 3.0.0

Password should be converted to a string

When using Mysql2 with Rails 3 and you have a numeric password then an exception is raised as the password is a Fixnum. Putting quotes around the password forces it to be a String but it should be automatically cast to a string.

ActiveRecord mysql2 adapter doesn't work

I installed mysql2 gem and tried to use it as adapter for ActiveRecord. I am using ruby1.9.2-head and rails3 rc. I changed adapter to mysql2, but got this message:

ras@chef:~/Programming/rails/latest$ r c
/home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in 'rescue in establish_connection': Please install the mysql2 adapter: 'gem install activerecord-mysql2-adapter' (no such file to load -- active_record/connection_adapters/mysql2_adapter) (RuntimeError)
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/connection_specification.rb:68:in 'establish_connection'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/connection_specification.rb:60:in 'establish_connection'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in 'establish_connection'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/railtie.rb:55:in 'block (2 levels) in class:Railtie'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.0.rc/lib/active_support/lazy_load_hooks.rb:17:in 'instance_eval'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.0.rc/lib/active_support/lazy_load_hooks.rb:17:in 'execute_hook'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.0.rc/lib/active_support/lazy_load_hooks.rb:24:in 'block in run_load_hooks'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.0.rc/lib/active_support/lazy_load_hooks.rb:23:in 'each'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activesupport-3.0.0.rc/lib/active_support/lazy_load_hooks.rb:23:in 'run_load_hooks'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/base.rb:1803:in '<top (required)>'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/activerecord-3.0.0.rc/lib/active_record/railtie.rb:28:in 'block in class:Railtie'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/railtie.rb:180:in 'call'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/railtie.rb:180:in 'each'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/railtie.rb:180:in 'load_console'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/application.rb:154:in 'block in load_console'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/application/railties.rb:11:in 'each'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/application/railties.rb:11:in 'all'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/application.rb:154:in 'load_console'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/commands/console.rb:26:in 'start'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/commands/console.rb:8:in 'start'
from /home/ras/.rvm/gems/ruby-1.9.2-head/gems/railties-3.0.0.rc/lib/rails/commands.rb:23:in '<top (required)>'
from script/rails:6:in 'require'
from script/rails:6:in '

'

performance

hi,

you say that its 25% faster then mysql gem, i have tested it with your Benchmark and ApacheBench and at least at my machine its about 10% slower ;)

im using ubuntu 10.4, ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux], rails 2.3.5

Does not compile against OSX binary install

I installed the 5.1.45 OSX binary package from dev.mysql.com. The headers are in /usr/local/mysql/include:

/usr/local/mysql/include> ls
decimal.h         my_config.h       my_no_pthread.h   mysql_time.h      sslopt-case.h
errmsg.h          my_dbug.h         my_pthread.h      mysql_version.h   sslopt-longopts.h
keycache.h        my_dir.h          my_sys.h          mysqld_ername.h   sslopt-vars.h
m_ctype.h         my_getopt.h       my_xml.h          mysqld_error.h    typelib.h
m_string.h        my_global.h       mysql.h           plugin.h
my_alloc.h        my_list.h         mysql_com.h       sql_common.h
my_attribute.h    my_net.h          mysql_embed.h     sql_state.h

So I get this error:

/Users/mike/.rvm/rubies/ruby-1.9.1-p378/bin/ruby extconf.rb --with-mysql-dir=/usr/local/mysql
checking for mysql/mysql.h... no
checking for main() in -lmysqlclient... yes
creating Makefile

make
gcc -I. -I/Users/mike/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/i386-darwin10.2.0 -I/Users/mike/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/backward -I/Users/mike/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1 -I. -I/usr/local/mysql/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -fno-common  -O2 -g -Wall -Wno-parentheses -pipe -fno-common -Wall -Wextra -funroll-loops -DRUBY_19_COMPATIBILITY  -o mysql2_ext.o -c mysql2_ext.c
In file included from mysql2_ext.c:1:
mysql2_ext.h:4:25: error: mysql/mysql.h: No such file or directory
mysql2_ext.h:5:29: error: mysql/mysql_com.h: No such file or directory
mysql2_ext.h:6:26: error: mysql/errmsg.h: No such file or directory
mysql2_ext.h:7:32: error: mysql/mysqld_error.h: No such file or directory

Raises when password is a fixnum

On localhost password was 123, it tells

can't convert Fixnum into String
/Users/pavel/.rvm/gems/ree-1.8.7-2010.02/gems/mysql2-0.2.3/lib/mysql2/client.rb:36:in `connect'

with '123' - is ok

runtime issue with mysql2, bundler(1.0.0.rc5) and mysql 5.1.38

When I deploy my application mysql2 builds fine but I get the following error when trying to run the application.

I tried it without any flags (and it built properly) and tried to force a flag (bundle config build.mysql2 --with-mysql-dir=/usr/local/mysql/)

For what it matters the file does exist at (/www/servers/app/current/vendor/bundle/ruby/1.8/gems/mysql2-0.1.9/lib/mysql2/mysql2.so) even though it says it doesn't.

libmysqlclient.so.16: cannot open shared object file: No such file or directory - /www/servers/app/current/vendor/bundle/ruby/1.8/gems/mysql2-0.1.9/lib/mysql2/mysql2.so

0   /www/servers/app/current/vendor/bundle/ruby/1.8/gems/mysql2-0.1.9/lib/mysql2/mysql2.so          
1   /www/servers/app/current/vendor/bundle/ruby/1.8/gems/mysql2-0.1.9/lib/mysql2.rb     6   
2   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler/runtime.rb     64  in `require'
3   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler/runtime.rb     64  in `require'
4   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler/runtime.rb     62  in `each'
5   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler/runtime.rb     62  in `require'
6   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler/runtime.rb     51  in `each'
7   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler/runtime.rb     51  in `require'
8   /usr/local/lib/ruby/gems/1.8/gems/bundler-1.0.0.rc.5/lib/bundler.rb     107     in `require'
9   /www/servers/app/current/config/application.rb  7   
10  /www/servers/app/current/config/environment.rb  2   in `require'
11  /www/servers/app/current/config/environment.rb  2   
12  config.ru   3   in `require'
13  config.ru   3   
14  /www/servers/app/current/vendor/bundle/ruby/1.8/gems/rack-1.2.1/lib/rack/builder.rb     46  in `instance_eval'
15  /www/servers/app/current/vendor/bundle/ruby/1.8/gems/rack-1.2.1/lib/rack/builder.rb     46  in `initialize'
16  config.ru   1   in `new'
17  config.ru

mysql_config
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/local/mysql/include/mysql -DUNIV_LINUX -DUNIV_LINUX]
--include [-I/usr/local/mysql/include/mysql]
--libs [-rdynamic -L/usr/local/mysql/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm]
--libs_r [-rdynamic -L/usr/local/mysql/lib/mysql -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread]
--plugindir [/usr/local/mysql/lib/mysql/plugin]
--socket [/tmp/mysql.sock]
--port [0]
--version [5.1.38]
--libmysqld-libs [-rdynamic -L/usr/local/mysql/lib/mysql -lmysqld -ldl -lz -lpthread -lcrypt -lnsl -lm -lpthread -lrt]

Gemfile
source 'http://rubygems.org'

gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'mysql2', :git => "git://github.com/brianmario/mysql2.git"
gem 'agraph', :require => 'allegro_graph'

gem 'haml'

gem 'warden'
gem 'devise', :git => "git://github.com/plataformatec/devise.git"
gem 'bcrypt-ruby', :require => 'bcrypt'
gem "oauth2"

gem "will_paginate", '3.0.pre2'
gem 'system_timer'

gem 'fastercsv'

group :development do
  gem "rspec-rails", ">= 2.0.0.beta.19"
end

group :test, :cucumber do
  # bundler requires these gems while running tests
  gem 'cucumber'
  gem 'webrat'
  gem "rspec", ">= 2.0.0.beta.19"
  gem "rspec-rails", ">= 2.0.0.beta.19"
end

rake db:create in rails 3

I have an issue with creating a new database in Rails 3 using Mysql2 gem, but if the database is already created, migrate works fine ... Here is the trace when running rake db:create :

rake db:create --trace
(in /home/radu/projects/r30)
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:create
rake aborted!
undefined method errno' for #<Mysql2::Error:0xb70e1ebc> /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:71:increate_database'
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:33
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in call' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:inexecute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in each' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:inexecute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in invoke_with_call_chain' /usr/lib/ruby/1.8/monitor.rb:242:insynchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in invoke_with_call_chain' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:ininvoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in invoke_task' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:intop_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in each' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:intop_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:intop_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in run' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in run' /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31 /usr/bin/rake:19:inload'
/usr/bin/rake:19

Segfault - mysql2_adapter.rb

I haven't had a chance to investigate this much, but I just put my Rails 3 app into production today and after about 5 hours there was a segfault and all my Thin instances crashed. I will see if I can track down where it happened, since I was in debug log_level in my app... figured I would go ahead and post this:

/rails_apps/my_app/current/vendor/bundle/ruby/1.9.1/gems/mysql2-0.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:309: [BUG] Segmentation fault
ruby 1.9.2dev (2010-07-11 revision 28618) [x86_64-linux]

-- control frame ----------
c:0116 p:---- s:0557 b:0557 l:000556 d:000556 CFUNC :query
c:0115 p:0014 s:0553 b:0553 l:000534 d:000552 BLOCK /rails_apps/my_app/current/vendor/bundle/ruby/1.9.1/gems/mysql2-0.2.1/lib/active_record/connection_adapters/mysql2_adapter
c:0114 p:0005 s:0551 b:0551 l:000541 d:000550 BLOCK /rails_apps/my_app/current/vendor/bundle/ruby/1.9.1/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstr
c:0113 p:0032 s:0549 b:0549 l:000548 d:000548 METHOD /rails_apps/myapp/current/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.0.rc/lib/active_support/notifications/instrumen

errors in schema.rb

mysql2 driver fails to generate schema.rb in rails 2.3.5 app
schema.rb contains following, instead of actual table dumps:

# Could not dump table "assets" because of following NameError
#   uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::PRIMARY

Date and date_select (rails)

When i try to update/create a model and it doesn't validates, it raises a NoMethodError exception while rendering the form again.
The raised exception says:
undefined method `day' for "2009-01-12 ":String

and the line where the error happens contains:

<%= date_select :user, :birthday %>

This doesn't happen with the classic mysql gem.

Using: Rails 2.3.5 and ruby 1.8.7 p249 and rubyee 1.8.7 2009.10

Rails 3 rake db:migrate Segmentation fault

When we attempt to do a migration we get a segmentation fault in mysql2, is there any fix for this?

rake db:migrate
(in /Users/user/Sites/railsapp)
/Users/user/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.3/lib/mysql2/mysql2.bundle: [BUG] Segmentation fault
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]

-- control frame ----------
c:0028 p:-541309752 s:0084 b:0084 l:000083 d:000083 TOP
c:0027 p:---- s:0082 b:0082 l:000081 d:000081 CFUNC :require
c:0026 p:0072 s:0078 b:0078 l:000077 d:000077 TOP /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.3/lib/mysql2.rb:7
c:0025 p:---- s:0076 b:0076 l:000075 d:000075 FINISH
c:0024 p:---- s:0074 b:0074 l:000073 d:000073 CFUNC :require
c:0023 p:0026 s:0070 b:0070 l:000051 d:000069 BLOCK /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:64
c:0022 p:---- s:0067 b:0067 l:000066 d:000066 FINISH
c:0021 p:---- s:0065 b:0065 l:000064 d:000064 CFUNC :each
c:0020 p:0091 s:0062 b:0062 l:000051 d:000061 BLOCK /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:62
c:0019 p:---- s:0057 b:0057 l:000056 d:000056 FINISH
c:0018 p:---- s:0055 b:0055 l:000054 d:000054 CFUNC :each
c:0017 p:0046 s:0052 b:0052 l:000051 d:000051 METHOD /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:51
c:0016 p:0021 s:0048 b:0048 l:000047 d:000047 METHOD /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler.rb:112
c:0015 p:0079 s:0044 b:0044 l:000043 d:000043 TOP /Users/user/Sites/railsapp/config/application.rb:7
c:0014 p:---- s:0042 b:0042 l:000041 d:000041 FINISH
c:0013 p:---- s:0040 b:0040 l:000039 d:000039 CFUNC :require
c:0012 p:0013 s:0036 b:0036 l:000035 d:000035 METHOD internal:lib/rubygems/custom_require:29
c:0011 p:0026 s:0031 b:0031 l:000030 d:000030 TOP /Users/user/Sites/railsapp/Rakefile:4
c:0010 p:---- s:0029 b:0029 l:000028 d:000028 FINISH
c:0009 p:---- s:0027 b:0027 l:000026 d:000026 CFUNC :loadc:0008 p:0334 s:0023 b:0023 l:000022 d:000022 METHOD /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb
:2373
c:0007 p:0009 s:0018 b:0018 l:000011 d:000017 BLOCK /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2007
c:0006 p:0009 s:0016 b:0016 l:000015 d:000015 METHOD /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2058
c:0005 p:0011 s:0012 b:0012 l:000011 d:000011 METHOD /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2006
c:0004 p:0021 s:0009 b:0009 l:000008 d:000008 METHOD /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:1991
c:0003 p:0312 s:0006 b:0006 l:001588 d:0011b8 EVAL /Users/user/.rvm/rubies/ruby-1.9.2-p0/bin/rake:41
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH

c:0001 p:0000 s:0002 b:0002 l:001588 d:001588 TOP

-- Ruby level backtrace information ----------------------------------------
/Users/user/.rvm/rubies/ruby-1.9.2-p0/bin/rake:41:in <main>' /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:1991:inrun'
/Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2006:in load_rakefile' /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2058:instandard_exception_handling'
/Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2007:in block in load_rakefile' /Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2373:inraw_load_rakefile'
/Users/user/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/rake.rb:2373:in load' /Users/user/Sites/railsapp/Rakefile:4:in<top (required)>'
internal:lib/rubygems/custom_require:29:in require' <internal:lib/rubygems/custom_require>:29:inrequire'
/Users/user/Sites/railsapp/config/application.rb:7:in <top (required)>' /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler.rb:112:inrequire'
/Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:51:in require' /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:51:ineach'
/Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:62:in block in require' /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:62:ineach'
/Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in block (2 levels) in require' /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:inrequire'
/Users/user/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.3/lib/mysql2.rb:7:in <top (required)>' /Users/user/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.3/lib/mysql2.rb:7:inrequire'

-- C level backtrace information -------------------------------------------

undefined method `errno' for #<Mysql2::Error:0x8fd09dc>

rails 3.0.0, ruby 1.9.2
Gem installed without any problems, but when i try to make rake db:create i get this error
full log of the error:
rake aborted!
undefined method errno' for #<Mysql2::Error:0x8fd09dc> /opt/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:71:inrescue in rescue in create_database'
/opt/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:66:in rescue in create_database' /opt/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:39:increate_database'
/opt/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/railties/databases.rake:33:in block (2 levels) in <top (required)>' /opt/ruby/lib/ruby/1.9.1/rake.rb:634:incall'
/opt/ruby/lib/ruby/1.9.1/rake.rb:634:in block in execute' /opt/ruby/lib/ruby/1.9.1/rake.rb:629:ineach'
/opt/ruby/lib/ruby/1.9.1/rake.rb:629:in execute' /opt/ruby/lib/ruby/1.9.1/rake.rb:595:inblock in invoke_with_call_chain'
/opt/ruby/lib/ruby/1.9.1/monitor.rb:201:in mon_synchronize' /opt/ruby/lib/ruby/1.9.1/rake.rb:588:ininvoke_with_call_chain'
/opt/ruby/lib/ruby/1.9.1/rake.rb:581:in invoke' /opt/ruby/lib/ruby/1.9.1/rake.rb:2041:ininvoke_task'
/opt/ruby/lib/ruby/1.9.1/rake.rb:2019:in block (2 levels) in top_level' /opt/ruby/lib/ruby/1.9.1/rake.rb:2019:ineach'
/opt/ruby/lib/ruby/1.9.1/rake.rb:2019:in block in top_level' /opt/ruby/lib/ruby/1.9.1/rake.rb:2058:instandard_exception_handling'
/opt/ruby/lib/ruby/1.9.1/rake.rb:2013:in top_level' /opt/ruby/lib/ruby/1.9.1/rake.rb:1992:inrun'
/usr/bin/rake:31:in `

'

mysql2 has issues with order in an array with AR 2.3.x

These work with the regular mysql gem. You can work around it by just removing the array or changing it into a string.

Failing
Ontology.all(:order => [:name])
Ontology.all(:order => [:name, :id])
Ontology.all(:order => ['name'])
Ontology.all(:order => ['name', 'id'])

Passing
Ontology.all(:order => :name)
Ontology.all(:order => 'name')
Ontology.all(:order => 'name, id')

Error messages
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[:name]' at line 1: SELECT * FROM ontologies ORDER BY [:name]
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '["name"]' at line 1: SELECT * FROM ontologies ORDER BY ["name"]

mysql2 branch for rails 2.3.x

Excellent news that mysql2 is going to Rails 3 core.
But what about users with rails 2.3?

I'm using mysql2 for bunch of my projects under rails 2.3. MySql2 0.1.9 gem has connection issues in development mode (RAIL_ENV = development + Rails 2.3.8 + Unicorn + Bundler + Mac Os X). I switched to Mysql2 master and was waiting for MySql2 0.2 to be released with connection closed issues fixed.

Can you create branch from 5a9ca9c and release new gem build? Last one %)

ActiveRecord: decimal attribute of model defaulting to "0.00"

When I extract an existing model with a decimal attribute from my database using ActiveRecord, the value is a BigDecimal:

>> sale = RetailSale.last
=> #<RetailSale id: 76, customer_id: 127, invoice_id: 177329, round: #<BigDecimal:7fec6373bbb0,'0.0',9(18)>

When I start a new AR object of the same model, the decimal attribute is set to a string:

>> RetailSale.new
=> #<RetailSale id: nil, customer_id: nil, invoice_id: nil, round: "0.00">

I was assuming the default should be a BigDecimal set to 0. Is my assumption incorrect or is it a bug? Running on mysql2 v0.1.4.

AR adapter connection pool management problem

I am having troubles with the connection management of the ActiveRecord adapter. For some reason, I am getting "Mysql2::Error: closed MySQL connection" after a couple of refreshes of the same page.

The page uses one ActiveRecord.find(id) call, and several (about 10) calls to the ActiveRecord::Base.connection.select_values(sql) method. It usually works fine the first time and after the first and second refresh of the page, but it'll fail eventually.

The error occurs outside of my own code. This is the backtrace:

mysql2 (0.1.9) lib/active_record/connection_adapters/mysql2_adapter.rb:246:in `close'
mysql2 (0.1.9) lib/active_record/connection_adapters/mysql2_adapter.rb:246:in `disconnect!'
activerecord (3.0.0.beta4) lib/active_record/connection_adapters/abstract/connection_pool.rb:145:in `clear_reloadable_connections_without_synchronization!'
activerecord (3.0.0.beta4) lib/active_record/connection_adapters/abstract/connection_pool.rb:144:in `each'
activerecord (3.0.0.beta4) lib/active_record/connection_adapters/abstract/connection_pool.rb:144:in `clear_reloadable_connections_without_synchronization!'
activesupport (3.0.0.beta4) lib/active_support/core_ext/module/synchronization.rb:34:in `clear_reloadable_connections!'
/Users/willem/.rvm/rubies/ruby-1.8.7-p174/lib/ruby/1.8/monitor.rb:242:in `synchronize'
activesupport (3.0.0.beta4) lib/active_support/core_ext/module/synchronization.rb:33:in `clear_reloadable_connections!'

When I comment out the calls to the custom select_values call, the problem does not seem to occur anymore. This is the code I am using:

  sql = <<-EOSQL 
        SELECT DISTINCT #{field} AS `possible_value`
          FROM `#{table_name}` 
         ORDER BY `possible_value`
      EOSQL

  ActiveRecord::Base.connection.select_values(sql)

These errors only occur in development mode; in production everything seems to be working OK. Also, when I rewrite the method above to use execute and handle the result myself, the problem persists. Moreover, the exact same code works fine with the normal mysql gem, except for some typecasting issues (which is why I want to use mysql2).

malloc error

I am getting this:

ruby(32263,0x10240a000) malloc: *** error for object 0x101192c00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Exception Mysql2::Error' at /path/mysql_logger.rb:21 - Lost connection to MySQL server at 'reading authorization packet', system error: 9 ExceptionMysql2::Error' at /path/mysql_logger.rb:23 - Lost connection to MySQL server at 'reading authorization packet', system error: 9
zsh: abort ruby -Ilib -d bin/tpm -f -c tpm.conf

And the file:
require 'mysql2'
require 'terminal-table/import'
module TPM
class MysqlLogger

def initialize(host)
  @host    = host
  server   = TPM.config['db']['server']
  user     = TPM.config['db']['user']
  password = TPM.config['db']['password']
  db       = TPM.config['db']['db']
  @con     = Mysql2::Client.new(:host => server, :username => user, :password => password)
  @con.query("USE `#{db}`")
end

def log(loginfo)
  loginfo = {:status => 'info', :action => nil, :version => nil}.merge! loginfo
  loginfo[:message] ||= ''
  loginfo[:category] = 'internal' if loginfo[:category].nil?
  begin
    @con.query("INSERT INTO logs (ts, status, category, action, message, host, site, version) VALUES (#{Time.now.to_i}, '#{loginfo[:status]}', '#{loginfo[:category]}', '#{loginfo[:action]}', '#{loginfo[:message].to_s.esc}', '#{@host}', '#{loginfo[:site]}', '#{loginfo[:version]}')", :async => true)
  rescue Exception
    raise
  end
end

end
end

Hope this helps.

Christoph

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.