Path: | doc/release_notes/4.21.0.txt |
Last Update: | Sun Oct 02 21:06:53 +0000 2016 |
DB[:items].where{{function(:column)=>0}} # SELECT FROM items WHERE function(column) = 0
You can now use =~ as an equivalent:
DB[:items].where{function(:column) =~ 0} # SELECT FROM items WHERE function(column) = 0
Like when using a hash, this works also for inclusion:
DB[:items].where{function(:column) =~ [1,2,3]} # SELECT FROM items WHERE function(column) IN (1, 2, 3)
for identity:
DB[:items].where{function(:column) =~ nil} # SELECT FROM items WHERE function(column) IS NULL
and for matching (on MySQL/PostgreSQL):
DB[:items].where{function(:column) =~ /foo/i} # SELECT FROM items WHERE function(column) ~* 'foo'
This new syntax makes more complex conditions simpler to express:
DB[:items].where{(function(:column) =~ 0) | (column =~ 1)} # SELECT FROM items WHERE function(column) = 0 OR column = 1
compared to previous versions of Sequel:
DB[:items].where{Sequel.|({function(:column) => 0}, {:column => 1})}
On ruby 1.9+, you can also use SQL::GenericExpression#!~ to invert the condition:
DB[:items].where{function(:column) !~ 0} # SELECT FROM items WHERE function(column) != 0 DB[:items].where{function(:column) !~ [1,2,3]} # SELECT FROM items WHERE function(column) NOT IN (1, 2, 3) DB[:items].where{function(:column) !~ nil} # SELECT FROM items WHERE function(column) IS NOT NULL DB[:items].where{function(:column) !~ /foo/i} # SELECT FROM items WHERE function(column) !~* 'foo'
This makes it simpler to write inverted conditions. Ruby 1.8 doesn‘t support overriding the !~ method, but you can still use the unary ~ method to invert:
DB[:items].where{~(function(:column) =~ 0)}
DB.add_named_conversion_proc(:citext){|s| s}