Class: Hash

Inherits:
Object show all
Defined in:
lib/sequel/extensions/core_extensions.rb,
lib/sequel/extensions/pg_json.rb,
lib/sequel/extensions/pg_hstore.rb

Overview

Sequel extends Hash to add methods to implement the SQL DSL.

Instance Method Summary collapse

Instance Method Details

#&(ce) ⇒ Object

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)

105
106
107
# File 'lib/sequel/extensions/core_extensions.rb', line 105

def &(ce)
  ::Sequel::SQL::BooleanExpression.new(:AND, self, ce)
end

#case(*args) ⇒ Object

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

{{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

133
134
135
# File 'lib/sequel/extensions/core_extensions.rb', line 133

def case(*args)
  ::Sequel::SQL::CaseExpression.new(to_a, *args)
end

#hstoreObject

Create a new HStore using the receiver as the input hash. Note that the HStore created will not use the receiver as the backing store, since it has to modify the hash. To get the new backing store, use:

hash.hstore.to_hash

317
318
319
# File 'lib/sequel/extensions/pg_hstore.rb', line 317

def hstore
  Sequel::Postgres::HStore.new(self)
end

#pg_jsonObject

Return a Sequel::Postgres::JSONHash proxy to the receiver. This is mostly useful as a short cut for creating JSONHash objects that didn't come from the database.


322
323
324
# File 'lib/sequel/extensions/pg_json.rb', line 322

def pg_json
  Sequel::Postgres::JSONHash.new(self)
end

#pg_jsonbObject

Return a Sequel::Postgres::JSONHash proxy to the receiver. This is mostly useful as a short cut for creating JSONHash objects that didn't come from the database.


329
330
331
# File 'lib/sequel/extensions/pg_json.rb', line 329

def pg_jsonb
  Sequel::Postgres::JSONBHash.new(self)
end

#sql_exprObject

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)))

143
144
145
# File 'lib/sequel/extensions/core_extensions.rb', line 143

def sql_expr
  ::Sequel::SQL::BooleanExpression.from_value_pairs(self)
end

#sql_negateObject

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)))

152
153
154
# File 'lib/sequel/extensions/core_extensions.rb', line 152

def sql_negate
  ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :AND, true)
end

#sql_orObject

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)))

161
162
163
# File 'lib/sequel/extensions/core_extensions.rb', line 161

def sql_or
  ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR)
end

#|(ce) ⇒ Object

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)

115
116
117
# File 'lib/sequel/extensions/core_extensions.rb', line 115

def |(ce)
  ::Sequel::SQL::BooleanExpression.new(:OR, self, ce)
end

#~Object

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)))

124
125
126
# File 'lib/sequel/extensions/core_extensions.rb', line 124

def ~
  ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR, true)
end