lib/upsert/buffer/sqlite3_database.rb in upsert-0.0.1 vs lib/upsert/buffer/sqlite3_database.rb in upsert-0.1.0

- old
+ new

@@ -1,44 +1,46 @@ class Upsert class Buffer + # @private class SQLite3_Database < Buffer - def compose(targets) - target = targets.first - parts = [] - parts << %{ INSERT OR IGNORE INTO "#{table_name}" (#{quote_idents(target.columns)}) VALUES (#{quote_values(target.inserts)}) } - if target.updates.length > 0 - parts << %{ UPDATE "#{table_name}" SET #{quote_pairs(target.updates)} WHERE #{quote_pairs(target.selector)} } - end - parts.join(';') + include Quoter + + def chunk + return false if rows.empty? + row = rows.shift + %{ + INSERT OR IGNORE INTO "#{table_name}" (#{row.columns_sql}) VALUES (#{row.values_sql}); + UPDATE "#{table_name}" SET #{row.set_sql} WHERE #{row.where_sql} + } end def execute(sql) connection.execute_batch sql end - def max_targets - 1 + def quote_string(v) + SINGLE_QUOTE + SQLite3::Database.quote(v) + SINGLE_QUOTE end - def max_length - INFINITY + def quote_binary(v) + X_AND_SINGLE_QUOTE + v.unpack("H*")[0] + SINGLE_QUOTE end - include Quoter - - def quote_value(v) - case v - when NilClass - 'NULL' - when String, Symbol - SINGLE_QUOTE + SQLite3::Database.quote(v.to_s) + SINGLE_QUOTE - else - v - end + def quote_time(v) + quote_string [v.strftime(ISO8601_DATETIME), sprintf(USEC_SPRINTF, v.usec)].join('.') end def quote_ident(k) DOUBLE_QUOTE + SQLite3::Database.quote(k.to_s) + DOUBLE_QUOTE + end + + def quote_boolean(v) + s = v ? 't' : 'f' + quote_string s + end + + def quote_big_decimal(v) + v.to_f end end end end