On Dec 7, 2:23 pm, Aman Gupta <[EMAIL PROTECTED]> wrote:
> > You may be able to make this even nicer:
>
> >   class Sequel::MySQL::Dataset
> >     def execute(sql)
> >       EventedMysql.select(sql){|r| yield r}
> >     end
> >   end
> >   User.where(:age > 10).limit(10).each{|user| p user.age}
>
> > Not sure if that would work or not, but it would make the code a lot
> > nicer.  You'll obviously need to handle deletes, inserts and updates
> > differently, you can override execute_dui or another method for those.
>
> This would work, but it tends to break existing code since the query
> is no longer blocking. Any code following the #each will be executed
> immediately, even before the query's results are returned and looped
> over.
>
> I'm using a simple monkey-patch (with sequel 2.1.0) to add an
> additional async api to Dataset and 
> Model:http://github.com/tmm1/em-mysql/tree/master/lib/sequel/async.rb
>
>   DB[:table].where(:id < 100).async_update do |num_updated|
>     p "done updating #{num_updated} rows"
>   end
>
>   User.async_each do |user|
>     user.async_update(:age => rand(100)){ p "#{user.name}'s age set to
> #{user.age}" }
>   end
>
>   p "this will be printed out before either query is completed"

This may be a nicer way to do that sort of thing:

  class Sequel::MySQL::Dataset
    def async(v=true)
      clone(:async=>v)
    end
    private
    def execute(sql, opts={}, &block)
      opts[:async] ? ADB.select(sql, &block) : super(sql,
{:type=>:select}.merge(opts), &block)
    end
  end

That way, you could choose which datasets to load asynchronously and
which to load synchronously.  Similar things could be done for insert,
update, and delete.  This is how sharding is supported in Sequel, via
Dataset#server.

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