public inbox for [email protected]  
help / color / mirror / Atom feed
From: Akshay Joshi <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin4][Patch] Add affected row count support in psycopg2
Date: Tue, 12 Apr 2016 15:54:10 +0530
Message-ID: <CANxoLDfMnBJSTgRht5OwSpHdhkzHXmViiGoZOeqbEsLL9VAuyg@mail.gmail.com> (raw)
List-Unsubscribe:  <mailto:[email protected]?body=unsub%20pgadmin-hackers>

Hi All

While implementing Query Tool, we need the affected row count for the
queries (DELETE, SELECT, UPDATE etc..) executed on the database server. I
have implemented the same. Attached is the patch file, please review it and
let me know the review comments if any.


-- 
*Akshay Joshi*
*Principal Software Engineer *



*Phone: +91 20-3058-9517Mobile: +91 976-788-8246*


-- 
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers


Attachments:

  [application/octet-stream] Psycopg2_rows_affected.patch (4.8K, 3-Psycopg2_rows_affected.patch)
  download | inline diff:
diff --git a/web/pgadmin/utils/driver/abstract.py b/web/pgadmin/utils/driver/abstract.py
index e068ca4..2ddc7dd 100644
--- a/web/pgadmin/utils/driver/abstract.py
+++ b/web/pgadmin/utils/driver/abstract.py
@@ -141,6 +141,10 @@ class BaseConnection(object):
     * messages()
       - Implement this method to return the list of the messages/notices from
         the database server.
+
+    * rows_affected()
+      - Implement this method to get the rows affected by the last command
+        executed on the server.
     """
 
     ASYNC_OK = 1
@@ -210,5 +214,9 @@ class BaseConnection(object):
         pass
 
     @abstractmethod
+    def rows_affected(self):
+        pass
+
+    @abstractmethod
     def cancel_transaction(self, conn_id, did=None):
         pass
diff --git a/web/pgadmin/utils/driver/psycopg2/__init__.py b/web/pgadmin/utils/driver/psycopg2/__init__.py
index 8496379..63869e3 100644
--- a/web/pgadmin/utils/driver/psycopg2/__init__.py
+++ b/web/pgadmin/utils/driver/psycopg2/__init__.py
@@ -98,6 +98,9 @@ class Connection(BaseConnection):
     * status_message()
       - Returns the status message returned by the last command executed on the server.
 
+    * rows_affected()
+      - Returns the no of rows affected by the last command executed on the server.
+
     * cancel_transaction(conn_id, did=None)
       - This method is used to cancel the transaction for the
         specified connection id and database id.
@@ -126,6 +129,7 @@ class Connection(BaseConnection):
         self.__async_query_id = None
         self.__backend_pid = None
         self.execution_aborted = False
+        self.row_count = 0
 
         super(Connection, self).__init__()
 
@@ -422,6 +426,7 @@ Attempt to reconnect it failed with the below error:
 
     def execute_scalar(self, query, params=None, formatted_exception_msg=False):
         status, cur = self.__cursor()
+        self.row_count = 0
 
         if not status:
             return False, str(cur)
@@ -451,6 +456,7 @@ Attempt to reconnect it failed with the below error:
                     )
             return False, errmsg
 
+        self.row_count = cur.rowcount
         if cur.rowcount > 0:
             res = cur.fetchone()
             if len(res) > 0:
@@ -518,6 +524,7 @@ Failed to execute query (execute_async) for the server #{server_id} - {conn_id}
             formatted_exception_msg: if True then function return the formatted exception message
         """
         status, cur = self.__cursor()
+        self.row_count = 0
 
         if not status:
             return False, str(cur)
@@ -551,10 +558,13 @@ Failed to execute query (execute_void) for the server #{server_id} - {conn_id}
             )
             return False, errmsg
 
+        self.row_count = cur.rowcount
+
         return True, None
 
     def execute_2darray(self, query, params=None, formatted_exception_msg=False):
         status, cur = self.__cursor()
+        self.row_count = 0
 
         if not status:
             return False, str(cur)
@@ -591,6 +601,7 @@ Failed to execute query (execute_void) for the server #{server_id} - {conn_id}
                 ] or []
 
         rows = []
+        self.row_count = cur.rowcount
         if cur.rowcount > 0:
             for row in cur:
                 rows.append(row)
@@ -599,6 +610,7 @@ Failed to execute query (execute_void) for the server #{server_id} - {conn_id}
 
     def execute_dict(self, query, params=None, formatted_exception_msg=False):
         status, cur = self.__cursor()
+        self.row_count = 0
 
         if not status:
             return False, str(cur)
@@ -633,6 +645,7 @@ Failed to execute query (execute_void) for the server #{server_id} - {conn_id}
                 ] or []
 
         rows = []
+        self.row_count = cur.rowcount
         if cur.rowcount > 0:
             for row in cur:
                 rows.append(dict(row))
@@ -789,6 +802,7 @@ Failed to reset the connection of the server due to following error:
 
         colinfo = None
         result = None
+        self.row_count = 0
         if status == self.ASYNC_OK:
 
             # if user has cancelled the transaction then changed the status
@@ -801,6 +815,7 @@ Failed to reset the connection of the server due to following error:
             if cur.description is not None:
                 colinfo = [desc for desc in cur.description]
 
+            self.row_count = cur.rowcount
             if cur.rowcount > 0:
                 result = []
 
@@ -834,6 +849,14 @@ Failed to reset the connection of the server due to following error:
 
         return cur.statusmessage
 
+    def rows_affected(self):
+        """
+        This function will return the no of rows affected by the last command
+        executed on the server.
+        """
+
+        return self.row_count
+
     def cancel_transaction(self, conn_id, did=None):
         """
         This function is used to cancel the running transaction


view thread (2+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: [pgAdmin4][Patch] Add affected row count support in psycopg2
  In-Reply-To: <CANxoLDfMnBJSTgRht5OwSpHdhkzHXmViiGoZOeqbEsLL9VAuyg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox