home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
komix
/
DATA.Z
/
DBObject.cxx
< prev
next >
Wrap
C/C++ Source or Header
|
1996-05-31
|
4KB
|
200 lines
/*---------------------------------------------------------------------------
*
* Copyright (c) 1995 by Cadre Technologies Inc.
*
* This software is furnished under a license and may be used only in
* accordance with the terms of such license and with the inclusion of
* the above copyright notice. This software or any other copies thereof
* may not be provided or otherwise made available to any other person.
* No title to and ownership of the software is hereby transferred.
*
* The information in this software is subject to change without notice
* and should not be construed as a commitment by Cadre Technologies Inc.
*
*---------------------------------------------------------------------------
*
* File : @(#)DBObject.cxx 1.6 17 Jan 1996
* Original date : 01-Dec-1995
* Description : Base Class for all Database Classes
*
*---------------------------------------------------------------------------
*/
static const char SccsId[]="@(#)DBObject.cxx 1.6 17 Jan 1996 Copyright 1995 Cadre Technologies Inc.";
#include <stdlib.h>
#include <string.h>
#ifndef DBOBJECT_HXX
#include "DBObject.hxx"
#endif
#ifndef SQLPRINT_HXX
#include "SqlPrint.hxx"
#endif
PDBPROCESS DBObject::dbproc;
PDBPROCESS DBObject::dbproc2;
DBObject::DBObject(const String &name) :
dbState(CREATED),
className(name)
{
}
DBObject::~DBObject()
{
}
int DBObject::connectDB(const char *name)
{
PLOGINREC login;
// Install error and message handlers
dberrhandle (err_handler);
dbmsghandle (msg_handler);
// Initialize DB-library
dbinit();
// Get a LOGINREC
login = dblogin();
// Get username
char username[80];
char *usernamePtr=getenv("DBUSERNAME");
if (usernamePtr==NULL) {
usernamePtr=getenv("USERNAME");
if (usernamePtr==NULL)
usernamePtr="default";
}
strcpy(username,usernamePtr);
// Get password
char passwd[80];
char *passwdPtr=getenv("DBPASSWD");
if (passwdPtr==NULL)
strcpy(passwd,"default");
else
strcpy(passwd,passwdPtr);
DBSETLUSER(login, username);
DBSETLPWD (login, passwd);
// Setup a connection to a database server
char servername[80];
char *servernamePtr=getenv("DBSERVER");
if (servernamePtr==NULL)
strcpy(servername,"");
else
strcpy(servername,servernamePtr);
dbproc=dbopen(login, servername);
dbproc2=dbopen(login, servername);
// Free storage allocated with dblogin
dbfreelogin(login);
// Use the requested database
if (dbproc!=0) {
dbcmd(dbproc,"USE ");
dbcmd(dbproc,name);
if (dbsqlexec(dbproc)!=SUCCEED)
return -1;
if (dbresults(dbproc)==FAIL)
return -1;
} else {
return -1;
}
if (dbproc2!=0) {
dbcmd(dbproc2,"USE ");
dbcmd(dbproc2,name);
if (dbsqlexec(dbproc2)!=SUCCEED)
return -1;
if (dbresults(dbproc2)==FAIL)
return -1;
} else {
return -1;
}
return 0;
}
int DBObject::beginWork()
{
dbcmd(dbproc,"BEGIN TRANSACTION");
int retval=dbsqlexec(dbproc)!=SUCCEED?-1:0;
if (retval==0) {
retval=dbresults(dbproc)==FAIL?-1:0;
}
return retval;
}
int DBObject::commit()
{
dbcmd(dbproc,"COMMIT TRANSACTION");
int retval=dbsqlexec(dbproc)!=SUCCEED?-1:0;
if (retval==0) {
retval=dbresults(dbproc)==FAIL?-1:0;
}
return retval;
}
int DBObject::saveTrans(const char *savepoint)
{
dbfcmd(dbproc,"SAVE TRANSACTION %s",savepoint);
int retval=dbsqlexec(dbproc)!=SUCCEED?-1:0;
if (retval==0) {
retval=dbresults(dbproc)==FAIL?-1:0;
}
return retval;
}
int DBObject::rollback()
{
dbcmd(dbproc,"ROLLBACK TRANSACTION");
int retval=dbsqlexec(dbproc)!=SUCCEED?-1:0;
if (retval==0) {
retval=dbresults(dbproc)==FAIL?-1:0;
}
return retval;
}
int DBObject::rollback(const char *savepoint)
{
dbfcmd(dbproc,"ROLLBACK TRANSACTION %s",savepoint);
int retval=dbsqlexec(dbproc)!=SUCCEED?-1:0;
if (retval==0) {
retval=dbresults(dbproc)==FAIL?-1:0;
}
return retval;
}
int DBObject::notFound()
{
return dbState == SQL_ERROR;
}
void DBObject::resetState()
{
dbState = CREATED;
}
int DBObject::processSqlStatus()
{
switch (dbState) {
case CREATED:
case NORMAL:
if (dberrorcode != 0) {
dbState = SQL_ERROR;
return -1;
} else {
dbState = NORMAL;
return 0;
}
case SQL_ERROR:
case TYPE_ERROR:
return -1;
}
return -1;
}