Sha256: 5b61349111254320779b400501a2c5c673dadd9cd40bc6caec668b25443e2cac

Contents?: true

Size: 990 Bytes

Versions: 11

Compression:

Stored size: 990 Bytes

Contents

#This class buffers a lot of queries and flushes them out via transactions.
class Knj::Db::Query_buffer
  #Constructor. Takes arguments to be used and a block.
  def initialize(args)
    @args = args
    @queries = []
    @debug = @args[:debug]
    
    begin
      yield(self)
    ensure
      self.flush if !@queries.empty?
    end
  end
  
  #Adds a query to the buffer.
  def query(str)
    STDOUT.print "Adding to buffer: #{str}\n" if @debug
    @queries << str
    self.flush if @queries.length > 1000
    return nil
  end
  
  #Delete as on a normal Knj::Db.
  def delete(table, where)
    self.query(@args[:db].delete(table, where, :return_sql => true))
    return nil
  end
  
  #Flushes all queries out in a transaction.
  def flush
    return nil if @queries.empty?
    
    @args[:db].transaction do
      @queries.shift(1000).each do |str|
        STDOUT.print "Executing via buffer: #{str}\n" if @debug
        @args[:db].q(str)
      end
    end
    
    return nil
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
knjrbfw-0.0.50 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.49 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.48 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.47 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.46 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.45 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.44 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.43 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.42 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.41 lib/knj/knjdb/query_buffer.rb
knjrbfw-0.0.40 lib/knj/knjdb/query_buffer.rb