Skip to content

Commit

Permalink
Merge branch 'master' into force_latin1_to_utf8
Browse files Browse the repository at this point in the history
* master:
  GitHub is HTTPS by default (brianmario#922)
  Fix wrong value of type YEAR on big endian environment. (brianmario#921)
  Avoid 0 byte allocation call when statement takes no parameters
  Small spec improvement for RSpec style
  Travis CI for Mac OS X builds use the same steps again
  More specific exception classes (brianmario#911)
  Travis CI matrix add Ruby 2.5 and switch Mac OS X to Ruby 2.3 (ala High Sierra)
  README text for query options on Statement#execute
  Accept query options on Statement#execute (brianmario#912)
  Drop 1.9.3 support (brianmario#913)
  • Loading branch information
jeremy committed Dec 11, 2017
2 parents 735d082 + e52e9ce commit 04d7daa
Show file tree
Hide file tree
Showing 45 changed files with 237 additions and 321 deletions.
6 changes: 1 addition & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 1.9
TargetRubyVersion: 2.0

DisplayCopNames: true
Exclude:
Expand All @@ -21,10 +21,6 @@ Layout/IndentHeredoc:
Lint/EndAlignment:
EnforcedStyleAlignWith: variable

Style/Encoding:
AutoCorrectEncodingComment: '# encoding: UTF-8'
EnforcedStyle: always

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma

Expand Down
13 changes: 4 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bundler_args: --without benchmarks development
# Pin Rubygems to a working version. Sometimes it breaks upstream. Update now and then.
before_install:
- gem --version
- gem update --system 2.6.12
- gem update --system 2.7.3
- gem update bundler
- gem --version
- bash .travis_setup.sh
Expand All @@ -18,12 +18,12 @@ addons:
- mysql-client-core-5.6
- mysql-client-5.6
rvm:
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0.0
- 1.9.3
- ruby-head
matrix:
include:
Expand Down Expand Up @@ -73,21 +73,16 @@ matrix:
hosts:
- mysql2gem.example.com
- os: osx
rvm: system
rvm: 2.3
env: DB=mysql56
addons:
hosts:
- mysql2gem.example.com
before_install:
# Use the system RubyGem with the system Ruby
- gem --version
- sudo gem install bundler
- bash .travis_setup.sh
fast_finish: true
allow_failures:
- rvm: ruby-head
- os: osx
rvm: system
rvm: 2.3
env: DB=mysql56
- rvm: 2.0.0
env: DB=mysql51
Expand Down
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

source 'https://rubygems.org'

gemspec
Expand All @@ -10,9 +8,8 @@ gem 'rake-compiler', '~> 1.0'
group :test do
gem 'eventmachine' unless RUBY_PLATFORM =~ /mswin|mingw/
gem 'rspec', '~> 3.2'
# https://github.com/bbatsov/rubocop/pull/3328
# https://github.com/bbatsov/rubocop/pull/4789
gem 'rubocop', '~> 0.50.0' unless RUBY_VERSION =~ /1.9/
gem 'rubocop', '~> 0.50.0'
end

group :benchmarks do
Expand Down
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The Mysql2 gem is meant to serve the extremely common use-case of connecting, qu
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 all strings in 1.9, unless Encoding.default_internal is set then it'll convert from UTF-8 to that encoding] and uses encoding-aware MySQL API calls where it can.
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:

Expand Down Expand Up @@ -185,7 +185,8 @@ 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.
question marks in the statement. Query options can be passed as keyword arguments
to the execute method.

``` ruby
statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
Expand All @@ -194,6 +195,9 @@ 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)
```

## Connection options
Expand Down Expand Up @@ -392,6 +396,15 @@ c = Mysql2::Client.new
c.query(sql, :symbolize_keys => true)
```

or

``` ruby
# 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
Expand Down Expand Up @@ -520,7 +533,7 @@ As for field values themselves, I'm workin on it - but expect that soon.

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

* Ruby MRI 1.9.3, 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x
* Ruby MRI 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x
* 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:
Expand Down
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

require 'rake'

# Load custom tasks (careful attention to define tasks before prerequisites)
Expand All @@ -12,7 +10,7 @@ load 'tasks/benchmarks.rake'
begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new
task default: [:spec, :rubocop]
task default: %i[spec rubocop]
rescue LoadError
warn 'RuboCop is not available'
task default: :spec
Expand Down
2 changes: 0 additions & 2 deletions benchmark/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/active_record_threaded.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/allocations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/escape.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/query_with_mysql_casting.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/query_without_mysql_casting.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/sequel.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions benchmark/setup_db.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

# This script is for generating psudo-random data into a single table consisting of nearly every
Expand Down
2 changes: 0 additions & 2 deletions examples/eventmachine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

$LOAD_PATH.unshift 'lib'

require 'rubygems'
Expand Down
2 changes: 0 additions & 2 deletions examples/threaded.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

$LOAD_PATH.unshift 'lib'
require 'mysql2'
require 'timeout'
Expand Down
6 changes: 4 additions & 2 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "mysql_enc_name_to_ruby.h"

VALUE cMysql2Client;
extern VALUE mMysql2, cMysql2Error;
extern VALUE mMysql2, cMysql2Error, cMysql2TimeoutError;
static VALUE sym_id, sym_version, sym_header_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
static VALUE sym_no_good_index_used, sym_no_index_used, sym_query_was_slow;
static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args;
Expand Down Expand Up @@ -595,6 +595,7 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
return Qnil;
}

// Duplicate the options hash and put the copy in the Result object
current = rb_hash_dup(rb_iv_get(self, "@current_query_options"));
(void)RB_GC_GUARD(current);
Check_Type(current, T_HASH);
Expand Down Expand Up @@ -659,7 +660,7 @@ static VALUE do_query(void *args) {
retval = rb_wait_for_single_fd(async_args->fd, RB_WAITFD_IN, tvp);

if (retval == 0) {
rb_raise(cMysql2Error, "Timeout waiting for a response from the last query. (waited %d seconds)", FIX2INT(read_timeout));
rb_raise(cMysql2TimeoutError, "Timeout waiting for a response from the last query. (waited %d seconds)", FIX2INT(read_timeout));
}

if (retval < 0) {
Expand Down Expand Up @@ -1155,6 +1156,7 @@ static VALUE rb_mysql_client_store_result(VALUE self)
return Qnil;
}

// Duplicate the options hash and put the copy in the Result object
current = rb_hash_dup(rb_iv_get(self, "@current_query_options"));
(void)RB_GC_GUARD(current);
Check_Type(current, T_HASH);
Expand Down
6 changes: 0 additions & 6 deletions ext/mysql2/client.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#ifndef MYSQL2_CLIENT_H
#define MYSQL2_CLIENT_H

#ifndef HAVE_RB_THREAD_CALL_WITHOUT_GVL
/* emulate rb_thread_call_without_gvl with rb_thread_blocking_region */
#define rb_thread_call_without_gvl(func, data1, ubf, data2) \
rb_thread_blocking_region((rb_blocking_function_t *)func, data1, ubf, data2)
#endif /* ! HAVE_RB_THREAD_CALL_WITHOUT_GVL */

typedef struct {
VALUE encoding;
VALUE active_thread; /* rb_thread_current() or Qnil */
Expand Down
13 changes: 0 additions & 13 deletions ext/mysql2/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

require 'mkmf'
require 'English'

Expand Down Expand Up @@ -27,9 +25,6 @@ def add_ssl_defines(header)
have_func('rb_absint_size')
have_func('rb_absint_singlebit_p')

# 2.0-only
have_header('ruby/thread.h') && have_func('rb_thread_call_without_gvl', 'ruby/thread.h')

# Missing in RBX (https://github.com/rubinius/rubinius/issues/3771)
have_func('rb_wait_for_single_fd')

Expand Down Expand Up @@ -57,14 +52,6 @@ def add_ssl_defines(header)
# If the user has provided a --with-mysql-dir argument, we must respect it or fail.
inc, lib = dir_config('mysql')
if inc && lib
# TODO: Remove when 2.0.0 is the minimum supported version
# Ruby versions not incorporating the mkmf fix at
# https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/39717
# do not properly search for lib directories, and must be corrected
unless lib && lib[-3, 3] == 'lib'
@libdir_basename = 'lib'
inc, lib = dir_config('mysql')
end
abort "-----\nCannot find include dir(s) #{inc}\n-----" unless inc && inc.split(File::PATH_SEPARATOR).any? { |dir| File.directory?(dir) }
abort "-----\nCannot find library dir(s) #{lib}\n-----" unless lib && lib.split(File::PATH_SEPARATOR).any? { |dir| File.directory?(dir) }
warn "-----\nUsing --with-mysql-dir=#{File.dirname inc}\n-----"
Expand Down
3 changes: 2 additions & 1 deletion ext/mysql2/mysql2_ext.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <mysql2_ext.h>

VALUE mMysql2, cMysql2Error;
VALUE mMysql2, cMysql2Error, cMysql2TimeoutError;

/* Ruby Extension initializer */
void Init_mysql2() {
mMysql2 = rb_define_module("Mysql2");
cMysql2Error = rb_const_get(mMysql2, rb_intern("Error"));
cMysql2TimeoutError = rb_const_get(cMysql2Error, rb_intern("TimeoutError"));

init_mysql2_client();
init_mysql2_result();
Expand Down
8 changes: 0 additions & 8 deletions ext/mysql2/mysql2_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ void Init_mysql2(void);
#endif

#include <ruby/encoding.h>
// ruby/thread.h was added in 2.0.0. See:
// https://github.com/ruby/ruby/commit/c51a826
//
// Rubinius doesn't define this, but it ships an empty thread.h (the symbols we
// care about are in ruby.h); this is safe to remove when < 2.0.0 is no longer
// supported.
#ifdef HAVE_RUBY_THREAD_H
#include <ruby/thread.h>
#endif

#if defined(__GNUC__) && (__GNUC__ >= 3)
#define RB_MYSQL_NORETURN __attribute__ ((noreturn))
Expand Down
4 changes: 2 additions & 2 deletions ext/mysql2/result.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ static void rb_mysql_result_alloc_result_buffers(VALUE self, MYSQL_FIELD *fields
wrapper->result_buffers[i].buffer_length = sizeof(signed char);
break;
case MYSQL_TYPE_SHORT: // short int
case MYSQL_TYPE_YEAR: // short int
wrapper->result_buffers[i].buffer = xcalloc(1, sizeof(short int));
wrapper->result_buffers[i].buffer_length = sizeof(short int);
break;
case MYSQL_TYPE_INT24: // int
case MYSQL_TYPE_LONG: // int
case MYSQL_TYPE_YEAR: // int
wrapper->result_buffers[i].buffer = xcalloc(1, sizeof(int));
wrapper->result_buffers[i].buffer_length = sizeof(int);
break;
Expand Down Expand Up @@ -365,6 +365,7 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
}
break;
case MYSQL_TYPE_SHORT: // short int
case MYSQL_TYPE_YEAR: // short int
if (result_buffer->is_unsigned) {
val = UINT2NUM(*((unsigned short int*)result_buffer->buffer));
} else {
Expand All @@ -373,7 +374,6 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
break;
case MYSQL_TYPE_INT24: // int
case MYSQL_TYPE_LONG: // int
case MYSQL_TYPE_YEAR: // int
if (result_buffer->is_unsigned) {
val = UINT2NUM(*((unsigned int*)result_buffer->buffer));
} else {
Expand Down
Loading

0 comments on commit 04d7daa

Please sign in to comment.