On 2008-02-04 15:23:26 +0100, Kostas Chatzikokolakis wrote: > >> I'm using a dbi handle that is shared between many packages in my > >> code. Some package might do a "USE db_name" to change the current > >> database of the connection. Can I retrieve the current database name > >> from the handle, either from DBI or from the DBD::mysql driver > >> (without querying the server)? > > > > What's wrong with 'perldoc DBI'? > > > > What's the name attribute of a database handle documented as doing? > > > > Does it not work for you? > > Hello Jonathan, thanks for your reply. > > From perldoc: > > > Name [...] > > Usually (and recommended to be) the same as the "dbi:DriverName:..." string > > used to connect to > > the database, but with the leading "dbi:DriverName:" removed. > > So Name returns the dsn used to connect, it is not meant to be the > "current" database (it's not updated when changing database by doing > "USE <db-name>"). I want something that is dynamic, similar to doing a > "SELECT DATABASE()" query in mysql, but without doing an actual query.
I don't think this is possible in general. It's certainly not possible in the DBI, as the SQL commands to change the "database" (or "schema", or whatever your RDBMS calls it[1]) is different between databases (in Oracle it's "alter session set current_schema = $scheme_name"). It may be possible in the driver which could check for these commands and remember the current database. But even that's not guaranteed to work: What happens if this command is hidden inside a stored procedure? And why do want to avoid the query? How often do you plan to make this call? hp [1] And these aren't really the same, anyway. -- _ | Peter J. Holzer | If I wanted to be "academically correct", |_|_) | Sysadmin WSR | I'd be programming in Java. | | | [EMAIL PROTECTED] | I don't, and I'm not. __/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users
pgpap6Z0pOepq.pgp
Description: PGP signature