module Sequel::CoreRefinements

:nocov:

Constants

INCLUDE_METH

:nocov:

Public Instance Methods

&(ce) click to toggle source

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions in this hash and the condition specified by the given argument.

{a: 1} & :b # SQL: ((a = 1) AND b)
{a: true} & ~:b # SQL: ((a IS TRUE) AND NOT b)
    # File lib/sequel/extensions/core_refinements.rb
108 def &(ce)
109   ::Sequel::SQL::BooleanExpression.new(:AND, self, ce)
110 end
*(ce=(arg=false;nil)) click to toggle source
    # File lib/sequel/extensions/core_refinements.rb
216 def *(ce=(arg=false;nil))
217   if arg == false
218     Sequel::SQL::ColumnAll.new(self)
219   else
220     Sequel::SQL::NumericExpression.new(:*, self, ce)
221   end
222 end
case(*args) click to toggle source

Return a Sequel::SQL::CaseExpression with this array as the conditions and the given default value and expression.

[[{a: [2,3]}, 1]].case(0) # SQL: CASE WHEN (a IN (2, 3)) THEN 1 ELSE 0 END
[[:a, 1], [:b, 2]].case(:d, :c) # SQL: CASE c WHEN a THEN 1 WHEN b THEN 2 ELSE d END
   # File lib/sequel/extensions/core_refinements.rb
39 def case(*args)
40   ::Sequel::SQL::CaseExpression.new(self, *args)
41 end
hstore() click to toggle source
    # File lib/sequel/extensions/pg_hstore.rb
347 def hstore
348   Sequel::Postgres::HStore.new(self)
349 end
identifier() click to toggle source

Returns receiver wrapped in an Sequel::SQL::Identifier.

:ab.identifier # SQL: "a"
    # File lib/sequel/extensions/core_refinements.rb
230 def identifier
231   Sequel::SQL::Identifier.new(self)
232 end
lit(*args) click to toggle source

Converts a string into a Sequel::LiteralString, in order to override string literalization, e.g.:

DB[:items].where(abc: 'def')
# "SELECT * FROM items WHERE (abc = 'def')"

DB[:items].where(abc: 'def'.lit)
# "SELECT * FROM items WHERE (abc = def)"

You can also provide arguments, to create a Sequel::SQL::PlaceholderLiteralString:

DB[:items].select{|o| o.count('DISTINCT ?'.lit(:a))}
# "SELECT count(DISTINCT a) FROM items"
    # File lib/sequel/extensions/core_refinements.rb
186 def lit(*args)
187   args.empty? ? Sequel::LiteralString.new(self) : Sequel::SQL::PlaceholderLiteralString.new(self, args)
188 end
pg_array(type=nil) click to toggle source
    # File lib/sequel/extensions/pg_array.rb
550 def pg_array(type=nil)
551   Sequel::Postgres::PGArray.new(self, type)
552 end
pg_json() click to toggle source
    # File lib/sequel/extensions/pg_json.rb
624 def pg_json
625   Sequel::Postgres::JSONArray.new(self)
626 end
pg_jsonb() click to toggle source
    # File lib/sequel/extensions/pg_json.rb
628 def pg_jsonb
629   Sequel::Postgres::JSONBArray.new(self)
630 end
pg_range(db_type=nil) click to toggle source
    # File lib/sequel/extensions/pg_range.rb
554 def pg_range(db_type=nil)
555   Sequel::Postgres::PGRange.from_range(self, db_type)
556 end
pg_row() click to toggle source
    # File lib/sequel/extensions/pg_row.rb
577 def pg_row
578   Sequel::Postgres::PGRow::ArrayRow.new(self)
579 end
sql_expr() click to toggle source

Return a Sequel::SQL::BooleanExpression created from this array, matching all of the conditions. Rarely do you need to call this explicitly, as Sequel generally assumes that arrays of two element arrays specify this type of condition. One case where it can be necessary to use this is if you are using the object as a value in a filter hash and want to use the = operator instead of the IN operator (which is used by default for arrays of two element arrays).

[[:a, true]].sql_expr # SQL: (a IS TRUE)
[[:a, 1], [:b, [2, 3]]].sql_expr # SQL: ((a = 1) AND (b IN (2, 3)))
   # File lib/sequel/extensions/core_refinements.rb
65 def sql_expr
66   Sequel[self]
67 end
sql_function(*args) click to toggle source

Returns a Sequel::SQL::Function with this as the function name, and the given arguments.

:now.sql_function # SQL: now()
:sum.sql_function(:a) # SQL: sum(a)
:concat.sql_function(:a, :b) # SQL: concat(a, b)
    # File lib/sequel/extensions/core_refinements.rb
240 def sql_function(*args)
241   Sequel::SQL::Function.new(self, *args)
242 end
sql_negate() click to toggle source

Return a Sequel::SQL::BooleanExpression created from this array, matching none of the conditions.

[[:a, true]].sql_negate # SQL: (a IS NOT TRUE)
[[:a, 1], [:b, [2, 3]]].sql_negate # SQL: ((a != 1) AND (b NOT IN (2, 3)))
   # File lib/sequel/extensions/core_refinements.rb
74 def sql_negate
75   Sequel.negate(self)
76 end
sql_or() click to toggle source

Return a Sequel::SQL::BooleanExpression created from this array, matching any of the conditions.

[[:a, true]].sql_or # SQL: (a IS TRUE)
[[:a, 1], [:b, [2, 3]]].sql_or # SQL: ((a = 1) OR (b IN (2, 3)))
   # File lib/sequel/extensions/core_refinements.rb
83 def sql_or
84   Sequel.or(self)
85 end
sql_string_join(joiner=nil) click to toggle source

Return a Sequel::SQL::StringExpression representing an SQL string made up of the concatenation of this array’s elements. If an argument is passed it is used in between each element of the array in the SQL concatenation.

[:a].sql_string_join # SQL: a
[:a, :b].sql_string_join # SQL: (a || b)
[:a, 'b'].sql_string_join # SQL: (a || 'b')
['a', :b].sql_string_join(' ') # SQL: ('a' || ' ' || b)
   # File lib/sequel/extensions/core_refinements.rb
96 def sql_string_join(joiner=nil)
97   Sequel.join(self, joiner)
98 end
sql_value_list() click to toggle source

Return a Sequel::SQL::ValueList created from this array. Used if this array contains all two element arrays and you want it treated as an SQL value list (IN predicate) instead of as a conditions specifier (similar to a hash). This is not necessary if you are using this array as a value in a filter, but may be necessary if you are using it as a value with placeholder SQL:

DB[:a].where([:a, :b]=>[[1, 2], [3, 4]]) # SQL: ((a, b) IN ((1, 2), (3, 4)))
DB[:a].where('(a, b) IN ?', [[1, 2], [3, 4]]) # SQL: ((a, b) IN ((1 = 2) AND (3 = 4)))
DB[:a].where('(a, b) IN ?', [[1, 2], [3, 4]].sql_value_list) # SQL: ((a, b) IN ((1, 2), (3, 4)))
   # File lib/sequel/extensions/core_refinements.rb
52 def sql_value_list
53   ::Sequel::SQL::ValueList.new(self)
54 end
to_sequel_blob() click to toggle source

Returns a Sequel::SQL::Blob that holds the same data as this string. Blobs provide proper escaping of binary data.

    # File lib/sequel/extensions/core_refinements.rb
192 def to_sequel_blob
193   ::Sequel::SQL::Blob.new(self)
194 end
|(ce) click to toggle source

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions in this hash or the condition specified by the given argument.

{a: 1} | :b # SQL: ((a = 1) OR b)
{a: true} | ~:b # SQL: ((a IS TRUE) OR NOT b)
    # File lib/sequel/extensions/core_refinements.rb
118 def |(ce)
119   ::Sequel::SQL::BooleanExpression.new(:OR, self, ce)
120 end
~() click to toggle source

Return a Sequel::SQL::BooleanExpression created from this array, not matching all of the conditions.

~[[:a, true]] # SQL: (a IS NOT TRUE)
~[[:a, 1], [:b, [2, 3]]] # SQL: ((a != 1) OR (b NOT IN (2, 3)))
   # File lib/sequel/extensions/core_refinements.rb
30 def ~
31   Sequel.~(self)
32 end