bring over latest from master

This commit is contained in:
Brian Lopez 2010-08-16 02:10:10 -07:00
commit 731b456862
21 changed files with 305 additions and 909 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ Makefile
*.rbc
mkmf.log
pkg/
tmp
vendor

View File

@ -21,8 +21,6 @@ end
require 'rake'
require 'spec/rake/spectask'
gem 'rake-compiler', '>= 0.4.1'
require "rake/extensiontask"
desc "Run all examples with RCov"
Spec::Rake::SpecTask.new('spec:rcov') do |t|
@ -43,7 +41,7 @@ Spec::Rake::SpecTask.new('spec:gdb') do |t|
t.ruby_cmd = "gdb --args #{RUBY}"
end
Rake::ExtensionTask.new("mysql2", JEWELER.gemspec) do |ext|
ext.lib_dir = File.join 'lib', 'mysql2'
end
Rake::Task[:spec].prerequisites << :compile
task :default => :spec
# Load custom tasks
Dir['tasks/*.rake'].sort.each { |f| load f }

33
benchmark/allocations.rb Normal file
View File

@ -0,0 +1,33 @@
# encoding: UTF-8
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
raise Mysql2::Mysql2Error.new("GC allocation benchmarks only supported on Ruby 1.9!") unless RUBY_VERSION =~ /1\.9/
require 'rubygems'
require 'benchmark'
require 'active_record'
ActiveRecord::Base.default_timezone = :local
ActiveRecord::Base.time_zone_aware_attributes = true
class Mysql2Model < ActiveRecord::Base
set_table_name :mysql2_test
end
def bench_allocations(feature, iterations = 10, &blk)
puts "GC overhead for #{feature}"
Mysql2Model.establish_connection(:adapter => 'mysql2', :database => 'test')
GC::Profiler.clear
GC::Profiler.enable
iterations.times{ blk.call }
GC::Profiler.report(STDOUT)
GC::Profiler.disable
end
bench_allocations('coercion') do
Mysql2Model.all(:limit => 1000).each{ |r|
r.attributes.keys.each{ |k|
r.send(k.to_sym)
}
}
end

View File

@ -7,31 +7,33 @@ require 'mysql'
require 'mysql2'
require 'do_mysql'
number_of = 1000
str = "abc'def\"ghi\0jkl%mno"
Benchmark.bmbm do |x|
mysql = Mysql.new("localhost", "root")
x.report do
puts "Mysql"
number_of.times do
mysql.quote str
def run_escape_benchmarks(str, number_of = 1000)
Benchmark.bmbm do |x|
mysql = Mysql.new("localhost", "root")
x.report do
puts "Mysql #{str.inspect}"
number_of.times do
mysql.quote str
end
end
end
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
x.report do
puts "Mysql2"
number_of.times do
mysql2.escape str
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
x.report do
puts "Mysql2 #{str.inspect}"
number_of.times do
mysql2.escape str
end
end
end
do_mysql = DataObjects::Connection.new("mysql://localhost/test")
x.report do
puts "do_mysql"
number_of.times do
do_mysql.quote_string str
do_mysql = DataObjects::Connection.new("mysql://localhost/test")
x.report do
puts "do_mysql #{str.inspect}"
number_of.times do
do_mysql.quote_string str
end
end
end
end
run_escape_benchmarks "abc'def\"ghi\0jkl%mno"
run_escape_benchmarks "clean string"

View File

@ -22,8 +22,10 @@ create_table_sql = %[
int_test INT,
big_int_test BIGINT,
float_test FLOAT(10,3),
float_zero_test FLOAT(10,3),
double_test DOUBLE(10,3),
decimal_test DECIMAL(10,3),
decimal_zero_test DECIMAL(10,3),
date_test DATE,
date_time_test DATETIME,
timestamp_test TIMESTAMP,
@ -55,7 +57,7 @@ def insert_record(args)
insert_sql = "
INSERT INTO mysql2_test (
null_test, bit_test, tiny_int_test, small_int_test, medium_int_test, int_test, big_int_test,
float_test, double_test, decimal_test, date_test, date_time_test, timestamp_test, time_test,
float_test, float_zero_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
long_blob_test, long_text_test, enum_test, set_test
@ -63,7 +65,7 @@ def insert_record(args)
VALUES (
NULL, #{args[:bit_test]}, #{args[:tiny_int_test]}, #{args[:small_int_test]}, #{args[:medium_int_test]}, #{args[:int_test]}, #{args[:big_int_test]},
#{args[:float_test]}, #{args[:double_test]}, #{args[:decimal_test]}, '#{args[:date_test]}', '#{args[:date_time_test]}', '#{args[:timestamp_test]}', '#{args[:time_test]}',
#{args[:float_test]}, #{args[:float_zero_test]}, #{args[:double_test]}, #{args[:decimal_test]}, #{args[:decimal_zero_test]}, '#{args[:date_test]}', '#{args[:date_time_test]}', '#{args[:timestamp_test]}', '#{args[:time_test]}',
#{args[:year_test]}, '#{args[:char_test]}', '#{args[:varchar_test]}', '#{args[:binary_test]}', '#{args[:varbinary_test]}', '#{args[:tiny_blob_test]}',
'#{args[:tiny_text_test]}', '#{args[:blob_test]}', '#{args[:text_test]}', '#{args[:medium_blob_test]}', '#{args[:medium_text_test]}',
'#{args[:long_blob_test]}', '#{args[:long_text_test]}', '#{args[:enum_test]}', '#{args[:set_test]}'
@ -82,8 +84,10 @@ num.times do |n|
:int_test => rand(2147483647),
:big_int_test => rand(9223372036854775807),
:float_test => rand(32767)/1.87,
:float_zero_test => 0.0,
:double_test => rand(8388607)/1.87,
:decimal_test => rand(8388607)/1.87,
:decimal_zero_test => 0,
:date_test => '2010-4-4',
:date_time_test => '2010-4-4 11:44:00',
:timestamp_test => '2010-4-4 11:44:00',

20
benchmark/thread_alone.rb Normal file
View File

@ -0,0 +1,20 @@
# encoding: UTF-8
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'rubygems'
require 'benchmark'
require 'mysql2'
iterations = 1000
client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "test")
query = lambda{ iterations.times{ client.query("SELECT mysql2_test.* FROM mysql2_test") } }
Benchmark.bmbm do |x|
x.report('select') do
query.call
end
x.report('rb_thread_select') do
thread = Thread.new{ sleep(10) }
query.call
thread.kill
end
end

20
examples/threaded.rb Normal file
View File

@ -0,0 +1,20 @@
# encoding: utf-8
$LOAD_PATH.unshift 'lib'
require 'mysql2'
require 'timeout'
threads = []
# Should never exceed worst case 3.5 secs across all 20 threads
Timeout.timeout(3.5) do
20.times do
threads << Thread.new do
overhead = rand(3)
puts ">> thread #{Thread.current.object_id} query, #{overhead} sec overhead"
# 3 second overhead per query
Mysql2::Client.new(:host => "localhost", :username => "root").query("SELECT sleep(#{overhead}) as result")
puts "<< thread #{Thread.current.object_id} result, #{overhead} sec overhead"
end
end
threads.each{|t| t.join }
end

View File

@ -5,7 +5,7 @@ VALUE cMysql2Client;
extern VALUE mMysql2, cMysql2Error;
static VALUE intern_encoding_from_charset;
static ID sym_id, sym_version, sym_async, sym_symbolize_keys, sym_as, sym_array;
static ID intern_merge;
static ID intern_merge, intern_error_number_eql, intern_sql_state_eql;
#define REQUIRE_OPEN_DB(_ctxt) \
if(!_ctxt->net.vio) { \
@ -14,7 +14,13 @@ static ID intern_merge;
}
#define MARK_CONN_INACTIVE(conn) \
rb_iv_set(conn, "@active", Qfalse);
wrapper->active = 0;
#define GET_CLIENT(self) \
mysql_client_wrapper *wrapper; \
MYSQL *client; \
Data_Get_Struct(self, mysql_client_wrapper, wrapper); \
client = &wrapper->client;
/*
* used to pass all arguments to mysql_real_connect while inside
@ -62,10 +68,17 @@ struct nogvl_send_query_args {
* - mysql_ssl_set()
*/
static void rb_mysql_client_mark(void * wrapper) {
mysql_client_wrapper * w = wrapper;
if (w) {
rb_gc_mark(w->encoding);
}
}
static VALUE rb_raise_mysql2_error(MYSQL *client) {
VALUE e = rb_exc_new2(cMysql2Error, mysql_error(client));
rb_funcall(e, rb_intern("error_number="), 1, INT2NUM(mysql_errno(client)));
rb_funcall(e, rb_intern("sql_state="), 1, rb_tainted_str_new2(mysql_sqlstate(client)));
rb_funcall(e, intern_error_number_eql, 1, INT2NUM(mysql_errno(client)));
rb_funcall(e, intern_sql_state_eql, 1, rb_tainted_str_new2(mysql_sqlstate(client)));
rb_exc_raise(e);
return Qnil;
}
@ -74,7 +87,7 @@ static VALUE nogvl_init(void *ptr) {
MYSQL * client = (MYSQL *)ptr;
/* may initialize embedded server and read /etc/services off disk */
mysql_init(client);
client = mysql_init(NULL);
return client ? Qtrue : Qfalse;
}
@ -92,7 +105,8 @@ static VALUE nogvl_connect(void *ptr) {
}
static void rb_mysql_client_free(void * ptr) {
MYSQL * client = (MYSQL *)ptr;
mysql_client_wrapper * wrapper = (mysql_client_wrapper *)ptr;
MYSQL * client = &wrapper->client;
/*
* we'll send a QUIT message to the server, but that message is more of a
@ -125,22 +139,17 @@ static VALUE nogvl_close(void * ptr) {
}
static VALUE allocate(VALUE klass) {
MYSQL * client;
return Data_Make_Struct(
klass,
MYSQL,
NULL,
rb_mysql_client_free,
client
);
VALUE obj;
mysql_client_wrapper * wrapper;
obj = Data_Make_Struct(klass, mysql_client_wrapper, rb_mysql_client_mark, rb_mysql_client_free, wrapper);
wrapper->encoding = Qnil;
wrapper->active = 0;
return obj;
}
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket) {
MYSQL * client;
struct nogvl_connect_args args;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
args.host = NIL_P(host) ? "localhost" : StringValuePtr(host);
args.unix_socket = NIL_P(socket) ? NULL : StringValuePtr(socket);
@ -166,9 +175,7 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
* for the garbage collector.
*/
static VALUE rb_mysql_client_close(VALUE self) {
MYSQL *client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
rb_thread_blocking_region(nogvl_close, client, RUBY_UBF_IO, 0);
@ -210,10 +217,8 @@ static VALUE nogvl_store_result(void *ptr) {
}
static VALUE rb_mysql_client_async_result(VALUE self) {
MYSQL * client;
MYSQL_RES * result;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
REQUIRE_OPEN_DB(client);
if (rb_thread_blocking_region(nogvl_read_query_result, client, RUBY_UBF_IO, 0) == Qfalse) {
@ -239,7 +244,9 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
rb_iv_set(resultObj, "@query_options", rb_obj_dup(rb_iv_get(self, "@query_options")));
#ifdef HAVE_RUBY_ENCODING_H
rb_iv_set(resultObj, "@encoding", rb_iv_get(self, "@encoding"));
mysql2_result_wrapper * result_wrapper;
GetMysql2Result(resultObj, result_wrapper);
result_wrapper->encoding = wrapper->encoding;
#endif
return resultObj;
}
@ -249,18 +256,17 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
fd_set fdset;
int fd, retval;
int async = 0;
VALUE opts, defaults, active;
MYSQL *client;
VALUE opts, defaults;
int(*selector)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL;
GET_CLIENT(self)
Data_Get_Struct(self, MYSQL, client);
REQUIRE_OPEN_DB(client);
args.mysql = client;
active = rb_iv_get(self, "@active");
// see if this connection is still waiting on a result from a previous query
if (NIL_P(active) || active == Qfalse) {
if (wrapper->active == 0) {
// mark this connection active
rb_iv_set(self, "@active", Qtrue);
wrapper->active = 1;
} else {
rb_raise(cMysql2Error, "This connection is still waiting for a result, try again once you have the result");
}
@ -278,7 +284,7 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
}
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
// ensure the string is in the encoding the connection is expecting
args.sql = rb_str_export_to_enc(args.sql, conn_enc);
#endif
@ -293,11 +299,12 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
// the below code is largely from do_mysql
// http://github.com/datamapper/do
fd = client->net.fd;
selector = rb_thread_alone() ? *select : *rb_thread_select;
for(;;) {
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
retval = rb_thread_select(fd + 1, &fdset, NULL, NULL, NULL);
retval = selector(fd + 1, &fdset, NULL, NULL, NULL);
if (retval < 0) {
rb_sys_fail(0);
@ -317,14 +324,14 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
}
static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
MYSQL * client;
VALUE newStr;
unsigned long newLen, oldLen;
GET_CLIENT(self)
Check_Type(str, T_STRING);
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
// ensure the string is in the encoding the connection is expecting
str = rb_str_export_to_enc(str, conn_enc);
#endif
@ -332,10 +339,8 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
oldLen = RSTRING_LEN(str);
char escaped[(oldLen*2)+1];
Data_Get_Struct(self, MYSQL, client);
REQUIRE_OPEN_DB(client);
newLen = mysql_real_escape_string(client, escaped, StringValuePtr(str), RSTRING_LEN(str));
newLen = mysql_real_escape_string(client, escaped, StringValuePtr(str), oldLen);
if (newLen == oldLen) {
// no need to return a new ruby string if nothing changed
return str;
@ -351,11 +356,12 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
}
}
static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE self) {
static VALUE rb_mysql_client_info(VALUE self) {
VALUE version = rb_hash_new(), client_info;
GET_CLIENT(self);
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
#endif
rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version()));
@ -371,14 +377,13 @@ static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE self) {
}
static VALUE rb_mysql_client_server_info(VALUE self) {
MYSQL * client;
VALUE version, server_info;
GET_CLIENT(self)
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
#endif
Data_Get_Struct(self, MYSQL, client);
REQUIRE_OPEN_DB(client);
version = rb_hash_new();
@ -395,31 +400,26 @@ static VALUE rb_mysql_client_server_info(VALUE self) {
}
static VALUE rb_mysql_client_socket(VALUE self) {
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
REQUIRE_OPEN_DB(client);
return INT2NUM(client->net.fd);
}
static VALUE rb_mysql_client_last_id(VALUE self) {
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
REQUIRE_OPEN_DB(client);
return ULL2NUM(mysql_insert_id(client));
}
static VALUE rb_mysql_client_affected_rows(VALUE self) {
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
REQUIRE_OPEN_DB(client);
return ULL2NUM(mysql_affected_rows(client));
}
static VALUE set_reconnect(VALUE self, VALUE value) {
my_bool reconnect;
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
if(!NIL_P(value)) {
reconnect = value == Qfalse ? 0 : 1;
@ -435,9 +435,7 @@ static VALUE set_reconnect(VALUE self, VALUE value) {
static VALUE set_connect_timeout(VALUE self, VALUE value) {
unsigned int connect_timeout = 0;
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
if(!NIL_P(value)) {
connect_timeout = NUM2INT(value);
@ -454,19 +452,16 @@ static VALUE set_connect_timeout(VALUE self, VALUE value) {
static VALUE set_charset_name(VALUE self, VALUE value) {
char * charset_name;
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
#ifdef HAVE_RUBY_ENCODING_H
VALUE new_encoding, old_encoding;
VALUE new_encoding;
new_encoding = rb_funcall(cMysql2Client, intern_encoding_from_charset, 1, value);
if (new_encoding == Qnil) {
rb_raise(cMysql2Error, "Unsupported charset: '%s'", RSTRING_PTR(value));
} else {
old_encoding = rb_iv_get(self, "@encoding");
if (old_encoding == Qnil) {
rb_iv_set(self, "@encoding", new_encoding);
if (wrapper->encoding == Qnil) {
wrapper->encoding = new_encoding;
}
}
#endif
@ -482,8 +477,7 @@ static VALUE set_charset_name(VALUE self, VALUE value) {
}
static VALUE set_ssl_options(VALUE self, VALUE key, VALUE cert, VALUE ca, VALUE capath, VALUE cipher) {
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
if(!NIL_P(ca) || !NIL_P(key)) {
mysql_ssl_set(client,
@ -498,8 +492,7 @@ static VALUE set_ssl_options(VALUE self, VALUE key, VALUE cert, VALUE ca, VALUE
}
static VALUE init_connection(VALUE self) {
MYSQL * client;
Data_Get_Struct(self, MYSQL, client);
GET_CLIENT(self)
if (rb_thread_blocking_region(nogvl_init, client, RUBY_UBF_IO, 0) == Qfalse) {
/* TODO: warning - not enough memory? */
@ -556,4 +549,7 @@ void init_mysql2_client() {
sym_array = ID2SYM(rb_intern("array"));
intern_merge = rb_intern("merge");
intern_error_number_eql = rb_intern("error_number=");
intern_sql_state_eql = rb_intern("sql_state=");
}

View File

@ -31,4 +31,10 @@ rb_thread_blocking_region(
void init_mysql2_client();
typedef struct {
VALUE encoding;
short int active;
MYSQL client;
} mysql_client_wrapper;
#endif

View File

@ -10,7 +10,7 @@ have_func('rb_thread_blocking_region')
# borrowed from mysqlplus
# http://github.com/oldmoe/mysqlplus/blob/master/ext/extconf.rb
dirs = ENV['PATH'].split(':') + %w[
dirs = ENV['PATH'].split(File::PATH_SEPARATOR) + %w[
/opt
/opt/local
/opt/local/mysql
@ -24,7 +24,7 @@ dirs = ENV['PATH'].split(':') + %w[
GLOB = "{#{dirs.join(',')}}/{mysql_config,mysql_config5}"
if /mswin32/ =~ RUBY_PLATFORM
if RUBY_PLATFORM =~ /mswin|mingw/
inc, lib = dir_config('mysql')
exit 1 unless have_library("libmysql")
elsif mc = (with_config('mysql-config') || Dir[GLOB].first) then
@ -57,7 +57,9 @@ end
asplode h unless have_header h
end
$CFLAGS << ' -Wall -funroll-loops'
unless RUBY_PLATFORM =~ /mswin/
$CFLAGS << ' -Wall -funroll-loops'
end
# $CFLAGS << ' -O0 -ggdb3 -Wextra'
create_makefile('mysql2/mysql2')

View File

@ -6,6 +6,7 @@ rb_encoding *binaryEncoding;
VALUE cMysql2Result;
VALUE cBigDecimal, cDate, cDateTime;
VALUE opt_decimal_zero, opt_float_zero, opt_time_year, opt_time_month;
extern VALUE mMysql2, cMysql2Client, cMysql2Error;
static VALUE intern_encoding_from_charset;
static ID intern_new, intern_utc, intern_local, intern_encoding_from_charset_code,
@ -19,6 +20,7 @@ static void rb_mysql_result_mark(void * wrapper) {
if (w) {
rb_gc_mark(w->fields);
rb_gc_mark(w->rows);
rb_gc_mark(w->encoding);
}
}
@ -64,7 +66,7 @@ static VALUE rb_mysql_result_fetch_field(VALUE self, unsigned int idx, short int
MYSQL_FIELD *field = NULL;
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
#endif
field = mysql_fetch_field_direct(wrapper->result, idx);
@ -96,13 +98,14 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
unsigned int i = 0;
unsigned long * fieldLengths;
void * ptr;
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding"));
#endif
GetMysql2Result(self, wrapper);
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
#endif
ptr = wrapper->result;
row = (MYSQL_ROW)rb_thread_blocking_region(nogvl_fetch_row, ptr, RUBY_UBF_IO, 0);
if (row == NULL) {
@ -146,16 +149,27 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
break;
case MYSQL_TYPE_DECIMAL: // DECIMAL or NUMERIC field
case MYSQL_TYPE_NEWDECIMAL: // Precision math DECIMAL or NUMERIC field (MySQL 5.0.3 and up)
val = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new(row[i], fieldLengths[i]));
if (strtod(row[i], NULL) == 0.000000){
val = rb_funcall(cBigDecimal, intern_new, 1, opt_decimal_zero);
}else{
val = rb_funcall(cBigDecimal, intern_new, 1, rb_str_new(row[i], fieldLengths[i]));
}
break;
case MYSQL_TYPE_FLOAT: // FLOAT field
case MYSQL_TYPE_DOUBLE: // DOUBLE or REAL field
val = rb_float_new(strtod(row[i], NULL));
case MYSQL_TYPE_DOUBLE: { // DOUBLE or REAL field
double column_to_double;
column_to_double = strtod(row[i], NULL);
if (column_to_double == 0.000000){
val = opt_float_zero;
}else{
val = rb_float_new(column_to_double);
}
break;
}
case MYSQL_TYPE_TIME: { // TIME field
int hour, min, sec, tokens;
tokens = sscanf(row[i], "%2d:%2d:%2d", &hour, &min, &sec);
val = rb_funcall(rb_cTime, db_timezone, 6, INT2NUM(2000), INT2NUM(1), INT2NUM(1), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
val = rb_funcall(rb_cTime, db_timezone, 6, opt_time_year, opt_time_month, opt_time_month, INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
if (!NIL_P(app_timezone)) {
if (app_timezone == intern_local) {
val = rb_funcall(val, intern_localtime, 0);
@ -327,7 +341,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
} else if (appTz == sym_utc) {
app_timezone = intern_utc;
} else {
app_timezone = intern_local;
app_timezone = Qnil;
}
if (wrapper->lastRowProcessed == 0) {
@ -389,6 +403,7 @@ VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
wrapper->result = r;
wrapper->fields = Qnil;
wrapper->rows = Qnil;
wrapper->encoding = Qnil;
rb_obj_call_init(obj, 0, NULL);
return obj;
}
@ -420,6 +435,13 @@ void init_mysql2_result() {
sym_database_timezone = ID2SYM(rb_intern("database_timezone"));
sym_application_timezone = ID2SYM(rb_intern("application_timezone"));
rb_global_variable(&opt_decimal_zero); //never GC
opt_decimal_zero = rb_str_new2("0.0");
rb_global_variable(&opt_float_zero);
opt_float_zero = rb_float_new((double)0);
opt_time_year = INT2NUM(2000);
opt_time_month = INT2NUM(1);
#ifdef HAVE_RUBY_ENCODING_H
binaryEncoding = rb_enc_find("binary");
#endif

View File

@ -7,6 +7,7 @@ VALUE rb_mysql_result_to_obj(MYSQL_RES * r);
typedef struct {
VALUE fields;
VALUE rows;
VALUE encoding;
unsigned int numberOfFields;
unsigned long numberOfRows;
unsigned long lastRowProcessed;

View File

@ -1,639 +0,0 @@
# encoding: utf-8
require 'mysql2' unless defined? Mysql2
module ActiveRecord
class Base
def self.mysql2_connection(config)
config[:username] = 'root' if config[:username].nil?
client = Mysql2::Client.new(config.symbolize_keys)
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
end
end
module ConnectionAdapters
class Mysql2Column < Column
BOOL = "tinyint(1)"
def extract_default(default)
if sql_type =~ /blob/i || type == :text
if default.blank?
return null ? nil : ''
else
raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
end
elsif missing_default_forged_as_empty_string?(default)
nil
else
super
end
end
def has_default?
return false if sql_type =~ /blob/i || type == :text #mysql forbids defaults on blob and text columns
super
end
# Returns the Ruby class that corresponds to the abstract data type.
def klass
case type
when :integer then Fixnum
when :float then Float
when :decimal then BigDecimal
when :datetime then Time
when :date then Date
when :timestamp then Time
when :time then Time
when :text, :string then String
when :binary then String
when :boolean then Object
end
end
def type_cast(value)
return nil if value.nil?
case type
when :string then value
when :text then value
when :integer then value.to_i rescue value ? 1 : 0
when :float then value.to_f # returns self if it's already a Float
when :decimal then self.class.value_to_decimal(value)
when :datetime, :timestamp then value.class == Time ? value : self.class.string_to_time(value)
when :time then value.class == Time ? value : self.class.string_to_dummy_time(value)
when :date then value.class == Date ? value : self.class.string_to_date(value)
when :binary then value
when :boolean then self.class.value_to_boolean(value)
else value
end
end
def type_cast_code(var_name)
case type
when :string then nil
when :text then nil
when :integer then "#{var_name}.to_i rescue #{var_name} ? 1 : 0"
when :float then "#{var_name}.to_f"
when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
when :datetime, :timestamp then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})"
when :time then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_dummy_time(#{var_name})"
when :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})"
when :binary then nil
when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
else nil
end
end
private
def simplified_type(field_type)
return :boolean if Mysql2Adapter.emulate_booleans && field_type.downcase.index(BOOL)
return :string if field_type =~ /enum/i or field_type =~ /set/i
return :integer if field_type =~ /year/i
return :binary if field_type =~ /bit/i
super
end
def extract_limit(sql_type)
case sql_type
when /blob|text/i
case sql_type
when /tiny/i
255
when /medium/i
16777215
when /long/i
2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
else
super # we could return 65535 here, but we leave it undecorated by default
end
when /^bigint/i; 8
when /^int/i; 4
when /^mediumint/i; 3
when /^smallint/i; 2
when /^tinyint/i; 1
else
super
end
end
# MySQL misreports NOT NULL column default when none is given.
# We can't detect this for columns which may have a legitimate ''
# default (string) but we can for others (integer, datetime, boolean,
# and the rest).
#
# Test whether the column has default '', is not null, and is not
# a type allowing default ''.
def missing_default_forged_as_empty_string?(default)
type != :string && !null && default == ''
end
end
class Mysql2Adapter < AbstractAdapter
cattr_accessor :emulate_booleans
self.emulate_booleans = true
ADAPTER_NAME = 'Mysql2'
PRIMARY = "PRIMARY"
LOST_CONNECTION_ERROR_MESSAGES = [
"Server shutdown in progress",
"Broken pipe",
"Lost connection to MySQL server during query",
"MySQL server has gone away" ]
QUOTED_TRUE, QUOTED_FALSE = '1', '0'
NATIVE_DATABASE_TYPES = {
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int", :limit => 4 },
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
:boolean => { :name => "tinyint", :limit => 1 }
}
def initialize(connection, logger, connection_options, config)
super(connection, logger)
@connection_options, @config = connection_options, config
@quoted_column_names, @quoted_table_names = {}, {}
configure_connection
end
def adapter_name
ADAPTER_NAME
end
def supports_migrations?
true
end
def supports_primary_key?
true
end
def supports_savepoints?
true
end
def native_database_types
NATIVE_DATABASE_TYPES
end
# QUOTING ==================================================
def quote(value, column = nil)
if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
s = column.class.string_to_binary(value).unpack("H*")[0]
"x'#{s}'"
elsif value.kind_of?(BigDecimal)
value.to_s("F")
else
super
end
end
def quote_column_name(name) #:nodoc:
@quoted_column_names[name] ||= "`#{name}`"
end
def quote_table_name(name) #:nodoc:
@quoted_table_names[name] ||= quote_column_name(name).gsub('.', '`.`')
end
def quote_string(string)
@connection.escape(string)
end
def quoted_true
QUOTED_TRUE
end
def quoted_false
QUOTED_FALSE
end
# REFERENTIAL INTEGRITY ====================================
def disable_referential_integrity(&block) #:nodoc:
old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
begin
update("SET FOREIGN_KEY_CHECKS = 0")
yield
ensure
update("SET FOREIGN_KEY_CHECKS = #{old}")
end
end
# CONNECTION MANAGEMENT ====================================
def active?
return false unless @connection
@connection.query 'select 1'
true
rescue Mysql2::Error
false
end
def reconnect!
disconnect!
connect
end
# this is set to true in 2.3, but we don't want it to be
def requires_reloading?
false
end
def disconnect!
unless @connection.nil?
@connection.close
@connection = nil
end
end
def reset!
disconnect!
connect
end
# DATABASE STATEMENTS ======================================
# FIXME: re-enable the following once a "better" query_cache solution is in core
#
# The overrides below perform much better than the originals in AbstractAdapter
# because we're able to take advantage of mysql2's lazy-loading capabilities
#
# # Returns a record hash with the column names as keys and column values
# # as values.
# def select_one(sql, name = nil)
# result = execute(sql, name)
# result.each(:as => :hash) do |r|
# return r
# end
# end
#
# # Returns a single value from a record
# def select_value(sql, name = nil)
# result = execute(sql, name)
# if first = result.first
# first.first
# end
# end
#
# # Returns an array of the values of the first column in a select:
# # select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
# def select_values(sql, name = nil)
# execute(sql, name).map { |row| row.first }
# end
# Returns an array of arrays containing the field values.
# Order is the same as that returned by +columns+.
def select_rows(sql, name = nil)
execute(sql, name).to_a
end
# Executes the SQL statement in the context of this connection.
def execute(sql, name = nil)
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
if name == :skip_logging
@connection.query(sql)
else
log(sql, name) { @connection.query(sql) }
end
rescue ActiveRecord::StatementInvalid => exception
if exception.message.split(":").first =~ /Packets out of order/
raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
else
raise
end
end
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
super
id_value || @connection.last_id
end
alias :create :insert_sql
def update_sql(sql, name = nil)
super
@connection.affected_rows
end
def begin_db_transaction
execute "BEGIN"
rescue Exception
# Transactions aren't supported
end
def commit_db_transaction
execute "COMMIT"
rescue Exception
# Transactions aren't supported
end
def rollback_db_transaction
execute "ROLLBACK"
rescue Exception
# Transactions aren't supported
end
def create_savepoint
execute("SAVEPOINT #{current_savepoint_name}")
end
def rollback_to_savepoint
execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
def release_savepoint
execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end
def add_limit_offset!(sql, options)
limit, offset = options[:limit], options[:offset]
if limit && offset
sql << " LIMIT #{offset.to_i}, #{sanitize_limit(limit)}"
elsif limit
sql << " LIMIT #{sanitize_limit(limit)}"
elsif offset
sql << " OFFSET #{offset.to_i}"
end
sql
end
# SCHEMA STATEMENTS ========================================
def structure_dump
if supports_views?
sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
else
sql = "SHOW TABLES"
end
select_all(sql).inject("") do |structure, table|
table.delete('Table_type')
structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
end
end
def recreate_database(name, options = {})
drop_database(name)
create_database(name, options)
end
# Create a new MySQL database with optional <tt>:charset</tt> and <tt>:collation</tt>.
# Charset defaults to utf8.
#
# Example:
# create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
# create_database 'matt_development'
# create_database 'matt_development', :charset => :big5
def create_database(name, options = {})
if options[:collation]
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
else
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
end
end
def drop_database(name) #:nodoc:
execute "DROP DATABASE IF EXISTS `#{name}`"
end
def current_database
select_value 'SELECT DATABASE() as db'
end
# Returns the database character set.
def charset
show_variable 'character_set_database'
end
# Returns the database collation strategy.
def collation
show_variable 'collation_database'
end
def tables(name = nil)
tables = []
execute("SHOW TABLES", name).each do |field|
tables << field.first
end
tables
end
def drop_table(table_name, options = {})
super(table_name, options)
end
def indexes(table_name, name = nil)
indexes = []
current_index = nil
result = execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name)
result.each(:symbolize_keys => true, :as => :hash) do |row|
if current_index != row[:Key_name]
next if row[:Key_name] == PRIMARY # skip the primary key
current_index = row[:Key_name]
indexes << IndexDefinition.new(row[:Table], row[:Key_name], row[:Non_unique] == 0, [])
end
indexes.last.columns << row[:Column_name]
end
indexes
end
def columns(table_name, name = nil)
sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
columns = []
result = execute(sql, :skip_logging)
result.each(:symbolize_keys => true, :as => :hash) { |field|
columns << Mysql2Column.new(field[:Field], field[:Default], field[:Type], field[:Null] == "YES")
}
columns
end
def create_table(table_name, options = {})
super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
end
def rename_table(table_name, new_name)
execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
end
def add_column(table_name, column_name, type, options = {})
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(add_column_sql, options)
add_column_position!(add_column_sql, options)
execute(add_column_sql)
end
def change_column_default(table_name, column_name, default)
column = column_for(table_name, column_name)
change_column table_name, column_name, column.sql_type, :default => default
end
def change_column_null(table_name, column_name, null, default = nil)
column = column_for(table_name, column_name)
unless null || default.nil?
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
end
change_column table_name, column_name, column.sql_type, :null => null
end
def change_column(table_name, column_name, type, options = {})
column = column_for(table_name, column_name)
unless options_include_default?(options)
options[:default] = column.default
end
unless options.has_key?(:null)
options[:null] = column.null
end
change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(change_column_sql, options)
add_column_position!(change_column_sql, options)
execute(change_column_sql)
end
def rename_column(table_name, column_name, new_column_name)
options = {}
if column = columns(table_name).find { |c| c.name == column_name.to_s }
options[:default] = column.default
options[:null] = column.null
else
raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
end
current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
add_column_options!(rename_column_sql, options)
execute(rename_column_sql)
end
# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)' # compatibility with MySQL default
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
end
def add_column_position!(sql, options)
if options[:first]
sql << " FIRST"
elsif options[:after]
sql << " AFTER #{quote_column_name(options[:after])}"
end
end
def show_variable(name)
variables = select_all("SHOW VARIABLES LIKE '#{name}'")
variables.first['Value'] unless variables.empty?
end
def pk_and_sequence_for(table)
keys = []
result = execute("describe #{quote_table_name(table)}")
result.each(:symbolize_keys => true, :as => :hash) do |row|
keys << row[:Field] if row[:Key] == "PRI"
end
keys.length == 1 ? [keys.first, nil] : nil
end
# Returns just a table's primary key
def primary_key(table)
pk_and_sequence = pk_and_sequence_for(table)
pk_and_sequence && pk_and_sequence.first
end
def case_sensitive_equality_operator
"= BINARY"
end
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end
protected
def quoted_columns_for_index(column_names, options = {})
length = options[:length] if options.is_a?(Hash)
quoted_column_names = case length
when Hash
column_names.map {|name| length[name] ? "#{quote_column_name(name)}(#{length[name]})" : quote_column_name(name) }
when Fixnum
column_names.map {|name| "#{quote_column_name(name)}(#{length})"}
else
column_names.map {|name| quote_column_name(name) }
end
end
def translate_exception(exception, message)
return super unless exception.respond_to?(:error_number)
case exception.error_number
when 1062
RecordNotUnique.new(message, exception)
when 1452
InvalidForeignKey.new(message, exception)
else
super
end
end
private
def connect
@connection = Mysql2::Client.new(@config)
configure_connection
end
def configure_connection
@connection.query_options.merge!(:as => :array)
encoding = @config[:encoding]
execute("SET NAMES '#{encoding}'", :skip_logging) if encoding
# By default, MySQL 'where id is null' selects the last inserted id.
# Turn this off. http://dev.rubyonrails.org/ticket/6778
execute("SET SQL_AUTO_IS_NULL=0", :skip_logging)
end
# Returns an array of record hashes with the column names as keys and
# column values as values.
def select(sql, name = nil)
execute(sql, name).each(:as => :hash)
end
def supports_views?
version[0] >= 5
end
def version
@version ||= @connection.info[:version].scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
end
def column_for(table_name, column_name)
unless column = columns(table_name).find { |c| c.name == column_name.to_s }
raise "No such column: #{table_name}.#{column_name}"
end
column
end
end
end
end

View File

@ -12,7 +12,6 @@ module Mysql2
def initialize(opts = {})
@query_options = @@default_query_options.dup
@active = false
init_connection
@ -21,7 +20,6 @@ module Mysql2
send(:"#{key}=", opts[key])
end
# force the encoding to utf8
@encoding = nil
self.charset_name = opts[:encoding] || 'utf8'
ssl_set(*opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslciper))

View File

@ -37,7 +37,6 @@ Gem::Specification.new do |s|
"ext/mysql2/result.c",
"ext/mysql2/result.h",
"lib/active_record/connection_adapters/em_mysql2_adapter.rb",
"lib/active_record/connection_adapters/mysql2_adapter.rb",
"lib/active_record/fiber_patches.rb",
"lib/arel/engines/sql/compilers/mysql2_compiler.rb",
"lib/mysql2.rb",
@ -45,9 +44,7 @@ Gem::Specification.new do |s|
"lib/mysql2/em.rb",
"lib/mysql2/error.rb",
"lib/mysql2/result.rb",
"lib/sequel/adapters/mysql2.rb",
"mysql2.gemspec",
"spec/active_record/active_record_spec.rb",
"spec/em/em_spec.rb",
"spec/mysql2/client_spec.rb",
"spec/mysql2/error_spec.rb",
@ -62,8 +59,7 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.summary = %q{A simple, fast Mysql library for Ruby, binding to libmysql}
s.test_files = [
"spec/active_record/active_record_spec.rb",
"spec/em/em_spec.rb",
"spec/em/em_spec.rb",
"spec/mysql2/client_spec.rb",
"spec/mysql2/error_spec.rb",
"spec/mysql2/result_spec.rb",

View File

@ -1,150 +0,0 @@
# encoding: UTF-8
require 'spec_helper'
require 'rubygems'
require 'active_record'
require 'active_record/connection_adapters/mysql2_adapter'
class Mysql2Test2 < ActiveRecord::Base
set_table_name "mysql2_test2"
end
describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
it "should be able to connect" do
lambda {
ActiveRecord::Base.establish_connection(:adapter => 'mysql2')
}.should_not raise_error(Mysql2::Error)
end
context "once connected" do
before(:each) do
@connection = ActiveRecord::Base.connection
end
it "should be able to execute a raw query" do
@connection.execute("SELECT 1 as one").first.first.should eql(1)
@connection.execute("SELECT NOW() as n").first.first.class.should eql(Time)
end
end
context "columns" do
before(:all) do
ActiveRecord::Base.default_timezone = :local
ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test')
Mysql2Test2.connection.execute %[
CREATE TABLE IF NOT EXISTS mysql2_test2 (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`null_test` varchar(10) DEFAULT NULL,
`bit_test` bit(64) NOT NULL DEFAULT b'1',
`boolean_test` tinyint(1) DEFAULT 0,
`tiny_int_test` tinyint(4) NOT NULL DEFAULT '1',
`small_int_test` smallint(6) NOT NULL DEFAULT '1',
`medium_int_test` mediumint(9) NOT NULL DEFAULT '1',
`int_test` int(11) NOT NULL DEFAULT '1',
`big_int_test` bigint(20) NOT NULL DEFAULT '1',
`float_test` float(10,3) NOT NULL DEFAULT '1.000',
`double_test` double(10,3) NOT NULL DEFAULT '1.000',
`decimal_test` decimal(10,3) NOT NULL DEFAULT '1.000',
`date_test` date NOT NULL DEFAULT '2010-01-01',
`date_time_test` datetime NOT NULL DEFAULT '2010-01-01 00:00:00',
`timestamp_test` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`time_test` time NOT NULL DEFAULT '00:00:00',
`year_test` year(4) NOT NULL DEFAULT '2010',
`char_test` char(10) NOT NULL DEFAULT 'abcdefghij',
`varchar_test` varchar(10) NOT NULL DEFAULT 'abcdefghij',
`binary_test` binary(10) NOT NULL DEFAULT 'abcdefghij',
`varbinary_test` varbinary(10) NOT NULL DEFAULT 'abcdefghij',
`tiny_blob_test` tinyblob NOT NULL,
`tiny_text_test` tinytext,
`blob_test` blob,
`text_test` text,
`medium_blob_test` mediumblob,
`medium_text_test` mediumtext,
`long_blob_test` longblob,
`long_text_test` longtext,
`enum_test` enum('val1','val2') NOT NULL DEFAULT 'val1',
`set_test` set('val1','val2') NOT NULL DEFAULT 'val1,val2',
PRIMARY KEY (`id`)
)
]
Mysql2Test2.connection.execute "INSERT INTO mysql2_test2 (null_test) VALUES (NULL)"
@test_result = Mysql2Test2.connection.execute("SELECT * FROM mysql2_test2 ORDER BY id DESC LIMIT 1").first
end
after(:all) do
Mysql2Test2.connection.execute("DELETE FROM mysql2_test WHERE id=#{@test_result.first}")
end
it "default value should be cast to the expected type of the field" do
test = Mysql2Test2.new
test.null_test.should be_nil
test.bit_test.should eql("b'1'")
test.boolean_test.should eql(false)
test.tiny_int_test.should eql(1)
test.small_int_test.should eql(1)
test.medium_int_test.should eql(1)
test.int_test.should eql(1)
test.big_int_test.should eql(1)
test.float_test.should eql('1.0000'.to_f)
test.double_test.should eql('1.0000'.to_f)
test.decimal_test.should eql(BigDecimal.new('1.0000'))
test.date_test.should eql(Date.parse('2010-01-01'))
test.date_time_test.should eql(Time.local(2010,1,1,0,0,0))
test.timestamp_test.should be_nil
test.time_test.class.should eql(Time)
test.year_test.should eql(2010)
test.char_test.should eql('abcdefghij')
test.varchar_test.should eql('abcdefghij')
test.binary_test.should eql('abcdefghij')
test.varbinary_test.should eql('abcdefghij')
test.tiny_blob_test.should eql("")
test.tiny_text_test.should be_nil
test.blob_test.should be_nil
test.text_test.should be_nil
test.medium_blob_test.should be_nil
test.medium_text_test.should be_nil
test.long_blob_test.should be_nil
test.long_text_test.should be_nil
test.long_blob_test.should be_nil
test.enum_test.should eql('val1')
test.set_test.should eql('val1,val2')
test.save
end
it "should have correct values when pulled from a db record" do
test = Mysql2Test2.last
test.null_test.should be_nil
test.bit_test.class.should eql(String)
test.boolean_test.should eql(false)
test.tiny_int_test.should eql(1)
test.small_int_test.should eql(1)
test.medium_int_test.should eql(1)
test.int_test.should eql(1)
test.big_int_test.should eql(1)
test.float_test.should eql('1.0000'.to_f)
test.double_test.should eql('1.0000'.to_f)
test.decimal_test.should eql(BigDecimal.new('1.0000'))
test.date_test.should eql(Date.parse('2010-01-01'))
test.date_time_test.should eql(Time.local(2010,1,1,0,0,0))
test.timestamp_test.class.should eql(ActiveSupport::TimeWithZone) if RUBY_VERSION >= "1.9"
test.timestamp_test.class.should eql(Time) if RUBY_VERSION < "1.9"
test.time_test.class.should eql(Time)
test.year_test.should eql(2010)
test.char_test.should eql('abcdefghij')
test.varchar_test.should eql('abcdefghij')
test.binary_test.should eql('abcdefghij')
test.varbinary_test.should eql('abcdefghij')
test.tiny_blob_test.should eql("")
test.tiny_text_test.should be_nil
test.blob_test.should be_nil
test.text_test.should be_nil
test.medium_blob_test.should be_nil
test.medium_text_test.should be_nil
test.long_blob_test.should be_nil
test.long_text_test.should be_nil
test.long_blob_test.should be_nil
test.enum_test.should eql('val1')
test.set_test.should eql('val1,val2')
end
end
end

View File

@ -176,6 +176,20 @@ describe Mysql2::Client do
}.should_not raise_error(Mysql2::Error)
end
it "threaded queries should be supported" do
threads, results = [], {}
connect = lambda{ Mysql2::Client.new(:host => "localhost", :username => "root") }
Timeout.timeout(0.7) do
5.times {
threads << Thread.new do
results[Thread.current.object_id] = connect.call.query("SELECT sleep(0.5) as result")
end
}
end
threads.each{|t| t.join }
results.keys.sort.should eql(threads.map{|t| t.object_id }.sort)
end
it "evented async queries should be supported" do
# should immediately return nil
@client.query("SELECT sleep(0.1)", :async => true).should eql(nil)

View File

@ -3,6 +3,7 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/..')
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'mysql2'
require 'timeout'
Spec::Runner.configure do |config|
config.before(:all) do
@ -19,8 +20,10 @@ Spec::Runner.configure do |config|
int_test INT,
big_int_test BIGINT,
float_test FLOAT(10,3),
float_zero_test FLOAT(10,3),
double_test DOUBLE(10,3),
decimal_test DECIMAL(10,3),
decimal_zero_test DECIMAL(10,3),
date_test DATE,
date_time_test DATETIME,
timestamp_test TIMESTAMP,
@ -46,7 +49,7 @@ Spec::Runner.configure do |config|
client.query %[
INSERT INTO mysql2_test (
null_test, bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
float_test, double_test, decimal_test, date_test, date_time_test, timestamp_test, time_test,
float_test, float_zero_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
long_blob_test, long_text_test, enum_test, set_test
@ -54,7 +57,7 @@ Spec::Runner.configure do |config|
VALUES (
NULL, b'101', 1, 1, 10, 10, 10, 10,
10.3, 10.3, 10.3, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
10.3, 0, 10.3, 10.3, 0, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
2009, "test", "test", "test", "test", "test",
"test", "test", "test", "test", "test",
"test", "test", 'val1', 'val1,val2'

8
tasks/benchmarks.rake Normal file
View File

@ -0,0 +1,8 @@
namespace :bench do
[ :active_record, :escape, :query_with_mysql_casting,
:query_without_mysql_casting, :sequel, :allocations,
:thread_alone].each do |feature|
desc "Run #{feature} benchmarks"
task(feature){ ruby "benchmark/#{feature}.rb" }
end
end

19
tasks/compile.rake Normal file
View File

@ -0,0 +1,19 @@
gem 'rake-compiler', '~> 0.7.1'
require "rake/extensiontask"
MYSQL_VERSION = "5.1.49"
MYSQL_MIRROR = ENV['MYSQL_MIRROR'] || "http://mysql.localhost.net.ar"
Rake::ExtensionTask.new("mysql2", JEWELER.gemspec) do |ext|
# reference where the vendored MySQL got extracted
mysql_lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor', "mysql-#{MYSQL_VERSION}-win32"))
# automatically add build options to avoid need of manual input
if RUBY_PLATFORM =~ /mswin|mingw/ then
ext.config_options << "--with-mysql-include=#{mysql_lib}/include"
ext.config_options << "--with-mysql-lib=#{mysql_lib}/lib/opt"
end
ext.lib_dir = File.join 'lib', 'mysql2'
end
Rake::Task[:spec].prerequisites << :compile

41
tasks/vendor_mysql.rake Normal file
View File

@ -0,0 +1,41 @@
require 'rake/clean'
require 'rake/extensioncompiler'
# download mysql library and headers
directory "vendor"
file "vendor/mysql-noinstall-#{MYSQL_VERSION}-win32.zip" => ['vendor'] do |t|
base_version = MYSQL_VERSION.gsub(/\.[0-9]+$/, '')
url = "http://dev.mysql.com/get/Downloads/MySQL-#{base_version}/#{File.basename(t.name)}/from/#{MYSQL_MIRROR}/"
when_writing "downloading #{t.name}" do
cd File.dirname(t.name) do
sh "wget -c #{url} || curl -C - -O #{url}"
end
end
end
file "vendor/mysql-#{MYSQL_VERSION}-win32/include/mysql.h" => ["vendor/mysql-noinstall-#{MYSQL_VERSION}-win32.zip"] do |t|
full_file = File.expand_path(t.prerequisites.last)
when_writing "creating #{t.name}" do
cd "vendor" do
sh "unzip #{full_file} mysql-#{MYSQL_VERSION}-win32/bin/** mysql-#{MYSQL_VERSION}-win32/include/** mysql-#{MYSQL_VERSION}-win32/lib/**"
end
# update file timestamp to avoid Rake perform this extraction again.
touch t.name
end
end
# clobber expanded packages
CLOBBER.include("vendor/mysql-#{MYSQL_VERSION}-win32")
# vendor:mysql
task 'vendor:mysql' => ["vendor/mysql-#{MYSQL_VERSION}-win32/include/mysql.h"]
# hook into cross compilation vendored mysql dependency
if RUBY_PLATFORM =~ /mingw|mswin/ then
Rake::Task['compile'].prerequisites.unshift 'vendor:mysql'
else
if Rake::Task.tasks.map {|t| t.name }.include? 'cross'
Rake::Task['cross'].prerequisites.unshift 'vendor:mysql'
end
end