serrano-vk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (588) hide show
  1. checksums.yaml +7 -0
  2. data/EXAMPLES.md +271 -0
  3. data/README.md +170 -0
  4. data/bin/serrano +6 -0
  5. data/examples/demo_crud/Gemfile +9 -0
  6. data/examples/demo_crud/Gemfile.lock +31 -0
  7. data/examples/demo_crud/app/controllers/articles_controller.rb +92 -0
  8. data/examples/demo_crud/app/controllers/categories_controller.rb +92 -0
  9. data/examples/demo_crud/app/entities/article.rb +17 -0
  10. data/examples/demo_crud/app/entities/category.rb +17 -0
  11. data/examples/demo_crud/app/entities/concerns/validatable.rb +136 -0
  12. data/examples/demo_crud/app/repositories/article_repository.rb +42 -0
  13. data/examples/demo_crud/app/repositories/category_repository.rb +42 -0
  14. data/examples/demo_crud/app/services/articles/create.rb +29 -0
  15. data/examples/demo_crud/app/services/articles/destroy.rb +20 -0
  16. data/examples/demo_crud/app/services/articles/index.rb +17 -0
  17. data/examples/demo_crud/app/services/articles/show.rb +20 -0
  18. data/examples/demo_crud/app/services/articles/update.rb +26 -0
  19. data/examples/demo_crud/app/services/categories/create.rb +27 -0
  20. data/examples/demo_crud/app/services/categories/destroy.rb +20 -0
  21. data/examples/demo_crud/app/services/categories/index.rb +17 -0
  22. data/examples/demo_crud/app/services/categories/show.rb +20 -0
  23. data/examples/demo_crud/app/services/categories/update.rb +26 -0
  24. data/examples/demo_crud/config/db.rb +3 -0
  25. data/examples/demo_crud/config.ru +19 -0
  26. data/examples/demo_crud/db/development.sqlite3 +0 -0
  27. data/examples/demo_crud/db/migrations/20260307212844_create_articles.rb +14 -0
  28. data/examples/demo_crud/db/migrations/20260307212845_create_categories.rb +12 -0
  29. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/bin/rackup +29 -0
  30. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/bin/rackup.bat +2 -0
  31. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/bin/sequel +29 -0
  32. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/bin/sequel.bat +2 -0
  33. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/bin/serrano +29 -0
  34. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/bin/serrano.bat +2 -0
  35. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/cache/bigdecimal-4.0.1.gem +0 -0
  36. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/cache/rack-3.2.5.gem +0 -0
  37. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/cache/rackup-2.3.1.gem +0 -0
  38. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/cache/sequel-5.102.0.gem +0 -0
  39. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/cache/sqlite3-2.9.1-x64-mingw-ucrt.gem +0 -0
  40. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/cache/webrick-1.9.2.gem +0 -0
  41. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/extensions/x64-mingw-ucrt/3.4.0/bigdecimal-4.0.1/bigdecimal.so +0 -0
  42. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/extensions/x64-mingw-ucrt/3.4.0/bigdecimal-4.0.1/gem.build_complete +0 -0
  43. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/extensions/x64-mingw-ucrt/3.4.0/bigdecimal-4.0.1/gem_make.out +43 -0
  44. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/extensions/x64-mingw-ucrt/3.4.0/bigdecimal-4.0.1/mkmf.log +669 -0
  45. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/LICENSE +56 -0
  46. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/bigdecimal.gemspec +57 -0
  47. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/Makefile +278 -0
  48. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/bigdecimal.c +6206 -0
  49. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/bigdecimal.h +292 -0
  50. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/bits.h +144 -0
  51. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/extconf.rb +60 -0
  52. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/feature.h +68 -0
  53. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/missing/dtoa.c +3462 -0
  54. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/missing.c +28 -0
  55. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/missing.h +104 -0
  56. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/ext/bigdecimal/static_assert.h +54 -0
  57. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal/jacobian.rb +92 -0
  58. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal/ludcmp.rb +91 -0
  59. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal/math.rb +948 -0
  60. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal/newton.rb +82 -0
  61. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal/util.rb +186 -0
  62. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal.rb +360 -0
  63. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/lib/bigdecimal.so +0 -0
  64. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/sample/linear.rb +74 -0
  65. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/sample/nlsolve.rb +40 -0
  66. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/bigdecimal-4.0.1/sample/pi.rb +21 -0
  67. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/CHANGELOG.md +1314 -0
  68. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/CONTRIBUTING.md +144 -0
  69. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/MIT-LICENSE +20 -0
  70. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/README.md +384 -0
  71. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/SPEC.rdoc +258 -0
  72. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/auth/abstract/handler.rb +41 -0
  73. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/auth/abstract/request.rb +51 -0
  74. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/auth/basic.rb +58 -0
  75. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/bad_request.rb +8 -0
  76. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/body_proxy.rb +63 -0
  77. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/builder.rb +296 -0
  78. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/cascade.rb +67 -0
  79. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/common_logger.rb +89 -0
  80. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/conditional_get.rb +87 -0
  81. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/config.rb +22 -0
  82. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/constants.rb +68 -0
  83. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/content_length.rb +34 -0
  84. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/content_type.rb +33 -0
  85. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/deflater.rb +158 -0
  86. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/directory.rb +208 -0
  87. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/etag.rb +71 -0
  88. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/events.rb +172 -0
  89. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/files.rb +216 -0
  90. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/head.rb +25 -0
  91. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/headers.rb +238 -0
  92. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/lint.rb +964 -0
  93. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/lock.rb +29 -0
  94. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/media_type.rb +52 -0
  95. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/method_override.rb +56 -0
  96. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/mime.rb +694 -0
  97. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/mock.rb +3 -0
  98. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/mock_request.rb +161 -0
  99. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/mock_response.rb +156 -0
  100. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/multipart/generator.rb +99 -0
  101. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/multipart/parser.rb +580 -0
  102. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/multipart/uploaded_file.rb +82 -0
  103. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/multipart.rb +77 -0
  104. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/null_logger.rb +48 -0
  105. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/query_parser.rb +261 -0
  106. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/recursive.rb +66 -0
  107. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/reloader.rb +112 -0
  108. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/request.rb +790 -0
  109. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/response.rb +403 -0
  110. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/rewindable_input.rb +116 -0
  111. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/runtime.rb +35 -0
  112. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/sendfile.rb +197 -0
  113. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/show_exceptions.rb +409 -0
  114. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/show_status.rb +121 -0
  115. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/static.rb +188 -0
  116. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/tempfile_reaper.rb +33 -0
  117. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/urlmap.rb +99 -0
  118. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/utils.rb +622 -0
  119. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack/version.rb +17 -0
  120. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rack-3.2.5/lib/rack.rb +64 -0
  121. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/bin/rackup +5 -0
  122. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/handler/cgi.rb +61 -0
  123. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/handler/webrick.rb +196 -0
  124. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/handler.rb +113 -0
  125. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/lobster.rb +81 -0
  126. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/server.rb +462 -0
  127. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/stream.rb +199 -0
  128. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup/version.rb +8 -0
  129. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/lib/rackup.rb +21 -0
  130. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/license.md +80 -0
  131. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/readme.md +82 -0
  132. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/releases.md +28 -0
  133. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/rackup-2.3.1/security.md +3 -0
  134. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/MIT-LICENSE +19 -0
  135. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/bin/sequel +280 -0
  136. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/ado/access.rb +335 -0
  137. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/ado/mssql.rb +62 -0
  138. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/ado.rb +283 -0
  139. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/amalgalite.rb +184 -0
  140. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/ibmdb.rb +423 -0
  141. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/db2.rb +83 -0
  142. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/derby.rb +318 -0
  143. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/h2.rb +290 -0
  144. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/hsqldb.rb +228 -0
  145. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/jtds.rb +39 -0
  146. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/mssql.rb +30 -0
  147. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/mysql.rb +89 -0
  148. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/oracle.rb +146 -0
  149. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/postgresql.rb +239 -0
  150. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/sqlanywhere.rb +87 -0
  151. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/sqlite.rb +133 -0
  152. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/sqlserver.rb +92 -0
  153. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc/transactions.rb +95 -0
  154. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/jdbc.rb +850 -0
  155. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/mock.rb +381 -0
  156. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/mysql.rb +380 -0
  157. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/mysql2.rb +307 -0
  158. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/odbc/db2.rb +11 -0
  159. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/odbc/mssql.rb +57 -0
  160. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/odbc/oracle.rb +11 -0
  161. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/odbc.rb +150 -0
  162. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/oracle.rb +427 -0
  163. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/postgres.rb +863 -0
  164. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/postgresql.rb +3 -0
  165. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/access.rb +301 -0
  166. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/db2.rb +509 -0
  167. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/mssql.rb +1238 -0
  168. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/mysql.rb +1175 -0
  169. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/oracle.rb +732 -0
  170. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/postgres.rb +3022 -0
  171. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/sqlanywhere.rb +470 -0
  172. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/shared/sqlite.rb +1073 -0
  173. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/sqlanywhere.rb +192 -0
  174. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/sqlite.rb +475 -0
  175. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/tinytds.rb +259 -0
  176. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/trilogy.rb +116 -0
  177. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/columns_limit_1.rb +22 -0
  178. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb +74 -0
  179. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +93 -0
  180. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/mysql_mysql2.rb +87 -0
  181. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/mysql_prepared_statements.rb +56 -0
  182. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/replace.rb +35 -0
  183. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/split_alter_table.rb +46 -0
  184. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/stored_procedures.rb +61 -0
  185. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/adapters/utils/unmodified_identifiers.rb +28 -0
  186. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/ast_transformer.rb +132 -0
  187. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool/sharded_single.rb +113 -0
  188. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool/sharded_threaded.rb +392 -0
  189. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool/sharded_timed_queue.rb +399 -0
  190. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool/single.rb +57 -0
  191. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool/threaded.rb +307 -0
  192. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool/timed_queue.rb +288 -0
  193. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/connection_pool.rb +175 -0
  194. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/core.rb +476 -0
  195. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/connecting.rb +349 -0
  196. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/dataset.rb +89 -0
  197. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/dataset_defaults.rb +93 -0
  198. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/features.rb +150 -0
  199. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/logging.rb +91 -0
  200. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/misc.rb +663 -0
  201. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/query.rb +436 -0
  202. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/schema_generator.rb +720 -0
  203. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/schema_methods.rb +1157 -0
  204. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database/transactions.rb +552 -0
  205. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/database.rb +37 -0
  206. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/actions.rb +1412 -0
  207. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/dataset_module.rb +46 -0
  208. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/deprecated_singleton_class_methods.rb +42 -0
  209. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/features.rb +284 -0
  210. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/graph.rb +297 -0
  211. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/misc.rb +381 -0
  212. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/placeholder_literalizer.rb +230 -0
  213. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/prepared_statements.rb +474 -0
  214. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/query.rb +1571 -0
  215. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset/sql.rb +1863 -0
  216. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/dataset.rb +60 -0
  217. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/deprecated.rb +70 -0
  218. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/exceptions.rb +130 -0
  219. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/_model_constraint_validations.rb +16 -0
  220. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/_model_pg_row.rb +31 -0
  221. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/_pretty_table.rb +85 -0
  222. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/any_not_empty.rb +45 -0
  223. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/arbitrary_servers.rb +114 -0
  224. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/async_thread_pool.rb +446 -0
  225. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/auto_cast_date_and_time.rb +94 -0
  226. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/auto_literal_strings.rb +74 -0
  227. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/blank.rb +57 -0
  228. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/caller_logging.rb +80 -0
  229. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/columns_introspection.rb +88 -0
  230. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/connection_checkout_event_callback.rb +151 -0
  231. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/connection_expiration.rb +105 -0
  232. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/connection_validator.rb +133 -0
  233. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/constant_sql_override.rb +65 -0
  234. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/constraint_validations.rb +510 -0
  235. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/core_extensions.rb +222 -0
  236. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/core_refinements.rb +244 -0
  237. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/current_datetime_timestamp.rb +59 -0
  238. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/dataset_run.rb +41 -0
  239. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/dataset_source_alias.rb +95 -0
  240. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/date_arithmetic.rb +254 -0
  241. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/date_parse_input_handler.rb +67 -0
  242. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/datetime_parse_to_time.rb +41 -0
  243. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/duplicate_columns_handler.rb +95 -0
  244. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/empty_array_consider_nulls.rb +46 -0
  245. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/error_sql.rb +76 -0
  246. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/escaped_like.rb +100 -0
  247. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/eval_inspect.rb +185 -0
  248. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/exclude_or_null.rb +68 -0
  249. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/fiber_concurrency.rb +24 -0
  250. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/freeze_datasets.rb +3 -0
  251. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/from_block.rb +3 -0
  252. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/graph_each.rb +88 -0
  253. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/identifier_mangling.rb +180 -0
  254. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/implicit_subquery.rb +48 -0
  255. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/index_caching.rb +113 -0
  256. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/inflector.rb +258 -0
  257. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/integer64.rb +32 -0
  258. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/is_distinct_from.rb +141 -0
  259. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/looser_typecasting.rb +50 -0
  260. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/migration.rb +867 -0
  261. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb +84 -0
  262. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/named_timezones.rb +184 -0
  263. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/no_auto_literal_strings.rb +4 -0
  264. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/null_dataset.rb +109 -0
  265. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pagination.rb +140 -0
  266. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_array.rb +556 -0
  267. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_array_ops.rb +377 -0
  268. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_auto_parameterize.rb +516 -0
  269. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_auto_parameterize_duplicate_query_detection.rb +191 -0
  270. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_auto_parameterize_in_array.rb +194 -0
  271. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_enum.rb +199 -0
  272. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_extended_date_support.rb +261 -0
  273. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_extended_integer_support.rb +116 -0
  274. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_hstore.rb +353 -0
  275. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_hstore_ops.rb +418 -0
  276. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_inet.rb +136 -0
  277. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_inet_ops.rb +204 -0
  278. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_interval.rb +224 -0
  279. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_json.rb +644 -0
  280. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_json_ops.rb +1460 -0
  281. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_loose_count.rb +39 -0
  282. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_multirange.rb +367 -0
  283. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_range.rb +572 -0
  284. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_range_ops.rb +195 -0
  285. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_row.rb +585 -0
  286. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_row_ops.rb +217 -0
  287. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_schema_caching.rb +90 -0
  288. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_static_cache_updater.rb +144 -0
  289. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pg_timestamptz.rb +52 -0
  290. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/pretty_table.rb +40 -0
  291. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/provenance.rb +108 -0
  292. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/query.rb +85 -0
  293. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/query_blocker.rb +172 -0
  294. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/round_timestamps.rb +49 -0
  295. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/run_transaction_hooks.rb +72 -0
  296. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/s.rb +60 -0
  297. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/schema_caching.rb +103 -0
  298. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/schema_dumper.rb +550 -0
  299. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/select_remove.rb +52 -0
  300. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/sequel_4_dataset_methods.rb +85 -0
  301. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/server_block.rb +179 -0
  302. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/server_logging.rb +61 -0
  303. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/set_literalizer.rb +39 -0
  304. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/split_array_nil.rb +80 -0
  305. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/sql_comments.rb +203 -0
  306. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/sql_expr.rb +23 -0
  307. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/sql_log_normalizer.rb +108 -0
  308. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/sqlite_json_ops.rb +313 -0
  309. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/stdio_logger.rb +48 -0
  310. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/string_agg.rb +194 -0
  311. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/string_date_time.rb +48 -0
  312. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/symbol_aref.rb +55 -0
  313. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/symbol_aref_refinement.rb +43 -0
  314. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/symbol_as.rb +23 -0
  315. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/symbol_as_refinement.rb +37 -0
  316. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/synchronize_sql.rb +45 -0
  317. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/temporarily_release_connection.rb +178 -0
  318. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/thread_local_timezones.rb +59 -0
  319. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/to_dot.rb +169 -0
  320. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/transaction_connection_validator.rb +78 -0
  321. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/extensions/virtual_row_method_block.rb +45 -0
  322. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/associations.rb +4066 -0
  323. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/base.rb +2360 -0
  324. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/dataset_module.rb +36 -0
  325. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/default_inflections.rb +47 -0
  326. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/errors.rb +67 -0
  327. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/exceptions.rb +67 -0
  328. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/inflections.rb +151 -0
  329. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model/plugins.rb +165 -0
  330. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/model.rb +85 -0
  331. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/accessed_columns.rb +63 -0
  332. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/active_model.rb +124 -0
  333. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/after_initialize.rb +39 -0
  334. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/association_dependencies.rb +106 -0
  335. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/association_lazy_eager_option.rb +66 -0
  336. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/association_multi_add_remove.rb +85 -0
  337. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/association_pks.rb +316 -0
  338. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/association_proxies.rb +131 -0
  339. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/async_thread_pool.rb +39 -0
  340. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/auto_restrict_eager_graph.rb +62 -0
  341. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/auto_validations.rb +302 -0
  342. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/auto_validations_constraint_validations_presence_message.rb +68 -0
  343. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/before_after_save.rb +8 -0
  344. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/blacklist_security.rb +104 -0
  345. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/boolean_readers.rb +59 -0
  346. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/boolean_subsets.rb +64 -0
  347. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/caching.rb +164 -0
  348. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/class_table_inheritance.rb +439 -0
  349. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/class_table_inheritance_constraint_validations.rb +82 -0
  350. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/column_conflicts.rb +108 -0
  351. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/column_encryption.rb +749 -0
  352. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/column_select.rb +61 -0
  353. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/columns_updated.rb +42 -0
  354. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/composition.rb +205 -0
  355. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/concurrent_eager_loading.rb +174 -0
  356. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/constraint_validations.rb +259 -0
  357. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/csv_serializer.rb +196 -0
  358. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/dataset_associations.rb +152 -0
  359. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/def_dataset_method.rb +90 -0
  360. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/defaults_setter.rb +158 -0
  361. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/delay_add_association.rb +53 -0
  362. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/deprecated_associations.rb +151 -0
  363. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/detect_unnecessary_association_options.rb +164 -0
  364. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/dirty.rb +276 -0
  365. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/eager_each.rb +88 -0
  366. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/eager_graph_eager.rb +139 -0
  367. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/empty_failure_backtraces.rb +38 -0
  368. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/enum.rb +124 -0
  369. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/error_splitter.rb +61 -0
  370. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/finder.rb +248 -0
  371. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/forbid_lazy_load.rb +229 -0
  372. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/force_encoding.rb +78 -0
  373. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/hook_class_methods.rb +110 -0
  374. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/input_transformer.rb +89 -0
  375. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/insert_conflict.rb +76 -0
  376. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/insert_returning_select.rb +81 -0
  377. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/inspect_pk.rb +44 -0
  378. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/instance_filters.rb +138 -0
  379. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/instance_hooks.rb +115 -0
  380. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/instance_specific_default.rb +113 -0
  381. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/inverted_subsets.rb +60 -0
  382. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/json_serializer.rb +445 -0
  383. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/lazy_attributes.rb +126 -0
  384. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/list.rb +208 -0
  385. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/many_through_many.rb +437 -0
  386. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/modification_detection.rb +102 -0
  387. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/mssql_optimistic_locking.rb +65 -0
  388. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/nested_attributes.rb +340 -0
  389. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/optimistic_locking.rb +54 -0
  390. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/optimistic_locking_base.rb +55 -0
  391. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/paged_operations.rb +184 -0
  392. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/pg_array_associations.rb +580 -0
  393. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/pg_auto_constraint_validations.rb +361 -0
  394. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/pg_auto_validate_enums.rb +88 -0
  395. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/pg_eager_any_typed_array.rb +95 -0
  396. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/pg_row.rb +79 -0
  397. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/pg_xmin_optimistic_locking.rb +109 -0
  398. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/prepared_statements.rb +196 -0
  399. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  400. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/primary_key_lookup_check_values.rb +154 -0
  401. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/rcte_tree.rb +343 -0
  402. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/require_valid_schema.rb +67 -0
  403. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/serialization.rb +242 -0
  404. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/serialization_modification_detection.rb +87 -0
  405. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/sharding.rb +126 -0
  406. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/single_statement_dataset_destroy.rb +49 -0
  407. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/single_table_inheritance.rb +271 -0
  408. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/singular_table_names.rb +33 -0
  409. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/skip_create_refresh.rb +37 -0
  410. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/skip_saving_columns.rb +108 -0
  411. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/split_values.rb +81 -0
  412. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/sql_comments.rb +194 -0
  413. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/static_cache.rb +315 -0
  414. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/static_cache_cache.rb +94 -0
  415. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/string_stripper.rb +59 -0
  416. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/subclasses.rb +96 -0
  417. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/subset_conditions.rb +128 -0
  418. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/subset_static_cache.rb +278 -0
  419. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/table_select.rb +50 -0
  420. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/tactical_eager_loading.rb +216 -0
  421. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/throw_failures.rb +110 -0
  422. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/timestamps.rb +109 -0
  423. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/touch.rb +153 -0
  424. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/tree.rb +188 -0
  425. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/typecast_on_load.rb +90 -0
  426. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/unlimited_update.rb +27 -0
  427. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/unused_associations.rb +529 -0
  428. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/update_or_create.rb +64 -0
  429. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/update_primary_key.rb +72 -0
  430. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/update_refresh.rb +88 -0
  431. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/uuid.rb +70 -0
  432. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/validate_associated.rb +85 -0
  433. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/validation_class_methods.rb +460 -0
  434. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/validation_contexts.rb +49 -0
  435. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/validation_helpers.rb +351 -0
  436. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/validation_helpers_generic_type_messages.rb +73 -0
  437. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/whitelist_security.rb +122 -0
  438. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/plugins/xml_serializer.rb +411 -0
  439. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/sql.rb +2061 -0
  440. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/timezones.rb +254 -0
  441. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel/version.rb +25 -0
  442. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sequel-5.102.0/lib/sequel.rb +3 -0
  443. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/CHANGELOG.md +1014 -0
  444. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/CONTRIBUTING.md +60 -0
  445. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/FAQ.md +399 -0
  446. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/INSTALLATION.md +267 -0
  447. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/LICENSE +23 -0
  448. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/README.md +198 -0
  449. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/dependencies.yml +13 -0
  450. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/aggregator.c +270 -0
  451. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/aggregator.h +10 -0
  452. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/backup.c +190 -0
  453. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/backup.h +15 -0
  454. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/database.c +1006 -0
  455. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/database.h +28 -0
  456. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/exception.c +122 -0
  457. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/exception.h +12 -0
  458. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/extconf.rb +297 -0
  459. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/sqlite3.c +225 -0
  460. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/sqlite3_ruby.h +48 -0
  461. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/statement.c +739 -0
  462. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/statement.h +17 -0
  463. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/ext/sqlite3/timespec.h +20 -0
  464. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/3.2/sqlite3_native.so +0 -0
  465. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/3.3/sqlite3_native.so +0 -0
  466. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/3.4/sqlite3_native.so +0 -0
  467. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/4.0/sqlite3_native.so +0 -0
  468. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/constants.rb +198 -0
  469. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/database.rb +798 -0
  470. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/errors.rb +88 -0
  471. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/fork_safety.rb +66 -0
  472. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/pragmas.rb +648 -0
  473. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/resultset.rb +96 -0
  474. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/statement.rb +190 -0
  475. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/value.rb +54 -0
  476. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/version.rb +4 -0
  477. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3/version_info.rb +17 -0
  478. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/sqlite3-2.9.1-x64-mingw-ucrt/lib/sqlite3.rb +19 -0
  479. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/Gemfile +10 -0
  480. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/LICENSE.txt +22 -0
  481. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/README.md +63 -0
  482. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/Rakefile +10 -0
  483. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/accesslog.rb +157 -0
  484. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/cgi.rb +313 -0
  485. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/compat.rb +36 -0
  486. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/config.rb +158 -0
  487. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/cookie.rb +172 -0
  488. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/htmlutils.rb +30 -0
  489. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/authenticator.rb +117 -0
  490. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/basicauth.rb +116 -0
  491. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/digestauth.rb +395 -0
  492. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/htdigest.rb +132 -0
  493. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/htgroup.rb +97 -0
  494. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/htpasswd.rb +158 -0
  495. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth/userdb.rb +53 -0
  496. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpauth.rb +96 -0
  497. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpproxy.rb +354 -0
  498. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httprequest.rb +668 -0
  499. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpresponse.rb +588 -0
  500. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/https.rb +152 -0
  501. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpserver.rb +294 -0
  502. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet/abstract.rb +152 -0
  503. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet/cgi_runner.rb +47 -0
  504. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet/cgihandler.rb +126 -0
  505. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet/erbhandler.rb +88 -0
  506. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet/filehandler.rb +552 -0
  507. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet/prochandler.rb +48 -0
  508. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpservlet.rb +23 -0
  509. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpstatus.rb +194 -0
  510. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httputils.rb +543 -0
  511. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/httpversion.rb +76 -0
  512. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/log.rb +156 -0
  513. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/server.rb +380 -0
  514. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/ssl.rb +219 -0
  515. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/utils.rb +265 -0
  516. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick/version.rb +18 -0
  517. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/lib/webrick.rb +232 -0
  518. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/accesslog.rbs +24 -0
  519. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/cgi.rbs +92 -0
  520. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/compat.rbs +18 -0
  521. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/config.rbs +17 -0
  522. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/cookie.rbs +37 -0
  523. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/htmlutils.rbs +5 -0
  524. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/authenticator.rbs +55 -0
  525. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/basicauth.rbs +29 -0
  526. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/digestauth.rbs +85 -0
  527. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/htdigest.rbs +31 -0
  528. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/htgroup.rbs +21 -0
  529. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/htpasswd.rbs +31 -0
  530. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth/userdb.rbs +13 -0
  531. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpauth.rbs +13 -0
  532. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpproxy.rbs +61 -0
  533. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httprequest.rbs +167 -0
  534. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpresponse.rbs +117 -0
  535. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/https.rbs +49 -0
  536. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpserver.rbs +71 -0
  537. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet/abstract.rbs +36 -0
  538. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet/cgi_runner.rbs +3 -0
  539. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet/cgihandler.rbs +23 -0
  540. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet/erbhandler.rbs +17 -0
  541. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet/filehandler.rbs +76 -0
  542. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet/prochandler.rbs +21 -0
  543. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpservlet.rbs +4 -0
  544. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpstatus.rbs +255 -0
  545. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httputils.rbs +116 -0
  546. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/httpversion.rbs +17 -0
  547. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/log.rbs +93 -0
  548. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/manifest.yaml +8 -0
  549. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/server.rbs +57 -0
  550. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/ssl.rbs +19 -0
  551. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/utils.rbs +122 -0
  552. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/sig/version.rbs +3 -0
  553. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/gems/webrick-1.9.2/webrick.gemspec +105 -0
  554. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/specifications/bigdecimal-4.0.1.gemspec +25 -0
  555. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/specifications/rack-3.2.5.gemspec +31 -0
  556. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/specifications/rackup-2.3.1.gemspec +26 -0
  557. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/specifications/sequel-5.102.0.gemspec +36 -0
  558. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/specifications/sqlite3-2.9.1-x64-mingw-ucrt.gemspec +25 -0
  559. data/examples/demo_crud/vendor/bundle/ruby/3.4.0/specifications/webrick-1.9.2.gemspec +22 -0
  560. data/lib/serrano/application.rb +36 -0
  561. data/lib/serrano/cli/base.rb +92 -0
  562. data/lib/serrano/cli/generate.rb +285 -0
  563. data/lib/serrano/cli/templates/controller.rb.tt +92 -0
  564. data/lib/serrano/cli/templates/entity.rb.tt +43 -0
  565. data/lib/serrano/cli/templates/entity_validatable.rb.tt +136 -0
  566. data/lib/serrano/cli/templates/migration.rb.tt +19 -0
  567. data/lib/serrano/cli/templates/new_default_config.ru.tt +6 -0
  568. data/lib/serrano/cli/templates/new_default_db.rb.tt +3 -0
  569. data/lib/serrano/cli/templates/new_default_gemfile.tt +6 -0
  570. data/lib/serrano/cli/templates/new_minimal_config.ru.tt +5 -0
  571. data/lib/serrano/cli/templates/new_minimal_gemfile.tt +4 -0
  572. data/lib/serrano/cli/templates/new_project_config.ru.tt +8 -0
  573. data/lib/serrano/cli/templates/new_project_db.rb.tt +3 -0
  574. data/lib/serrano/cli/templates/new_project_gemfile.tt +8 -0
  575. data/lib/serrano/cli/templates/repository.rb.tt +42 -0
  576. data/lib/serrano/cli/templates/service_create.rb.tt +40 -0
  577. data/lib/serrano/cli/templates/service_destroy.rb.tt +20 -0
  578. data/lib/serrano/cli/templates/service_generic.rb.tt +15 -0
  579. data/lib/serrano/cli/templates/service_index.rb.tt +17 -0
  580. data/lib/serrano/cli/templates/service_show.rb.tt +20 -0
  581. data/lib/serrano/cli/templates/service_update.rb.tt +41 -0
  582. data/lib/serrano/dispatcher.rb +53 -0
  583. data/lib/serrano/request.rb +78 -0
  584. data/lib/serrano/response.rb +33 -0
  585. data/lib/serrano/router.rb +80 -0
  586. data/lib/serrano/version.rb +5 -0
  587. data/lib/serrano.rb +11 -0
  588. metadata +768 -0
@@ -0,0 +1,4066 @@
1
+ # frozen-string-literal: true
2
+
3
+ module Sequel
4
+ class Model
5
+ # Associations are used in order to specify relationships between model classes
6
+ # that reflect relations between tables in the database using foreign keys.
7
+ module Associations
8
+ # Map of association type symbols to association reflection classes.
9
+ ASSOCIATION_TYPES = {}
10
+
11
+ # Set an empty association reflection hash in the model
12
+ def self.apply(model)
13
+ model.instance_exec do
14
+ @association_reflections = {}
15
+ @autoreloading_associations = {}
16
+ @cache_associations = true
17
+ @default_eager_limit_strategy = true
18
+ @default_association_options = {}
19
+ @default_association_type_options = {}
20
+ @dataset_module_class = DatasetModule
21
+ end
22
+ end
23
+
24
+ # The dataset module to use for classes using the associations plugin.
25
+ class DatasetModule < Model::DatasetModule
26
+ def_dataset_caching_method(self, :eager)
27
+ end
28
+
29
+ # AssociationReflection is a Hash subclass that keeps information on Sequel::Model associations. It
30
+ # provides methods to reduce internal code duplication. It should not
31
+ # be instantiated by the user.
32
+ class AssociationReflection < Hash
33
+ include Sequel::Inflections
34
+
35
+ # Name symbol for the _add internal association method
36
+ def _add_method
37
+ self[:_add_method]
38
+ end
39
+
40
+ # Name symbol for the _remove_all internal association method
41
+ def _remove_all_method
42
+ self[:_remove_all_method]
43
+ end
44
+
45
+ # Name symbol for the _remove internal association method
46
+ def _remove_method
47
+ self[:_remove_method]
48
+ end
49
+
50
+ # Name symbol for the _setter association method
51
+ def _setter_method
52
+ self[:_setter_method]
53
+ end
54
+
55
+ # Name symbol for the add association method
56
+ def add_method
57
+ self[:add_method]
58
+ end
59
+
60
+ # Name symbol for association method, the same as the name of the association.
61
+ def association_method
62
+ self[:name]
63
+ end
64
+
65
+ # The class associated to the current model class via this association
66
+ def associated_class
67
+ cached_fetch(:class) do
68
+ begin
69
+ constantize(self[:class_name])
70
+ rescue NameError => e
71
+ raise NameError, "#{e.message} (this happened when attempting to find the associated class for #{inspect})", e.backtrace
72
+ end
73
+ end
74
+ end
75
+
76
+ # The dataset associated via this association, with the non-instance specific
77
+ # changes already applied. This will be a joined dataset if the association
78
+ # requires joining tables.
79
+ def associated_dataset
80
+ cached_fetch(:_dataset){apply_dataset_changes(_associated_dataset)}
81
+ end
82
+
83
+ # Apply all non-instance specific changes to the given dataset and return it.
84
+ def apply_dataset_changes(ds)
85
+ ds = ds.with_extend(AssociationDatasetMethods).clone(:association_reflection => self)
86
+ if exts = self[:reverse_extend]
87
+ ds = ds.with_extend(*exts)
88
+ end
89
+ ds = ds.select(*select) if select
90
+ if c = self[:conditions]
91
+ ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.where(*c) : ds.where(c)
92
+ end
93
+ ds = ds.order(*self[:order]) if self[:order]
94
+ ds = ds.limit(*self[:limit]) if self[:limit]
95
+ ds = ds.limit(1).skip_limit_check if limit_to_single_row?
96
+ ds = ds.eager(self[:eager]) if self[:eager]
97
+ ds = ds.distinct if self[:distinct]
98
+ ds
99
+ end
100
+
101
+ # Apply all non-instance specific changes and the eager_block option to the given
102
+ # dataset and return it.
103
+ def apply_eager_dataset_changes(ds)
104
+ ds = apply_dataset_changes(ds)
105
+ if block = self[:eager_block]
106
+ ds = block.call(ds)
107
+ end
108
+ ds
109
+ end
110
+
111
+ # Apply the eager graph limit strategy to the dataset to graph into the current dataset, or return
112
+ # the dataset unmodified if no SQL limit strategy is needed.
113
+ def apply_eager_graph_limit_strategy(strategy, ds)
114
+ case strategy
115
+ when :distinct_on
116
+ apply_distinct_on_eager_limit_strategy(ds.order_prepend(*self[:order]))
117
+ when :window_function
118
+ apply_window_function_eager_limit_strategy(ds.order_prepend(*self[:order])).select(*ds.columns)
119
+ else
120
+ ds
121
+ end
122
+ end
123
+
124
+ # Apply an eager limit strategy to the dataset, or return the dataset
125
+ # unmodified if it doesn't need an eager limit strategy.
126
+ def apply_eager_limit_strategy(ds, strategy=eager_limit_strategy, limit_and_offset=limit_and_offset())
127
+ case strategy
128
+ when :distinct_on
129
+ apply_distinct_on_eager_limit_strategy(ds)
130
+ when :window_function
131
+ apply_window_function_eager_limit_strategy(ds, limit_and_offset)
132
+ else
133
+ ds
134
+ end
135
+ end
136
+
137
+ # Use DISTINCT ON and ORDER BY clauses to limit the results to the first record with matching keys.
138
+ def apply_distinct_on_eager_limit_strategy(ds)
139
+ keys = predicate_key
140
+ ds.distinct(*keys).order_prepend(*keys)
141
+ end
142
+
143
+ # Use a window function to limit the results of the eager loading dataset.
144
+ def apply_window_function_eager_limit_strategy(ds, limit_and_offset=limit_and_offset())
145
+ rn = ds.row_number_column
146
+ limit, offset = limit_and_offset
147
+ ds = ds.unordered.select_append{|o| o.row_number.function.over(:partition=>predicate_key, :order=>ds.opts[:order]).as(rn)}.from_self
148
+ ds = ds.order(rn) if ds.db.database_type == :mysql
149
+ ds = if !returns_array?
150
+ ds.where(rn => offset ? offset+1 : 1)
151
+ elsif offset
152
+ offset += 1
153
+ if limit
154
+ ds.where(rn => (offset...(offset+limit)))
155
+ else
156
+ ds.where{SQL::Identifier.new(rn) >= offset}
157
+ end
158
+ else
159
+ ds.where{SQL::Identifier.new(rn) <= limit}
160
+ end
161
+ end
162
+
163
+ # If the ruby eager limit strategy is being used, slice the array using the slice
164
+ # range to return the object(s) at the correct offset/limit.
165
+ def apply_ruby_eager_limit_strategy(rows, limit_and_offset = limit_and_offset())
166
+ name = self[:name]
167
+ return unless range = slice_range(limit_and_offset)
168
+ if returns_array?
169
+ rows.each{|o| o.associations[name] = o.associations[name][range] || []}
170
+ else
171
+ offset = range.begin
172
+ rows.each{|o| o.associations[name] = o.associations[name][offset]}
173
+ end
174
+ end
175
+
176
+ # Whether the associations cache should use an array when storing the
177
+ # associated records during eager loading.
178
+ def assign_singular?
179
+ !returns_array?
180
+ end
181
+
182
+ # Whether this association can have associated objects, given the current
183
+ # object. Should be false if obj cannot have associated objects because
184
+ # the necessary key columns are NULL.
185
+ def can_have_associated_objects?(obj)
186
+ true
187
+ end
188
+
189
+ # Whether you are able to clone from the given association type to the current
190
+ # association type, true by default only if the types match.
191
+ def cloneable?(ref)
192
+ ref[:type] == self[:type]
193
+ end
194
+
195
+ # Name symbol for the dataset association method
196
+ def dataset_method
197
+ self[:dataset_method]
198
+ end
199
+
200
+ # Whether the dataset needs a primary key to function, true by default.
201
+ def dataset_need_primary_key?
202
+ true
203
+ end
204
+
205
+ # Return the symbol used for the row number column if the window function
206
+ # eager limit strategy is being used, or nil otherwise.
207
+ def delete_row_number_column(ds=associated_dataset)
208
+ if eager_limit_strategy == :window_function
209
+ ds.row_number_column
210
+ end
211
+ end
212
+
213
+ # Return an dataset that will load the appropriate associated objects for
214
+ # the given object using this association.
215
+ def association_dataset_for(object)
216
+ condition = if can_have_associated_objects?(object)
217
+ predicate_keys.zip(predicate_key_values(object))
218
+ else
219
+ false
220
+ end
221
+
222
+ associated_dataset.where(condition)
223
+ end
224
+
225
+ ASSOCIATION_DATASET_PROC = proc{|r| r.association_dataset_for(self)}
226
+ # Proc used to create the association dataset method.
227
+ def association_dataset_proc
228
+ ASSOCIATION_DATASET_PROC
229
+ end
230
+
231
+ # The eager_graph limit strategy to use for this dataset
232
+ def eager_graph_limit_strategy(strategy)
233
+ if self[:limit] || !returns_array?
234
+ strategy = strategy[self[:name]] if strategy.is_a?(Hash)
235
+ case strategy
236
+ when true
237
+ true_eager_graph_limit_strategy
238
+ when Symbol
239
+ strategy
240
+ else
241
+ if returns_array? || offset
242
+ :ruby
243
+ end
244
+ end
245
+ end
246
+ end
247
+
248
+ # The eager limit strategy to use for this dataset.
249
+ def eager_limit_strategy
250
+ cached_fetch(:_eager_limit_strategy) do
251
+ if self[:limit] || !returns_array?
252
+ case s = cached_fetch(:eager_limit_strategy){default_eager_limit_strategy}
253
+ when true
254
+ true_eager_limit_strategy
255
+ else
256
+ s
257
+ end
258
+ end
259
+ end
260
+ end
261
+
262
+ # Eager load the associated objects using the hash of eager options,
263
+ # yielding each row to the block.
264
+ def eager_load_results(eo, &block)
265
+ rows = eo[:rows]
266
+ unless eo[:initialize_rows] == false
267
+ Sequel.synchronize_with(eo[:mutex]){initialize_association_cache(rows)}
268
+ end
269
+ if eo[:id_map]
270
+ ids = eo[:id_map].keys
271
+ return ids if ids.empty?
272
+ end
273
+ strategy = eager_limit_strategy
274
+ cascade = eo[:associations]
275
+ eager_limit = nil
276
+
277
+ if eo[:no_results]
278
+ no_results = true
279
+ elsif eo[:eager_block] || eo[:loader] == false || !use_placeholder_loader?
280
+ ds = eager_loading_dataset(eo)
281
+
282
+ strategy = ds.opts[:eager_limit_strategy] || strategy
283
+
284
+ eager_limit =
285
+ if el = ds.opts[:eager_limit]
286
+ raise Error, "The :eager_limit dataset option is not supported for associations returning a single record" unless returns_array?
287
+ strategy ||= true_eager_graph_limit_strategy
288
+ if el.is_a?(Array)
289
+ el
290
+ else
291
+ [el, nil]
292
+ end
293
+ else
294
+ limit_and_offset
295
+ end
296
+
297
+ strategy = true_eager_graph_limit_strategy if strategy == :union
298
+ # Correlated subqueries are not supported for regular eager loading
299
+ strategy = :ruby if strategy == :correlated_subquery
300
+ strategy = nil if strategy == :ruby && assign_singular?
301
+ objects = apply_eager_limit_strategy(ds, strategy, eager_limit).all
302
+
303
+ if strategy == :window_function
304
+ delete_rn = ds.row_number_column
305
+ objects.each{|obj| obj.remove_key!(delete_rn)}
306
+ end
307
+ elsif strategy == :union
308
+ objects = []
309
+ ds = associated_dataset
310
+ loader = union_eager_loader
311
+ joiner = " UNION ALL "
312
+ ids.each_slice(subqueries_per_union).each do |slice|
313
+ sql = loader.send(:sql_origin)
314
+ join = false
315
+ slice.each do |k|
316
+ if join
317
+ sql << joiner
318
+ else
319
+ join = true
320
+ end
321
+ loader.append_sql(sql, *k)
322
+ end
323
+ objects.concat(ds.with_sql(sql).to_a)
324
+ end
325
+ ds = ds.eager(cascade) if cascade
326
+ ds.send(:post_load, objects)
327
+ else
328
+ loader = placeholder_eager_loader
329
+ loader = loader.with_dataset{|dataset| dataset.eager(cascade)} if cascade
330
+ objects = loader.all(ids)
331
+ end
332
+
333
+ Sequel.synchronize_with(eo[:mutex]){objects.each(&block)} unless no_results
334
+
335
+ if strategy == :ruby
336
+ apply_ruby_eager_limit_strategy(rows, eager_limit || limit_and_offset)
337
+ end
338
+ end
339
+
340
+ # The key to use for the key hash when eager loading
341
+ def eager_loader_key
342
+ self[:eager_loader_key]
343
+ end
344
+
345
+ # By default associations do not need to select a key in an associated table
346
+ # to eagerly load.
347
+ def eager_loading_use_associated_key?
348
+ false
349
+ end
350
+
351
+ # Whether to eagerly graph a lazy dataset, true by default. If this
352
+ # is false, the association won't respect the :eager_graph option
353
+ # when loading the association for a single record.
354
+ def eager_graph_lazy_dataset?
355
+ true
356
+ end
357
+
358
+ # Whether additional conditions should be added when using the filter
359
+ # by associations support.
360
+ def filter_by_associations_add_conditions?
361
+ self[:conditions] || self[:eager_block] || self[:limit]
362
+ end
363
+
364
+ # The expression to use for the additional conditions to be added for
365
+ # the filter by association support, when the association itself is
366
+ # filtered. Works by using a subquery to test that the objects passed
367
+ # also meet the association filter criteria.
368
+ def filter_by_associations_conditions_expression(obj)
369
+ ds = filter_by_associations_conditions_dataset.where(filter_by_associations_conditions_subquery_conditions(obj))
370
+ {filter_by_associations_conditions_key=>ds}
371
+ end
372
+
373
+ # Finalize the association by first attempting to populate the thread-safe cache,
374
+ # and then transfering the thread-safe cache value to the association itself,
375
+ # so that a mutex is not needed to get the value.
376
+ def finalize
377
+ return unless cache = self[:cache]
378
+
379
+ finalizer = proc do |meth, key|
380
+ next if has_key?(key)
381
+
382
+ # Allow calling private methods to make sure caching is done appropriately
383
+ send(meth)
384
+ self[key] = cache.delete(key) if cache.has_key?(key)
385
+ end
386
+
387
+ finalize_settings.each(&finalizer)
388
+
389
+ unless self[:instance_specific]
390
+ finalizer.call(:associated_eager_dataset, :associated_eager_dataset)
391
+ finalizer.call(:filter_by_associations_conditions_dataset, :filter_by_associations_conditions_dataset)
392
+ end
393
+
394
+ nil
395
+ end
396
+
397
+ # Map of methods to cache keys used for finalizing associations.
398
+ FINALIZE_SETTINGS = {
399
+ :associated_class=>:class,
400
+ :associated_dataset=>:_dataset,
401
+ :eager_limit_strategy=>:_eager_limit_strategy,
402
+ :placeholder_loader=>:placeholder_loader,
403
+ :predicate_key=>:predicate_key,
404
+ :predicate_keys=>:predicate_keys,
405
+ :reciprocal=>:reciprocal,
406
+ }.freeze
407
+ def finalize_settings
408
+ FINALIZE_SETTINGS
409
+ end
410
+
411
+ # Whether to handle silent modification failure when adding/removing
412
+ # associated records, false by default.
413
+ def handle_silent_modification_failure?
414
+ false
415
+ end
416
+
417
+ # Hash value for the association reflection. This is precomputed to avoid
418
+ # concurrency issues at runtime.
419
+ def hash
420
+ self[:_hash]
421
+ end
422
+
423
+ # Initialize the associations cache for the current association for the given objects.
424
+ def initialize_association_cache(objects)
425
+ name = self[:name]
426
+ if assign_singular?
427
+ objects.each{|object| object.associations[name] = nil}
428
+ else
429
+ objects.each{|object| object.associations[name] = []}
430
+ end
431
+ end
432
+
433
+ # Show which type of reflection this is, and a guess at what code was used to create the
434
+ # association.
435
+ def inspect
436
+ o = self[:orig_opts].dup
437
+ o.delete(:class)
438
+ o.delete(:class_name)
439
+ o.delete(:block) unless o[:block]
440
+ o[:class] = self[:orig_class] if self[:orig_class]
441
+
442
+ "#<#{self.class} #{self[:model]}.#{self[:type]} #{self[:name].inspect}#{", #{o.inspect[1...-1]}" unless o.empty?}>"
443
+ end
444
+
445
+ # The limit and offset for this association (returned as a two element array).
446
+ def limit_and_offset
447
+ if (v = self[:limit]).is_a?(Array)
448
+ v
449
+ else
450
+ [v, nil]
451
+ end
452
+ end
453
+
454
+ # Whether the associated object needs a primary key to be added/removed,
455
+ # false by default.
456
+ def need_associated_primary_key?
457
+ false
458
+ end
459
+
460
+ # A placeholder literalizer that can be used to lazily load the association. If
461
+ # one can't be used, returns nil.
462
+ def placeholder_loader
463
+ if use_placeholder_loader?
464
+ cached_fetch(:placeholder_loader) do
465
+ associated_dataset.placeholder_literalizer_loader do |pl, ds|
466
+ ds = ds.where(Sequel.&(*predicate_keys.map{|k| SQL::BooleanExpression.new(:'=', k, pl.arg)}))
467
+ if self[:block]
468
+ ds = self[:block].call(ds)
469
+ end
470
+ ds
471
+ end
472
+ end
473
+ end
474
+ end
475
+
476
+ # The keys to use for loading of the regular dataset, as an array.
477
+ def predicate_keys
478
+ cached_fetch(:predicate_keys){Array(predicate_key)}
479
+ end
480
+
481
+ # The values that predicate_keys should match for objects to be associated.
482
+ def predicate_key_values(object)
483
+ predicate_key_methods.map{|k| object.get_column_value(k)}
484
+ end
485
+
486
+ # Qualify +col+ with the given table name.
487
+ def qualify(table, col)
488
+ transform(col) do |k|
489
+ case k
490
+ when Symbol, SQL::Identifier
491
+ SQL::QualifiedIdentifier.new(table, k)
492
+ else
493
+ Sequel::Qualifier.new(table).transform(k)
494
+ end
495
+ end
496
+ end
497
+
498
+ # Qualify col with the associated model's table name.
499
+ def qualify_assoc(col)
500
+ qualify(associated_class.table_name, col)
501
+ end
502
+
503
+ # Qualify col with the current model's table name.
504
+ def qualify_cur(col)
505
+ qualify(self[:model].table_name, col)
506
+ end
507
+
508
+ # Returns the reciprocal association variable, if one exists. The reciprocal
509
+ # association is the association in the associated class that is the opposite
510
+ # of the current association. For example, Album.many_to_one :artist and
511
+ # Artist.one_to_many :albums are reciprocal associations. This information is
512
+ # to populate reciprocal associations. For example, when you do this_artist.add_album(album)
513
+ # it sets album.artist to this_artist.
514
+ def reciprocal
515
+ cached_fetch(:reciprocal) do
516
+ possible_recips = []
517
+
518
+ associated_class.all_association_reflections.each do |assoc_reflect|
519
+ if reciprocal_association?(assoc_reflect)
520
+ possible_recips << assoc_reflect
521
+ end
522
+ end
523
+
524
+ if possible_recips.length == 1
525
+ cached_set(:reciprocal_type, possible_recips.first[:type]) if ambiguous_reciprocal_type?
526
+ possible_recips.first[:name]
527
+ end
528
+ end
529
+ end
530
+
531
+ # Whether the reciprocal of this association returns an array of objects instead of a single object,
532
+ # true by default.
533
+ def reciprocal_array?
534
+ true
535
+ end
536
+
537
+ # Name symbol for the remove_all_ association method
538
+ def remove_all_method
539
+ self[:remove_all_method]
540
+ end
541
+
542
+ # Whether associated objects need to be removed from the association before
543
+ # being destroyed in order to preserve referential integrity.
544
+ def remove_before_destroy?
545
+ true
546
+ end
547
+
548
+ # Name symbol for the remove_ association method
549
+ def remove_method
550
+ self[:remove_method]
551
+ end
552
+
553
+ # Whether to check that an object to be disassociated is already associated to this object, false by default.
554
+ def remove_should_check_existing?
555
+ false
556
+ end
557
+
558
+ # Whether this association returns an array of objects instead of a single object,
559
+ # true by default.
560
+ def returns_array?
561
+ true
562
+ end
563
+
564
+ # The columns to select when loading the association.
565
+ def select
566
+ self[:select]
567
+ end
568
+
569
+ # Whether to set the reciprocal association to self when loading associated
570
+ # records, false by default.
571
+ def set_reciprocal_to_self?
572
+ false
573
+ end
574
+
575
+ # Name symbol for the setter association method
576
+ def setter_method
577
+ self[:setter_method]
578
+ end
579
+
580
+ # The range used for slicing when using the :ruby eager limit strategy.
581
+ def slice_range(limit_and_offset = limit_and_offset())
582
+ limit, offset = limit_and_offset
583
+ if limit || offset
584
+ (offset||0)..(limit ? (offset||0)+limit-1 : -1)
585
+ end
586
+ end
587
+
588
+ private
589
+
590
+ # If the key exists in the reflection hash, return it.
591
+ # If the key doesn't exist and association reflections are uncached, then yield to get the value.
592
+ # If the key doesn't exist and association reflection are cached, check the cache and return
593
+ # the value if present, or yield to get the value, cache the value, and return it.
594
+ def cached_fetch(key)
595
+ fetch(key) do
596
+ return yield unless h = self[:cache]
597
+ Sequel.synchronize{return h[key] if h.has_key?(key)}
598
+ value = yield
599
+ Sequel.synchronize{h[key] = value}
600
+ end
601
+ end
602
+
603
+ # Cache the value at the given key if caching.
604
+ def cached_set(key, value)
605
+ return unless h = self[:cache]
606
+ Sequel.synchronize{h[key] = value}
607
+ end
608
+
609
+ # The base dataset used for the association, before any order/conditions
610
+ # options have been applied.
611
+ def _associated_dataset
612
+ associated_class.dataset
613
+ end
614
+
615
+ # Whether for the reciprocal type for the given association cannot be
616
+ # known in advantage, false by default.
617
+ def ambiguous_reciprocal_type?
618
+ false
619
+ end
620
+
621
+ # Apply a limit strategy to the given dataset so that filter by
622
+ # associations works with a limited dataset.
623
+ def apply_filter_by_associations_limit_strategy(ds)
624
+ case filter_by_associations_limit_strategy
625
+ when :distinct_on
626
+ apply_filter_by_associations_distinct_on_limit_strategy(ds)
627
+ when :window_function
628
+ apply_filter_by_associations_window_function_limit_strategy(ds)
629
+ else
630
+ ds
631
+ end
632
+ end
633
+
634
+ # Apply a distinct on eager limit strategy using IN with a subquery
635
+ # that uses DISTINCT ON to ensure only the first matching record for
636
+ # each key is included.
637
+ def apply_filter_by_associations_distinct_on_limit_strategy(ds)
638
+ k = filter_by_associations_limit_key
639
+ ds.where(k=>apply_distinct_on_eager_limit_strategy(associated_eager_dataset.select(*k)))
640
+ end
641
+
642
+ # Apply a distinct on eager limit strategy using IN with a subquery
643
+ # that uses a filter on the row_number window function to ensure
644
+ # that only rows inside the limit are returned.
645
+ def apply_filter_by_associations_window_function_limit_strategy(ds)
646
+ ds.where(filter_by_associations_limit_key=>apply_window_function_eager_limit_strategy(associated_eager_dataset.select(*filter_by_associations_limit_alias_key)).select(*filter_by_associations_limit_aliases))
647
+ end
648
+
649
+ # The associated_dataset with the eager_block callback already applied.
650
+ def associated_eager_dataset
651
+ cached_fetch(:associated_eager_dataset) do
652
+ ds = associated_dataset.unlimited
653
+ if block = self[:eager_block]
654
+ ds = block.call(ds)
655
+ end
656
+ ds
657
+ end
658
+ end
659
+
660
+ # The dataset to use for eager loading associated objects for multiple current objects,
661
+ # given the hash passed to the eager loader.
662
+ def eager_loading_dataset(eo=OPTS)
663
+ ds = eo[:dataset] || associated_eager_dataset
664
+ ds = eager_loading_set_predicate_condition(ds, eo)
665
+ if associations = eo[:associations]
666
+ ds = ds.eager(associations)
667
+ end
668
+ if block = eo[:eager_block]
669
+ orig_ds = ds
670
+ ds = block.call(ds)
671
+ end
672
+ if eager_loading_use_associated_key?
673
+ ds = if ds.opts[:eager_graph] && !orig_ds.opts[:eager_graph]
674
+ block.call(orig_ds.select_append(*associated_key_array))
675
+ else
676
+ ds.select_append(*associated_key_array)
677
+ end
678
+ end
679
+ if self[:eager_graph]
680
+ raise(Error, "cannot eagerly load a #{self[:type]} association that uses :eager_graph") if eager_loading_use_associated_key?
681
+ ds = ds.eager_graph(self[:eager_graph])
682
+ end
683
+ ds
684
+ end
685
+
686
+ # The default eager limit strategy to use for this association
687
+ def default_eager_limit_strategy
688
+ self[:model].default_eager_limit_strategy || :ruby
689
+ end
690
+
691
+ # Set the predicate condition for the eager loading dataset based on the id map
692
+ # in the eager loading options.
693
+ def eager_loading_set_predicate_condition(ds, eo)
694
+ if id_map = eo[:id_map]
695
+ ds = ds.where(eager_loading_predicate_condition(id_map.keys))
696
+ end
697
+ ds
698
+ end
699
+
700
+ # The predicate condition to use for the eager_loader.
701
+ def eager_loading_predicate_condition(keys)
702
+ if transform = self[:eager_loading_predicate_transform]
703
+ keys = transform.call(keys, self)
704
+ end
705
+ {predicate_key=>keys}
706
+ end
707
+
708
+ # Add conditions to the dataset to not include NULL values for
709
+ # the associated keys, and select those keys.
710
+ def filter_by_associations_add_conditions_dataset_filter(ds)
711
+ k = filter_by_associations_conditions_associated_keys
712
+ ds.select(*k).where(Sequel.negate(k.zip([])))
713
+ end
714
+
715
+ # The conditions to add to the filter by associations conditions
716
+ # subquery to restrict it to to the object(s) that was used as the
717
+ # filter value.
718
+ def filter_by_associations_conditions_subquery_conditions(obj)
719
+ key = qualify(associated_class.table_name, associated_class.primary_key)
720
+ case obj
721
+ when Array
722
+ {key=>obj.map(&:pk)}
723
+ when Sequel::Dataset
724
+ {key=>obj.select(*Array(qualify(associated_class.table_name, associated_class.primary_key)))}
725
+ else
726
+ Array(key).zip(Array(obj.pk))
727
+ end
728
+ end
729
+
730
+ # The base dataset to use for the filter by associations conditions
731
+ # subquery, regardless of the objects that are passed in as filter
732
+ # values.
733
+ def filter_by_associations_conditions_dataset
734
+ cached_fetch(:filter_by_associations_conditions_dataset) do
735
+ ds = associated_eager_dataset.unordered
736
+ ds = filter_by_associations_add_conditions_dataset_filter(ds)
737
+ ds = apply_filter_by_associations_limit_strategy(ds)
738
+ ds
739
+ end
740
+ end
741
+
742
+ # The strategy to use to filter by a limited association
743
+ def filter_by_associations_limit_strategy
744
+ v = fetch(:filter_limit_strategy, self[:eager_limit_strategy])
745
+ if v || self[:limit] || !returns_array?
746
+ case v ||= self[:model].default_eager_limit_strategy
747
+ when true, :union, :ruby
748
+ # Can't use a union or ruby-based strategy for filtering by associations, switch to default eager graph limit
749
+ # strategy.
750
+ true_eager_graph_limit_strategy
751
+ when Symbol
752
+ v
753
+ end
754
+ end
755
+ end
756
+
757
+ # Whether to limit the associated dataset to a single row.
758
+ def limit_to_single_row?
759
+ !returns_array?
760
+ end
761
+
762
+ # Any offset to use for this association (or nil if there is no offset).
763
+ def offset
764
+ limit_and_offset.last
765
+ end
766
+
767
+ # A placeholder literalizer used to speed up eager loading.
768
+ def placeholder_eager_loader
769
+ cached_fetch(:placeholder_eager_loader) do
770
+ eager_loading_dataset.placeholder_literalizer_loader do |pl, ds|
771
+ arg = pl.arg
772
+
773
+ if transform = self[:eager_loading_predicate_transform]
774
+ arg = arg.transform do |v|
775
+ transform.call(v, self)
776
+ end
777
+ end
778
+
779
+ apply_eager_limit_strategy(ds.where(predicate_key=>arg), eager_limit_strategy)
780
+ end
781
+ end
782
+ end
783
+
784
+ # The reciprocal type as an array, should be overridden in reflection subclasses that
785
+ # have ambiguous reciprocal types.
786
+ def possible_reciprocal_types
787
+ [reciprocal_type]
788
+ end
789
+
790
+ # Whether the given association reflection is possible reciprocal
791
+ # association for the current association reflection.
792
+ def reciprocal_association?(assoc_reflect)
793
+ possible_reciprocal_types.include?(assoc_reflect[:type]) &&
794
+ (begin; assoc_reflect.associated_class; rescue NameError; end) == self[:model] &&
795
+ assoc_reflect[:conditions].nil? &&
796
+ assoc_reflect[:block].nil?
797
+ end
798
+
799
+ # The number of subqueries to use in each union query, used to eagerly load
800
+ # limited associations. Defaults to 40, the optimal number depends on the
801
+ # latency between the database and the application.
802
+ def subqueries_per_union
803
+ self[:subqueries_per_union] || 40
804
+ end
805
+
806
+ # If +s+ is an array, map +s+ over the block. Otherwise, just call the
807
+ # block with +s+.
808
+ def transform(s, &block)
809
+ s.is_a?(Array) ? s.map(&block) : (yield s)
810
+ end
811
+
812
+ # What eager limit strategy should be used when true is given as the value,
813
+ # defaults to UNION as that is the fastest strategy if the appropriate keys are indexed.
814
+ def true_eager_limit_strategy
815
+ if self[:eager_graph] || (offset && !associated_dataset.supports_offsets_in_correlated_subqueries?)
816
+ # An SQL-based approach won't work if you are also eager graphing,
817
+ # so use a ruby based approach in that case.
818
+ :ruby
819
+ else
820
+ :union
821
+ end
822
+ end
823
+
824
+ # The eager_graph limit strategy used when true is given as the value, choosing the
825
+ # best strategy based on what the database supports.
826
+ def true_eager_graph_limit_strategy
827
+ if associated_class.dataset.supports_window_functions?
828
+ :window_function
829
+ else
830
+ :ruby
831
+ end
832
+ end
833
+
834
+ # A placeholder literalizer used to speed up the creation of union queries when eager
835
+ # loading a limited association.
836
+ def union_eager_loader
837
+ cached_fetch(:union_eager_loader) do
838
+ associated_dataset.placeholder_literalizer_loader do |pl, ds|
839
+ ds = self[:eager_block].call(ds) if self[:eager_block]
840
+ keys = predicate_keys
841
+ ds = ds.where(keys.map{pl.arg}.zip(keys))
842
+ if eager_loading_use_associated_key?
843
+ ds = ds.select_append(*associated_key_array)
844
+ end
845
+ ds.from_self
846
+ end
847
+ end
848
+ end
849
+
850
+ # Whether the placeholder loader can be used to load the association.
851
+ def use_placeholder_loader?
852
+ self[:use_placeholder_loader] && _associated_dataset.supports_placeholder_literalizer?
853
+ end
854
+ end
855
+
856
+ class ManyToOneAssociationReflection < AssociationReflection
857
+ ASSOCIATION_TYPES[:many_to_one] = self
858
+
859
+ # many_to_one associations can only have associated objects if none of
860
+ # the :keys options have a nil value.
861
+ def can_have_associated_objects?(obj)
862
+ !self[:keys].any?{|k| obj.get_column_value(k).nil?}
863
+ end
864
+
865
+ # Whether the dataset needs a primary key to function, false for many_to_one associations.
866
+ def dataset_need_primary_key?
867
+ false
868
+ end
869
+
870
+ # Default foreign key name symbol for foreign key in current model's table that points to
871
+ # the given association's table's primary key.
872
+ def default_key
873
+ :"#{self[:name]}_id"
874
+ end
875
+
876
+ # Whether to eagerly graph a lazy dataset, true for many_to_one associations
877
+ # only if the key is nil.
878
+ def eager_graph_lazy_dataset?
879
+ self[:key].nil?
880
+ end
881
+
882
+ # many_to_one associations don't need an eager_graph limit strategy
883
+ def eager_graph_limit_strategy(_)
884
+ nil
885
+ end
886
+
887
+ # many_to_one associations don't need an eager limit strategy
888
+ def eager_limit_strategy
889
+ nil
890
+ end
891
+
892
+ # many_to_one associations don't need a filter by associations limit strategy
893
+ def filter_by_associations_limit_strategy
894
+ nil
895
+ end
896
+
897
+ FINALIZE_SETTINGS = superclass::FINALIZE_SETTINGS.merge(
898
+ :primary_key=>:primary_key,
899
+ :primary_keys=>:primary_keys,
900
+ :primary_key_method=>:primary_key_method,
901
+ :primary_key_methods=>:primary_key_methods,
902
+ :qualified_primary_key=>:qualified_primary_key,
903
+ :reciprocal_type=>:reciprocal_type
904
+ ).freeze
905
+ def finalize_settings
906
+ FINALIZE_SETTINGS
907
+ end
908
+
909
+ # The expression to use on the left hand side of the IN lookup when eager loading
910
+ def predicate_key
911
+ cached_fetch(:predicate_key){qualified_primary_key}
912
+ end
913
+
914
+ # The column(s) in the associated table that the key in the current table references (either a symbol or an array).
915
+ def primary_key
916
+ cached_fetch(:primary_key){associated_class.primary_key || raise(Error, "no primary key specified for #{associated_class.inspect}")}
917
+ end
918
+
919
+ # The columns in the associated table that the key in the current table references (always an array).
920
+ def primary_keys
921
+ cached_fetch(:primary_keys){Array(primary_key)}
922
+ end
923
+ alias associated_object_keys primary_keys
924
+
925
+ # The method symbol or array of method symbols to call on the associated object
926
+ # to get the value to use for the foreign keys.
927
+ def primary_key_method
928
+ cached_fetch(:primary_key_method){primary_key}
929
+ end
930
+
931
+ # The array of method symbols to call on the associated object
932
+ # to get the value to use for the foreign keys.
933
+ def primary_key_methods
934
+ cached_fetch(:primary_key_methods){Array(primary_key_method)}
935
+ end
936
+
937
+ # #primary_key qualified by the associated table
938
+ def qualified_primary_key
939
+ cached_fetch(:qualified_primary_key){self[:qualify] == false ? primary_key : qualify_assoc(primary_key)}
940
+ end
941
+
942
+ # True only if the reciprocal is a one_to_many association.
943
+ def reciprocal_array?
944
+ !set_reciprocal_to_self?
945
+ end
946
+
947
+ # Whether this association returns an array of objects instead of a single object,
948
+ # false for a many_to_one association.
949
+ def returns_array?
950
+ false
951
+ end
952
+
953
+ # True only if the reciprocal is a one_to_one association.
954
+ def set_reciprocal_to_self?
955
+ reciprocal
956
+ reciprocal_type == :one_to_one
957
+ end
958
+
959
+ private
960
+
961
+ # Reciprocals of many_to_one associations could be either one_to_many or one_to_one,
962
+ # and which is not known in advance.
963
+ def ambiguous_reciprocal_type?
964
+ true
965
+ end
966
+
967
+ def filter_by_associations_conditions_associated_keys
968
+ qualify(associated_class.table_name, primary_keys)
969
+ end
970
+
971
+ def filter_by_associations_conditions_key
972
+ qualify(self[:model].table_name, self[:key_column])
973
+ end
974
+
975
+ # many_to_one associations do not need to be limited to a single row if they
976
+ # explicitly do not have a key.
977
+ def limit_to_single_row?
978
+ super && self[:key]
979
+ end
980
+
981
+ def predicate_key_methods
982
+ self[:keys]
983
+ end
984
+
985
+ # The reciprocal type of a many_to_one association is either
986
+ # a one_to_many or a one_to_one association.
987
+ def possible_reciprocal_types
988
+ [:one_to_many, :one_to_one]
989
+ end
990
+
991
+ # Whether the given association reflection is possible reciprocal
992
+ def reciprocal_association?(assoc_reflect)
993
+ super && self[:keys] == assoc_reflect[:keys] && primary_key == assoc_reflect.primary_key
994
+ end
995
+
996
+ # The reciprocal type of a many_to_one association is either
997
+ # a one_to_many or a one_to_one association, look in the associated class
998
+ # to try to figure out which.
999
+ def reciprocal_type
1000
+ cached_fetch(:reciprocal_type) do
1001
+ possible_recips = []
1002
+
1003
+ associated_class.all_association_reflections.each do |assoc_reflect|
1004
+ if reciprocal_association?(assoc_reflect)
1005
+ possible_recips << assoc_reflect
1006
+ end
1007
+ end
1008
+
1009
+ if possible_recips.length == 1
1010
+ possible_recips.first[:type]
1011
+ else
1012
+ possible_reciprocal_types
1013
+ end
1014
+ end
1015
+ end
1016
+ end
1017
+
1018
+ class OneToManyAssociationReflection < AssociationReflection
1019
+ ASSOCIATION_TYPES[:one_to_many] = self
1020
+
1021
+ # Support a correlated subquery limit strategy when using eager_graph.
1022
+ def apply_eager_graph_limit_strategy(strategy, ds)
1023
+ case strategy
1024
+ when :correlated_subquery
1025
+ apply_correlated_subquery_limit_strategy(ds)
1026
+ else
1027
+ super
1028
+ end
1029
+ end
1030
+
1031
+ # The keys in the associated model's table related to this association
1032
+ def associated_object_keys
1033
+ self[:keys]
1034
+ end
1035
+
1036
+ # one_to_many associations can only have associated objects if none of
1037
+ # the :keys options have a nil value.
1038
+ def can_have_associated_objects?(obj)
1039
+ !self[:primary_keys].any?{|k| obj.get_column_value(k).nil?}
1040
+ end
1041
+
1042
+ # one_to_many and one_to_one associations can be clones
1043
+ def cloneable?(ref)
1044
+ ref[:type] == :one_to_many || ref[:type] == :one_to_one
1045
+ end
1046
+
1047
+ # Default foreign key name symbol for key in associated table that points to
1048
+ # current table's primary key.
1049
+ def default_key
1050
+ :"#{underscore(demodulize(self[:model].name))}_id"
1051
+ end
1052
+
1053
+ FINALIZE_SETTINGS = superclass::FINALIZE_SETTINGS.merge(
1054
+ :qualified_primary_key=>:qualified_primary_key
1055
+ ).freeze
1056
+ def finalize_settings
1057
+ FINALIZE_SETTINGS
1058
+ end
1059
+
1060
+ # Handle silent failure of add/remove methods if raise_on_save_failure is false.
1061
+ def handle_silent_modification_failure?
1062
+ self[:raise_on_save_failure] == false
1063
+ end
1064
+
1065
+ # The hash key to use for the eager loading predicate (left side of IN (1, 2, 3))
1066
+ def predicate_key
1067
+ cached_fetch(:predicate_key){qualify_assoc(self[:key])}
1068
+ end
1069
+ alias qualified_key predicate_key
1070
+
1071
+ # The column in the current table that the key in the associated table references.
1072
+ def primary_key
1073
+ self[:primary_key]
1074
+ end
1075
+
1076
+ # #primary_key qualified by the current table
1077
+ def qualified_primary_key
1078
+ cached_fetch(:qualified_primary_key){qualify_cur(primary_key)}
1079
+ end
1080
+
1081
+ # Whether the reciprocal of this association returns an array of objects instead of a single object,
1082
+ # false for a one_to_many association.
1083
+ def reciprocal_array?
1084
+ false
1085
+ end
1086
+
1087
+ # Destroying one_to_many associated objects automatically deletes the foreign key.
1088
+ def remove_before_destroy?
1089
+ false
1090
+ end
1091
+
1092
+ # The one_to_many association needs to check that an object to be removed already is associated.
1093
+ def remove_should_check_existing?
1094
+ true
1095
+ end
1096
+
1097
+ # One to many associations set the reciprocal to self when loading associated records.
1098
+ def set_reciprocal_to_self?
1099
+ true
1100
+ end
1101
+
1102
+ private
1103
+
1104
+ # Use a correlated subquery to limit the dataset. Note that this will not
1105
+ # work correctly if the associated dataset uses qualified identifers in the WHERE clause,
1106
+ # as they would reference the containing query instead of the subquery.
1107
+ def apply_correlated_subquery_limit_strategy(ds)
1108
+ table = ds.first_source_table
1109
+ table_alias = ds.first_source_alias
1110
+ primary_key = associated_class.primary_key
1111
+ key = self[:key]
1112
+ cs_alias = :t1
1113
+ cs = associated_dataset.
1114
+ from(Sequel.as(table, :t1)).
1115
+ select(*qualify(cs_alias, primary_key)).
1116
+ where(Array(qualify(cs_alias, key)).zip(Array(qualify(table_alias, key)))).
1117
+ limit(*limit_and_offset)
1118
+ ds.where(qualify(table_alias, primary_key)=>cs)
1119
+ end
1120
+
1121
+ # Support correlated subquery strategy when filtering by limited associations.
1122
+ def apply_filter_by_associations_limit_strategy(ds)
1123
+ case filter_by_associations_limit_strategy
1124
+ when :correlated_subquery
1125
+ apply_correlated_subquery_limit_strategy(ds)
1126
+ else
1127
+ super
1128
+ end
1129
+ end
1130
+
1131
+ def filter_by_associations_conditions_associated_keys
1132
+ qualify(associated_class.table_name, self[:keys])
1133
+ end
1134
+
1135
+ def filter_by_associations_conditions_key
1136
+ qualify(self[:model].table_name, self[:primary_key_column])
1137
+ end
1138
+
1139
+ def filter_by_associations_limit_alias_key
1140
+ Array(filter_by_associations_limit_key)
1141
+ end
1142
+
1143
+ def filter_by_associations_limit_aliases
1144
+ filter_by_associations_limit_alias_key.map(&:column)
1145
+ end
1146
+
1147
+ def filter_by_associations_limit_key
1148
+ qualify(associated_class.table_name, associated_class.primary_key)
1149
+ end
1150
+
1151
+ def predicate_key_methods
1152
+ self[:primary_keys]
1153
+ end
1154
+
1155
+ def reciprocal_association?(assoc_reflect)
1156
+ super && self[:keys] == assoc_reflect[:keys] && primary_key == assoc_reflect.primary_key
1157
+ end
1158
+
1159
+ # The reciprocal type of a one_to_many association is a many_to_one association.
1160
+ def reciprocal_type
1161
+ :many_to_one
1162
+ end
1163
+
1164
+ # Support automatic use of correlated subqueries if :ruby option is best available option,
1165
+ # the database supports them, and either the associated class has a non-composite primary key
1166
+ # or the database supports multiple columns in IN.
1167
+ def true_eager_graph_limit_strategy
1168
+ r = super
1169
+ ds = associated_dataset
1170
+ if r == :ruby && ds.supports_limits_in_correlated_subqueries? && (Array(associated_class.primary_key).length == 1 || ds.supports_multiple_column_in?) && (!offset || ds.supports_offsets_in_correlated_subqueries?)
1171
+ :correlated_subquery
1172
+ else
1173
+ r
1174
+ end
1175
+ end
1176
+ end
1177
+
1178
+ # Methods that turn an association that returns multiple objects into an association that
1179
+ # returns a single object.
1180
+ module SingularAssociationReflection
1181
+ # Singular associations do not assign singular if they are using the ruby eager limit strategy
1182
+ # and have a slice range, since they need to store the array of associated objects in order to
1183
+ # pick the correct one with an offset.
1184
+ def assign_singular?
1185
+ super && (eager_limit_strategy != :ruby || !slice_range)
1186
+ end
1187
+
1188
+ # Add conditions when filtering by singular associations with orders, since the
1189
+ # underlying relationship is probably not one-to-one.
1190
+ def filter_by_associations_add_conditions?
1191
+ super || self[:order] || self[:eager_limit_strategy] || self[:filter_limit_strategy]
1192
+ end
1193
+
1194
+ # Make sure singular associations always have 1 as the limit
1195
+ def limit_and_offset
1196
+ r = super
1197
+ if r.first == 1
1198
+ r
1199
+ else
1200
+ [1, r[1]]
1201
+ end
1202
+ end
1203
+
1204
+ # Singular associations always return a single object, not an array.
1205
+ def returns_array?
1206
+ false
1207
+ end
1208
+
1209
+ private
1210
+
1211
+ # Only use a eager limit strategy by default if there is an offset or an order.
1212
+ def default_eager_limit_strategy
1213
+ super if self[:order] || offset
1214
+ end
1215
+
1216
+ # Use a strategy for filtering by associations if there is an order or an offset,
1217
+ # or a specific limiting strategy has been specified.
1218
+ def filter_by_associations_limit_strategy
1219
+ super if self[:order] || offset || self[:eager_limit_strategy] || self[:filter_limit_strategy]
1220
+ end
1221
+
1222
+ # Use the DISTINCT ON eager limit strategy for true if the database supports it.
1223
+ def true_eager_graph_limit_strategy
1224
+ if associated_class.dataset.supports_ordered_distinct_on? && !offset
1225
+ :distinct_on
1226
+ else
1227
+ super
1228
+ end
1229
+ end
1230
+ end
1231
+
1232
+ class OneToOneAssociationReflection < OneToManyAssociationReflection
1233
+ ASSOCIATION_TYPES[:one_to_one] = self
1234
+ include SingularAssociationReflection
1235
+ end
1236
+
1237
+ class ManyToManyAssociationReflection < AssociationReflection
1238
+ ASSOCIATION_TYPES[:many_to_many] = self
1239
+
1240
+ # The alias to use for the associated key when eagerly loading
1241
+ def associated_key_alias
1242
+ self[:left_key_alias]
1243
+ end
1244
+
1245
+ # Array of associated keys used when eagerly loading.
1246
+ def associated_key_array
1247
+ cached_fetch(:associated_key_array) do
1248
+ if self[:uses_left_composite_keys]
1249
+ associated_key_alias.zip(predicate_keys).map{|a, k| SQL::AliasedExpression.new(k, a)}
1250
+ else
1251
+ [SQL::AliasedExpression.new(predicate_key, associated_key_alias)]
1252
+ end
1253
+ end
1254
+ end
1255
+
1256
+ # The column to use for the associated key when eagerly loading
1257
+ def associated_key_column
1258
+ self[:left_key]
1259
+ end
1260
+
1261
+ # Alias of right_primary_keys
1262
+ def associated_object_keys
1263
+ right_primary_keys
1264
+ end
1265
+
1266
+ # many_to_many associations can only have associated objects if none of
1267
+ # the :left_primary_keys options have a nil value.
1268
+ def can_have_associated_objects?(obj)
1269
+ !self[:left_primary_keys].any?{|k| obj.get_column_value(k).nil?}
1270
+ end
1271
+
1272
+ # one_through_one and many_to_many associations can be clones
1273
+ def cloneable?(ref)
1274
+ ref[:type] == :many_to_many || ref[:type] == :one_through_one
1275
+ end
1276
+
1277
+ # The default associated key alias(es) to use when eager loading
1278
+ # associations via eager.
1279
+ def default_associated_key_alias
1280
+ self[:uses_left_composite_keys] ? (0...self[:left_keys].length).map{|i| :"x_foreign_key_#{i}_x"} : :x_foreign_key_x
1281
+ end
1282
+
1283
+ # The default eager loader used if the user doesn't override it. Extracted
1284
+ # to a method so the code can be shared with the many_through_many plugin.
1285
+ def default_eager_loader(eo)
1286
+ h = eo[:id_map]
1287
+ assign_singular = assign_singular?
1288
+ delete_rn = delete_row_number_column
1289
+ uses_lcks = self[:uses_left_composite_keys]
1290
+ left_key_alias = self[:left_key_alias]
1291
+ name = self[:name]
1292
+
1293
+ self[:model].eager_load_results(self, eo) do |assoc_record|
1294
+ assoc_record.remove_key!(delete_rn) if delete_rn
1295
+ hash_key = if uses_lcks
1296
+ left_key_alias.map{|k| assoc_record.remove_key!(k)}
1297
+ else
1298
+ assoc_record.remove_key!(left_key_alias)
1299
+ end
1300
+
1301
+ objects = h[hash_key]
1302
+
1303
+ if assign_singular
1304
+ objects.each do |object|
1305
+ object.associations[name] ||= assoc_record
1306
+ end
1307
+ else
1308
+ objects.each do |object|
1309
+ object.associations[name].push(assoc_record)
1310
+ end
1311
+ end
1312
+ end
1313
+ end
1314
+
1315
+ # Default name symbol for the join table.
1316
+ def default_join_table
1317
+ [self[:class_name], self[:model].name].map{|i| underscore(pluralize(demodulize(i)))}.sort.join('_').to_sym
1318
+ end
1319
+
1320
+ # Default foreign key name symbol for key in join table that points to
1321
+ # current table's primary key (or :left_primary_key column).
1322
+ def default_left_key
1323
+ :"#{underscore(demodulize(self[:model].name))}_id"
1324
+ end
1325
+
1326
+ # Default foreign key name symbol for foreign key in join table that points to
1327
+ # the association's table's primary key (or :right_primary_key column).
1328
+ def default_right_key
1329
+ :"#{singularize(self[:name])}_id"
1330
+ end
1331
+
1332
+ FINALIZE_SETTINGS = superclass::FINALIZE_SETTINGS.merge(
1333
+ :associated_key_array=>:associated_key_array,
1334
+ :qualified_right_key=>:qualified_right_key,
1335
+ :join_table_source=>:join_table_source,
1336
+ :join_table_alias=>:join_table_alias,
1337
+ :qualified_right_primary_key=>:qualified_right_primary_key,
1338
+ :right_primary_key=>:right_primary_key,
1339
+ :right_primary_keys=>:right_primary_keys,
1340
+ :right_primary_key_method=>:right_primary_key_method,
1341
+ :right_primary_key_methods=>:right_primary_key_methods,
1342
+ :select=>:select
1343
+ ).freeze
1344
+ def finalize_settings
1345
+ FINALIZE_SETTINGS
1346
+ end
1347
+
1348
+ # The hash key to use for the eager loading predicate (left side of IN (1, 2, 3)).
1349
+ # The left key qualified by the join table.
1350
+ def predicate_key
1351
+ cached_fetch(:predicate_key){qualify(join_table_alias, self[:left_key])}
1352
+ end
1353
+ alias qualified_left_key predicate_key
1354
+
1355
+ # The right key qualified by the join table.
1356
+ def qualified_right_key
1357
+ cached_fetch(:qualified_right_key){qualify(join_table_alias, self[:right_key])}
1358
+ end
1359
+
1360
+ # many_to_many associations need to select a key in an associated table to eagerly load
1361
+ def eager_loading_use_associated_key?
1362
+ !separate_query_per_table?
1363
+ end
1364
+
1365
+ # The source of the join table. This is the join table itself, unless it
1366
+ # is aliased, in which case it is the unaliased part.
1367
+ def join_table_source
1368
+ cached_fetch(:join_table_source){split_join_table_alias[0]}
1369
+ end
1370
+
1371
+ # The join table itself, unless it is aliased, in which case this
1372
+ # is the alias.
1373
+ def join_table_alias
1374
+ cached_fetch(:join_table_alias) do
1375
+ s, a = split_join_table_alias
1376
+ a || s
1377
+ end
1378
+ end
1379
+ alias associated_key_table join_table_alias
1380
+
1381
+ # Whether the associated object needs a primary key to be added/removed,
1382
+ # true for many_to_many associations.
1383
+ def need_associated_primary_key?
1384
+ true
1385
+ end
1386
+
1387
+ # #right_primary_key qualified by the associated table
1388
+ def qualified_right_primary_key
1389
+ cached_fetch(:qualified_right_primary_key){qualify_assoc(right_primary_key)}
1390
+ end
1391
+
1392
+ # The primary key column(s) to use in the associated table (can be symbol or array).
1393
+ def right_primary_key
1394
+ cached_fetch(:right_primary_key){associated_class.primary_key || raise(Error, "no primary key specified for #{associated_class.inspect}")}
1395
+ end
1396
+
1397
+ # The primary key columns to use in the associated table (always array).
1398
+ def right_primary_keys
1399
+ cached_fetch(:right_primary_keys){Array(right_primary_key)}
1400
+ end
1401
+
1402
+ # The method symbol or array of method symbols to call on the associated objects
1403
+ # to get the foreign key values for the join table.
1404
+ def right_primary_key_method
1405
+ cached_fetch(:right_primary_key_method){right_primary_key}
1406
+ end
1407
+
1408
+ # The array of method symbols to call on the associated objects
1409
+ # to get the foreign key values for the join table.
1410
+ def right_primary_key_methods
1411
+ cached_fetch(:right_primary_key_methods){Array(right_primary_key_method)}
1412
+ end
1413
+
1414
+ # The columns to select when loading the association, associated_class.table_name.* by default.
1415
+ def select
1416
+ cached_fetch(:select){default_select}
1417
+ end
1418
+
1419
+ # Whether a separate query should be used for the join table.
1420
+ def separate_query_per_table?
1421
+ self[:join_table_db]
1422
+ end
1423
+
1424
+ private
1425
+
1426
+ # Join to the the join table, unless using a separate query per table.
1427
+ def _associated_dataset
1428
+ if separate_query_per_table?
1429
+ super
1430
+ else
1431
+ super.inner_join(self[:join_table], self[:right_keys].zip(right_primary_keys), :qualify=>:deep)
1432
+ end
1433
+ end
1434
+
1435
+ # Use the right_keys from the eager loading options if
1436
+ # using a separate query per table.
1437
+ def eager_loading_set_predicate_condition(ds, eo)
1438
+ if separate_query_per_table?
1439
+ ds.where(right_primary_key=>eo[:right_keys])
1440
+ else
1441
+ super
1442
+ end
1443
+ end
1444
+
1445
+ # The default selection for associations that require joins. These do not use the default
1446
+ # model selection unless all entries in the select are explicitly qualified identifiers, as
1447
+ # other it can include unqualified columns which would be made ambiguous by joining.
1448
+ def default_select
1449
+ if (sel = associated_class.dataset.opts[:select]) && sel.all?{|c| selection_is_qualified?(c)}
1450
+ sel
1451
+ else
1452
+ Sequel::SQL::ColumnAll.new(associated_class.table_name)
1453
+ end
1454
+ end
1455
+
1456
+ def filter_by_associations_conditions_associated_keys
1457
+ qualify(join_table_alias, self[:left_keys])
1458
+ end
1459
+
1460
+ def filter_by_associations_conditions_key
1461
+ qualify(self[:model].table_name, self[:left_primary_key_column])
1462
+ end
1463
+
1464
+ def filter_by_associations_limit_alias_key
1465
+ aliaz = 'a'
1466
+ filter_by_associations_limit_key.map{|c| c.as(Sequel.identifier(aliaz = aliaz.next))}
1467
+ end
1468
+
1469
+ def filter_by_associations_limit_aliases
1470
+ filter_by_associations_limit_alias_key.map(&:alias)
1471
+ end
1472
+
1473
+ def filter_by_associations_limit_key
1474
+ qualify(join_table_alias, self[:left_keys]) + Array(qualify(associated_class.table_name, associated_class.primary_key))
1475
+ end
1476
+
1477
+ def predicate_key_methods
1478
+ self[:left_primary_keys]
1479
+ end
1480
+
1481
+ def reciprocal_association?(assoc_reflect)
1482
+ super && assoc_reflect[:left_keys] == self[:right_keys] &&
1483
+ assoc_reflect[:right_keys] == self[:left_keys] &&
1484
+ assoc_reflect[:join_table] == self[:join_table] &&
1485
+ right_primary_keys == assoc_reflect[:left_primary_key_columns] &&
1486
+ self[:left_primary_key_columns] == assoc_reflect.right_primary_keys
1487
+ end
1488
+
1489
+ def reciprocal_type
1490
+ :many_to_many
1491
+ end
1492
+
1493
+ # Whether the given expression represents a qualified identifier. Used to determine if it is
1494
+ # OK to use directly when joining.
1495
+ def selection_is_qualified?(c)
1496
+ case c
1497
+ when Symbol
1498
+ Sequel.split_symbol(c)[0]
1499
+ when Sequel::SQL::QualifiedIdentifier
1500
+ true
1501
+ when Sequel::SQL::AliasedExpression
1502
+ selection_is_qualified?(c.expression)
1503
+ else
1504
+ false
1505
+ end
1506
+ end
1507
+
1508
+ # Split the join table into source and alias parts.
1509
+ def split_join_table_alias
1510
+ associated_class.dataset.split_alias(self[:join_table])
1511
+ end
1512
+ end
1513
+
1514
+ class OneThroughOneAssociationReflection < ManyToManyAssociationReflection
1515
+ ASSOCIATION_TYPES[:one_through_one] = self
1516
+ include SingularAssociationReflection
1517
+
1518
+ # one_through_one associations should not singularize the association name when
1519
+ # creating the foreign key.
1520
+ def default_right_key
1521
+ :"#{self[:name]}_id"
1522
+ end
1523
+
1524
+ # one_through_one associations have no reciprocals
1525
+ def reciprocal
1526
+ nil
1527
+ end
1528
+ end
1529
+
1530
+ # This module contains methods added to all association datasets
1531
+ module AssociationDatasetMethods
1532
+ # The model object that created the association dataset
1533
+ def model_object
1534
+ @opts[:model_object]
1535
+ end
1536
+
1537
+ # The association reflection related to the association dataset
1538
+ def association_reflection
1539
+ @opts[:association_reflection]
1540
+ end
1541
+
1542
+ private
1543
+
1544
+ def non_sql_option?(key)
1545
+ super || key == :model_object || key == :association_reflection
1546
+ end
1547
+ end
1548
+
1549
+ # Each kind of association adds a number of instance methods to the model class which
1550
+ # are specialized according to the association type and optional parameters
1551
+ # given in the definition. Example:
1552
+ #
1553
+ # class Project < Sequel::Model
1554
+ # many_to_one :portfolio
1555
+ # # or: one_to_one :portfolio
1556
+ # one_to_many :milestones
1557
+ # # or: many_to_many :milestones
1558
+ # end
1559
+ #
1560
+ # The project class now has the following instance methods:
1561
+ # portfolio :: Returns the associated portfolio.
1562
+ # portfolio=(obj) :: Sets the associated portfolio to the object,
1563
+ # but the change is not persisted until you save the record (for many_to_one associations).
1564
+ # portfolio_dataset :: Returns a dataset that would return the associated
1565
+ # portfolio, only useful in fairly specific circumstances.
1566
+ # milestones :: Returns an array of associated milestones
1567
+ # add_milestone(obj) :: Associates the passed milestone with this object.
1568
+ # remove_milestone(obj) :: Removes the association with the passed milestone.
1569
+ # remove_all_milestones :: Removes associations with all associated milestones.
1570
+ # milestones_dataset :: Returns a dataset that would return the associated
1571
+ # milestones, allowing for further filtering/limiting/etc.
1572
+ #
1573
+ # If you want to override the behavior of the add_/remove_/remove_all_/ methods
1574
+ # or the association setter method, use the :adder, :remover, :clearer, and/or :setter
1575
+ # options. These options override the default behavior.
1576
+ #
1577
+ # By default the classes for the associations are inferred from the association
1578
+ # name, so for example the Project#portfolio will return an instance of
1579
+ # Portfolio, and Project#milestones will return an array of Milestone
1580
+ # instances. You can use the :class option to change which class is used.
1581
+ #
1582
+ # Association definitions are also reflected by the class, e.g.:
1583
+ #
1584
+ # Project.associations
1585
+ # => [:portfolio, :milestones]
1586
+ # Project.association_reflection(:portfolio)
1587
+ # => #<Sequel::Model::Associations::ManyToOneAssociationReflection Project.many_to_one :portfolio>
1588
+ #
1589
+ # Associations should not have the same names as any of the columns in the
1590
+ # model's current table they reference. If you are dealing with an existing schema that
1591
+ # has a column named status, you can't name the association status, you'd
1592
+ # have to name it foo_status or something else. If you give an association the same name
1593
+ # as a column, you will probably end up with an association that doesn't work, or a SystemStackError.
1594
+ #
1595
+ # For a more in depth general overview, as well as a reference guide,
1596
+ # see the {Association Basics guide}[rdoc-ref:doc/association_basics.rdoc].
1597
+ # For examples of advanced usage, see the {Advanced Associations guide}[rdoc-ref:doc/advanced_associations.rdoc].
1598
+ module ClassMethods
1599
+ # All association reflections defined for this model (default: {}).
1600
+ attr_reader :association_reflections
1601
+
1602
+ # Hash with column symbol keys and arrays of many_to_one
1603
+ # association symbols that should be cleared when the column
1604
+ # value changes.
1605
+ attr_reader :autoreloading_associations
1606
+
1607
+ # Whether association metadata should be cached in the association reflection. If not cached, it will be computed
1608
+ # on demand. In general you only want to set this to false when using code reloading. When using code reloading,
1609
+ # setting this will make sure that if an associated class is removed or modified, this class will not have a reference to
1610
+ # the previous class.
1611
+ attr_accessor :cache_associations
1612
+
1613
+ # The default options to use for all associations. This hash is merged into the association reflection hash for
1614
+ # all association reflections.
1615
+ attr_accessor :default_association_options
1616
+
1617
+ # The default options to use for all associations of a given type. This is a hash keyed by association type
1618
+ # symbol. If there is a value for the association type symbol key, the resulting hash will be merged into the
1619
+ # association reflection hash for all association reflections of that type.
1620
+ attr_accessor :default_association_type_options
1621
+
1622
+ # The default :eager_limit_strategy option to use for limited or offset associations (default: true, causing Sequel
1623
+ # to use what it considers the most appropriate strategy).
1624
+ attr_accessor :default_eager_limit_strategy
1625
+
1626
+ # Array of all association reflections for this model class
1627
+ def all_association_reflections
1628
+ association_reflections.values
1629
+ end
1630
+
1631
+ # Associates a related model with the current model. The following types are
1632
+ # supported:
1633
+ #
1634
+ # :many_to_one :: Foreign key in current model's table points to
1635
+ # associated model's primary key. Each associated model object can
1636
+ # be associated with more than one current model objects. Each current
1637
+ # model object can be associated with only one associated model object.
1638
+ # :one_to_many :: Foreign key in associated model's table points to this
1639
+ # model's primary key. Each current model object can be associated with
1640
+ # more than one associated model objects. Each associated model object
1641
+ # can be associated with only one current model object.
1642
+ # :one_through_one :: Similar to many_to_many in terms of foreign keys, but only one object
1643
+ # is associated to the current object through the association.
1644
+ # Provides only getter methods, no setter or modification methods.
1645
+ # :one_to_one :: Similar to one_to_many in terms of foreign keys, but
1646
+ # only one object is associated to the current object through the
1647
+ # association. The methods created are similar to many_to_one, except
1648
+ # that the one_to_one setter method saves the passed object.
1649
+ # :many_to_many :: A join table is used that has a foreign key that points
1650
+ # to this model's primary key and a foreign key that points to the
1651
+ # associated model's primary key. Each current model object can be
1652
+ # associated with many associated model objects, and each associated
1653
+ # model object can be associated with many current model objects.
1654
+ #
1655
+ # The following options can be supplied:
1656
+ # === Multiple Types
1657
+ # :adder :: Proc used to define the private _add_* method for doing the database work
1658
+ # to associate the given object to the current object (*_to_many assocations).
1659
+ # Set to nil to not define a add_* method for the association.
1660
+ # :after_add :: Symbol, Proc, or array of both/either specifying a callback to call
1661
+ # after a new item is added to the association.
1662
+ # :after_load :: Symbol, Proc, or array of both/either specifying a callback to call
1663
+ # after the associated record(s) have been retrieved from the database.
1664
+ # :after_remove :: Symbol, Proc, or array of both/either specifying a callback to call
1665
+ # after an item is removed from the association.
1666
+ # :after_set :: Symbol, Proc, or array of both/either specifying a callback to call
1667
+ # after an item is set using the association setter method.
1668
+ # :allow_eager :: If set to false, you cannot load the association eagerly
1669
+ # via eager or eager_graph
1670
+ # :allow_eager_graph :: If set to false, you cannot load the association eagerly via eager_graph.
1671
+ # :allow_filtering_by :: If set to false, you cannot use the association when filtering
1672
+ # :before_add :: Symbol, Proc, or array of both/either specifying a callback to call
1673
+ # before a new item is added to the association.
1674
+ # :before_remove :: Symbol, Proc, or array of both/either specifying a callback to call
1675
+ # before an item is removed from the association.
1676
+ # :before_set :: Symbol, Proc, or array of both/either specifying a callback to call
1677
+ # before an item is set using the association setter method.
1678
+ # :cartesian_product_number :: the number of joins completed by this association that could cause more
1679
+ # than one row for each row in the current table (default: 0 for
1680
+ # many_to_one, one_to_one, and one_through_one associations, 1
1681
+ # for one_to_many and many_to_many associations).
1682
+ # :class :: The associated class or its name as a string or symbol. If not
1683
+ # given, uses the association's name, which is camelized (and
1684
+ # singularized unless the type is :many_to_one, :one_to_one, or one_through_one). If this is specified
1685
+ # as a string or symbol, you must specify the full class name (e.g. "::SomeModule::MyModel").
1686
+ # :class_namespace :: If :class is given as a string or symbol, sets the default namespace in which to look for
1687
+ # the class. <tt>class: 'Foo', class_namespace: 'Bar'</tt> looks for <tt>::Bar::Foo</tt>.)
1688
+ # :clearer :: Proc used to define the private _remove_all_* method for doing the database work
1689
+ # to remove all objects associated to the current object (*_to_many assocations).
1690
+ # Set to nil to not define a remove_all_* method for the association.
1691
+ # :clone :: Merge the current options and block into the options and block used in defining
1692
+ # the given association. Can be used to DRY up a bunch of similar associations that
1693
+ # all share the same options such as :class and :key, while changing the order and block used.
1694
+ # :conditions :: The conditions to use to filter the association, can be any argument passed to where.
1695
+ # This option is not respected when using eager_graph or association_join, unless it
1696
+ # is hash or array of two element arrays. Consider also specifying the :graph_block
1697
+ # option if the value for this option is not a hash or array of two element arrays
1698
+ # and you plan to use this association in eager_graph or association_join.
1699
+ # :dataset :: A proc that is used to define the method to get the base dataset to use (before the other
1700
+ # options are applied). If the proc accepts an argument, it is passed the related
1701
+ # association reflection. It is a best practice to always have the dataset accept an argument
1702
+ # and use the argument to return the appropriate dataset.
1703
+ # :distinct :: Use the DISTINCT clause when selecting associating object, both when
1704
+ # lazy loading and eager loading via .eager (but not when using .eager_graph).
1705
+ # :eager :: The associations to eagerly load via +eager+ when loading the associated object(s).
1706
+ # :eager_block :: If given, use the block instead of the default block when
1707
+ # eagerly loading. To not use a block when eager loading (when one is used normally),
1708
+ # set to nil.
1709
+ # :eager_graph :: The associations to eagerly load via +eager_graph+ when loading the associated object(s).
1710
+ # many_to_many associations with this option cannot be eagerly loaded via +eager+.
1711
+ # :eager_grapher :: A proc to use to implement eager loading via +eager_graph+, overriding the default.
1712
+ # Takes an options hash with at least the entries :self (the receiver of the eager_graph call),
1713
+ # :table_alias (the alias to use for table to graph into the association), and :implicit_qualifier
1714
+ # (the alias that was used for the current table).
1715
+ # Should return a copy of the dataset with the association graphed into it.
1716
+ # :eager_limit_strategy :: Determines the strategy used for enforcing limits and offsets when eager loading
1717
+ # associations via the +eager+ method.
1718
+ # :eager_loader :: A proc to use to implement eager loading, overriding the default. Takes a single hash argument,
1719
+ # with at least the keys: :rows, which is an array of current model instances, :associations,
1720
+ # which is a hash of dependent associations, :self, which is the dataset doing the eager loading,
1721
+ # :eager_block, which is a dynamic callback that should be called with the dataset, and :id_map,
1722
+ # which is a mapping of key values to arrays of current model instances. In the proc, the
1723
+ # associated records should be queried from the database and the associations cache for each
1724
+ # record should be populated.
1725
+ # :eager_loader_key :: A symbol for the key column to use to populate the key_hash
1726
+ # for the eager loader. Can be set to nil to not populate the key_hash.
1727
+ # :eager_loading_predicate_transform :: A callable object with which to transform the predicate key values used
1728
+ # when eager loading. Called with two arguments, the array of predicate key
1729
+ # values, and a the reflection for the association being eager loaded.
1730
+ # :extend :: A module or array of modules to extend the dataset with.
1731
+ # :filter_limit_strategy :: Determines the strategy used for enforcing limits and offsets when filtering by
1732
+ # limited associations. Possible options are :window_function, :distinct_on, or
1733
+ # :correlated_subquery depending on association type and database type.
1734
+ # :graph_alias_base :: The base name to use for the table alias when eager graphing. Defaults to the name
1735
+ # of the association. If the alias name has already been used in the query, Sequel will create
1736
+ # a unique alias by appending a numeric suffix (e.g. alias_0, alias_1, ...) until the alias is
1737
+ # unique.
1738
+ # :graph_block :: The block to pass to join_table when eagerly loading
1739
+ # the association via +eager_graph+.
1740
+ # :graph_conditions :: The additional conditions to use on the SQL join when eagerly loading
1741
+ # the association via +eager_graph+. Should be a hash or an array of two element arrays. If not
1742
+ # specified, the :conditions option is used if it is a hash or array of two element arrays.
1743
+ # :graph_join_type :: The type of SQL join to use when eagerly loading the association via
1744
+ # eager_graph. Defaults to :left_outer.
1745
+ # :graph_only_conditions :: The conditions to use on the SQL join when eagerly loading
1746
+ # the association via +eager_graph+, instead of the default conditions specified by the
1747
+ # foreign/primary keys. This option causes the :graph_conditions option to be ignored.
1748
+ # :graph_order :: the order to use when using eager_graph, instead of the default order. This should be used
1749
+ # in the case where :order contains an identifier qualified by the table's name, which may not match
1750
+ # the alias used when eager graphing. By setting this to the unqualified identifier, it will be
1751
+ # automatically qualified when using eager_graph.
1752
+ # :graph_select :: A column or array of columns to select from the associated table
1753
+ # when eagerly loading the association via +eager_graph+. Defaults to all
1754
+ # columns in the associated table.
1755
+ # :graph_use_association_block :: Makes eager_graph consider the association block. Without this, eager_graph
1756
+ # ignores the bock and only use the :graph_* options.
1757
+ # :instance_specific :: Marks the association as instance specific. Should be used if the association block
1758
+ # uses instance specific state, or transient state (accessing current date/time, etc.).
1759
+ # :limit :: Limit the number of records to the provided value. Use
1760
+ # an array with two elements for the value to specify a
1761
+ # limit (first element) and an offset (second element).
1762
+ # :methods_module :: The module that methods the association creates will be placed into. Defaults
1763
+ # to the module containing the model's columns.
1764
+ # :no_association_method :: Do not add a method for the association. This can save memory if the association
1765
+ # method is never used.
1766
+ # :no_dataset_method :: Do not add a method for the association dataset. This can save memory if the dataset
1767
+ # method is never used.
1768
+ # :order :: the column(s) by which to order the association dataset. Can be a
1769
+ # singular column symbol or an array of column symbols.
1770
+ # :order_eager_graph :: Whether to add the association's order to the graphed dataset's order when graphing
1771
+ # via +eager_graph+. Defaults to true, so set to false to disable.
1772
+ # :read_only :: Do not add a setter method (for many_to_one or one_to_one associations),
1773
+ # or add_/remove_/remove_all_ methods (for one_to_many and many_to_many associations).
1774
+ # :reciprocal :: the symbol name of the reciprocal association,
1775
+ # if it exists. By default, Sequel will try to determine it by looking at the
1776
+ # associated model's assocations for a association that matches
1777
+ # the current association's key(s). Set to nil to not use a reciprocal.
1778
+ # :remover :: Proc used to define the private _remove_* method for doing the database work
1779
+ # to remove the association between the given object and the current object (*_to_many assocations).
1780
+ # Set to nil to not define a remove_* method for the association.
1781
+ # :select :: the columns to select. Defaults to the associated class's table_name.* in an association
1782
+ # that uses joins, which means it doesn't include the attributes from the
1783
+ # join table. If you want to include the join table attributes, you can
1784
+ # use this option, but beware that the join table attributes can clash with
1785
+ # attributes from the model table, so you should alias any attributes that have
1786
+ # the same name in both the join table and the associated table.
1787
+ # :setter :: Proc used to define the private _*= method for doing the work to setup the assocation
1788
+ # between the given object and the current object (*_to_one associations).
1789
+ # Set to nil to not define a setter method for the association.
1790
+ # :subqueries_per_union :: The number of subqueries to use in each UNION query, for eager
1791
+ # loading limited associations using the default :union strategy.
1792
+ # :use_placeholder_loader :: Whether to use a placeholder loader when eager loading the
1793
+ # association. Can be set to false to disable the use of a placeholder
1794
+ # loader if one would be used by default.
1795
+ # :validate :: Set to false to not validate when implicitly saving any associated object.
1796
+ # === :many_to_one
1797
+ # :key :: foreign key in current model's table that references
1798
+ # associated model's primary key, as a symbol. Defaults to :"#{name}_id". Can use an
1799
+ # array of symbols for a composite key association.
1800
+ # :key_column :: Similar to, and usually identical to, :key, but :key refers to the model method
1801
+ # to call, where :key_column refers to the underlying column. Should only be
1802
+ # used if the model method differs from the foreign key column, in conjunction
1803
+ # with defining a model alias method for the key column.
1804
+ # :primary_key :: column in the associated table that :key option references, as a symbol.
1805
+ # Defaults to the primary key of the associated table. Can use an
1806
+ # array of symbols for a composite key association.
1807
+ # :primary_key_method :: the method symbol or array of method symbols to call on the associated
1808
+ # object to get the foreign key values. Defaults to :primary_key option.
1809
+ # :qualify :: Whether to use qualified primary keys when loading the association. The default
1810
+ # is true, so you must set to false to not qualify. Qualification rarely causes
1811
+ # problems, but it's necessary to disable in some cases, such as when you are doing
1812
+ # a JOIN USING operation on the column on Oracle.
1813
+ # === :one_to_many and :one_to_one
1814
+ # :key :: foreign key in associated model's table that references
1815
+ # current model's primary key, as a symbol. Defaults to
1816
+ # :"#{self.name.underscore}_id". Can use an
1817
+ # array of symbols for a composite key association.
1818
+ # :key_method :: the method symbol or array of method symbols to call on the associated
1819
+ # object to get the foreign key values. Defaults to :key option.
1820
+ # :primary_key :: column in the current table that :key option references, as a symbol.
1821
+ # Defaults to primary key of the current table. Can use an
1822
+ # array of symbols for a composite key association.
1823
+ # :primary_key_column :: Similar to, and usually identical to, :primary_key, but :primary_key refers
1824
+ # to the model method call, where :primary_key_column refers to the underlying column.
1825
+ # Should only be used if the model method differs from the primary key column, in
1826
+ # conjunction with defining a model alias method for the primary key column.
1827
+ # :raise_on_save_failure :: Do not raise exceptions for hook or validation failures when saving associated
1828
+ # objects in the add/remove methods (return nil instead) [one_to_many only].
1829
+ # === :many_to_many and :one_through_one
1830
+ # :graph_join_table_block :: The block to pass to +join_table+ for
1831
+ # the join table when eagerly loading the association via +eager_graph+.
1832
+ # :graph_join_table_conditions :: The additional conditions to use on the SQL join for
1833
+ # the join table when eagerly loading the association via +eager_graph+.
1834
+ # Should be a hash or an array of two element arrays.
1835
+ # :graph_join_table_join_type :: The type of SQL join to use for the join table when eagerly
1836
+ # loading the association via +eager_graph+. Defaults to the
1837
+ # :graph_join_type option or :left_outer.
1838
+ # :graph_join_table_only_conditions :: The conditions to use on the SQL join for the join
1839
+ # table when eagerly loading the association via +eager_graph+,
1840
+ # instead of the default conditions specified by the
1841
+ # foreign/primary keys. This option causes the
1842
+ # :graph_join_table_conditions option to be ignored.
1843
+ # :join_table :: name of table that includes the foreign keys to both
1844
+ # the current model and the associated model, as a symbol. Defaults to the name
1845
+ # of current model and name of associated model, pluralized,
1846
+ # underscored, sorted, and joined with '_'.
1847
+ # :join_table_block :: proc that can be used to modify the dataset used in the add/remove/remove_all
1848
+ # methods. Should accept a dataset argument and return a modified dataset if present.
1849
+ # :join_table_db :: When retrieving records when using lazy loading or eager loading via +eager+, instead of
1850
+ # a join between to the join table and the associated table, use a separate query for the
1851
+ # join table using the given Database object.
1852
+ # :left_key :: foreign key in join table that points to current model's
1853
+ # primary key, as a symbol. Defaults to :"#{self.name.underscore}_id".
1854
+ # Can use an array of symbols for a composite key association.
1855
+ # :left_primary_key :: column in current table that :left_key points to, as a symbol.
1856
+ # Defaults to primary key of current table. Can use an
1857
+ # array of symbols for a composite key association.
1858
+ # :left_primary_key_column :: Similar to, and usually identical to, :left_primary_key, but :left_primary_key refers to
1859
+ # the model method to call, where :left_primary_key_column refers to the underlying column. Should only
1860
+ # be used if the model method differs from the left primary key column, in conjunction
1861
+ # with defining a model alias method for the left primary key column.
1862
+ # :right_key :: foreign key in join table that points to associated
1863
+ # model's primary key, as a symbol. Defaults to :"#{name.to_s.singularize}_id".
1864
+ # Can use an array of symbols for a composite key association.
1865
+ # :right_primary_key :: column in associated table that :right_key points to, as a symbol.
1866
+ # Defaults to primary key of the associated table. Can use an
1867
+ # array of symbols for a composite key association.
1868
+ # :right_primary_key_method :: the method symbol or array of method symbols to call on the associated
1869
+ # object to get the foreign key values for the join table.
1870
+ # Defaults to :right_primary_key option.
1871
+ # :uniq :: Adds a after_load callback that makes the array of objects unique.
1872
+ def associate(type, name, opts = OPTS, &block)
1873
+ raise(Error, 'invalid association type') unless assoc_class = Sequel.synchronize{ASSOCIATION_TYPES[type]}
1874
+ raise(Error, 'Model.associate name argument must be a symbol') unless name.is_a?(Symbol)
1875
+
1876
+ # dup early so we don't modify opts
1877
+ orig_opts = opts.dup
1878
+
1879
+ if opts[:clone]
1880
+ cloned_assoc = association_reflection(opts[:clone])
1881
+ remove_class_name = orig_opts[:class] && !orig_opts[:class_name]
1882
+ orig_opts = cloned_assoc[:orig_opts].merge(orig_opts)
1883
+ orig_opts.delete(:class_name) if remove_class_name
1884
+ end
1885
+
1886
+ opts = Hash[default_association_options]
1887
+ if type_options = default_association_type_options[type]
1888
+ opts.merge!(type_options)
1889
+ end
1890
+ opts.merge!(orig_opts)
1891
+ opts.merge!(:type => type, :name => name, :cache=>({} if cache_associations), :model => self)
1892
+
1893
+ opts[:block] = block if block
1894
+ opts[:instance_specific] = true if orig_opts[:dataset]
1895
+ if !opts.has_key?(:instance_specific) && (block || orig_opts[:block])
1896
+ # It's possible the association is instance specific, in that it depends on
1897
+ # values other than the foreign key value. This needs to be checked for
1898
+ # in certain places to disable optimizations.
1899
+ opts[:instance_specific] = _association_instance_specific_default(name)
1900
+ end
1901
+ if (orig_opts[:instance_specific] || orig_opts[:dataset]) && !opts.has_key?(:allow_eager) && !opts[:eager_loader]
1902
+ # For associations explicitly marked as instance specific, or that use the
1903
+ # :dataset option, where :allow_eager is not set, and no :eager_loader is
1904
+ # provided, disallow eager loading. In these cases, eager loading is
1905
+ # unlikely to work. This is not done for implicit setting of :instance_specific,
1906
+ # because implicit use is done by default for all associations with blocks,
1907
+ # and the vast majority of associations with blocks use the block for filtering
1908
+ # in a manner compatible with eager loading.
1909
+ opts[:allow_eager] = false
1910
+ end
1911
+ opts = assoc_class.new.merge!(opts)
1912
+
1913
+ if opts[:clone] && !opts.cloneable?(cloned_assoc)
1914
+ raise(Error, "cannot clone an association to an association of different type (association #{name} with type #{type} cloning #{opts[:clone]} with type #{cloned_assoc[:type]})")
1915
+ end
1916
+
1917
+ opts[:use_placeholder_loader] = !opts[:instance_specific] && !opts[:eager_graph] unless opts.include?(:use_placeholder_loader)
1918
+ opts[:eager_block] = opts[:block] unless opts.include?(:eager_block)
1919
+ opts[:graph_join_type] ||= :left_outer
1920
+ opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph)
1921
+ conds = opts[:conditions]
1922
+ opts[:graph_alias_base] ||= name
1923
+ opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds)
1924
+ opts[:graph_conditions] = opts.fetch(:graph_conditions, []).to_a
1925
+ opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select]
1926
+ [:before_add, :before_remove, :after_add, :after_remove, :after_load, :before_set, :after_set].each do |cb_type|
1927
+ opts[cb_type] = Array(opts[cb_type]) if opts[cb_type]
1928
+ end
1929
+
1930
+ if opts[:extend]
1931
+ opts[:extend] = Array(opts[:extend])
1932
+ opts[:reverse_extend] = opts[:extend].reverse
1933
+ end
1934
+
1935
+ late_binding_class_option(opts, opts.returns_array? ? singularize(name) : name)
1936
+
1937
+ # Remove :class entry if it exists and is nil, to work with cached_fetch
1938
+ opts.delete(:class) unless opts[:class]
1939
+
1940
+ opts[:_hash] = [self, name].hash
1941
+
1942
+ def_association(opts)
1943
+
1944
+ orig_opts.delete(:clone)
1945
+ opts[:orig_class] = orig_opts[:class] || orig_opts[:class_name]
1946
+ orig_opts.merge!(:class_name=>opts[:class_name], :class=>opts[:class], :block=>opts[:block])
1947
+ opts[:orig_opts] = orig_opts
1948
+ # don't add to association_reflections until we are sure there are no errors
1949
+ association_reflections[name] = opts
1950
+ end
1951
+
1952
+ # The association reflection hash for the association of the given name.
1953
+ def association_reflection(name)
1954
+ association_reflections[name]
1955
+ end
1956
+
1957
+ # Array of association name symbols
1958
+ def associations
1959
+ association_reflections.keys
1960
+ end
1961
+
1962
+ # Eager load the association with the given eager loader options.
1963
+ def eager_load_results(opts, eo, &block)
1964
+ opts.eager_load_results(eo, &block)
1965
+ end
1966
+
1967
+ # Freeze association related metadata when freezing model class.
1968
+ def freeze
1969
+ @association_reflections.freeze.each_value(&:freeze)
1970
+ @autoreloading_associations.freeze.each_value(&:freeze)
1971
+ @default_association_options.freeze
1972
+ @default_association_type_options.freeze
1973
+ @default_association_type_options.each_value(&:freeze)
1974
+
1975
+ super
1976
+ end
1977
+
1978
+ # Finalize all associations such that values that are looked up
1979
+ # dynamically in associated classes are set statically.
1980
+ # As this modifies the associations, it must be done before
1981
+ # calling freeze.
1982
+ def finalize_associations
1983
+ @association_reflections.each_value(&:finalize)
1984
+ end
1985
+
1986
+ # Shortcut for adding a many_to_many association, see #associate
1987
+ def many_to_many(name, opts=OPTS, &block)
1988
+ associate(:many_to_many, name, opts, &block)
1989
+ end
1990
+
1991
+ # Shortcut for adding a many_to_one association, see #associate
1992
+ def many_to_one(name, opts=OPTS, &block)
1993
+ associate(:many_to_one, name, opts, &block)
1994
+ end
1995
+
1996
+ # Shortcut for adding a one_through_one association, see #associate
1997
+ def one_through_one(name, opts=OPTS, &block)
1998
+ associate(:one_through_one, name, opts, &block)
1999
+ end
2000
+
2001
+ # Shortcut for adding a one_to_many association, see #associate
2002
+ def one_to_many(name, opts=OPTS, &block)
2003
+ associate(:one_to_many, name, opts, &block)
2004
+ end
2005
+
2006
+ # Shortcut for adding a one_to_one association, see #associate
2007
+ def one_to_one(name, opts=OPTS, &block)
2008
+ associate(:one_to_one, name, opts, &block)
2009
+ end
2010
+
2011
+ Plugins.inherited_instance_variables(self, :@association_reflections=>:dup, :@autoreloading_associations=>:hash_dup, :@default_association_options=>:dup, :@default_association_type_options=>:hash_dup, :@cache_associations=>nil, :@default_eager_limit_strategy=>nil)
2012
+ Plugins.def_dataset_methods(self, [:eager, :eager_graph, :eager_graph_with_options, :association_join, :association_full_join, :association_inner_join, :association_left_join, :association_right_join])
2013
+
2014
+ private
2015
+
2016
+ # The default value for the instance_specific option, if the association
2017
+ # could be instance specific and the :instance_specific option is not specified.
2018
+ def _association_instance_specific_default(_)
2019
+ true
2020
+ end
2021
+
2022
+ # The module to use for the association's methods. Defaults to
2023
+ # the overridable_methods_module.
2024
+ def association_module(opts=OPTS)
2025
+ opts.fetch(:methods_module, overridable_methods_module)
2026
+ end
2027
+
2028
+ # Add a method to the module included in the class, so the method
2029
+ # can be easily overridden in the class itself while allowing for
2030
+ # super to be called.
2031
+ def association_module_def(name, opts=OPTS, &block)
2032
+ mod = association_module(opts)
2033
+ mod.send(:define_method, name, &block)
2034
+ mod.send(:alias_method, name, name)
2035
+ end
2036
+
2037
+ # Add a method to the module included in the class, so the method
2038
+ # can be easily overridden in the class itself while allowing for
2039
+ # super to be called. This method allows passing keywords through
2040
+ # the defined methods.
2041
+ def association_module_delegate_def(name, opts, &block)
2042
+ mod = association_module(opts)
2043
+ mod.send(:define_method, name, &block)
2044
+ # :nocov:
2045
+ mod.send(:ruby2_keywords, name) if mod.respond_to?(:ruby2_keywords, true)
2046
+ # :nocov:
2047
+ mod.send(:alias_method, name, name)
2048
+ end
2049
+
2050
+ # Add a private method to the module included in the class.
2051
+ def association_module_private_def(name, opts=OPTS, &block)
2052
+ association_module_def(name, opts, &block)
2053
+ association_module(opts).send(:private, name)
2054
+ end
2055
+
2056
+ # Delegate to the type-specific association method to setup the
2057
+ # association, and define the association instance methods.
2058
+ def def_association(opts)
2059
+ send(:"def_#{opts[:type]}", opts)
2060
+ def_association_instance_methods(opts)
2061
+ end
2062
+
2063
+ # Adds the association method to the association methods module.
2064
+ def def_association_method(opts)
2065
+ association_module_def(opts.association_method, opts) do |dynamic_opts=OPTS, &block|
2066
+ load_associated_objects(opts, dynamic_opts, &block)
2067
+ end
2068
+ end
2069
+
2070
+ # Define all of the association instance methods for this association.
2071
+ def def_association_instance_methods(opts)
2072
+ # Always set the method names in the association reflection, even if they
2073
+ # are not used, for backwards compatibility.
2074
+ opts[:dataset_method] = :"#{opts[:name]}_dataset"
2075
+ if opts.returns_array?
2076
+ sname = singularize(opts[:name])
2077
+ opts[:_add_method] = :"_add_#{sname}"
2078
+ opts[:add_method] = :"add_#{sname}"
2079
+ opts[:_remove_method] = :"_remove_#{sname}"
2080
+ opts[:remove_method] = :"remove_#{sname}"
2081
+ opts[:_remove_all_method] = :"_remove_all_#{opts[:name]}"
2082
+ opts[:remove_all_method] = :"remove_all_#{opts[:name]}"
2083
+ else
2084
+ opts[:_setter_method] = :"_#{opts[:name]}="
2085
+ opts[:setter_method] = :"#{opts[:name]}="
2086
+ end
2087
+
2088
+ association_module_def(opts.dataset_method, opts){_dataset(opts)} unless opts[:no_dataset_method]
2089
+ if opts[:block]
2090
+ opts[:block_method] = Plugins.def_sequel_method(association_module(opts), "#{opts[:name]}_block", 1, &opts[:block])
2091
+ end
2092
+ opts[:dataset_opt_arity] = opts[:dataset].arity == 0 ? 0 : 1
2093
+ opts[:dataset_opt_method] = Plugins.def_sequel_method(association_module(opts), "#{opts[:name]}_dataset_opt", opts[:dataset_opt_arity], &opts[:dataset])
2094
+ def_association_method(opts) unless opts[:no_association_method]
2095
+
2096
+ return if opts[:read_only]
2097
+
2098
+ if opts[:setter] && opts[:_setter]
2099
+ # This is backwards due to backwards compatibility
2100
+ association_module_private_def(opts[:_setter_method], opts, &opts[:setter])
2101
+ association_module_def(opts[:setter_method], opts, &opts[:_setter])
2102
+ end
2103
+
2104
+ if adder = opts[:adder]
2105
+ association_module_private_def(opts[:_add_method], opts, &adder)
2106
+ association_module_delegate_def(opts[:add_method], opts){|o,*args| add_associated_object(opts, o, *args)}
2107
+ end
2108
+
2109
+ if remover = opts[:remover]
2110
+ association_module_private_def(opts[:_remove_method], opts, &remover)
2111
+ association_module_delegate_def(opts[:remove_method], opts){|o,*args| remove_associated_object(opts, o, *args)}
2112
+ end
2113
+
2114
+ if clearer = opts[:clearer]
2115
+ association_module_private_def(opts[:_remove_all_method], opts, &clearer)
2116
+ association_module_delegate_def(opts[:remove_all_method], opts){|*args| remove_all_associated_objects(opts, *args)}
2117
+ end
2118
+ end
2119
+
2120
+ # Configures many_to_many and one_through_one association reflection and adds the related association methods
2121
+ def def_many_to_many(opts)
2122
+ one_through_one = opts[:type] == :one_through_one
2123
+ left = (opts[:left_key] ||= opts.default_left_key)
2124
+ lcks = opts[:left_keys] = Array(left)
2125
+ right = (opts[:right_key] ||= opts.default_right_key)
2126
+ rcks = opts[:right_keys] = Array(right)
2127
+ left_pk = (opts[:left_primary_key] ||= self.primary_key)
2128
+ opts[:eager_loader_key] = left_pk unless opts.has_key?(:eager_loader_key)
2129
+ lcpks = opts[:left_primary_keys] = Array(left_pk)
2130
+ lpkc = opts[:left_primary_key_column] ||= left_pk
2131
+ lpkcs = opts[:left_primary_key_columns] ||= Array(lpkc)
2132
+ raise(Error, "mismatched number of left keys: #{lcks.inspect} vs #{lcpks.inspect}") unless lcks.length == lcpks.length
2133
+ if opts[:right_primary_key]
2134
+ rcpks = Array(opts[:right_primary_key])
2135
+ raise(Error, "mismatched number of right keys: #{rcks.inspect} vs #{rcpks.inspect}") unless rcks.length == rcpks.length
2136
+ end
2137
+ opts[:uses_left_composite_keys] = lcks.length > 1
2138
+ uses_rcks = opts[:uses_right_composite_keys] = rcks.length > 1
2139
+ opts[:cartesian_product_number] ||= one_through_one ? 0 : 1
2140
+ join_table = (opts[:join_table] ||= opts.default_join_table)
2141
+ opts[:left_key_alias] ||= opts.default_associated_key_alias
2142
+ opts[:graph_join_table_join_type] ||= opts[:graph_join_type]
2143
+ if opts[:uniq]
2144
+ opts[:after_load] ||= []
2145
+ opts[:after_load].unshift(:array_uniq!)
2146
+ end
2147
+ if join_table_db = opts[:join_table_db]
2148
+ opts[:use_placeholder_loader] = false
2149
+ opts[:allow_eager_graph] = false
2150
+ opts[:allow_filtering_by] = false
2151
+ opts[:eager_limit_strategy] = nil
2152
+ join_table_ds = join_table_db.from(join_table)
2153
+ opts[:dataset] ||= proc do |r|
2154
+ vals = join_table_ds.where(lcks.zip(lcpks.map{|k| get_column_value(k)})).select_map(right)
2155
+ ds = r.associated_dataset.where(opts.right_primary_key => vals)
2156
+ if uses_rcks
2157
+ vals.delete_if{|v| v.any?(&:nil?)}
2158
+ else
2159
+ vals.delete(nil)
2160
+ end
2161
+ ds = ds.clone(:no_results=>true) if vals.empty?
2162
+ ds
2163
+ end
2164
+ opts[:eager_loader] ||= proc do |eo|
2165
+ h = eo[:id_map]
2166
+ assign_singular = opts.assign_singular?
2167
+ rpk = opts.right_primary_key
2168
+ name = opts[:name]
2169
+
2170
+ join_map = join_table_ds.where(left=>h.keys).select_hash_groups(right, left)
2171
+
2172
+ if uses_rcks
2173
+ join_map.delete_if{|v,| v.any?(&:nil?)}
2174
+ else
2175
+ join_map.delete(nil)
2176
+ end
2177
+
2178
+ eo = Hash[eo]
2179
+
2180
+ if join_map.empty?
2181
+ eo[:no_results] = true
2182
+ else
2183
+ join_map.each_value do |vs|
2184
+ vs.replace(vs.flat_map{|v| h[v]})
2185
+ vs.uniq!
2186
+ end
2187
+
2188
+ eo[:loader] = false
2189
+ eo[:right_keys] = join_map.keys
2190
+ end
2191
+
2192
+ opts[:model].eager_load_results(opts, eo) do |assoc_record|
2193
+ rpkv = if uses_rcks
2194
+ assoc_record.values.values_at(*rpk)
2195
+ else
2196
+ assoc_record.values[rpk]
2197
+ end
2198
+
2199
+ objects = join_map[rpkv]
2200
+
2201
+ if assign_singular
2202
+ objects.each do |object|
2203
+ object.associations[name] ||= assoc_record
2204
+ end
2205
+ else
2206
+ objects.each do |object|
2207
+ object.associations[name].push(assoc_record)
2208
+ end
2209
+ end
2210
+ end
2211
+ end
2212
+ else
2213
+ opts[:dataset] ||= opts.association_dataset_proc
2214
+ opts[:eager_loader] ||= opts.method(:default_eager_loader)
2215
+ end
2216
+
2217
+ join_type = opts[:graph_join_type]
2218
+ select = opts[:graph_select]
2219
+ use_only_conditions = opts.include?(:graph_only_conditions)
2220
+ only_conditions = opts[:graph_only_conditions]
2221
+ conditions = opts[:graph_conditions]
2222
+ graph_block = opts[:graph_block]
2223
+ graph_jt_conds = opts[:graph_join_table_conditions] = opts.fetch(:graph_join_table_conditions, []).to_a
2224
+ use_jt_only_conditions = opts.include?(:graph_join_table_only_conditions)
2225
+ jt_only_conditions = opts[:graph_join_table_only_conditions]
2226
+ jt_join_type = opts[:graph_join_table_join_type]
2227
+ jt_graph_block = opts[:graph_join_table_block]
2228
+ opts[:eager_grapher] ||= proc do |eo|
2229
+ ds = eo[:self]
2230
+ egls = eo[:limit_strategy]
2231
+ if egls && egls != :ruby
2232
+ associated_key_array = opts.associated_key_array
2233
+ orig_egds = egds = eager_graph_dataset(opts, eo)
2234
+ egds = egds.
2235
+ inner_join(join_table, rcks.zip(opts.right_primary_keys) + graph_jt_conds, :qualify=>:deep).
2236
+ select_all(egds.first_source).
2237
+ select_append(*associated_key_array)
2238
+ egds = opts.apply_eager_graph_limit_strategy(egls, egds)
2239
+ ds.graph(egds, associated_key_array.map(&:alias).zip(lpkcs) + conditions, :qualify=>:deep, :table_alias=>eo[:table_alias], :implicit_qualifier=>eo[:implicit_qualifier], :join_type=>eo[:join_type]||join_type, :from_self_alias=>eo[:from_self_alias], :join_only=>eo[:join_only], :select=>select||orig_egds.columns, &graph_block)
2240
+ else
2241
+ ds = ds.graph(join_table, use_jt_only_conditions ? jt_only_conditions : lcks.zip(lpkcs) + graph_jt_conds, :select=>false, :table_alias=>ds.unused_table_alias(join_table, [eo[:table_alias]]), :join_type=>eo[:join_type]||jt_join_type, :join_only=>eo[:join_only], :implicit_qualifier=>eo[:implicit_qualifier], :qualify=>:deep, :from_self_alias=>eo[:from_self_alias], &jt_graph_block)
2242
+ ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : opts.right_primary_keys.zip(rcks) + conditions, :select=>select, :table_alias=>eo[:table_alias], :qualify=>:deep, :join_type=>eo[:join_type]||join_type, :join_only=>eo[:join_only], &graph_block)
2243
+ end
2244
+ end
2245
+
2246
+ return if opts[:read_only]
2247
+
2248
+ if one_through_one
2249
+ unless opts.has_key?(:setter)
2250
+ opts[:setter] = proc do |o|
2251
+ h = {}
2252
+ lh = lcks.zip(lcpks.map{|k| get_column_value(k)})
2253
+ jtds = _join_table_dataset(opts).where(lh)
2254
+
2255
+ checked_transaction do
2256
+ current = jtds.first
2257
+
2258
+ if o
2259
+ new_values = []
2260
+ rcks.zip(opts.right_primary_key_methods).each{|k, pk| new_values << (h[k] = o.get_column_value(pk))}
2261
+ end
2262
+
2263
+ if current
2264
+ current_values = rcks.map{|k| current[k]}
2265
+ jtds = jtds.where(rcks.zip(current_values))
2266
+ if o
2267
+ if current_values != new_values
2268
+ jtds.update(h)
2269
+ end
2270
+ else
2271
+ jtds.delete
2272
+ end
2273
+ elsif o
2274
+ lh.each{|k,v| h[k] = v}
2275
+ jtds.insert(h)
2276
+ end
2277
+ end
2278
+ end
2279
+ end
2280
+ if opts.fetch(:setter, true)
2281
+ opts[:_setter] = proc{|o| set_one_through_one_associated_object(opts, o)}
2282
+ end
2283
+ else
2284
+ unless opts.has_key?(:adder)
2285
+ opts[:adder] = proc do |o|
2286
+ h = {}
2287
+ lcks.zip(lcpks).each{|k, pk| h[k] = get_column_value(pk)}
2288
+ rcks.zip(opts.right_primary_key_methods).each{|k, pk| h[k] = o.get_column_value(pk)}
2289
+ _join_table_dataset(opts).insert(h)
2290
+ end
2291
+ end
2292
+
2293
+ unless opts.has_key?(:remover)
2294
+ opts[:remover] = proc do |o|
2295
+ _join_table_dataset(opts).where(lcks.zip(lcpks.map{|k| get_column_value(k)}) + rcks.zip(opts.right_primary_key_methods.map{|k| o.get_column_value(k)})).delete
2296
+ end
2297
+ end
2298
+
2299
+ unless opts.has_key?(:clearer)
2300
+ opts[:clearer] = proc do
2301
+ _join_table_dataset(opts).where(lcks.zip(lcpks.map{|k| get_column_value(k)})).delete
2302
+ end
2303
+ end
2304
+ end
2305
+ end
2306
+
2307
+ # Configures many_to_one association reflection and adds the related association methods
2308
+ def def_many_to_one(opts)
2309
+ name = opts[:name]
2310
+ opts[:key] = opts.default_key unless opts.has_key?(:key)
2311
+ key = opts[:key]
2312
+ opts[:eager_loader_key] = key unless opts.has_key?(:eager_loader_key)
2313
+ cks = opts[:graph_keys] = opts[:keys] = Array(key)
2314
+ opts[:key_column] ||= key
2315
+ opts[:graph_keys] = opts[:key_columns] = Array(opts[:key_column])
2316
+ opts[:qualified_key] = opts.qualify_cur(key)
2317
+ if opts[:primary_key]
2318
+ cpks = Array(opts[:primary_key])
2319
+ raise(Error, "mismatched number of keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
2320
+ end
2321
+ uses_cks = opts[:uses_composite_keys] = cks.length > 1
2322
+ opts[:cartesian_product_number] ||= 0
2323
+
2324
+ if !opts.has_key?(:many_to_one_pk_lookup) &&
2325
+ (opts[:dataset] || opts[:conditions] || opts[:block] || opts[:select] ||
2326
+ (opts.has_key?(:key) && opts[:key] == nil))
2327
+ opts[:many_to_one_pk_lookup] = false
2328
+ end
2329
+ auto_assocs = @autoreloading_associations
2330
+ cks.each do |k|
2331
+ (auto_assocs[k] ||= []) << name
2332
+ end
2333
+
2334
+ opts[:dataset] ||= opts.association_dataset_proc
2335
+ opts[:eager_loader] ||= proc do |eo|
2336
+ h = eo[:id_map]
2337
+ pk_meths = opts.primary_key_methods
2338
+
2339
+ eager_load_results(opts, eo) do |assoc_record|
2340
+ hash_key = uses_cks ? pk_meths.map{|k| assoc_record.get_column_value(k)} : assoc_record.get_column_value(opts.primary_key_method)
2341
+ h[hash_key].each{|object| object.associations[name] = assoc_record}
2342
+ end
2343
+ end
2344
+
2345
+ join_type = opts[:graph_join_type]
2346
+ select = opts[:graph_select]
2347
+ use_only_conditions = opts.include?(:graph_only_conditions)
2348
+ only_conditions = opts[:graph_only_conditions]
2349
+ conditions = opts[:graph_conditions]
2350
+ graph_block = opts[:graph_block]
2351
+ graph_cks = opts[:graph_keys]
2352
+ opts[:eager_grapher] ||= proc do |eo|
2353
+ ds = eo[:self]
2354
+ ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : opts.primary_keys.zip(graph_cks) + conditions, eo.merge(:select=>select, :join_type=>eo[:join_type]||join_type, :qualify=>:deep), &graph_block)
2355
+ end
2356
+
2357
+ return if opts[:read_only]
2358
+
2359
+ unless opts.has_key?(:setter)
2360
+ opts[:setter] = proc{|o| cks.zip(opts.primary_key_methods).each{|k, pk| set_column_value(:"#{k}=", (o.get_column_value(pk) if o))}}
2361
+ end
2362
+ if opts.fetch(:setter, true)
2363
+ opts[:_setter] = proc{|o| set_associated_object(opts, o)}
2364
+ end
2365
+ end
2366
+
2367
+ # Configures one_to_many and one_to_one association reflections and adds the related association methods
2368
+ def def_one_to_many(opts)
2369
+ one_to_one = opts[:type] == :one_to_one
2370
+ name = opts[:name]
2371
+ key = (opts[:key] ||= opts.default_key)
2372
+ km = opts[:key_method] ||= opts[:key]
2373
+ cks = opts[:keys] = Array(key)
2374
+ opts[:key_methods] = Array(opts[:key_method])
2375
+ primary_key = (opts[:primary_key] ||= self.primary_key)
2376
+ opts[:eager_loader_key] = primary_key unless opts.has_key?(:eager_loader_key)
2377
+ cpks = opts[:primary_keys] = Array(primary_key)
2378
+ pkc = opts[:primary_key_column] ||= primary_key
2379
+ pkcs = opts[:primary_key_columns] ||= Array(pkc)
2380
+ raise(Error, "mismatched number of keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
2381
+ uses_cks = opts[:uses_composite_keys] = cks.length > 1
2382
+ opts[:dataset] ||= opts.association_dataset_proc
2383
+ opts[:eager_loader] ||= proc do |eo|
2384
+ h = eo[:id_map]
2385
+ reciprocal = opts.reciprocal
2386
+ assign_singular = opts.assign_singular?
2387
+ delete_rn = opts.delete_row_number_column
2388
+
2389
+ eager_load_results(opts, eo) do |assoc_record|
2390
+ assoc_record.remove_key!(delete_rn) if delete_rn
2391
+ hash_key = uses_cks ? km.map{|k| assoc_record.get_column_value(k)} : assoc_record.get_column_value(km)
2392
+ objects = h[hash_key]
2393
+ if assign_singular
2394
+ objects.each do |object|
2395
+ unless object.associations[name]
2396
+ object.associations[name] = assoc_record
2397
+ assoc_record.associations[reciprocal] = object if reciprocal
2398
+ end
2399
+ end
2400
+ else
2401
+ objects.each do |object|
2402
+ object.associations[name].push(assoc_record)
2403
+ assoc_record.associations[reciprocal] = object if reciprocal
2404
+ end
2405
+ end
2406
+ end
2407
+ end
2408
+
2409
+ join_type = opts[:graph_join_type]
2410
+ select = opts[:graph_select]
2411
+ use_only_conditions = opts.include?(:graph_only_conditions)
2412
+ only_conditions = opts[:graph_only_conditions]
2413
+ conditions = opts[:graph_conditions]
2414
+ opts[:cartesian_product_number] ||= one_to_one ? 0 : 1
2415
+ graph_block = opts[:graph_block]
2416
+ opts[:eager_grapher] ||= proc do |eo|
2417
+ ds = eo[:self]
2418
+ ds = ds.graph(opts.apply_eager_graph_limit_strategy(eo[:limit_strategy], eager_graph_dataset(opts, eo)), use_only_conditions ? only_conditions : cks.zip(pkcs) + conditions, eo.merge(:select=>select, :join_type=>eo[:join_type]||join_type, :qualify=>:deep), &graph_block)
2419
+ # We only load reciprocals for one_to_many associations, as other reciprocals don't make sense
2420
+ ds.opts[:eager_graph][:reciprocals][eo[:table_alias]] = opts.reciprocal
2421
+ ds
2422
+ end
2423
+
2424
+ return if opts[:read_only]
2425
+
2426
+ save_opts = {:validate=>opts[:validate]}
2427
+ ck_nil_hash ={}
2428
+ cks.each{|k| ck_nil_hash[k] = nil}
2429
+
2430
+ if one_to_one
2431
+ unless opts.has_key?(:setter)
2432
+ opts[:setter] = proc do |o|
2433
+ up_ds = _apply_association_options(opts, opts.associated_dataset.where(cks.zip(cpks.map{|k| get_column_value(k)})))
2434
+
2435
+ if (froms = up_ds.opts[:from]) && (from = froms[0]) && (from.is_a?(Sequel::Dataset) || (from.is_a?(Sequel::SQL::AliasedExpression) && from.expression.is_a?(Sequel::Dataset)))
2436
+ if old = up_ds.first
2437
+ cks.each{|k| old.set_column_value(:"#{k}=", nil)}
2438
+ end
2439
+ save_old = true
2440
+ end
2441
+
2442
+ if o
2443
+ if !o.new? && !save_old
2444
+ up_ds = up_ds.exclude(o.pk_hash)
2445
+ end
2446
+ cks.zip(cpks).each{|k, pk| o.set_column_value(:"#{k}=", get_column_value(pk))}
2447
+ end
2448
+
2449
+ checked_transaction do
2450
+ if save_old
2451
+ old.save(save_opts) || raise(Sequel::Error, "invalid previously associated object, cannot save") if old
2452
+ else
2453
+ up_ds.skip_limit_check.update(ck_nil_hash)
2454
+ end
2455
+
2456
+ o.save(save_opts) || raise(Sequel::Error, "invalid associated object, cannot save") if o
2457
+ end
2458
+ end
2459
+ end
2460
+ if opts.fetch(:setter, true)
2461
+ opts[:_setter] = proc{|o| set_one_to_one_associated_object(opts, o)}
2462
+ end
2463
+ else
2464
+ save_opts[:raise_on_failure] = opts[:raise_on_save_failure] != false
2465
+
2466
+ unless opts.has_key?(:adder)
2467
+ opts[:adder] = proc do |o|
2468
+ cks.zip(cpks).each{|k, pk| o.set_column_value(:"#{k}=", get_column_value(pk))}
2469
+ o.save(save_opts)
2470
+ end
2471
+ end
2472
+
2473
+ unless opts.has_key?(:remover)
2474
+ opts[:remover] = proc do |o|
2475
+ cks.each{|k| o.set_column_value(:"#{k}=", nil)}
2476
+ o.save(save_opts)
2477
+ end
2478
+ end
2479
+
2480
+ unless opts.has_key?(:clearer)
2481
+ opts[:clearer] = proc do
2482
+ _apply_association_options(opts, opts.associated_dataset.where(cks.zip(cpks.map{|k| get_column_value(k)}))).update(ck_nil_hash)
2483
+ end
2484
+ end
2485
+ end
2486
+ end
2487
+
2488
+ # Alias of def_many_to_many, since they share pretty much the same code.
2489
+ def def_one_through_one(opts)
2490
+ def_many_to_many(opts)
2491
+ end
2492
+
2493
+ # Alias of def_one_to_many, since they share pretty much the same code.
2494
+ def def_one_to_one(opts)
2495
+ def_one_to_many(opts)
2496
+ end
2497
+
2498
+ # Return dataset to graph into given the association reflection, applying the :callback option if set.
2499
+ def eager_graph_dataset(opts, eager_options)
2500
+ ds = opts.associated_class.dataset
2501
+ if opts[:graph_use_association_block] && (b = opts[:block])
2502
+ ds = b.call(ds)
2503
+ end
2504
+ if cb = eager_options[:callback]
2505
+ ds = cb.call(ds)
2506
+ end
2507
+ ds
2508
+ end
2509
+
2510
+ # If not caching associations, reload the database schema by default,
2511
+ # ignoring any cached values.
2512
+ def reload_db_schema?
2513
+ !@cache_associations
2514
+ end
2515
+ end
2516
+
2517
+ # Instance methods used to implement the associations support.
2518
+ module InstanceMethods
2519
+ # The currently cached associations. A hash with the keys being the
2520
+ # association name symbols and the values being the associated object
2521
+ # or nil (many_to_one), or the array of associated objects (*_to_many).
2522
+ def associations
2523
+ @associations ||= {}
2524
+ end
2525
+
2526
+ # Freeze the associations cache when freezing the object. Note that
2527
+ # retrieving associations after freezing will still work in most cases,
2528
+ # but the associations will not be cached in the association cache.
2529
+ def freeze
2530
+ associations
2531
+ super
2532
+ associations.freeze
2533
+ self
2534
+ end
2535
+
2536
+ private
2537
+
2538
+ # Apply the association options such as :order and :limit to the given dataset, returning a modified dataset.
2539
+ def _apply_association_options(opts, ds)
2540
+ unless ds.kind_of?(AssociationDatasetMethods)
2541
+ ds = opts.apply_dataset_changes(ds)
2542
+ end
2543
+ ds = ds.clone(:model_object => self)
2544
+ ds = ds.eager_graph(opts[:eager_graph]) if opts[:eager_graph] && opts.eager_graph_lazy_dataset?
2545
+ # block method is private
2546
+ ds = send(opts[:block_method], ds) if opts[:block_method]
2547
+ ds
2548
+ end
2549
+
2550
+ # Return a dataset for the association after applying any dynamic callback.
2551
+ def _associated_dataset(opts, dynamic_opts)
2552
+ ds = public_send(opts.dataset_method)
2553
+ if callback = dynamic_opts[:callback]
2554
+ ds = callback.call(ds)
2555
+ end
2556
+ ds
2557
+ end
2558
+
2559
+ # A placeholder literalizer that can be used to load the association, or nil to not use one.
2560
+ def _associated_object_loader(opts, dynamic_opts)
2561
+ if !dynamic_opts[:callback] && (loader = opts.placeholder_loader)
2562
+ loader
2563
+ end
2564
+ end
2565
+
2566
+ # Return an association dataset for the given association reflection
2567
+ def _dataset(opts)
2568
+ raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
2569
+ ds = if opts[:dataset_opt_arity] == 1
2570
+ # dataset_opt_method is private
2571
+ send(opts[:dataset_opt_method], opts)
2572
+ else
2573
+ send(opts[:dataset_opt_method])
2574
+ end
2575
+ _apply_association_options(opts, ds)
2576
+ end
2577
+
2578
+ # Dataset for the join table of the given many to many association reflection
2579
+ def _join_table_dataset(opts)
2580
+ ds = (opts[:join_table_db] || model.db).from(opts.join_table_source)
2581
+ opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
2582
+ end
2583
+
2584
+ # Return the associated single object for the given association reflection and dynamic options
2585
+ # (or nil if no associated object).
2586
+ def _load_associated_object(opts, dynamic_opts)
2587
+ _load_associated_object_array(opts, dynamic_opts).first
2588
+ end
2589
+
2590
+ # Return the associated single object using a primary key lookup on the associated class.
2591
+ def _load_associated_object_via_primary_key(opts)
2592
+ opts.associated_class.send(:primary_key_lookup, ((fk = opts[:key]).is_a?(Array) ? fk.map{|c| get_column_value(c)} : get_column_value(fk)))
2593
+ end
2594
+
2595
+ # Load the associated objects for the given association reflection and dynamic options
2596
+ # as an array.
2597
+ def _load_associated_object_array(opts, dynamic_opts)
2598
+ if loader = _associated_object_loader(opts, dynamic_opts)
2599
+ loader.all(*opts.predicate_key_values(self))
2600
+ else
2601
+ ds = _associated_dataset(opts, dynamic_opts)
2602
+ if ds.opts[:no_results]
2603
+ []
2604
+ else
2605
+ ds.all
2606
+ end
2607
+ end
2608
+ end
2609
+
2610
+ # Return the associated objects from the dataset, without association callbacks, reciprocals, and caching.
2611
+ # Still apply the dynamic callback if present.
2612
+ def _load_associated_objects(opts, dynamic_opts=OPTS)
2613
+ if opts.can_have_associated_objects?(self)
2614
+ if opts.returns_array?
2615
+ _load_associated_object_array(opts, dynamic_opts)
2616
+ elsif load_with_primary_key_lookup?(opts, dynamic_opts)
2617
+ _load_associated_object_via_primary_key(opts)
2618
+ else
2619
+ _load_associated_object(opts, dynamic_opts)
2620
+ end
2621
+ elsif opts.returns_array?
2622
+ []
2623
+ end
2624
+ end
2625
+
2626
+ # Clear the associations cache when refreshing
2627
+ def _refresh_set_values(hash)
2628
+ @associations.clear if @associations
2629
+ super
2630
+ end
2631
+
2632
+ # Add the given associated object to the given association
2633
+ def add_associated_object(opts, o, *args)
2634
+ o = make_add_associated_object(opts, o)
2635
+ raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
2636
+ ensure_associated_primary_key(opts, o, *args)
2637
+ return if run_association_callbacks(opts, :before_add, o) == false
2638
+ # Allow calling private _add method
2639
+ return if !send(opts[:_add_method], o, *args) && opts.handle_silent_modification_failure?
2640
+ if array = associations[opts[:name]] and !array.include?(o)
2641
+ array.push(o)
2642
+ end
2643
+ add_reciprocal_object(opts, o)
2644
+ run_association_callbacks(opts, :after_add, o)
2645
+ o
2646
+ end
2647
+ # :nocov:
2648
+ ruby2_keywords(:add_associated_object) if respond_to?(:ruby2_keywords, true)
2649
+ # :nocov:
2650
+
2651
+ # Add/Set the current object to/as the given object's reciprocal association.
2652
+ def add_reciprocal_object(opts, o)
2653
+ return if o.frozen?
2654
+ return unless reciprocal = opts.reciprocal
2655
+ if opts.reciprocal_array?
2656
+ if array = o.associations[reciprocal] and !array.include?(self)
2657
+ array.push(self)
2658
+ end
2659
+ else
2660
+ o.associations[reciprocal] = self
2661
+ end
2662
+ end
2663
+
2664
+ # Call uniq! on the given array. This is used by the :uniq option,
2665
+ # and is an actual method for memory reasons.
2666
+ def array_uniq!(a)
2667
+ a.uniq!
2668
+ end
2669
+
2670
+ # If a foreign key column value changes, clear the related
2671
+ # cached associations.
2672
+ def change_column_value(column, value)
2673
+ if assocs = model.autoreloading_associations[column]
2674
+ vals = @values
2675
+ if new?
2676
+ # Do deeper checking for new objects, so that associations are
2677
+ # not deleted when values do not change. This code is run at
2678
+ # a higher level for existing objects.
2679
+ if value == (c = vals[column]) && value.class == c.class
2680
+ # If the value is the same, there is no reason to delete
2681
+ # the related associations, so exit early in that case.
2682
+ return super
2683
+ end
2684
+
2685
+ only_delete_nil = c.nil?
2686
+ elsif vals[column].nil?
2687
+ only_delete_nil = true
2688
+ end
2689
+
2690
+ if only_delete_nil
2691
+ # If the current foreign key value is nil, but the association
2692
+ # is already present in the cache, it was probably added to the
2693
+ # cache for a reason, and we do not want to delete it in that case.
2694
+ # However, we still want to delete associations with nil values
2695
+ # to remove the cached false negative.
2696
+ assocs.each{|a| associations.delete(a) if associations[a].nil?}
2697
+ else
2698
+ assocs.each{|a| associations.delete(a)}
2699
+ end
2700
+ end
2701
+ super
2702
+ end
2703
+
2704
+ # Save the associated object if the associated object needs a primary key
2705
+ # and the associated object is new and does not have one. Raise an error if
2706
+ # the object still does not have a primary key
2707
+ def ensure_associated_primary_key(opts, o, *args)
2708
+ if opts.need_associated_primary_key?
2709
+ o.save(:validate=>opts[:validate]) if o.new?
2710
+ raise(Sequel::Error, "associated object #{o.inspect} does not have a primary key") unless o.pk
2711
+ end
2712
+ end
2713
+
2714
+ # Duplicate the associations hash when duplicating the object.
2715
+ def initialize_copy(other)
2716
+ super
2717
+ @associations = Hash[@associations] if @associations
2718
+ self
2719
+ end
2720
+
2721
+ # If a block is given, assign it as the :callback option in the hash, and return the hash.
2722
+ def load_association_objects_options(dynamic_opts, &block)
2723
+ if block
2724
+ dynamic_opts = Hash[dynamic_opts]
2725
+ dynamic_opts[:callback] = block
2726
+ end
2727
+
2728
+ dynamic_opts
2729
+ end
2730
+
2731
+ # Load the associated objects using the dataset, handling callbacks, reciprocals, and caching.
2732
+ def load_associated_objects(opts, dynamic_opts, &block)
2733
+ dynamic_opts = load_association_objects_options(dynamic_opts, &block)
2734
+ name = opts[:name]
2735
+ if associations.include?(name) && !dynamic_opts[:callback] && !dynamic_opts[:reload]
2736
+ associations[name]
2737
+ else
2738
+ objs = _load_associated_objects(opts, dynamic_opts)
2739
+ if opts.set_reciprocal_to_self?
2740
+ if opts.returns_array?
2741
+ objs.each{|o| add_reciprocal_object(opts, o)}
2742
+ elsif objs
2743
+ add_reciprocal_object(opts, objs)
2744
+ end
2745
+ end
2746
+
2747
+ # If the current object is frozen, you can't update the associations
2748
+ # cache. This can cause issues for after_load procs that expect
2749
+ # the objects to be already cached in the associations, but
2750
+ # unfortunately that case cannot be handled.
2751
+ associations[name] = objs unless frozen?
2752
+ run_association_callbacks(opts, :after_load, objs)
2753
+ frozen? ? objs : associations[name]
2754
+ end
2755
+ end
2756
+
2757
+ # Whether to use a simple primary key lookup on the associated class when loading.
2758
+ def load_with_primary_key_lookup?(opts, dynamic_opts)
2759
+ opts[:type] == :many_to_one &&
2760
+ !dynamic_opts[:callback] &&
2761
+ opts.send(:cached_fetch, :many_to_one_pk_lookup){opts.primary_key == opts.associated_class.primary_key}
2762
+ end
2763
+
2764
+ # Convert the input of the add_* association method into an associated object. For
2765
+ # hashes, this creates a new object using the hash. For integers, strings, and arrays,
2766
+ # assume the value specifies a primary key, and lookup an existing object with that primary key.
2767
+ # Otherwise, if the object is not already an instance of the class, raise an exception.
2768
+ def make_add_associated_object(opts, o)
2769
+ klass = opts.associated_class
2770
+
2771
+ case o
2772
+ when Hash
2773
+ klass.new(o)
2774
+ when Integer, String, Array
2775
+ klass.with_pk!(o)
2776
+ when klass
2777
+ o
2778
+ else
2779
+ raise(Sequel::Error, "associated object #{o.inspect} not of correct type #{klass}")
2780
+ end
2781
+ end
2782
+
2783
+ # Remove all associated objects from the given association
2784
+ def remove_all_associated_objects(opts, *args)
2785
+ raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
2786
+ # Allow calling private _remove_all method
2787
+ send(opts[:_remove_all_method], *args)
2788
+ ret = associations[opts[:name]].each{|o| remove_reciprocal_object(opts, o)} if associations.include?(opts[:name])
2789
+ associations[opts[:name]] = []
2790
+ ret
2791
+ end
2792
+ # :nocov:
2793
+ ruby2_keywords(:remove_all_associated_objects) if respond_to?(:ruby2_keywords, true)
2794
+ # :nocov:
2795
+
2796
+ # Remove the given associated object from the given association
2797
+ def remove_associated_object(opts, o, *args)
2798
+ klass = opts.associated_class
2799
+ if o.is_a?(Integer) || o.is_a?(String) || o.is_a?(Array)
2800
+ o = remove_check_existing_object_from_pk(opts, o, *args)
2801
+ elsif !o.is_a?(klass)
2802
+ raise(Sequel::Error, "associated object #{o.inspect} not of correct type #{klass}")
2803
+ elsif opts.remove_should_check_existing? && public_send(opts.dataset_method).where(o.pk_hash).empty?
2804
+ raise(Sequel::Error, "associated object #{o.inspect} is not currently associated to #{inspect}")
2805
+ end
2806
+ raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
2807
+ raise(Sequel::Error, "associated object #{o.inspect} does not have a primary key") if opts.need_associated_primary_key? && !o.pk
2808
+ return if run_association_callbacks(opts, :before_remove, o) == false
2809
+ # Allow calling private _remove method
2810
+ return if !send(opts[:_remove_method], o, *args) && opts.handle_silent_modification_failure?
2811
+ associations[opts[:name]].delete_if{|x| o === x} if associations.include?(opts[:name])
2812
+ remove_reciprocal_object(opts, o)
2813
+ run_association_callbacks(opts, :after_remove, o)
2814
+ o
2815
+ end
2816
+ # :nocov:
2817
+ ruby2_keywords(:remove_associated_object) if respond_to?(:ruby2_keywords, true)
2818
+ # :nocov:
2819
+
2820
+ # Check that the object from the associated table specified by the primary key
2821
+ # is currently associated to the receiver. If it is associated, return the object, otherwise
2822
+ # raise an error.
2823
+ def remove_check_existing_object_from_pk(opts, o, *args)
2824
+ key = o
2825
+ pkh = opts.associated_class.qualified_primary_key_hash(key)
2826
+ raise(Sequel::Error, "no object with key(s) #{key.inspect} is currently associated to #{inspect}") unless o = public_send(opts.dataset_method).first(pkh)
2827
+ o
2828
+ end
2829
+
2830
+ # Remove/unset the current object from/as the given object's reciprocal association.
2831
+ def remove_reciprocal_object(opts, o)
2832
+ return unless reciprocal = opts.reciprocal
2833
+ if opts.reciprocal_array?
2834
+ if array = o.associations[reciprocal]
2835
+ array.delete_if{|x| self === x}
2836
+ end
2837
+ else
2838
+ o.associations[reciprocal] = nil
2839
+ end
2840
+ end
2841
+
2842
+ # Run the callback for the association with the object.
2843
+ def run_association_callbacks(reflection, callback_type, object)
2844
+ return unless cbs = reflection[callback_type]
2845
+
2846
+ begin
2847
+ cbs.each do |cb|
2848
+ case cb
2849
+ when Symbol
2850
+ # Allow calling private methods in association callbacks
2851
+ send(cb, object)
2852
+ when Proc
2853
+ cb.call(self, object)
2854
+ else
2855
+ raise Error, "callbacks should either be Procs or Symbols"
2856
+ end
2857
+ end
2858
+ rescue HookFailed
2859
+ # The reason we automatically set raise_error for singular associations is that
2860
+ # assignment in ruby always returns the argument instead of the result of the
2861
+ # method, so we can't return nil to signal that the association callback prevented
2862
+ # the modification
2863
+ return false unless raise_on_save_failure || !reflection.returns_array?
2864
+ raise
2865
+ end
2866
+ end
2867
+
2868
+ # Set the given object as the associated object for the given *_to_one association reflection
2869
+ def _set_associated_object(opts, o)
2870
+ a = associations[opts[:name]]
2871
+ reciprocal = opts.reciprocal
2872
+ if set_associated_object_if_same?
2873
+ if reciprocal
2874
+ remove_reciprocal = a && (a != o || a.associations[reciprocal] != self)
2875
+ add_reciprocal = o && o.associations[reciprocal] != self
2876
+ end
2877
+ else
2878
+ return if a && a == o
2879
+ if reciprocal
2880
+ remove_reciprocal = a
2881
+ add_reciprocal = o
2882
+ end
2883
+ end
2884
+ run_association_callbacks(opts, :before_set, o)
2885
+ remove_reciprocal_object(opts, a) if remove_reciprocal
2886
+ # Allow calling private _setter method
2887
+ send(opts[:_setter_method], o)
2888
+ associations[opts[:name]] = o
2889
+ add_reciprocal_object(opts, o) if add_reciprocal
2890
+ run_association_callbacks(opts, :after_set, o)
2891
+ o
2892
+ end
2893
+
2894
+ # Whether run the associated object setter code if passed the same object as the one already
2895
+ # cached in the association. Usually not set (so nil), can be set on a per-object basis
2896
+ # if necessary.
2897
+ def set_associated_object_if_same?
2898
+ @set_associated_object_if_same
2899
+ end
2900
+
2901
+ # Set the given object as the associated object for the given many_to_one association reflection
2902
+ def set_associated_object(opts, o)
2903
+ raise(Error, "associated object #{o.inspect} does not have a primary key") if o && !o.pk
2904
+ _set_associated_object(opts, o)
2905
+ end
2906
+
2907
+ # Set the given object as the associated object for the given one_through_one association reflection
2908
+ def set_one_through_one_associated_object(opts, o)
2909
+ raise(Error, "object #{inspect} does not have a primary key") unless pk
2910
+ raise(Error, "associated object #{o.inspect} does not have a primary key") if o && !o.pk
2911
+ _set_associated_object(opts, o)
2912
+ end
2913
+
2914
+ # Set the given object as the associated object for the given one_to_one association reflection
2915
+ def set_one_to_one_associated_object(opts, o)
2916
+ raise(Error, "object #{inspect} does not have a primary key") unless pk
2917
+ _set_associated_object(opts, o)
2918
+ end
2919
+ end
2920
+
2921
+ # Eager loading makes it so that you can load all associated records for a
2922
+ # set of objects in a single query, instead of a separate query for each object.
2923
+ #
2924
+ # Two separate implementations are provided. +eager+ should be used most of the
2925
+ # time, as it loads associated records using one query per association. However,
2926
+ # it does not allow you the ability to filter or order based on columns in associated tables. +eager_graph+ loads
2927
+ # all records in a single query using JOINs, allowing you to filter or order based on columns in associated
2928
+ # tables. However, +eager_graph+ is usually slower than +eager+, especially if multiple
2929
+ # one_to_many or many_to_many associations are joined.
2930
+ #
2931
+ # You can cascade the eager loading (loading associations on associated objects)
2932
+ # with no limit to the depth of the cascades. You do this by passing a hash to +eager+ or +eager_graph+
2933
+ # with the keys being associations of the current model and values being
2934
+ # associations of the model associated with the current model via the key.
2935
+ #
2936
+ # The arguments can be symbols or hashes with symbol keys (for cascaded
2937
+ # eager loading). Examples:
2938
+ #
2939
+ # Album.eager(:artist).all
2940
+ # Album.eager_graph(:artist).all
2941
+ # Album.eager(:artist, :genre).all
2942
+ # Album.eager_graph(:artist, :genre).all
2943
+ # Album.eager(:artist).eager(:genre).all
2944
+ # Album.eager_graph(:artist).eager_graph(:genre).all
2945
+ # Artist.eager(albums: :tracks).all
2946
+ # Artist.eager_graph(albums: :tracks).all
2947
+ # Artist.eager(albums: {tracks: :genre}).all
2948
+ # Artist.eager_graph(albums: {tracks: :genre}).all
2949
+ #
2950
+ # You can also pass a callback as a hash value in order to customize the dataset being
2951
+ # eager loaded at query time, analogous to the way the :eager_block association option
2952
+ # allows you to customize it at association definition time. For example,
2953
+ # if you wanted artists with their albums since 1990:
2954
+ #
2955
+ # Artist.eager(albums: proc{|ds| ds.where{year > 1990}})
2956
+ #
2957
+ # Or if you needed albums and their artist's name only, using a single query:
2958
+ #
2959
+ # Albums.eager_graph(artist: proc{|ds| ds.select(:name)})
2960
+ #
2961
+ # To cascade eager loading while using a callback, you substitute the cascaded
2962
+ # associations with a single entry hash that has the proc callback as the key and
2963
+ # the cascaded associations as the value. This will load artists with their albums
2964
+ # since 1990, and also the tracks on those albums and the genre for those tracks:
2965
+ #
2966
+ # Artist.eager(albums: {proc{|ds| ds.where{year > 1990}}=>{tracks: :genre}})
2967
+ module DatasetMethods
2968
+ %w'inner left right full'.each do |type|
2969
+ class_eval(<<-END, __FILE__, __LINE__+1)
2970
+ def association_#{type}_join(*associations)
2971
+ _association_join(:#{type}, associations)
2972
+ end
2973
+ END
2974
+ end
2975
+
2976
+ # Adds one or more INNER JOINs to the existing dataset using the keys and conditions
2977
+ # specified by the given association(s). Take the same arguments as eager_graph, and
2978
+ # operates similarly, but only adds the joins as opposed to making the other changes
2979
+ # (such as adding selected columns and setting up eager loading).
2980
+ #
2981
+ # The following methods also exist for specifying a different type of JOIN:
2982
+ #
2983
+ # association_full_join :: FULL JOIN
2984
+ # association_inner_join :: INNER JOIN
2985
+ # association_left_join :: LEFT JOIN
2986
+ # association_right_join :: RIGHT JOIN
2987
+ #
2988
+ # Examples:
2989
+ #
2990
+ # # For each album, association_join load the artist
2991
+ # Album.association_join(:artist).all
2992
+ # # SELECT *
2993
+ # # FROM albums
2994
+ # # INNER JOIN artists AS artist ON (artists.id = albums.artist_id)
2995
+ #
2996
+ # # For each album, association_join load the artist, using a specified alias
2997
+ # Album.association_join(Sequel[:artist].as(:a)).all
2998
+ # # SELECT *
2999
+ # # FROM albums
3000
+ # # INNER JOIN artists AS a ON (a.id = albums.artist_id)
3001
+ #
3002
+ # # For each album, association_join load the artist and genre
3003
+ # Album.association_join(:artist, :genre).all
3004
+ # Album.association_join(:artist).association_join(:genre).all
3005
+ # # SELECT *
3006
+ # # FROM albums
3007
+ # # INNER JOIN artists AS artist ON (artist.id = albums.artist_id)
3008
+ # # INNER JOIN genres AS genre ON (genre.id = albums.genre_id)
3009
+ #
3010
+ # # For each artist, association_join load albums and tracks for each album
3011
+ # Artist.association_join(albums: :tracks).all
3012
+ # # SELECT *
3013
+ # # FROM artists
3014
+ # # INNER JOIN albums ON (albums.artist_id = artists.id)
3015
+ # # INNER JOIN tracks ON (tracks.album_id = albums.id)
3016
+ #
3017
+ # # For each artist, association_join load albums, tracks for each album, and genre for each track
3018
+ # Artist.association_join(albums: {tracks: :genre}).all
3019
+ # # SELECT *
3020
+ # # FROM artists
3021
+ # # INNER JOIN albums ON (albums.artist_id = artists.id)
3022
+ # # INNER JOIN tracks ON (tracks.album_id = albums.id)
3023
+ # # INNER JOIN genres AS genre ON (genre.id = tracks.genre_id)
3024
+ #
3025
+ # # For each artist, association_join load albums with year > 1990
3026
+ # Artist.association_join(albums: proc{|ds| ds.where{year > 1990}}).all
3027
+ # # SELECT *
3028
+ # # FROM artists
3029
+ # # INNER JOIN (
3030
+ # # SELECT * FROM albums WHERE (year > 1990)
3031
+ # # ) AS albums ON (albums.artist_id = artists.id)
3032
+ #
3033
+ # # For each artist, association_join load albums and tracks 1-10 for each album
3034
+ # Artist.association_join(albums: {tracks: proc{|ds| ds.where(number: 1..10)}}).all
3035
+ # # SELECT *
3036
+ # # FROM artists
3037
+ # # INNER JOIN albums ON (albums.artist_id = artists.id)
3038
+ # # INNER JOIN (
3039
+ # # SELECT * FROM tracks WHERE ((number >= 1) AND (number <= 10))
3040
+ # # ) AS tracks ON (tracks.albums_id = albums.id)
3041
+ #
3042
+ # # For each artist, association_join load albums with year > 1990, and tracks for those albums
3043
+ # Artist.association_join(albums: {proc{|ds| ds.where{year > 1990}}=>:tracks}).all
3044
+ # # SELECT *
3045
+ # # FROM artists
3046
+ # # INNER JOIN (
3047
+ # # SELECT * FROM albums WHERE (year > 1990)
3048
+ # # ) AS albums ON (albums.artist_id = artists.id)
3049
+ # # INNER JOIN tracks ON (tracks.album_id = albums.id)
3050
+ def association_join(*associations)
3051
+ association_inner_join(*associations)
3052
+ end
3053
+
3054
+ # If the expression is in the form <tt>x = y</tt> where +y+ is a <tt>Sequel::Model</tt>
3055
+ # instance, array of <tt>Sequel::Model</tt> instances, or a <tt>Sequel::Model</tt> dataset,
3056
+ # assume +x+ is an association symbol and look up the association reflection
3057
+ # via the dataset's model. From there, return the appropriate SQL based on the type of
3058
+ # association and the values of the foreign/primary keys of +y+. For most association
3059
+ # types, this is a simple transformation, but for +many_to_many+ associations this
3060
+ # creates a subquery to the join table.
3061
+ def complex_expression_sql_append(sql, op, args)
3062
+ r = args[1]
3063
+ if (((op == :'=' || op == :'!=') && r.is_a?(Sequel::Model)) ||
3064
+ (multiple = ((op == :IN || op == :'NOT IN') && ((is_ds = r.is_a?(Sequel::Dataset)) || (r.respond_to?(:all?) && r.all?{|x| x.is_a?(Sequel::Model)})))))
3065
+ l = args[0]
3066
+ if ar = model.association_reflection(l)
3067
+ raise Error, "filtering by associations is not allowed for #{ar.inspect}" if ar[:allow_filtering_by] == false
3068
+
3069
+ if multiple
3070
+ klass = ar.associated_class
3071
+ if is_ds
3072
+ if r.respond_to?(:model)
3073
+ unless r.model <= klass
3074
+ # A dataset for a different model class, could be a valid regular query
3075
+ return super
3076
+ end
3077
+ else
3078
+ # Not a model dataset, could be a valid regular query
3079
+ return super
3080
+ end
3081
+ else
3082
+ unless r.all?{|x| x.is_a?(klass)}
3083
+ raise Sequel::Error, "invalid association class for one object for association #{l.inspect} used in dataset filter for model #{model.inspect}, expected class #{klass.inspect}"
3084
+ end
3085
+ end
3086
+ elsif !r.is_a?(ar.associated_class)
3087
+ raise Sequel::Error, "invalid association class #{r.class.inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}, expected class #{ar.associated_class.inspect}"
3088
+ end
3089
+
3090
+ if exp = association_filter_expression(op, ar, r)
3091
+ literal_append(sql, exp)
3092
+ else
3093
+ raise Sequel::Error, "invalid association type #{ar[:type].inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}"
3094
+ end
3095
+ elsif multiple && (is_ds || r.empty?)
3096
+ # Not a query designed for this support, could be a valid regular query
3097
+ super
3098
+ else
3099
+ raise Sequel::Error, "invalid association #{l.inspect} used in dataset filter for model #{model.inspect}"
3100
+ end
3101
+ else
3102
+ super
3103
+ end
3104
+ end
3105
+
3106
+ # The preferred eager loading method. Loads all associated records using one
3107
+ # query for each association.
3108
+ #
3109
+ # The basic idea for how it works is that the dataset is first loaded normally.
3110
+ # Then it goes through all associations that have been specified via +eager+.
3111
+ # It loads each of those associations separately, then associates them back
3112
+ # to the original dataset via primary/foreign keys. Due to the necessity of
3113
+ # all objects being present, you need to use +all+ to use eager loading, as it
3114
+ # can't work with +each+.
3115
+ #
3116
+ # This implementation avoids the complexity of extracting an object graph out
3117
+ # of a single dataset, by building the object graph out of multiple datasets,
3118
+ # one for each association. By using a separate dataset for each association,
3119
+ # it avoids problems such as aliasing conflicts and creating cartesian product
3120
+ # result sets if multiple one_to_many or many_to_many eager associations are requested.
3121
+ #
3122
+ # One limitation of using this method is that you cannot filter the current dataset
3123
+ # based on values of columns in an associated table, since the associations are loaded
3124
+ # in separate queries. To do that you need to load all associations in the
3125
+ # same query, and extract an object graph from the results of that query. If you
3126
+ # need to filter based on columns in associated tables, look at +eager_graph+
3127
+ # or join the tables you need to filter on manually.
3128
+ #
3129
+ # Each association's order, if defined, is respected.
3130
+ # If the association uses a block or has an :eager_block argument, it is used.
3131
+ #
3132
+ # To modify the associated dataset that will be used for the eager load, you should use a
3133
+ # hash for the association, with the key being the association name symbol, and the value being
3134
+ # a callable object that is called with the associated dataset and should return a modified
3135
+ # dataset. If that association also has dependent associations, instead of a callable object,
3136
+ # use a hash with the callable object being the key, and the dependent association(s) as the value.
3137
+ #
3138
+ # Examples:
3139
+ #
3140
+ # # For each album, eager load the artist
3141
+ # Album.eager(:artist).all
3142
+ # # SELECT * FROM albums
3143
+ # # SELECT * FROM artists WHERE (id IN (...))
3144
+ #
3145
+ # # For each album, eager load the artist and genre
3146
+ # Album.eager(:artist, :genre).all
3147
+ # Album.eager(:artist).eager(:genre).all
3148
+ # # SELECT * FROM albums
3149
+ # # SELECT * FROM artists WHERE (id IN (...))
3150
+ # # SELECT * FROM genres WHERE (id IN (...))
3151
+ #
3152
+ # # For each artist, eager load albums and tracks for each album
3153
+ # Artist.eager(albums: :tracks).all
3154
+ # # SELECT * FROM artists
3155
+ # # SELECT * FROM albums WHERE (artist_id IN (...))
3156
+ # # SELECT * FROM tracks WHERE (album_id IN (...))
3157
+ #
3158
+ # # For each artist, eager load albums, tracks for each album, and genre for each track
3159
+ # Artist.eager(albums: {tracks: :genre}).all
3160
+ # # SELECT * FROM artists
3161
+ # # SELECT * FROM albums WHERE (artist_id IN (...))
3162
+ # # SELECT * FROM tracks WHERE (album_id IN (...))
3163
+ # # SELECT * FROM genre WHERE (id IN (...))
3164
+ #
3165
+ # # For each artist, eager load albums with year > 1990
3166
+ # Artist.eager(albums: proc{|ds| ds.where{year > 1990}}).all
3167
+ # # SELECT * FROM artists
3168
+ # # SELECT * FROM albums WHERE ((year > 1990) AND (artist_id IN (...)))
3169
+ #
3170
+ # # For each artist, eager load albums and tracks 1-10 for each album
3171
+ # Artist.eager(albums: {tracks: proc{|ds| ds.where(number: 1..10)}}).all
3172
+ # # SELECT * FROM artists
3173
+ # # SELECT * FROM albums WHERE (artist_id IN (...))
3174
+ # # SELECT * FROM tracks WHERE ((number >= 1) AND (number <= 10) AND (album_id IN (...)))
3175
+ #
3176
+ # # For each artist, eager load albums with year > 1990, and tracks for those albums
3177
+ # Artist.eager(albums: {proc{|ds| ds.where{year > 1990}}=>:tracks}).all
3178
+ # # SELECT * FROM artists
3179
+ # # SELECT * FROM albums WHERE ((year > 1990) AND (artist_id IN (...)))
3180
+ # # SELECT * FROM albums WHERE (artist_id IN (...))
3181
+ def eager(*associations)
3182
+ opts = @opts[:eager]
3183
+ association_opts = eager_options_for_associations(associations)
3184
+ opts = opts ? opts.merge(association_opts) : association_opts
3185
+ clone(:eager=>opts.freeze)
3186
+ end
3187
+
3188
+ # The secondary eager loading method. Loads all associations in a single query. This
3189
+ # method should only be used if you need to filter or order based on columns in associated tables,
3190
+ # or if you have done comparative benchmarking and determined it is faster.
3191
+ #
3192
+ # This method uses <tt>Dataset#graph</tt> to create appropriate aliases for columns in all the
3193
+ # tables. Then it uses the graph's metadata to build the associations from the single hash, and
3194
+ # finally replaces the array of hashes with an array model objects inside all.
3195
+ #
3196
+ # Be very careful when using this with multiple one_to_many or many_to_many associations, as you can
3197
+ # create large cartesian products. If you must graph multiple one_to_many and many_to_many associations,
3198
+ # make sure your filters are narrow if the datasets are large.
3199
+ #
3200
+ # Each association's order, if defined, is respected. +eager_graph+ probably
3201
+ # won't work correctly on a limited dataset, unless you are
3202
+ # only graphing many_to_one, one_to_one, and one_through_one associations.
3203
+ #
3204
+ # Does not use the block defined for the association, since it does a single query for
3205
+ # all objects. You can use the :graph_* association options to modify the SQL query.
3206
+ #
3207
+ # Like +eager+, you need to call +all+ on the dataset for the eager loading to work. If you just
3208
+ # call +each+, it will yield plain hashes, each containing all columns from all the tables.
3209
+ #
3210
+ # To modify the associated dataset that will be joined to the current dataset, you should use a
3211
+ # hash for the association, with the key being the association name symbol, and the value being
3212
+ # a callable object that is called with the associated dataset and should return a modified
3213
+ # dataset. If that association also has dependent associations, instead of a callable object,
3214
+ # use a hash with the callable object being the key, and the dependent association(s) as the value.
3215
+ #
3216
+ # You can specify an custom alias and/or join type on a per-association basis by providing an
3217
+ # Sequel::SQL::AliasedExpression object instead of an a Symbol for the association name.
3218
+ #
3219
+ # You cannot mix calls to +eager_graph+ and +graph+ on the same dataset.
3220
+ #
3221
+ # Examples:
3222
+ #
3223
+ # # For each album, eager_graph load the artist
3224
+ # Album.eager_graph(:artist).all
3225
+ # # SELECT ...
3226
+ # # FROM albums
3227
+ # # LEFT OUTER JOIN artists AS artist ON (artists.id = albums.artist_id)
3228
+ #
3229
+ # # For each album, eager_graph load the artist, using a specified alias
3230
+ # Album.eager_graph(Sequel[:artist].as(:a)).all
3231
+ # # SELECT ...
3232
+ # # FROM albums
3233
+ # # LEFT OUTER JOIN artists AS a ON (a.id = albums.artist_id)
3234
+ #
3235
+ # # For each album, eager_graph load the artist, using a specified alias
3236
+ # # and custom join type
3237
+ #
3238
+ # Album.eager_graph(Sequel[:artist].as(:a, join_type: :inner)).all
3239
+ # # SELECT ...
3240
+ # # FROM albums
3241
+ # # INNER JOIN artists AS a ON (a.id = albums.artist_id)
3242
+ #
3243
+ # # For each album, eager_graph load the artist and genre
3244
+ # Album.eager_graph(:artist, :genre).all
3245
+ # Album.eager_graph(:artist).eager_graph(:genre).all
3246
+ # # SELECT ...
3247
+ # # FROM albums
3248
+ # # LEFT OUTER JOIN artists AS artist ON (artist.id = albums.artist_id)
3249
+ # # LEFT OUTER JOIN genres AS genre ON (genre.id = albums.genre_id)
3250
+ #
3251
+ # # For each artist, eager_graph load albums and tracks for each album
3252
+ # Artist.eager_graph(albums: :tracks).all
3253
+ # # SELECT ...
3254
+ # # FROM artists
3255
+ # # LEFT OUTER JOIN albums ON (albums.artist_id = artists.id)
3256
+ # # LEFT OUTER JOIN tracks ON (tracks.album_id = albums.id)
3257
+ #
3258
+ # # For each artist, eager_graph load albums, tracks for each album, and genre for each track
3259
+ # Artist.eager_graph(albums: {tracks: :genre}).all
3260
+ # # SELECT ...
3261
+ # # FROM artists
3262
+ # # LEFT OUTER JOIN albums ON (albums.artist_id = artists.id)
3263
+ # # LEFT OUTER JOIN tracks ON (tracks.album_id = albums.id)
3264
+ # # LEFT OUTER JOIN genres AS genre ON (genre.id = tracks.genre_id)
3265
+ #
3266
+ # # For each artist, eager_graph load albums with year > 1990
3267
+ # Artist.eager_graph(albums: proc{|ds| ds.where{year > 1990}}).all
3268
+ # # SELECT ...
3269
+ # # FROM artists
3270
+ # # LEFT OUTER JOIN (
3271
+ # # SELECT * FROM albums WHERE (year > 1990)
3272
+ # # ) AS albums ON (albums.artist_id = artists.id)
3273
+ #
3274
+ # # For each artist, eager_graph load albums and tracks 1-10 for each album
3275
+ # Artist.eager_graph(albums: {tracks: proc{|ds| ds.where(number: 1..10)}}).all
3276
+ # # SELECT ...
3277
+ # # FROM artists
3278
+ # # LEFT OUTER JOIN albums ON (albums.artist_id = artists.id)
3279
+ # # LEFT OUTER JOIN (
3280
+ # # SELECT * FROM tracks WHERE ((number >= 1) AND (number <= 10))
3281
+ # # ) AS tracks ON (tracks.albums_id = albums.id)
3282
+ #
3283
+ # # For each artist, eager_graph load albums with year > 1990, and tracks for those albums
3284
+ # Artist.eager_graph(albums: {proc{|ds| ds.where{year > 1990}}=>:tracks}).all
3285
+ # # SELECT ...
3286
+ # # FROM artists
3287
+ # # LEFT OUTER JOIN (
3288
+ # # SELECT * FROM albums WHERE (year > 1990)
3289
+ # # ) AS albums ON (albums.artist_id = artists.id)
3290
+ # # LEFT OUTER JOIN tracks ON (tracks.album_id = albums.id)
3291
+ def eager_graph(*associations)
3292
+ eager_graph_with_options(associations)
3293
+ end
3294
+
3295
+ # Run eager_graph with some options specific to just this call. Unlike eager_graph, this takes
3296
+ # the associations as a single argument instead of multiple arguments.
3297
+ #
3298
+ # Options:
3299
+ #
3300
+ # :join_type :: Override the join type specified in the association
3301
+ # :limit_strategy :: Use a strategy for handling limits on associations.
3302
+ # Appropriate :limit_strategy values are:
3303
+ # true :: Pick the most appropriate based on what the database supports
3304
+ # :distinct_on :: Force use of DISTINCT ON stategy (*_one associations only)
3305
+ # :correlated_subquery :: Force use of correlated subquery strategy (one_to_* associations only)
3306
+ # :window_function :: Force use of window function strategy
3307
+ # :ruby :: Don't modify the SQL, implement limits/offsets with array slicing
3308
+ #
3309
+ # This can also be a hash with association name symbol keys and one of the above values,
3310
+ # to use different strategies per association.
3311
+ #
3312
+ # The default is the :ruby strategy. Choosing a different strategy can make your code
3313
+ # significantly slower in some cases (perhaps even the majority of cases), so you should
3314
+ # only use this if you have benchmarked that it is faster for your use cases.
3315
+ def eager_graph_with_options(associations, opts=OPTS)
3316
+ return self if associations.empty?
3317
+
3318
+ opts = opts.dup unless opts.frozen?
3319
+ associations = [associations] unless associations.is_a?(Array)
3320
+ ds = if eg = @opts[:eager_graph]
3321
+ eg = eg.dup
3322
+ [:requirements, :reflections, :reciprocals, :limits].each{|k| eg[k] = eg[k].dup}
3323
+ eg[:local] = opts
3324
+ ds = clone(:eager_graph=>eg)
3325
+ ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations)
3326
+ else
3327
+ # Each of the following have a symbol key for the table alias, with the following values:
3328
+ # :reciprocals :: the reciprocal value to use for this association
3329
+ # :reflections :: AssociationReflection instance related to this association
3330
+ # :requirements :: array of requirements for this association
3331
+ # :limits :: Any limit/offset array slicing that need to be handled in ruby land after loading
3332
+ opts = {:requirements=>{}, :master=>alias_symbol(first_source), :reflections=>{}, :reciprocals=>{}, :limits=>{}, :local=>opts, :cartesian_product_number=>0, :row_proc=>row_proc}
3333
+ ds = clone(:eager_graph=>opts)
3334
+ ds = ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations).naked
3335
+ end
3336
+
3337
+ ds.opts[:eager_graph].freeze
3338
+ ds.opts[:eager_graph].each_value{|v| v.freeze if v.is_a?(Hash)}
3339
+ ds
3340
+ end
3341
+
3342
+ # If the dataset is being eagerly loaded, default to calling all
3343
+ # instead of each.
3344
+ def as_hash(key_column=nil, value_column=nil, opts=OPTS)
3345
+ if (@opts[:eager_graph] || @opts[:eager]) && !opts.has_key?(:all)
3346
+ opts = Hash[opts]
3347
+ opts[:all] = true
3348
+ end
3349
+ super
3350
+ end
3351
+
3352
+ # If the dataset is being eagerly loaded, default to calling all
3353
+ # instead of each.
3354
+ def to_hash_groups(key_column, value_column=nil, opts=OPTS)
3355
+ if (@opts[:eager_graph] || @opts[:eager]) && !opts.has_key?(:all)
3356
+ opts = Hash[opts]
3357
+ opts[:all] = true
3358
+ end
3359
+ super
3360
+ end
3361
+
3362
+ # Do not attempt to split the result set into associations,
3363
+ # just return results as simple objects. This is useful if you
3364
+ # want to use eager_graph as a shortcut to have all of the joins
3365
+ # and aliasing set up, but want to do something else with the dataset.
3366
+ def ungraphed
3367
+ ds = super.clone(:eager_graph=>nil)
3368
+ if (eg = @opts[:eager_graph]) && (rp = eg[:row_proc])
3369
+ ds = ds.with_row_proc(rp)
3370
+ end
3371
+ ds
3372
+ end
3373
+
3374
+ protected
3375
+
3376
+ # Call graph on the association with the correct arguments,
3377
+ # update the eager_graph data structure, and recurse into
3378
+ # eager_graph_associations if there are any passed in associations
3379
+ # (which would be dependencies of the current association)
3380
+ #
3381
+ # Arguments:
3382
+ # ds :: Current dataset
3383
+ # model :: Current Model
3384
+ # ta :: table_alias used for the parent association
3385
+ # requirements :: an array, used as a stack for requirements
3386
+ # r :: association reflection for the current association, or an SQL::AliasedExpression
3387
+ # with the reflection as the expression, the alias base as the alias (or nil to
3388
+ # use the default alias), and an optional hash with a :join_type entry as the columns
3389
+ # to use a custom join type.
3390
+ # *associations :: any associations dependent on this one
3391
+ def eager_graph_association(ds, model, ta, requirements, r, *associations)
3392
+ if r.is_a?(SQL::AliasedExpression)
3393
+ alias_base = r.alias
3394
+ if r.columns.is_a?(Hash)
3395
+ join_type = r.columns[:join_type]
3396
+ end
3397
+ r = r.expression
3398
+ else
3399
+ alias_base = r[:graph_alias_base]
3400
+ end
3401
+ assoc_table_alias = ds.unused_table_alias(alias_base)
3402
+ loader = r[:eager_grapher]
3403
+ if !associations.empty?
3404
+ if associations.first.respond_to?(:call)
3405
+ callback = associations.first
3406
+ associations = {}
3407
+ elsif associations.length == 1 && (assocs = associations.first).is_a?(Hash) && assocs.length == 1 && (pr_assoc = assocs.to_a.first) && pr_assoc.first.respond_to?(:call)
3408
+ callback, assoc = pr_assoc
3409
+ associations = assoc.is_a?(Array) ? assoc : [assoc]
3410
+ end
3411
+ end
3412
+ local_opts = ds.opts[:eager_graph][:local]
3413
+ limit_strategy = r.eager_graph_limit_strategy(local_opts[:limit_strategy])
3414
+
3415
+ # SEQUEL6: remove and integrate the auto_restrict_eager_graph plugin
3416
+ if !r[:orig_opts].has_key?(:graph_conditions) && !r[:orig_opts].has_key?(:graph_only_conditions) && !r.has_key?(:graph_block) && !r[:allow_eager_graph]
3417
+ if r[:conditions] && !Sequel.condition_specifier?(r[:conditions])
3418
+ raise Error, "Cannot eager_graph association when :conditions specified and not a hash or an array of pairs. Specify :graph_conditions, :graph_only_conditions, or :graph_block for the association. Model: #{r[:model]}, association: #{r[:name]}"
3419
+ end
3420
+
3421
+ if r[:block] && !r[:graph_use_association_block]
3422
+ warn "eager_graph used for association when association given a block without graph options. The block is ignored in this case. This will result in an exception starting in Sequel 6. Model: #{r[:model]}, association: #{r[:name]}"
3423
+ end
3424
+ end
3425
+
3426
+ ds = loader.call(:self=>ds, :table_alias=>assoc_table_alias, :implicit_qualifier=>(ta == ds.opts[:eager_graph][:master]) ? first_source : qualifier_from_alias_symbol(ta, first_source), :callback=>callback, :join_type=>join_type || local_opts[:join_type], :join_only=>local_opts[:join_only], :limit_strategy=>limit_strategy, :from_self_alias=>ds.opts[:eager_graph][:master])
3427
+ if r[:order_eager_graph] && (order = r.fetch(:graph_order, r[:order]))
3428
+ ds = ds.order_append(*qualified_expression(order, assoc_table_alias))
3429
+ end
3430
+ eager_graph = ds.opts[:eager_graph]
3431
+ eager_graph[:requirements][assoc_table_alias] = requirements.dup
3432
+ eager_graph[:reflections][assoc_table_alias] = r
3433
+ if limit_strategy == :ruby
3434
+ eager_graph[:limits][assoc_table_alias] = r.limit_and_offset
3435
+ end
3436
+ eager_graph[:cartesian_product_number] += r[:cartesian_product_number] || 2
3437
+ ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty?
3438
+ ds
3439
+ end
3440
+
3441
+ # Check the associations are valid for the given model.
3442
+ # Call eager_graph_association on each association.
3443
+ #
3444
+ # Arguments:
3445
+ # ds :: Current dataset
3446
+ # model :: Current Model
3447
+ # ta :: table_alias used for the parent association
3448
+ # requirements :: an array, used as a stack for requirements
3449
+ # *associations :: the associations to add to the graph
3450
+ def eager_graph_associations(ds, model, ta, requirements, *associations)
3451
+ associations.flatten.each do |association|
3452
+ ds = case association
3453
+ when Symbol, SQL::AliasedExpression
3454
+ ds.eager_graph_association(ds, model, ta, requirements, eager_graph_check_association(model, association))
3455
+ when Hash
3456
+ association.each do |assoc, assoc_assocs|
3457
+ ds = ds.eager_graph_association(ds, model, ta, requirements, eager_graph_check_association(model, assoc), assoc_assocs)
3458
+ end
3459
+ ds
3460
+ else
3461
+ raise(Sequel::Error, 'Associations must be in the form of a symbol or hash')
3462
+ end
3463
+ end
3464
+ ds
3465
+ end
3466
+
3467
+ # Replace the array of plain hashes with an array of model objects will all eager_graphed
3468
+ # associations set in the associations cache for each object.
3469
+ def eager_graph_build_associations(hashes)
3470
+ hashes.replace(_eager_graph_build_associations(hashes, eager_graph_loader))
3471
+ end
3472
+
3473
+ private
3474
+
3475
+ # Return a new dataset with JOINs of the given type added, using the tables and
3476
+ # conditions specified by the associations.
3477
+ def _association_join(type, associations)
3478
+ clone(:join=>clone(:graph_from_self=>false).eager_graph_with_options(associations, :join_type=>type, :join_only=>true).opts[:join])
3479
+ end
3480
+
3481
+ # Process the array of hashes using the eager graph loader to return an array
3482
+ # of model objects with the associations set.
3483
+ def _eager_graph_build_associations(hashes, egl)
3484
+ egl.load(hashes)
3485
+ end
3486
+
3487
+ # If the association has conditions itself, then it requires additional filters be
3488
+ # added to the current dataset to ensure that the passed in object would also be
3489
+ # included by the association's conditions.
3490
+ def add_association_filter_conditions(ref, obj, expr)
3491
+ if expr != SQL::Constants::FALSE && ref.filter_by_associations_add_conditions?
3492
+ Sequel[ref.filter_by_associations_conditions_expression(obj)]
3493
+ else
3494
+ expr
3495
+ end
3496
+ end
3497
+
3498
+ # Process the array of associations arguments (Symbols, Arrays, and Hashes),
3499
+ # and return a hash of options suitable for cascading.
3500
+ def eager_options_for_associations(associations)
3501
+ opts = {}
3502
+ associations.flatten.each do |association|
3503
+ case association
3504
+ when Symbol
3505
+ check_association(model, association)
3506
+ opts[association] = nil
3507
+ when Hash
3508
+ association.keys.each{|assoc| check_association(model, assoc)}
3509
+ opts.merge!(association)
3510
+ else
3511
+ raise(Sequel::Error, 'Associations must be in the form of a symbol or hash')
3512
+ end
3513
+ end
3514
+ opts
3515
+ end
3516
+
3517
+ # Return an expression for filtering by the given association reflection and associated object.
3518
+ def association_filter_expression(op, ref, obj)
3519
+ meth = :"#{ref[:type]}_association_filter_expression"
3520
+ # Allow calling private association specific method to get filter expression
3521
+ send(meth, op, ref, obj) if respond_to?(meth, true)
3522
+ end
3523
+
3524
+ # Handle inversion for association filters by returning an inverted expression,
3525
+ # plus also handling cases where the referenced columns are NULL.
3526
+ def association_filter_handle_inversion(op, exp, cols)
3527
+ if op == :'!=' || op == :'NOT IN'
3528
+ if exp == SQL::Constants::FALSE
3529
+ ~exp
3530
+ else
3531
+ ~exp | Sequel::SQL::BooleanExpression.from_value_pairs(cols.zip([]), :OR)
3532
+ end
3533
+ else
3534
+ exp
3535
+ end
3536
+ end
3537
+
3538
+ # Return an expression for making sure that the given keys match the value of
3539
+ # the given methods for either the single object given or for any of the objects
3540
+ # given if +obj+ is an array.
3541
+ def association_filter_key_expression(keys, meths, obj)
3542
+ vals = if obj.is_a?(Sequel::Dataset)
3543
+ {(keys.length == 1 ? keys.first : keys)=>obj.select(*meths).exclude(Sequel::SQL::BooleanExpression.from_value_pairs(meths.zip([]), :OR))}
3544
+ else
3545
+ vals = Array(obj).reject{|o| !meths.all?{|m| o.get_column_value(m)}}
3546
+ return SQL::Constants::FALSE if vals.empty?
3547
+ if obj.is_a?(Array) || obj.is_a?(Set)
3548
+ if keys.length == 1
3549
+ meth = meths.first
3550
+ {keys.first=>vals.map{|o| o.get_column_value(meth)}}
3551
+ else
3552
+ {keys=>vals.map{|o| meths.map{|m| o.get_column_value(m)}}}
3553
+ end
3554
+ else
3555
+ keys.zip(meths.map{|k| obj.get_column_value(k)})
3556
+ end
3557
+ end
3558
+ SQL::BooleanExpression.from_value_pairs(vals)
3559
+ end
3560
+
3561
+ # Make sure the association is valid for this model, and return the related AssociationReflection.
3562
+ def check_association(model, association)
3563
+ raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
3564
+ raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
3565
+ reflection
3566
+ end
3567
+
3568
+ # Allow associations that are eagerly graphed to be specified as an SQL::AliasedExpression, for
3569
+ # per-call determining of the alias base.
3570
+ def eager_graph_check_association(model, association)
3571
+ reflection = if association.is_a?(SQL::AliasedExpression)
3572
+ expr = association.expression
3573
+ if expr.is_a?(SQL::Identifier)
3574
+ expr = expr.value
3575
+ if expr.is_a?(String)
3576
+ expr = expr.to_sym
3577
+ end
3578
+ end
3579
+
3580
+ check_reflection = check_association(model, expr)
3581
+ SQL::AliasedExpression.new(check_reflection, association.alias || expr, association.columns)
3582
+ else
3583
+ check_reflection = check_association(model, association)
3584
+ end
3585
+
3586
+ if check_reflection && check_reflection[:allow_eager_graph] == false
3587
+ raise Error, "eager_graph not allowed for #{reflection.inspect}"
3588
+ end
3589
+
3590
+ reflection
3591
+ end
3592
+
3593
+ # The EagerGraphLoader instance used for converting eager_graph results.
3594
+ def eager_graph_loader
3595
+ unless egl = cache_get(:_model_eager_graph_loader)
3596
+ egl = cache_set(:_model_eager_graph_loader, EagerGraphLoader.new(self))
3597
+ end
3598
+ egl.dup
3599
+ end
3600
+
3601
+ # Eagerly load all specified associations.
3602
+ def eager_load(a, eager_assoc=@opts[:eager], m=model)
3603
+ return if a.empty?
3604
+
3605
+ # Reflections for all associations to eager load
3606
+ reflections = eager_assoc.keys.map{|assoc| m.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
3607
+
3608
+ perform_eager_loads(prepare_eager_load(a, reflections, eager_assoc))
3609
+
3610
+ reflections.each do |r|
3611
+ a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} if r[:after_load]
3612
+ end
3613
+
3614
+ nil
3615
+ end
3616
+
3617
+ # Prepare a hash loaders and eager options which will be used to implement the eager loading.
3618
+ def prepare_eager_load(a, reflections, eager_assoc)
3619
+ eager_load_data = {}.compare_by_identity
3620
+
3621
+ # Key is foreign/primary key name symbol.
3622
+ # Value is hash with keys being foreign/primary key values (generally integers)
3623
+ # and values being an array of current model objects with that specific foreign/primary key
3624
+ key_hash = {}
3625
+
3626
+ # Populate the key_hash entry for each association being eagerly loaded
3627
+ reflections.each do |r|
3628
+ if key = r.eager_loader_key
3629
+ # key_hash for this key has already been populated,
3630
+ # skip populating again so that duplicate values
3631
+ # aren't added.
3632
+ unless id_map = key_hash[key]
3633
+ id_map = key_hash[key] = Hash.new{|h,k| h[k] = []}
3634
+
3635
+ # Supporting both single (Symbol) and composite (Array) keys.
3636
+ a.each do |rec|
3637
+ case key
3638
+ when Array
3639
+ if (k = key.map{|k2| rec.get_column_value(k2)}) && k.all?
3640
+ id_map[k] << rec
3641
+ end
3642
+ when Symbol
3643
+ if k = rec.get_column_value(key)
3644
+ id_map[k] << rec
3645
+ end
3646
+ else
3647
+ raise Error, "unhandled eager_loader_key #{key.inspect} for association #{r[:name]}"
3648
+ end
3649
+ end
3650
+ end
3651
+ else
3652
+ id_map = nil
3653
+ end
3654
+
3655
+ associations = eager_assoc[r[:name]]
3656
+ if associations.respond_to?(:call)
3657
+ eager_block = associations
3658
+ associations = OPTS
3659
+ elsif associations.is_a?(Hash) && associations.length == 1 && (pr_assoc = associations.to_a.first) && pr_assoc.first.respond_to?(:call)
3660
+ eager_block, associations = pr_assoc
3661
+ end
3662
+
3663
+ eager_load_data[r[:eager_loader]] = {:key_hash=>key_hash, :rows=>a, :associations=>associations, :self=>self, :eager_block=>eager_block, :id_map=>id_map}
3664
+ end
3665
+
3666
+ eager_load_data
3667
+ end
3668
+
3669
+ # Using the hash of loaders and eager options, perform the eager loading.
3670
+ def perform_eager_loads(eager_load_data)
3671
+ eager_load_data.map do |loader, eo|
3672
+ perform_eager_load(loader, eo)
3673
+ end
3674
+ end
3675
+
3676
+ # Perform eager loading for a single association using the loader and eager options.
3677
+ def perform_eager_load(loader, eo)
3678
+ loader.call(eo)
3679
+ end
3680
+
3681
+ # Return a subquery expression for filering by a many_to_many association
3682
+ def many_to_many_association_filter_expression(op, ref, obj)
3683
+ lpks, lks, rks = ref.values_at(:left_primary_key_columns, :left_keys, :right_keys)
3684
+ jt = ref.join_table_alias
3685
+ lpks = lpks.first if lpks.length == 1
3686
+ lpks = ref.qualify(model.table_name, lpks)
3687
+
3688
+ meths = if obj.is_a?(Sequel::Dataset)
3689
+ ref.qualify(obj.model.table_name, ref.right_primary_keys)
3690
+ else
3691
+ ref.right_primary_key_methods
3692
+ end
3693
+
3694
+ expr = association_filter_key_expression(ref.qualify(jt, rks), meths, obj)
3695
+ unless expr == SQL::Constants::FALSE
3696
+ expr = SQL::BooleanExpression.from_value_pairs(lpks=>model.db.from(ref[:join_table]).select(*ref.qualify(jt, lks)).where(expr).exclude(SQL::BooleanExpression.from_value_pairs(ref.qualify(jt, lks).zip([]), :OR)))
3697
+ expr = add_association_filter_conditions(ref, obj, expr)
3698
+ end
3699
+
3700
+ association_filter_handle_inversion(op, expr, Array(lpks))
3701
+ end
3702
+ alias one_through_one_association_filter_expression many_to_many_association_filter_expression
3703
+
3704
+ # Return a simple equality expression for filering by a many_to_one association
3705
+ def many_to_one_association_filter_expression(op, ref, obj)
3706
+ keys = ref.qualify(model.table_name, ref[:key_columns])
3707
+ meths = if obj.is_a?(Sequel::Dataset)
3708
+ ref.qualify(obj.model.table_name, ref.primary_keys)
3709
+ else
3710
+ ref.primary_key_methods
3711
+ end
3712
+
3713
+ expr = association_filter_key_expression(keys, meths, obj)
3714
+ expr = add_association_filter_conditions(ref, obj, expr)
3715
+ association_filter_handle_inversion(op, expr, keys)
3716
+ end
3717
+
3718
+ # Return a simple equality expression for filering by a one_to_* association
3719
+ def one_to_many_association_filter_expression(op, ref, obj)
3720
+ keys = ref.qualify(model.table_name, ref[:primary_key_columns])
3721
+ meths = if obj.is_a?(Sequel::Dataset)
3722
+ ref.qualify(obj.model.table_name, ref[:keys])
3723
+ else
3724
+ ref[:key_methods]
3725
+ end
3726
+
3727
+ expr = association_filter_key_expression(keys, meths, obj)
3728
+ expr = add_association_filter_conditions(ref, obj, expr)
3729
+ association_filter_handle_inversion(op, expr, keys)
3730
+ end
3731
+ alias one_to_one_association_filter_expression one_to_many_association_filter_expression
3732
+
3733
+ def non_sql_option?(key)
3734
+ super || key == :eager || key == :eager_graph
3735
+ end
3736
+
3737
+ # Build associations from the graph if #eager_graph was used,
3738
+ # and/or load other associations if #eager was used.
3739
+ def post_load(all_records)
3740
+ eager_graph_build_associations(all_records) if @opts[:eager_graph]
3741
+ eager_load(all_records) if @opts[:eager] && (row_proc || @opts[:eager_graph])
3742
+ super
3743
+ end
3744
+ end
3745
+
3746
+ # This class is the internal implementation of eager_graph. It is responsible for taking an array of plain
3747
+ # hashes and returning an array of model objects with all eager_graphed associations already set in the
3748
+ # association cache.
3749
+ class EagerGraphLoader
3750
+ # Hash with table alias symbol keys and after_load hook values
3751
+ attr_reader :after_load_map
3752
+
3753
+ # Hash with table alias symbol keys and association name values
3754
+ attr_reader :alias_map
3755
+
3756
+ # Hash with table alias symbol keys and subhash values mapping column_alias symbols to the
3757
+ # symbol of the real name of the column
3758
+ attr_reader :column_maps
3759
+
3760
+ # Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys.
3761
+ attr_reader :dependency_map
3762
+
3763
+ # Hash with table alias symbol keys and [limit, offset] values
3764
+ attr_reader :limit_map
3765
+
3766
+ # The table alias symbol for the primary model
3767
+ attr_reader :master
3768
+
3769
+ # Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for
3770
+ # composite key tables)
3771
+ attr_reader :primary_keys
3772
+
3773
+ # Hash with table alias symbol keys and reciprocal association symbol values,
3774
+ # used for setting reciprocals for one_to_many associations.
3775
+ attr_reader :reciprocal_map
3776
+
3777
+ # Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols)
3778
+ # to model instances. Used so that only a single model instance is created for each object.
3779
+ attr_reader :records_map
3780
+
3781
+ # Hash with table alias symbol keys and AssociationReflection values
3782
+ attr_reader :reflection_map
3783
+
3784
+ # Hash with table alias symbol keys and callable values used to create model instances
3785
+ attr_reader :row_procs
3786
+
3787
+ # Hash with table alias symbol keys and true/false values, where true means the
3788
+ # association represented by the table alias uses an array of values instead of
3789
+ # a single value (i.e. true => *_many, false => *_to_one).
3790
+ attr_reader :type_map
3791
+
3792
+ # Initialize all of the data structures used during loading.
3793
+ def initialize(dataset)
3794
+ opts = dataset.opts
3795
+ eager_graph = opts[:eager_graph]
3796
+ @master = eager_graph[:master]
3797
+ requirements = eager_graph[:requirements]
3798
+ reflection_map = @reflection_map = eager_graph[:reflections]
3799
+ reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
3800
+ limit_map = @limit_map = eager_graph[:limits]
3801
+ @unique = eager_graph[:cartesian_product_number] > 1
3802
+
3803
+ alias_map = @alias_map = {}
3804
+ type_map = @type_map = {}
3805
+ after_load_map = @after_load_map = {}
3806
+ reflection_map.each do |k, v|
3807
+ alias_map[k] = v[:name]
3808
+ after_load_map[k] = v[:after_load] if v[:after_load]
3809
+ type_map[k] = if v.returns_array?
3810
+ true
3811
+ elsif (limit_and_offset = limit_map[k]) && !limit_and_offset.last.nil?
3812
+ :offset
3813
+ end
3814
+ end
3815
+ after_load_map.freeze
3816
+ alias_map.freeze
3817
+ type_map.freeze
3818
+
3819
+ # Make dependency map hash out of requirements array for each association.
3820
+ # This builds a tree of dependencies that will be used for recursion
3821
+ # to ensure that all parts of the object graph are loaded into the
3822
+ # appropriate subordinate association.
3823
+ dependency_map = @dependency_map = {}
3824
+ # Sort the associations by requirements length, so that
3825
+ # requirements are added to the dependency hash before their
3826
+ # dependencies.
3827
+ requirements.sort_by{|a| a[1].length}.each do |ta, deps|
3828
+ if deps.empty?
3829
+ dependency_map[ta] = {}
3830
+ else
3831
+ deps = deps.dup
3832
+ hash = dependency_map[deps.shift]
3833
+ deps.each do |dep|
3834
+ hash = hash[dep]
3835
+ end
3836
+ hash[ta] = {}
3837
+ end
3838
+ end
3839
+ freezer = lambda do |h|
3840
+ h.freeze
3841
+ h.each_value(&freezer)
3842
+ end
3843
+ freezer.call(dependency_map)
3844
+
3845
+ datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
3846
+ column_aliases = opts[:graph][:column_aliases]
3847
+ primary_keys = {}
3848
+ column_maps = {}
3849
+ models = {}
3850
+ row_procs = {}
3851
+ datasets.each do |ta, ds|
3852
+ models[ta] = ds.model
3853
+ primary_keys[ta] = []
3854
+ column_maps[ta] = {}
3855
+ row_procs[ta] = ds.row_proc
3856
+ end
3857
+ column_aliases.each do |col_alias, tc|
3858
+ ta, column = tc
3859
+ column_maps[ta][col_alias] = column
3860
+ end
3861
+ column_maps.each do |ta, h|
3862
+ pk = models[ta].primary_key
3863
+ if pk.is_a?(Array)
3864
+ primary_keys[ta] = []
3865
+ h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
3866
+ else
3867
+ h.select{|ca, c| primary_keys[ta] = ca if pk == c}
3868
+ end
3869
+ end
3870
+ @column_maps = column_maps.freeze
3871
+ @primary_keys = primary_keys.freeze
3872
+ @row_procs = row_procs.freeze
3873
+
3874
+ # For performance, create two special maps for the master table,
3875
+ # so you can skip a hash lookup.
3876
+ @master_column_map = column_maps[master]
3877
+ @master_primary_keys = primary_keys[master]
3878
+
3879
+ # Add a special hash mapping table alias symbols to 5 element arrays that just
3880
+ # contain the data in other data structures for that table alias. This is
3881
+ # used for performance, to get all values in one hash lookup instead of
3882
+ # separate hash lookups for each data structure.
3883
+ ta_map = {}
3884
+ alias_map.each_key do |ta|
3885
+ ta_map[ta] = [row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]].freeze
3886
+ end
3887
+ @ta_map = ta_map.freeze
3888
+ freeze
3889
+ end
3890
+
3891
+ # Return an array of primary model instances with the associations cache prepopulated
3892
+ # for all model objects (both primary and associated).
3893
+ def load(hashes)
3894
+ # This mapping is used to make sure that duplicate entries in the
3895
+ # result set are mapped to a single record. For example, using a
3896
+ # single one_to_many association with 10 associated records,
3897
+ # the main object column values appear in the object graph 10 times.
3898
+ # We map by primary key, if available, or by the object's entire values,
3899
+ # if not. The mapping must be per table, so create sub maps for each table
3900
+ # alias.
3901
+ @records_map = records_map = {}
3902
+ alias_map.keys.each{|ta| records_map[ta] = {}}
3903
+
3904
+ master = master()
3905
+
3906
+ # Assign to local variables for speed increase
3907
+ rp = row_procs[master]
3908
+ rm = records_map[master] = {}
3909
+ dm = dependency_map
3910
+
3911
+ records_map.freeze
3912
+
3913
+ # This will hold the final record set that we will be replacing the object graph with.
3914
+ records = []
3915
+
3916
+ hashes.each do |h|
3917
+ unless key = master_pk(h)
3918
+ key = hkey(master_hfor(h))
3919
+ end
3920
+ unless primary_record = rm[key]
3921
+ primary_record = rm[key] = rp.call(master_hfor(h))
3922
+ # Only add it to the list of records to return if it is a new record
3923
+ records.push(primary_record)
3924
+ end
3925
+ # Build all associations for the current object and it's dependencies
3926
+ _load(dm, primary_record, h)
3927
+ end
3928
+
3929
+ # Remove duplicate records from all associations if this graph could possibly be a cartesian product
3930
+ # Run after_load procs if there are any
3931
+ post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
3932
+
3933
+ records_map.each_value(&:freeze)
3934
+ freeze
3935
+
3936
+ records
3937
+ end
3938
+
3939
+ private
3940
+
3941
+ # Recursive method that creates associated model objects and associates them to the current model object.
3942
+ def _load(dependency_map, current, h)
3943
+ dependency_map.each do |ta, deps|
3944
+ unless key = pk(ta, h)
3945
+ ta_h = hfor(ta, h)
3946
+ unless ta_h.values.any?
3947
+ assoc_name = alias_map[ta]
3948
+ unless (assoc = current.associations).has_key?(assoc_name)
3949
+ assoc[assoc_name] = type_map[ta] ? [] : nil
3950
+ end
3951
+ next
3952
+ end
3953
+ key = hkey(ta_h)
3954
+ end
3955
+ rp, assoc_name, tm, rcm = @ta_map[ta]
3956
+ rm = records_map[ta]
3957
+
3958
+ # Check type map for all dependencies, and use a unique
3959
+ # object if any are dependencies for multiple objects,
3960
+ # to prevent duplicate objects from showing up in the case
3961
+ # the normal duplicate removal code is not being used.
3962
+ if !@unique && !deps.empty? && deps.any?{|dep_key,_| @ta_map[dep_key][2]}
3963
+ key = [current.object_id, key]
3964
+ end
3965
+
3966
+ unless rec = rm[key]
3967
+ rec = rm[key] = rp.call(hfor(ta, h))
3968
+ end
3969
+
3970
+ if tm
3971
+ unless (assoc = current.associations).has_key?(assoc_name)
3972
+ assoc[assoc_name] = []
3973
+ end
3974
+ assoc[assoc_name].push(rec)
3975
+ rec.associations[rcm] = current if rcm
3976
+ else
3977
+ current.associations[assoc_name] ||= rec
3978
+ end
3979
+ # Recurse into dependencies of the current object
3980
+ _load(deps, rec, h) unless deps.empty?
3981
+ end
3982
+ end
3983
+
3984
+ # Return the subhash for the specific table alias +ta+ by parsing the values out of the main hash +h+
3985
+ def hfor(ta, h)
3986
+ out = {}
3987
+ @column_maps[ta].each{|ca, c| out[c] = h[ca]}
3988
+ out
3989
+ end
3990
+
3991
+ # Return a suitable hash key for any subhash +h+, which is an array of values by column order.
3992
+ # This is only used if the primary key cannot be used.
3993
+ def hkey(h)
3994
+ h.sort_by{|x| x[0]}
3995
+ end
3996
+
3997
+ # Return the subhash for the master table by parsing the values out of the main hash +h+
3998
+ def master_hfor(h)
3999
+ out = {}
4000
+ @master_column_map.each{|ca, c| out[c] = h[ca]}
4001
+ out
4002
+ end
4003
+
4004
+ # Return a primary key value for the master table by parsing it out of the main hash +h+.
4005
+ def master_pk(h)
4006
+ x = @master_primary_keys
4007
+ if x.is_a?(Array)
4008
+ unless x == []
4009
+ x = x.map{|ca| h[ca]}
4010
+ x if x.all?
4011
+ end
4012
+ else
4013
+ h[x]
4014
+ end
4015
+ end
4016
+
4017
+ # Return a primary key value for the given table alias by parsing it out of the main hash +h+.
4018
+ def pk(ta, h)
4019
+ x = primary_keys[ta]
4020
+ if x.is_a?(Array)
4021
+ unless x == []
4022
+ x = x.map{|ca| h[ca]}
4023
+ x if x.all?
4024
+ end
4025
+ else
4026
+ h[x]
4027
+ end
4028
+ end
4029
+
4030
+ # If the result set is the result of a cartesian product, then it is possible that
4031
+ # there are multiple records for each association when there should only be one.
4032
+ # In that case, for each object in all associations loaded via +eager_graph+, run
4033
+ # uniq! on the association to make sure no duplicate records show up.
4034
+ # Note that this can cause legitimate duplicate records to be removed.
4035
+ def post_process(records, dependency_map)
4036
+ records.each do |record|
4037
+ dependency_map.each do |ta, deps|
4038
+ assoc_name = alias_map[ta]
4039
+ list = record.public_send(assoc_name)
4040
+ rec_list = if type_map[ta]
4041
+ list.uniq!
4042
+ if lo = limit_map[ta]
4043
+ limit, offset = lo
4044
+ offset ||= 0
4045
+ if type_map[ta] == :offset
4046
+ [record.associations[assoc_name] = list[offset]]
4047
+ else
4048
+ list.replace(list[(offset)..(limit ? (offset)+limit-1 : -1)] || [])
4049
+ end
4050
+ else
4051
+ list
4052
+ end
4053
+ elsif list
4054
+ [list]
4055
+ else
4056
+ []
4057
+ end
4058
+ record.send(:run_association_callbacks, reflection_map[ta], :after_load, list) if after_load_map[ta]
4059
+ post_process(rec_list, deps) if !rec_list.empty? && !deps.empty?
4060
+ end
4061
+ end
4062
+ end
4063
+ end
4064
+ end
4065
+ end
4066
+ end