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