Re: ESQL and C++?
Posted in 1994
Topics: Error Codes & Troubleshooting, Connectivity: ESQL/C, 4GL & Embedded SQL, Server Administration
Yes it is possible, if you proceed with caution. We found that you have to do a couple of things, such as provide prototypes for all the Informix functions that are generated by the esqlc Preprocessor. We created a standard header called Informix.h that looks like the following: //////////////////////////////////////////////////////////////////////////////// // // External ESQL functions needed for C++ // // // //////////////////////////////////////////////////////////////////////////////// #ifndef _INFORMIX_H_ #define _INFORMIX_H_ extern "C" { _iqdbase(char *,int); void _iqdbclose(void); struct _SQCURSOR * _iqlocate_cursor(char *,int,int,int); void _iqcdcl(struct _SQCURSOR *,char *,char **,struct sqlda *, struct sqlda *,int); void _iqdcopen(struct _SQCURSOR *,struct sqlda *,char *,char *,int); void _iqcftch(struct _SQCURSOR *,struct sqlda *,struct sqlda *,char *, struct _FetchSpec *); void _iqclose(struct _SQCURSOR *); int rgetmsg(long,char *,unsigned int); int dttoasc(dtime_t *,char *); int dtcvasc(char *, dtime_t *); int dtcurrent(dtime *); int risnull(int, char *); void _iqslct(struct _SQCURSOR *,char **,int, struct sqlvar_struct *,int,struct sqlvar_struct *,int); void _iqstmnt( _SQSTMT *,char **,int,struct sqlvar_struct *, char *); }; #endif It would be nice if Informix would provide a complete C++ type header for us, since we developed this file on a trial and error basis and it does not include every possible esql function. You can then define a class such as the one below: ====================== DBClass.h: ========================= //////////////////////////////////////////////////////////////////////////////// // // File: DBClass.h // //////////////////////////////////////////////////////////////////////////////// #ifndef _DBCLASS_H_ #define _DBCLASS_H_ #include <stdio.h> #include <iostream.h> #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE !FALSE #endif ////////////////////////////////////////////////////////////////////////////// // // CLASS: DBClass // ////////////////////////////////////////////////////////////////////////////// class DBClass { public: DBClass(); ~DBClass(); int DBConnect(char *); void DBDisconnect(); private: int connected; }; #endif ====================== DBClass.C: ========================= // includes for ESQL C EXEC SQL include datetime.h; EXEC SQL include sqlca; EXEC SQL include sqlstype; #include <Informix.h> // Header file for Informix prototypes #include <DBClass.h> DBClass::DBClass() { connected = FALSE; } DBClass::~DBClass() { // if we have a connection to a database, then disconnect if (connected == TRUE) EXEC SQL CLOSE DATABASE; } int DBClass::DBConnect(char *dbPath) { EXEC SQL BEGIN DECLARE SECTION; char path[255]; EXEC SQL END DECLARE SECTION; // if we are connected to a database, disconnect from it first if (connected == TRUE) DBDisconnect(); strcpy(path,dbPath); EXEC SQL DATABASE:path; if (sqlca.sqlcode == 0) connected = TRUE; return connected; }