class Multiwoven::Integrations::Source::Sftp::Client

def query(conn, query)

def query(conn, query)
  @sftp.download!(@remote_file_path, @tempfile.path)
  if query.gsub(/FROM\s+\S+/i).count > 1
    # multiple select/from with trailing closing parenthesis (replacing first occurrence in reverse)
    query = query.reverse.sub(/\S+\s+MORF/i, "FROM read_csv_auto('#{@tempfile.path}'))".reverse).reverse
  elsif query.match?(/\bFROM\b/i)
    # single select statement
    query = query.gsub(/FROM\s+\S+/i, "FROM read_csv_auto('#{@tempfile.path}')")
  end
  records = get_results(conn, query)
  records.map do |row|
    RecordMessage.new(data: row, emitted_at: Time.now.to_i).to_multiwoven_message
  end
end