activerecord 5.0.6 → 6.0.1

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 (358) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +638 -2023
  3. data/MIT-LICENSE +3 -1
  4. data/README.rdoc +8 -6
  5. data/examples/performance.rb +31 -29
  6. data/examples/simple.rb +5 -3
  7. data/lib/active_record/aggregations.rb +249 -246
  8. data/lib/active_record/association_relation.rb +24 -13
  9. data/lib/active_record/associations/alias_tracker.rb +24 -33
  10. data/lib/active_record/associations/association.rb +119 -56
  11. data/lib/active_record/associations/association_scope.rb +94 -94
  12. data/lib/active_record/associations/belongs_to_association.rb +58 -42
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
  14. data/lib/active_record/associations/builder/association.rb +18 -25
  15. data/lib/active_record/associations/builder/belongs_to.rb +43 -54
  16. data/lib/active_record/associations/builder/collection_association.rb +7 -18
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +42 -61
  18. data/lib/active_record/associations/builder/has_many.rb +4 -0
  19. data/lib/active_record/associations/builder/has_one.rb +37 -1
  20. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  21. data/lib/active_record/associations/collection_association.rb +80 -252
  22. data/lib/active_record/associations/collection_proxy.rb +158 -121
  23. data/lib/active_record/associations/foreign_association.rb +9 -0
  24. data/lib/active_record/associations/has_many_association.rb +23 -29
  25. data/lib/active_record/associations/has_many_through_association.rb +58 -44
  26. data/lib/active_record/associations/has_one_association.rb +59 -54
  27. data/lib/active_record/associations/has_one_through_association.rb +20 -11
  28. data/lib/active_record/associations/join_dependency/join_association.rb +38 -90
  29. data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
  30. data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
  31. data/lib/active_record/associations/join_dependency.rb +134 -176
  32. data/lib/active_record/associations/preloader/association.rb +84 -125
  33. data/lib/active_record/associations/preloader/through_association.rb +82 -75
  34. data/lib/active_record/associations/preloader.rb +90 -102
  35. data/lib/active_record/associations/singular_association.rb +12 -45
  36. data/lib/active_record/associations/through_association.rb +26 -14
  37. data/lib/active_record/associations.rb +1603 -1592
  38. data/lib/active_record/attribute_assignment.rb +54 -60
  39. data/lib/active_record/attribute_decorators.rb +38 -15
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +12 -7
  41. data/lib/active_record/attribute_methods/dirty.rb +179 -109
  42. data/lib/active_record/attribute_methods/primary_key.rb +86 -91
  43. data/lib/active_record/attribute_methods/query.rb +4 -3
  44. data/lib/active_record/attribute_methods/read.rb +21 -49
  45. data/lib/active_record/attribute_methods/serialization.rb +30 -7
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -64
  47. data/lib/active_record/attribute_methods/write.rb +35 -33
  48. data/lib/active_record/attribute_methods.rb +66 -106
  49. data/lib/active_record/attributes.rb +38 -24
  50. data/lib/active_record/autosave_association.rb +53 -32
  51. data/lib/active_record/base.rb +27 -24
  52. data/lib/active_record/callbacks.rb +63 -33
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +11 -11
  55. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +553 -321
  56. data/lib/active_record/connection_adapters/abstract/database_limits.rb +23 -5
  57. data/lib/active_record/connection_adapters/abstract/database_statements.rb +213 -94
  58. data/lib/active_record/connection_adapters/abstract/query_cache.rb +59 -28
  59. data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -75
  60. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  61. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +33 -27
  62. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +207 -126
  63. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +68 -80
  64. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +369 -199
  65. data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
  66. data/lib/active_record/connection_adapters/abstract_adapter.rb +363 -202
  67. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +405 -551
  68. data/lib/active_record/connection_adapters/column.rb +41 -13
  69. data/lib/active_record/connection_adapters/connection_specification.rb +172 -138
  70. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +11 -4
  71. data/lib/active_record/connection_adapters/mysql/column.rb +8 -31
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +143 -49
  73. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +24 -22
  74. data/lib/active_record/connection_adapters/mysql/quoting.rb +50 -20
  75. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +50 -45
  76. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +58 -56
  77. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +70 -36
  78. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
  79. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +12 -13
  80. data/lib/active_record/connection_adapters/mysql2_adapter.rb +49 -30
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +22 -7
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +60 -54
  83. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +5 -3
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +22 -10
  85. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +6 -5
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -1
  89. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +4 -2
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -3
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +19 -17
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -5
  98. data/lib/active_record/connection_adapters/postgresql/oid/{json.rb → oid.rb} +6 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +31 -9
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +34 -30
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -4
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +24 -21
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +95 -35
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +147 -105
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +35 -32
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +380 -300
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +26 -25
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +10 -6
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +382 -275
  116. data/lib/active_record/connection_adapters/schema_cache.rb +46 -12
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +13 -8
  118. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +3 -1
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +74 -19
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +3 -8
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +254 -262
  126. data/lib/active_record/connection_adapters/statement_pool.rb +9 -7
  127. data/lib/active_record/connection_handling.rb +159 -40
  128. data/lib/active_record/core.rb +202 -162
  129. data/lib/active_record/counter_cache.rb +57 -28
  130. data/lib/active_record/database_configurations/database_config.rb +37 -0
  131. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  132. data/lib/active_record/database_configurations/url_config.rb +79 -0
  133. data/lib/active_record/database_configurations.rb +233 -0
  134. data/lib/active_record/define_callbacks.rb +22 -0
  135. data/lib/active_record/dynamic_matchers.rb +87 -86
  136. data/lib/active_record/enum.rb +60 -23
  137. data/lib/active_record/errors.rb +114 -18
  138. data/lib/active_record/explain.rb +4 -3
  139. data/lib/active_record/explain_registry.rb +3 -1
  140. data/lib/active_record/explain_subscriber.rb +9 -4
  141. data/lib/active_record/fixture_set/file.rb +13 -8
  142. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  143. data/lib/active_record/fixture_set/render_context.rb +17 -0
  144. data/lib/active_record/fixture_set/table_row.rb +153 -0
  145. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  146. data/lib/active_record/fixtures.rb +195 -502
  147. data/lib/active_record/gem_version.rb +4 -2
  148. data/lib/active_record/inheritance.rb +151 -97
  149. data/lib/active_record/insert_all.rb +179 -0
  150. data/lib/active_record/integration.rb +116 -25
  151. data/lib/active_record/internal_metadata.rb +15 -18
  152. data/lib/active_record/legacy_yaml_adapter.rb +4 -2
  153. data/lib/active_record/locking/optimistic.rb +78 -87
  154. data/lib/active_record/locking/pessimistic.rb +18 -6
  155. data/lib/active_record/log_subscriber.rb +48 -29
  156. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  157. data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
  158. data/lib/active_record/middleware/database_selector.rb +75 -0
  159. data/lib/active_record/migration/command_recorder.rb +143 -97
  160. data/lib/active_record/migration/compatibility.rb +174 -56
  161. data/lib/active_record/migration/join_table.rb +8 -6
  162. data/lib/active_record/migration.rb +367 -300
  163. data/lib/active_record/model_schema.rb +145 -139
  164. data/lib/active_record/nested_attributes.rb +214 -201
  165. data/lib/active_record/no_touching.rb +10 -1
  166. data/lib/active_record/null_relation.rb +13 -34
  167. data/lib/active_record/persistence.rb +442 -72
  168. data/lib/active_record/query_cache.rb +15 -14
  169. data/lib/active_record/querying.rb +36 -23
  170. data/lib/active_record/railtie.rb +128 -36
  171. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  172. data/lib/active_record/railties/console_sandbox.rb +2 -0
  173. data/lib/active_record/railties/controller_runtime.rb +34 -33
  174. data/lib/active_record/railties/databases.rake +309 -177
  175. data/lib/active_record/readonly_attributes.rb +5 -4
  176. data/lib/active_record/reflection.rb +211 -249
  177. data/lib/active_record/relation/batches/batch_enumerator.rb +3 -1
  178. data/lib/active_record/relation/batches.rb +99 -52
  179. data/lib/active_record/relation/calculations.rb +211 -172
  180. data/lib/active_record/relation/delegation.rb +67 -65
  181. data/lib/active_record/relation/finder_methods.rb +208 -247
  182. data/lib/active_record/relation/from_clause.rb +2 -8
  183. data/lib/active_record/relation/merger.rb +78 -61
  184. data/lib/active_record/relation/predicate_builder/array_handler.rb +20 -14
  185. data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
  186. data/lib/active_record/relation/predicate_builder/base_handler.rb +4 -3
  187. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +6 -4
  188. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
  189. data/lib/active_record/relation/predicate_builder/range_handler.rb +7 -18
  190. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  191. data/lib/active_record/relation/predicate_builder.rb +86 -104
  192. data/lib/active_record/relation/query_attribute.rb +33 -2
  193. data/lib/active_record/relation/query_methods.rb +458 -329
  194. data/lib/active_record/relation/record_fetch_warning.rb +5 -3
  195. data/lib/active_record/relation/spawn_methods.rb +8 -7
  196. data/lib/active_record/relation/where_clause.rb +111 -95
  197. data/lib/active_record/relation/where_clause_factory.rb +6 -11
  198. data/lib/active_record/relation.rb +429 -318
  199. data/lib/active_record/result.rb +69 -39
  200. data/lib/active_record/runtime_registry.rb +5 -3
  201. data/lib/active_record/sanitization.rb +83 -99
  202. data/lib/active_record/schema.rb +7 -14
  203. data/lib/active_record/schema_dumper.rb +71 -69
  204. data/lib/active_record/schema_migration.rb +15 -5
  205. data/lib/active_record/scoping/default.rb +93 -95
  206. data/lib/active_record/scoping/named.rb +45 -25
  207. data/lib/active_record/scoping.rb +20 -19
  208. data/lib/active_record/secure_token.rb +4 -2
  209. data/lib/active_record/serialization.rb +2 -0
  210. data/lib/active_record/statement_cache.rb +63 -28
  211. data/lib/active_record/store.rb +121 -41
  212. data/lib/active_record/suppressor.rb +4 -1
  213. data/lib/active_record/table_metadata.rb +26 -20
  214. data/lib/active_record/tasks/database_tasks.rb +276 -85
  215. data/lib/active_record/tasks/mysql_database_tasks.rb +54 -90
  216. data/lib/active_record/tasks/postgresql_database_tasks.rb +78 -47
  217. data/lib/active_record/tasks/sqlite_database_tasks.rb +34 -16
  218. data/lib/active_record/test_databases.rb +23 -0
  219. data/lib/active_record/test_fixtures.rb +224 -0
  220. data/lib/active_record/timestamp.rb +70 -35
  221. data/lib/active_record/touch_later.rb +7 -4
  222. data/lib/active_record/transactions.rb +133 -149
  223. data/lib/active_record/translation.rb +3 -1
  224. data/lib/active_record/type/adapter_specific_registry.rb +44 -45
  225. data/lib/active_record/type/date.rb +2 -0
  226. data/lib/active_record/type/date_time.rb +2 -0
  227. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  228. data/lib/active_record/type/hash_lookup_type_map.rb +5 -3
  229. data/lib/active_record/type/internal/timezone.rb +2 -0
  230. data/lib/active_record/type/json.rb +30 -0
  231. data/lib/active_record/type/serialized.rb +16 -8
  232. data/lib/active_record/type/text.rb +11 -0
  233. data/lib/active_record/type/time.rb +2 -1
  234. data/lib/active_record/type/type_map.rb +13 -15
  235. data/lib/active_record/type/unsigned_integer.rb +17 -0
  236. data/lib/active_record/type.rb +23 -17
  237. data/lib/active_record/type_caster/connection.rb +17 -12
  238. data/lib/active_record/type_caster/map.rb +5 -4
  239. data/lib/active_record/type_caster.rb +4 -2
  240. data/lib/active_record/validations/absence.rb +2 -0
  241. data/lib/active_record/validations/associated.rb +3 -1
  242. data/lib/active_record/validations/length.rb +2 -0
  243. data/lib/active_record/validations/presence.rb +4 -2
  244. data/lib/active_record/validations/uniqueness.rb +29 -42
  245. data/lib/active_record/validations.rb +7 -4
  246. data/lib/active_record/version.rb +3 -1
  247. data/lib/active_record.rb +36 -22
  248. data/lib/arel/alias_predication.rb +9 -0
  249. data/lib/arel/attributes/attribute.rb +37 -0
  250. data/lib/arel/attributes.rb +22 -0
  251. data/lib/arel/collectors/bind.rb +24 -0
  252. data/lib/arel/collectors/composite.rb +31 -0
  253. data/lib/arel/collectors/plain_string.rb +20 -0
  254. data/lib/arel/collectors/sql_string.rb +20 -0
  255. data/lib/arel/collectors/substitute_binds.rb +28 -0
  256. data/lib/arel/crud.rb +42 -0
  257. data/lib/arel/delete_manager.rb +18 -0
  258. data/lib/arel/errors.rb +9 -0
  259. data/lib/arel/expressions.rb +29 -0
  260. data/lib/arel/factory_methods.rb +49 -0
  261. data/lib/arel/insert_manager.rb +49 -0
  262. data/lib/arel/math.rb +45 -0
  263. data/lib/arel/nodes/and.rb +32 -0
  264. data/lib/arel/nodes/ascending.rb +23 -0
  265. data/lib/arel/nodes/binary.rb +52 -0
  266. data/lib/arel/nodes/bind_param.rb +36 -0
  267. data/lib/arel/nodes/case.rb +55 -0
  268. data/lib/arel/nodes/casted.rb +50 -0
  269. data/lib/arel/nodes/comment.rb +29 -0
  270. data/lib/arel/nodes/count.rb +12 -0
  271. data/lib/arel/nodes/delete_statement.rb +45 -0
  272. data/lib/arel/nodes/descending.rb +23 -0
  273. data/lib/arel/nodes/equality.rb +18 -0
  274. data/lib/arel/nodes/extract.rb +24 -0
  275. data/lib/arel/nodes/false.rb +16 -0
  276. data/lib/arel/nodes/full_outer_join.rb +8 -0
  277. data/lib/arel/nodes/function.rb +44 -0
  278. data/lib/arel/nodes/grouping.rb +8 -0
  279. data/lib/arel/nodes/in.rb +8 -0
  280. data/lib/arel/nodes/infix_operation.rb +80 -0
  281. data/lib/arel/nodes/inner_join.rb +8 -0
  282. data/lib/arel/nodes/insert_statement.rb +37 -0
  283. data/lib/arel/nodes/join_source.rb +20 -0
  284. data/lib/arel/nodes/matches.rb +18 -0
  285. data/lib/arel/nodes/named_function.rb +23 -0
  286. data/lib/arel/nodes/node.rb +50 -0
  287. data/lib/arel/nodes/node_expression.rb +13 -0
  288. data/lib/arel/nodes/outer_join.rb +8 -0
  289. data/lib/arel/nodes/over.rb +15 -0
  290. data/lib/arel/nodes/regexp.rb +16 -0
  291. data/lib/arel/nodes/right_outer_join.rb +8 -0
  292. data/lib/arel/nodes/select_core.rb +67 -0
  293. data/lib/arel/nodes/select_statement.rb +41 -0
  294. data/lib/arel/nodes/sql_literal.rb +16 -0
  295. data/lib/arel/nodes/string_join.rb +11 -0
  296. data/lib/arel/nodes/table_alias.rb +27 -0
  297. data/lib/arel/nodes/terminal.rb +16 -0
  298. data/lib/arel/nodes/true.rb +16 -0
  299. data/lib/arel/nodes/unary.rb +45 -0
  300. data/lib/arel/nodes/unary_operation.rb +20 -0
  301. data/lib/arel/nodes/unqualified_column.rb +22 -0
  302. data/lib/arel/nodes/update_statement.rb +41 -0
  303. data/lib/arel/nodes/values_list.rb +9 -0
  304. data/lib/arel/nodes/window.rb +126 -0
  305. data/lib/arel/nodes/with.rb +11 -0
  306. data/lib/arel/nodes.rb +68 -0
  307. data/lib/arel/order_predications.rb +13 -0
  308. data/lib/arel/predications.rb +257 -0
  309. data/lib/arel/select_manager.rb +271 -0
  310. data/lib/arel/table.rb +110 -0
  311. data/lib/arel/tree_manager.rb +72 -0
  312. data/lib/arel/update_manager.rb +34 -0
  313. data/lib/arel/visitors/depth_first.rb +204 -0
  314. data/lib/arel/visitors/dot.rb +297 -0
  315. data/lib/arel/visitors/ibm_db.rb +34 -0
  316. data/lib/arel/visitors/informix.rb +62 -0
  317. data/lib/arel/visitors/mssql.rb +157 -0
  318. data/lib/arel/visitors/mysql.rb +83 -0
  319. data/lib/arel/visitors/oracle.rb +159 -0
  320. data/lib/arel/visitors/oracle12.rb +66 -0
  321. data/lib/arel/visitors/postgresql.rb +110 -0
  322. data/lib/arel/visitors/sqlite.rb +39 -0
  323. data/lib/arel/visitors/to_sql.rb +889 -0
  324. data/lib/arel/visitors/visitor.rb +46 -0
  325. data/lib/arel/visitors/where_sql.rb +23 -0
  326. data/lib/arel/visitors.rb +20 -0
  327. data/lib/arel/window_predications.rb +9 -0
  328. data/lib/arel.rb +58 -0
  329. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  330. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  331. data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -35
  332. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +1 -1
  333. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +4 -2
  334. data/lib/rails/generators/active_record/migration.rb +17 -2
  335. data/lib/rails/generators/active_record/model/model_generator.rb +9 -29
  336. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +10 -1
  337. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  338. data/lib/rails/generators/active_record.rb +7 -5
  339. metadata +133 -50
  340. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  341. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  342. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  343. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  344. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  345. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  346. data/lib/active_record/associations/preloader/singular_association.rb +0 -20
  347. data/lib/active_record/attribute/user_provided_default.rb +0 -28
  348. data/lib/active_record/attribute.rb +0 -213
  349. data/lib/active_record/attribute_mutation_tracker.rb +0 -70
  350. data/lib/active_record/attribute_set/builder.rb +0 -130
  351. data/lib/active_record/attribute_set.rb +0 -110
  352. data/lib/active_record/collection_cache_key.rb +0 -50
  353. data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +0 -50
  354. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  355. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  356. data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
  357. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -57
  358. data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,11 +1,17 @@
1
- require 'active_record/type'
2
- require 'active_record/connection_adapters/determine_if_preparable_visitor'
3
- require 'active_record/connection_adapters/schema_cache'
4
- require 'active_record/connection_adapters/sql_type_metadata'
5
- require 'active_record/connection_adapters/abstract/schema_dumper'
6
- require 'active_record/connection_adapters/abstract/schema_creation'
7
- require 'arel/collectors/bind'
8
- require 'arel/collectors/sql_string'
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+ require "active_record/connection_adapters/determine_if_preparable_visitor"
5
+ require "active_record/connection_adapters/schema_cache"
6
+ require "active_record/connection_adapters/sql_type_metadata"
7
+ require "active_record/connection_adapters/abstract/schema_dumper"
8
+ require "active_record/connection_adapters/abstract/schema_creation"
9
+ require "active_support/concurrency/load_interlock_aware_monitor"
10
+ require "active_support/deprecation"
11
+ require "arel/collectors/bind"
12
+ require "arel/collectors/composite"
13
+ require "arel/collectors/sql_string"
14
+ require "arel/collectors/substitute_binds"
9
15
 
10
16
  module ActiveRecord
11
17
  module ConnectionAdapters # :nodoc:
@@ -14,7 +20,7 @@ module ActiveRecord
14
20
  autoload :Column
15
21
  autoload :ConnectionSpecification
16
22
 
17
- autoload_at 'active_record/connection_adapters/abstract/schema_definitions' do
23
+ autoload_at "active_record/connection_adapters/abstract/schema_definitions" do
18
24
  autoload :IndexDefinition
19
25
  autoload :ColumnDefinition
20
26
  autoload :ChangeColumnDefinition
@@ -25,11 +31,11 @@ module ActiveRecord
25
31
  autoload :ReferenceDefinition
26
32
  end
27
33
 
28
- autoload_at 'active_record/connection_adapters/abstract/connection_pool' do
34
+ autoload_at "active_record/connection_adapters/abstract/connection_pool" do
29
35
  autoload :ConnectionHandler
30
36
  end
31
37
 
32
- autoload_under 'abstract' do
38
+ autoload_under "abstract" do
33
39
  autoload :SchemaStatements
34
40
  autoload :DatabaseStatements
35
41
  autoload :DatabaseLimits
@@ -39,7 +45,7 @@ module ActiveRecord
39
45
  autoload :Savepoints
40
46
  end
41
47
 
42
- autoload_at 'active_record/connection_adapters/abstract/transaction' do
48
+ autoload_at "active_record/connection_adapters/abstract/transaction" do
43
49
  autoload :TransactionManager
44
50
  autoload :NullTransaction
45
51
  autoload :RealTransaction
@@ -61,24 +67,27 @@ module ActiveRecord
61
67
  # Most of the methods in the adapter are useful during migrations. Most
62
68
  # notably, the instance methods provided by SchemaStatements are very useful.
63
69
  class AbstractAdapter
64
- ADAPTER_NAME = "Abstract".freeze
70
+ ADAPTER_NAME = "Abstract"
65
71
  include ActiveSupport::Callbacks
66
72
  define_callbacks :checkout, :checkin
67
73
 
68
74
  include Quoting, DatabaseStatements, SchemaStatements
69
75
  include DatabaseLimits
70
76
  include QueryCache
71
- include ColumnDumper
72
77
  include Savepoints
73
78
 
74
79
  SIMPLE_INT = /\A\d+\z/
75
80
 
76
- attr_accessor :visitor, :pool
77
- attr_reader :schema_cache, :owner, :logger
81
+ attr_accessor :pool
82
+ attr_reader :visitor, :owner, :logger, :lock
78
83
  alias :in_use? :owner
79
84
 
85
+ set_callback :checkin, :after, :enable_lazy_transactions!
86
+
80
87
  def self.type_cast_config_to_integer(config)
81
- if config =~ SIMPLE_INT
88
+ if config.is_a?(Integer)
89
+ config
90
+ elsif SIMPLE_INT.match?(config)
82
91
  config.to_i
83
92
  else
84
93
  config
@@ -93,7 +102,18 @@ module ActiveRecord
93
102
  end
94
103
  end
95
104
 
96
- attr_reader :prepared_statements
105
+ def self.build_read_query_regexp(*parts) # :nodoc:
106
+ parts = parts.map { |part| /\A[\(\s]*#{part}/i }
107
+ Regexp.union(*parts)
108
+ end
109
+
110
+ def self.quoted_column_names # :nodoc:
111
+ @quoted_column_names ||= {}
112
+ end
113
+
114
+ def self.quoted_table_names # :nodoc:
115
+ @quoted_table_names ||= {}
116
+ end
97
117
 
98
118
  def initialize(connection, logger = nil, config = {}) # :nodoc:
99
119
  super()
@@ -103,10 +123,11 @@ module ActiveRecord
103
123
  @instrumenter = ActiveSupport::Notifications.instrumenter
104
124
  @logger = logger
105
125
  @config = config
106
- @pool = nil
107
- @schema_cache = SchemaCache.new self
108
- @quoted_column_names, @quoted_table_names = {}, {}
109
- @visitor = arel_visitor
126
+ @pool = ActiveRecord::ConnectionAdapters::NullPool.new
127
+ @idle_since = Concurrent.monotonic_time
128
+ @visitor = arel_visitor
129
+ @statements = build_statement_pool
130
+ @lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
110
131
 
111
132
  if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
112
133
  @prepared_statements = true
@@ -114,61 +135,86 @@ module ActiveRecord
114
135
  else
115
136
  @prepared_statements = false
116
137
  end
138
+
139
+ @advisory_locks_enabled = self.class.type_cast_config_to_boolean(
140
+ config.fetch(:advisory_locks, true)
141
+ )
117
142
  end
118
143
 
119
- class Version
120
- include Comparable
144
+ def replica?
145
+ @config[:replica] || false
146
+ end
121
147
 
122
- def initialize(version_string)
123
- @version = version_string.split('.').map(&:to_i)
124
- end
148
+ # Determines whether writes are currently being prevents.
149
+ #
150
+ # Returns true if the connection is a replica, or if +prevent_writes+
151
+ # is set to true.
152
+ def preventing_writes?
153
+ replica? || ActiveRecord::Base.connection_handler.prevent_writes
154
+ end
125
155
 
126
- def <=>(version_string)
127
- @version <=> version_string.split('.').map(&:to_i)
128
- end
156
+ def migrations_paths # :nodoc:
157
+ @config[:migrations_paths] || Migrator.migrations_paths
129
158
  end
130
159
 
131
- class BindCollector < Arel::Collectors::Bind
132
- def compile(bvs, conn)
133
- casted_binds = conn.prepare_binds_for_database(bvs)
134
- super(casted_binds.map { |value| conn.quote(value) })
135
- end
160
+ def migration_context # :nodoc:
161
+ MigrationContext.new(migrations_paths, schema_migration)
136
162
  end
137
163
 
138
- class SQLString < Arel::Collectors::SQLString
139
- def compile(bvs, conn)
140
- super(bvs)
141
- end
164
+ def schema_migration # :nodoc:
165
+ @schema_migration ||= begin
166
+ conn = self
167
+ spec_name = conn.pool.spec.name
168
+ name = "#{spec_name}::SchemaMigration"
169
+
170
+ Class.new(ActiveRecord::SchemaMigration) do
171
+ define_singleton_method(:name) { name }
172
+ define_singleton_method(:to_s) { name }
173
+
174
+ self.connection_specification_name = spec_name
175
+ end
176
+ end
142
177
  end
143
178
 
144
- def collector
145
- if prepared_statements
146
- SQLString.new
147
- else
148
- BindCollector.new
149
- end
179
+ def prepared_statements
180
+ @prepared_statements && !prepared_statements_disabled_cache.include?(object_id)
150
181
  end
151
182
 
152
- def arel_visitor # :nodoc:
153
- Arel::Visitors::ToSql.new(self)
183
+ def prepared_statements_disabled_cache # :nodoc:
184
+ Thread.current[:ar_prepared_statements_disabled_cache] ||= Set.new
154
185
  end
155
186
 
156
- def valid_type?(type)
157
- false
187
+ class Version
188
+ include Comparable
189
+
190
+ attr_reader :full_version_string
191
+
192
+ def initialize(version_string, full_version_string = nil)
193
+ @version = version_string.split(".").map(&:to_i)
194
+ @full_version_string = full_version_string
195
+ end
196
+
197
+ def <=>(version_string)
198
+ @version <=> version_string.split(".").map(&:to_i)
199
+ end
200
+
201
+ def to_s
202
+ @version.join(".")
203
+ end
158
204
  end
159
205
 
160
- def schema_creation
161
- SchemaCreation.new self
206
+ def valid_type?(type) # :nodoc:
207
+ !native_database_types[type].nil?
162
208
  end
163
209
 
164
210
  # this method must only be called while holding connection pool's mutex
165
211
  def lease
166
212
  if in_use?
167
- msg = 'Cannot lease connection, '
213
+ msg = +"Cannot lease connection, "
168
214
  if @owner == Thread.current
169
- msg << 'it is already leased by the current thread.'
215
+ msg << "it is already leased by the current thread."
170
216
  else
171
- msg << "it is already in use by a different thread: #{@owner}. " <<
217
+ msg << "it is already in use by a different thread: #{@owner}. " \
172
218
  "Current thread: #{Thread.current}."
173
219
  end
174
220
  raise ActiveRecordError, msg
@@ -177,23 +223,28 @@ module ActiveRecord
177
223
  @owner = Thread.current
178
224
  end
179
225
 
226
+ def schema_cache
227
+ @pool.get_schema_cache(self)
228
+ end
229
+
180
230
  def schema_cache=(cache)
181
231
  cache.connection = self
182
- @schema_cache = cache
232
+ @pool.set_schema_cache(cache)
183
233
  end
184
234
 
185
235
  # this method must only be called while holding connection pool's mutex
186
236
  def expire
187
237
  if in_use?
188
238
  if @owner != Thread.current
189
- raise ActiveRecordError, "Cannot expire connection, " <<
190
- "it is owned by a different thread: #{@owner}. " <<
239
+ raise ActiveRecordError, "Cannot expire connection, " \
240
+ "it is owned by a different thread: #{@owner}. " \
191
241
  "Current thread: #{Thread.current}."
192
242
  end
193
243
 
244
+ @idle_since = Concurrent.monotonic_time
194
245
  @owner = nil
195
246
  else
196
- raise ActiveRecordError, 'Cannot expire connection, it is not currently leased.'
247
+ raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
197
248
  end
198
249
  end
199
250
 
@@ -206,15 +257,21 @@ module ActiveRecord
206
257
  @owner = Thread.current
207
258
  end
208
259
  else
209
- raise ActiveRecordError, 'Cannot steal connection, it is not currently leased.'
260
+ raise ActiveRecordError, "Cannot steal connection, it is not currently leased."
210
261
  end
211
262
  end
212
263
 
264
+ # Seconds since this connection was returned to the pool
265
+ def seconds_idle # :nodoc:
266
+ return 0 if in_use?
267
+ Concurrent.monotonic_time - @idle_since
268
+ end
269
+
213
270
  def unprepared_statement
214
- old_prepared_statements, @prepared_statements = @prepared_statements, false
271
+ cache = prepared_statements_disabled_cache.add(object_id) if @prepared_statements
215
272
  yield
216
273
  ensure
217
- @prepared_statements = old_prepared_statements
274
+ cache&.delete(object_id)
218
275
  end
219
276
 
220
277
  # Returns the human-readable name of the adapter. Use mixed case - one
@@ -223,15 +280,9 @@ module ActiveRecord
223
280
  self.class::ADAPTER_NAME
224
281
  end
225
282
 
226
- # Does this adapter support migrations?
227
- def supports_migrations?
228
- false
229
- end
230
-
231
- # Can this adapter determine the primary key for tables not attached
232
- # to an Active Record class, such as join tables?
233
- def supports_primary_key?
234
- false
283
+ # Does the database for this adapter exist?
284
+ def self.database_exists?(config)
285
+ raise NotImplementedError
235
286
  end
236
287
 
237
288
  # Does this adapter support DDL rollbacks in transactions? That is, would
@@ -302,11 +353,28 @@ module ActiveRecord
302
353
  false
303
354
  end
304
355
 
356
+ # Does this adapter support creating invalid constraints?
357
+ def supports_validate_constraints?
358
+ false
359
+ end
360
+
361
+ # Does this adapter support creating foreign key constraints
362
+ # in the same statement as creating the table?
363
+ def supports_foreign_keys_in_create?
364
+ supports_foreign_keys?
365
+ end
366
+ deprecate :supports_foreign_keys_in_create?
367
+
305
368
  # Does this adapter support views?
306
369
  def supports_views?
307
370
  false
308
371
  end
309
372
 
373
+ # Does this adapter support materialized views?
374
+ def supports_materialized_views?
375
+ false
376
+ end
377
+
310
378
  # Does this adapter support datetime with precision?
311
379
  def supports_datetime_with_precision?
312
380
  false
@@ -331,6 +399,46 @@ module ActiveRecord
331
399
  def supports_multi_insert?
332
400
  true
333
401
  end
402
+ deprecate :supports_multi_insert?
403
+
404
+ # Does this adapter support virtual columns?
405
+ def supports_virtual_columns?
406
+ false
407
+ end
408
+
409
+ # Does this adapter support foreign/external tables?
410
+ def supports_foreign_tables?
411
+ false
412
+ end
413
+
414
+ # Does this adapter support optimizer hints?
415
+ def supports_optimizer_hints?
416
+ false
417
+ end
418
+
419
+ def supports_common_table_expressions?
420
+ false
421
+ end
422
+
423
+ def supports_lazy_transactions?
424
+ false
425
+ end
426
+
427
+ def supports_insert_returning?
428
+ false
429
+ end
430
+
431
+ def supports_insert_on_duplicate_skip?
432
+ false
433
+ end
434
+
435
+ def supports_insert_on_duplicate_update?
436
+ false
437
+ end
438
+
439
+ def supports_insert_conflict_target?
440
+ false
441
+ end
334
442
 
335
443
  # This is meant to be implemented by the adapters that support extensions
336
444
  def disable_extension(name)
@@ -340,6 +448,10 @@ module ActiveRecord
340
448
  def enable_extension(name)
341
449
  end
342
450
 
451
+ def advisory_locks_enabled? # :nodoc:
452
+ supports_advisory_locks? && @advisory_locks_enabled
453
+ end
454
+
343
455
  # This is meant to be implemented by the adapters that support advisory
344
456
  # locks
345
457
  #
@@ -394,6 +506,22 @@ module ActiveRecord
394
506
  reset_transaction
395
507
  end
396
508
 
509
+ # Immediately forget this connection ever existed. Unlike disconnect!,
510
+ # this will not communicate with the server.
511
+ #
512
+ # After calling this method, the behavior of all other methods becomes
513
+ # undefined. This is called internally just before a forked process gets
514
+ # rid of a connection that belonged to its parent.
515
+ def discard!
516
+ # This should be overridden by concrete adapters.
517
+ #
518
+ # Prevent @connection's finalizer from touching the socket, or
519
+ # otherwise communicating with its server, when it is collected.
520
+ if schema_cache.connection == self
521
+ schema_cache.connection = nil
522
+ end
523
+ end
524
+
397
525
  # Reset the state of this connection, directing the DBMS to clear
398
526
  # transactions and other connection-related server-side state. Usually a
399
527
  # database-dependent operation.
@@ -404,11 +532,9 @@ module ActiveRecord
404
532
  # this should be overridden by concrete adapters
405
533
  end
406
534
 
407
- ###
408
- # Clear any caching the database adapter may be doing, for example
409
- # clearing the prepared statement cache. This is database specific.
535
+ # Clear any caching the database adapter may be doing.
410
536
  def clear_cache!
411
- # this should be overridden by concrete adapters
537
+ @lock.synchronize { @statements.clear } if @statements
412
538
  end
413
539
 
414
540
  # Returns true if its required to reload the connection between requests for development mode.
@@ -419,7 +545,7 @@ module ActiveRecord
419
545
  # Checks whether the connection to the database is still active (i.e. not stale).
420
546
  # This is done under the hood by calling #active?. If the connection
421
547
  # is no longer active, then this method will reconnect to the database.
422
- def verify!(*ignored)
548
+ def verify!
423
549
  reconnect! unless active?
424
550
  end
425
551
 
@@ -430,22 +556,25 @@ module ActiveRecord
430
556
  # This is useful for when you need to call a proprietary method such as
431
557
  # PostgreSQL's lo_* methods.
432
558
  def raw_connection
559
+ disable_lazy_transactions!
433
560
  @connection
434
561
  end
435
562
 
436
- def case_sensitive_comparison(table, attribute, column, value)
437
- if value.nil?
438
- table[attribute].eq(value)
439
- else
440
- table[attribute].eq(Arel::Nodes::BindParam.new)
441
- end
563
+ def default_uniqueness_comparison(attribute, value, klass) # :nodoc:
564
+ attribute.eq(value)
442
565
  end
443
566
 
444
- def case_insensitive_comparison(table, attribute, column, value)
567
+ def case_sensitive_comparison(attribute, value) # :nodoc:
568
+ attribute.eq(value)
569
+ end
570
+
571
+ def case_insensitive_comparison(attribute, value) # :nodoc:
572
+ column = column_for_attribute(attribute)
573
+
445
574
  if can_perform_case_insensitive_comparison_for?(column)
446
- table[attribute].lower.eq(table.lower(Arel::Nodes::BindParam.new))
575
+ attribute.lower.eq(attribute.relation.lower(value))
447
576
  else
448
- table[attribute].eq(Arel::Nodes::BindParam.new)
577
+ attribute.eq(value)
449
578
  end
450
579
  end
451
580
 
@@ -459,153 +588,185 @@ module ActiveRecord
459
588
  pool.checkin self
460
589
  end
461
590
 
462
- def type_map # :nodoc:
463
- @type_map ||= Type::TypeMap.new.tap do |mapping|
464
- initialize_type_map(mapping)
591
+ def column_name_for_operation(operation, node) # :nodoc:
592
+ visitor.compile(node)
593
+ end
594
+
595
+ def default_index_type?(index) # :nodoc:
596
+ index.using.nil?
597
+ end
598
+
599
+ # Called by ActiveRecord::InsertAll,
600
+ # Passed an instance of ActiveRecord::InsertAll::Builder,
601
+ # This method implements standard bulk inserts for all databases, but
602
+ # should be overridden by adapters to implement common features with
603
+ # non-standard syntax like handling duplicates or returning values.
604
+ def build_insert_sql(insert) # :nodoc:
605
+ if insert.skip_duplicates? || insert.update_duplicates?
606
+ raise NotImplementedError, "#{self.class} should define `build_insert_sql` to implement adapter-specific logic for handling duplicates during INSERT"
465
607
  end
608
+
609
+ "INSERT #{insert.into} #{insert.values_list}"
466
610
  end
467
611
 
468
- def new_column(name, default, sql_type_metadata, null, table_name, default_function = nil, collation = nil) # :nodoc:
469
- Column.new(name, default, sql_type_metadata, null, table_name, default_function, collation)
612
+ def get_database_version # :nodoc:
470
613
  end
471
614
 
472
- def lookup_cast_type(sql_type) # :nodoc:
473
- type_map.lookup(sql_type)
615
+ def database_version # :nodoc:
616
+ schema_cache.database_version
474
617
  end
475
618
 
476
- def column_name_for_operation(operation, node) # :nodoc:
477
- visitor.accept(node, collector).value
478
- end
479
-
480
- def combine_bind_parameters(
481
- from_clause: [],
482
- join_clause: [],
483
- where_clause: [],
484
- having_clause: [],
485
- limit: nil,
486
- offset: nil
487
- ) # :nodoc:
488
- result = from_clause + join_clause + where_clause + having_clause
489
- if limit
490
- result << limit
491
- end
492
- if offset
493
- result << offset
619
+ def check_version # :nodoc:
620
+ end
621
+
622
+ private
623
+
624
+ def type_map
625
+ @type_map ||= Type::TypeMap.new.tap do |mapping|
626
+ initialize_type_map(mapping)
627
+ end
494
628
  end
495
- result
496
- end
497
-
498
- protected
499
-
500
- def initialize_type_map(m) # :nodoc:
501
- register_class_with_limit m, %r(boolean)i, Type::Boolean
502
- register_class_with_limit m, %r(char)i, Type::String
503
- register_class_with_limit m, %r(binary)i, Type::Binary
504
- register_class_with_limit m, %r(text)i, Type::Text
505
- register_class_with_precision m, %r(date)i, Type::Date
506
- register_class_with_precision m, %r(time)i, Type::Time
507
- register_class_with_precision m, %r(datetime)i, Type::DateTime
508
- register_class_with_limit m, %r(float)i, Type::Float
509
- register_class_with_limit m, %r(int)i, Type::Integer
510
-
511
- m.alias_type %r(blob)i, 'binary'
512
- m.alias_type %r(clob)i, 'text'
513
- m.alias_type %r(timestamp)i, 'datetime'
514
- m.alias_type %r(numeric)i, 'decimal'
515
- m.alias_type %r(number)i, 'decimal'
516
- m.alias_type %r(double)i, 'float'
517
-
518
- m.register_type(%r(decimal)i) do |sql_type|
519
- scale = extract_scale(sql_type)
520
- precision = extract_precision(sql_type)
521
-
522
- if scale == 0
523
- # FIXME: Remove this class as well
524
- Type::DecimalWithoutScale.new(precision: precision)
525
- else
526
- Type::Decimal.new(precision: precision, scale: scale)
629
+
630
+ def initialize_type_map(m = type_map)
631
+ register_class_with_limit m, %r(boolean)i, Type::Boolean
632
+ register_class_with_limit m, %r(char)i, Type::String
633
+ register_class_with_limit m, %r(binary)i, Type::Binary
634
+ register_class_with_limit m, %r(text)i, Type::Text
635
+ register_class_with_precision m, %r(date)i, Type::Date
636
+ register_class_with_precision m, %r(time)i, Type::Time
637
+ register_class_with_precision m, %r(datetime)i, Type::DateTime
638
+ register_class_with_limit m, %r(float)i, Type::Float
639
+ register_class_with_limit m, %r(int)i, Type::Integer
640
+
641
+ m.alias_type %r(blob)i, "binary"
642
+ m.alias_type %r(clob)i, "text"
643
+ m.alias_type %r(timestamp)i, "datetime"
644
+ m.alias_type %r(numeric)i, "decimal"
645
+ m.alias_type %r(number)i, "decimal"
646
+ m.alias_type %r(double)i, "float"
647
+
648
+ m.register_type %r(^json)i, Type::Json.new
649
+
650
+ m.register_type(%r(decimal)i) do |sql_type|
651
+ scale = extract_scale(sql_type)
652
+ precision = extract_precision(sql_type)
653
+
654
+ if scale == 0
655
+ # FIXME: Remove this class as well
656
+ Type::DecimalWithoutScale.new(precision: precision)
657
+ else
658
+ Type::Decimal.new(precision: precision, scale: scale)
659
+ end
527
660
  end
528
661
  end
529
- end
530
662
 
531
- def reload_type_map # :nodoc:
532
- type_map.clear
533
- initialize_type_map(type_map)
534
- end
663
+ def reload_type_map
664
+ type_map.clear
665
+ initialize_type_map
666
+ end
535
667
 
536
- def register_class_with_limit(mapping, key, klass) # :nodoc:
537
- mapping.register_type(key) do |*args|
538
- limit = extract_limit(args.last)
539
- klass.new(limit: limit)
668
+ def register_class_with_limit(mapping, key, klass)
669
+ mapping.register_type(key) do |*args|
670
+ limit = extract_limit(args.last)
671
+ klass.new(limit: limit)
672
+ end
540
673
  end
541
- end
542
674
 
543
- def register_class_with_precision(mapping, key, klass) # :nodoc:
544
- mapping.register_type(key) do |*args|
545
- precision = extract_precision(args.last)
546
- klass.new(precision: precision)
675
+ def register_class_with_precision(mapping, key, klass)
676
+ mapping.register_type(key) do |*args|
677
+ precision = extract_precision(args.last)
678
+ klass.new(precision: precision)
679
+ end
547
680
  end
548
- end
549
681
 
550
- def extract_scale(sql_type) # :nodoc:
551
- case sql_type
682
+ def extract_scale(sql_type)
683
+ case sql_type
552
684
  when /\((\d+)\)/ then 0
553
685
  when /\((\d+)(,(\d+))\)/ then $3.to_i
686
+ end
554
687
  end
555
- end
556
688
 
557
- def extract_precision(sql_type) # :nodoc:
558
- $1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/
559
- end
689
+ def extract_precision(sql_type)
690
+ $1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/
691
+ end
560
692
 
561
- def extract_limit(sql_type) # :nodoc:
562
- case sql_type
563
- when /^bigint/i
564
- 8
565
- when /\((.*)\)/
566
- $1.to_i
693
+ def extract_limit(sql_type)
694
+ $1.to_i if sql_type =~ /\((.*)\)/
567
695
  end
568
- end
569
696
 
570
- def translate_exception_class(e, sql)
571
- begin
572
- message = "#{e.class.name}: #{e.message}: #{sql}"
573
- rescue Encoding::CompatibilityError
574
- message = "#{e.class.name}: #{e.message.force_encoding sql.encoding}: #{sql}"
697
+ def translate_exception_class(e, sql, binds)
698
+ message = "#{e.class.name}: #{e.message}"
699
+
700
+ exception = translate_exception(
701
+ e, message: message, sql: sql, binds: binds
702
+ )
703
+ exception.set_backtrace e.backtrace
704
+ exception
575
705
  end
576
706
 
577
- exception = translate_exception(e, message)
578
- exception.set_backtrace e.backtrace
579
- exception
580
- end
707
+ def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) # :doc:
708
+ @instrumenter.instrument(
709
+ "sql.active_record",
710
+ sql: sql,
711
+ name: name,
712
+ binds: binds,
713
+ type_casted_binds: type_casted_binds,
714
+ statement_name: statement_name,
715
+ connection_id: object_id,
716
+ connection: self) do
717
+ @lock.synchronize do
718
+ yield
719
+ end
720
+ rescue => e
721
+ raise translate_exception_class(e, sql, binds)
722
+ end
723
+ end
581
724
 
582
- def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil)
583
- @instrumenter.instrument(
584
- "sql.active_record",
585
- sql: sql,
586
- name: name,
587
- binds: binds,
588
- type_casted_binds: type_casted_binds,
589
- statement_name: statement_name,
590
- connection_id: object_id) { yield }
591
- rescue => e
592
- raise translate_exception_class(e, sql)
593
- end
725
+ def translate_exception(exception, message:, sql:, binds:)
726
+ # override in derived class
727
+ case exception
728
+ when RuntimeError
729
+ exception
730
+ else
731
+ ActiveRecord::StatementInvalid.new(message, sql: sql, binds: binds)
732
+ end
733
+ end
594
734
 
595
- def translate_exception(exception, message)
596
- # override in derived class
597
- ActiveRecord::StatementInvalid.new(message)
598
- end
735
+ def without_prepared_statement?(binds)
736
+ !prepared_statements || binds.empty?
737
+ end
599
738
 
600
- def without_prepared_statement?(binds)
601
- !prepared_statements || binds.empty?
602
- end
739
+ def column_for(table_name, column_name)
740
+ column_name = column_name.to_s
741
+ columns(table_name).detect { |c| c.name == column_name } ||
742
+ raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}")
743
+ end
603
744
 
604
- def column_for(table_name, column_name) # :nodoc:
605
- column_name = column_name.to_s
606
- columns(table_name).detect { |c| c.name == column_name } ||
607
- raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}")
608
- end
745
+ def column_for_attribute(attribute)
746
+ table_name = attribute.relation.name
747
+ schema_cache.columns_hash(table_name)[attribute.name.to_s]
748
+ end
749
+
750
+ def collector
751
+ if prepared_statements
752
+ Arel::Collectors::Composite.new(
753
+ Arel::Collectors::SQLString.new,
754
+ Arel::Collectors::Bind.new,
755
+ )
756
+ else
757
+ Arel::Collectors::SubstituteBinds.new(
758
+ self,
759
+ Arel::Collectors::SQLString.new,
760
+ )
761
+ end
762
+ end
763
+
764
+ def arel_visitor
765
+ Arel::Visitors::ToSql.new(self)
766
+ end
767
+
768
+ def build_statement_pool
769
+ end
609
770
  end
610
771
  end
611
772
  end