Module: MassInsert::Builder::Adapters::Helpers::AbstractQuery

Included in:
Adapter
Defined in:
lib/mass_insert/builder/adapters/helpers/abstract_query.rb

Instance Method Summary collapse

Instance Method Details

#begin_stringObject

Returns a basic beginning of the query.



8
9
10
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 8

def begin_string
  "INSERT INTO #{class_name.table_name} "
end

#executeObject

Values will be treated in batches according to the values per insertion value. It’ll generate an array with queries.



41
42
43
44
45
46
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 41

def execute
  @values.each_slice(values_per_insertion).map do |slice|
    @values = slice;
    "#{begin_string}#{string_columns}#{string_values}"
  end
end

#string_columnsObject

Returns a basic part of the query with the columns definition.



13
14
15
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 13

def string_columns
  "(#{columns.join(", ")}) "
end

#string_rows_valuesObject

Returns all the column values to all the records



23
24
25
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 23

def string_rows_values
  values.map{ |row| string_single_row_values(row) }.join("), (")
end

#string_single_row_values(row) ⇒ Object

Returns all the column values to a single record.



28
29
30
31
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 28

def string_single_row_values row
  row.merge!(timestamp_hash) if timestamp?
  columns.map{ |col| string_single_value(row, col) }.join(", ")
end

#string_single_value(row, column) ⇒ Object

Returns a single column value. According to the database configuration, column type and presence.



35
36
37
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 35

def string_single_value row, column
  ColumnValue.new(row, column, class_name).build
end

#string_valuesObject

Returns a basic part of the query with all the records values.



18
19
20
# File 'lib/mass_insert/builder/adapters/helpers/abstract_query.rb', line 18

def string_values
  "VALUES (#{string_rows_values});"
end