16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/loader/import_sql.rb', line 16
def build_load_data_infile options
options.reverse_merge!(:replace => true,
:fields_terminated_by => "\\t",
:lines_terminated_by => "\\n"
)
handle = options[:replace] ? 'REPLACE' : 'IGNORE'
sql = "LOAD DATA LOCAL INFILE '#{options[:filepath]}' #{handle} "
sql += "INTO TABLE #{options[:db]}.#{options[:table_name]} "
if options.include?(:character_set)
sql << " CHARACTER SET #{options[:character_set]}"
end
if options.include?(:fields_terminated_by) or options.include?(:enclosed_by) or options.include?(:escaped_by)
sql << " FIELDS"
end
if options.include?(:fields_terminated_by)
sql << " TERMINATED BY '#{options[:fields_terminated_by]}'"
end
if options.include?(:enclosed_by)
sql << " ENCLOSED BY '#{options[:enclosed_by]}'"
end
if options.include?(:escaped_by)
sql << " ESCAPED BY '#{options[:escaped_by]}'"
end
if options.include?(:starting_by) or options.include?(:lines_terminated_by)
sql << " LINES"
end
if options.include?(:starting_by)
sql << " STARTING BY '#{options[:starting_by]}'"
end
if options.include?(:lines_terminated_by)
sql << " TERMINATED BY '#{options[:lines_terminated_by]}'"
end
if options.include?(:ignore)
sql << " IGNORE #{options[:ignore]} LINES"
end
if options.include?(:fields) and !options[:fields].empty?
sql << "( #{options[:fields].join(', ')} )"
end
return sql
end
|