Re: Find out database
Posted in 1997
Rob J McAllister wrote:
>
> If you are using 7.x ESQL/C you can use GET DIAGNOSTICS to fetch
> several types of information including the current database name. RTFM
I must have missed it. I just went through the GET DIAGNOSTICS doc in
the Guide (Syntax) for release 7.2 and found no syntax to specify the
current database. Current database server yes, but the not database
itself. A remarkable omission IMO. (Or is that just not an ANSI
standard? >8-[ )
However, I DO have an idea for an ESQL function to accomplish this.
Note that this code is untested in ESQL. I have run each step in
dbaccess and it DOES work. (I got the PID from the ps command before
starting the queries.) However, when I try to compile it, I get
"Unknown error message -33071.
Note that this quickie makes no attempt to be efficient by prepared
statments and cursors. Nor does this attempt to account for the
possibility of multiple connections. And we don't have to be a multi-
threaded program to have multiple connections; just a transaction that
spans multiple databases.
But it's a start IMO.
------------------------------------------------------------------------
int curdbs(mydbs)
EXEC SQL BEGIN DECLARE SECTION;
char *mydb; # Return pointer for database name
EXEC SQL END DECLARE SECTION;
{
EXEC SQL BEGIN DECLARE SECTION;
int mypid, mysession, mydb_rowid;
EXEC SQL END DECLARE SECTION;
mypid = getpid(); /* Get my process ID */
EXEC SQL /* Get my current session ID from PID */
select sid
into :mysession
from sysmaster:syssessions s
where s.pid = :mypid;
/* The following assumes I do not have multiple connections, *
* though some refinements can be made to accomdate this
*/
EXEC SQL /* Get rowid for my database lock */
select rowidlk
into :mydb_rowid
from sysmaster:syslocks
where dbsname = sysmaster
and tabname = sysdatabases
and rowidlk != 513; /* NOT the lock from this query */
/* I have the rowid of the database "header" that I have the
* connection to
*/
EXEC SQL
select name
into :mydb
from sysmaster:sysdatabases
where rowid = :mydb_rowid;
return(1); /* Return TRUE if I got it */
}
------------------------------------------------------------------------
--
-- Jake (In pursuit of undomesticated aquatic avians)
+----------------------------------------------------------+
|Aside from that, how did you enjoy the play, Mrs. Lincoln?|
+----------------------------------------------------------+