From c3c5c8f85a3e262b27e65d383f4a6e3e47713a9d Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Fri, 6 Aug 2010 17:05:53 -0700 Subject: [PATCH 01/22] update gemspec now that the Sequel adapter is gone --- mysql2.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/mysql2.gemspec b/mysql2.gemspec index 318814b..3cd6509 100644 --- a/mysql2.gemspec +++ b/mysql2.gemspec @@ -45,7 +45,6 @@ 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", From 5a9ca9c76f52fe4503285dd17b146b988f4d3dc6 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 9 Aug 2010 14:50:58 -0700 Subject: [PATCH 02/22] app_timezone defaults to nil --- ext/mysql2/result.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c index 6baa678..63be08f 100644 --- a/ext/mysql2/result.c +++ b/ext/mysql2/result.c @@ -327,7 +327,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) { From 0bab31a61f63e86aa285f523ad4ef3de6b84e979 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 9 Aug 2010 14:52:32 -0700 Subject: [PATCH 03/22] mysql2 adapter moved into AR3 core --- .../connection_adapters/mysql2_adapter.rb | 639 ------------------ mysql2.gemspec | 1 - 2 files changed, 640 deletions(-) delete mode 100644 lib/active_record/connection_adapters/mysql2_adapter.rb diff --git a/lib/active_record/connection_adapters/mysql2_adapter.rb b/lib/active_record/connection_adapters/mysql2_adapter.rb deleted file mode 100644 index 5687597..0000000 --- a/lib/active_record/connection_adapters/mysql2_adapter.rb +++ /dev/null @@ -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 :charset and :collation. - # 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 diff --git a/mysql2.gemspec b/mysql2.gemspec index 3cd6509..ed31d19 100644 --- a/mysql2.gemspec +++ b/mysql2.gemspec @@ -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", From 254f42f502b91eee6551079c7a28c4c2b4076345 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 9 Aug 2010 22:11:47 -0700 Subject: [PATCH 04/22] no need for AR specs since the adapter was moved to Rails core --- mysql2.gemspec | 4 +- spec/active_record/active_record_spec.rb | 150 ----------------------- 2 files changed, 1 insertion(+), 153 deletions(-) delete mode 100644 spec/active_record/active_record_spec.rb diff --git a/mysql2.gemspec b/mysql2.gemspec index ed31d19..4fac216 100644 --- a/mysql2.gemspec +++ b/mysql2.gemspec @@ -45,7 +45,6 @@ Gem::Specification.new do |s| "lib/mysql2/error.rb", "lib/mysql2/result.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", @@ -60,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", diff --git a/spec/active_record/active_record_spec.rb b/spec/active_record/active_record_spec.rb deleted file mode 100644 index 055db0e..0000000 --- a/spec/active_record/active_record_spec.rb +++ /dev/null @@ -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 From 4c27a1e4fcd8be4a11f5ba2bee416e0968bca182 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 11 Aug 2010 22:45:52 +0800 Subject: [PATCH 05/22] extconf detect properly mingw. --- ext/mysql2/extconf.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ext/mysql2/extconf.rb b/ext/mysql2/extconf.rb index bc48e6b..c8806d0 100644 --- a/ext/mysql2/extconf.rb +++ b/ext/mysql2/extconf.rb @@ -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') From 5fb043b4daaf41369a6e5537e61626115a23429a Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 11 Aug 2010 22:51:27 +0800 Subject: [PATCH 06/22] Require a newer rake-compiler for cross-compilation. --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index edecf8d..39f4acf 100644 --- a/Rakefile +++ b/Rakefile @@ -20,7 +20,7 @@ end require 'rake' require 'spec/rake/spectask' -gem 'rake-compiler', '>= 0.4.1' +gem 'rake-compiler', '~> 0.7.1' require "rake/extensiontask" desc "Run all examples with RCov" From db64470831d4de0d841cbeed55028a46f9c47fca Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Wed, 11 Aug 2010 23:40:55 +0800 Subject: [PATCH 07/22] Quick hack from mysql gem. Added vendored MySQL. --- .gitignore | 2 ++ Rakefile | 8 ++------ tasks/compile.rake | 19 +++++++++++++++++++ tasks/vendor_mysql.rake | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tasks/compile.rake create mode 100644 tasks/vendor_mysql.rake diff --git a/.gitignore b/.gitignore index e4d4e35..07fb61a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ Makefile *.rbc mkmf.log pkg/ +tmp +vendor diff --git a/Rakefile b/Rakefile index 39f4acf..9f497de 100644 --- a/Rakefile +++ b/Rakefile @@ -20,8 +20,6 @@ end require 'rake' require 'spec/rake/spectask' -gem 'rake-compiler', '~> 0.7.1' -require "rake/extensiontask" desc "Run all examples with RCov" Spec::Rake::SpecTask.new('spec:rcov') do |t| @@ -36,7 +34,5 @@ Spec::Rake::SpecTask.new('spec') do |t| t.spec_opts << '--options' << 'spec/spec.opts' end -Rake::ExtensionTask.new("mysql2", JEWELER.gemspec) do |ext| - ext.lib_dir = File.join 'lib', 'mysql2' -end -Rake::Task[:spec].prerequisites << :compile +# Load custom tasks +Dir['tasks/*.rake'].sort.each { |f| load f } diff --git a/tasks/compile.rake b/tasks/compile.rake new file mode 100644 index 0000000..712eb17 --- /dev/null +++ b/tasks/compile.rake @@ -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 diff --git a/tasks/vendor_mysql.rake b/tasks/vendor_mysql.rake new file mode 100644 index 0000000..b2eaeeb --- /dev/null +++ b/tasks/vendor_mysql.rake @@ -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 From b15ddc4f7518b11d562e16fee3ed04a4e8cf6b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Fri, 6 Aug 2010 23:54:24 +0100 Subject: [PATCH 08/22] Let spec be the default rake task --- Rakefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Rakefile b/Rakefile index 9f497de..7a1c103 100644 --- a/Rakefile +++ b/Rakefile @@ -34,5 +34,7 @@ Spec::Rake::SpecTask.new('spec') do |t| t.spec_opts << '--options' << 'spec/spec.opts' end +task :default => :spec + # Load custom tasks Dir['tasks/*.rake'].sort.each { |f| load f } From e250e337cfccf8389b9539fdfe09664eb598c3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 00:01:35 +0100 Subject: [PATCH 09/22] Define rake tasks for running benchmarks --- Rakefile | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Rakefile b/Rakefile index 7a1c103..55f2498 100644 --- a/Rakefile +++ b/Rakefile @@ -36,5 +36,18 @@ end task :default => :spec +def define_bench_task(feature) + desc "Run #{feature} benchmarks" + task(feature){ ruby "benchmark/#{feature}.rb" } +end + +namespace :bench do + define_bench_task :active_record + define_bench_task :escape + define_bench_task :query_with_mysql_casting + define_bench_task :query_without_mysql_casting + define_bench_task :sequel +end + # Load custom tasks Dir['tasks/*.rake'].sort.each { |f| load f } From 931765ee05eefedfdd05f15ac702fa381aecc48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 00:13:39 +0100 Subject: [PATCH 10/22] Extract a GET_ENCODING macro --- ext/mysql2/client.c | 10 +++++----- ext/mysql2/mysql2_ext.h | 3 +++ ext/mysql2/result.c | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 3a06ed1..3cbd7b2 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -239,7 +239,7 @@ 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")); + rb_iv_set(resultObj, "@encoding", GET_ENCODING(self)); #endif return resultObj; } @@ -278,7 +278,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(GET_ENCODING(self)); // ensure the string is in the encoding the connection is expecting args.sql = rb_str_export_to_enc(args.sql, conn_enc); #endif @@ -324,7 +324,7 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) { 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(GET_ENCODING(self)); // ensure the string is in the encoding the connection is expecting str = rb_str_export_to_enc(str, conn_enc); #endif @@ -355,7 +355,7 @@ static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE self) { VALUE version = rb_hash_new(), client_info; #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(GET_ENCODING(self)); #endif rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version())); @@ -375,7 +375,7 @@ static VALUE rb_mysql_client_server_info(VALUE self) { VALUE version, server_info; #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(GET_ENCODING(self)); #endif Data_Get_Struct(self, MYSQL, client); diff --git a/ext/mysql2/mysql2_ext.h b/ext/mysql2/mysql2_ext.h index e01e092..e2b4e6e 100644 --- a/ext/mysql2/mysql2_ext.h +++ b/ext/mysql2/mysql2_ext.h @@ -29,4 +29,7 @@ #include #include +#define GET_ENCODING(self) \ + rb_iv_get(self, "@encoding") + #endif diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c index 63be08f..869f121 100644 --- a/ext/mysql2/result.c +++ b/ext/mysql2/result.c @@ -64,7 +64,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(GET_ENCODING(self)); #endif field = mysql_fetch_field_direct(wrapper->result, idx); @@ -98,7 +98,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo 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")); + rb_encoding *conn_enc = rb_to_encoding(GET_ENCODING(self)); #endif GetMysql2Result(self, wrapper); From f601c3cef8837b38b9f9f67b23620ea858bf3321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 01:00:41 +0100 Subject: [PATCH 11/22] Skip additional string length access in rb_mysql_client_escape and benchmark escaping "clean" strings as well --- benchmark/escape.rb | 50 +++++++++++++++++++++++---------------------- ext/mysql2/client.c | 2 +- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/benchmark/escape.rb b/benchmark/escape.rb index d52d2bc..1b159da 100644 --- a/benchmark/escape.rb +++ b/benchmark/escape.rb @@ -7,31 +7,33 @@ require 'mysql' require 'mysql2' require 'do_mysql' -number_of = 1000 -str = "abc'def\"ghi\0jkl%mno" +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 -Benchmark.bmbm do |x| - mysql = Mysql.new("localhost", "root") - x.report do - puts "Mysql" - number_of.times do - mysql.quote 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 + + 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 - mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root") - x.report do - puts "Mysql2" - number_of.times do - mysql2.escape str - 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 - end - end -end \ No newline at end of file +run_escape_benchmarks "abc'def\"ghi\0jkl%mno" +run_escape_benchmarks "clean string" \ No newline at end of file diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 3cbd7b2..9451554 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -335,7 +335,7 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) { 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; From 80810f7e432fc513c2a0cc2f4c4ed5599cd85d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 01:15:37 +0100 Subject: [PATCH 12/22] Extract a GET_CLIENT mactro to cleanup inline Data_Get_Struct etc. on methods that require client access --- ext/mysql2/client.c | 53 +++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 9451554..1ce805e 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -16,6 +16,10 @@ static ID intern_merge; #define MARK_CONN_INACTIVE(conn) \ rb_iv_set(conn, "@active", Qfalse); +#define GET_CLIENT(self) \ + MYSQL * client; \ + Data_Get_Struct(self, MYSQL, client); + /* * used to pass all arguments to mysql_real_connect while inside * rb_thread_blocking_region @@ -137,10 +141,8 @@ static VALUE allocate(VALUE klass) { } 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 +168,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 +210,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) { @@ -250,9 +248,8 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) { int fd, retval; int async = 0; VALUE opts, defaults, active; - MYSQL *client; + GET_CLIENT(self) - Data_Get_Struct(self, MYSQL, client); REQUIRE_OPEN_DB(client); args.mysql = client; @@ -317,9 +314,9 @@ 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 @@ -332,8 +329,6 @@ 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), oldLen); if (newLen == oldLen) { @@ -371,14 +366,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(GET_ENCODING(self)); #endif - Data_Get_Struct(self, MYSQL, client); REQUIRE_OPEN_DB(client); version = rb_hash_new(); @@ -395,31 +389,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 +424,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,9 +441,7 @@ 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; @@ -482,8 +467,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 +482,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? */ From 0190457dbd6d963de1a16a3c5165ad346a0571d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 01:35:07 +0100 Subject: [PATCH 13/22] Skip rb_thread_select overhead if we're running in single threaded mode --- ext/mysql2/client.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 1ce805e..78bd42e 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -290,11 +290,13 @@ 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; + int(*selector)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; + 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); From 9a63a587c01e4ac116ba79e9cc23a3b3fddf589d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 21:33:52 +0100 Subject: [PATCH 14/22] Declare the selector function pointer as per ISO C90 spec + address brian's concern / comment re. rb_thread_alone() --- ext/mysql2/client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 78bd42e..8e2577b 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -248,6 +248,7 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) { int fd, retval; int async = 0; VALUE opts, defaults, active; + int(*selector)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; GET_CLIENT(self) REQUIRE_OPEN_DB(client); @@ -290,8 +291,7 @@ 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; - int(*selector)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; - selector = rb_thread_alone ? *select : *rb_thread_select; + selector = rb_thread_alone() ? *select : *rb_thread_select; for(;;) { FD_ZERO(&fdset); FD_SET(fd, &fdset); From 9f19b958b0cb06ac397ff1311f1d6bffe8d16bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 21:35:00 +0100 Subject: [PATCH 15/22] Add a threaded example as well --- examples/threaded.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 examples/threaded.rb diff --git a/examples/threaded.rb b/examples/threaded.rb new file mode 100644 index 0000000..7d3b961 --- /dev/null +++ b/examples/threaded.rb @@ -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 \ No newline at end of file From c5d9b7ff65dde5164a1f88394f1aadce792c3759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 21:52:46 +0100 Subject: [PATCH 16/22] Introduce test case for Mysql2::Client in multi-threaded environments --- spec/mysql2/client_spec.rb | 14 ++++++++++++++ spec/spec_helper.rb | 1 + 2 files changed, 15 insertions(+) diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index a6ff3c7..7c5c8ce 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -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) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b50e6cc..0063bb7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 From c808e78028321898c64ec4eede72fe097e3ecadb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 22:00:41 +0100 Subject: [PATCH 17/22] Intern error_number= && sql_state= as well --- ext/mysql2/client.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 8e2577b..258f3fb 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -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) { \ @@ -68,8 +68,8 @@ struct nogvl_send_query_args { 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; } @@ -526,4 +526,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="); + } \ No newline at end of file From d9153b82fcc013627bb9d3d7b012625e8b9e0876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sun, 8 Aug 2010 18:33:55 +0100 Subject: [PATCH 18/22] Save on coercion overhead for zero value decimal and float column values --- benchmark/setup_db.rb | 8 ++++++-- ext/mysql2/result.c | 20 ++++++++++++++++++-- spec/spec_helper.rb | 6 ++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/benchmark/setup_db.rb b/benchmark/setup_db.rb index 4f1cfe7..df3cbf2 100644 --- a/benchmark/setup_db.rb +++ b/benchmark/setup_db.rb @@ -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', diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c index 869f121..d3ce673 100644 --- a/ext/mysql2/result.c +++ b/ext/mysql2/result.c @@ -6,6 +6,7 @@ rb_encoding *binaryEncoding; VALUE cMysql2Result; VALUE cBigDecimal, cDate, cDateTime; +VALUE opt_decimal_zero, opt_float_zero; extern VALUE mMysql2, cMysql2Client, cMysql2Error; static VALUE intern_encoding_from_charset; static ID intern_new, intern_utc, intern_local, intern_encoding_from_charset_code, @@ -95,6 +96,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo MYSQL_FIELD * fields = NULL; unsigned int i = 0; unsigned long * fieldLengths; + double column_to_double; void * ptr; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); @@ -146,11 +148,20 @@ 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)); + 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; @@ -420,6 +431,11 @@ 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); + #ifdef HAVE_RUBY_ENCODING_H binaryEncoding = rb_enc_find("binary"); #endif diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0063bb7..49c3b78 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,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, @@ -47,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 @@ -55,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' From 2609783aeb8dc6379f3de00250fc8f1400712ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sun, 8 Aug 2010 23:02:28 +0100 Subject: [PATCH 19/22] Avoid INT2NUM overhead when coercing date specific elements for MYSQL_TYPE_TIME + introduce basic infrastructure for measuring GC overhead --- Rakefile | 2 +- benchmark/allocations.rb | 33 +++++++++++++++++++++++++++++++++ ext/mysql2/result.c | 11 +++++++---- 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 benchmark/allocations.rb diff --git a/Rakefile b/Rakefile index 55f2498..ad0a387 100644 --- a/Rakefile +++ b/Rakefile @@ -47,7 +47,7 @@ namespace :bench do define_bench_task :query_with_mysql_casting define_bench_task :query_without_mysql_casting define_bench_task :sequel + define_bench_task :allocations end - # Load custom tasks Dir['tasks/*.rake'].sort.each { |f| load f } diff --git a/benchmark/allocations.rb b/benchmark/allocations.rb new file mode 100644 index 0000000..cf0c931 --- /dev/null +++ b/benchmark/allocations.rb @@ -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 \ No newline at end of file diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c index d3ce673..93f2990 100644 --- a/ext/mysql2/result.c +++ b/ext/mysql2/result.c @@ -6,7 +6,7 @@ rb_encoding *binaryEncoding; VALUE cMysql2Result; VALUE cBigDecimal, cDate, cDateTime; -VALUE opt_decimal_zero, opt_float_zero; +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, @@ -96,7 +96,6 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo MYSQL_FIELD * fields = NULL; unsigned int i = 0; unsigned long * fieldLengths; - double column_to_double; void * ptr; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); @@ -155,7 +154,8 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo } break; case MYSQL_TYPE_FLOAT: // FLOAT field - case MYSQL_TYPE_DOUBLE: // DOUBLE or REAL field + 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; @@ -163,10 +163,11 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo 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); @@ -435,6 +436,8 @@ void init_mysql2_result() { 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"); From 10ee0256473fd0ca9406fb37ed79ab6117baa37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sun, 8 Aug 2010 23:49:44 +0100 Subject: [PATCH 20/22] Benchmark for select VS rb_thread_select --- Rakefile | 4 ++-- benchmark/thread_alone.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 benchmark/thread_alone.rb diff --git a/Rakefile b/Rakefile index ad0a387..60fb02a 100644 --- a/Rakefile +++ b/Rakefile @@ -48,6 +48,6 @@ namespace :bench do define_bench_task :query_without_mysql_casting define_bench_task :sequel define_bench_task :allocations -end -# Load custom tasks + define_bench_task :thread_alone +end# Load custom tasks Dir['tasks/*.rake'].sort.each { |f| load f } diff --git a/benchmark/thread_alone.rb b/benchmark/thread_alone.rb new file mode 100644 index 0000000..c6b4c1e --- /dev/null +++ b/benchmark/thread_alone.rb @@ -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 \ No newline at end of file From 3239a449b9b3ff6e262658c6a553ee4a02468373 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Wed, 11 Aug 2010 11:29:10 -0700 Subject: [PATCH 21/22] move and slightly refactor benchmark rake task --- Rakefile | 15 +-------------- tasks/benchmarks.rake | 8 ++++++++ 2 files changed, 9 insertions(+), 14 deletions(-) create mode 100644 tasks/benchmarks.rake diff --git a/Rakefile b/Rakefile index 60fb02a..7a1c103 100644 --- a/Rakefile +++ b/Rakefile @@ -36,18 +36,5 @@ end task :default => :spec -def define_bench_task(feature) - desc "Run #{feature} benchmarks" - task(feature){ ruby "benchmark/#{feature}.rb" } -end - -namespace :bench do - define_bench_task :active_record - define_bench_task :escape - define_bench_task :query_with_mysql_casting - define_bench_task :query_without_mysql_casting - define_bench_task :sequel - define_bench_task :allocations - define_bench_task :thread_alone -end# Load custom tasks +# Load custom tasks Dir['tasks/*.rake'].sort.each { |f| load f } diff --git a/tasks/benchmarks.rake b/tasks/benchmarks.rake new file mode 100644 index 0000000..8ec2236 --- /dev/null +++ b/tasks/benchmarks.rake @@ -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 \ No newline at end of file From cebf9af0684335d8ffd542908409a8f6ffdd5620 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 16 Aug 2010 02:03:19 -0700 Subject: [PATCH 22/22] Wrap the MYSQL* again so we can: 1) let mysql_init/mysql_close take care of any/all allocation, thread state and freeing 2) for faster access to the encoding and active state variables for the connection --- ext/mysql2/client.c | 64 +++++++++++++++++++++++------------------ ext/mysql2/client.h | 6 ++++ ext/mysql2/mysql2_ext.h | 3 -- ext/mysql2/result.c | 13 +++++---- ext/mysql2/result.h | 1 + lib/mysql2/client.rb | 2 -- 6 files changed, 51 insertions(+), 38 deletions(-) diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 258f3fb..2650b2f 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -14,11 +14,13 @@ static ID intern_merge, intern_error_number_eql, intern_sql_state_eql; } #define MARK_CONN_INACTIVE(conn) \ - rb_iv_set(conn, "@active", Qfalse); + wrapper->active = 0; #define GET_CLIENT(self) \ - MYSQL * client; \ - Data_Get_Struct(self, MYSQL, client); + 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 @@ -66,6 +68,13 @@ 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, intern_error_number_eql, 1, INT2NUM(mysql_errno(client))); @@ -78,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; } @@ -96,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 @@ -129,15 +139,12 @@ 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) { @@ -237,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", GET_ENCODING(self)); + mysql2_result_wrapper * result_wrapper; + GetMysql2Result(resultObj, result_wrapper); + result_wrapper->encoding = wrapper->encoding; #endif return resultObj; } @@ -247,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; + VALUE opts, defaults; int(*selector)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; GET_CLIENT(self) 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"); } @@ -276,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(GET_ENCODING(self)); + 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 @@ -323,7 +331,7 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) { 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(GET_ENCODING(self)); + 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 @@ -348,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(GET_ENCODING(self)); + rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); #endif rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version())); @@ -372,7 +381,7 @@ static VALUE rb_mysql_client_server_info(VALUE self) { GET_CLIENT(self) #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); - rb_encoding *conn_enc = rb_to_encoding(GET_ENCODING(self)); + rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); #endif REQUIRE_OPEN_DB(client); @@ -446,14 +455,13 @@ static VALUE set_charset_name(VALUE self, VALUE value) { 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 diff --git a/ext/mysql2/client.h b/ext/mysql2/client.h index a962303..6bd9963 100644 --- a/ext/mysql2/client.h +++ b/ext/mysql2/client.h @@ -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 \ No newline at end of file diff --git a/ext/mysql2/mysql2_ext.h b/ext/mysql2/mysql2_ext.h index e2b4e6e..e01e092 100644 --- a/ext/mysql2/mysql2_ext.h +++ b/ext/mysql2/mysql2_ext.h @@ -29,7 +29,4 @@ #include #include -#define GET_ENCODING(self) \ - rb_iv_get(self, "@encoding") - #endif diff --git a/ext/mysql2/result.c b/ext/mysql2/result.c index 93f2990..9df4131 100644 --- a/ext/mysql2/result.c +++ b/ext/mysql2/result.c @@ -20,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); } } @@ -65,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(GET_ENCODING(self)); + rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); #endif field = mysql_fetch_field_direct(wrapper->result, idx); @@ -97,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(GET_ENCODING(self)); -#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) { @@ -401,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; } diff --git a/ext/mysql2/result.h b/ext/mysql2/result.h index c466a7c..dd47ced 100644 --- a/ext/mysql2/result.h +++ b/ext/mysql2/result.h @@ -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; diff --git a/lib/mysql2/client.rb b/lib/mysql2/client.rb index 5206e54..81d08bc 100644 --- a/lib/mysql2/client.rb +++ b/lib/mysql2/client.rb @@ -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))