Class: JunglePath::DBAccess::Import::Insert
- Inherits:
-
Object
- Object
- JunglePath::DBAccess::Import::Insert
- Defined in:
- lib/jungle_path/db_access/import/insert.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #_model(model) ⇒ Object
- #close_files ⇒ Object
-
#initialize(config, logger = nil) ⇒ Insert
constructor
A new instance of Insert.
- #json_dump(value) ⇒ Object
- #json_dump_array(array) ⇒ Object
- #json_dump_hash(hash) ⇒ Object
- #model_values_to_string(model) ⇒ Object
- #open_file(model) ⇒ Object
- #reset ⇒ Object
- #string_fix(value) ⇒ Object
- #value_to_string(value, json = false) ⇒ Object
- #write_to_db ⇒ Object
Constructor Details
#initialize(config, logger = nil) ⇒ Insert
Returns a new instance of Insert.
10 11 12 13 14 15 16 17 18 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 10 def initialize(config, logger=nil) @logger = logger @path = config.import_file_path #@db = nil #if config.postgresql @db = JunglePath::DBAccess::IO::DB.new(config, logger) #end reset end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
9 10 11 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 9 def db @db end |
Instance Method Details
#_model(model) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 20 def _model(model) table_name = model.class.table_name file = open_file(model) @counters[table_name] += 1 if model.respond_to?(:id) and model.id == nil model.id = @counters[table_name] end file.puts model_values_to_string(model) puts " #{table_name} rows processed count: #{@counters[table_name]}." if @counters[table_name] % 1000 == 0 model end |
#close_files ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 45 def close_files @files.each do |key, file| unless file.closed? file.fsync file.close puts " #{key} rows processed count: #{@counters[key]}." end end end |
#json_dump(value) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 92 def json_dump(value) if value.class == Hash json_dump_hash(value) elsif value.class == Array json_dump_array(value) else value_to_string(value, true) end end |
#json_dump_array(array) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 118 def json_dump_array(array) values = [] array.each do |value| if value.class == Hash #a << json_dump_hash(value) a << "\"#{key}\":#{json_dump_hash(value)}" elsif value.class == Array #a << json_dump_array(value) a << "\"#{key}\":#{json_dump_array(value)}" else values << "#{value_to_string(value, true)}" end end "[#{values.join(',')}]" end |
#json_dump_hash(hash) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 102 def json_dump_hash(hash) a = [] hash.each do |key, value| if value.class == Hash #a << json_dump_hash(value) a << "\"#{key}\":#{json_dump_hash(value)}" elsif value.class == Array #a << json_dump_array(value) a << "\"#{key}\":#{json_dump_array(value)}" else a << "\"#{key}\":#{value_to_string(value, true)}" end end "{#{a.join(',')}}" end |
#model_values_to_string(model) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 71 def model_values_to_string(model) values = [] model._columns.each do |key, column| value = model._values[key] if column.base_type == :json or column.base_type == :jsonb values << json_dump(value) else values << value_to_string(value) end end values.join("\t") end |
#open_file(model) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 32 def open_file(model) table_name = model.class.table_name file = @files[table_name] unless file file = File.open(JunglePath::DBAccess::Import.data_file_name(@path, table_name), 'a') @models[table_name] = model @files[table_name] = file @counters[table_name] = 0 puts "writing file: #{table_name}." end file end |
#reset ⇒ Object
55 56 57 58 59 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 55 def reset @models = {} @files = {} @counters = {} end |
#string_fix(value) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 84 def string_fix(value) v = value.gsub('\\', '\\\\\\\\') v = v.gsub('"', '\\\\\"') v = v.gsub("\n", '\n') v = v.gsub("\r", '\r') v = v.gsub("\t", '\t') end |
#value_to_string(value, json = false) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 134 def value_to_string value, json=false v = nil if value == nil v = '\N' elsif value.class == String v = string_fix(value) v = "\"#{v}\"" if json elsif value.class == DateTime v = value.strftime("%Y-%m-%d %H:%M:%S.%6N") v = "\"#{v}\"" if json elsif value.class == Date v = value.strftime("%Y-%m-%d") v = "\"#{v}\"" if json elsif value.class == Fixnum v = "#{value}" elsif value.class == Float v = "#{value}" elsif value.class == Integer v = "#{value}" elsif value.class == TrueClass v = "t" v = "\"#{v}\"" if json elsif value.class == FalseClass v = "f" v = "\"#{v}\"" if json else v = "#{value}" v = "\"#{v}\"" if json end v end |
#write_to_db ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/jungle_path/db_access/import/insert.rb', line 61 def write_to_db if @db @models.each do |key, model| file_name = JunglePath::DBAccess::Import.data_file_name(@path, model.class.table_name) @db.copy.into_table model.class, file_name File.delete file_name end end end |