Sha256: 33f9eac8a097a077fb2b75406cee6ff3c8d1ae45118a0fcd5a208c554c089187

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module MassInsert
  module Adapters
    class SQLite3Adapter < Adapter

      MAX_VALUES_PER_INSERTION = 500

      # This method is overwrite because the query string to the Sqlite3
      # adapter is different. Then the method in the AbstractQuery module
      # is ignored.
      def string_values
        "SELECT #{string_rows_values};"
      end

      # This method is overwrite because the query string to complete the
      # string rows values is different. The separator to sqlite adapter is
      # 'UNION SELECT' instead of '), (' in other sql adapters.
      def string_rows_values
        values.map{ |row| string_single_row_values(row) }.join(" UNION SELECT ")
      end

      # This functions calls the necessary functions to create a complete
      # sqlite3 query to multiple insertion. The methods are in the Abstract
      # Query module. If some method is too specific to this database adapter
      # you can overwrite it. The values that the user gave will be treated
      # in batches of 500 items because sqlite database allows by default
      # batches of 500.and each batch will generate a query. This method will
      # generate an array with batch queries.
      def execute
        @values.each_slice(MAX_VALUES_PER_INSERTION).map do |slice|
          @values = slice
          super
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mass_insert-0.1.1 lib/mass_insert/adapters/sqlite3_adapter.rb
mass_insert-0.1.0 lib/mass_insert/adapters/sqlite3_adapter.rb
mass_insert-0.0.4 lib/mass_insert/adapters/sqlite3_adapter.rb