Class: YDD::SerializationHelper::Load
- Inherits:
-
Object
- Object
- YDD::SerializationHelper::Load
- Defined in:
- lib/ydd/serialization_helper.rb
Direct Known Subclasses
Class Method Summary collapse
- .clean_column_value(value_column) ⇒ Object
- .convert_from(char_enc) ⇒ Object
- .load(io, truncate = true) ⇒ Object
- .load_records(table, column_names, records) ⇒ Object
- .load_table(table, data, truncate = true) ⇒ Object
- .reset_pk_sequence!(table_name) ⇒ Object
- .truncate_table(table) ⇒ Object
Class Method Details
.clean_column_value(value_column) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ydd/serialization_helper.rb', line 98 def self.clean_column_value(value_column) value, column = value_column[0], value_column[1] if value.present? value = case column.type when :string, :text unless value.is_a?(String) value = value.to_s puts "WARNING: Conversion to string required to value '#{value}'. Data loss may occur." end character_encoding = CharDet.detect(value)["encoding"] convert_from(character_encoding).iconv(value + ' ')[0..-2] else value end end YDD.connection.quote(value, column) end |
.convert_from(char_enc) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ydd/serialization_helper.rb', line 43 def self.convert_from(char_enc) @converter ||= {} return @converter[char_enc] if @converter[char_enc].present? if YDD.connection.encoding.eql? "UTF8" puts "Creating a string converter from #{char_enc} that will ignore non-UTF8 characters." @converter[char_enc] = Iconv.new('UTF-8//TRANSLIT//IGNORE', char_enc) end @converter.fetch(char_enc, IdentityConvertor) end |
.load(io, truncate = true) ⇒ Object
56 57 58 59 60 |
# File 'lib/ydd/serialization_helper.rb', line 56 def self.load(io, truncate = true) YDD.connection.transaction do load_documents(io, truncate) end end |
.load_records(table, column_names, records) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ydd/serialization_helper.rb', line 77 def self.load_records(table, column_names, records) return if column_names.nil? columns = column_names.map{ |cn| YDD.connection.columns(table).detect{ |c| c.name == cn } } quoted_column_names = column_names.map { |column| YDD.connection.quote_column_name(column) }.join(',') quoted_table_name = SerializationHelper::Utils.quote_table(table) #records.sort_by! { |r| r['id'].to_i } if column_names.include? 'id' # records.each do |record| quoted_values = record.zip(columns).map { |value_column| clean_column_value(value_column) }.join(",") YDD.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})") end end |
.load_table(table, data, truncate = true) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/ydd/serialization_helper.rb', line 70 def self.load_table(table, data, truncate = true) column_names = data['columns'] truncate_table(table) if truncate load_records(table, column_names, data['records']) reset_pk_sequence!(table) end |
.reset_pk_sequence!(table_name) ⇒ Object
92 93 94 95 96 |
# File 'lib/ydd/serialization_helper.rb', line 92 def self.reset_pk_sequence!(table_name) if YDD.connection.respond_to?(:reset_pk_sequence!) YDD.connection.reset_pk_sequence!(table_name) end end |
.truncate_table(table) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/ydd/serialization_helper.rb', line 62 def self.truncate_table(table) begin YDD.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)}") rescue Exception YDD.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}") end end |