activerecord 4.2.11.1 → 6.0.3.5

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