Sha256: e2cacdb25490771dff929dabccec6acc46da695aad26c2727a8e88f763b17789

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

module MassInsert
  module Adapters
    module AdapterHelpers
      module AbstractQuery

        # Returns a begin string to a basic mysql query insertion. Include
        # the class table_name and it's included in the string.
        def begin_string
          "INSERT INTO #{table_name} "
        end

        # Returns a string  with the column names to the class table name
        # and divided by commas.
        def string_columns
          "(#{columns.join(", ")}) "
        end

        # Returns the string with all the row values that will be included
        # in the sql string.
        def string_values
          "VALUES (#{string_rows_values});"
        end

        # Gives the correct format to the values string to all rows. This
        # functions calls a function that will generate a single string row
        # and at the end all the strings are concatenated.
        def string_rows_values
          values.map{ |row| string_single_row_values(row) }.join("), (")
        end

        # Returns the row column values string to be added in query string
        # according to the type column and values.
        # Before that row is prepared with the correct values.
        def string_single_row_values row
          row.merge!(timestamp_hash) if timestamp?
          columns.map{ |col| string_single_value(row, col) }.join(", ")
        end

        # Returns a single column string value with the correct format and
        # according to the database configuration, column type and presence.
        def string_single_value row, column
          ColumnValue.new(row, column, class_name).build
        end

        # This functions calls the necessary functions to create a complete
        # mysql query to multiple insertion. The methods are in the Abstract
        # Sql String module. If some method is too specific to this database
        # adapter you can overwrite it.
        def execute
          "#{begin_string}#{string_columns}#{string_values}"
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mass_insert-0.1.1 lib/mass_insert/adapters/adapter_helpers/abstract_query.rb
mass_insert-0.1.0 lib/mass_insert/adapters/adapter_helpers/abstract_query.rb