Compare:

  int PQntuples(const PGresult *res)

Reasonable: doesn't modify 'res'.

With:

  char *PQcmdStatus(PGresult *res);
  char *PQcmdTuples(PGresult *res);

Unreasonable:

  a. What, these two can modify 'res' I pass in?..

  b. Oh, yes, because they return 'char *' pointing to
     'res->cmdStatus+n', so, a libpq user may write:

        char *s = PQcmdStatus(res);
        *s = 'x';

     and have 'res' modified.  (Would be the user's fault, of course.)
  
The non-const-ness of 'PGresult *' for these two functions seems to
stand out among the functions covered in the "30.3.2. Retrieving Query
Result Information" manual section and inhibits writing the strict
client code.

I would suggest to change the signatures by applying this trivial
patch (and changing the documentation):

============================================================
== diff orig/postgresql-9.1.1/src/interfaces/libpq/libpq-fe.h 
./postgresql-9.1.1/src/interfaces/libpq/libpq-fe.h
450c450
< extern char *PQcmdStatus(PGresult *res);
---
> extern const char *PQcmdStatus(const PGresult *res);
453c453
< extern char *PQcmdTuples(PGresult *res);
---
> extern const char *PQcmdTuples(const PGresult *res);
== diff orig/postgresql-9.1.1/src/interfaces/libpq/fe-exec.c 
./postgresql-9.1.1/src/interfaces/libpq/fe-exec.c
2665,2666c2665,2666
< char *
< PQcmdStatus(PGresult *res)
---
> const char *
> PQcmdStatus(const PGresult *res)
2736,2737c2736,2737
< char *
< PQcmdTuples(PGresult *res)
---
> const char *
> PQcmdTuples(const PGresult *res)
2739,2740c2739
<       char       *p,
<                          *c;
---
>       const char *p, *c;
============================================================

(The above was obtained in 9.1.1; the subsequent build with GCC 4.1.2
succeeds without warnings.)

If the above change causes a warning in a client code, so much the
better: the client code is doing something unreasonable like the "*s"
assignment in my example above.

-- Alex -- alex-goncha...@comcast.net --

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to