Re: Get active database name at runtime
Posted in 2004
Andrea De Angeli wrote: > This is a multi-part message in MIME format. Don't post MIME or we'll set the clown onto you! > I am currently writing a library function in 4gl for an application > that must run both in IDS and SE. > > Now, the function must perform some actions on a database but can be > called by several modules that open and work on other databases. > Something like: > > ---begin module ---- > DATABASE somedb > DEFINE somerecord LIKE sometable.* > MAIN > --- do stuff --- > CALL library_function(params) > ---- do other stuff on somedb --- > END MAIN > ---end of module---- > > --- begin library module ---- > DATABASE library > DEFINE somerecord LIKE sometable.* > FUNCTION library_function(params) > DATABASE library # it won't work otherwise: it will be connected = > to somedb! > --- do stuff on library database > RETURN somevalue > END FUNCTION > --- end library module --- > > Problem comes when after the return the application is connected to > library db, not the original one. Runtime error. I think you need to investigate the CONNECT statement - you use two connections, one to the main database and a different one for the library database. Remember to use WITH CONCURRENT TRANSACTIONS. CONNECT TO 'somedb' AS 'maindb' USER 'me' USING passwd WITH CONCURRENT TRANSACTIONS CONNECT TO 'library' AS 'library' USER 'metoo' USING otherpasswd WITH CONCURRENT TRANSACTIONS Now, in your library code, you do SET CONNECTION 'library'; when you've finished, you do SET CONNECTION 'maindb'. The passwd and otherpasswd variables need to be VARCHAR. You can use host variables in place of the database names. At a pinch, you can use host variables for the connection names. You can use host variables for the user name; you must a host variable for the password. You clearly don't have any transaction context worth keeping at the moment; a DATABASE statement terminates any transactions (or fails to work because you have a TX open). This is the only way I can think of that works reliably with SE too. Indeed, you can even have SE running the library and IDS running the main database - or vice versa. If your version of I4GL does not support CONNECT, you need to upgrade to one that does - 7.3x does. -- Jonathan Leffler #include <disclaimer.h> Email: jleffler@earthlink.net, jleffler@us.ibm.com Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/