您可以将选择规则存储在哈希中并重用哈希:
require 'sequel'
DB = Sequel.sqlite
selection = { a: 1, b: 2 }
ds1 = DB[:x].where( selection )
selection[:a] = 5 #change data
ds2a = ds1.unfiltered.filter(selection) # change selection
ds2b = DB[:x].where( selection ) #Alternative usage
puts ds1.sql #SELECT * FROM `x` WHERE ((`a` = 1) AND (`b` = 2))
puts ds2a.sql #SELECT * FROM `x` WHERE ((`a` = 5) AND (`b` = 2))
puts ds2b.sql #SELECT * FROM `x` WHERE ((`a` = 5) AND (`b` = 2))
我发现可以使用新方法filter_exchange
和filter_exchange!
.
require 'sequel'
DB = Sequel.sqlite
module Sequel
class Dataset
def filter_exchange!( filter )
filter.each{|key, value|
opts[:where].args.each{|arg|
if arg.args.first == key
arg.args.pop
arg.args << value
end
}
}
self
end
def filter_exchange( filter )
sel = self.unfiltered
filter.each{|key, value|
opts[:where].args.each{|arg|
if arg.args.first == key
sel = sel.filter(key => value)
else
sel = sel.filter(arg)
end
}
}
sel
end
end
end
ds1 = DB[:x].filter(a:1, b:2)
puts ds1.sql #SELECT * FROM `x` WHERE ((`a` = 1) AND (`b` = 2))
puts ds1.filter_exchange( a: 7).sql #SELECT * FROM `x` WHERE ((`a` = 7) AND (`b` = 2)
puts ds1.sql #SELECT * FROM `x` WHERE ((`a` = 1) AND (`b` = 2)
puts ds1.filter_exchange!( a: 7).sql#SELECT * FROM `x` WHERE ((`a` = 7) AND (`b` = 2)
puts ds1.sql #SELECT * FROM `x` WHERE ((`a` = 7) AND (`b` = 2)
filter_exchange!
修改您的原始数据集命令!即使是 ads.dup.filter_exchange!
也会改变你原来的选择。
与filter_exchange
您一起获得修改后的数据集。(到目前为止还没有经过很好的测试)。