/*
 * call-seq:
 *    conn.exec( sql )
 *
 * Sends SQL query request specified by _sql_ to the PostgreSQL.
 * Returns a PGresult instance on success.
 * On failure, it raises a PGError exception.
 */
static VALUE
pgconn_exec(obj, str)
  VALUE obj, str;
{
  PGconn *conn = get_pgconn(obj);
  PGresult *result;
  int status;
  char *msg;

  Check_Type(str, T_STRING);

  result = PQexec(conn, STR2CSTR(str));
  if (!result) {
  rb_raise(rb_ePGError, PQerrorMessage(conn));
  }
  status = PQresultStatus(result);

  switch (status) {
  case PGRES_TUPLES_OK:
  case PGRES_COPY_OUT:
  case PGRES_COPY_IN:
  case PGRES_EMPTY_QUERY:
  case PGRES_COMMAND_OK:  /* no data will be received */
    return pgresult_new(result);

  case PGRES_BAD_RESPONSE:
  case PGRES_FATAL_ERROR:
  case PGRES_NONFATAL_ERROR:
    msg = PQerrorMessage(conn);
    break;
  default:
    msg = "internal error : unknown result status.";
    break;
  }
  PQclear(result);
  rb_raise(rb_ePGError, msg);
}