Module: BulkRecord::Base::ClassMethods

Included in:
BulkRecord::Base
Defined in:
lib/bulk_record.rb

Instance Method Summary collapse

Instance Method Details

#add(row) ⇒ Object



65
66
67
68
# File 'lib/bulk_record.rb', line 65

def add(row)
  self.columns_in_rows = columns_in_rows | row.keys
  self.rows << row
end

#format(value) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bulk_record.rb', line 119

def format(value)
  formalized = ""
  if value.nil?
    formalized = "NULL"
  elsif value.respond_to?(:strftime)
    formalized = "'" + rawvalue.strftime('%Y-%m-%d %H:%M:%S') + "'"
  elsif value.is_a?(Array)
    formalized = value.map{|v| "'" + Mysql2::Client.escape(v.to_s) + "'" }.join(",")
  else
    formalized = "'" + Mysql2::Client.escape(value.to_s) + "'"
  end
end

#full_table_nameObject



70
71
72
73
74
75
76
# File 'lib/bulk_record.rb', line 70

def full_table_name
  if @table_name_prefix.nil?
    @table_name
  else
    @table_name_prefix + @table_name
  end
end

#import(option = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bulk_record.rb', line 96

def import(option = nil)
  values = []
  update_columns = columns_in_rows & columns

  self.rows.each do |row|
    value = []
    self.columns.each do |col|
      value << format(row[col])
    end
    values << "(#{value.join(",")})"
  end
  query = "INSERT INTO #{self.full_table_name} (#{columns.join(',')}) VALUES #{values.join(",")}"

  if (!option.nil? && option[:on_duplicate_key_update])
    counter_columns = update_columns.select { |x| !fix_columns.include?(x) }
    update = " ON DUPLICATE KEY UPDATE " + counter_columns.map{ |x| "#{x} = #{x} + VALUES(#{x})"}.join(',')
    query += update
    connection.query(query)
  else
    connection.query(query)
  end
end

#initialize(option = nil) ⇒ Object



47
48
49
50
# File 'lib/bulk_record.rb', line 47

def initialize(option = nil)
  set_schema(option)
  set_primary_keys
end

#loadObject

TODO: bulk insert useing LOAD DATA INFILE



133
134
# File 'lib/bulk_record.rb', line 133

def load
end

#set_primary_keysObject



52
53
54
55
56
57
58
59
# File 'lib/bulk_record.rb', line 52

def set_primary_keys
  query = "SHOW KEYS FROM #{self.full_table_name} WHERE Key_name = 'PRIMARY'"
  result = connection.query(query)
  self.primary_keys = []
  result.each do |row|
    self.primary_keys << row['Column_name']
  end
end

#set_schema(option) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bulk_record.rb', line 78

def set_schema(option)
  self.table_name = self.class.name.underscore.pluralize
  unless option.blank?
    unless option[:table_name_prefix].blank?
      self.table_name_prefix = option[:table_name_prefix]
    end
    unless option[:fix_columns].blank?
      self.fix_columns = option[:fix_columns]
    end
  end
  query = "DESC #{self.full_table_name}"
  connection.query(query).each do |r|
    unless r["Extra"] && r["Extra"] == "auto_increment"
      self.columns << r["Field"].to_sym
    end
  end
end

#set_table_name(name) ⇒ Object



61
62
63
# File 'lib/bulk_record.rb', line 61

def set_table_name(name)
  self.table_name = name
end