Method: Myreplicator::SqlCommands.get_outfile_sql

Defined in:
lib/exporter/sql_commands.rb

.get_outfile_sql(*args) ⇒ Object

Mysql export data into outfile option Provided for tables that need special delimiters



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/exporter/sql_commands.rb', line 174

def self.get_outfile_sql *args 
  options = args.extract_options!
  #Kernel.p "===== SELECT * INTO OUTFILE OPTIONS====="
  #Kernel.p options
  columns = get_columns options
  sql = "SELECT #{columns.join(',')} INTO OUTFILE '#{options[:filepath]}' "
  #sql = "SELECT * INTO OUTFILE '#{options[:filepath]}' " 
  
  if options[:enclosed_by].blank?
    sql += " FIELDS TERMINATED BY '\\0' ESCAPED BY '' LINES TERMINATED BY ';~~;\n'"
  else
    sql += " FIELDS TERMINATED BY '\\0' ESCAPED BY '' ENCLOSED BY '#{options[:enclosed_by]}'  LINES TERMINATED BY ';~~;\n'"
  end
  
  sql += "FROM #{options[:db]}.#{options[:table]} "

  if options[:export_type]=="incremental" && !options[:incremental_col].blank? && !options[:incremental_val].blank?
    if options[:incremental_col_type] == "datetime"
      if options[:incremental_val] == "0"
        options[:incremental_val] = "1900-01-01 00:00:00"
      end
      sql += "WHERE #{options[:incremental_col]} >= '#{(DateTime.parse(options[:incremental_val]) -1.hour).to_s(:db)}'" #buffer 1 hour
    elsif options[:incremental_col_type] == "int"
      if options[:incremental_val].blank?
        options[:incremental_val] = "0"
      end
      sql += "WHERE #{options[:incremental_col]} >= #{options[:incremental_val].to_i - 10000}" #buffer 10000 
    end
  end
  Kernel.p sql
  return sql
end