Sample ESQL/C-Plus-Plus Application
The following code is a sample ESQL/C++ application that illustrates the requirements described in this section:
***************************** Main Routine ***************************
# include <stream.h>
// Simple ESQL/C++ program that uses the class Employee
// declared in employee.h.
// This program asks for a employee id, and then retrieves and prints
// that employee's information.
#include "employee.h"
main()
{
// Connect to the database
exec sql connect testdatabase;
char buf[20]; // Input buffer
// Prompt for Employee id
while (1)
{
Employee a; // Declare Employee object
cout << "\nPlease enter employee id (or 'e' to exit): " << flush;
cin >> buf;
if (buf[0] == 'e')
break;
a.select(buf); // Select employee info from database
a.print(); // Print employee info
}
exec sql disconnect;
}
************************ Member functions ******************************
# include <string.h>
# include <stream.h>
exec sql include 'employee.sh';
// Employee member routines
// Constructor - declare storage for all the character fields and
// Initialize to empty.
//
Employee::Employee()
{
name = new char[MAXDATA];
name[0] = '\0';
address = new char[MAXDATA];
address[0] = '\0';
title = new char[MAXDATA];
title[0] = '\0';
age = 0;
}
// Destructor
Employee::~Employee()
{
delete name;
delete address;
delete title;
}
// Assignment Operator
void Employee::operator=(const Employee& a)
{
int n;
n = strlen(a.name);
for ( int i = 0; i <n ; i++)
name[i] = a.name[i];
}
//Member functions
void Employee::print()
{
cout << "Employee Info \n";
cout << "------------- \n";
if (name[0] == '\0')
cout << "** Employee Not found **\n";
else
{
cout << "Name = " << name << '\n';
cout << "Address = " << address << '\n';
cout << "Title = " << title << '\n';
cout << "Age = " << age << '\n';
}
}
void Employee::select(char *empid)
{
// Use a local variable to store function argument so it can
// be declared to ESQL
exec sql begin declare section;
char *sqlempid;
exec sql end declare section;
sqlempid = empid;
exec sql select name, address, title, age into
:name, :address, :title, :age
from employee where empid = :sqlempid;
}
********************** Class header file employee.h ********************
// Declare an employee class for C++
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
};
const int MAXDATA = 60
Last modified date: 11/09/2022