@@ -1596,7 +1596,8 @@ class Connection(LoggingIdMixin):
15961596 def __init__ (self , att : iAttachment , dsn : str , dpb : bytes = None , sql_dialect : int = 3 ,
15971597 charset : str = None ) -> None :
15981598 self ._att : iAttachment = att
1599- self .__str : str = f'Connection[{ self ._get_db_handle ()} ]'
1599+ self .__handle : a .FB_API_HANDLE = None
1600+ self .__str : str = f'Connection[{ self ._get_handle ().value } ]'
16001601 self .__charset : str = charset
16011602 self .__precision_cache = {}
16021603 self .__sqlsubtype_cache = {}
@@ -1639,16 +1640,16 @@ def __exit__(self, exc_type, exc_value, traceback) -> None:
16391640 self .close ()
16401641 def __repr__ (self ):
16411642 return self .__str
1642- def _get_db_handle (self ) -> int :
1643- isc_status = a . ISC_STATUS_ARRAY ()
1644- db_handle = a .FB_API_HANDLE ( 0 )
1645- api = a .get_api ( )
1646- api . fb_get_database_handle (isc_status , db_handle , self ._att )
1647- if a .db_api_error (isc_status ): # pragma: no cover
1648- raise a .exception_from_status (DatabaseError ,
1649- isc_status ,
1650- "Error in Cursor._unpack_output :fb_get_database_handle()" )
1651- return db_handle . value
1643+ def _get_handle (self ) -> a . FB_API_HANDLE :
1644+ if self . __handle is None :
1645+ isc_status = a .ISC_STATUS_ARRAY ( )
1646+ self . __handle = a .FB_API_HANDLE ( 0 )
1647+ a . get_api (). fb_get_database_handle (isc_status , self . __handle , self ._att )
1648+ if a .db_api_error (isc_status ): # pragma: no cover
1649+ raise a .exception_from_status (DatabaseError ,
1650+ isc_status ,
1651+ "Error in Connection._get_handle :fb_get_database_handle()" )
1652+ return self . __handle
16521653 def __stmt_deleted (self , stmt ) -> None :
16531654 self ._statements .remove (stmt )
16541655 def _close (self ) -> None :
@@ -1791,14 +1792,7 @@ def event_collector(self, event_names: Sequence[str]) -> EventCollector:
17911792 Arguments:
17921793 event_names: Sequence of database event names to whom the collector should be subscribed.
17931794 """
1794- isc_status = a .ISC_STATUS_ARRAY ()
1795- db_handle = a .FB_API_HANDLE (0 )
1796- a .api .fb_get_database_handle (isc_status , db_handle , self ._att )
1797- if a .db_api_error (isc_status ): # pragma: no cover
1798- raise a .exception_from_status (DatabaseError ,
1799- isc_status ,
1800- "Error in Connection.get_events:fb_get_database_handle()" )
1801- conduit = EventCollector (db_handle , event_names )
1795+ conduit = EventCollector (self ._get_handle (), event_names )
18021796 self .__ecollectors .append (conduit )
18031797 return conduit
18041798 def close (self ) -> None :
@@ -2343,6 +2337,7 @@ def __init__(self, connection: Connection, default_tpb: bytes,
23432337 self .default_tpb : bytes = default_tpb
23442338 #: Default action (commit/rollback) to be performed when transaction is closed.
23452339 self .default_action : DefaultAction = default_action
2340+ self .__handle : a .FB_API_HANDLE = None
23462341 self .__info : Union [TransactionInfoProvider , TransactionInfoProvider3 ] = None
23472342 self ._cursors : List = [] # Weak references to cursors
23482343 self ._tra : iTransaction = None
@@ -2377,6 +2372,16 @@ def _finish(self, default_action: DefaultAction=None) -> None:
23772372 self .rollback ()
23782373 finally :
23792374 self ._tra = None
2375+ def _get_handle (self ) -> a .FB_API_HANDLE :
2376+ if self .__handle is None :
2377+ isc_status = a .ISC_STATUS_ARRAY ()
2378+ self .__handle = a .FB_API_HANDLE (0 )
2379+ a .get_api ().fb_get_transaction_handle (isc_status , self .__handle , self ._tra )
2380+ if a .db_api_error (isc_status ): # pragma: no cover
2381+ raise a .exception_from_status (DatabaseError ,
2382+ isc_status ,
2383+ "Error in TransactionManager._get_handle:fb_get_transaction_handle()" )
2384+ return self .__handle
23802385 def close (self ) -> None :
23812386 """Close the transaction manager and release all associated resources.
23822387
@@ -2667,7 +2672,7 @@ def __init__(self, connection: Connection, stmt: iStatement, sql: str, dialect:
26672672 self ._out_meta = meta
26682673 self ._out_buffer = create_string_buffer (meta .get_message_length ())
26692674 self ._out_desc = create_meta_descriptors (meta )
2670- self ._names = [meta .field if meta .field == meta .alias else meta .alias for meta in self ._out_desc ]
2675+ self ._names = [m .field if m .field == m .alias else m .alias for m in self ._out_desc ]
26712676 def __enter__ (self ) -> Statement :
26722677 return self
26732678 def __exit__ (self , exc_type , exc_value , traceback ) -> None :
@@ -3371,21 +3376,11 @@ def _pack_input(self, meta: iMessageMetadata, buffer: bytes,
33713376 arrayid_ptr = pointer (arrayid )
33723377 arraydesc = a .ISC_ARRAY_DESC (0 )
33733378 isc_status = a .ISC_STATUS_ARRAY ()
3374- db_handle = a . FB_API_HANDLE ( 0 )
3375- tr_handle = a . FB_API_HANDLE ( 0 )
3379+ db_handle = self . _connection . _get_handle ( )
3380+ tr_handle = self . _transaction . _get_handle ( )
33763381 relname = in_meta .get_relation (i ).encode (self ._encoding )
33773382 sqlname = in_meta .get_field (i ).encode (self ._encoding )
33783383 api = a .get_api ()
3379- api .fb_get_database_handle (isc_status , db_handle , self ._connection ._att )
3380- if a .db_api_error (isc_status ): # pragma: no cover
3381- raise a .exception_from_status (DatabaseError ,
3382- isc_status ,
3383- "Error in Cursor._pack_input:fb_get_database_handle()" )
3384- api .fb_get_transaction_handle (isc_status , tr_handle , self ._transaction ._tra )
3385- if a .db_api_error (isc_status ): # pragma: no cover
3386- raise a .exception_from_status (DatabaseError ,
3387- isc_status ,
3388- "Error in Cursor._pack_input:fb_get_transaction_handle()" )
33893384 sqlsubtype = self ._connection ._get_array_sqlsubtype (relname , sqlname )
33903385 api .isc_array_lookup_bounds (isc_status , db_handle , tr_handle ,
33913386 relname , sqlname , arraydesc )
@@ -3525,21 +3520,11 @@ def _unpack_output(self) -> Tuple:
35253520 (0 ).from_bytes (val [4 :], 'little' ))
35263521 arraydesc = a .ISC_ARRAY_DESC (0 )
35273522 isc_status = a .ISC_STATUS_ARRAY ()
3528- db_handle = a . FB_API_HANDLE ( 0 )
3529- tr_handle = a . FB_API_HANDLE ( 0 )
3523+ db_handle = self . _connection . _get_handle ( )
3524+ tr_handle = self . _transaction . _get_handle ( )
35303525 relname = desc .relation .encode (self ._encoding )
35313526 sqlname = desc .field .encode (self ._encoding )
35323527 api = a .get_api ()
3533- api .fb_get_database_handle (isc_status , db_handle , self ._connection ._att )
3534- if a .db_api_error (isc_status ): # pragma: no cover
3535- raise a .exception_from_status (DatabaseError ,
3536- isc_status ,
3537- "Error in Cursor._unpack_output:fb_get_database_handle()" )
3538- api .fb_get_transaction_handle (isc_status , tr_handle , self ._transaction ._tra )
3539- if a .db_api_error (isc_status ): # pragma: no cover
3540- raise a .exception_from_status (DatabaseError ,
3541- isc_status ,
3542- "Error in Cursor._unpack_output:fb_get_transaction_handle()" )
35433528 sqlsubtype = self ._connection ._get_array_sqlsubtype (relname , sqlname )
35443529 api .isc_array_lookup_bounds (isc_status , db_handle , tr_handle ,
35453530 relname , sqlname , arraydesc )
0 commit comments