Module: SQB::Filtering
Instance Method Summary collapse
-
#or(&block) ⇒ Object
Set that all conditions added in this block should be joined using OR rather than AND.
-
#where(hash = nil, &block) ⇒ Query
Add a condition to the query by providing a hash of keys and values.
Instance Method Details
#or(&block) ⇒ Object
Set that all conditions added in this block should be joined using OR rather than AND.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sqb/filtering.rb', line 30 def or(&block) if @where_within_or.is_a?(Array) raise QueryError, "Cannot nest an or block within another or block" end @where_within_or ||= [] # Start by making an array within the OR block for this calling @where_within_or << [] # Execute the block. All queries to 'where' will be added to the last # array in the chain (created above) block.call ensure # Start work on a full array of SQL fragments for all OR queries @where_within_or_sql ||= [] # After each OR call, store up the SQL fragment for all where queries # executed within the block. if w = @where_within_or.pop @where_within_or_sql << w.map do |w| hash_to_sql(w) end.join(' OR ') end # When there are no fragments in the chain left, add it to the main # where chain for the query. if @where_within_or.empty? @where ||= [] @where << "(#{@where_within_or_sql.flatten.join(' OR ')})" @where_within_or_sql = nil end self end |
#where(hash = nil, &block) ⇒ Query
Add a condition to the query by providing a hash of keys and values.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sqb/filtering.rb', line 10 def where(hash = nil, &block) if hash if @where_within_or && @where_within_or.last @where_within_or.last << hash else @where ||= [] @where << hash_to_sql(hash) end elsif block_given? dsl = WhereDSL.new block.call(dsl) where(dsl.hash) else raise QueryError, "Must provide a hash or a block to `where`" end self end |