On Oct 7, 12:24 pm, Jay Stotz <[email protected]> wrote:
> Thanks! That makes sense. I just wanted to make sure I wasn't missing
> anything obvious. For now I'll stick with the Thread-based approach
> since it's not performance critical and I'd rather not have to modify
> every query within the block.
>
> Would it be feasible to develop a Sequel plugin or feature that
> changes the default server within the scope of a block? I'd take a
> stab at it if you could point me in the right direction.
>
> Something like:
>
>   with_server(:fake) do
>     ...
>   end

People have asked for that in the past.  The implementation would be
ugly (but simple).  Something like (completely untested):

  class Sequel::Dataset
    private
    def default_server_opts(opts)
      {:server=>@opts[:server] || Thread.current[:sequel_server]
|| :default}.merge(opts)
    end
    def execute(sql, opts={}, &block)
      @db.execute(sql, {:server=>@opts[:server] ||
Thread.current[:sequel_server] || :read_only}.merge(opts), &block)
    end
  end
  class Sequel::Database
    def with_server(s)
      Thread.current[:sequel_server] = s
      yield
    ensure
      Thread.current[:sequel_server] = nil
    end
  end

I dislike abusing Thread.current, but we currently do so for both the
connection pool and transactions, so maybe this isn't so bad.  I still
think this is extension material, but I wouldn't be opposed to
refactoring a little to make the implementation of the extension
easier.

Does anyone else want this?

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to