In my SQLite-DB I want to select all entries on a monday.
How can I do it with Sequel?
A test example:
require 'sequel'
DB = Sequel.sqlite()#'test.db')
DB.create_table(:days) do
String :text
Date :start
end
Date.today.upto(Date.today + 30){|d|
DB[:days].insert( :text => d.strftime("%Y-%m-%d day %w in week %W"), :start => d)
}
How can I select all mondays?
In native SQL I can do:
select * from days where strftime("%w", start) = "1"
Using this, I can define a view and select for it:
DB.run('create view mondays as select * from days where strftime("%w", start) = "1"')
p DB[:mondays].all
But I would like to use it from Sequel.
I tried
sel = DB[:days].filter{ start.strftime("%w", :start) == '1' }
#NoMethodError
sel = DB[:days].filter{ Sequel::SQL::Function.new(:strftime, "%w", :start) == '1' }
#SELECT * FROM `days` WHERE (1 = 0)
but without success.
Are there any other solutions?
I’m looking also for a possibility to select items by hour of day (all items with a timestamp, and then from 12:00-13:00 …) I think this is the same problem and a solution for the day of week-selection will also solve my other problem.
Here are the problems with your examples:
This uses a virtual row, so calling
startreturns aSequel::SQL::Identifier. TheSequel::SQL::Identifier#strftimemethod does not exist, hence the NoMethodError.This fails because you are using
==. Sequel doesn’t override==, and ruby’s default==returnsfalse. On SQLite, Sequel representsfalseas(1 = 0).This should work:
This uses a hash (the inner {}) inside a virtual row block (the outer {}). In Sequel, equality is represented by hashes.