Update of /usr/cvs/Public/pygresql/module
In directory druid.net:/tmp/cvs-serv22969/module

Modified Files:
        pgmodule.c test_pg.py 
Log Message:
The pg module no provides a way to find the number of affected rows for DML 
statements.
To see the diffs for this commit:
   
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pgmodule.c.diff?r1=1.86&r2=1.87

Index: pgmodule.c
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pgmodule.c,v
retrieving revision 1.86
retrieving revision 1.87
diff -u -r1.86 -r1.87
--- pgmodule.c  21 Nov 2008 22:06:34 -0000      1.86
+++ pgmodule.c  21 Nov 2008 23:24:38 -0000      1.87
@@ -1,5 +1,5 @@
 /*
- * $Id: pgmodule.c,v 1.86 2008/11/21 22:06:34 cito Exp $
+ * $Id: pgmodule.c,v 1.87 2008/11/21 23:24:38 cito Exp $
  * PyGres, version 2.2 A Python interface for PostgreSQL database. Written by
  * D'Arcy J.M. Cain, ([EMAIL PROTECTED]).  Based heavily on code written by
  * Pascal Andre, [EMAIL PROTECTED] Copyright (c) 1995, Pascal Andre
@@ -507,7 +507,6 @@
 pgsource_execute(pgsourceobject * self, PyObject * args)
 {
        char            *query;
-       const char *temp;
 
        /* checks validity */
        if (!check_source_obj(self, CHECK_CNX))
@@ -550,6 +549,7 @@
        switch (PQresultStatus(self->last_result))
        {
                long    num_rows;
+               char   *temp;
 
                /* query succeeded */
                case PGRES_TUPLES_OK:   /* DQL: returns None (DB-SIG compliant) 
*/
@@ -564,7 +564,7 @@
                        self->result_type = RESULT_DDL;
                        temp = PQcmdTuples(self->last_result);
                        num_rows = -1;
-                       if (temp[0] != 0)
+                       if (temp[0])
                        {
                                self->result_type = RESULT_DML;
                                num_rows = atol(temp);
@@ -2278,10 +2278,6 @@
        /* checks result status */
        if ((status = PQresultStatus(result)) != PGRES_TUPLES_OK)
        {
-               Oid                     oid = PQoidValue(result);
-
-               PQclear(result);
-
                switch (status)
                {
                        case PGRES_EMPTY_QUERY:
@@ -2292,16 +2288,28 @@
                        case PGRES_NONFATAL_ERROR:
                                PyErr_SetString(ProgrammingError, 
PQerrorMessage(self->cnx));
                                break;
-                       case PGRES_COMMAND_OK:          /* could be an INSERT */
-                               if (oid == InvalidOid)  /* nope */
-                               {
-                                       Py_INCREF(Py_None);
-                                       return Py_None;
+                       case PGRES_COMMAND_OK:
+                               {                                               
/* INSERT, UPDATE, DELETE */
+                                       Oid             oid = 
PQoidValue(result);
+                                       if (oid == InvalidOid)  /* not a single 
insert */
+                                       {
+                                               char    *ret = 
PQcmdTuples(result);
+
+                                               PQclear(result);
+                                               if (ret[0])             /* 
return number of rows affected */
+                                               {
+                                                       return 
PyString_FromString(ret);
+                                               }
+                                               Py_INCREF(Py_None);
+                                               return Py_None;
+                                       }
+                                       /* for a single insert, return the oid 
*/
+                                       PQclear(result);
+                                       return PyInt_FromLong(oid);
                                }
-                               /* otherwise, return the oid */
-                               return PyInt_FromLong(oid);
                        case PGRES_COPY_OUT:            /* no data will be 
received */
                        case PGRES_COPY_IN:
+                               PQclear(result);
                                Py_INCREF(Py_None);
                                return Py_None;
                        default:
@@ -2310,6 +2318,7 @@
                                break;
                }
 
+               PQclear(result);
                return NULL;                    /* error detected on query */
        }
 

   
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/test_pg.py.diff?r1=1.17&r2=1.18

Index: test_pg.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/test_pg.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- test_pg.py  21 Nov 2008 21:17:53 -0000      1.17
+++ test_pg.py  21 Nov 2008 23:24:38 -0000      1.18
@@ -4,7 +4,7 @@
 #
 # Written by Christoph Zwerschke
 #
-# $Id: test_pg.py,v 1.17 2008/11/21 21:17:53 cito Exp $
+# $Id: test_pg.py,v 1.18 2008/11/21 23:24:38 cito Exp $
 #
 
 """Test the classic PyGreSQL interface in the pg module.
@@ -524,6 +524,38 @@
         result = 2
         self.assertEqual(r, result)
 
+    def testQuery(self):
+        smart_ddl(self.c, "drop table test_table")
+        q = "create table test_table (n integer) with oids"
+        r = self.c.query(q)
+        self.assert_(r is None)
+        q = "insert into test_table values (1)"
+        r = self.c.query(q)
+        self.assert_(isinstance(r, int)), r
+        q = "insert into test_table select 2"
+        r = self.c.query(q)
+        self.assert_(isinstance(r, int))
+        oid = r
+        q = "select oid from test_table where n=2"
+        r = self.c.query(q).getresult()
+        self.assertEqual(len(r), 1)
+        r = r[0]
+        self.assertEqual(len(r), 1)
+        r = r[0]
+        self.assertEqual(r, oid)
+        q = "insert into test_table select 3 union select 4 union select 5"
+        r = self.c.query(q)
+        self.assert_(isinstance(r, str))
+        self.assertEqual(r, '3')
+        q = "update test_table set n=4 where n<5"
+        r = self.c.query(q)
+        self.assert_(isinstance(r, str))
+        self.assertEqual(r, '4')
+        q = "delete from test_table"
+        r = self.c.query(q)
+        self.assert_(isinstance(r, str))
+        self.assertEqual(r, '5')
+
     def testPrint(self):
         q = "select 1 as a, 'hello' as h, 'w' as world" \
             " union select 2, 'xyz', 'uvw'"
@@ -842,6 +874,38 @@
         self.assertEqual(f('ab\\c', 'text'), "'ab\\\\c'")
         self.assertEqual(f("a\\b'c", 'text'), "'a\\\\b''c'")
 
+    def testQuery(self):
+        smart_ddl(self.db, "drop table test_table")
+        q = "create table test_table (n integer) with oids"
+        r = self.db.query(q)
+        self.assert_(r is None)
+        q = "insert into test_table values (1)"
+        r = self.db.query(q)
+        self.assert_(isinstance(r, int)), r
+        q = "insert into test_table select 2"
+        r = self.db.query(q)
+        self.assert_(isinstance(r, int))
+        oid = r
+        q = "select oid from test_table where n=2"
+        r = self.db.query(q).getresult()
+        self.assertEqual(len(r), 1)
+        r = r[0]
+        self.assertEqual(len(r), 1)
+        r = r[0]
+        self.assertEqual(r, oid)
+        q = "insert into test_table select 3 union select 4 union select 5"
+        r = self.db.query(q)
+        self.assert_(isinstance(r, str))
+        self.assertEqual(r, '3')
+        q = "update test_table set n=4 where n<5"
+        r = self.db.query(q)
+        self.assert_(isinstance(r, str))
+        self.assertEqual(r, '4')
+        q = "delete from test_table"
+        r = self.db.query(q)
+        self.assert_(isinstance(r, str))
+        self.assertEqual(r, '5')
+
     def testPkey(self):
         smart_ddl(self.db, 'drop table pkeytest0')
         smart_ddl(self.db, "create table pkeytest0 ("

_______________________________________________
PyGreSQL mailing list
[email protected]
http://mailman.vex.net/mailman/listinfo/pygresql

Reply via email to