Method: Zena::Use::QueryNode::Compiler#process_equal

Defined in:
lib/zena/use/query_node.rb

#process_equal(left, right, is_not = nil) ⇒ Object

Handle special case for ‘class = ’ and ‘role = ’ and ‘foo.date =’



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/zena/use/query_node.rb', line 298

def process_equal(left, right, is_not = nil)
  if (left == [:field, 'class'] || left == [:field, 'klass']) && (right[0] == :field || right[0] == :string)
    if klass = Node.get_class(right.last)
      "#{field_or_attr('kpath')} #{is_not ? '<>' : '='} #{quote(klass.kpath)}"
    else
      raise ::QueryBuilder::Error.new("Unknown class #{right.last.inspect}.")
    end
  elsif left == [:field, 'role'] && (right[0] == :field || right[0] == :string)
    if role = Node.get_role(right[1])
      # FIXME: how to only add table once if the other clause in not an OR ?
      add_table('nodes_roles')
      "(#{table('nodes_roles')}.node_id = #{table('nodes')}.id AND #{table('nodes_roles')}.role_id = #{role.id})"
    end
  elsif left.first == :function && left.last.last == 'date'
    # transform "foo.date = baz"
    # [:function, [:field, "foo"], [:method, "date"]]
    # [:field, baz]
    # ==> into
    # "baz >= foo and foo < baz + 1 day"
    a = left[1]
    b = right
    process([:and, [:<=, b, a], [:<, a, [:+, b, [:interval, [:integer, '1'], 'day']]]])
  else
    process_op(:'=', left, right)
  end
end