sequel 3.21.0 → 3.39.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 (352) hide show
  1. data/CHANGELOG +1061 -3
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +103 -63
  4. data/Rakefile +59 -27
  5. data/bin/sequel +50 -26
  6. data/doc/active_record.rdoc +67 -51
  7. data/doc/advanced_associations.rdoc +189 -75
  8. data/doc/association_basics.rdoc +327 -79
  9. data/doc/cheat_sheet.rdoc +21 -21
  10. data/doc/core_extensions.rdoc +374 -0
  11. data/doc/dataset_basics.rdoc +10 -10
  12. data/doc/dataset_filtering.rdoc +54 -42
  13. data/doc/mass_assignment.rdoc +56 -0
  14. data/doc/migration.rdoc +89 -557
  15. data/doc/model_hooks.rdoc +80 -29
  16. data/doc/object_model.rdoc +547 -0
  17. data/doc/opening_databases.rdoc +114 -53
  18. data/doc/prepared_statements.rdoc +41 -15
  19. data/doc/querying.rdoc +137 -56
  20. data/doc/reflection.rdoc +36 -10
  21. data/doc/release_notes/3.22.0.txt +39 -0
  22. data/doc/release_notes/3.23.0.txt +172 -0
  23. data/doc/release_notes/3.24.0.txt +420 -0
  24. data/doc/release_notes/3.25.0.txt +88 -0
  25. data/doc/release_notes/3.26.0.txt +88 -0
  26. data/doc/release_notes/3.27.0.txt +82 -0
  27. data/doc/release_notes/3.28.0.txt +304 -0
  28. data/doc/release_notes/3.29.0.txt +459 -0
  29. data/doc/release_notes/3.30.0.txt +135 -0
  30. data/doc/release_notes/3.31.0.txt +146 -0
  31. data/doc/release_notes/3.32.0.txt +202 -0
  32. data/doc/release_notes/3.33.0.txt +157 -0
  33. data/doc/release_notes/3.34.0.txt +671 -0
  34. data/doc/release_notes/3.35.0.txt +144 -0
  35. data/doc/release_notes/3.36.0.txt +245 -0
  36. data/doc/release_notes/3.37.0.txt +338 -0
  37. data/doc/release_notes/3.38.0.txt +234 -0
  38. data/doc/release_notes/3.39.0.txt +237 -0
  39. data/doc/schema_modification.rdoc +585 -0
  40. data/doc/sharding.rdoc +99 -8
  41. data/doc/sql.rdoc +154 -112
  42. data/doc/testing.rdoc +169 -0
  43. data/doc/thread_safety.rdoc +17 -0
  44. data/doc/transactions.rdoc +137 -0
  45. data/doc/validations.rdoc +1 -1
  46. data/doc/virtual_rows.rdoc +78 -43
  47. data/lib/sequel/adapters/ado/mssql.rb +18 -6
  48. data/lib/sequel/adapters/ado.rb +40 -18
  49. data/lib/sequel/adapters/amalgalite.rb +17 -9
  50. data/lib/sequel/adapters/db2.rb +175 -86
  51. data/lib/sequel/adapters/dbi.rb +15 -15
  52. data/lib/sequel/adapters/do/mysql.rb +17 -11
  53. data/lib/sequel/adapters/do/postgres.rb +2 -61
  54. data/lib/sequel/adapters/do/sqlite.rb +0 -10
  55. data/lib/sequel/adapters/do.rb +18 -36
  56. data/lib/sequel/adapters/firebird.rb +27 -208
  57. data/lib/sequel/adapters/ibmdb.rb +453 -0
  58. data/lib/sequel/adapters/informix.rb +6 -23
  59. data/lib/sequel/adapters/jdbc/as400.rb +16 -34
  60. data/lib/sequel/adapters/jdbc/db2.rb +56 -0
  61. data/lib/sequel/adapters/jdbc/derby.rb +325 -0
  62. data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
  63. data/lib/sequel/adapters/jdbc/h2.rb +73 -31
  64. data/lib/sequel/adapters/jdbc/hsqldb.rb +184 -0
  65. data/lib/sequel/adapters/jdbc/informix.rb +21 -0
  66. data/lib/sequel/adapters/jdbc/jtds.rb +40 -0
  67. data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
  68. data/lib/sequel/adapters/jdbc/mysql.rb +7 -24
  69. data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
  70. data/lib/sequel/adapters/jdbc/postgresql.rb +77 -52
  71. data/lib/sequel/adapters/jdbc/progress.rb +21 -0
  72. data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
  73. data/lib/sequel/adapters/jdbc/sqlserver.rb +66 -0
  74. data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
  75. data/lib/sequel/adapters/jdbc.rb +279 -97
  76. data/lib/sequel/adapters/mock.rb +372 -0
  77. data/lib/sequel/adapters/mysql.rb +125 -222
  78. data/lib/sequel/adapters/mysql2.rb +65 -56
  79. data/lib/sequel/adapters/odbc/mssql.rb +17 -9
  80. data/lib/sequel/adapters/odbc.rb +28 -9
  81. data/lib/sequel/adapters/openbase.rb +3 -5
  82. data/lib/sequel/adapters/oracle.rb +349 -53
  83. data/lib/sequel/adapters/postgres.rb +368 -138
  84. data/lib/sequel/adapters/shared/access.rb +32 -9
  85. data/lib/sequel/adapters/shared/db2.rb +343 -0
  86. data/lib/sequel/adapters/shared/firebird.rb +221 -0
  87. data/lib/sequel/adapters/shared/informix.rb +53 -0
  88. data/lib/sequel/adapters/shared/mssql.rb +401 -116
  89. data/lib/sequel/adapters/shared/mysql.rb +424 -89
  90. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +155 -0
  91. data/lib/sequel/adapters/shared/oracle.rb +257 -45
  92. data/lib/sequel/adapters/shared/postgres.rb +675 -279
  93. data/lib/sequel/adapters/shared/progress.rb +5 -8
  94. data/lib/sequel/adapters/shared/sqlite.rb +285 -96
  95. data/lib/sequel/adapters/sqlite.rb +101 -39
  96. data/lib/sequel/adapters/swift/mysql.rb +10 -12
  97. data/lib/sequel/adapters/swift/postgres.rb +13 -79
  98. data/lib/sequel/adapters/swift/sqlite.rb +9 -6
  99. data/lib/sequel/adapters/swift.rb +28 -19
  100. data/lib/sequel/adapters/tinytds.rb +174 -22
  101. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +77 -0
  102. data/lib/sequel/adapters/utils/pg_types.rb +81 -0
  103. data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
  104. data/lib/sequel/ast_transformer.rb +194 -0
  105. data/lib/sequel/connection_pool/sharded_single.rb +11 -1
  106. data/lib/sequel/connection_pool/sharded_threaded.rb +33 -2
  107. data/lib/sequel/connection_pool/single.rb +5 -0
  108. data/lib/sequel/connection_pool/threaded.rb +43 -14
  109. data/lib/sequel/connection_pool.rb +10 -2
  110. data/lib/sequel/core.rb +143 -11
  111. data/lib/sequel/database/connecting.rb +38 -28
  112. data/lib/sequel/database/dataset.rb +4 -4
  113. data/lib/sequel/database/dataset_defaults.rb +63 -2
  114. data/lib/sequel/database/logging.rb +7 -2
  115. data/lib/sequel/database/misc.rb +190 -46
  116. data/lib/sequel/database/query.rb +272 -110
  117. data/lib/sequel/database/schema_generator.rb +103 -36
  118. data/lib/sequel/database/schema_methods.rb +300 -67
  119. data/lib/sequel/dataset/actions.rb +344 -90
  120. data/lib/sequel/dataset/features.rb +101 -8
  121. data/lib/sequel/dataset/graph.rb +42 -15
  122. data/lib/sequel/dataset/misc.rb +52 -25
  123. data/lib/sequel/dataset/mutation.rb +12 -7
  124. data/lib/sequel/dataset/prepared_statements.rb +68 -30
  125. data/lib/sequel/dataset/query.rb +305 -54
  126. data/lib/sequel/dataset/sql.rb +720 -292
  127. data/lib/sequel/dataset.rb +8 -0
  128. data/lib/sequel/exceptions.rb +4 -0
  129. data/lib/sequel/extensions/_pretty_table.rb +83 -0
  130. data/lib/sequel/extensions/arbitrary_servers.rb +109 -0
  131. data/lib/sequel/extensions/blank.rb +4 -0
  132. data/lib/sequel/extensions/columns_introspection.rb +72 -0
  133. data/lib/sequel/extensions/constraint_validations.rb +451 -0
  134. data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +30 -46
  135. data/lib/sequel/extensions/eval_inspect.rb +173 -0
  136. data/lib/sequel/extensions/inflector.rb +4 -0
  137. data/lib/sequel/extensions/looser_typecasting.rb +5 -4
  138. data/lib/sequel/extensions/migration.rb +100 -11
  139. data/lib/sequel/extensions/named_timezones.rb +9 -0
  140. data/lib/sequel/extensions/null_dataset.rb +94 -0
  141. data/lib/sequel/extensions/pagination.rb +4 -0
  142. data/lib/sequel/extensions/pg_array.rb +537 -0
  143. data/lib/sequel/extensions/pg_array_ops.rb +263 -0
  144. data/lib/sequel/extensions/pg_auto_parameterize.rb +175 -0
  145. data/lib/sequel/extensions/pg_hstore.rb +313 -0
  146. data/lib/sequel/extensions/pg_hstore_ops.rb +293 -0
  147. data/lib/sequel/extensions/pg_inet.rb +113 -0
  148. data/lib/sequel/extensions/pg_interval.rb +191 -0
  149. data/lib/sequel/extensions/pg_json.rb +208 -0
  150. data/lib/sequel/extensions/pg_range.rb +512 -0
  151. data/lib/sequel/extensions/pg_range_ops.rb +150 -0
  152. data/lib/sequel/extensions/pg_row.rb +572 -0
  153. data/lib/sequel/extensions/pg_row_ops.rb +182 -0
  154. data/lib/sequel/extensions/pg_statement_cache.rb +317 -0
  155. data/lib/sequel/extensions/pretty_table.rb +17 -72
  156. data/lib/sequel/extensions/query.rb +8 -4
  157. data/lib/sequel/extensions/query_literals.rb +79 -0
  158. data/lib/sequel/extensions/schema_caching.rb +76 -0
  159. data/lib/sequel/extensions/schema_dumper.rb +282 -76
  160. data/lib/sequel/extensions/select_remove.rb +39 -0
  161. data/lib/sequel/extensions/server_block.rb +140 -0
  162. data/lib/sequel/extensions/split_array_nil.rb +65 -0
  163. data/lib/sequel/extensions/sql_expr.rb +8 -110
  164. data/lib/sequel/extensions/string_date_time.rb +4 -0
  165. data/lib/sequel/extensions/thread_local_timezones.rb +10 -4
  166. data/lib/sequel/extensions/to_dot.rb +99 -83
  167. data/lib/sequel/model/associations.rb +1263 -409
  168. data/lib/sequel/model/base.rb +624 -171
  169. data/lib/sequel/model/errors.rb +1 -1
  170. data/lib/sequel/model/exceptions.rb +24 -2
  171. data/lib/sequel/model/inflections.rb +1 -1
  172. data/lib/sequel/model.rb +30 -11
  173. data/lib/sequel/no_core_ext.rb +2 -0
  174. data/lib/sequel/plugins/active_model.rb +13 -1
  175. data/lib/sequel/plugins/association_pks.rb +22 -4
  176. data/lib/sequel/plugins/caching.rb +25 -18
  177. data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
  178. data/lib/sequel/plugins/composition.rb +44 -12
  179. data/lib/sequel/plugins/constraint_validations.rb +198 -0
  180. data/lib/sequel/plugins/dataset_associations.rb +100 -0
  181. data/lib/sequel/plugins/defaults_setter.rb +72 -0
  182. data/lib/sequel/plugins/dirty.rb +214 -0
  183. data/lib/sequel/plugins/eager_each.rb +59 -0
  184. data/lib/sequel/plugins/force_encoding.rb +6 -6
  185. data/lib/sequel/plugins/hook_class_methods.rb +1 -1
  186. data/lib/sequel/plugins/identity_map.rb +134 -15
  187. data/lib/sequel/plugins/instance_filters.rb +10 -0
  188. data/lib/sequel/plugins/instance_hooks.rb +1 -1
  189. data/lib/sequel/plugins/json_serializer.rb +58 -6
  190. data/lib/sequel/plugins/list.rb +13 -2
  191. data/lib/sequel/plugins/many_through_many.rb +103 -51
  192. data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
  193. data/lib/sequel/plugins/nested_attributes.rb +150 -66
  194. data/lib/sequel/plugins/optimistic_locking.rb +8 -0
  195. data/lib/sequel/plugins/pg_row.rb +121 -0
  196. data/lib/sequel/plugins/pg_typecast_on_load.rb +65 -0
  197. data/lib/sequel/plugins/prepared_statements.rb +167 -0
  198. data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
  199. data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  200. data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
  201. data/lib/sequel/plugins/rcte_tree.rb +31 -17
  202. data/lib/sequel/plugins/schema.rb +8 -3
  203. data/lib/sequel/plugins/serialization.rb +98 -49
  204. data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
  205. data/lib/sequel/plugins/sharding.rb +21 -54
  206. data/lib/sequel/plugins/single_table_inheritance.rb +5 -3
  207. data/lib/sequel/plugins/static_cache.rb +99 -0
  208. data/lib/sequel/plugins/subclasses.rb +29 -3
  209. data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
  210. data/lib/sequel/plugins/timestamps.rb +1 -1
  211. data/lib/sequel/plugins/tree.rb +3 -3
  212. data/lib/sequel/plugins/typecast_on_load.rb +9 -12
  213. data/lib/sequel/plugins/update_primary_key.rb +2 -2
  214. data/lib/sequel/plugins/validation_class_methods.rb +1 -1
  215. data/lib/sequel/plugins/validation_helpers.rb +55 -4
  216. data/lib/sequel/plugins/xml_serializer.rb +15 -4
  217. data/lib/sequel/sql.rb +649 -122
  218. data/lib/sequel/timezones.rb +67 -40
  219. data/lib/sequel/version.rb +1 -1
  220. data/spec/adapters/db2_spec.rb +146 -0
  221. data/spec/adapters/firebird_spec.rb +1 -1
  222. data/spec/adapters/mssql_spec.rb +194 -66
  223. data/spec/adapters/mysql_spec.rb +475 -306
  224. data/spec/adapters/oracle_spec.rb +92 -117
  225. data/spec/adapters/postgres_spec.rb +1982 -325
  226. data/spec/adapters/spec_helper.rb +9 -6
  227. data/spec/adapters/sqlite_spec.rb +203 -75
  228. data/spec/core/connection_pool_spec.rb +218 -93
  229. data/spec/core/database_spec.rb +956 -522
  230. data/spec/core/dataset_spec.rb +1631 -1242
  231. data/spec/core/expression_filters_spec.rb +690 -334
  232. data/spec/core/mock_adapter_spec.rb +453 -0
  233. data/spec/core/object_graph_spec.rb +88 -129
  234. data/spec/core/schema_generator_spec.rb +4 -4
  235. data/spec/core/schema_spec.rb +556 -53
  236. data/spec/core/spec_helper.rb +7 -48
  237. data/spec/core_extensions_spec.rb +626 -0
  238. data/spec/extensions/active_model_spec.rb +13 -0
  239. data/spec/extensions/arbitrary_servers_spec.rb +110 -0
  240. data/spec/extensions/association_autoreloading_spec.rb +18 -10
  241. data/spec/extensions/association_dependencies_spec.rb +15 -25
  242. data/spec/extensions/association_pks_spec.rb +66 -32
  243. data/spec/extensions/association_proxies_spec.rb +4 -4
  244. data/spec/extensions/boolean_readers_spec.rb +25 -25
  245. data/spec/extensions/caching_spec.rb +47 -51
  246. data/spec/extensions/class_table_inheritance_spec.rb +31 -83
  247. data/spec/extensions/columns_introspection_spec.rb +91 -0
  248. data/spec/extensions/composition_spec.rb +18 -13
  249. data/spec/extensions/constraint_validations_plugin_spec.rb +196 -0
  250. data/spec/extensions/constraint_validations_spec.rb +316 -0
  251. data/spec/extensions/dataset_associations_spec.rb +199 -0
  252. data/spec/extensions/defaults_setter_spec.rb +88 -0
  253. data/spec/extensions/dirty_spec.rb +155 -0
  254. data/spec/extensions/eager_each_spec.rb +34 -0
  255. data/spec/extensions/eval_inspect_spec.rb +67 -0
  256. data/spec/extensions/force_encoding_spec.rb +4 -2
  257. data/spec/extensions/hook_class_methods_spec.rb +97 -128
  258. data/spec/extensions/identity_map_spec.rb +142 -24
  259. data/spec/extensions/inflector_spec.rb +0 -4
  260. data/spec/extensions/instance_filters_spec.rb +11 -21
  261. data/spec/extensions/instance_hooks_spec.rb +72 -0
  262. data/spec/extensions/json_serializer_spec.rb +49 -12
  263. data/spec/extensions/lazy_attributes_spec.rb +16 -20
  264. data/spec/extensions/list_spec.rb +49 -40
  265. data/spec/extensions/looser_typecasting_spec.rb +7 -7
  266. data/spec/extensions/many_through_many_spec.rb +368 -254
  267. data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
  268. data/spec/extensions/migration_spec.rb +140 -35
  269. data/spec/extensions/named_timezones_spec.rb +29 -11
  270. data/spec/extensions/nested_attributes_spec.rb +268 -89
  271. data/spec/extensions/null_dataset_spec.rb +85 -0
  272. data/spec/extensions/optimistic_locking_spec.rb +24 -21
  273. data/spec/extensions/pg_array_ops_spec.rb +112 -0
  274. data/spec/extensions/pg_array_spec.rb +320 -0
  275. data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
  276. data/spec/extensions/pg_hstore_ops_spec.rb +140 -0
  277. data/spec/extensions/pg_hstore_spec.rb +194 -0
  278. data/spec/extensions/pg_inet_spec.rb +47 -0
  279. data/spec/extensions/pg_interval_spec.rb +72 -0
  280. data/spec/extensions/pg_json_spec.rb +99 -0
  281. data/spec/extensions/pg_range_ops_spec.rb +56 -0
  282. data/spec/extensions/pg_range_spec.rb +395 -0
  283. data/spec/extensions/pg_row_ops_spec.rb +58 -0
  284. data/spec/extensions/pg_row_plugin_spec.rb +49 -0
  285. data/spec/extensions/pg_row_spec.rb +323 -0
  286. data/spec/extensions/pg_statement_cache_spec.rb +208 -0
  287. data/spec/extensions/pg_typecast_on_load_spec.rb +58 -0
  288. data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
  289. data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
  290. data/spec/extensions/prepared_statements_spec.rb +87 -0
  291. data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
  292. data/spec/extensions/pretty_table_spec.rb +7 -6
  293. data/spec/extensions/query_literals_spec.rb +167 -0
  294. data/spec/extensions/query_spec.rb +9 -3
  295. data/spec/extensions/rcte_tree_spec.rb +50 -43
  296. data/spec/extensions/schema_caching_spec.rb +41 -0
  297. data/spec/extensions/schema_dumper_spec.rb +434 -49
  298. data/spec/extensions/schema_spec.rb +30 -49
  299. data/spec/extensions/select_remove_spec.rb +38 -0
  300. data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
  301. data/spec/extensions/serialization_spec.rb +86 -48
  302. data/spec/extensions/server_block_spec.rb +90 -0
  303. data/spec/extensions/sharding_spec.rb +69 -143
  304. data/spec/extensions/single_table_inheritance_spec.rb +23 -29
  305. data/spec/extensions/skip_create_refresh_spec.rb +1 -1
  306. data/spec/extensions/spec_helper.rb +34 -67
  307. data/spec/extensions/split_array_nil_spec.rb +24 -0
  308. data/spec/extensions/sql_expr_spec.rb +29 -60
  309. data/spec/extensions/static_cache_spec.rb +145 -0
  310. data/spec/extensions/subclasses_spec.rb +14 -0
  311. data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
  312. data/spec/extensions/thread_local_timezones_spec.rb +22 -2
  313. data/spec/extensions/timestamps_spec.rb +6 -6
  314. data/spec/extensions/to_dot_spec.rb +8 -11
  315. data/spec/extensions/touch_spec.rb +13 -14
  316. data/spec/extensions/tree_spec.rb +11 -26
  317. data/spec/extensions/typecast_on_load_spec.rb +9 -6
  318. data/spec/extensions/update_primary_key_spec.rb +30 -24
  319. data/spec/extensions/validation_class_methods_spec.rb +58 -67
  320. data/spec/extensions/validation_helpers_spec.rb +31 -37
  321. data/spec/extensions/xml_serializer_spec.rb +29 -4
  322. data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
  323. data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
  324. data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
  325. data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
  326. data/spec/integration/associations_test.rb +659 -21
  327. data/spec/integration/database_test.rb +11 -5
  328. data/spec/integration/dataset_test.rb +717 -129
  329. data/spec/integration/eager_loader_test.rb +38 -54
  330. data/spec/integration/migrator_test.rb +2 -3
  331. data/spec/integration/model_test.rb +81 -2
  332. data/spec/integration/plugin_test.rb +604 -147
  333. data/spec/integration/prepared_statement_test.rb +272 -128
  334. data/spec/integration/schema_test.rb +367 -57
  335. data/spec/integration/spec_helper.rb +42 -32
  336. data/spec/integration/timezone_test.rb +38 -12
  337. data/spec/integration/transaction_test.rb +183 -8
  338. data/spec/integration/type_test.rb +31 -8
  339. data/spec/model/association_reflection_spec.rb +207 -8
  340. data/spec/model/associations_spec.rb +1206 -693
  341. data/spec/model/base_spec.rb +334 -100
  342. data/spec/model/dataset_methods_spec.rb +45 -27
  343. data/spec/model/eager_loading_spec.rb +828 -692
  344. data/spec/model/hooks_spec.rb +355 -67
  345. data/spec/model/model_spec.rb +251 -179
  346. data/spec/model/plugins_spec.rb +24 -13
  347. data/spec/model/record_spec.rb +506 -232
  348. data/spec/model/spec_helper.rb +16 -77
  349. data/spec/model/validations_spec.rb +38 -2
  350. data/spec/rcov.opts +2 -0
  351. metadata +209 -80
  352. data/spec/core/core_sql_spec.rb +0 -451
@@ -0,0 +1,537 @@
1
+ # The pg_array extension adds support for Sequel to handle
2
+ # PostgreSQL's array types.
3
+ #
4
+ # This extension integrates with Sequel's native postgres adapter, so
5
+ # that when array fields are retrieved, they are parsed and returned
6
+ # as instances of Sequel::Postgres::PGArray. PGArray is
7
+ # a DelegateClass of Array, so it mostly acts like an array, but not
8
+ # completely (is_a?(Array) is false). If you want the actual array,
9
+ # you can call PGArray#to_a. This is done so that Sequel does not
10
+ # treat a PGArray like an Array by default, which would cause issues.
11
+ #
12
+ # In addition to the parsers, this extension comes with literalizers
13
+ # for PGArray using the standard Sequel literalization callbacks, so
14
+ # they work with on all adapters.
15
+ #
16
+ # To turn an existing Array into a PGArray:
17
+ #
18
+ # Sequel.pg_array(array)
19
+ #
20
+ # If you have loaded the {core_extensions extension}[link:files/doc/core_extensions_rdoc.html]),
21
+ # you can also use Array#pg_array:
22
+ #
23
+ # array.pg_array
24
+ #
25
+ # You can also provide a type, though it many cases it isn't necessary:
26
+ #
27
+ # Sequel.pg_array(array, :varchar) # or :integer, :"double precision", etc.
28
+ # array.pg_array(:varchar) # or :integer, :"double precision", etc.
29
+ #
30
+ # So if you want to insert an array into an integer[] database column:
31
+ #
32
+ # DB[:table].insert(:column=>Sequel.pg_array([1, 2, 3]))
33
+ #
34
+ # If you would like to use PostgreSQL arrays in your model objects, you
35
+ # probably want to modify the schema parsing/typecasting so that it
36
+ # recognizes and correctly handles the arrays, which you can do by:
37
+ #
38
+ # DB.extension :pg_array
39
+ #
40
+ # If you are not using the native postgres adapter, you probably
41
+ # also want to use the typecast_on_load plugin in the model, and
42
+ # set it to typecast the array column(s) on load.
43
+ #
44
+ # This extension by default includes handlers for array types for
45
+ # all scalar types that the native postgres adapter handles. It
46
+ # also makes it easy to add support for other array types. In
47
+ # general, you just need to make sure that the scalar type is
48
+ # handled and has the appropriate converter installed in
49
+ # Sequel::Postgres::PG_TYPES under the appropriate type OID.
50
+ # Then you can call Sequel::Postgres::PGArray.register with
51
+ # the appropriate arguments to automatically set up a handler
52
+ # for the array type.
53
+ #
54
+ # For example, if you add support for a scalar custom type named
55
+ # foo which uses OID 1234, and you want to add support for the
56
+ # foo[] type, which uses type OID 4321, you need to do:
57
+ #
58
+ # Sequel::Postgres::PGArray.register('foo', :oid=>4321, :scalar_oid=>1234)
59
+ #
60
+ # Sequel::Postgres::PGArray.register has many additional options
61
+ # and should be able to handle most PostgreSQL array types.
62
+ #
63
+ # If you want an easy way to call PostgreSQL array functions and
64
+ # operators, look into the pg_array_ops extension.
65
+ #
66
+ # This extension requires both the json and delegate libraries.
67
+ #
68
+ # == Additional License
69
+ #
70
+ # PGArray::Parser code was translated from Javascript code in the
71
+ # node-postgres project and has the following additional license:
72
+ #
73
+ # Copyright (c) 2010 Brian Carlson (brian.m.carlson@gmail.com)
74
+ #
75
+ # Permission is hereby granted, free of charge, to any person obtaining
76
+ # a copy of this software and associated documentation files (the
77
+ # "Software"), to deal in the Software without restriction, including
78
+ # without limitation the rights to use, copy, modify, merge, publish,
79
+ # distribute, sublicense, and/or sell copies of the Software, and to
80
+ # permit persons to whom the Software is furnished to do so, subject
81
+ # to the following conditions:
82
+ #
83
+ # The above copyright notice and this permission notice shall be included
84
+ # in all copies or substantial portions of the Software.
85
+ #
86
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
87
+ # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
88
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
89
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
90
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
91
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
92
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93
+
94
+ require 'delegate'
95
+ require 'json'
96
+ Sequel.require 'adapters/utils/pg_types'
97
+
98
+ module Sequel
99
+ module Postgres
100
+ # Represents a PostgreSQL array column value.
101
+ class PGArray < DelegateClass(Array)
102
+ ARRAY = "ARRAY".freeze
103
+ DOUBLE_COLON = '::'.freeze
104
+ EMPTY_BRACKET = '[]'.freeze
105
+ OPEN_BRACKET = '['.freeze
106
+ CLOSE_BRACKET = ']'.freeze
107
+ COMMA = ','.freeze
108
+ BACKSLASH = '\\'.freeze
109
+ EMPTY_STRING = ''.freeze
110
+ OPEN_BRACE = '{'.freeze
111
+ CLOSE_BRACE = '}'.freeze
112
+ NULL = 'NULL'.freeze
113
+ QUOTE = '"'.freeze
114
+
115
+ # Hash of database array type name strings to symbols (e.g. 'double precision' => :float),
116
+ # used by the schema parsing.
117
+ ARRAY_TYPES = {}
118
+
119
+ # Registers an array type that the extension should handle. Makes a Database instance that
120
+ # has been extended with DatabaseMethods recognize the array type given and set up the
121
+ # appropriate typecasting. Also sets up automatic typecasting for the native postgres
122
+ # adapter, so that on retrieval, the values are automatically converted to PGArray instances.
123
+ # The db_type argument should be the exact database type used (as returned by the PostgreSQL
124
+ # format_type database function). Accepts the following options:
125
+ #
126
+ # :array_type :: The type to automatically cast the array to when literalizing the array.
127
+ # Usually the same as db_type.
128
+ # :converter :: A callable object (e.g. Proc), that is called with each element of the array
129
+ # (usually a string), and should return the appropriate typecasted object.
130
+ # :oid :: The PostgreSQL OID for the array type. This is used by the Sequel postgres adapter
131
+ # to set up automatic type conversion on retrieval from the database.
132
+ # :parser :: Can be set to :json to use the faster JSON-based parser. Note that the JSON-based
133
+ # parser can only correctly handle integers values correctly. It doesn't handle
134
+ # full precision for numeric types, and doesn't handle NaN/Infinity values for
135
+ # floating point types.
136
+ # :scalar_oid :: Should be the PostgreSQL OID for the scalar version of this array type. If given,
137
+ # automatically sets the :converter option by looking for scalar conversion
138
+ # proc.
139
+ # :scalar_typecast :: Should be a symbol indicating the typecast method that should be called on
140
+ # each element of the array, when a plain array is passed into a database
141
+ # typecast method. For example, for an array of integers, this could be set to
142
+ # :integer, so that the typecast_value_integer method is called on all of the
143
+ # array elements. Defaults to :type_symbol option.
144
+ # :type_procs :: A hash mapping oids to conversion procs, used for looking up the :scalar_oid and
145
+ # value and setting the :oid value. Defaults to the global Sequel::Postgres::PG_TYPES.
146
+ # :type_symbol :: The base of the schema type symbol for this type. For example, if you provide
147
+ # :integer, Sequel will recognize this type as :integer_array during schema parsing.
148
+ # Defaults to the db_type argument.
149
+ # :typecast_method :: If given, specifies the :type_symbol option, but additionally causes no
150
+ # typecasting method to be created in the database. This should only be used
151
+ # to alias existing array types. For example, if there is an array type that can be
152
+ # treated just like an integer array, you can do :typecast_method=>:integer.
153
+ # :typecast_methods_module :: If given, a module object to add the typecasting method to. Defaults
154
+ # to DatabaseMethods.
155
+ #
156
+ # If a block is given, it is treated as the :converter option.
157
+ def self.register(db_type, opts={}, &block)
158
+ db_type = db_type.to_s
159
+ typecast_method = opts[:typecast_method]
160
+ type = (typecast_method || opts[:type_symbol] || db_type).to_sym
161
+ type_procs = opts[:type_procs] || PG_TYPES
162
+ mod = opts[:typecast_methods_module] || DatabaseMethods
163
+
164
+ if converter = opts[:converter]
165
+ raise Error, "can't provide both a block and :converter option to register" if block
166
+ else
167
+ converter = block
168
+ end
169
+
170
+ if soid = opts[:scalar_oid]
171
+ raise Error, "can't provide both a converter and :scalar_oid option to register" if converter
172
+ raise Error, "no conversion proc for :scalar_oid=>#{soid.inspect}" unless converter = type_procs[soid]
173
+ end
174
+
175
+ array_type = (opts[:array_type] || db_type).to_s.dup.freeze
176
+ creator = (opts[:parser] == :json ? JSONCreator : Creator).new(array_type, converter)
177
+
178
+ ARRAY_TYPES[db_type] = :"#{type}_array"
179
+
180
+ define_array_typecast_method(mod, type, creator, opts.fetch(:scalar_typecast, type)) unless typecast_method
181
+
182
+ if oid = opts[:oid]
183
+ type_procs[oid] = creator
184
+ end
185
+
186
+ nil
187
+ end
188
+
189
+ # Define a private array typecasting method in the given module for the given type that uses
190
+ # the creator argument to do the type conversion.
191
+ def self.define_array_typecast_method(mod, type, creator, scalar_typecast)
192
+ mod.class_eval do
193
+ meth = :"typecast_value_#{type}_array"
194
+ scalar_typecast_method = :"typecast_value_#{scalar_typecast}"
195
+ define_method(meth){|v| typecast_value_pg_array(v, creator, scalar_typecast_method)}
196
+ private meth
197
+ end
198
+ end
199
+ private_class_method :define_array_typecast_method
200
+
201
+ module DatabaseMethods
202
+ APOS = "'".freeze
203
+ DOUBLE_APOS = "''".freeze
204
+ ESCAPE_RE = /("|\\)/.freeze
205
+ ESCAPE_REPLACEMENT = '\\\\\1'.freeze
206
+ BLOB_RANGE = 1...-1
207
+
208
+ # Handle arrays in bound variables
209
+ def bound_variable_arg(arg, conn)
210
+ case arg
211
+ when PGArray
212
+ bound_variable_array(arg.to_a)
213
+ when Array
214
+ bound_variable_array(arg)
215
+ else
216
+ super
217
+ end
218
+ end
219
+
220
+ # Make the column type detection handle registered array types.
221
+ def schema_column_type(db_type)
222
+ if (db_type =~ /\A([^(]+)(?:\([^(]+\))?\[\]\z/io) && (type = ARRAY_TYPES[$1])
223
+ type
224
+ else
225
+ super
226
+ end
227
+ end
228
+
229
+ private
230
+
231
+ # Format arrays used in bound variables.
232
+ def bound_variable_array(a)
233
+ case a
234
+ when Array
235
+ "{#{a.map{|i| bound_variable_array(i)}.join(COMMA)}}"
236
+ when Sequel::SQL::Blob
237
+ "\"#{literal(a)[BLOB_RANGE].gsub(DOUBLE_APOS, APOS).gsub(ESCAPE_RE, ESCAPE_REPLACEMENT)}\""
238
+ when Sequel::LiteralString
239
+ a
240
+ when String
241
+ "\"#{a.gsub(ESCAPE_RE, ESCAPE_REPLACEMENT)}\""
242
+ else
243
+ literal(a)
244
+ end
245
+ end
246
+
247
+ # Manually override the typecasting for timestamp array types so that
248
+ # they use the database's timezone instead of the global Sequel
249
+ # timezone.
250
+ def get_conversion_procs
251
+ procs = super
252
+
253
+ procs[1115] = Creator.new("timestamp without time zone", procs[1114])
254
+ procs[1185] = Creator.new("timestamp with time zone", procs[1184])
255
+
256
+ procs
257
+ end
258
+
259
+ # Given a value to typecast and the type of PGArray subclass:
260
+ # * If given a PGArray with a matching array_type, use it directly.
261
+ # * If given a PGArray with a different array_type, return a PGArray
262
+ # with the creator's type.
263
+ # * If given an Array, create a new PGArray instance for it. This does not
264
+ # typecast all members of the array in ruby for performance reasons, but
265
+ # it will cast the array the appropriate database type when the array is
266
+ # literalized.
267
+ # * If given a String, call the parser for the subclass with it.
268
+ def typecast_value_pg_array(value, creator, scalar_typecast_method=nil)
269
+ case value
270
+ when PGArray
271
+ if value.array_type != creator.type
272
+ PGArray.new(value.to_a, creator.type)
273
+ else
274
+ value
275
+ end
276
+ when Array
277
+ if scalar_typecast_method && respond_to?(scalar_typecast_method, true)
278
+ value = Sequel.recursive_map(value, method(scalar_typecast_method))
279
+ end
280
+ PGArray.new(value, creator.type)
281
+ else
282
+ raise Sequel::InvalidValue, "invalid value for array type: #{value.inspect}"
283
+ end
284
+ end
285
+ end
286
+
287
+ # PostgreSQL array parser that handles all types of input.
288
+ #
289
+ # This parser is very simple and unoptimized, but should still
290
+ # be O(n) where n is the length of the input string.
291
+ class Parser
292
+ # Current position in the input string.
293
+ attr_reader :pos
294
+
295
+ # Set the source for the input, and any converter callable
296
+ # to call with objects to be created. For nested parsers
297
+ # the source may contain text after the end current parse,
298
+ # which will be ignored.
299
+ def initialize(source, converter=nil)
300
+ @source = source
301
+ @source_length = source.length
302
+ @converter = converter
303
+ @pos = -1
304
+ @entries = []
305
+ @recorded = ""
306
+ @dimension = 0
307
+ end
308
+
309
+ # Return 2 objects, whether the next character in the input
310
+ # was escaped with a backslash, and what the next character is.
311
+ def next_char
312
+ @pos += 1
313
+ if (c = @source[@pos..@pos]) == BACKSLASH
314
+ @pos += 1
315
+ [true, @source[@pos..@pos]]
316
+ else
317
+ [false, c]
318
+ end
319
+ end
320
+
321
+ # Add a new character to the buffer of recorded characters.
322
+ def record(c)
323
+ @recorded << c
324
+ end
325
+
326
+ # Take the buffer of recorded characters and add it to the array
327
+ # of entries, and use a new buffer for recorded characters.
328
+ def new_entry(include_empty=false)
329
+ if !@recorded.empty? || include_empty
330
+ entry = @recorded
331
+ if entry == NULL && !include_empty
332
+ entry = nil
333
+ elsif @converter
334
+ entry = @converter.call(entry)
335
+ end
336
+ @entries.push(entry)
337
+ @recorded = ""
338
+ end
339
+ end
340
+
341
+ # Parse the input character by character, returning an array
342
+ # of parsed (and potentially converted) objects.
343
+ def parse(nested=false)
344
+ # quote sets whether we are inside of a quoted string.
345
+ quote = false
346
+ until @pos >= @source_length
347
+ escaped, char = next_char
348
+ if char == OPEN_BRACE && !quote
349
+ @dimension += 1
350
+ if (@dimension > 1)
351
+ # Multi-dimensional array encounter, use a subparser
352
+ # to parse the next level down.
353
+ subparser = self.class.new(@source[@pos..-1], @converter)
354
+ @entries.push(subparser.parse(true))
355
+ @pos += subparser.pos - 1
356
+ end
357
+ elsif char == CLOSE_BRACE && !quote
358
+ @dimension -= 1
359
+ if (@dimension == 0)
360
+ new_entry
361
+ # Exit early if inside a subparser, since the
362
+ # text after parsing the current level should be
363
+ # ignored as it is handled by the parent parser.
364
+ return @entries if nested
365
+ end
366
+ elsif char == QUOTE && !escaped
367
+ # If already inside the quoted string, this is the
368
+ # ending quote, so add the entry. Otherwise, this
369
+ # is the opening quote, so set the quote flag.
370
+ new_entry(true) if quote
371
+ quote = !quote
372
+ elsif char == COMMA && !quote
373
+ # If not inside a string and a comma occurs, it indicates
374
+ # the end of the entry, so add the entry.
375
+ new_entry
376
+ else
377
+ # Add the character to the recorded character buffer.
378
+ record(char)
379
+ end
380
+ end
381
+ raise Sequel::Error, "array dimensions not balanced" unless @dimension == 0
382
+ @entries
383
+ end
384
+ end unless Sequel::Postgres.respond_to?(:parse_pg_array)
385
+
386
+ # Callable object that takes the input string and parses it using Parser.
387
+ class Creator
388
+ # The converter callable that is called on each member of the array
389
+ # to convert it to the correct type.
390
+ attr_reader :converter
391
+
392
+ # The database type to set on the PGArray instances returned.
393
+ attr_reader :type
394
+
395
+ # Set the type and optional converter callable that will be used.
396
+ def initialize(type, converter=nil)
397
+ @type = type
398
+ @converter = converter
399
+ end
400
+
401
+ if Sequel::Postgres.respond_to?(:parse_pg_array)
402
+ # Use sequel_pg's C-based parser if it has already been defined.
403
+ def call(string)
404
+ PGArray.new(Sequel::Postgres.parse_pg_array(string, @converter), @type)
405
+ end
406
+ else
407
+ # Parse the string using Parser with the appropriate
408
+ # converter, and return a PGArray with the appropriate database
409
+ # type.
410
+ def call(string)
411
+ PGArray.new(Parser.new(string, @converter).parse, @type)
412
+ end
413
+ end
414
+ end
415
+
416
+ # Callable object that takes the input string and parses it using.
417
+ # a JSON parser. This should be faster than the standard Creator,
418
+ # but only handles integer types correctly.
419
+ class JSONCreator < Creator
420
+ # Character conversion map mapping input strings to JSON replacements
421
+ SUBST = {'{'.freeze=>'['.freeze, '}'.freeze=>']'.freeze, 'NULL'.freeze=>'null'.freeze}
422
+
423
+ # Regular expression matching input strings to convert
424
+ SUBST_RE = %r[\{|\}|NULL].freeze
425
+
426
+ # Parse the input string by using a gsub to convert non-JSON characters to
427
+ # JSON, running it through a regular JSON parser. If a converter is used, a
428
+ # recursive map of the output is done to make sure that the entires in the
429
+ # correct type.
430
+ def call(string)
431
+ array = JSON.parse(string.gsub(SUBST_RE){|m| SUBST[m]})
432
+ array = Sequel.recursive_map(array, @converter) if @converter
433
+ PGArray.new(array, @type)
434
+ end
435
+ end
436
+
437
+ # The type of this array. May be nil if no type was given. If a type
438
+ # is provided, the array is automatically casted to this type when
439
+ # literalizing. This type is the underlying type, not the array type
440
+ # itself, so for an int4[] database type, it should be :int4 or 'int4'
441
+ attr_accessor :array_type
442
+
443
+ # Set the array to delegate to, and a database type.
444
+ def initialize(array, type=nil)
445
+ super(array)
446
+ @array_type = type
447
+ end
448
+
449
+ # Append the array SQL to the given sql string.
450
+ # If the receiver has a type, add a cast to the
451
+ # database array type.
452
+ def sql_literal_append(ds, sql)
453
+ sql << ARRAY
454
+ _literal_append(sql, ds, to_a)
455
+ if at = array_type
456
+ sql << DOUBLE_COLON << at.to_s << EMPTY_BRACKET
457
+ end
458
+ end
459
+
460
+ private
461
+
462
+ # Recursive method that handles multi-dimensional
463
+ # arrays, surrounding each with [] and interspersing
464
+ # entries with ,.
465
+ def _literal_append(sql, ds, array)
466
+ sql << OPEN_BRACKET
467
+ comma = false
468
+ commas = COMMA
469
+ array.each do |i|
470
+ sql << commas if comma
471
+ if i.is_a?(Array)
472
+ _literal_append(sql, ds, i)
473
+ else
474
+ ds.literal_append(sql, i)
475
+ end
476
+ comma = true
477
+ end
478
+ sql << CLOSE_BRACKET
479
+ end
480
+
481
+ # Register all array types that this extension handles by default.
482
+
483
+ register('text', :oid=>1009, :type_symbol=>:string)
484
+ register('integer', :oid=>1007, :parser=>:json)
485
+ register('bigint', :oid=>1016, :parser=>:json, :scalar_typecast=>:integer)
486
+ register('numeric', :oid=>1231, :scalar_oid=>1700, :type_symbol=>:decimal)
487
+ register('double precision', :oid=>1022, :scalar_oid=>701, :type_symbol=>:float)
488
+
489
+ register('boolean', :oid=>1000, :scalar_oid=>16)
490
+ register('bytea', :oid=>1001, :scalar_oid=>17, :type_symbol=>:blob)
491
+ register('date', :oid=>1182, :scalar_oid=>1082)
492
+ register('time without time zone', :oid=>1183, :scalar_oid=>1083, :type_symbol=>:time)
493
+ register('timestamp without time zone', :oid=>1115, :scalar_oid=>1114, :type_symbol=>:datetime)
494
+ register('time with time zone', :oid=>1270, :scalar_oid=>1083, :type_symbol=>:time_timezone, :scalar_typecast=>:time)
495
+ register('timestamp with time zone', :oid=>1185, :scalar_oid=>1184, :type_symbol=>:datetime_timezone, :scalar_typecast=>:datetime)
496
+
497
+ register('smallint', :oid=>1005, :parser=>:json, :typecast_method=>:integer)
498
+ register('oid', :oid=>1028, :parser=>:json, :typecast_method=>:integer)
499
+ register('real', :oid=>1021, :scalar_oid=>701, :typecast_method=>:float)
500
+ register('character', :oid=>1014, :array_type=>:text, :typecast_method=>:string)
501
+ register('character varying', :oid=>1015, :typecast_method=>:string)
502
+ end
503
+ end
504
+
505
+ module SQL::Builders
506
+ # Return a Postgres::PGArray proxy for the given array and database array type.
507
+ def pg_array(v, array_type=nil)
508
+ case v
509
+ when Postgres::PGArray
510
+ if array_type.nil? || v.array_type == array_type
511
+ v
512
+ else
513
+ Postgres::PGArray.new(v.to_a, array_type)
514
+ end
515
+ when Array
516
+ Postgres::PGArray.new(v, array_type)
517
+ else
518
+ # May not be defined unless the pg_array_ops extension is used
519
+ pg_array_op(v)
520
+ end
521
+ end
522
+ end
523
+
524
+ Database.register_extension(:pg_array, Postgres::PGArray::DatabaseMethods)
525
+ end
526
+
527
+ if Sequel.core_extensions?
528
+ class Array
529
+ # Return a PGArray proxy to the receiver, using a
530
+ # specific database type if given. This is mostly useful
531
+ # as a short cut for creating PGArray objects that didn't
532
+ # come from the database.
533
+ def pg_array(type=nil)
534
+ Sequel::Postgres::PGArray.new(self, type)
535
+ end
536
+ end
537
+ end