home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------
- *
- * Copyright (c) 1995 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.cp 1.1
- * Original date : 10-3-1993 / 30-11-1995
- * Description : Sybase Base Class for all Database Classes
- * uses RogueWave class library
- *
- *---------------------------------------------------------------------------
- */
- static const char SccsId[]="@(#)DBObject.cp 1.1\t06 Feb 1996 Copyright 1995 Cadre Technologies Inc.";
-
- EXEC SQL INCLUDE SQLCA;
-
- #ifdef WIN32
- # include "fstream.h"
- #else
- # include "stream.h"
- #endif
- #include <string.h>
- #include <stdlib.h>
-
- #ifndef DBOBJECT_HXX
- #include "DBObject.hxx"
- #endif
-
- #ifndef SQLPRINT_HXX
- #include "SqlPrint.hxx"
- #endif
-
- static unsigned hashInt(const int& i)
- {
- return i;
- }
-
- static unsigned hashName(const RWCString& s)
- {
- return s.hash();
- }
-
-
- EXEC SQL WHENEVER SQLERROR CALL sqlprint();
- EXEC SQL WHENEVER SQLWARNING CALL sqlprint();
- EXEC SQL WHENEVER NOT FOUND CONTINUE;
-
- DBObject::DBObject(const RWCString &name) :
- dbState(CREATED),
- className(name)
- {
- }
-
- DBObject::~DBObject()
- {
- }
-
- int DBObject::connectDB(const char *name)
- {
- // cast away const
- char *_name = (char *)name;
-
- EXEC SQL BEGIN DECLARE SECTION;
- char *dbName = _name;
- char user[80];
- char passwd[80];
- char server[80];
- EXEC SQL END DECLARE SECTION;
-
- EXEC SQL WHENEVER SQLERROR CALL sqlprint();
-
- char *p;
-
- #ifdef WIN32
- if (p = ::getenv("DBUSERNAME"))
- strcpy(user, p);
- else if (p = ::getenv("USERNAME"))
- strcpy(user, p);
- #else /* !WIN32 */
- if (p = ::getlogin())
- strcpy(user, p);
- #endif /* !WIN32 */
- else
- strcpy(user, "unknown");
-
- if (p = ::getenv("DBPASSWD"))
- strcpy(passwd, p);
- else {
- char tmp[80];
- strcpy(tmp, user);
- strcpy(passwd, strcat(tmp, "_password"));
- }
-
- if (p = ::getenv("DSQUERY"))
- strcpy(server, p);
- else
- strcpy(server, "SYBASE");
-
- EXEC SQL CONNECT :user IDENTIFIED BY :passwd USING :server;
- EXEC SQL USE :dbName;
- EXEC SQL EXECUTE IMMEDIATE "SET CHAINED OFF";
-
- return sqlca.sqlcode != 0 ? -1 : 0;
- }
-
- #if 0
- //
- // included as an example
- //
-
- int DBObject::disConnectDB()
- {
- E*EC SQL DISCONNECT DEFAULT;
- return sqlca.sqlcode != 0 ? -1 : 0;
- }
- #endif /* 0 */
-
- int DBObject::beginWork()
- {
- // does not exist in in Sybase 10.0
- //
- // E*EC SQL BEGIN WORK;
- // return sqlca.sqlcode != 0 ? -1 : 0;
- return 0;
- }
-
- int DBObject::commit()
- {
- EXEC SQL COMMIT WORK;
- return sqlca.sqlcode != 0 ? -1 : 0;
- }
-
- int DBObject::rollback()
- {
- EXEC SQL ROLLBACK WORK;
- return sqlca.sqlcode != 0 ? -1 : 0;
- }
-
- int DBObject::notFound()
- {
- return dbState == SQL_ERROR && sqlca.sqlcode == 100;
- }
-
- void DBObject::resetState()
- {
- dbState = CREATED;
- }
-
- int DBObject::processSqlStatus()
- {
- switch (dbState) {
- case CREATED:
- case NORMAL:
- if (sqlca.sqlcode != 0) {
- dbState = SQL_ERROR;
- return -1;
- } else {
- dbState = NORMAL;
- return 0;
- }
- case SQL_ERROR:
- case TYPE_ERROR:
- return -1;
- }
- return -1;
- }
-
- void DBObject::stripTrailingSpaces(char *str)
- {
- register char *ptr = &(str[strlen(str) - 1]);
-
- while (*ptr == ' ')
- *ptr-- = '\0';
- }
-
-