Re: Find out database
Posted in 1997
This is a multi-part message in MIME format.
--------------63DD60FC434
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Jacob Salomon wrote:
>
> 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-[ )
I'm with you Jake this cannot be done this way!
>
> 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
Jake, I took your example and finished it. The compile error was a ksh
style comment on the declaration of the parameter to curdbs. Also in
transposing from SQL to ESQL you lost a filter 'AND owner =
:mysession'. I also made the whole thing into a single three table
join, it should be faster than three separate statements.
Art S. Kagel
--------------63DD60FC434
Content-Type: text/plain; charset=us-ascii; name="getdbs.ec"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="getdbs.ec"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
EXEC SQL INCLUDE sqlca;
int curdbs( char *dbs )
{
EXEC SQL BEGIN DECLARE SECTION;
int mypid;
char *mydb;
EXEC SQL END DECLARE SECTION;
mydb = dbs;
mypid = getpid(); /* Get my process ID */
/* The following assumes I do not have multiple connections, *
* though some refinements can be made to accommodate this.
* Just get the current connection name and find session id
* for that connection.
*
* I have the rowid of the database "header" that I have the
* connection to.
*/
EXEC SQL
select name
into :mydb
from sysmaster:sysdatabases d,
sysmaster:syslocks l,
sysmaster:syssessions s
where d.rowid = rowidlk
and dbsname = 'sysmaster'
and tabname = 'sysdatabases'
and l.owner = s.sid
and s.pid = :mypid
and name != 'sysmaster';
return sqlca.sqlcode == 0; /* Return TRUE if I got it */
}
int main( int argc, char **argv )
{
EXEC SQL BEGIN DECLARE SECTION;
char mydb[37], db[37];
EXEC SQL END DECLARE SECTION;
strcpy( db, argv[1] );
EXEC SQL DATABASE :db;
curdbs( mydb );
printf( "Database: %s.\\n", mydb );
}
--------------63DD60FC434--