On Thursday, May 14, 2020 at 10:40:20 AM UTC-7, Troex Nevelin wrote:
>
> Hi Jeremy,
>
> We are heavily using Sequel in PostgreSQL (dev/staging) / Redshift (prod) 
> environment, so sometimes we have to keep adapter specific code, and 
> recently some expressions where added like this:
>
> module Sequel
>   # a NULL-safe "not equal" comparison condition
>   # (
>   #   (a IS NULL AND b IS NOT NULL) OR
>   #   (a IS NOT NULL AND b IS NULL) OR
>   #   (a <> b)
>   # )
>   def self.is_distinct_from(a, b)
>     if defined?(DB) == 'constant' && DB.adapter_scheme == :redshift
>       (Sequel[a] =~ nil) & (Sequel[b] !~ nil) |
>       (Sequel[a] !~ nil) & (Sequel[b] =~ nil) |
>       (Sequel[a] !~ Sequel[b])
>     else
>       Sequel.lit('? IS DISTINCT FROM ?', a, b)
>     end
>   end
>
>   module Timezones
>     def timezone_fn(timezone, field)
>       fn = if defined?(DB) == 'constant' && DB.adapter_scheme == :redshift
>         'CONVERT_TIMEZONE'
>       else
>         'timezone'
>       end
>
>       Sequel.lit("#{fn}(?, ?)", timezone, field)
>     end
>   end
> end
>
> It works, but it's not very nice momkey-patching, can point me to some 
> example in the code - the way it should be done properly?
> I tried to add it into SQL Builders module but it didn't work for me, 
> probably I missed something.
>

You should have is_distinct_from return an object that implements 
sql_literal_append(dataset, sql).  You can then use 
"dataset.db.adapter_scheme == :redshift" to check for Redshift.  You would 
then append the appropriate SQL code to the given string (sql).  I think 
the same approach could be used for timezone_fn.

We keeping a fork of "working for us" redshift adapter for sequel here 
> https://github.com/attribution/sequel-redshift 
> <https://github.com/attribution/sequel-redshift/commit/8d2828706970c6fd7661909315b5eecde9759bb7#diff-05c70fb8a8093e49de9c56c14213044cR125>
>
> Btw had to copy whole class here just to add ":redshift" to the case 
> statement:
>
> https://github.com/attribution/sequel-redshift/commit/8d2828706970c6fd7661909315b5eecde9759bb7#diff-05c70fb8a8093e49de9c56c14213044cR125
>

If you look at the top of the function, you'll see it does:

  if defined?(super)
    return super
  end

So you just need to override Dataset#string_agg_sql_append in your redshift 
adapter and the string_agg extension will call it automatically.

Thanks for your hard work on sequel, our new devs hate it at first, but 
> couple months later they can't live without it anymore ;) Great gem!
>

That's good to hear.  I would be great if they didn't hate it at first. :)

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/2c431fa6-6c74-46d5-b127-5ea8682ca68e%40googlegroups.com.

Reply via email to