lib/upsert/buffer.rb in upsert-0.0.1 vs lib/upsert/buffer.rb in upsert-0.1.0
- old
+ new
@@ -1,13 +1,26 @@
class Upsert
+ # @private
class Buffer
class << self
def for(connection, table_name)
+ if connection.respond_to?(:raw_connection)
+ # deal with ActiveRecord::Base.connection or ActiveRecord::Base.connection_pool.checkout
+ connection = connection.raw_connection
+ end
const_get(connection.class.name.gsub(/\W+/, '_')).new connection, table_name
end
end
+ SINGLE_QUOTE = %{'}
+ DOUBLE_QUOTE = %{"}
+ BACKTICK = %{`}
+ E_AND_SINGLE_QUOTE = %{E'}
+ X_AND_SINGLE_QUOTE = %{x'}
+ USEC_SPRINTF = '%06d'
+ ISO8601_DATETIME = '%Y-%m-%d %H:%M:%S' #FIXME ignores timezones i think
+
attr_reader :connection
attr_reader :table_name
attr_reader :rows
attr_writer :async
@@ -20,39 +33,18 @@
def async?
!!@async
end
def add(selector, document)
- rows << Row.new(selector, document)
+ rows << Row.new(self, selector, document)
if sql = chunk
execute sql
end
end
def clear
while sql = chunk
execute sql
end
- end
-
- def chunk
- return if rows.empty?
- targets = []
- sql = nil
- begin
- targets << rows.pop
- last_sql = sql
- sql = compose(targets)
- end until rows.empty? or targets.length >= max_targets or sql.length > max_length
- if sql.length > max_length
- raise if last_sql.nil?
- sql = last_sql
- rows << targets.pop
- end
- sql
- end
-
- def cleanup
- clear
end
end
end