More ESQL questions
Posted in 1993
More Informix SE/ESQL questions for the experts --
I am building a database server on top of Informix SE. The server in
question is simple (I hope) and basically gets given a list of field names
that are of interest, and the text of a WHERE clause. I assemble a SELECT
statement, PREPARE, DECLARE, and build a SQLDA struct to hold the
results. Then, on client request, I do some number of FETCHes and send
the results back. Not a conceptually difficult task. Since I can
have a large number of clients at once, and since they can ask me to fetch
one record at a time if they want, I might have any number of cursors
running at once, on behalf of my clients.
The ESQL manual, though, is not notably clear on things like memory
management and resource allocation. Here are some questions.
1) I'm building my own SQLDA struct to receive results from a $FETCH.
I more or less need to do this, since I get handed the column names
on the fly. I set the .sqld to the number of columns, and the values
of each .sqlvar's .sqltype, .sqllen, and .sqldata for the values I
expect back. After each fetch, I would like .sqlname to point to the
name of the column. Do I set .sqlname when I build the struct? Or does
the $FETCH code allocate space and set the name of the column each time?
If the $FETCH code sets the name, do I free() it myself when I finish
up the query and am throwing out the SQLDA struct? These things
matter; I can't afford memory leaks.
2) As noted, I prepare and declare and open a SELECT string built on the fly.
After doing this, and possibly doing a few fetches based on the resulting
cursor, the client decides he wants to delete all rows that he Selected.
I already have a cursor all built for use with $FETCHs; that cursor must
contain the information needed to select out records. How do I construct
a $DELETE statement with the effect of killing all records selected within
an existing cursor? can I do something like
$DELETE FROM ... WHERE $cursorname; /* ?? */
or do I really have to recover the original WHERE clause text and use
that for the DELETE's WHERE? It seems that, having already set up the
WHERE clause for a SELECT, I shouldn't need to repeat myself to do the
DELETE.
I don't want to just do a loop of $FETCH, $DELETE ... WHERE CURRENT OF $curname;
because I'm afraid it would be horribly inefficient (and because the delete
command is expected to delete *all* rows, including any already fetched.)
3) I want to insert a row (append, actually). I build an insert statement
and do $EXECUTE IMMEDIATE. It is a simple case; I build an
Insert into tablename colname1, colname2,... values (v1, v2...)which is about as easy as it gets. As it happens, the table I am
inserting into has a SERIAL-type column. I don't allow the caller to
specify a value for it; I want it to default to a unique number, for
predictable reasons.
I also want to know what number the Insert assigned, so I can pass it back
as the unique key to the new record. Do I have to construct a cursor
and do a $FETCH LAST to get it? Or is there an alternate way to build
and execute an Insert that will let me see what got inserted? (For that
matter, am I guaranteed that an Inserted record, with no cursor involved,
will go to the bottom of the table, where FETCH LAST will find it?)
These may be foolish questions whose answers are immediately apparent
to database cognizanti, but this is my first effort in this area.
Please help!! Post here, or mail to smayo@world.std.com if you'd rather
not embarrass me publically. Thanks!