activerecord 4.2.0 → 6.0.5.1

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

Potentially problematic release.


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

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