diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-02-17 23:18:07 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-04-21 09:17:42 +0900 |
commit | 3f6574efb1757e2eeebb7b960055a07aea93d144 (patch) | |
tree | 4a8d5203a22418cf01f08e340437153ffb73ff5e /activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | |
parent | 115efeb036d4d56186ba7b0b64750cdc57471b59 (diff) | |
download | rails-3f6574efb1757e2eeebb7b960055a07aea93d144.tar.gz rails-3f6574efb1757e2eeebb7b960055a07aea93d144.tar.bz2 rails-3f6574efb1757e2eeebb7b960055a07aea93d144.zip |
Add prepared statements support for `Mysql2Adapter`
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 64070f0e6c..fc60c4c00d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/abstract_adapter' +require 'active_record/connection_adapters/statement_pool' require 'active_record/connection_adapters/mysql/column' require 'active_record/connection_adapters/mysql/explain_pretty_printer' require 'active_record/connection_adapters/mysql/quoting' @@ -56,9 +57,19 @@ module ActiveRecord INDEX_TYPES = [:fulltext, :spatial] INDEX_USINGS = [:btree, :hash] + class StatementPool < ConnectionAdapters::StatementPool + private + + def dealloc(stmt) + stmt[:stmt].close + end + end + def initialize(connection, logger, connection_options, config) super(connection, logger, config) + @statements = StatementPool.new(self.class.type_cast_config_to_integer(config.fetch(:statement_limit) { 1000 })) + if version < '5.0.0' raise "Your version of MySQL (#{full_version.match(/^\d+\.\d+\.\d+/)[0]}) is too old. Active Record supports MySQL >= 5.0." end @@ -93,6 +104,12 @@ module ActiveRecord true end + # Returns true, since this connection adapter supports prepared statement + # caching. + def supports_statement_cache? + true + end + # Technically MySQL allows to create indexes with the sort order syntax # but at the moment (5.5) it doesn't yet implement them def supports_index_sort_order? @@ -178,6 +195,14 @@ module ActiveRecord end end + # CONNECTION MANAGEMENT ==================================== + + # Clears the prepared statements cache. + def clear_cache! + reload_type_map + @statements.clear + end + #-- # DATABASE STATEMENTS ====================================== #++ @@ -191,11 +216,6 @@ module ActiveRecord MySQL::ExplainPrettyPrinter.new.pp(result, elapsed) end - def clear_cache! - super - reload_type_map - end - # Executes the SQL statement in the context of this connection. def execute(sql, name = nil) log(sql, name) { @connection.query(sql) } |