# File sample/psql.rb, line 478
def do_copy(settings, table, from_p, file)
  if (table == nil || from_p == nil || file == nil)
    printf("Syntax error, reffer \\copy help with  \\? \n")
    return
  end

  if from_p.upcase! == "FROM"
    from = TRUE
  else
    from = FALSE
  end

  query  = "COPY "
  query += table

  if from
    query += " FROM stdin"
    copystream = File.new(file, "r")
  else
    query += " TO stdout"
    copystream = File.new(file, "w")
  end

  begin
    success = SendQuery(settings, query, from, !from, copystream);
    copystream.close
    if !settings.quiet
      if success
        printf("Successfully copied.\n");
      else
        printf("Copy failed.\n");
      end
    end
  rescue
    printf(STDERR, "Unable to open file %s which to copy.",
           if from then "from" else "to" end)
  end
end