Re: Definition techniques using ESQL-C
Posted in 1992
Topics: Error Codes & Troubleshooting, Connectivity: ESQL/C, 4GL & Embedded SQL
In article <1992Oct29.154519.22826@informix.com> davek@informix.com (David Kosenko) writes:
>Rory Martin writes:
> [ in response to the question:]
>>> I am a bit confused about how to do something that seems like it should
>>> be easy.
>>Jon,
>>If anyone comes up with a reasonable way of doing this, let me know.
>>When I was confronted with the problem, I only found two ways of handling it.
>>The first was the obvious, having two separate include files, one for the
>>ESQL program and one for the standard C program.
>>The second was to duplicate the structure inside the same file using
>>the '$struct foo' in one and the plain 'struct foo' in the other.
>>Then using #ifdef ESQL for the ESQL structure.
>>Both of these require you to update both structures; at least the second
>>one puts them both in the same file.
>>If anyone has a cleaner way of doing this I would certainly appreciate
>>hearing about it.
>>
>You could make everything a .ec file (even if it contains no SQL) and
>compile it with esql. Then all your structs can be $structs.
Shouldn't there be a smiley face on that last comment?
We ran into the same problem with ESQL.
Basically you don't want to have to update two header files with two
redundant structures every time you modify your program. We have all our
ESQL source in one directory from which we make a library. We have all
our common include files with structure definitions and so forth
in another directory. We use one *_struct.h file to define the
structure used. The contents of the _struct.h file is one typedef
of one structure. It cannot contain anything tricky or ESQL will
choke (ESQL does parse /* */ comments at least).
src_db directory
foo.ec:
$include "foo_inf.h";
int
db_read_a_foo(key_arg, pfoo)
$ char *key_arg;
$ FOO *pfoo;
{
$ select * from foo
into $pfoo
where key = $key_arg;
return sqlca.sqlcode
}
foo_inf.h:
EXEC SQL BEGIN DECLARE SECTION
$include "../src_include/foo_struct.h";
EXEC SQL END DECLARE SECTION
---------------------------------------------------------
src_include directory
foo.h:
#include "foo_struct.h"
#define FOO_DEFINE "esql chokes on #defines, enums, etc"
foo_struct.h:
typedef struct foo {
int x, y
} FOO;
-----------------------------------------------------------
src_app directory
foo.c:
#include "foo.h"
FOO foo_instance;
printf("%s\\n", FOO_DEFINE);
db_read_a_foo("esql-sucks-rocks", &foo_instance);
-------------------------------------------------------------
This is about as elegant as we could come up with.
--
Eric Pederson (W) 510-210-8890
ericp@netcom.com