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