Sequel gem increment

2019-07-27 10:06发布

问题:

I am trying to use the Ruby Sequel gem for DB operations.

I am stuck for incrementing and decrementing values.

The doc says that this should work, even though it seems very strange for me to be able to add a number and a symbol.

2.0.0-p247 :019 > require 'sequel'
 => true 
2.0.0-p247 :020 > s = Sequel.connect('sqlite://db.sqlite')
 => #<Sequel::SQLite::Database: "sqlite://db.sqlite"> 
2.0.0-p247 :021 > s[:query_volume].update_sql(:queries =>  3)
 => "UPDATE `query_volume` SET `queries` = 3" 
2.0.0-p247 :022 > s[:query_volume].update_sql(:queries => :queries + 3)
NoMethodError: undefined method `+' for :queries:Symbol
    from (irb):21
    from /Users/avandra/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'

But as you can see it gives undefined method on the queries symbol. Which is kindof concurs with why it was strange for me.

I tried using curly braces, but that gives another error:

2.0.0-p247 :023 > s[:query_volume].update_sql{:queries => :queries + 3}
SyntaxError: (irb):23: syntax error, unexpected =>, expecting '}'
s[:query_volume].update_sql{:queries => :queries + 3}
                                       ^
        from /Users/avandra/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'

And using

2.0.0-p247 :033 >   s[:query_volume].update_sql{queries = queries + 3}
 => "UPDATE `query_volume` SET " 

just gives a badly formatted SQL...

Could anyone shed some light on how this can be done?

回答1:

You should use Sequel.expr for that:

s[:query_volume].update_sql(:queries => Sequel.expr(3) + :queries)