Please sign in to comment.
Handle arrays of two element arrays as filter hash values automatically
Previously, Sequel's behavior of arrays of two element arrays as hash values was suboptimal (some would say broken). Because the values were an array, an IN operator was used, but because arrays of two element arrays are considered condition specifiers, the argument was converted to a boolean condition: DB[:a].filter([:a, :b]=>[[1, 2], [3, 4]]) # SELECT * FROM a WHERE ((a, b) IN ((1 = 2) AND (3 = 4))) This changes the behavior so that you now get the SQL you expect: SELECT * FROM a WHERE ((a, b) IN ((1, 2), (3, 4))) Previously, you had to call sql_array (which was added in Sequel 2.7.0) on the array to get the correct SQL, but that is no longer necessary when the array is a filter hash value. However, it is still necessary to use a method that wraps the array to two element arrays if you are going to use it as a placeholder value: DB[:a].filter('(a, b) IN ?', [[1, 2], [3, 4]]) This is because Sequel doesn't have the context to know that the array of two element arrays is being used as a an IN predicate (as Sequel does not parse SQL). This actually started when I decided that SQLArray was a bad name for the class, since arrays in standard SQL (starting with SQL99) are very different that what SQLArray actually represented, which was an IN predicate value list. To that end, SQLArray has been renamed to ValueList, though SQLArray still exists as an alias. Likewise Array#sql_value_list has been added, though Array#sql_array still exists as an alias to it. However, in most cases, the methods are no longer necessary, and Sequel internally no longer uses ValueList, as the only time it wants to deal with arrays of two element arrays as value lists, it uses them as hash values, which Sequel can now handle automatically. Another main difference is that ValueList now inherits from ::Array, not from Expression. This simplified some code, and since you can't really perform any operations on an SQL value list by itself (until it is used as part of an IN expression), there's no reason it needs to be an Expression subclass. While here, fix a minor bug in Expression#inspect.
- Loading branch information...
Showing with 66 additions and 51 deletions.
- +2 −0 CHANGELOG
- +1 −1 lib/sequel/core.rb
- +13 −9 lib/sequel/core_sql.rb
- +7 −6 lib/sequel/dataset/sql.rb
- +3 −3 lib/sequel/model/associations.rb
- +1 −1 lib/sequel/plugins/lazy_attributes.rb
- +1 −1 lib/sequel/plugins/many_through_many.rb
- +24 −23 lib/sequel/sql.rb
- +10 −4 spec/core/core_sql_spec.rb
- +4 −2 spec/core/dataset_spec.rb
- +0 −1 spec/extensions/lazy_attributes_spec.rb