activerecord 3.2.19 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (264) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1715 -604
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +40 -45
  5. data/examples/performance.rb +33 -22
  6. data/examples/simple.rb +3 -4
  7. data/lib/active_record/aggregations.rb +76 -51
  8. data/lib/active_record/association_relation.rb +35 -0
  9. data/lib/active_record/associations/alias_tracker.rb +54 -40
  10. data/lib/active_record/associations/association.rb +76 -56
  11. data/lib/active_record/associations/association_scope.rb +125 -93
  12. data/lib/active_record/associations/belongs_to_association.rb +57 -28
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -2
  14. data/lib/active_record/associations/builder/association.rb +120 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +115 -62
  16. data/lib/active_record/associations/builder/collection_association.rb +61 -53
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +117 -43
  18. data/lib/active_record/associations/builder/has_many.rb +9 -65
  19. data/lib/active_record/associations/builder/has_one.rb +18 -52
  20. data/lib/active_record/associations/builder/singular_association.rb +18 -19
  21. data/lib/active_record/associations/collection_association.rb +268 -186
  22. data/lib/active_record/associations/collection_proxy.rb +1003 -63
  23. data/lib/active_record/associations/foreign_association.rb +11 -0
  24. data/lib/active_record/associations/has_many_association.rb +81 -41
  25. data/lib/active_record/associations/has_many_through_association.rb +76 -55
  26. data/lib/active_record/associations/has_one_association.rb +51 -21
  27. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  28. data/lib/active_record/associations/join_dependency/join_association.rb +83 -108
  29. data/lib/active_record/associations/join_dependency/join_base.rb +7 -9
  30. data/lib/active_record/associations/join_dependency/join_part.rb +30 -37
  31. data/lib/active_record/associations/join_dependency.rb +239 -155
  32. data/lib/active_record/associations/preloader/association.rb +97 -62
  33. data/lib/active_record/associations/preloader/collection_association.rb +2 -8
  34. data/lib/active_record/associations/preloader/has_many_through.rb +7 -3
  35. data/lib/active_record/associations/preloader/has_one.rb +0 -8
  36. data/lib/active_record/associations/preloader/singular_association.rb +3 -3
  37. data/lib/active_record/associations/preloader/through_association.rb +75 -33
  38. data/lib/active_record/associations/preloader.rb +111 -79
  39. data/lib/active_record/associations/singular_association.rb +35 -13
  40. data/lib/active_record/associations/through_association.rb +41 -19
  41. data/lib/active_record/associations.rb +727 -501
  42. data/lib/active_record/attribute/user_provided_default.rb +28 -0
  43. data/lib/active_record/attribute.rb +213 -0
  44. data/lib/active_record/attribute_assignment.rb +32 -162
  45. data/lib/active_record/attribute_decorators.rb +67 -0
  46. data/lib/active_record/attribute_methods/before_type_cast.rb +52 -7
  47. data/lib/active_record/attribute_methods/dirty.rb +101 -61
  48. data/lib/active_record/attribute_methods/primary_key.rb +50 -36
  49. data/lib/active_record/attribute_methods/query.rb +7 -6
  50. data/lib/active_record/attribute_methods/read.rb +56 -117
  51. data/lib/active_record/attribute_methods/serialization.rb +43 -96
  52. data/lib/active_record/attribute_methods/time_zone_conversion.rb +93 -42
  53. data/lib/active_record/attribute_methods/write.rb +34 -45
  54. data/lib/active_record/attribute_methods.rb +333 -144
  55. data/lib/active_record/attribute_mutation_tracker.rb +70 -0
  56. data/lib/active_record/attribute_set/builder.rb +108 -0
  57. data/lib/active_record/attribute_set.rb +108 -0
  58. data/lib/active_record/attributes.rb +265 -0
  59. data/lib/active_record/autosave_association.rb +285 -223
  60. data/lib/active_record/base.rb +95 -490
  61. data/lib/active_record/callbacks.rb +95 -61
  62. data/lib/active_record/coders/json.rb +13 -0
  63. data/lib/active_record/coders/yaml_column.rb +28 -19
  64. data/lib/active_record/collection_cache_key.rb +40 -0
  65. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +724 -277
  66. data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
  67. data/lib/active_record/connection_adapters/abstract/database_statements.rb +199 -192
  68. data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -26
  69. data/lib/active_record/connection_adapters/abstract/quoting.rb +140 -57
  70. data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
  71. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +147 -0
  72. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +419 -276
  73. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +105 -0
  74. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +963 -276
  75. data/lib/active_record/connection_adapters/abstract/transaction.rb +232 -0
  76. data/lib/active_record/connection_adapters/abstract_adapter.rb +397 -106
  77. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +643 -342
  78. data/lib/active_record/connection_adapters/column.rb +30 -259
  79. data/lib/active_record/connection_adapters/connection_specification.rb +263 -0
  80. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +22 -0
  81. data/lib/active_record/connection_adapters/mysql/column.rb +50 -0
  82. data/lib/active_record/connection_adapters/mysql/database_statements.rb +125 -0
  83. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +70 -0
  84. data/lib/active_record/connection_adapters/mysql/quoting.rb +51 -0
  85. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +67 -0
  86. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +93 -0
  87. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +54 -0
  88. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +32 -0
  89. data/lib/active_record/connection_adapters/mysql2_adapter.rb +47 -196
  90. data/lib/active_record/connection_adapters/postgresql/column.rb +15 -0
  91. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +170 -0
  92. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +42 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +70 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +48 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +21 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +10 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +39 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +50 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +93 -0
  109. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
  110. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
  111. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  112. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  113. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  114. data/lib/active_record/connection_adapters/postgresql/oid.rb +31 -0
  115. data/lib/active_record/connection_adapters/postgresql/quoting.rb +116 -0
  116. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +49 -0
  117. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +180 -0
  118. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +47 -0
  119. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +682 -0
  120. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +35 -0
  121. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  122. data/lib/active_record/connection_adapters/postgresql_adapter.rb +558 -1039
  123. data/lib/active_record/connection_adapters/schema_cache.rb +74 -36
  124. data/lib/active_record/connection_adapters/sql_type_metadata.rb +32 -0
  125. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +19 -0
  126. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +48 -0
  127. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
  128. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +538 -24
  129. data/lib/active_record/connection_adapters/statement_pool.rb +31 -12
  130. data/lib/active_record/connection_handling.rb +155 -0
  131. data/lib/active_record/core.rb +561 -0
  132. data/lib/active_record/counter_cache.rb +146 -105
  133. data/lib/active_record/dynamic_matchers.rb +101 -64
  134. data/lib/active_record/enum.rb +234 -0
  135. data/lib/active_record/errors.rb +153 -56
  136. data/lib/active_record/explain.rb +15 -63
  137. data/lib/active_record/explain_registry.rb +30 -0
  138. data/lib/active_record/explain_subscriber.rb +10 -6
  139. data/lib/active_record/fixture_set/file.rb +77 -0
  140. data/lib/active_record/fixtures.rb +355 -232
  141. data/lib/active_record/gem_version.rb +15 -0
  142. data/lib/active_record/inheritance.rb +144 -79
  143. data/lib/active_record/integration.rb +66 -13
  144. data/lib/active_record/internal_metadata.rb +56 -0
  145. data/lib/active_record/legacy_yaml_adapter.rb +46 -0
  146. data/lib/active_record/locale/en.yml +9 -1
  147. data/lib/active_record/locking/optimistic.rb +77 -56
  148. data/lib/active_record/locking/pessimistic.rb +6 -6
  149. data/lib/active_record/log_subscriber.rb +53 -28
  150. data/lib/active_record/migration/command_recorder.rb +166 -33
  151. data/lib/active_record/migration/compatibility.rb +126 -0
  152. data/lib/active_record/migration/join_table.rb +15 -0
  153. data/lib/active_record/migration.rb +792 -264
  154. data/lib/active_record/model_schema.rb +192 -130
  155. data/lib/active_record/nested_attributes.rb +238 -145
  156. data/lib/active_record/no_touching.rb +52 -0
  157. data/lib/active_record/null_relation.rb +89 -0
  158. data/lib/active_record/persistence.rb +357 -157
  159. data/lib/active_record/query_cache.rb +22 -43
  160. data/lib/active_record/querying.rb +34 -23
  161. data/lib/active_record/railtie.rb +88 -48
  162. data/lib/active_record/railties/console_sandbox.rb +3 -4
  163. data/lib/active_record/railties/controller_runtime.rb +5 -4
  164. data/lib/active_record/railties/databases.rake +170 -422
  165. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  166. data/lib/active_record/readonly_attributes.rb +2 -5
  167. data/lib/active_record/reflection.rb +715 -189
  168. data/lib/active_record/relation/batches/batch_enumerator.rb +67 -0
  169. data/lib/active_record/relation/batches.rb +203 -50
  170. data/lib/active_record/relation/calculations.rb +203 -194
  171. data/lib/active_record/relation/delegation.rb +103 -25
  172. data/lib/active_record/relation/finder_methods.rb +457 -261
  173. data/lib/active_record/relation/from_clause.rb +32 -0
  174. data/lib/active_record/relation/merger.rb +167 -0
  175. data/lib/active_record/relation/predicate_builder/array_handler.rb +43 -0
  176. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +88 -0
  177. data/lib/active_record/relation/predicate_builder/base_handler.rb +17 -0
  178. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +17 -0
  179. data/lib/active_record/relation/predicate_builder/class_handler.rb +27 -0
  180. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +57 -0
  181. data/lib/active_record/relation/predicate_builder/range_handler.rb +33 -0
  182. data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
  183. data/lib/active_record/relation/predicate_builder.rb +153 -48
  184. data/lib/active_record/relation/query_attribute.rb +19 -0
  185. data/lib/active_record/relation/query_methods.rb +1019 -194
  186. data/lib/active_record/relation/record_fetch_warning.rb +49 -0
  187. data/lib/active_record/relation/spawn_methods.rb +46 -150
  188. data/lib/active_record/relation/where_clause.rb +174 -0
  189. data/lib/active_record/relation/where_clause_factory.rb +38 -0
  190. data/lib/active_record/relation.rb +450 -245
  191. data/lib/active_record/result.rb +104 -12
  192. data/lib/active_record/runtime_registry.rb +22 -0
  193. data/lib/active_record/sanitization.rb +120 -94
  194. data/lib/active_record/schema.rb +28 -18
  195. data/lib/active_record/schema_dumper.rb +141 -74
  196. data/lib/active_record/schema_migration.rb +50 -0
  197. data/lib/active_record/scoping/default.rb +64 -57
  198. data/lib/active_record/scoping/named.rb +93 -108
  199. data/lib/active_record/scoping.rb +73 -121
  200. data/lib/active_record/secure_token.rb +38 -0
  201. data/lib/active_record/serialization.rb +7 -5
  202. data/lib/active_record/statement_cache.rb +113 -0
  203. data/lib/active_record/store.rb +173 -15
  204. data/lib/active_record/suppressor.rb +58 -0
  205. data/lib/active_record/table_metadata.rb +68 -0
  206. data/lib/active_record/tasks/database_tasks.rb +313 -0
  207. data/lib/active_record/tasks/mysql_database_tasks.rb +151 -0
  208. data/lib/active_record/tasks/postgresql_database_tasks.rb +110 -0
  209. data/lib/active_record/tasks/sqlite_database_tasks.rb +59 -0
  210. data/lib/active_record/timestamp.rb +42 -24
  211. data/lib/active_record/touch_later.rb +58 -0
  212. data/lib/active_record/transactions.rb +233 -105
  213. data/lib/active_record/type/adapter_specific_registry.rb +130 -0
  214. data/lib/active_record/type/date.rb +7 -0
  215. data/lib/active_record/type/date_time.rb +7 -0
  216. data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
  217. data/lib/active_record/type/internal/abstract_json.rb +29 -0
  218. data/lib/active_record/type/internal/timezone.rb +15 -0
  219. data/lib/active_record/type/serialized.rb +63 -0
  220. data/lib/active_record/type/time.rb +20 -0
  221. data/lib/active_record/type/type_map.rb +64 -0
  222. data/lib/active_record/type.rb +72 -0
  223. data/lib/active_record/type_caster/connection.rb +29 -0
  224. data/lib/active_record/type_caster/map.rb +19 -0
  225. data/lib/active_record/type_caster.rb +7 -0
  226. data/lib/active_record/validations/absence.rb +23 -0
  227. data/lib/active_record/validations/associated.rb +33 -18
  228. data/lib/active_record/validations/length.rb +24 -0
  229. data/lib/active_record/validations/presence.rb +66 -0
  230. data/lib/active_record/validations/uniqueness.rb +128 -68
  231. data/lib/active_record/validations.rb +48 -40
  232. data/lib/active_record/version.rb +5 -7
  233. data/lib/active_record.rb +71 -47
  234. data/lib/rails/generators/active_record/migration/migration_generator.rb +56 -8
  235. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +24 -0
  236. data/lib/rails/generators/active_record/migration/templates/migration.rb +28 -16
  237. data/lib/rails/generators/active_record/migration.rb +18 -8
  238. data/lib/rails/generators/active_record/model/model_generator.rb +38 -16
  239. data/lib/rails/generators/active_record/model/templates/application_record.rb +5 -0
  240. data/lib/rails/generators/active_record/model/templates/model.rb +7 -6
  241. data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
  242. data/lib/rails/generators/active_record.rb +3 -11
  243. metadata +188 -134
  244. data/examples/associations.png +0 -0
  245. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  246. data/lib/active_record/associations/join_helper.rb +0 -55
  247. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  248. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  249. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
  250. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -441
  251. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
  252. data/lib/active_record/dynamic_finder_match.rb +0 -68
  253. data/lib/active_record/dynamic_scope_match.rb +0 -23
  254. data/lib/active_record/fixtures/file.rb +0 -65
  255. data/lib/active_record/identity_map.rb +0 -162
  256. data/lib/active_record/observer.rb +0 -121
  257. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  258. data/lib/active_record/session_store.rb +0 -360
  259. data/lib/active_record/test_case.rb +0 -73
  260. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  261. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  262. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  263. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  264. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,191 +0,0 @@
1
- require 'uri'
2
-
3
- module ActiveRecord
4
- class Base
5
- class ConnectionSpecification #:nodoc:
6
- attr_reader :config, :adapter_method
7
- def initialize (config, adapter_method)
8
- @config, @adapter_method = config, adapter_method
9
- end
10
-
11
- ##
12
- # Builds a ConnectionSpecification from user input
13
- class Resolver # :nodoc:
14
- attr_reader :config, :klass, :configurations
15
-
16
- def initialize(config, configurations)
17
- @config = config
18
- @configurations = configurations
19
- end
20
-
21
- def spec
22
- case config
23
- when nil
24
- raise AdapterNotSpecified unless defined?(Rails.env)
25
- resolve_string_connection Rails.env
26
- when Symbol, String
27
- resolve_string_connection config.to_s
28
- when Hash
29
- resolve_hash_connection config
30
- end
31
- end
32
-
33
- private
34
- def resolve_string_connection(spec) # :nodoc:
35
- hash = configurations.fetch(spec) do |k|
36
- connection_url_to_hash(k)
37
- end
38
-
39
- raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash
40
-
41
- resolve_hash_connection hash
42
- end
43
-
44
- def resolve_hash_connection(spec) # :nodoc:
45
- spec = spec.symbolize_keys
46
-
47
- raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
48
-
49
- begin
50
- require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
51
- rescue LoadError => e
52
- raise LoadError, "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e.message})", e.backtrace
53
- end
54
-
55
- adapter_method = "#{spec[:adapter]}_connection"
56
-
57
- ConnectionSpecification.new(spec, adapter_method)
58
- end
59
-
60
- def connection_url_to_hash(url) # :nodoc:
61
- config = URI.parse url
62
- adapter = config.scheme
63
- adapter = "postgresql" if adapter == "postgres"
64
- spec = { :adapter => adapter,
65
- :username => config.user,
66
- :password => config.password,
67
- :port => config.port,
68
- :database => config.path.sub(%r{^/},""),
69
- :host => config.host }
70
- spec.reject!{ |_,value| value.blank? }
71
- spec.map { |key,value| spec[key] = URI.unescape(value) if value.is_a?(String) }
72
- if config.query
73
- options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
74
- spec.merge!(options)
75
- end
76
- spec
77
- end
78
- end
79
- end
80
-
81
- ##
82
- # :singleton-method:
83
- # The connection handler
84
- class_attribute :connection_handler, :instance_writer => false
85
- self.connection_handler = ConnectionAdapters::ConnectionHandler.new
86
-
87
- # Returns the connection currently associated with the class. This can
88
- # also be used to "borrow" the connection to do database work that isn't
89
- # easily done without going straight to SQL.
90
- def connection
91
- self.class.connection
92
- end
93
-
94
- # Establishes the connection to the database. Accepts a hash as input where
95
- # the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case)
96
- # example for regular databases (MySQL, Postgresql, etc):
97
- #
98
- # ActiveRecord::Base.establish_connection(
99
- # :adapter => "mysql",
100
- # :host => "localhost",
101
- # :username => "myuser",
102
- # :password => "mypass",
103
- # :database => "somedatabase"
104
- # )
105
- #
106
- # Example for SQLite database:
107
- #
108
- # ActiveRecord::Base.establish_connection(
109
- # :adapter => "sqlite",
110
- # :database => "path/to/dbfile"
111
- # )
112
- #
113
- # Also accepts keys as strings (for parsing from YAML for example):
114
- #
115
- # ActiveRecord::Base.establish_connection(
116
- # "adapter" => "sqlite",
117
- # "database" => "path/to/dbfile"
118
- # )
119
- #
120
- # Or a URL:
121
- #
122
- # ActiveRecord::Base.establish_connection(
123
- # "postgres://myuser:mypass@localhost/somedatabase"
124
- # )
125
- #
126
- # The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
127
- # may be returned on an error.
128
- def self.establish_connection(spec = ENV["DATABASE_URL"])
129
- resolver = ConnectionSpecification::Resolver.new spec, configurations
130
- spec = resolver.spec
131
-
132
- unless respond_to?(spec.adapter_method)
133
- raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
134
- end
135
-
136
- remove_connection
137
- connection_handler.establish_connection name, spec
138
- end
139
-
140
- class << self
141
- # Returns the connection currently associated with the class. This can
142
- # also be used to "borrow" the connection to do database work unrelated
143
- # to any of the specific Active Records.
144
- def connection
145
- retrieve_connection
146
- end
147
-
148
- def connection_id
149
- Thread.current['ActiveRecord::Base.connection_id']
150
- end
151
-
152
- def connection_id=(connection_id)
153
- Thread.current['ActiveRecord::Base.connection_id'] = connection_id
154
- end
155
-
156
- # Returns the configuration of the associated connection as a hash:
157
- #
158
- # ActiveRecord::Base.connection_config
159
- # # => {:pool=>5, :timeout=>5000, :database=>"db/development.sqlite3", :adapter=>"sqlite3"}
160
- #
161
- # Please use only for reading.
162
- def connection_config
163
- connection_pool.spec.config
164
- end
165
-
166
- def connection_pool
167
- connection_handler.retrieve_connection_pool(self) or raise ConnectionNotEstablished
168
- end
169
-
170
- def retrieve_connection
171
- connection_handler.retrieve_connection(self)
172
- end
173
-
174
- # Returns true if Active Record is connected.
175
- def connected?
176
- connection_handler.connected?(self)
177
- end
178
-
179
- def remove_connection(klass = self)
180
- connection_handler.remove_connection(klass)
181
- end
182
-
183
- def clear_active_connections!
184
- connection_handler.clear_active_connections!
185
- end
186
-
187
- delegate :clear_reloadable_connections!,
188
- :clear_all_connections!,:verify_active_connections!, :to => :connection_handler
189
- end
190
- end
191
- end
@@ -1,441 +0,0 @@
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
-
5
- gem 'mysql', '~> 2.8'
6
- require 'mysql'
7
-
8
- class Mysql
9
- class Time
10
- ###
11
- # This monkey patch is for test_additional_columns_from_join_table
12
- def to_date
13
- Date.new(year, month, day)
14
- end
15
- end
16
- class Stmt; include Enumerable end
17
- class Result; include Enumerable end
18
- end
19
-
20
- module ActiveRecord
21
- class Base
22
- # Establishes a connection to the database that's used by all Active Record objects.
23
- def self.mysql_connection(config) # :nodoc:
24
- config = config.symbolize_keys
25
- host = config[:host]
26
- port = config[:port]
27
- socket = config[:socket]
28
- username = config[:username] ? config[:username].to_s : 'root'
29
- password = config[:password].to_s
30
- database = config[:database]
31
-
32
- mysql = Mysql.init
33
- mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey]
34
-
35
- default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0
36
- default_flags |= Mysql::CLIENT_FOUND_ROWS if Mysql.const_defined?(:CLIENT_FOUND_ROWS)
37
- options = [host, username, password, database, port, socket, default_flags]
38
- ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
39
- end
40
- end
41
-
42
- module ConnectionAdapters
43
- # The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with
44
- # the faster C-based MySQL/Ruby adapter (available both as a gem and from http://www.tmtm.org/en/mysql/ruby/).
45
- #
46
- # Options:
47
- #
48
- # * <tt>:host</tt> - Defaults to "localhost".
49
- # * <tt>:port</tt> - Defaults to 3306.
50
- # * <tt>:socket</tt> - Defaults to "/tmp/mysql.sock".
51
- # * <tt>:username</tt> - Defaults to "root"
52
- # * <tt>:password</tt> - Defaults to nothing.
53
- # * <tt>:database</tt> - The name of the database. No default, must be provided.
54
- # * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
55
- # * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
56
- # * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
57
- # * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
58
- # * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
59
- # * <tt>:sslcapath</tt> - Necessary to use MySQL with an SSL connection.
60
- # * <tt>:sslcipher</tt> - Necessary to use MySQL with an SSL connection.
61
- #
62
- class MysqlAdapter < AbstractMysqlAdapter
63
-
64
- class Column < AbstractMysqlAdapter::Column #:nodoc:
65
- def self.string_to_time(value)
66
- return super unless Mysql::Time === value
67
- new_time(
68
- value.year,
69
- value.month,
70
- value.day,
71
- value.hour,
72
- value.minute,
73
- value.second,
74
- value.second_part)
75
- end
76
-
77
- def self.string_to_dummy_time(v)
78
- return super unless Mysql::Time === v
79
- new_time(2000, 01, 01, v.hour, v.minute, v.second, v.second_part)
80
- end
81
-
82
- def self.string_to_date(v)
83
- return super unless Mysql::Time === v
84
- new_date(v.year, v.month, v.day)
85
- end
86
-
87
- def adapter
88
- MysqlAdapter
89
- end
90
- end
91
-
92
- ADAPTER_NAME = 'MySQL'
93
-
94
- class StatementPool < ConnectionAdapters::StatementPool
95
- def initialize(connection, max = 1000)
96
- super
97
- @cache = Hash.new { |h,pid| h[pid] = {} }
98
- end
99
-
100
- def each(&block); cache.each(&block); end
101
- def key?(key); cache.key?(key); end
102
- def [](key); cache[key]; end
103
- def length; cache.length; end
104
- def delete(key); cache.delete(key); end
105
-
106
- def []=(sql, key)
107
- while @max <= cache.size
108
- cache.shift.last[:stmt].close
109
- end
110
- cache[sql] = key
111
- end
112
-
113
- def clear
114
- cache.values.each do |hash|
115
- hash[:stmt].close
116
- end
117
- cache.clear
118
- end
119
-
120
- private
121
- def cache
122
- @cache[$$]
123
- end
124
- end
125
-
126
- def initialize(connection, logger, connection_options, config)
127
- super
128
- @statements = StatementPool.new(@connection,
129
- config.fetch(:statement_limit) { 1000 })
130
- @client_encoding = nil
131
- connect
132
- end
133
-
134
- # Returns true, since this connection adapter supports prepared statement
135
- # caching.
136
- def supports_statement_cache?
137
- true
138
- end
139
-
140
- # HELPER METHODS ===========================================
141
-
142
- def each_hash(result) # :nodoc:
143
- if block_given?
144
- result.each_hash do |row|
145
- row.symbolize_keys!
146
- yield row
147
- end
148
- else
149
- to_enum(:each_hash, result)
150
- end
151
- end
152
-
153
- def new_column(field, default, type, null, collation) # :nodoc:
154
- Column.new(field, default, type, null, collation)
155
- end
156
-
157
- def error_number(exception) # :nodoc:
158
- exception.errno if exception.respond_to?(:errno)
159
- end
160
-
161
- # QUOTING ==================================================
162
-
163
- def type_cast(value, column)
164
- return super unless value == true || value == false
165
-
166
- value ? 1 : 0
167
- end
168
-
169
- def quote_string(string) #:nodoc:
170
- @connection.quote(string)
171
- end
172
-
173
- # CONNECTION MANAGEMENT ====================================
174
-
175
- def active?
176
- if @connection.respond_to?(:stat)
177
- @connection.stat
178
- else
179
- @connection.query 'select 1'
180
- end
181
-
182
- # mysql-ruby doesn't raise an exception when stat fails.
183
- if @connection.respond_to?(:errno)
184
- @connection.errno.zero?
185
- else
186
- true
187
- end
188
- rescue Mysql::Error
189
- false
190
- end
191
-
192
- def reconnect!
193
- disconnect!
194
- clear_cache!
195
- connect
196
- end
197
-
198
- # Disconnects from the database if already connected. Otherwise, this
199
- # method does nothing.
200
- def disconnect!
201
- @connection.close rescue nil
202
- end
203
-
204
- def reset!
205
- if @connection.respond_to?(:change_user)
206
- # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
207
- # reset the connection is to change the user to the same user.
208
- @connection.change_user(@config[:username], @config[:password], @config[:database])
209
- configure_connection
210
- end
211
- end
212
-
213
- # DATABASE STATEMENTS ======================================
214
-
215
- def select_rows(sql, name = nil)
216
- @connection.query_with_result = true
217
- rows = exec_query(sql, name).rows
218
- @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
219
- rows
220
- end
221
-
222
- # Clears the prepared statements cache.
223
- def clear_cache!
224
- @statements.clear
225
- end
226
-
227
- if "<3".respond_to?(:encode)
228
- # Taken from here:
229
- # https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
230
- # Author: TOMITA Masahiro <tommy@tmtm.org>
231
- ENCODINGS = {
232
- "armscii8" => nil,
233
- "ascii" => Encoding::US_ASCII,
234
- "big5" => Encoding::Big5,
235
- "binary" => Encoding::ASCII_8BIT,
236
- "cp1250" => Encoding::Windows_1250,
237
- "cp1251" => Encoding::Windows_1251,
238
- "cp1256" => Encoding::Windows_1256,
239
- "cp1257" => Encoding::Windows_1257,
240
- "cp850" => Encoding::CP850,
241
- "cp852" => Encoding::CP852,
242
- "cp866" => Encoding::IBM866,
243
- "cp932" => Encoding::Windows_31J,
244
- "dec8" => nil,
245
- "eucjpms" => Encoding::EucJP_ms,
246
- "euckr" => Encoding::EUC_KR,
247
- "gb2312" => Encoding::EUC_CN,
248
- "gbk" => Encoding::GBK,
249
- "geostd8" => nil,
250
- "greek" => Encoding::ISO_8859_7,
251
- "hebrew" => Encoding::ISO_8859_8,
252
- "hp8" => nil,
253
- "keybcs2" => nil,
254
- "koi8r" => Encoding::KOI8_R,
255
- "koi8u" => Encoding::KOI8_U,
256
- "latin1" => Encoding::ISO_8859_1,
257
- "latin2" => Encoding::ISO_8859_2,
258
- "latin5" => Encoding::ISO_8859_9,
259
- "latin7" => Encoding::ISO_8859_13,
260
- "macce" => Encoding::MacCentEuro,
261
- "macroman" => Encoding::MacRoman,
262
- "sjis" => Encoding::SHIFT_JIS,
263
- "swe7" => nil,
264
- "tis620" => Encoding::TIS_620,
265
- "ucs2" => Encoding::UTF_16BE,
266
- "ujis" => Encoding::EucJP_ms,
267
- "utf8" => Encoding::UTF_8,
268
- "utf8mb4" => Encoding::UTF_8,
269
- }
270
- else
271
- ENCODINGS = Hash.new { |h,k| h[k] = k }
272
- end
273
-
274
- # Get the client encoding for this database
275
- def client_encoding
276
- return @client_encoding if @client_encoding
277
-
278
- result = exec_query(
279
- "SHOW VARIABLES WHERE Variable_name = 'character_set_client'",
280
- 'SCHEMA')
281
- @client_encoding = ENCODINGS[result.rows.last.last]
282
- end
283
-
284
- def exec_query(sql, name = 'SQL', binds = [])
285
- # If the configuration sets prepared_statements:false, binds will
286
- # always be empty, since the bind variables will have been already
287
- # substituted and removed from binds by BindVisitor, so this will
288
- # effectively disable prepared statement usage completely.
289
- if binds.empty?
290
- result_set, affected_rows = exec_without_stmt(sql, name)
291
- else
292
- result_set, affected_rows = exec_stmt(sql, name, binds)
293
- end
294
-
295
- yield affected_rows if block_given?
296
-
297
- result_set
298
- end
299
-
300
- def last_inserted_id(result)
301
- @connection.insert_id
302
- end
303
-
304
- def exec_without_stmt(sql, name = 'SQL') # :nodoc:
305
- # Some queries, like SHOW CREATE TABLE don't work through the prepared
306
- # statement API. For those queries, we need to use this method. :'(
307
- log(sql, name) do
308
- result = @connection.query(sql)
309
- affected_rows = @connection.affected_rows
310
-
311
- if result
312
- cols = result.fetch_fields.map { |field| field.name }
313
- result_set = ActiveRecord::Result.new(cols, result.to_a)
314
- result.free
315
- else
316
- result_set = ActiveRecord::Result.new([], [])
317
- end
318
-
319
- [result_set, affected_rows]
320
- end
321
- end
322
-
323
- def execute_and_free(sql, name = nil)
324
- result = execute(sql, name)
325
- ret = yield result
326
- result.free
327
- ret
328
- end
329
-
330
- def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
331
- super sql, name
332
- id_value || @connection.insert_id
333
- end
334
- alias :create :insert_sql
335
-
336
- def exec_delete(sql, name, binds)
337
- affected_rows = 0
338
-
339
- exec_query(sql, name, binds) do |n|
340
- affected_rows = n
341
- end
342
-
343
- affected_rows
344
- end
345
- alias :exec_update :exec_delete
346
-
347
- def begin_db_transaction #:nodoc:
348
- exec_query "BEGIN"
349
- rescue Mysql::Error
350
- # Transactions aren't supported
351
- end
352
-
353
- private
354
-
355
- def exec_stmt(sql, name, binds)
356
- cache = {}
357
- log(sql, name, binds) do
358
- if binds.empty?
359
- stmt = @connection.prepare(sql)
360
- else
361
- cache = @statements[sql] ||= {
362
- :stmt => @connection.prepare(sql)
363
- }
364
- stmt = cache[:stmt]
365
- end
366
-
367
- begin
368
- stmt.execute(*binds.map { |col, val| type_cast(val, col) })
369
- rescue Mysql::Error => e
370
- # Older versions of MySQL leave the prepared statement in a bad
371
- # place when an error occurs. To support older mysql versions, we
372
- # need to close the statement and delete the statement from the
373
- # cache.
374
- stmt.close
375
- @statements.delete sql
376
- raise e
377
- end
378
-
379
- cols = nil
380
- if metadata = stmt.result_metadata
381
- cols = cache[:cols] ||= metadata.fetch_fields.map { |field|
382
- field.name
383
- }
384
- end
385
-
386
- result_set = ActiveRecord::Result.new(cols, stmt.to_a) if cols
387
- affected_rows = stmt.affected_rows
388
-
389
- stmt.result_metadata.free if cols
390
- stmt.free_result
391
- stmt.close if binds.empty?
392
-
393
- [result_set, affected_rows]
394
- end
395
- end
396
-
397
- def connect
398
- encoding = @config[:encoding]
399
- if encoding
400
- @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
401
- end
402
-
403
- if @config[:sslca] || @config[:sslkey]
404
- @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher])
405
- end
406
-
407
- @connection.options(Mysql::OPT_CONNECT_TIMEOUT, @config[:connect_timeout]) if @config[:connect_timeout]
408
- @connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout]
409
- @connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout]
410
-
411
- @connection.real_connect(*@connection_options)
412
-
413
- # reconnect must be set after real_connect is called, because real_connect sets it to false internally
414
- @connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
415
-
416
- configure_connection
417
- end
418
-
419
- def configure_connection
420
- encoding = @config[:encoding]
421
- execute("SET NAMES '#{encoding}'", :skip_logging) if encoding
422
-
423
- # By default, MySQL 'where id is null' selects the last inserted id.
424
- # Turn this off. http://dev.rubyonrails.org/ticket/6778
425
- execute("SET SQL_AUTO_IS_NULL=0", :skip_logging)
426
- end
427
-
428
- def select(sql, name = nil, binds = [])
429
- @connection.query_with_result = true
430
- rows = exec_query(sql, name, binds).to_a
431
- @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
432
- rows
433
- end
434
-
435
- # Returns the version of the connected MySQL server.
436
- def version
437
- @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
438
- end
439
- end
440
- end
441
- end