# File sample/psql.rb, line 814
def SendQuery(settings, query, copy_in, copy_out, copystream)
  if settings.singleStep
    printf("\n**************************************")
    printf("*****************************************\n")
  end

  if (settings.echoQuery || settings.singleStep)
    printf(STDERR, "QUERY: %s\n", query);
  end

  if settings.singleStep
    printf("\n**************************************");
    printf("*****************************************\n")
    STDOUT.flush
    printf("\npress return to continue ..\n");
    gets("", STDIN);
  end

  begin
    results = settings.db.exec(query)
    case results.status
    when PGresult::TUPLES_OK
      success = TRUE
      if settings.gfname
        setFout(settings, settings.gfname)
        settings.gfname = nil
        results.print(settings.queryFout, settings.opt)
        settings.queryFout.flush
        if settings.queryFout != STDOUT
          settings.queryFout.close
          settings.queryFout = STDOUT
        end
      else
        results.print(settings.queryFout, settings.opt)
        settings.queryFout.flush
      end
      results.clear

    when PGresult::EMPTY_QUERY
      success = TRUE

    when PGresult::COMMAND_OK
      success = TRUE
      if !settings.quiet
        printf("%s\n", results.cmdstatus)
      end

    when PGresult::COPY_OUT
      success = TRUE
      if copy_out
          handleCopyOut(settings, copystream)
      else
        if !settings.quiet
          printf("Copy command returns...\n")
        end

        handleCopyOut(settings, STDOUT)
      end

    when PGresult::COPY_IN
      success = TRUE
      if copy_in
        handleCopyIn(settings, FALSE, copystream)
      else
        handleCopyIn(settings, !settings.quiet, STDIN)
      end
    end

    if (settings.db.status == PGconn::CONNECTION_BAD)
      printf(STDERR, "We have lost the connection to the backend, so ")
      printf(STDERR, "further processing is impossible.  ")
      printf(STDERR, "Terminating.\n")
      exit(2)
    end

    # check for asynchronous returns
    # notify = settings.db.notifies()
    # if notify
    #   printf(STDERR,"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
    #   notify.relname, notify.be_pid)
    # end

  rescue
    printf(STDERR, "%s", $!)
    success = FALSE
  end

  return success
end