Class: Myreplicator::ImportSql

Inherits:
Object
  • Object
show all
Defined in:
lib/loader/import_sql.rb

Class Method Summary collapse

Class Method Details

.build_load_data_infile(options) ⇒ Object



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

.initial_load(*args) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/loader/import_sql.rb', line 66

def initial_load *args
  options = args.extract_options!

  cmd = mysql_cmd(options[:db])       
  cmd += " #{options[:db]} "
  cmd += " < #{options[:filepath]} "
  
  return cmd
end

.load_data_infile(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/loader/import_sql.rb', line 5

def load_data_infile *args
  options = args.extract_options!
  sql = build_load_data_infile options

  cmd = mysql_cmd(options[:db])
  cmd += " --local_infile=1 " # Used for mysql versions that do not support -e and LDI
  cmd += "-e \"#{sql}\" "

  return cmd
end

.mysql_cmd(db) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/loader/import_sql.rb', line 76

def mysql_cmd db
  # Destination database host
  db_host = SqlCommands.db_configs(db).has_key?("host") ? SqlCommands.db_configs(db)["host"] : "127.0.0.1"
  
  cmd = Myreplicator.mysql
  cmd += " -u#{SqlCommands.db_configs(db)["username"]} -p#{SqlCommands.db_configs(db)["password"]} "
  cmd += " -h#{db_host} " 
  cmd += " -P#{SqlCommands.db_configs(db)["port"]} " if SqlCommands.db_configs(db)["port"]
  
  return cmd
end