Getting number of tuples affected

Started by Michael Richardsover 26 years ago10 messages
#1Michael Richards
miker@scifair.acadiau.ca

I was looking for a function to return the number of tuples an update
returned, but couldn't find anything for libpq++. Any ideas?

I also noticed a bug in the docs worth mentioning...
http://postgresql.nextpath.com/doxlist.html

PgDatabase data;
data.exec("create table foo (a int4, b char16, d float8)");
data.exec("copy foo from stdin");
data.putline("3\etHello World\et4.5\en");
data.putline("4\etGoodbye World\et7.11\en");
&...
data.putline(".\en");
data.endcopy();

There is no PgDatabase::exec

thanks
-Michael

#2Vince Vielhaber
vev@michvhf.com
In reply to: Michael Richards (#1)
Re: [HACKERS] Getting number of tuples affected

On Wed, 30 Jun 1999, Michael Richards wrote:

I was looking for a function to return the number of tuples an update
returned, but couldn't find anything for libpq++. Any ideas?

Added to my list of stuff to do. As to what else you can do, submit
patches? :)

I also noticed a bug in the docs worth mentioning...
http://postgresql.nextpath.com/doxlist.html

PgDatabase data;
data.exec("create table foo (a int4, b char16, d float8)");
data.exec("copy foo from stdin");
data.putline("3\etHello World\et4.5\en");
data.putline("4\etGoodbye World\et7.11\en");
&...
data.putline(".\en");
data.endcopy();

There is no PgDatabase::exec

Yep. Guess that should be data.Exec(...

Thanks!
Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com flame-mail: /dev/null
# include <std/disclaimers.h> TEAM-OS2
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

#3Michael Richards
miker@scifair.acadiau.ca
In reply to: Vince Vielhaber (#2)
Re: [HACKERS] Getting number of tuples affected

On Wed, 30 Jun 1999, Vince Vielhaber wrote:

I was looking for a function to return the number of tuples an update
returned, but couldn't find anything for libpq++. Any ideas?

Added to my list of stuff to do. As to what else you can do, submit
patches? :)

There is no PgDatabase::exec

Yep. Guess that should be data.Exec(...

Not sure there is an Exec either :)

I've been making a pile of changes to the PgDatabase class. (Actually I
derived it). One such change is a function to quote a string you're going
to use in SQL. I'm not sure if this belongs in the PgDatabase class, but
if you think so, I'll add it before I submit the tuple count patches.

This allows me to do something like:
String updatesql(form("UPDATE users SET lastrequest='now' WHERE
loginid=%s",dbh->quote(_username).chars()));
cout << dbh->ExecTuplesOk(updatesql);

That is actually the call I needed the update count to ensure it worked...

Here is the routine:

// routine to quote any \ or ' chars in the passed string
// this isn't too efficient, but how much data are we really quoting?
String TDatabase::quote(const char *dirty) {
// start with a single quote
String clean("'");

const char *strptr=dirty;
// escape the string if it contains any ' or \ chars
while (*strptr) {
if ((*strptr=='\'') || (*strptr=='\\'))
clean+='\\';

clean+=*(strptr++);
}
// end with a quote
clean+="'";

return clean;
}

-Michael

#4Vince Vielhaber
vev@michvhf.com
In reply to: Michael Richards (#3)
Re: [HACKERS] Getting number of tuples affected

On Wed, 30 Jun 1999, Michael Richards wrote:

On Wed, 30 Jun 1999, Vince Vielhaber wrote:

I was looking for a function to return the number of tuples an update
returned, but couldn't find anything for libpq++. Any ideas?

Added to my list of stuff to do. As to what else you can do, submit
patches? :)

There is no PgDatabase::exec

Yep. Guess that should be data.Exec(...

Not sure there is an Exec either :)

It's inherited.

I've been making a pile of changes to the PgDatabase class. (Actually I
derived it). One such change is a function to quote a string you're going
to use in SQL. I'm not sure if this belongs in the PgDatabase class, but
if you think so, I'll add it before I submit the tuple count patches.

I don't think it belongs in PgDatabase. I'm not even sure it belongs in
libpq++, although I can see its usefulness.

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com flame-mail: /dev/null
# include <std/disclaimers.h> TEAM-OS2
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

#5Michael Richards
miker@scifair.acadiau.ca
In reply to: Vince Vielhaber (#4)
3 attachment(s)
Patches to get number of tuples affected

Hi.
Here are some diffs that implement a function called TuplesAffected. It
returns the number of tuples the last command affected, or 0 if the last
command was a SELECT. I added it to the PgConnection because it contains
the Exec method as well as the PQresult structure. Maybe farther down in
the inheritance there should be a function that executes a query and
returns the number of tuples affected or returned (according to whether it
was a select or not) or a -1 on error.

Patches are attached...

-Michael

Attachments:

pgconnection.cc.patchtext/plain; charset=US-ASCII; name=pgconnection.cc.patchDownload
*** pgconnection.cc	Wed Jun 30 13:24:41 1999
--- pgconnection.cc.bak	Wed Jun 30 13:16:54 1999
***************
*** 119,136 ****
  } // End ExecTuplesOk()
  
  
- // return the number of tuples affected by an INSERT, UPDATE or DELETE
- long int PgConnection::TuplesAffected() {
-   const char *numaffected=PQcmdTuples(pgResult);
- 
-   // if it returns a null, it was probably a SELECT
-   if (!numaffected)
-     return 0;
- 
-   // otherwise convert the string to a long
-   return atol(numaffected); 
- }
- 
  
  // Don't know why these next two need to be part of Connection
  
--- 119,124 ----
pgconnection.h.patchtext/plain; charset=US-ASCII; name=pgconnection.h.patchDownload
*** pgconnection.h	Wed Jun 30 13:20:40 1999
--- pgconnection.h.bak	Wed Jun 30 13:16:44 1999
***************
*** 59,65 ****
     int ExecCommandOk(const char* query);    // send a command and check if it's OK
     int ExecTuplesOk(const char* query);     // send a command and check if tuples are returned
     PGnotify* Notifies();
!    long int TuplesAffected(); // how many rows were affected by an INSERT, UPDATE or DELETE  
  protected:
     ConnStatusType Connect(const char* conninfo);
     string IntToString(int);
--- 59,65 ----
     int ExecCommandOk(const char* query);    // send a command and check if it's OK
     int ExecTuplesOk(const char* query);     // send a command and check if tuples are returned
     PGnotify* Notifies();
!     
  protected:
     ConnStatusType Connect(const char* conninfo);
     string IntToString(int);
pgconnection.patchtext/plain; charset=US-ASCII; name=pgconnection.patchDownload
*** pgconnection.h	Wed Jun 30 13:20:40 1999
--- pgconnection.h.bak	Wed Jun 30 13:16:44 1999
***************
*** 59,65 ****
     int ExecCommandOk(const char* query);    // send a command and check if it's OK
     int ExecTuplesOk(const char* query);     // send a command and check if tuples are returned
     PGnotify* Notifies();
!    long int TuplesAffected(); // how many rows were affected by an INSERT, UPDATE or DELETE  
  protected:
     ConnStatusType Connect(const char* conninfo);
     string IntToString(int);
--- 59,65 ----
     int ExecCommandOk(const char* query);    // send a command and check if it's OK
     int ExecTuplesOk(const char* query);     // send a command and check if tuples are returned
     PGnotify* Notifies();
!     
  protected:
     ConnStatusType Connect(const char* conninfo);
     string IntToString(int);
#6J.M.
darcy@druid.net
In reply to: Michael Richards (#5)
Re: [PATCHES] Patches to get number of tuples affected

Thus spake Michael Richards

Here are some diffs that implement a function called TuplesAffected. It
returns the number of tuples the last command affected, or 0 if the last
command was a SELECT. I added it to the PgConnection because it contains

Why not overload PGTuples() instead (assuming it doesn't already do this)?

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.
#7Michael Richards
miker@scifair.acadiau.ca
In reply to: J.M. (#6)
Re: [PATCHES] Patches to get number of tuples affected

On Wed, 30 Jun 1999, D'Arcy J.M. Cain wrote:

Thus spake Michael Richards

Here are some diffs that implement a function called TuplesAffected. It
returns the number of tuples the last command affected, or 0 if the last
command was a SELECT. I added it to the PgConnection because it contains

Why not overload PGTuples() instead (assuming it doesn't already do this)?

Tuples returned tells you how many you can get using the getvalue series.
If you tried that with an update, it core dumps. I think the two are
really related, but fundamentally different.

-Michael

#8Vince Vielhaber
vev@michvhf.com
In reply to: Michael Richards (#7)
Re: [PATCHES] Patches to get number of tuples affected

Darcy said:

Thus spake Michael Richards

Here are some diffs that implement a function called TuplesAffected. It
returns the number of tuples the last command affected, or 0 if the last
command was a SELECT. I added it to the PgConnection because it contains

Why not overload PGTuples() instead (assuming it doesn't already do this)?

Why can't I find PGTuples() anywhere? What/where is it?

Anyway, I'm not sure I agree with TuplesAffected returning a 0 if
PQcmdTuples() returns NULL. An UPDATE can return 0 if it doesn't
update anything. It may be better to just wrap PQcmdTuples() in
pgdatabase.cc (since that's where PQntuples() is wrapped).

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com flame-mail: /dev/null
# include <std/disclaimers.h> TEAM-OS2
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

#9J.M.
darcy@druid.net
In reply to: Vince Vielhaber (#8)
Re: [PATCHES] Patches to get number of tuples affected

Thus spake Vince Vielhaber

Why not overload PGTuples() instead (assuming it doesn't already do this)?

Why can't I find PGTuples() anywhere? What/where is it?

Sorry, I meant PQntuples().

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.
#10J.M.
darcy@druid.net
In reply to: Michael Richards (#7)
Re: [PATCHES] Patches to get number of tuples affected

Thus spake Michael Richards

Why not overload PGTuples() instead (assuming it doesn't already do this)?

As mentioned in another posting, I meant PQntuples().

Tuples returned tells you how many you can get using the getvalue series.
If you tried that with an update, it core dumps. I think the two are
really related, but fundamentally different.

I'm just thinking that it's easy for PQntuples() to tell what it has
to return and branch accordingly. It just makes it easier to remember
the function. No asking which get tuple count function works for update
and which for select.

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.