FirebirdSQL / python3-driver

Firebird driver for Python that uses new Firebird API
https://www.firebirdsql.org/en/devel-python-driver/
MIT License
26 stars 10 forks source link

Share db_handle between native application and embedded python script #32

Closed arkinform closed 7 months ago

arkinform commented 8 months ago

We want to embed python scripting in our native application (Delphi, FreePascal). Firebird connections are managed by our application but we want to use the same connections inside embedded python scripts. Embedded python shares the same process and firebird3_driver uses native firebird client library, so I think it should be possible.

I have done some research but I have not found suitable parameters in the method "connect" in src/firebird/driver/core.py. I plunged further into "__make_connection" and "provider.attach_database" and realized that python firebird3_driver uses the new object-oriented Firebird API. Is it possible to implement connection sharing via db_handle with current implementation of firebird3_driver?

AlexPeshkoff commented 8 months ago

Current API has limited set of conversions of old handles <-> new interfaces. Not to say it's problematic, we just did not want to pollute API with suspicious functions. Particularly we have:

ISC_STATUS ISC_EXPORT fb_get_database_handle(ISC_STATUS, isc_db_handle, void*);

i.e. one can get DB handle from interface pointer (here it's void*, 3-rd parameter). Vice versa requires new API function. I.e. right now only export of DB handle from python driver is possible.

pcisar commented 7 months ago

You can do this trick with adapted legacy FDB driver and custom Connection and your own connect() function. Take a look at FDB connect(), especially the end where Connection is created. The Connection accepts DB handle in constructor, along with other parameters. However, you need to adapt the Connection to do not destroy / close the DB handle in __close() method. As it's pryvate, using descendant Connection class will not work, so you need to fork the FDB driver and make changes you need.