Class: InsertSelect::ActiveRecord::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/insert_select/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(insert_select_from) ⇒ Builder



6
7
8
9
10
11
12
13
14
# File 'lib/insert_select/builder.rb', line 6

def initialize(insert_select_from)
  @insert_select_from = insert_select_from
  @connection = insert_select_from.connection
  @relation = insert_select_from.relation.all
  @mapping = insert_select_from.mapping || {}
  @model = insert_select_from.model
  @returning = insert_select_from.returning
  @on_duplicate = insert_select_from.on_duplicate
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def connection
  @connection
end

#insert_select_fromObject (readonly)

Returns the value of attribute insert_select_from.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def insert_select_from
  @insert_select_from
end

#mappingObject (readonly)

Returns the value of attribute mapping.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def mapping
  @mapping
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def model
  @model
end

#on_duplicateObject (readonly)

Returns the value of attribute on_duplicate.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def on_duplicate
  @on_duplicate
end

#relationObject (readonly)

Returns the value of attribute relation.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def relation
  @relation
end

#returningObject (readonly)

Returns the value of attribute returning.



4
5
6
# File 'lib/insert_select/builder.rb', line 4

def returning
  @returning
end

Instance Method Details

#constant_mappingObject



44
45
46
# File 'lib/insert_select/builder.rb', line 44

def constant_mapping
  @constant_mapping ||= mapper[:constant_mapping]
end

#constant_valuesObject



61
62
63
64
65
66
# File 'lib/insert_select/builder.rb', line 61

def constant_values
  constant_mapping.map do |k, v|
    type = model.type_for_attribute(k.to_s)
    type.serialize(v)
  end
end

#insert_mappingObject



40
41
42
# File 'lib/insert_select/builder.rb', line 40

def insert_mapping
  @insert_mapping ||= mapper[:insert_mapping]
end

#inserting_column_namesObject



53
54
55
# File 'lib/insert_select/builder.rb', line 53

def inserting_column_names
  (insert_mapping.values + constant_mapping.keys).map {|k| @connection.quote_column_name(k) }
end

#intoObject



48
49
50
51
# File 'lib/insert_select/builder.rb', line 48

def into
  return nil if insert_mapping.blank? && constant_mapping.blank?
  "INTO #{model.quoted_table_name} (#{inserting_column_names.join(", ")})"
end

#mapperObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/insert_select/builder.rb', line 16

def mapper
  @mapper ||= begin
    result = {}
    insert_mapping = {}

    if selected_column_names.present?
      selected_column_names.each do |column_name|
        insert_mapping[column_name.to_s] = mapping[column_name.to_sym] || column_name
      end
    elsif mapping.present? || model.scope_attributes.present?
      @connection.columns(relation.table_name).each do |column|
        insert_mapping[column.name.to_s] =  mapping[column.name.to_sym] || column.name
      end
    end

    model.scope_attributes.keys.each {|column_name| insert_mapping.delete(column_name.to_s) }

    result[:insert_mapping] = insert_mapping
    result[:constant_mapping] = model.scope_attributes || {}

    result
  end
end

#relation_sqlObject



57
58
59
# File 'lib/insert_select/builder.rb', line 57

def relation_sql
  relation.to_sql
end

#reselect_relation!Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/insert_select/builder.rb', line 68

def reselect_relation!
  current_selected_column_names = relation.select_values.map(&:to_s)

  insert_mapping.keys.each do |k, v|
    relation._select!(k) if current_selected_column_names.exclude?(k.to_s)
  end

  constant_mapping.keys.each do |k, v|
    relation._select!("?")
    remove_select_values!(k)
  end
end

#returning?Boolean



97
98
99
# File 'lib/insert_select/builder.rb', line 97

def returning?
  @returning.present?
end