include/query_buffer.rb in baza-0.0.5 vs include/query_buffer.rb in baza-0.0.6
- old
+ new
@@ -45,41 +45,40 @@
#===Example
# buffer.update(:users, {:name => "Kasper"}, {:id => 5})
def update(table, update, terms)
STDOUT.puts "Update called on table #{table}." if @debug
self.query(@args[:db].update(table, update, terms, :return_sql => true))
- self.flush if @queries_count >= 1000
return nil
end
#Shortcut to doing upsert through the buffer instead of through the db-object with the buffer as an argument.
#===Example
# buffer.upsert(:users, {:id => 5}, {:name => "Kasper"})
def upsert(table, data, terms)
@args[:db].upsert(table, data, terms, :buffer => self)
- self.flush if @queries_count >= 1000
return nil
end
#Plans to inset a hash into a table. It will only be inserted when flush is called.
#===Examples
# buffer.insert(:users, {:name => "John Doe"})
def insert(table, data)
self.query(@args[:db].insert(table, data, :return_sql => true))
- self.flush if @queries_count >= 1000
return nil
end
#Flushes all queries out in a transaction. This will automatically be called for every 1000 queries.
def flush
return nil if @queries_count <= 0
@lock.synchronize do
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)
+ while !@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
end
end
@inserts.each do |table, datas_arr|
\ No newline at end of file