On Mon, Mar 29, 2021 at 2:34 AM Denis Mushnin <[email protected]> wrote:

> cat test.rb
> require 'sequel'
> DB = Sequel.connect(adapter: 'postgres', host: 'localhost', database:
> 'test1', user: 'us1', password: 'pass')
>
> lf1 = 'f1'
>
> puts DB[:t1].select(:f1, :f2).where{ Sequel.lit(lf1) =~ nil }.sql
>
> puts DB[:t1].select(:f1, :f2).where{ f2 >= Sequel.lit(lf1) }.sql
>
> puts DB[:t1].select(:f1, :f2).where{ (f3 > f1) & ((Sequel.lit(lf1) =~ nil)
> | (f2 >= f1)) }.sql
>
> puts DB[:t1].select(:f1, :f2).where{ (f3 > f1) & ( (f2 >= f1) |
> (Sequel.lit(lf1) =~ nil) ) }.sql
>
> ruby test.rb
> SELECT "f1", "f2" FROM "t1" WHERE NULL
> SELECT "f1", "f2" FROM "t1" WHERE ("f2" >= f1)
> SELECT "f1", "f2" FROM "t1" WHERE (("f3" > "f1") AND true)
> SELECT "f1", "f2" FROM "t1" WHERE (("f3" > "f1") AND (("f2" >= "f1") OR
> NULL))
>

It's not a bug.  Sequel.lit creates a Sequel::LiteralString, which is a
String subclass, and does not override String#=~.  You should probably use
a hash in this case:

  puts DB[:t1].select(:f1, :f2).where(Sequel.lit(lf1) => nil).sql

Alternatively, you can make Sequel::LiteralString#=~ operate similar to
Sequel expressions instead of strings:

  Sequel::LiteralString.include Sequel::SQL::PatternMatchMethods

I can't do that by default without breaking backwards compatibility, and
doing so would prevent the ability to use =~ as you normally would on
strings, so I don't think it is a good idea.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSScsnxrd3q07KCdwHzDsaYmPn9YENOYcq6X6JVsaT6FFFA%40mail.gmail.com.
  • this is bug Denis Mushnin
    • Re: this is bug Jeremy Evans

Reply via email to