sequel 5.98.0 → 5.99.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28641a0240d39f16a5b6f2d30986d44a8744c26e24de5e78b8e3823a409c48d2
4
- data.tar.gz: 25435173509b5058e781f4b06b1ebf8005244e86864c5a72ac35bc739656a6b1
3
+ metadata.gz: 7e5d22a32d8c5f27e15e00203d0c2ce2a2fb3f5352d7b7c25c82389f4947882c
4
+ data.tar.gz: 81909dc070cc8d48def396ae76e953a1f31dc478b45d3a5fa8b69646bc7df094
5
5
  SHA512:
6
- metadata.gz: 11c7f62e3e296c43bef8af4de71ecaaf01e2e4d7e034910403991b91bc2075778dde0d16e899bc3ddacc1351e38da0f61f43476272e3b90c9a2aee5b09d82599
7
- data.tar.gz: 173dfc5d3bbc19846a2d27b077d2241e6f64473f8a8f16ba052d9fd021d514036af1cf42cade4ea8974f2d920e126d4c23e824ab0e2d49a9062857ecd7e93342
6
+ metadata.gz: c24ae48ece66511a09b2d8a5ad8b5b82e7486a4446bcb66108140b8afe449cd9c9e7645a29ca0bbe0c994d5c060a52f1ab4c41434dc068175751b1d6bd57b5dc
7
+ data.tar.gz: e8acd2e5cb4272bd47aed87e3077018dfc5ba9673856b871c8222eec6b2de1be0843840969e046efc459b52902753e9e0f0e35b95dcdcf31914d98b5f487d78a
@@ -679,7 +679,7 @@ module Sequel
679
679
 
680
680
  # MSSQL uses the CONTAINS keyword for full text search
681
681
  def full_text_search(cols, terms, opts = OPTS)
682
- terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
682
+ terms = "\"#{Sequel.array_or_set_join(terms, '" OR "')}\"" if terms.is_a?(Array) || terms.is_a?(Set)
683
683
  where(Sequel.lit("CONTAINS (?, ?)", cols, terms))
684
684
  end
685
685
 
@@ -797,7 +797,7 @@ module Sequel
797
797
 
798
798
  # MySQL specific full text search syntax.
799
799
  def full_text_sql(cols, terms, opts = OPTS)
800
- terms = terms.join(' ') if terms.is_a?(Array)
800
+ terms = Sequel.array_or_set_join(terms, ' ') if terms.is_a?(Array) || terms.is_a?(Set)
801
801
  SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms])
802
802
  end
803
803
 
@@ -669,12 +669,26 @@ module Sequel
669
669
  _set_constraints(' IMMEDIATE', opts)
670
670
  end
671
671
 
672
- # Use the pg_* system tables to determine indexes on a table
672
+ # Use the pg_* system tables to determine indexes on a table. Options:
673
+ #
674
+ # :include_partial :: Set to true to include partial indexes
675
+ # :invalid :: Set to true or :only to only return invalid indexes.
676
+ # Set to :include to also return both valid and invalid indexes.
677
+ # When not set or other value given, does not return invalid indexes.
673
678
  def indexes(table, opts=OPTS)
674
679
  m = output_identifier_meth
675
680
  cond = {Sequel[:tab][:oid]=>regclass_oid(table, opts)}
676
681
  cond[:indpred] = nil unless opts[:include_partial]
677
682
 
683
+ case opts[:invalid]
684
+ when true, :only
685
+ cond[:indisvalid] = false
686
+ when :include
687
+ # nothing
688
+ else
689
+ cond[:indisvalid] = true
690
+ end
691
+
678
692
  indexes = {}
679
693
  _indexes_ds.where_each(cond) do |r|
680
694
  i = indexes[m.call(r[:name])] ||= {:columns=>[], :unique=>r[:unique], :deferrable=>r[:deferrable]}
@@ -1049,8 +1063,7 @@ module Sequel
1049
1063
  where{{
1050
1064
  indc[:relkind]=>%w'i I',
1051
1065
  ind[:indisprimary]=>false,
1052
- :indexprs=>nil,
1053
- :indisvalid=>true}}.
1066
+ :indexprs=>nil}}.
1054
1067
  order(*order).
1055
1068
  select{[indc[:relname].as(:name), ind[:indisunique].as(:unique), att[:attname].as(:column), con[:condeferrable].as(:deferrable)]}
1056
1069
 
@@ -1757,7 +1770,7 @@ module Sequel
1757
1770
  index_type = :gist
1758
1771
  end
1759
1772
 
1760
- "CREATE #{unique}INDEX#{' CONCURRENTLY' if index[:concurrently]}#{if_not_exists} #{quote_identifier(index_name)} ON #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{" INCLUDE #{literal(Array(index[:include]))}" if index[:include]}#{nulls_distinct}#{" TABLESPACE #{quote_identifier(index[:tablespace])}" if index[:tablespace]}#{filter}"
1773
+ "CREATE #{unique}INDEX#{' CONCURRENTLY' if index[:concurrently]}#{if_not_exists} #{quote_identifier(index_name)} ON#{' ONLY' if index[:only]} #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{" INCLUDE #{literal(Array(index[:include]))}" if index[:include]}#{nulls_distinct}#{" TABLESPACE #{quote_identifier(index[:tablespace])}" if index[:tablespace]}#{filter}"
1761
1774
  end
1762
1775
 
1763
1776
  # Setup datastructures shared by all postgres adapters.
@@ -2160,7 +2173,7 @@ module Sequel
2160
2173
  end
2161
2174
 
2162
2175
  unless opts[:tsquery]
2163
- phrase_terms = terms.is_a?(Array) ? terms.join(' | ') : terms
2176
+ phrase_terms = terms.is_a?(Array) || terms.is_a?(Set) ? Sequel.array_or_set_join(terms, ' | ') : terms
2164
2177
 
2165
2178
  query_func = case to_tsquery = opts[:to_tsquery]
2166
2179
  when :phrase, :plain
@@ -26,6 +26,8 @@ module Sequel
26
26
  h = {}
27
27
  o.each{|k, val| h[v(k)] = v(val)}
28
28
  h
29
+ when Set
30
+ Set.new(o.map{|x| v(x)})
29
31
  when SQL::NumericExpression
30
32
  if o.op == :extract
31
33
  o.class.new(o.op, o.args[0], v(o.args[1]))
data/lib/sequel/core.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen-string-literal: true
2
2
 
3
- %w'bigdecimal date thread time uri'.each{|f| require f}
3
+ %w'bigdecimal date set thread time uri'.each{|f| require f}
4
4
 
5
5
  # Top level module for Sequel
6
6
  #
@@ -164,6 +164,20 @@ module Sequel
164
164
  JSON::ParserError
165
165
  end
166
166
 
167
+ if RUBY_VERSION >= '3'
168
+ # Join the array or set.
169
+ def array_or_set_join(obj, arg)
170
+ obj.join(arg)
171
+ end
172
+ # :nocov:
173
+ else
174
+ def array_or_set_join(obj, arg)
175
+ obj = obj.to_a if obj.is_a?(Set)
176
+ obj.join(arg)
177
+ end
178
+ end
179
+ # :nocov:
180
+
167
181
  if RUBY_VERSION >= '3.3'
168
182
  # Create a new module using the block, and set the temporary name
169
183
  # on it using the given a containing module and name.
@@ -635,7 +635,7 @@ module Sequel
635
635
  # Typecast the value to a String
636
636
  def typecast_value_string(value)
637
637
  case value
638
- when Hash, Array
638
+ when Hash, Array, Set
639
639
  raise Sequel::InvalidValue, "invalid value for String: #{value.inspect}"
640
640
  else
641
641
  value.to_s
@@ -299,6 +299,7 @@ module Sequel
299
299
  # :opclass :: Set an opclass to use for all columns (per-column opclasses require
300
300
  # custom SQL).
301
301
  # :tablespace :: Specify tablespace for index.
302
+ # :only :: Create index only for given table, not for any child tables (PostgreSQL 11+)
302
303
  #
303
304
  # Microsoft SQL Server specific options:
304
305
  #
@@ -11,11 +11,12 @@ module Sequel
11
11
 
12
12
  # Action methods defined by Sequel that execute code on the database.
13
13
  ACTION_METHODS = (<<-METHS).split.map(&:to_sym).freeze
14
- << [] all as_hash avg count columns columns! delete each
14
+ << [] all as_hash as_set avg count columns columns! delete each
15
15
  empty? fetch_rows first first! get import insert last
16
- map max min multi_insert paged_each select_hash select_hash_groups select_map select_order_map
17
- single_record single_record! single_value single_value! sum to_hash to_hash_groups truncate update
18
- where_all where_each where_single_value
16
+ map max min multi_insert paged_each select_hash select_hash_groups
17
+ select_map select_order_map select_set single_record single_record!
18
+ single_value single_value! sum to_hash to_hash_groups
19
+ truncate update where_all where_each where_single_value
19
20
  METHS
20
21
 
21
22
  # The clone options to use when retrieving columns for a dataset.
@@ -51,6 +52,26 @@ module Sequel
51
52
  _all(block){|a| each{|r| a << r}}
52
53
  end
53
54
 
55
+ # Returns sets for column values for each record in the dataset.
56
+ #
57
+ # DB[:table].as_set(:id) # SELECT * FROM table
58
+ # # => Set[1, 2, 3, ...]
59
+ #
60
+ # You can also provide an array of column names, in which case the elements
61
+ # of the returned set are arrays (not sets):
62
+ #
63
+ # DB[:table].as_set([:id, :name]) # SELECT * FROM table
64
+ # # => Set[[1, 'A'], [2, 'B'], [3, 'C'], ...]
65
+ def as_set(column)
66
+ return naked.as_set(column) if row_proc
67
+
68
+ if column.is_a?(Array)
69
+ to_set{|r| r.values_at(*column)}
70
+ else
71
+ to_set{|r| r[column]}
72
+ end
73
+ end
74
+
54
75
  # Returns the average value for the given column/expression.
55
76
  # Uses a virtual row block if no argument is given.
56
77
  #
@@ -727,10 +748,7 @@ module Sequel
727
748
  end
728
749
 
729
750
  # Selects the column given (either as an argument or as a block), and
730
- # returns an array of all values of that column in the dataset. If you
731
- # give a block argument that returns an array with multiple entries,
732
- # the contents of the resulting array are undefined. Raises an Error
733
- # if called with both an argument and a block.
751
+ # returns an array of all values of that column in the dataset.
734
752
  #
735
753
  # DB[:table].select_map(:id) # SELECT id FROM table
736
754
  # # => [3, 5, 8, 1, ...]
@@ -768,6 +786,34 @@ module Sequel
768
786
  _select_map(column, true, &block)
769
787
  end
770
788
 
789
+ # Selects the column given (either as an argument or as a block), and
790
+ # returns a set of all values of that column in the dataset.
791
+ #
792
+ # DB[:table].select_set(:id) # SELECT id FROM table
793
+ # # => Set[3, 5, 8, 1, ...]
794
+ #
795
+ # DB[:table].select_set{id * 2} # SELECT (id * 2) FROM table
796
+ # # => Set[6, 10, 16, 2, ...]
797
+ #
798
+ # You can also provide an array of column names, which returns a set
799
+ # with array elements (not set elements):
800
+ #
801
+ # DB[:table].select_map([:id, :name]) # SELECT id, name FROM table
802
+ # # => Set[[1, 'A'], [2, 'B'], [3, 'C'], ...]
803
+ #
804
+ # If you provide an array of expressions, you must be sure that each entry
805
+ # in the array has an alias that Sequel can determine.
806
+ def select_set(column=nil, &block)
807
+ ds = ungraphed.naked
808
+ columns = Array(column)
809
+ virtual_row_columns(columns, block)
810
+ if column.is_a?(Array) || (columns.length > 1)
811
+ ds.select(*columns)._select_set_multiple(hash_key_symbols(columns))
812
+ else
813
+ ds.select(auto_alias_expression(columns.first))._select_set_single
814
+ end
815
+ end
816
+
771
817
  # Limits the dataset to one record, and returns the first record in the dataset,
772
818
  # or nil if the dataset has no records. Users should probably use +first+ instead of
773
819
  # this method. Example:
@@ -1092,6 +1138,17 @@ module Sequel
1092
1138
  map{|r| r[k||=r.keys.first]}
1093
1139
  end
1094
1140
 
1141
+ # Return a set of arrays of values given by the symbols in ret_cols.
1142
+ def _select_set_multiple(ret_cols)
1143
+ to_set{|r| r.values_at(*ret_cols)}
1144
+ end
1145
+
1146
+ # Returns a set of the first value in each row.
1147
+ def _select_set_single
1148
+ k = nil
1149
+ to_set{|r| r[k||=r.keys.first]}
1150
+ end
1151
+
1095
1152
  # A dataset for returning single values from the current dataset.
1096
1153
  def single_value_ds
1097
1154
  clone(:limit=>1).ungraphed.naked
@@ -1496,7 +1496,7 @@ module Sequel
1496
1496
  end
1497
1497
  when LiteralString
1498
1498
  LiteralString.new("(#{expr})")
1499
- when Numeric, SQL::NumericExpression, SQL::StringExpression, Proc, String
1499
+ when Numeric, SQL::NumericExpression, SQL::StringExpression, Proc, String, Set
1500
1500
  raise Error, "Invalid filter expression: #{expr.inspect}"
1501
1501
  when TrueClass, FalseClass
1502
1502
  if supports_where_true?
@@ -85,6 +85,8 @@ module Sequel
85
85
  literal_date_append(sql, v)
86
86
  when Dataset
87
87
  literal_dataset_append(sql, v)
88
+ when Set
89
+ literal_set_append(sql, v)
88
90
  else
89
91
  literal_other_append(sql, v)
90
92
  end
@@ -375,9 +377,9 @@ module Sequel
375
377
  cols = args[0]
376
378
  vals = args[1]
377
379
  col_array = true if cols.is_a?(Array)
378
- if vals.is_a?(Array)
380
+ if vals.is_a?(Array) || vals.is_a?(Set)
379
381
  val_array = true
380
- empty_val_array = vals == []
382
+ empty_val_array = vals.empty?
381
383
  end
382
384
  if empty_val_array
383
385
  literal_append(sql, empty_array_value(op, cols))
@@ -1448,6 +1450,12 @@ module Sequel
1448
1450
  end
1449
1451
  end
1450
1452
 
1453
+ # Append a literalization of the set to SQL string.
1454
+ # Treats as an expression as an SQL value list.
1455
+ def literal_set_append(sql, v)
1456
+ array_sql_append(sql, v)
1457
+ end
1458
+
1451
1459
  # SQL fragment for Sequel::SQLTime, containing just the time part
1452
1460
  def literal_sqltime(v)
1453
1461
  v.strftime(default_time_format)
@@ -436,7 +436,7 @@ module Sequel
436
436
  else
437
437
  raise Error, "validates includes with a range only supports integers currently, cannot handle: #{arg.inspect}"
438
438
  end
439
- elsif arg.is_a?(Array)
439
+ elsif arg.is_a?(Array) || arg.is_a?(Set)
440
440
  if arg.all?{|x| x.is_a?(Integer)}
441
441
  validation_type = :includes_int_array
442
442
  elsif arg.all?{|x| x.is_a?(String)}
@@ -444,9 +444,9 @@ module Sequel
444
444
  else
445
445
  raise Error, "validates includes with an array only supports strings and integers currently, cannot handle: #{arg.inspect}"
446
446
  end
447
- arg = arg.join(',')
447
+ arg = Sequel.array_or_set_join(arg, ',')
448
448
  else
449
- raise Error, "validates includes only supports arrays and ranges currently, cannot handle: #{arg.inspect}"
449
+ raise Error, "validates includes only supports arrays, sets, and ranges currently, cannot handle: #{arg.inspect}"
450
450
  end
451
451
  when :like, :ilike
452
452
  generator.constraint(constraint, constraint_validation_expression(columns, allow_nil){|c| Sequel.public_send(validation_type, c, arg)})
@@ -9,6 +9,13 @@
9
9
  # DB[:test].exclude(name: [])
10
10
  # # SELECT * FROM test WHERE (name = name)
11
11
  #
12
+ # This works for sets in addition to arrays:
13
+ #
14
+ # DB[:test].where(name: Set[])
15
+ # # SELECT * FROM test WHERE (name != name)
16
+ # DB[:test].exclude(name: Set[])
17
+ # # SELECT * FROM test WHERE (name = name)
18
+ #
12
19
  # The default Sequel behavior is to ignore NULLs, as the above
13
20
  # query is not generally optimized well by databases.
14
21
  #
@@ -34,6 +34,8 @@ module Sequel
34
34
  "#{obj.class}.new(#{obj.to_a.inspect})"
35
35
  when Array
36
36
  "[#{obj.map{|o| eval_inspect(o)}.join(', ')}]"
37
+ when Set
38
+ "Set[#{obj.map{|o| eval_inspect(o)}.join(', ')}]"
37
39
  when Hash
38
40
  "{#{obj.map{|k, v| "#{eval_inspect(k)} => #{eval_inspect(v)}"}.join(', ')}}"
39
41
  when Time
@@ -408,9 +408,9 @@ module Sequel
408
408
  end
409
409
  end
410
410
 
411
- # Whether the given argument is an array of integers or NULL values, recursively.
411
+ # Whether the given argument is an array or set of integers or NULL values, recursively.
412
412
  def _integer_array?(v)
413
- Array === v && v.all?{|x| nil == x || Integer === x}
413
+ (Array === v || Set === v) && v.all?{|x| nil == x || Integer === x}
414
414
  end
415
415
 
416
416
  # Create the bound variable string that will be used for the IN (int, ...) to = ANY($)
@@ -120,7 +120,7 @@ module Sequel
120
120
  # The bound variable type string to use for the bound variable array.
121
121
  # Returns nil if a bound variable should not be used for the array.
122
122
  def _bound_variable_type_for_array(r)
123
- return unless Array === r && r.size >= pg_auto_parameterize_min_array_size
123
+ return unless (Array === r || Set === r) && r.size >= pg_auto_parameterize_min_array_size
124
124
  classes = r.map(&:class)
125
125
  classes.uniq!
126
126
  classes.delete(NilClass)
@@ -165,6 +165,7 @@ module Sequel
165
165
 
166
166
  # Convert RHS of IN/NOT IN operator to PGArray with given type.
167
167
  def _convert_array_to_pg_array_with_type(r, type)
168
+ r = r.to_a if Set === r
168
169
  Sequel.pg_array(r, type)
169
170
  end
170
171
  end
@@ -294,13 +294,18 @@ module Sequel
294
294
  SQL::Function.new(name, self, *args)
295
295
  end
296
296
 
297
- # Wrap argument in a PGArray if it is an array
297
+ # Wrap argument in a PGArray if it is an array or a set
298
298
  def wrap_input_array(obj)
299
- if obj.is_a?(Array) && Sequel.respond_to?(:pg_array)
300
- Sequel.pg_array(obj)
301
- else
302
- obj
299
+ if Sequel.respond_to?(:pg_array)
300
+ case obj
301
+ when Array
302
+ return Sequel.pg_array(obj)
303
+ when Set
304
+ return Sequel.pg_array(obj.to_a)
305
+ end
303
306
  end
307
+
308
+ obj
304
309
  end
305
310
 
306
311
  # Wrap argument in an Hstore if it is a hash
@@ -841,13 +841,20 @@ module Sequel
841
841
  Sequel::SQL::BooleanExpression.new(:NOOP, Sequel::SQL::PlaceholderLiteralString.new(str, [value, other]))
842
842
  end
843
843
 
844
- # Wrap argument in a PGArray if it is an array
844
+ # Wrap argument in a PGArray if it is an array or a set
845
845
  def wrap_input_array(obj)
846
- if obj.is_a?(Array) && Sequel.respond_to?(:pg_array)
847
- Sequel.pg_array(obj)
848
- else
849
- obj
846
+ # :nocov:
847
+ if Sequel.respond_to?(:pg_array)
848
+ # :nocov:
849
+ case obj
850
+ when Array
851
+ return Sequel.pg_array(obj)
852
+ when Set
853
+ return Sequel.pg_array(obj.to_a)
854
+ end
850
855
  end
856
+
857
+ obj
851
858
  end
852
859
 
853
860
  # Wrap argument in a JSONBArray or JSONBHash if it is an array or hash.
@@ -319,8 +319,8 @@ module Sequel
319
319
  end
320
320
 
321
321
  # Typecast the given object to the appropriate type using the
322
- # typecaster. Note that this does not conversion for the members
323
- # of the composite type, since those conversion expect strings and
322
+ # typecaster. Note that this does no conversion for the members
323
+ # of the composite type, since those conversions expect strings and
324
324
  # strings may not be provided.
325
325
  def typecast(obj)
326
326
  case obj
@@ -1,56 +1,37 @@
1
1
  # frozen-string-literal: true
2
2
  #
3
- # The set_literalizer extension allows for using Set instances in many of the
4
- # same places that you would use Array instances:
5
- #
6
- # DB[:table].where(column: Set.new([1, 2, 3]))
7
- # # SELECT FROM table WHERE (column IN (1, 2, 3))
8
- #
9
- # To load the extension into all datasets created from a given Database:
10
- #
11
- # DB.extension :set_literalizer
3
+ # The set_literalizer extension should no longer be used, as Sequel
4
+ # now supports Set values by default. For backwards compatibility
5
+ # the set_literalizer extension will treat a set that contains only
6
+ # 2 element arrays as a condition specifier (matching the behavior
7
+ # for arrays where all elements are 2 element arrays). This is not
8
+ # compatible with Sequel's current default behavior. If you are
9
+ # relying on this behavior, it is recommended you convert the set
10
+ # to an array.
12
11
  #
13
12
  # Related module: Sequel::Dataset::SetLiteralizer
14
13
 
15
- require 'set'
16
-
17
14
  module Sequel
15
+ # SEQUEL6: Remove
16
+ Sequel::Deprecation.deprecate("The set_literalizer extension", "Sequel now supports set literalization by default")
17
+
18
18
  class Dataset
19
19
  module SetLiteralizer
20
- # Try to generate the same SQL for Set instances used in datasets
21
- # that would be used for equivalent Array instances.
22
- def complex_expression_sql_append(sql, op, args)
23
- # Array instances are treated specially by
24
- # Sequel::SQL::BooleanExpression.from_value_pairs. That cannot
25
- # be modified by a dataset extension, so this tries to convert
26
- # the complex expression values generated by default to what would
27
- # be the complex expression values used for the equivalent array.
28
- case op
29
- when :'=', :'!='
30
- if (set = args[1]).is_a?(Set)
31
- op = op == :'=' ? :IN : :'NOT IN'
32
- col = args[0]
33
- array = set.to_a
34
- if Sequel.condition_specifier?(array) && col.is_a?(Array)
35
- array = Sequel.value_list(array)
36
- end
37
- args = [col, array]
38
- end
39
- end
40
-
41
- super
42
- end
43
-
44
20
  private
45
21
 
46
- # Literalize Set instances by converting the set to array.
47
- def literal_other_append(sql, v)
48
- if Set === v
49
- literal_append(sql, v.to_a)
22
+ # Allow using sets as condition specifiers.
23
+ def filter_expr(expr = nil, &block)
24
+ if expr.is_a?(Set)
25
+ expr
50
26
  else
51
27
  super
52
28
  end
53
29
  end
30
+
31
+ # Literalize Set instances by converting the set to array.
32
+ def literal_set_append(sql, v)
33
+ literal_append(sql, v.to_a)
34
+ end
54
35
  end
55
36
 
56
37
  register_extension(:set_literalizer, SetLiteralizer)
@@ -25,6 +25,11 @@
25
25
  # # with split_array_nils extension:
26
26
  # # SELECT * FROM table WHERE ((column NOT IN (1)) AND (column IS NOT NULL)))
27
27
  #
28
+ # In addition to arrays, this splitting is also done for sets:
29
+ #
30
+ # ds = DB[:table].where(column: Set[1, nil])
31
+ # # SELECT * FROM table WHERE ((column IN (1)) OR (column IS NULL)))
32
+ #
28
33
  # To use this extension with a single dataset:
29
34
  #
30
35
  # ds = ds.extension(:split_array_nil)
@@ -47,9 +52,14 @@ module Sequel
47
52
  case op
48
53
  when :IN, :"NOT IN"
49
54
  vals = args[1]
50
- if vals.is_a?(Array) && vals.any?(&:nil?)
55
+ if (vals.is_a?(Array) || vals.is_a?(Set)) && vals.any?(&:nil?)
51
56
  cols = args[0]
52
- vals = vals.compact
57
+ if vals.is_a?(Set)
58
+ vals = vals.dup
59
+ vals.delete(nil)
60
+ else
61
+ vals = vals.compact
62
+ end
53
63
  c = Sequel::SQL::BooleanExpression
54
64
  if op == :IN
55
65
  literal_append(sql, c.new(:OR, c.new(:IN, cols, vals), c.new(:IS, cols, nil)))
@@ -79,6 +79,11 @@ module Sequel
79
79
  e.each_with_index do |val, j|
80
80
  v(val, j)
81
81
  end
82
+ when Set
83
+ dot "Set"
84
+ e.each_with_index do |val, j|
85
+ v(val, j)
86
+ end
82
87
  when Hash
83
88
  dot "Hash"
84
89
  e.each do |k, val|
@@ -3544,7 +3544,7 @@ module Sequel
3544
3544
  else
3545
3545
  vals = Array(obj).reject{|o| !meths.all?{|m| o.get_column_value(m)}}
3546
3546
  return SQL::Constants::FALSE if vals.empty?
3547
- if obj.is_a?(Array)
3547
+ if obj.is_a?(Array) || obj.is_a?(Set)
3548
3548
  if keys.length == 1
3549
3549
  meth = meths.first
3550
3550
  {keys.first=>vals.map{|o| o.get_column_value(meth)}}
@@ -10,7 +10,7 @@ module Sequel
10
10
  # Class methods for Sequel::Model that implement basic model functionality.
11
11
  #
12
12
  # * All of the following methods have class methods created that send the method
13
- # to the model's dataset: all, any?, as_hash, avg, count, cross_join, distinct, each,
13
+ # to the model's dataset: all, any?, as_hash, as_set, avg, count, cross_join, distinct, each,
14
14
  # each_server, empty?, except, exclude, exclude_having, fetch_rows,
15
15
  # filter, first, first!, for_update, from, from_self, full_join, full_outer_join,
16
16
  # get, graph, grep, group, group_and_count, group_append, group_by, having, import,
@@ -19,7 +19,7 @@ module Sequel
19
19
  # natural_join, natural_left_join, natural_right_join, offset, order, order_append, order_by,
20
20
  # order_more, order_prepend, paged_each, qualify, reverse, reverse_order, right_join,
21
21
  # right_outer_join, select, select_all, select_append, select_group, select_hash,
22
- # select_hash_groups, select_map, select_more, select_order_map, select_prepend, server,
22
+ # select_hash_groups, select_map, select_more, select_order_map, select_prepend, select_set, server,
23
23
  # single_record, single_record!, single_value, single_value!, sum, to_hash, to_hash_groups,
24
24
  # truncate, unfiltered, ungraphed, ungrouped, union, unlimited, unordered, where, where_all,
25
25
  # where_each, where_single_value, with, with_recursive, with_sql
@@ -2344,5 +2344,17 @@ END
2344
2344
  # :nocov:
2345
2345
  singleton_class.send(:undef_method, :initialize_clone, :initialize_dup)
2346
2346
  end
2347
+
2348
+ # :nocov:
2349
+ if defined?(Sequel::Postgres::SEQUEL_PG_VERSION_INTEGER) && Sequel::Postgres::SEQUEL_PG_VERSION_INTEGER >= 11800
2350
+ # Automatically optimize model loading when sequel/core was loaded,
2351
+ # then sequel/adapters/postgres (with sequel_pg), then sequel/model
2352
+ begin
2353
+ require "sequel_pg/model"
2354
+ rescue LoadError
2355
+ # nothing
2356
+ end
2357
+ end
2358
+ # :nocov:
2347
2359
  end
2348
2360
  end
@@ -541,7 +541,7 @@ module Sequel
541
541
  if (assoc_pks = obj.get_column_value(key)) && !assoc_pks.empty?
542
542
  Sequel[pk=>assoc_pks.to_a]
543
543
  end
544
- when Array
544
+ when Array, Set
545
545
  if (assoc_pks = obj.map{|o| o.get_column_value(key)}.flatten.compact.uniq) && !assoc_pks.empty?
546
546
  Sequel[pk=>assoc_pks]
547
547
  end
@@ -563,7 +563,7 @@ module Sequel
563
563
  if pkv = obj.get_column_value(ref.primary_key_method)
564
564
  Sequel.pg_array_op(key).contains(Sequel.pg_array([pkv], ref.array_type))
565
565
  end
566
- when Array
566
+ when Array, Set
567
567
  if (pkvs = obj.map{|o| o.get_column_value(ref.primary_key_method)}.compact) && !pkvs.empty?
568
568
  Sequel.pg_array(key).overlaps(Sequel.pg_array(pkvs, ref.array_type))
569
569
  end
@@ -176,6 +176,19 @@ module Sequel
176
176
  h
177
177
  end
178
178
 
179
+ # Use the cache instead of a query to get the results.
180
+ def as_set(column)
181
+ set = Set.new
182
+
183
+ if column.is_a?(Array)
184
+ @all.each{|r| set.add(r.values.values_at(*column))}
185
+ else
186
+ @all.each{|r| set.add(r[column])}
187
+ end
188
+
189
+ set
190
+ end
191
+
179
192
  # Alias of as_hash for backwards compatibility.
180
193
  def to_hash(*a)
181
194
  as_hash(*a)
@@ -226,6 +226,21 @@ module Sequel
226
226
  h
227
227
  end
228
228
 
229
+ # Use the cache instead of a query to get the results.
230
+ def as_set(column)
231
+ return super unless all = @cache[:subset_static_cache_all]
232
+
233
+ set = Set.new
234
+
235
+ if column.is_a?(Array)
236
+ all.each{|r| set.add(r.values.values_at(*column))}
237
+ else
238
+ all.each{|r| set.add(r[column])}
239
+ end
240
+
241
+ set
242
+ end
243
+
229
244
  # Alias of as_hash for backwards compatibility.
230
245
  def to_hash(*a)
231
246
  as_hash(*a)
data/lib/sequel/sql.rb CHANGED
@@ -1108,7 +1108,7 @@ module Sequel
1108
1108
  else
1109
1109
  new(:'=', 1, 1)
1110
1110
  end
1111
- when ::Array
1111
+ when ::Array, ::Set
1112
1112
  r = r.dup.freeze unless r.frozen?
1113
1113
  new(:IN, l, r)
1114
1114
  when ::String
@@ -6,7 +6,7 @@ module Sequel
6
6
 
7
7
  # The minor version of Sequel. Bumped for every non-patch level
8
8
  # release, generally around once a month.
9
- MINOR = 98
9
+ MINOR = 99
10
10
 
11
11
  # The tiny version of Sequel. Usually 0, only bumped for bugfix
12
12
  # releases that fix regressions from previous versions.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.98.0
4
+ version: 5.99.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans