class Myreplicator::ImportSql

Public Class Methods

build_load_data_infile(options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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