Thread: PQcmdTuples() declaration

PQcmdTuples() declaration

From
Michael Klatt
Date:
I am running version 7.1.3, installed as an RPM from Red Hat.  According to
the documentation for libpq, function PQcmdTuples is declared as
'PQcmdTuples(const PGresult*)'.  In my 'libpq-fe.h' include file it is
declared as 'PQcmdTuples(PGresult*)' (no const).  Is this an error in the
documentation or my include file?  I have changed the include file to match
the documentation, and I haven't noticed any negative effects so far.

___________________________________
Michael Klatt

University of Oklahoma
Environmental Verification and Analysis Center
3200 Marshall Avenue, Suite 110
Norman, OK  73072-8032

405.447.8412
405.447.8455  FAX
http://www.evac.ou.edu/

"It's easy to be virtuous when you can't afford the vice."




Re: PQcmdTuples() declaration

From
Payman
Date:
Hi Michael
I am running version 7.2.3 on MacOS X and seam thing
I think this is a doc probleme .
Strange my doc is a 7.2.1 documentation and 7.2.3 Package.
If you change the include file you must rebuild all postgresql .
you dont have link error ?

Payman

>I am running version 7.1.3, installed as an RPM from Red Hat.  According to
>the documentation for libpq, function PQcmdTuples is declared as
>'PQcmdTuples(const PGresult*)'.  In my 'libpq-fe.h' include file it is
>declared as 'PQcmdTuples(PGresult*)' (no const).  Is this an error in the
>documentation or my include file?  I have changed the include file to match
>the documentation, and I haven't noticed any negative effects so far.
>
>  
>




Re: PQcmdTuples() declaration

From
Bruce Momjian
Date:
Good catch.  It looks like PQcmdStatus and PQcmdStatus are both wrong. 
They should be 'const' in the code.  I will fix that in 7.4.

---------------------------------------------------------------------------

Payman wrote:
> Hi Michael
> I am running version 7.2.3 on MacOS X and seam thing
> I think this is a doc probleme .
> Strange my doc is a 7.2.1 documentation and 7.2.3 Package.
> If you change the include file you must rebuild all postgresql .
> you dont have link error ?
> 
> Payman
> 
> >I am running version 7.1.3, installed as an RPM from Red Hat.  According to
> >the documentation for libpq, function PQcmdTuples is declared as
> >'PQcmdTuples(const PGresult*)'.  In my 'libpq-fe.h' include file it is
> >declared as 'PQcmdTuples(PGresult*)' (no const).  Is this an error in the
> >documentation or my include file?  I have changed the include file to match
> >the documentation, and I haven't noticed any negative effects so far.
> >
> >  
> >
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> 

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073
 


Re: PQcmdTuples() declaration

From
Bruce Momjian
Date:
I am not sure how to deal with this.  I have updated the 7.3/CVS docs to
match the include file and code, which has no consts.

The issue with PQcmdTuples() and PQcmdStatus() are that they return
*result structure member pointers.  The patch makes the *result a const,
then we have to return the values as const's.  The code compiled OK, so
I assume that is fine.  How does that sound to others, or should we keep
the result non-const?

---------------------------------------------------------------------------

Payman wrote:
> Hi Michael
> I am running version 7.2.3 on MacOS X and seam thing
> I think this is a doc probleme .
> Strange my doc is a 7.2.1 documentation and 7.2.3 Package.
> If you change the include file you must rebuild all postgresql .
> you dont have link error ?
>
> Payman
>
> >I am running version 7.1.3, installed as an RPM from Red Hat.  According to
> >the documentation for libpq, function PQcmdTuples is declared as
> >'PQcmdTuples(const PGresult*)'.  In my 'libpq-fe.h' include file it is
> >declared as 'PQcmdTuples(PGresult*)' (no const).  Is this an error in the
> >documentation or my include file?  I have changed the include file to match
> >the documentation, and I haven't noticed any negative effects so far.
> >
> >
> >
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: doc/src/sgml/libpq.sgml
===================================================================
RCS file: /cvsroot/pgsql-server/doc/src/sgml/libpq.sgml,v
retrieving revision 1.98
diff -c -c -r1.98 libpq.sgml
*** doc/src/sgml/libpq.sgml    3 Nov 2002 01:30:46 -0000    1.98
--- doc/src/sgml/libpq.sgml    7 Nov 2002 23:14:22 -0000
***************
*** 1210,1216 ****
            Returns the command status string from the SQL command that
        generated the <structname>PGresult</structname>.
  <synopsis>
! char * PQcmdStatus(PGresult *res);
  </synopsis>
  </para>
  </listitem>
--- 1210,1216 ----
            Returns the command status string from the SQL command that
        generated the <structname>PGresult</structname>.
  <synopsis>
! const char * PQcmdStatus(const PGresult *res);
  </synopsis>
  </para>
  </listitem>
***************
*** 1220,1226 ****
  <function>PQcmdTuples</function>
        Returns the number of rows affected by the SQL command.
  <synopsis>
! char * PQcmdTuples(PGresult *res);
  </synopsis>
            If the <acronym>SQL</acronym> command that generated the
        <structname>PGresult</structname> was INSERT, UPDATE or DELETE, this returns a
--- 1220,1226 ----
  <function>PQcmdTuples</function>
        Returns the number of rows affected by the SQL command.
  <synopsis>
! const char * PQcmdTuples(const PGresult *res);
  </synopsis>
            If the <acronym>SQL</acronym> command that generated the
        <structname>PGresult</structname> was INSERT, UPDATE or DELETE, this returns a
Index: src/interfaces/libpq/fe-exec.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-exec.c,v
retrieving revision 1.122
diff -c -c -r1.122 fe-exec.c
*** src/interfaces/libpq/fe-exec.c    4 Sep 2002 20:31:47 -0000    1.122
--- src/interfaces/libpq/fe-exec.c    7 Nov 2002 23:14:43 -0000
***************
*** 2234,2241 ****
          return 0;
  }

! char *
! PQcmdStatus(PGresult *res)
  {
      if (!res)
          return NULL;
--- 2234,2241 ----
          return 0;
  }

! const char *
! PQcmdStatus(const PGresult *res)
  {
      if (!res)
          return NULL;
***************
*** 2303,2310 ****
      if the last command was an INSERT/UPDATE/DELETE, return number
      of inserted/affected tuples, if not, return ""
  */
! char *
! PQcmdTuples(PGresult *res)
  {
      char        noticeBuf[128];

--- 2303,2310 ----
      if the last command was an INSERT/UPDATE/DELETE, return number
      of inserted/affected tuples, if not, return ""
  */
! const char *
! PQcmdTuples(const PGresult *res)
  {
      char        noticeBuf[128];

***************
*** 2315,2321 ****
          strncmp(res->cmdStatus, "DELETE", 6) == 0 ||
          strncmp(res->cmdStatus, "UPDATE", 6) == 0)
      {
!         char       *p = res->cmdStatus + 6;

          if (*p == 0)
          {
--- 2315,2321 ----
          strncmp(res->cmdStatus, "DELETE", 6) == 0 ||
          strncmp(res->cmdStatus, "UPDATE", 6) == 0)
      {
!         const char       *p = res->cmdStatus + 6;

          if (*p == 0)
          {
Index: src/interfaces/libpq/libpq-fe.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/libpq-fe.h,v
retrieving revision 1.86
diff -c -c -r1.86 libpq-fe.h
*** src/interfaces/libpq/libpq-fe.h    4 Sep 2002 20:31:47 -0000    1.86
--- src/interfaces/libpq/libpq-fe.h    7 Nov 2002 23:14:45 -0000
***************
*** 307,316 ****
  extern Oid    PQftype(const PGresult *res, int field_num);
  extern int    PQfsize(const PGresult *res, int field_num);
  extern int    PQfmod(const PGresult *res, int field_num);
! extern char *PQcmdStatus(PGresult *res);
  extern char *PQoidStatus(const PGresult *res);    /* old and ugly */
  extern Oid    PQoidValue(const PGresult *res);    /* new and improved */
! extern char *PQcmdTuples(PGresult *res);
  extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
  extern int    PQgetlength(const PGresult *res, int tup_num, int field_num);
  extern int    PQgetisnull(const PGresult *res, int tup_num, int field_num);
--- 307,316 ----
  extern Oid    PQftype(const PGresult *res, int field_num);
  extern int    PQfsize(const PGresult *res, int field_num);
  extern int    PQfmod(const PGresult *res, int field_num);
! extern const char *PQcmdStatus(const PGresult *res);
  extern char *PQoidStatus(const PGresult *res);    /* old and ugly */
  extern Oid    PQoidValue(const PGresult *res);    /* new and improved */
! extern const char *PQcmdTuples(const PGresult *res);
  extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
  extern int    PQgetlength(const PGresult *res, int tup_num, int field_num);
  extern int    PQgetisnull(const PGresult *res, int tup_num, int field_num);

Re: PQcmdTuples() declaration

From
Payman
Date:
Hi Bruce,

The code compile ok, PGresult *res  is not changed
in source code of PQcmdTuples, for me it's a const PGresult *res.
Now the probleme is : who put it in non const and why ?
Is changing this will cause problems in actual project for anyone?

Other thing is, PQcmdTuples return numbre of tuples after a insert, 
delete, update
I say Number, Number is a numerical value and not String value ...

in libpq++ :
int PgDatabase::CmdTuples() const
{
const char *a = PQcmdTuples(pgResult);
return a[0] ? atoi(a) : -1;
}

return int value and not char *
(changing this cause more problems for other users...)

thanks
Payman



Bruce Momjian wrote:

>I am not sure how to deal with this.  I have updated the 7.3/CVS docs to
>match the include file and code, which has no consts.
>
>The issue with PQcmdTuples() and PQcmdStatus() are that they return
>*result structure member pointers.  The patch makes the *result a const,
>then we have to return the values as const's.  The code compiled OK, so
>I assume that is fine.  How does that sound to others, or should we keep
>the result non-const?
>
>  
>




Re: PQcmdTuples() declaration

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> The issue with PQcmdTuples() and PQcmdStatus() are that they return
> *result structure member pointers.  The patch makes the *result a const,
> then we have to return the values as const's.  The code compiled OK, so
> I assume that is fine.  How does that sound to others, or should we keep
> the result non-const?

Adding a const to the result type will break client applications that
aren't expecting it.  I don't think we can get away with that.

We could still declare the input type as const if we were willing to
cast away const inside the functions, but that seems pretty ugly.
        regards, tom lane


Re: PQcmdTuples() declaration

From
Bruce Momjian
Date:
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > The issue with PQcmdTuples() and PQcmdStatus() are that they return
> > *result structure member pointers.  The patch makes the *result a const,
> > then we have to return the values as const's.  The code compiled OK, so
> > I assume that is fine.  How does that sound to others, or should we keep
> > the result non-const?
> 
> Adding a const to the result type will break client applications that
> aren't expecting it.  I don't think we can get away with that.
> 
> We could still declare the input type as const if we were willing to
> cast away const inside the functions, but that seems pretty ugly.

Yep, seems pretty ugly to me too.  Why bother calling it a const if we
are going to cast it away.

I have updated the docs for 7.3 and CVS to match the non-const code. 
Let's see if this issue comes up again.

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073