home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
bufop.zip
/
TSTA.SQC
< prev
next >
Wrap
Text File
|
1993-09-07
|
7KB
|
304 lines
/*****************************************************************/
/* 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;
// 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;
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;
EXEC SQL COMMIT WORK;
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);
EXEC SQL COMMIT WORK;
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;
EXEC SQL OPEN C1;
do {
EXEC SQL FETCH C1 INTO
:s.name,
:s.addr,
:s.age,
:s.recID;
if (_sqlfound) {
aSet.add(s);
} /* endif */
} while (_sqlfound || _sqlwarn || _sqlerror ); /* enddo */
EXEC SQL CLOSE C1;
EXEC SQL COMMIT WORK;
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;
}
//=========================================================
// 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;
}
void stopit()
{
EXEC SQL connect reset;
}