Class: MassInsert::Builder::Adapters::Adapter

Inherits:
Object
  • Object
show all
Includes:
Helpers::AbstractQuery
Defined in:
lib/mass_insert/builder/adapters/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::AbstractQuery

#begin_string, #execute, #string_columns, #string_rows_values, #string_single_row_values, #string_single_value, #string_values

Constructor Details

#initialize(values, options) ⇒ Adapter

Returns a new instance of Adapter.



9
10
11
12
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 9

def initialize values, options
  @values  = values
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Returns the options according to the method that wasn’t found.



15
16
17
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 15

def method_missing method, *args
  @options.has_key?(method) ? @options[method] : super
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 7

def options
  @options
end

#valuesObject

Returns the value of attribute values.



7
8
9
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 7

def values
  @values
end

Instance Method Details

#columnsObject

Returns an array according to the options with the column names that will be included in the queries.



21
22
23
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 21

def columns
  @columns ||= sanitized_columns
end

#sanitized_columnsObject

Prepare array with the column names according to the options.



26
27
28
29
30
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 26

def sanitized_columns
  columns = class_name.column_names
  columns.delete(class_name.primary_key) unless primary_key
  columns.map(&:to_sym)
end

#timestampObject

Returns the timestamp value according to the correct timestamp format to that database engine.



45
46
47
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 45

def timestamp
  Time.now.strftime(timestamp_format)
end

#timestamp?Boolean

Returns true o false if the database table has timestamp columns.

Returns:

  • (Boolean)


33
34
35
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 33

def timestamp?
  columns.include?(:created_at) && columns.include?(:updated_at)
end

#timestamp_formatObject

Returns timestamp format according to the database adapter. This function can be overwrite in database adapters classes.



39
40
41
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 39

def timestamp_format
  "%Y-%m-%d %H:%M:%S.%6N"
end

#timestamp_hashObject

Returns the timestamp hash to be merge into row values that will be saved in the database.



51
52
53
54
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 51

def timestamp_hash
  timestamp_value = timestamp
  {:created_at => timestamp_value, :updated_at => timestamp_value}
end

#values_per_insertionObject

Returns the amount of records to each query. Tries to take the each_slice option value or the length of values.



58
59
60
# File 'lib/mass_insert/builder/adapters/adapter.rb', line 58

def values_per_insertion
  each_slice || @values.count
end