0 Replies Latest reply on Jul 15, 2013 7:29 AM by Gerbrand Stap

    Issues with Rhom conditions

    Gerbrand Stap

      While combining some conditions in Rhom methods, I found some issues with how these conditions are handled.

       

      First of all there are two ways to specify condition in which some field has to be equal to a value:

      Item.find( :all, :conditions => { :some_field => 1 } )
      Item.find( :all, :conditions => { { :name => :some_field, :op => '=' } => 2 } )
      

      These result in the following queries (I've uncommented a 'puts' statement in rhom_db_adapter.rb):

      SELECT * FROM Item WHERE "some_field"=?; [[1]]
      SELECT * FROM Item WHERE some_field = ?; [[2]]
      

      It's obvious that the conditions are handled in different ways, because in the first query the field name is escaped. Shouldn't this be done always, in case a column has a reserved name like 'order'?

       

      These conditions can be combined:

      Item.find( :all, :conditions => { :some_field=> 3, :another_field=> 4 } )
      Item.find( :all, :conditions => { { :name => :some_field, :op => '=' } => 5, { :name => :another_field, :op => '=' } => 6 } )
      

      Which results in the following queries:

      SELECT * FROM Item WHERE "some_field"=? AND "another_field"=?; [[3, 4]]
      SELECT * FROM Item WHERE some_field = ? AND another_field = ?; [[5, 6]]
      

      The same problem here with the escaping, but it works.

       

      But if these combinations are mixed:

      Item.find( :all, :conditions => { :some_field=> 7, { :name => :another_field, :op => '=' } => 8 } )
      Item.find( :all, :conditions => { { :name => :some_field, :op => '=' } => 9, :another_field=> 10 } )
      

      Then suddenly the form using :name and :op becomes stronger, resulting in:

      SELECT * FROM Item WHERE another_field = ?; [[8]]
      SELECT * FROM Item WHERE some_field = ?; [[9]]
      

      This seems like a bug, or is mixing the different forms not supported? It is never shown in the documentation, but it seems to me that this shouldn't make any difference.

       

      It even gets weirder when using one field multiple times in conditions:

      Item.find( :all, :conditions => { { :name => :some_field, :op => '=' } => 1, { :name => :another_field, :op => '=' } => 2 }, :op => 'OR' )
      Item.find( :all, :conditions => { { :name => :some_field, :op => '=' } => 1, { :name => :some_field, :op => '=' } => 2 }, :op => 'OR' )
      

      The statements produce the following queries:

      SELECT * FROM Item WHERE some_field = ? OR another_field = ?; [[1, 2]]
      SELECT * FROM Item WHERE some_field = ?; [[2]]
      

      When using two different fields, the query is correct. But when one field is used more often, only the last condition is preserved.

       

      A summary of the bugs:

      • Column names are not consequently escaped.
      • Condition forms can not be mixed.
      • One field can not be used multiple times in conditions.

       

      Posted this issue on GitHub.