PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

http://github.com/rails/rails
Ruby | 422 lines | 310 code | 63 blank | 49 comment | 30 complexity | fe402370e15335891572675e2efa65ee MD5 | raw file
  1. require 'active_record/connection_adapters/abstract_mysql_adapter'
  2. require 'active_record/connection_adapters/statement_pool'
  3. require 'active_support/core_ext/hash/keys'
  4. gem 'mysql', '~> 2.8.1'
  5. require 'mysql'
  6. class Mysql
  7. class Time
  8. ###
  9. # This monkey patch is for test_additional_columns_from_join_table
  10. def to_date
  11. Date.new(year, month, day)
  12. end
  13. end
  14. class Stmt; include Enumerable end
  15. class Result; include Enumerable end
  16. end
  17. module ActiveRecord
  18. module ConnectionHandling
  19. # Establishes a connection to the database that's used by all Active Record objects.
  20. def mysql_connection(config) # :nodoc:
  21. config = config.symbolize_keys
  22. host = config[:host]
  23. port = config[:port]
  24. socket = config[:socket]
  25. username = config[:username] ? config[:username].to_s : 'root'
  26. password = config[:password].to_s
  27. database = config[:database]
  28. mysql = Mysql.init
  29. mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey]
  30. default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0
  31. default_flags |= Mysql::CLIENT_FOUND_ROWS if Mysql.const_defined?(:CLIENT_FOUND_ROWS)
  32. options = [host, username, password, database, port, socket, default_flags]
  33. ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
  34. end
  35. end
  36. module ConnectionAdapters
  37. # The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with
  38. # the faster C-based MySQL/Ruby adapter (available both as a gem and from http://www.tmtm.org/en/mysql/ruby/).
  39. #
  40. # Options:
  41. #
  42. # * <tt>:host</tt> - Defaults to "localhost".
  43. # * <tt>:port</tt> - Defaults to 3306.
  44. # * <tt>:socket</tt> - Defaults to "/tmp/mysql.sock".
  45. # * <tt>:username</tt> - Defaults to "root"
  46. # * <tt>:password</tt> - Defaults to nothing.
  47. # * <tt>:database</tt> - The name of the database. No default, must be provided.
  48. # * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
  49. # * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
  50. # * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
  51. # * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
  52. # * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
  53. # * <tt>:sslcapath</tt> - Necessary to use MySQL with an SSL connection.
  54. # * <tt>:sslcipher</tt> - Necessary to use MySQL with an SSL connection.
  55. #
  56. class MysqlAdapter < AbstractMysqlAdapter
  57. class Column < AbstractMysqlAdapter::Column #:nodoc:
  58. def self.string_to_time(value)
  59. return super unless Mysql::Time === value
  60. new_time(
  61. value.year,
  62. value.month,
  63. value.day,
  64. value.hour,
  65. value.minute,
  66. value.second,
  67. value.second_part)
  68. end
  69. def self.string_to_dummy_time(v)
  70. return super unless Mysql::Time === v
  71. new_time(2000, 01, 01, v.hour, v.minute, v.second, v.second_part)
  72. end
  73. def self.string_to_date(v)
  74. return super unless Mysql::Time === v
  75. new_date(v.year, v.month, v.day)
  76. end
  77. def adapter
  78. MysqlAdapter
  79. end
  80. end
  81. ADAPTER_NAME = 'MySQL'
  82. class StatementPool < ConnectionAdapters::StatementPool
  83. def initialize(connection, max = 1000)
  84. super
  85. @cache = Hash.new { |h,pid| h[pid] = {} }
  86. end
  87. def each(&block); cache.each(&block); end
  88. def key?(key); cache.key?(key); end
  89. def [](key); cache[key]; end
  90. def length; cache.length; end
  91. def delete(key); cache.delete(key); end
  92. def []=(sql, key)
  93. while @max <= cache.size
  94. cache.shift.last[:stmt].close
  95. end
  96. cache[sql] = key
  97. end
  98. def clear
  99. cache.values.each do |hash|
  100. hash[:stmt].close
  101. end
  102. cache.clear
  103. end
  104. private
  105. def cache
  106. @cache[$$]
  107. end
  108. end
  109. def initialize(connection, logger, connection_options, config)
  110. super
  111. @statements = StatementPool.new(@connection,
  112. config.fetch(:statement_limit) { 1000 })
  113. @client_encoding = nil
  114. connect
  115. end
  116. # Returns true, since this connection adapter supports prepared statement
  117. # caching.
  118. def supports_statement_cache?
  119. true
  120. end
  121. # HELPER METHODS ===========================================
  122. def each_hash(result) # :nodoc:
  123. if block_given?
  124. result.each_hash do |row|
  125. row.symbolize_keys!
  126. yield row
  127. end
  128. else
  129. to_enum(:each_hash, result)
  130. end
  131. end
  132. def new_column(field, default, type, null, collation) # :nodoc:
  133. Column.new(field, default, type, null, collation)
  134. end
  135. def error_number(exception) # :nodoc:
  136. exception.errno if exception.respond_to?(:errno)
  137. end
  138. # QUOTING ==================================================
  139. def type_cast(value, column)
  140. return super unless value == true || value == false
  141. value ? 1 : 0
  142. end
  143. def quote_string(string) #:nodoc:
  144. @connection.quote(string)
  145. end
  146. # CONNECTION MANAGEMENT ====================================
  147. def active?
  148. if @connection.respond_to?(:stat)
  149. @connection.stat
  150. else
  151. @connection.query 'select 1'
  152. end
  153. # mysql-ruby doesn't raise an exception when stat fails.
  154. if @connection.respond_to?(:errno)
  155. @connection.errno.zero?
  156. else
  157. true
  158. end
  159. rescue Mysql::Error
  160. false
  161. end
  162. def reconnect!
  163. disconnect!
  164. clear_cache!
  165. connect
  166. end
  167. # Disconnects from the database if already connected. Otherwise, this
  168. # method does nothing.
  169. def disconnect!
  170. @connection.close rescue nil
  171. end
  172. def reset!
  173. if @connection.respond_to?(:change_user)
  174. # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
  175. # reset the connection is to change the user to the same user.
  176. @connection.change_user(@config[:username], @config[:password], @config[:database])
  177. configure_connection
  178. end
  179. end
  180. # DATABASE STATEMENTS ======================================
  181. def select_rows(sql, name = nil)
  182. @connection.query_with_result = true
  183. rows = exec_without_stmt(sql, name).rows
  184. @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
  185. rows
  186. end
  187. # Clears the prepared statements cache.
  188. def clear_cache!
  189. @statements.clear
  190. end
  191. # Taken from here:
  192. # https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
  193. # Author: TOMITA Masahiro <tommy@tmtm.org>
  194. ENCODINGS = {
  195. "armscii8" => nil,
  196. "ascii" => Encoding::US_ASCII,
  197. "big5" => Encoding::Big5,
  198. "binary" => Encoding::ASCII_8BIT,
  199. "cp1250" => Encoding::Windows_1250,
  200. "cp1251" => Encoding::Windows_1251,
  201. "cp1256" => Encoding::Windows_1256,
  202. "cp1257" => Encoding::Windows_1257,
  203. "cp850" => Encoding::CP850,
  204. "cp852" => Encoding::CP852,
  205. "cp866" => Encoding::IBM866,
  206. "cp932" => Encoding::Windows_31J,
  207. "dec8" => nil,
  208. "eucjpms" => Encoding::EucJP_ms,
  209. "euckr" => Encoding::EUC_KR,
  210. "gb2312" => Encoding::EUC_CN,
  211. "gbk" => Encoding::GBK,
  212. "geostd8" => nil,
  213. "greek" => Encoding::ISO_8859_7,
  214. "hebrew" => Encoding::ISO_8859_8,
  215. "hp8" => nil,
  216. "keybcs2" => nil,
  217. "koi8r" => Encoding::KOI8_R,
  218. "koi8u" => Encoding::KOI8_U,
  219. "latin1" => Encoding::ISO_8859_1,
  220. "latin2" => Encoding::ISO_8859_2,
  221. "latin5" => Encoding::ISO_8859_9,
  222. "latin7" => Encoding::ISO_8859_13,
  223. "macce" => Encoding::MacCentEuro,
  224. "macroman" => Encoding::MacRoman,
  225. "sjis" => Encoding::SHIFT_JIS,
  226. "swe7" => nil,
  227. "tis620" => Encoding::TIS_620,
  228. "ucs2" => Encoding::UTF_16BE,
  229. "ujis" => Encoding::EucJP_ms,
  230. "utf8" => Encoding::UTF_8,
  231. "utf8mb4" => Encoding::UTF_8,
  232. }
  233. # Get the client encoding for this database
  234. def client_encoding
  235. return @client_encoding if @client_encoding
  236. result = exec_query(
  237. "SHOW VARIABLES WHERE Variable_name = 'character_set_client'",
  238. 'SCHEMA')
  239. @client_encoding = ENCODINGS[result.rows.last.last]
  240. end
  241. def exec_query(sql, name = 'SQL', binds = [])
  242. log(sql, name, binds) do
  243. exec_stmt(sql, name, binds) do |cols, stmt|
  244. ActiveRecord::Result.new(cols, stmt.to_a) if cols
  245. end
  246. end
  247. end
  248. def last_inserted_id(result)
  249. @connection.insert_id
  250. end
  251. def exec_without_stmt(sql, name = 'SQL') # :nodoc:
  252. # Some queries, like SHOW CREATE TABLE don't work through the prepared
  253. # statement API. For those queries, we need to use this method. :'(
  254. log(sql, name) do
  255. result = @connection.query(sql)
  256. cols = []
  257. rows = []
  258. if result
  259. cols = result.fetch_fields.map { |field| field.name }
  260. rows = result.to_a
  261. result.free
  262. end
  263. ActiveRecord::Result.new(cols, rows)
  264. end
  265. end
  266. def execute_and_free(sql, name = nil)
  267. result = execute(sql, name)
  268. ret = yield result
  269. result.free
  270. ret
  271. end
  272. def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
  273. super sql, name
  274. id_value || @connection.insert_id
  275. end
  276. alias :create :insert_sql
  277. def exec_delete(sql, name, binds)
  278. log(sql, name, binds) do
  279. exec_stmt(sql, name, binds) do |cols, stmt|
  280. stmt.affected_rows
  281. end
  282. end
  283. end
  284. alias :exec_update :exec_delete
  285. def begin_db_transaction #:nodoc:
  286. exec_without_stmt "BEGIN"
  287. rescue Mysql::Error
  288. # Transactions aren't supported
  289. end
  290. private
  291. def exec_stmt(sql, name, binds)
  292. cache = {}
  293. if binds.empty?
  294. stmt = @connection.prepare(sql)
  295. else
  296. cache = @statements[sql] ||= {
  297. :stmt => @connection.prepare(sql)
  298. }
  299. stmt = cache[:stmt]
  300. end
  301. begin
  302. stmt.execute(*binds.map { |col, val| type_cast(val, col) })
  303. rescue Mysql::Error => e
  304. # Older versions of MySQL leave the prepared statement in a bad
  305. # place when an error occurs. To support older mysql versions, we
  306. # need to close the statement and delete the statement from the
  307. # cache.
  308. stmt.close
  309. @statements.delete sql
  310. raise e
  311. end
  312. cols = nil
  313. if metadata = stmt.result_metadata
  314. cols = cache[:cols] ||= metadata.fetch_fields.map { |field|
  315. field.name
  316. }
  317. end
  318. result = yield [cols, stmt]
  319. stmt.result_metadata.free if cols
  320. stmt.free_result
  321. stmt.close if binds.empty?
  322. result
  323. end
  324. def connect
  325. encoding = @config[:encoding]
  326. if encoding
  327. @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
  328. end
  329. if @config[:sslca] || @config[:sslkey]
  330. @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher])
  331. end
  332. @connection.options(Mysql::OPT_CONNECT_TIMEOUT, @config[:connect_timeout]) if @config[:connect_timeout]
  333. @connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout]
  334. @connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout]
  335. @connection.real_connect(*@connection_options)
  336. # reconnect must be set after real_connect is called, because real_connect sets it to false internally
  337. @connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
  338. configure_connection
  339. end
  340. def configure_connection
  341. encoding = @config[:encoding]
  342. execute("SET NAMES '#{encoding}'", :skip_logging) if encoding
  343. # By default, MySQL 'where id is null' selects the last inserted id.
  344. # Turn this off. http://dev.rubyonrails.org/ticket/6778
  345. execute("SET SQL_AUTO_IS_NULL=0", :skip_logging)
  346. end
  347. def select(sql, name = nil, binds = [])
  348. @connection.query_with_result = true
  349. rows = exec_query(sql, name, binds)
  350. @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
  351. rows
  352. end
  353. # Returns the version of the connected MySQL server.
  354. def version
  355. @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
  356. end
  357. end
  358. end
  359. end