module Chicago
  module ETL
    module SequelExtensions
      module LoadDataInfile
        # Loads the CSV data columns in filepath into this dataset's table.
        def load_csv_infile(filepath, columns)
          execute_dui(load_csv_infile_sql(filepath, columns))
        end
        
        def load_csv_infile_sql(filepath, columns)
          "LOAD DATA INFILE '#{filepath}' REPLACE INTO TABLE `#{opts[:from]}` CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' (`#{columns.join('`,`')}`);"
        end
      end
    end
  end
end

Sequel::Dataset.send :include, Chicago::ETL::SequelExtensions::LoadDataInfile

module Chicago
  module ETL
    module SequelExtensions
      module LoadDataInfile
        # Loads the CSV data columns in filepath into this dataset's table.
        def load_csv_infile(filepath, columns)
          execute_dui(load_csv_infile_sql(filepath, columns))
        end
        
        def load_csv_infile_sql(filepath, columns)
          "LOAD DATA INFILE '#{filepath}' REPLACE INTO TABLE `#{opts[:from].first}` CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' (`#{columns.join('`,`')}`);"
        end
      end
    end
  end
end

Sequel::Dataset.send :include, Chicago::ETL::SequelExtensions::LoadDataInfile

Fix 1.9 bug with load_csv_infile.
Fix 1.9 bug with load_csv_infile.

[:foo].to_s returns "foo" in ruby 1.8.7, but returns "[:foo]" in ruby
1.9.3. Take the first element of the opts[:from] array to fix this.
