home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
bufop.zip
/
TSTA.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-11
|
15KB
|
519 lines
//
// processed by precompiler 09-11-93 09:32:24
//
/*****************************************************************/
/* N O T I C E */
/* PRECOMPILE THIS PROGRAM ONLY */
/* IT WILL INCLUDE THE NEEDED HEADER FILE */
/*****************************************************************/
//
// Implemenation of the testCls & some
// misc routines
//
#include <iostream.h>
//
// Notice - I include the file here (for the C compiler)
// and the SQL Include for BUFOP
// BUFOP will generate the tsta.hpp file from tsta.sqh
//
#include "tsta.hpp"
//
// this will include the host variables found in file tsta.sqh
// and will product the file tsta.hpp
//
//### EXEC SQL include 'tsta.sqh';
//
// include the sqlca which will gen code for prog ID
// and the boolean variables _found, _error, _warn
// which can be used instead of the gotos
//
//### EXEC SQL include SQLCA;
#include <sqlda.h> // sqlda structure
#include <sqlca.h> // sqlca structre
#include <sqlaprep.h> // api(s) for most calls
#include <sqlenv.h> // api(s) for start & stop db
sqlca sqlca;
typedef int Boolean;
#define false 0
#define true 1
Boolean _sqlfound = false;
Boolean _sqlerror = false;
Boolean _sqlwarn = false;
int _RC;
const unsigned char sqla_program_id[40] = {
0x6f,0x41,0x41,0x42,0x41,0x45,0x43,0x43,0x45,0x44,
0x20,0x20,0x20,0x20,0x20,0x20,0x74,0x73,0x74,0x61,
0x2e,0x73,0x71,0x63,0x66,0x41,0x58,0x67,0x4a,0x4c,
0x4a,0x4a,0x30,0x20,0x20,0x20,0x20,0x20,0x20,0x20};
// when not found set the _found to false
// else set it to true
//
//### EXEC SQL whenever not found goto _sqlfound;
//
// errors will set the sqlerror boolean
// Could have used a label but hate thoses things
// and really hard to implement in a class
//
//### EXEC SQL whenever sqlerror goto _sqlerror;
//
// sql warning will set the boolean _sqlwarn
//
// EXEC SQL whenever sqlwarning goto _sqlwarn;
// If we get an error - report it through this thing
// dump the error message and return wow! heavy duty stuff
//
void SQLError(char type)
{ char * buffer;
buffer = new char(255);
sqlaintp ( buffer, /* buffer for message text */
254, /* buffer size gota leave a byte free */
60, /* line width */
&sqlca ); /* SQLCA will be included in SQL INCLUDE*/
if (type == 'W') {
cout << "SQL WARNING " << buffer;
} else {
cout << "SQL ERROR " << buffer;
} /* endif */
delete buffer;
}
testCls::testCls() {pn = name;}
testCls::~testCls() {}
testCls::create( char * aname, char * aAddr, int aage, int aid) // init and create
{
memset(name,'\0',sizeof(name));
memset(addr,'\0',sizeof(addr));
strcpy(name,aname);
strcpy(addr, aAddr);
age = aage;
recID = aid;
pn = name;
insertDBRec();
}
testCls & testCls::operator=(testCls const & a){
if (this == &a) {
return *this;
} else {
strcpy(name,a.name);
strcpy(addr,a.addr);
age = a.age;
pn = name;
recID = a.recID;
} /* endif */
return *this;
}
testCls::changeName(char * aname)
{strcpy(name,aname);}
testCls::changeID(int id) {recID = id;}
testCls::changeAge(int aage) {age = aage;}
//
// read a Database record based on ID
// notice 'rec' was not declared in the declare section
// so it could not be used directly
// I could have declared a variable 'rec' in the header file
// and used it directly as all variables are global to
// BUFOP (and to sqlprep)
//
testCls::read(int rec)
{ recID = rec;
//### EXEC SQL SELECT name,Addr,Age
//### INTO :name,:addr :nullInd,:age
//### FROM test
//### WHERE id=:recID;
sqlastrt(&sqla_program_id,NULL,&sqlca);
_RC = sqlaaloc(1, 1,1,NULL);// allocate an sqlda
sqlasetv(1,0 ,496,4,&recID,NULL,NULL); // set host variable with no null indicator
_RC = sqlaaloc(2, 3,1,NULL);// allocate an sqlda
sqlasetv(2,0 ,460,40,&name,NULL,NULL); // set host variable with no null indicator
sqlasetv(2,1 ,460,60,&addr,&nullInd,NULL); // set host variable with a null indicator
sqlasetv(2,2 ,496,4,&age,NULL,NULL); // set host variable with no null indicator
sqlacall(24, 1, 1, 2,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
if (_sqlerror) {
SQLError('E');
} else {
if (_sqlwarn) {
SQLError('W');
} /* endif */
} /* endif */
}
//
// update the record defined in this object
//
testCls::update()
{
//### EXEC SQL UPDATE test
//### SET
//### name = :name,
//### addr = :addr:nullInd,
//### age = :age
//### WHERE id = :recID;
sqlastrt(&sqla_program_id,NULL,&sqlca);
_RC = sqlaaloc(3, 4,2,NULL);// allocate an sqlda
sqlasetv(3,0 ,460,40,&name,NULL,NULL); // set host variable with no null indicator
sqlasetv(3,1 ,460,60,&addr,&nullInd,NULL); // set host variable with a null indicator
sqlasetv(3,2 ,496,4,&age,NULL,NULL); // set host variable with no null indicator
sqlasetv(3,3 ,496,4,&recID,NULL,NULL); // set host variable with no null indicator
sqlacall(24, 2, 3, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
//### EXEC SQL COMMIT WORK;
sqlastrt(&sqla_program_id,NULL,&sqlca);
sqlacall(21, 0, 0, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
if (_sqlerror) {
SQLError('E');
} else {
if (_sqlwarn) {
SQLError('W');
} /* endif */
} /* endif */
}
//
// create a record on the DB using the data in
// this object
//
testCls::insertDBRec()
{
//### EXEC SQL INSERT INTO test (name, addr, age,id)
//### VALUES (:name,:addr,:age,:recID);
sqlastrt(&sqla_program_id,NULL,&sqlca);
_RC = sqlaaloc(4, 4,3,NULL);// allocate an sqlda
sqlasetv(4,0 ,460,40,&name,NULL,NULL); // set host variable with no null indicator
sqlasetv(4,1 ,460,60,&addr,NULL,NULL); // set host variable with no null indicator
sqlasetv(4,2 ,496,4,&age,NULL,NULL); // set host variable with no null indicator
sqlasetv(4,3 ,496,4,&recID,NULL,NULL); // set host variable with no null indicator
sqlacall(24, 3, 4, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
//### EXEC SQL COMMIT WORK;
sqlastrt(&sqla_program_id,NULL,&sqlca);
sqlacall(21, 0, 0, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
if (_sqlerror) {
SQLError('E');
} else {
if (_sqlwarn) {
SQLError('W');
} /* endif */
} /* endif */
}
//
// over ride the << for printing the record
//
ostream &operator <<(ostream & aStream, testCls const & theRec){
//
// print the record
//
aStream << "Name: " << theRec.name << "\n";
aStream << "Address: " << theRec.addr << "\n";
aStream << "Age: " << theRec.age << "\n";
return aStream;
};
//-----------------------------------------------------------
// get a collection from the database using dyamic SQL
// try to be clever and use the << to fill the container
// NOTE - If you do not have the IBM CSet++ you should remove this
// class - or do something different with the bag collection
//---------------------------------------------------------------
//
// the basic part of the select statement is used and
// the conditional part is appended
//
const char basicSQL[] = "SELECT NAME, ADDR,AGE,ID FROM TEST WHERE ";
selectTest:: selectTest()
{
}
selectTest:: selectTest(char * const & theCondition)
{
strcpy(theSelectStr,basicSQL);
strcat(theSelectStr,theCondition);
}
selectTest:: setSelect(char * const & theCondition){
strcpy(theSelectStr,basicSQL);
strcat(theSelectStr,theCondition);
}
//
// overload the << to fill the container
//
nameSet & operator<<(nameSet & aSet, selectTest & s)
{
//### EXEC SQL DECLARE C1 CURSOR FOR DSTR;
//### EXEC SQL PREPARE DSTR FROM :s.theSelectStr;
sqlastrt(&sqla_program_id,NULL,&sqlca);
_RC = sqlasets(strlen( s.theSelectStr),s.theSelectStr,NULL);
sqlacall(27, 4, 0, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
//### EXEC SQL OPEN C1;
sqlastrt(&sqla_program_id,NULL,&sqlca);
sqlacall(26, 4, 0, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
do {
//### EXEC SQL FETCH C1 INTO
//### :s.name,
//### :s.addr,
//### :s.age,
//### :s.recID;
sqlastrt(&sqla_program_id,NULL,&sqlca);
_RC = sqlaaloc(5, 4,4,NULL);// allocate an sqlda
sqlasetv(5,0 ,460,40,&s.name,NULL,NULL); // set host variable with no null indicator
sqlasetv(5,1 ,460,60,&s.addr,NULL,NULL); // set host variable with no null indicator
sqlasetv(5,2 ,496,4,&s.age,NULL,NULL); // set host variable with no null indicator
sqlasetv(5,3 ,496,4,&s.recID,NULL,NULL); // set host variable with no null indicator
sqlacall(25, 4, 0, 5,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
if (_sqlfound) {
aSet.add(s);
} /* endif */
} while (_sqlfound || _sqlwarn || _sqlerror ); /* enddo */
//### EXEC SQL CLOSE C1;
sqlastrt(&sqla_program_id,NULL,&sqlca);
sqlacall(20, 4, 0, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
//### EXEC SQL COMMIT WORK;
sqlastrt(&sqla_program_id,NULL,&sqlca);
sqlacall(21, 0, 0, 0,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
return aSet;
}
//==================================================
// read/write into/out of structures
//====================================================
void readDB(stest * a) // read indirect using pointer
{
//### EXEC SQL SELECT name,Addr,Age
//### INTO :a->sname,:a->saddr,:a->sage
//### FROM test
//### WHERE id=101;
sqlastrt(&sqla_program_id,NULL,&sqlca);
_RC = sqlaaloc(6, 3,5,NULL);// allocate an sqlda
sqlasetv(6,0 ,460,40,&a->sname,NULL,NULL); // set host variable with no null indicator
sqlasetv(6,1 ,460,80,&a->saddr,NULL,NULL); // set host variable with no null indicator
sqlasetv(6,2 ,496,4,&a->sage,NULL,NULL); // set host variable with no null indicator
sqlacall(24, 5, 0, 6,NULL);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
sqlastop(NULL);
}
//=========================================================
// support functions put here (rather than the main program)
// because this module goes through the precompiler
//========================================================
void startit()
{
//### EXEC SQL connect to test in share mode;
unsigned char dbname[]= "test";
_RC = sqlestrd(dbname, 'S',&sqlca);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
}
void stopit()
{
//### EXEC SQL connect reset;
_RC = sqlestpd(&sqlca);
if (sqlca.sqlcode < 0) {
_sqlerror = true;
} else {
_sqlerror = false;
} /* endif */
if (sqlca.sqlcode == 100) {
_sqlfound = false;
} else {
_sqlfound = true;
} /* endif */
}