Sha256: 6449f20410d6fea1c675070d4251751f76c87a6a64168346b6488a32b90c6ebe

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module MassInsert
  module Adapters
    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 commmas.
      def string_columns
        "(#{column_names.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

      def string_single_row_values row
        # Prepare the single row to be included in the sql string.
        row.merge!(timestamp_values) if timestamp?

        # Generates the values to this row that will be included according
        # to the type column and values.
        column_names.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, options).build
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mass_insert-0.0.2 lib/mass_insert/adapters/abstract_query.rb
mass_insert-0.0.1 lib/mass_insert/adapters/abstract_query.rb