2 messages in com.mysql.lists.mysqlwanted: mysql_select_db with a NULL (...
FromSent OnAttachments
terry jones02 Jul 1999 16:46 
Paul DuBois03 Jul 1999 07:47 
Subject:wanted: mysql_select_db with a NULL (or empty) db
From:terry jones (ter@cliffs.ucsd.edu)
Date:07/02/1999 04:46:57 PM
List:com.mysql.lists.mysql

I'd like to be able to select "no" database via a call to mysql_select_db.

Here's my reasoning. I'm writing an API that allows people to do things with MySQL databases. When they first connect, they have not selected a database. This is the state I'd like to be able to return to.

My application allows the caller to switch to another db (via mysql_select_db), but it is possible that the db selected is not a db created by my library. I don't want to leave the connection with a selected db that is not one my application created.

Instead, when they ask me to select a new db, I'd like to call mysql_select_db and look at the tables in that database and if they are not the ones I expect to see, unselect that db. I don't want to do something like selecting (or making) a "test" db, because apart from being a kludge it may not exist and/or I may not have permission to do that. It would be cleanest if I could just call mysql_select_db(mysql, NULL) and have my connection return to the way it was when I first called mysql_connect. Presumably this would be quite easily to implement.

Leaving my client connected to a database that was not created by my application is dangerous. I'd rather not have to keep track of whether the client has selected a valid (i.e., created by my application) db and test it before every potentially dangerous call.

You might argue that the db permissions shouldn't allow anything bad to happen. That's true, but bad things can happen in spite of correct permissions.

Here's a concrete scenario:

My app creates a db "DB" with two tables:

create database DB create table x (a bigint) create table y (a bigint)

But suppose there is another existing db "DB1" with a table called x with a column called a.

When a client uses my code to select DB1, I can tell that is not one of my databases by calling SHOW TABLES, but I can't unselect that db, because I don't know what to select in its place. If I just leave the connection as it is, my client could call a function in my API that altered the a column in the x table in the wrong db, or that even dropped the table x. They may have legitimate db permissions to do this, but it's certainly not what they thought they were doing!

If I could just tell mysql to select no db after I noticed that DB1 was not a valid db to be selecting, this could not happen.

I hope I'm making myself clear. Sorry this was so long.

Regards, Terry.