Skip to content

Commit ecdf3b4

Browse files
Added properties on the Connection object to determine the database
domain name, the database name, the maximum number of open cursors, the service name and whether a transaction is in progress or not.
1 parent 673a0f4 commit ecdf3b4

File tree

9 files changed

+252
-0
lines changed

9 files changed

+252
-0
lines changed

doc/src/api_manual/connection.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,30 @@ Connection Attributes
609609

610610
This attribute is an extension to the DB API definition.
611611

612+
.. attribute:: Connection.db_domain
613+
614+
This read-only attribute specifies the Oracle Database domain name
615+
associated with the connection. It is the same value returned by the SQL
616+
``SELECT value FROM V$PARAMETER WHERE NAME = 'db_domain'``.
617+
618+
.. versionadded:: 2.0.0
619+
620+
.. note::
621+
622+
This attribute is an extension to the DB API definition.
623+
624+
.. attribute:: Connection.db_name
625+
626+
This read-only attribute specifies the Oracle Database name associated with
627+
the connection. It is the same value returned by the SQL
628+
``SELECT NAME FROM V$DATABASE``.
629+
630+
.. versionadded:: 2.0.0
631+
632+
.. note::
633+
634+
This attribute is an extension to the DB API definition.
635+
612636
.. attribute:: Connection.dbop
613637

614638
This write-only attribute sets the database operation that is to be
@@ -742,6 +766,19 @@ Connection Attributes
742766
This attribute is an extension to the DB API definition.
743767

744768

769+
.. attribute:: Connection.max_open_cursors
770+
771+
This read-only attribute specifies the maximum number of cursors that the
772+
database can have open concurrently. It is the same value returned by the
773+
SQL ``SELECT VALUE FROM V$PARAMETER WHERE NAME = 'open_cursors'``.
774+
775+
.. versionadded:: 2.0.0
776+
777+
.. note::
778+
779+
This attribute is an extension to the DB API definition.
780+
781+
745782
.. attribute:: Connection.module
746783

747784
This write-only attribute sets the module column in the v$session table.
@@ -784,6 +821,18 @@ Connection Attributes
784821

785822
This attribute is an extension to the DB API definition.
786823

824+
.. attribute:: Connection.service_name
825+
826+
This read-only attribute specifies the Oracle Database service name
827+
associated with the connection. This is the same value returned by the SQL
828+
``SELECT SYS_CONTEXT('USERENV', 'SERVICE_NAME') FROM DUAL``.
829+
830+
.. versionadded:: 2.0.0
831+
832+
.. note::
833+
834+
This attribute is an extension to the DB API definition.
835+
787836
.. attribute:: Connection.stmtcachesize
788837

789838
This read-write attribute specifies the size of the statement cache. This
@@ -837,6 +886,17 @@ Connection Attributes
837886

838887
This attribute is an extension to the DB API definition.
839888

889+
.. attribute:: Connection.transaction_in_progress
890+
891+
This read-only attribute specifies whether a transaction is currently in
892+
progress on the database associated with the connection.
893+
894+
.. versionadded:: 2.0.0
895+
896+
.. note::
897+
898+
This attribute is an extension to the DB API definition.
899+
840900
.. attribute:: Connection.username
841901

842902
This read-only attribute returns the name of the user which established the

doc/src/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Common Changes
2525
++++++++++++++
2626

2727
#) Dropped support for Python 3.6 and added support for Python 3.12.
28+
#) Added properties that provide information about the database:
29+
:attr:`Connection.db_domain`, :attr:`Connection.db_name`,
30+
:attr:`Connection.max_open_cursors`, :attr:`Connection.service_name`
31+
and :attr:`Connection.transaction_in_progress`.
2832
#) Added attributes :data:`FetchInfo.domain_schema`,
2933
:data:`FetchInfo.domain_name` and :data:`FetchInfo.annotations` for the
3034
`SQL domain <https://docs.oracle.com/en/database/oracle/oracle-database/

src/oracledb/connection.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,22 @@ def econtext_id(self, value: str) -> None:
405405
self._verify_connected()
406406
self._impl.set_econtext_id(value)
407407

408+
@property
409+
def db_domain(self) -> str:
410+
"""
411+
Specifies the name of the database domain.
412+
"""
413+
self._verify_connected()
414+
return self._impl.get_db_domain()
415+
416+
@property
417+
def db_name(self) -> str:
418+
"""
419+
Specifies the name of the database.
420+
"""
421+
self._verify_connected()
422+
return self._impl.get_db_name()
423+
408424
@property
409425
def edition(self) -> str:
410426
"""
@@ -547,6 +563,15 @@ def maxBytesPerCharacter(self) -> int:
547563
"""
548564
return 4
549565

566+
@property
567+
def max_open_cursors(self) -> int:
568+
"""
569+
Specifies the maximum number of cursors that the database can have open
570+
concurrently.
571+
"""
572+
self._verify_connected()
573+
return self._impl.get_max_open_cursors()
574+
550575
@property
551576
def module(self) -> None:
552577
raise AttributeError("module is not readable")
@@ -682,6 +707,15 @@ def rollback(self) -> None:
682707
self._verify_connected()
683708
self._impl.rollback()
684709

710+
@property
711+
def service_name(self) -> str:
712+
"""
713+
Specifies the name of the service that was used to connect to the
714+
database.
715+
"""
716+
self._verify_connected()
717+
return self._impl.get_service_name()
718+
685719
def shutdown(self, mode: int = 0) -> None:
686720
"""
687721
Shutdown the database. In order to do this the connection must be
@@ -1004,6 +1038,15 @@ def tpc_rollback(self, xid: Xid = None) -> None:
10041038
self._verify_xid(xid)
10051039
self._impl.tpc_rollback(xid)
10061040

1041+
@property
1042+
def transaction_in_progress(self) -> bool:
1043+
"""
1044+
Specifies whether a transaction is currently in progress on the
1045+
database using this connection.
1046+
"""
1047+
self._verify_connected()
1048+
return self._impl.get_transaction_in_progress()
1049+
10071050
def unsubscribe(self, subscr: Subscription) -> None:
10081051
"""
10091052
Unsubscribe from events in the database that were originally subscribed

src/oracledb/impl/base/connection.pyx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ cdef class BaseConnImpl:
211211
def get_current_schema(self):
212212
pass
213213

214+
@utils.CheckImpls("getting the database domain name")
215+
def get_db_domain(self):
216+
pass
217+
218+
@utils.CheckImpls("getting the database name")
219+
def get_db_name(self):
220+
pass
221+
214222
@utils.CheckImpls("getting the edition")
215223
def get_edition(self):
216224
pass
@@ -235,10 +243,22 @@ cdef class BaseConnImpl:
235243
def get_ltxid(self):
236244
pass
237245

246+
@utils.CheckImpls("getting the maximum number of open cursors")
247+
def get_max_open_cursors(self):
248+
pass
249+
250+
@utils.CheckImpls("getting the service name")
251+
def get_service_name(self):
252+
pass
253+
238254
@utils.CheckImpls("getting the statement cache size")
239255
def get_stmt_cache_size(self):
240256
pass
241257

258+
@utils.CheckImpls("getting if a transaction is in progress")
259+
def get_transaction_in_progress(self):
260+
pass
261+
242262
@utils.CheckImpls("getting an object type")
243263
def get_type(self, object conn, str name):
244264
pass

src/oracledb/impl/thick/connection.pyx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,24 @@ cdef class ThickConnImpl(BaseConnImpl):
466466
if value is not NULL:
467467
return value[:value_length].decode()
468468

469+
def get_db_domain(self):
470+
cdef:
471+
uint32_t value_length
472+
const char *value
473+
if dpiConn_getDbDomain(self._handle, &value, &value_length) < 0:
474+
_raise_from_odpi()
475+
if value is not NULL:
476+
return value[:value_length].decode()
477+
478+
def get_db_name(self):
479+
cdef:
480+
uint32_t value_length
481+
const char *value
482+
if dpiConn_getDbName(self._handle, &value, &value_length) < 0:
483+
_raise_from_odpi()
484+
if value is not NULL:
485+
return value[:value_length].decode()
486+
469487
def get_edition(self):
470488
cdef:
471489
uint32_t value_length
@@ -516,12 +534,33 @@ cdef class ThickConnImpl(BaseConnImpl):
516534
_raise_from_odpi()
517535
return value[:value_length]
518536

537+
def get_max_open_cursors(self):
538+
cdef uint32_t value
539+
if dpiConn_getMaxOpenCursors(self._handle, &value) < 0:
540+
_raise_from_odpi()
541+
return value
542+
543+
def get_service_name(self):
544+
cdef:
545+
uint32_t value_length
546+
const char *value
547+
if dpiConn_getServiceName(self._handle, &value, &value_length) < 0:
548+
_raise_from_odpi()
549+
if value is not NULL:
550+
return value[:value_length].decode()
551+
519552
def get_stmt_cache_size(self):
520553
cdef uint32_t value
521554
if dpiConn_getStmtCacheSize(self._handle, &value) < 0:
522555
_raise_from_odpi()
523556
return value
524557

558+
def get_transaction_in_progress(self):
559+
cdef bint value
560+
if dpiConn_getTransactionInProgress(self._handle, &value) < 0:
561+
_raise_from_odpi()
562+
return value
563+
525564
def get_type(self, object conn, str name):
526565
cdef:
527566
dpiObjectType *handle

src/oracledb/impl/thick/odpi.pxd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ cdef extern from "impl/thick/odpi/embed/dpi.c":
572572
int dpiConn_getCurrentSchema(dpiConn *conn, const char **value,
573573
uint32_t *valueLength) nogil
574574

575+
int dpiConn_getDbDomain(dpiConn *conn, const char **value,
576+
uint32_t *valueLength) nogil
577+
578+
int dpiConn_getDbName(dpiConn *conn, const char **value,
579+
uint32_t *valueLength) nogil
580+
575581
int dpiConn_getEdition(dpiConn *conn, const char **value,
576582
uint32_t *valueLength) nogil
577583

@@ -591,6 +597,9 @@ cdef extern from "impl/thick/odpi/embed/dpi.c":
591597
int dpiConn_getLTXID(dpiConn *conn, const char **value,
592598
uint32_t *valueLength) nogil
593599

600+
int dpiConn_getMaxOpenCursors(dpiConn *conn,
601+
uint32_t *maxOpenCursors) nogil
602+
594603
int dpiConn_getOciAttr(dpiConn *conn, uint32_t handleType,
595604
uint32_t attribute, dpiDataBuffer *value,
596605
uint32_t *valueLength) nogil
@@ -601,10 +610,16 @@ cdef extern from "impl/thick/odpi/embed/dpi.c":
601610
int dpiConn_getServerVersion(dpiConn *conn, const char **releaseString,
602611
uint32_t *releaseStringLength, dpiVersionInfo *versionInfo) nogil
603612

613+
int dpiConn_getServiceName(dpiConn *conn, const char **value,
614+
uint32_t *valueLength) nogil
615+
604616
int dpiConn_getSodaDb(dpiConn *conn, dpiSodaDb **db) nogil
605617

606618
int dpiConn_getStmtCacheSize(dpiConn *conn, uint32_t *cacheSize) nogil
607619

620+
int dpiConn_getTransactionInProgress(dpiConn *conn,
621+
bint *txnInProgress) nogil
622+
608623
int dpiConn_getCallTimeout(dpiConn *conn, uint32_t *value) nogil
609624

610625
int dpiConn_newMsgProps(dpiConn *conn, dpiMsgProps **props) nogil

src/oracledb/impl/thin/connection.pyx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ cdef class ThinConnImpl(BaseConnImpl):
5353
bytes _ltxid
5454
str _current_schema
5555
bint _current_schema_modified
56+
uint32_t _max_open_cursors
57+
str _db_domain
58+
str _db_name
5659
str _edition
5760
str _instance_name
5861
str _internal_name
5962
str _external_name
63+
str _service_name
6064
array.array _cursors_to_close
6165
ssize_t _num_cursors_to_close
6266
bint _drcp_enabled
@@ -306,6 +310,12 @@ cdef class ThinConnImpl(BaseConnImpl):
306310
def get_current_schema(self):
307311
return self._current_schema
308312

313+
def get_db_domain(self):
314+
return self._db_domain
315+
316+
def get_db_name(self):
317+
return self._db_name
318+
309319
def get_edition(self):
310320
return self._edition
311321

@@ -325,9 +335,18 @@ cdef class ThinConnImpl(BaseConnImpl):
325335
def get_ltxid(self):
326336
return self._ltxid or b''
327337

338+
def get_max_open_cursors(self):
339+
return self._max_open_cursors
340+
341+
def get_service_name(self):
342+
return self._service_name
343+
328344
def get_stmt_cache_size(self):
329345
return self._statement_cache_size
330346

347+
def get_transaction_in_progress(self):
348+
return self._protocol._txn_in_progress
349+
331350
def get_type(self, object conn, str name):
332351
cdef ThinDbObjectTypeCache cache = \
333352
get_dbobject_type_cache(self._dbobject_type_cache_num)

src/oracledb/impl/thin/messages.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,8 +1519,17 @@ cdef class AuthMessage(Message):
15191519
<uint32_t> int(self.session_data["AUTH_SESSION_ID"])
15201520
self.conn_impl._serial_num = \
15211521
<uint32_t> int(self.session_data["AUTH_SERIAL_NUM"])
1522+
self.conn_impl._db_domain = \
1523+
self.session_data.get("AUTH_SC_DB_DOMAIN")
1524+
self.conn_impl._db_name = \
1525+
self.session_data.get("AUTH_SC_DBUNIQUE_NAME")
1526+
self.conn_impl._max_open_cursors = \
1527+
int(self.session_data.get("AUTH_MAX_OPEN_CURSORS"))
1528+
self.conn_impl._service_name = \
1529+
self.session_data.get("AUTH_SC_SERVICE_NAME")
15221530
self.conn_impl._instance_name = \
15231531
self.session_data.get("AUTH_INSTANCENAME")
1532+
15241533
self.conn_impl._server_version = \
15251534
"%d.%d.%d.%d.%d" % self._get_version_tuple(buf)
15261535

0 commit comments

Comments
 (0)