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, line 98 98: def &(ce) 99: ::Sequel::SQL::BooleanExpression.new(:AND, self, ce) 100: end
Return a Sequel::SQL::CaseExpression with this hash as the conditions and the given default value. Note that the order of the conditions will be arbitrary on ruby 1.8, so all conditions should be orthogonal.
{{: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 # or: CASE c WHEN b THEN 2 WHEN a THEN 1 ELSE d END
# File lib/sequel/extensions/core_refinements.rb, line 128 128: def case(*args) 129: ::Sequel::SQL::CaseExpression.new(to_a, *args) 130: end
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, line 29 29: def case(*args) 30: ::Sequel::SQL::CaseExpression.new(self, *args) 31: end
# File lib/sequel/extensions/pg_hstore.rb, line 348 348: def hstore 349: Sequel::Postgres::HStore.new(self) 350: end
Returns receiver wrapped in an Sequel::SQL::Identifier. Usually used to prevent splitting the symbol.
:a__b # SQL: "a"."b" :a__b.identifier # SQL: "a__b"
# File lib/sequel/extensions/core_refinements.rb, line 205 205: def identifier 206: Sequel::SQL::Identifier.new(self) 207: end
Converts a string into a Sequel::LiteralString, in order to override string literalization, e.g.:
DB[:items].filter(:abc => 'def').sql #=> "SELECT * FROM items WHERE (abc = 'def')" DB[:items].filter(:abc => 'def'.lit).sql #=> "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))}.sql #=> "SELECT count(DISTINCT a) FROM items"
# File lib/sequel/extensions/core_refinements.rb, line 178 178: def lit(*args) 179: args.empty? ? Sequel::LiteralString.new(self) : Sequel::SQL::PlaceholderLiteralString.new(self, args) 180: end
# File lib/sequel/extensions/pg_array.rb, line 610 610: def pg_array(type=nil) 611: Sequel::Postgres::PGArray.new(self, type) 612: end
# File lib/sequel/extensions/pg_json.rb, line 242 242: def pg_json 243: Sequel::Postgres::JSONHash.new(self) 244: end
# File lib/sequel/extensions/pg_json.rb, line 236 236: def pg_json 237: Sequel::Postgres::JSONArray.new(self) 238: end
# File lib/sequel/extensions/pg_range.rb, line 535 535: def pg_range(db_type=nil) 536: Sequel::Postgres::PGRange.from_range(self, db_type) 537: end
# File lib/sequel/extensions/pg_row.rb, line 605 605: def pg_row 606: Sequel::Postgres::PGRow::ArrayRow.new(self) 607: end
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, line 55 55: def sql_expr 56: Sequel.expr(self) 57: end
Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions. Rarely do you need to call this explicitly, as Sequel generally assumes that hashes specify this type of condition.
{: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, line 138 138: def sql_expr 139: ::Sequel::SQL::BooleanExpression.from_value_pairs(self) 140: end
Returns a Sequel::SQL::Function with this as the function name, and the given arguments. This is aliased as Symbol#[] if the RUBY_VERSION is less than 1.9.0. Ruby 1.9 defines Symbol#[], and Sequel doesn‘t override methods defined by ruby itself.
: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, line 217 217: def sql_function(*args) 218: Sequel::SQL::Function.new(self, *args) 219: end
Return a Sequel::SQL::BooleanExpression created from this hash, 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, line 147 147: def sql_negate 148: ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :AND, true) 149: end
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, line 64 64: def sql_negate 65: Sequel.negate(self) 66: end
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, line 73 73: def sql_or 74: Sequel.or(self) 75: end
Return a Sequel::SQL::BooleanExpression created from this hash, 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, line 156 156: def sql_or 157: ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR) 158: end
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, line 86 86: def sql_string_join(joiner=nil) 87: Sequel.join(self, joiner) 88: end
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].filter([:a, :b]=>[[1, 2], [3, 4]]) # SQL: (a, b) IN ((1, 2), (3, 4)) DB[:a].filter('(a, b) IN ?', [[1, 2], [3, 4]]) # SQL: (a, b) IN ((1 = 2) AND (3 = 4)) DB[:a].filter('(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, line 42 42: def sql_value_list 43: ::Sequel::SQL::ValueList.new(self) 44: end
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, line 184 184: def to_sequel_blob 185: ::Sequel::SQL::Blob.new(self) 186: end
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, line 108 108: def |(ce) 109: ::Sequel::SQL::BooleanExpression.new(:OR, self, ce) 110: end
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, line 20 20: def ~ 21: Sequel.~(self) 22: end
Return a Sequel::SQL::BooleanExpression created from this hash, 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, line 117 117: def ~ 118: ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR, true) 119: end