[comp.databases] Embedded SQL in C++

bapat@rm1.UUCP (Bapat) (07/31/90)

A question for all you C++ programmers out there trying to build 
applications that use an embedded RDBMS:

What's the best way to declare C++ constructs to the Embedded SQL Precompiler
interface? Since C++ is a superset of C, and since the Precompiler
translates Embedded SQL to straight C, it should also work on C++, nyet?

The problem I'm having is when I declare host-language variables:

void Myclass::myfunc ( int myarg1, int myarg2)
{
      EXEC SQL BEGIN DECLARE SECTION;
      char sql_buffer[1024];
      ...
      EXEC SQL END DECLARE SECTION;

      [ Later... ]

      EXEC SQL PREPARE S1 FROM :sql_buffer;
      EXEC SQL DECLARE C1 CURSOR FOR S1;
      EXEC SQL OPEN C1;
      EXEC SQL FETCH C1 INTO
           :this->string1, :this->string2;
            ^^^^            ^^^^
      [ etc ]
}

The problem is that SQL Precompiler, which thinks it's precompiling pure C,
doesn't know about the implicit "this" pointer since it is not declared
following the EXEC SQL BEGIN DECLARE SECTION. On the other hand, the real host
language (C++) assumes an implicit meaning for "this", which therefore is
never declared as a variable anywhere.

The workaround we're thinking of is to copy the FETCH'ed data into regular temp
variables (which we can declare in the EXEC SQL DECLARE SECTION), and then
subsequently copy them into the appropriate members of "this" outside the
scope of an EXEC SQL.

Any thoughts, comments, etc are welcome.
-- 
Subodh Bapat              bapat@rm1.uu.net     OR           ...uunet!rm1!bapat
MS E-204, PO Box 407044,  Racal-Milgo, Ft Lauderdale, FL 33340  (305) 846-6068

"Sex is merely sublimated relief for the primitive urge to hack code."

nico@Unify.Com (Nico Nierenberg) (08/02/90)

In article <897@rm1.UUCP> bapat@rm1.UUCP (Bapat) writes:
>
>
>A question for all you C++ programmers out there trying to build 
>applications that use an embedded RDBMS:
>
>What's the best way to declare C++ constructs to the Embedded SQL Precompiler
>interface? Since C++ is a superset of C, and since the Precompiler
>translates Embedded SQL to straight C, it should also work on C++, nyet?
>
>The problem I'm having is when I declare host-language variables:
>
>void Myclass::myfunc ( int myarg1, int myarg2)
>{
>      EXEC SQL BEGIN DECLARE SECTION;
>      char sql_buffer[1024];
>      ...
>      EXEC SQL END DECLARE SECTION;
>
>      [ Later... ]
>
>      EXEC SQL PREPARE S1 FROM :sql_buffer;
>      EXEC SQL DECLARE C1 CURSOR FOR S1;
>      EXEC SQL OPEN C1;
>      EXEC SQL FETCH C1 INTO
>           :this->string1, :this->string2;
>            ^^^^            ^^^^
>      [ etc ]
>}
>
>The problem is that SQL Precompiler, which thinks it's precompiling pure C,
>doesn't know about the implicit "this" pointer since it is not declared
>following the EXEC SQL BEGIN DECLARE SECTION. On the other hand, the real host
>language (C++) assumes an implicit meaning for "this", which therefore is
>never declared as a variable anywhere.
>

I believe that standard embedded SQL can't deal with things like structures
in any event.  The solution is what you proposed unless the particluar SQL
has extensions to recognize pointers and structures.
>The workaround we're thinking of is to copy the FETCH'ed data into regular temp
>variables (which we can declare in the EXEC SQL DECLARE SECTION), and then
>subsequently copy them into the appropriate members of "this" outside the
>scope of an EXEC SQL.
>
>Any thoughts, comments, etc are welcome.
>-- 
>Subodh Bapat              bapat@rm1.uu.net     OR           ...uunet!rm1!bapat
>MS E-204, PO Box 407044,  Racal-Milgo, Ft Lauderdale, FL 33340  (305) 846-6068
>
>"Sex is merely sublimated relief for the primitive urge to hack code."


-- 
---------------------------------------------------------------------
Nicolas Nierenberg  			"No matter where you go,
Unify Corp.				 there you are."
nico@unify

dsa@dlogics.COM (David Angulo) (08/08/90)

In article <897@rm1.UUCP> bapat@rm1.UUCP (Bapat) writes:
>
>What's the best way to declare C++ constructs to the Embedded SQL Precompiler
>interface? 
>
> ... copy the FETCH'ed data into regular temp
>variables (which we can declare in the EXEC SQL DECLARE SECTION), and then
>subsequently copy them [back into c++ variables] 

Yes, that is really the only way.  Then send your source through the SQL
precompiler and send the output of that into your c++ precompiler.  We have
been doing this sort of thing for about a year.  This is really the easy part.
The harder part comes in trying to isolate your eSQL statements for a particular
class to the methods of that class.  At first glance, it would seem easy.
Each class, after all, needs an INSERT, UPDATE, FIND-BY-ID, FETCH-NEXT,
DELETE, etc. methods.  And, this works for simple vanilla type of queries.
However, when you get into larger, more complex queries, you'll find you
need a FIND-BY-this-type-of-query method for each different type of query
and a cursor to go along with this, then you'll need to have a FETCH-BY-
this-type-of-query, and variables to let you know if each cursor is opened
so that you can close them in the destructor, and it just keeps going on.
It is doable, however.  Good luck!
-- 
David S. Angulo                  (312) 266-3134
Datalogics                       Internet: dsa@dlogics.com
441 W. Huron                     UUCP: ..!uunet!dlogics!dsa
Chicago, Il. 60610               FAX: (312) 266-4473