Was this helpful?
How to Create ESQL/C-Plus-Plus Programs
The ESQL/C++ precompiler supports the same features as the ESQL/C precompiler, plus the additional features described in this section.
Program Comments
You can use either C-style comment delimiters (/* */) or C++ comment delimiters (//) in ESQL/C++ programs.
Example: Comment delimiters usage
/* Declare data */
exec sql begin declare section;
    int idno;
        // identification number
    exec sql end declare section;
How Data is Declared
ESQL/C++ supports C data types, including pointers and structures. To declare data in ESQL/C++ applications, use the data types and techniques described in C Variables and Data Types.
You cannot declare an entire class to ESQL/C++; however, you can declare the class members. For example:
Wrong:
exec sql begin declare section;
  class Employee {
  char *     name;     // Name
  char *  address;     // Address
  char *  title;      // Title 
  int     age;
  public:
       Employee();      // Constructor
       ~Employee();     // Destructor
       void operator=(const Employee&);     // Assignment
       void print();    // Print
       void select(char *);    // Select
};
exec sql end declare section;
Right:
class Employee {
exec sql begin declare section;
  char *     name;     // Name
  char *  address;     // Address
  char *  title;      // Title 
  int     age;
 exec sql end declare section;
 public:
     Employee();       // Constructor
     ~Employee();      // Destructor
     void operator=(const Employee&);     // Assignment
     void print();     // Print
     void select(char *);     // Select
};
How to Transfer Data Between Programs and the Database
To transfer data between your application and the database, you can use either of the following techniques:
Declare class members to ESQL, and use them in ESQL DML statements (select, update, insert, and delete) in class member functions.
Copy data between class members and local variables. Use the local variables in ESQL statements to transfer data between your application and the database.
Names of variables that are declared to the ESQL/C++ precompiler must be unique in the scope of the source file. If you declare class members, avoid using the same name for members in different classes.
How to Declare Function Parameters
To declare function parameters to ESQL/C++, use local variables. In the following example, the local variables ptrsqlvar1 and locsqlvar2 are declared to the ESQL precompiler. The function parameters sqlvar1 and sqlvar2 are copied to ptrsqlvar1 and locsqlvar2 when their values are required for use in ESQL statements.
int myfunc (int sqlvar1, int sqlvar2)
 {
  exec sql begin declare section
    int *ptrsqlvar1;   /* Use local pointer */
    int locsqlvar2;   /*Use local variable */
  exec sql end declare section
  ptrsqlvar1 = &sqlvar1;
  locsqlvar2 = sqlvar2;
 // Use local variables in SQL statement:
  exec sql insert into mytable
    values (ptrsqlvar1, locsqlvar2);
...
 }
DCLGEN and ESQL/C-Plus-Plus
DCLGEN does not generate classes for C++. DCLGEN generates structures as it does for C. However, you can specify C++ as the language parameter (by specifying cc).
For example:
dclgen cc mydatabase mytable myfile.dcl mystructure
Ingres Runtime Library Prototypes
In each ESQL/C++ file you precompile, the precompiler automatically includes header files containing function prototypes for the Ingres runtime library routines.
4GL Restriction
You cannot call an ESQL/C++ routine from 4GL, Vision, or OpenROAD.
How to Create User-Defined Handlers
To declare user‑defined handlers in ESQL/C++ programs, you must declare them to the C++ compiler as C functions. For example:
// Function prototype for event handler
extern "C" int event_func(void);
To direct the DBMS to call the handler routine when a database event is raised, your application must issue the following SQL statement:
exec sql set_sql(dbeventhandler=event_func);
You cannot overload a function that you intend to use as a handler routine.
User‑defined handlers (data handlers) for long varchar and long byte I/O require an argument to be passed to the data handler. The argument must be defined as a generic pointer (void *) in the function prototype, and must be cast to the correct data type in the data handler routine.
The following example illustrates the construction of a data handler in C++:
// Handler prototype
// ESQL/C++ requires extern "C"
extern "C" int Put_Handler(void *hdlr_arg);
 typedef struct hdlr_param_
{
       char * arg_str;
       int arg_int;
 } HDLR_PARAM;
 void
main()
{
       HDLR_PARAM hdlr_arg;
       // Connect to the database
       exec sql connect testdatabase;
       exec sql insert into book(idno, long_text) values (1,
          datahandler(Put_Handler(&hdlr_arg)));
       exec sql disconnect;
 }
 
// Argument is declared as a generic pointer
int Put_Handler(void *hdlr_arg)
 {
       exec sql begin declare section;
          char seg_buf[50];
          int seg_length;
          int data_end;
       exec sql end declare section;
 // Cast argument for ESQL/C++
 ((HDLR_PARAM *)hdlr_arg)->arg_int = 0;
 rloop:
   read data from a file
    fill seg_buf and set seg_length
   at end of loop sent data_end to 1
   exec sql put data (segment = :seg_buf,
             segmentlength = :seg_length,
             dataend = :data_end);
 end rloop
       return 0;
 }
Last modified date: 04/03/2024