mirror of
				https://github.com/psycopg/psycopg2.git
				synced 2025-11-04 09:47:30 +03:00 
			
		
		
		
	Added other members to the ConnectionInfo class
Starting deprecating softly some of the methods bloating the connection class.
This commit is contained in:
		
							parent
							
								
									0a04c8892d
								
							
						
					
					
						commit
						1ac6359fef
					
				| 
						 | 
				
			
			@ -342,6 +342,9 @@ The ``connection`` class
 | 
			
		|||
        obscured.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. rubric:: Transaction control methods and attributes.
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Transaction; Autocommit
 | 
			
		||||
        pair: Transaction; Isolation level
 | 
			
		||||
| 
						 | 
				
			
			@ -672,56 +675,6 @@ The ``connection`` class
 | 
			
		|||
        .. versionadded:: 2.7
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Transaction; Status
 | 
			
		||||
 | 
			
		||||
    .. method:: get_transaction_status()
 | 
			
		||||
 | 
			
		||||
        Return the current session transaction status as an integer.  Symbolic
 | 
			
		||||
        constants for the values are defined in the module
 | 
			
		||||
        `psycopg2.extensions`: see :ref:`transaction-status-constants`
 | 
			
		||||
        for the available values.
 | 
			
		||||
 | 
			
		||||
        .. seealso:: libpq docs for `PQtransactionStatus()`__ for details.
 | 
			
		||||
 | 
			
		||||
            .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQTRANSACTIONSTATUS
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Protocol; Version
 | 
			
		||||
 | 
			
		||||
    .. attribute:: protocol_version
 | 
			
		||||
 | 
			
		||||
        A read-only integer representing frontend/backend protocol being used.
 | 
			
		||||
        Currently Psycopg supports only protocol 3, which allows connection
 | 
			
		||||
        to PostgreSQL server from version 7.4. Psycopg versions previous than
 | 
			
		||||
        2.3 support both protocols 2 and 3.
 | 
			
		||||
 | 
			
		||||
        .. seealso:: libpq docs for `PQprotocolVersion()`__ for details.
 | 
			
		||||
 | 
			
		||||
            .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQPROTOCOLVERSION
 | 
			
		||||
 | 
			
		||||
        .. versionadded:: 2.0.12
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Server; Version
 | 
			
		||||
 | 
			
		||||
    .. attribute:: server_version
 | 
			
		||||
 | 
			
		||||
        A read-only integer representing the backend version.
 | 
			
		||||
 | 
			
		||||
        The number is formed by converting the major, minor, and revision
 | 
			
		||||
        numbers into two-decimal-digit numbers and appending them together.
 | 
			
		||||
        For example, version 8.1.5 will be returned as ``80105``.
 | 
			
		||||
 | 
			
		||||
        .. seealso:: libpq docs for `PQserverVersion()`__ for details.
 | 
			
		||||
 | 
			
		||||
            .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQSERVERVERSION
 | 
			
		||||
 | 
			
		||||
        .. versionadded:: 2.0.12
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Connection; Status
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -779,6 +732,7 @@ The ``connection`` class
 | 
			
		|||
            support.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. rubric:: Methods related to asynchronous support.
 | 
			
		||||
 | 
			
		||||
    .. versionadded:: 2.2.0
 | 
			
		||||
| 
						 | 
				
			
			@ -825,6 +779,74 @@ The ``connection`` class
 | 
			
		|||
        Return `!True` if the connection is executing an asynchronous operation.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. rubric:: informative methods of the native connection
 | 
			
		||||
 | 
			
		||||
    .. note:: 
 | 
			
		||||
 | 
			
		||||
        These methods are better accessed using the `~connection.info`
 | 
			
		||||
        attributes and may be dropped in future versions.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Transaction; Status
 | 
			
		||||
 | 
			
		||||
    .. method:: get_transaction_status()
 | 
			
		||||
 | 
			
		||||
        Also available as `~connection.info`\ `!.`\
 | 
			
		||||
        `~psycopg2.extensions.ConnectionInfo.transaction_status`.
 | 
			
		||||
 | 
			
		||||
        Return the current session transaction status as an integer.  Symbolic
 | 
			
		||||
        constants for the values are defined in the module
 | 
			
		||||
        `psycopg2.extensions`: see :ref:`transaction-status-constants`
 | 
			
		||||
        for the available values.
 | 
			
		||||
 | 
			
		||||
        .. seealso:: libpq docs for `PQtransactionStatus()`__ for details.
 | 
			
		||||
 | 
			
		||||
            .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQTRANSACTIONSTATUS
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Protocol; Version
 | 
			
		||||
 | 
			
		||||
    .. attribute:: protocol_version
 | 
			
		||||
 | 
			
		||||
        Also available as `~connection.info`\ `!.`\
 | 
			
		||||
        `~psycopg2.extensions.ConnectionInfo.protocol_version`.
 | 
			
		||||
 | 
			
		||||
        A read-only integer representing frontend/backend protocol being used.
 | 
			
		||||
        Currently Psycopg supports only protocol 3, which allows connection
 | 
			
		||||
        to PostgreSQL server from version 7.4. Psycopg versions previous than
 | 
			
		||||
        2.3 support both protocols 2 and 3.
 | 
			
		||||
 | 
			
		||||
        .. seealso:: libpq docs for `PQprotocolVersion()`__ for details.
 | 
			
		||||
 | 
			
		||||
            .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQPROTOCOLVERSION
 | 
			
		||||
 | 
			
		||||
        .. versionadded:: 2.0.12
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    .. index::
 | 
			
		||||
        pair: Server; Version
 | 
			
		||||
 | 
			
		||||
    .. attribute:: server_version
 | 
			
		||||
 | 
			
		||||
        Also available as `~connection.info`\ `!.`\
 | 
			
		||||
        `~psycopg2.extensions.ConnectionInfo.server_version`.
 | 
			
		||||
 | 
			
		||||
        A read-only integer representing the backend version.
 | 
			
		||||
 | 
			
		||||
        The number is formed by converting the major, minor, and revision
 | 
			
		||||
        numbers into two-decimal-digit numbers and appending them together.
 | 
			
		||||
        For example, version 8.1.5 will be returned as ``80105``.
 | 
			
		||||
 | 
			
		||||
        .. seealso:: libpq docs for `PQserverVersion()`__ for details.
 | 
			
		||||
 | 
			
		||||
            .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQSERVERVERSION
 | 
			
		||||
 | 
			
		||||
        .. versionadded:: 2.0.12
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. testcode::
 | 
			
		||||
    :hide:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -166,6 +166,21 @@ introspection etc.
 | 
			
		|||
    .. autoattribute:: port
 | 
			
		||||
    .. autoattribute:: options
 | 
			
		||||
    .. autoattribute:: status
 | 
			
		||||
    .. autoattribute:: transaction_status
 | 
			
		||||
 | 
			
		||||
    .. autoattribute:: protocol_version
 | 
			
		||||
 | 
			
		||||
        Currently Psycopg supports only protocol 3, which allows connection
 | 
			
		||||
        to PostgreSQL server from version 7.4. Psycopg versions previous than
 | 
			
		||||
        2.3 support both protocols 2 and 3.
 | 
			
		||||
 | 
			
		||||
    .. autoattribute:: server_version
 | 
			
		||||
 | 
			
		||||
        The number is formed by converting the major, minor, and revision
 | 
			
		||||
        numbers into two-decimal-digit numbers and appending them together.
 | 
			
		||||
        After PostgreSQL 10 the minor version was dropped, so the second group
 | 
			
		||||
        of digits is always ``00``. For example, version 9.3.5 will be
 | 
			
		||||
        returned as ``90305``, version 10.2 as ``100002``.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -167,7 +167,7 @@ options_get(connInfoObject *self)
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
static const char status_doc[] =
 | 
			
		||||
"Return the status of the connection.\n"
 | 
			
		||||
"The status of the connection.\n"
 | 
			
		||||
"\n"
 | 
			
		||||
".. seealso:: libpq docs for `PQstatus()`__ for details.\n"
 | 
			
		||||
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
 | 
			
		||||
| 
						 | 
				
			
			@ -183,6 +183,61 @@ status_get(connInfoObject *self)
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const char transaction_status_doc[] =
 | 
			
		||||
"The current in-transaction status of the connection.\n"
 | 
			
		||||
"\n"
 | 
			
		||||
"Symbolic constants for the values are defined in the module\n"
 | 
			
		||||
"`psycopg2.extensions`: see :ref:`transaction-status-constants` for the\n"
 | 
			
		||||
"available values.\n"
 | 
			
		||||
"\n"
 | 
			
		||||
".. seealso:: libpq docs for `PQtransactionStatus()`__ for details.\n"
 | 
			
		||||
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
 | 
			
		||||
    "#LIBPQ-PQTRANSACTIONSTATUS";
 | 
			
		||||
 | 
			
		||||
static PyObject *
 | 
			
		||||
transaction_status_get(connInfoObject *self)
 | 
			
		||||
{
 | 
			
		||||
    PGTransactionStatusType val;
 | 
			
		||||
 | 
			
		||||
    val = PQtransactionStatus(self->conn->pgconn);
 | 
			
		||||
    return PyInt_FromLong((long)val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const char protocol_version_doc[] =
 | 
			
		||||
"The frontend/backend protocol being used.\n"
 | 
			
		||||
"\n"
 | 
			
		||||
".. seealso:: libpq docs for `PQprotocolVersion()`__ for details.\n"
 | 
			
		||||
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
 | 
			
		||||
    "#LIBPQ-PQPROTOCOLVERSION";
 | 
			
		||||
 | 
			
		||||
static PyObject *
 | 
			
		||||
protocol_version_get(connInfoObject *self)
 | 
			
		||||
{
 | 
			
		||||
    int val;
 | 
			
		||||
 | 
			
		||||
    val = PQprotocolVersion(self->conn->pgconn);
 | 
			
		||||
    return PyInt_FromLong((long)val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const char server_version_doc[] =
 | 
			
		||||
"Returns an integer representing the server version.\n"
 | 
			
		||||
"\n"
 | 
			
		||||
".. seealso:: libpq docs for `PQserverVersion()`__ for details.\n"
 | 
			
		||||
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
 | 
			
		||||
    "#LIBPQ-PQSERVERVERSION";
 | 
			
		||||
 | 
			
		||||
static PyObject *
 | 
			
		||||
server_version_get(connInfoObject *self)
 | 
			
		||||
{
 | 
			
		||||
    int val;
 | 
			
		||||
 | 
			
		||||
    val = PQserverVersion(self->conn->pgconn);
 | 
			
		||||
    return PyInt_FromLong((long)val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static struct PyGetSetDef connInfoObject_getsets[] = {
 | 
			
		||||
    { "dbname", (getter)dbname_get, NULL, (char *)dbname_doc },
 | 
			
		||||
    { "user", (getter)user_get, NULL, (char *)user_doc },
 | 
			
		||||
| 
						 | 
				
			
			@ -191,6 +246,12 @@ static struct PyGetSetDef connInfoObject_getsets[] = {
 | 
			
		|||
    { "port", (getter)port_get, NULL, (char *)port_doc },
 | 
			
		||||
    { "options", (getter)options_get, NULL, (char *)options_doc },
 | 
			
		||||
    { "status", (getter)status_get, NULL, (char *)status_doc },
 | 
			
		||||
    { "transaction_status", (getter)transaction_status_get, NULL,
 | 
			
		||||
        (char *)transaction_status_doc },
 | 
			
		||||
    { "protocol_version", (getter)protocol_version_get, NULL,
 | 
			
		||||
        (char *)protocol_version_doc },
 | 
			
		||||
    { "server_version", (getter)server_version_get, NULL,
 | 
			
		||||
        (char *)server_version_doc },
 | 
			
		||||
    {NULL}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1699,7 +1699,9 @@ class TestConnectionInfo(ConnectingTestCase):
 | 
			
		|||
        self.assert_(self.bconn.info.dbname is None)
 | 
			
		||||
 | 
			
		||||
    def test_user(self):
 | 
			
		||||
        self.assert_(isinstance(self.conn.info.user, str))
 | 
			
		||||
        cur = self.conn.cursor()
 | 
			
		||||
        cur.execute("select user")
 | 
			
		||||
        self.assertEqual(self.conn.info.user, cur.fetchone()[0])
 | 
			
		||||
        self.assert_(self.bconn.info.user is None)
 | 
			
		||||
 | 
			
		||||
    def test_password(self):
 | 
			
		||||
| 
						 | 
				
			
			@ -1727,6 +1729,29 @@ class TestConnectionInfo(ConnectingTestCase):
 | 
			
		|||
        self.assertEqual(self.conn.info.status, 0)
 | 
			
		||||
        self.assertEqual(self.bconn.info.status, 1)
 | 
			
		||||
 | 
			
		||||
    def test_transaction_status(self):
 | 
			
		||||
        self.assertEqual(self.conn.info.transaction_status, 0)
 | 
			
		||||
        cur = self.conn.cursor()
 | 
			
		||||
        cur.execute("select 1")
 | 
			
		||||
        self.assertEqual(self.conn.info.transaction_status, 2)
 | 
			
		||||
        self.assertEqual(self.bconn.info.transaction_status, 4)
 | 
			
		||||
 | 
			
		||||
    def test_protocol_version(self):
 | 
			
		||||
        self.assertEqual(self.conn.info.protocol_version, 3)
 | 
			
		||||
        self.assertEqual(self.bconn.info.protocol_version, 0)
 | 
			
		||||
 | 
			
		||||
    def test_server_version(self):
 | 
			
		||||
        cur = self.conn.cursor()
 | 
			
		||||
        try:
 | 
			
		||||
            cur.execute("show server_version_num")
 | 
			
		||||
        except psycopg2.DatabaseError:
 | 
			
		||||
            self.assert_(isinstance(self.conn.info.server_version, int))
 | 
			
		||||
        else:
 | 
			
		||||
            self.assertEqual(
 | 
			
		||||
                self.conn.info.server_version, int(cur.fetchone()[0]))
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(self.bconn.info.server_version, 0)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_suite():
 | 
			
		||||
    return unittest.TestLoader().loadTestsFromName(__name__)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user