> > >> 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)?
> 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? You're right, it's impossible to monitor the commands. I though that maybe the server would notify the client on such changes, but this also seems unlikely. > And why do want to avoid the query? How often do you plan to make this > call? Here's the story: I use persistent connections under Apache::DBI and I have many databases on the same server. To keep them persistent, I need one connection per database per apache process, risking to hit the MySQL connection limit. So I thought to keep one connection *per database server*, and each time I want to query db1 but it's not the current one, I do "USE db1". If I don't have the info about what is the current db, I need to do "USE db" before *all queries*, which sounds not very efficient. Keeping track of the current db in the application level is possible, but you have to take care of annoying stuff like automatic reconnects. So now I changed plan, I always connect to a default db on the server and I use fully qualified table names (... FROM db.table) in all queries. Btw, if anybody believes that fully qualified table names have a performance overhead in MySQL, I'd be glad to know. Kostas