Re: DB Definition Reference
Posted in 1996
Mark R. Ainis wrote: > > ----------- Posted news begins ---------------- > Dear netters, > > We have an application that consists of Informix 4gl programs on our HP machine. > In all of the source codes, we have record declarations that reference > current database table definitions using the LIKE key word. A sample of > the source codes is attached below. The problem arises during compiling. > The compiler must know the database name explicitly specified in the fi > rst line of the codes in order to define the record definitions. Is there a > known way of eliminating the hard-coded database name in the source such as > passing the name to compiler during compilation? > > Thanks in advance. > > p.s. We called Informix's Tech. Support; however, the only answer we got from them is: > To get rid of the problem, DON'T use it! We are quite disappointed with > Informix's Tech Support. It looks like they are no better than us. > > Sample codes begin: > > database dbsrss01 > ############################################################ > # global variable declaration area > ############################################################ > globals > define g_request_id integer, > g_template_id integer, > g_max_group_no smallint, > g_sql_select_text char(512), > g_location_code char(08), > g_range_view_name like vwscttp00.range_view_name, > g_ret_tabname like vwscttp00.range_view_name, > g_eop_view_name like vwscttp00.eop_view_name, > g_top_view_name like vwscttp00.top_view_name, > > ----------- Posted news ends ---------------- Let's think about what's happening here. 4GL gets compiled to ESQL/C. (Even if, in your case, you're compiling to p-code the source is supposed to be compatible between p-code and fully compiled. Consequently any valid 4GL program must be capable of being compiled via the C route.) All your declarations are turned into the equivalent declarations in C. There is, however, no "like" in C. In order to declare "g_top_view_name" in C you must know what the data is. If, for instance it's a CHAR 20 then you will need to declare a char array of length 21 in C. The only way the compiler can "know" the length of your column is to look it up in the database. That's what the "like" keyword does. In order to do it must have a database name. You can, in fact change the current database with a DATABASE name so you are not limited to the database against which you compiled .....BUT..... What happens if you compile against a database where vwscttp00.top_view_name is CHAR 20, execute a DATABASE statement where it's a CHAR 40 and then SELECT vwscttp00.top_view_name INTO g_top_view_name? It's safer to include the maximum allowable size of the variable in the program spec. and to make a fixed declaration against that. If the databases presented to the program fall within spec all should be OK. Informix could, I suppose, have written the C version to use malloc() to allocate a block of memory appropriate to the column size in the current database. It would still be possible, however, to malloc() the memory against one database and then change the current database before the SELECT; the code would still break. In order to make it bullet proof they would have had to check the current column size immediately before execution of each SELECT and allocate memory on the fly. This would have hit performance quite severely. I think the way 4GL is implemented is reasonable and, given the implementation, the advice you were given is sound. Ian Goddard