Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

cOracleDatabase.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /* POLiTe - Persistent Object Library Test                                    */
00004 /*                                        Ph.D. Thesis by Mgr. Michal Kopecky */
00005 /*                                                                            */
00006 /* Charles University Prague                                                  */
00007 /*                                                                            */
00008 /******************************************************************************/
00009 /*                                                                            */
00010 /* File name: ...                                                             */
00011 /* Module: ......                                                             */
00012 /*                                                                            */
00013 /******************************************************************************/
00014 
00015 // Standard Header(s)
00016 #include <malloc.h>
00017 #include <string.h>
00018 
00019 // Common POLiTe Header(s)
00020 #include <lDefs.h>
00021 #include <lTypes.h>
00022 #include <lTrace.h>
00023 #include <preproc.h>
00024 
00025 // Own Header
00026 #include <cOracleDatabase.h>
00027 
00028 // Other POLiTe Header(s)
00029 #include <cOracleConnection.h>
00030 
00031 OracleDatabase::OracleDatabase(const char *ConnectString):
00032   Database(ConnectString)
00033 //Creates a Database object assigned to a real database
00034 {
00035         #ifdef C_ORACLEDATABASE_TRACE
00036                 if (ConnectString)
00037                         logmsg("Oracle7Database::Oracle7Database(\"%s\") invoked", ConnectString);
00038                 else
00039                         logmsg("OracleDatabase::OracleDatabase() invoked");
00040     #endif
00041 };
00042 
00043 OracleDatabase::~OracleDatabase()
00044 //Database object destructor
00045 {
00046         #ifdef C_ORACLEDATABASE_TRACE
00047                 logmsg("OracleDatabase::~OracleDatabase() invoked");
00048         #endif
00049 }
00050 
00051 bool OracleDatabase::Commit()
00052 //Commit database, after storing modified objects
00053 {
00054         #ifdef C_ORACLEDATABASE_TRACE
00055                 logmsg("Oracle7Database::Commit() invoked");
00056         #endif
00057         return(Database::Commit());
00058 };
00059 
00060 bool OracleDatabase::_Commit()
00061 //Commit database immediately
00062 {
00063         #ifdef C_ORACLEDATABASE_TRACE
00064                 logmsg("OracleDatabase::_Commit() invoked");
00065         #endif
00066         return(Database::_Commit());
00067 };
00068 
00069 bool OracleDatabase::Rollback()
00070 //Rollback database, then destroy all copies in memory
00071 {
00072         #ifdef C_ORACLEDATABASE_TRACE
00073                 logmsg("Oracle7Database::Rollback() invoked");
00074         #endif
00075         return(Database::Rollback());
00076 };
00077 
00078 bool OracleDatabase::_Rollback()
00079 //Rollback database
00080 {
00081         #ifdef C_ORACLEDATABASE_TRACE
00082                 logmsg("OracleDatabase::_Rollback() invoked");
00083         #endif
00084         return(Database::Rollback());
00085 };
00086 
00087 class Connection *OracleDatabase::Connect(
00088         const char * const UserName,
00089         const char * const Password
00090         )
00091 {
00092         int LocHandle, GlobHandle;
00093         int oci_ret; //return value of the OCI call
00094         OracleConnection *DbC;
00095         #ifdef C_ORACLEDATABASE_TRACE
00096                 logmsg("Oracle7Database::Connect() invoked");
00097         #endif
00098         if (!_FindFreeConnectionHandles(LocHandle,GlobHandle))
00099                 throw ObjLibException_ConnectionError();
00100         DbC = new OracleConnection(
00101                 NULL,
00102                 UserName,
00103                 Password,
00104                 LocHandle,
00105                 GlobHandle
00106                 );
00107         if (DbC != NULL) {
00108                 _AllConnections[GlobHandle] = DbC;
00109                 _Connections[LocHandle] = DbC;
00110                 oci_ret = olog(
00111                         (Lda_Def *)&DbC->_LDA,
00112                         (ub1 *)DbC->_HDA,
00113                         (OraText *)UserName,
00114                         (sword)-1,
00115                         (OraText *)Password,
00116                         (sword)-1,
00117                         (OraText *)_ConnectString,
00118                         (sword)-1,
00119                         OCI_LM_DEF
00120                         );
00121                 if (oci_ret) {
00122                         #ifdef C_ORACLEDATABASE_TRACE
00123                                 logmsg(TL_ERROR,"Connect(): olog returns %d", oci_ret);
00124                         #endif
00125                         _DisposeConnectionHandles(LocHandle,GlobHandle);
00126                         delete DbC;
00127                         throw ObjLibException_ConnectionError();
00128                 }
00129                 else {
00130                         DbC->_Database = this;
00131                         DbC->_Strategies = _Strategies;
00132                         return DbC;
00133                 };
00134         };
00135         return DbC;
00136 };
00137 
00138 bool OracleDatabase::WriteDDL(ofstream &S)
00139 // generates DDL stements nedded for the library to run
00140 {
00141         if (!S.rdbuf()->is_open())
00142                 return false;
00143         S <<
00144                 "-- OID generator for Oracle database" << endl <<
00145                 "CREATE SEQUENCE OID_GENERATOR" << endl <<
00146                 "START WITH 1" << endl <<
00147                 "MINVALUE 1" << endl <<
00148                 "MAXVALUE 999999999" << endl <<
00149                 "NOCYCLE" << endl <<
00150                 "NOCACHE;" << endl << endl;
00151         S <<
00152                 "-- Version generator for Oracle database" << endl <<
00153                 "CREATE SEQUENCE VERSION_GENERATOR" << endl <<
00154                 "START WITH 1" << endl <<
00155                 "MINVALUE 1" << endl <<
00156                 "MAXVALUE 999999" << endl <<
00157                 "CYCLE" << endl <<
00158                 "NOCACHE;" << endl << endl;
00159         S.flush();
00160         return true;
00161 };
00162 
00163 char *OracleDatabase::ColumnTypeDDL(char coltype, int collen)
00164 // returns correct database column type for given attribute type
00165 {
00166         switch (coltype)
00167         {
00168                 case TYPE_PTR:
00169                         return StrCpy("NUMBER(10)");
00170                         //break;
00171                 case TYPE_INT:
00172                 case TYPE_UNSIGNED:
00173                         if (collen == sizeof(long int))
00174                                 return StrCpy("NUMBER(10)");
00175                         else if (collen == sizeof(int))
00176                                 return StrCpy("NUMBER(10)");
00177                         else if (collen == sizeof(short int))
00178                                 return StrCpy("NUMBER(5)");
00179                         else if (collen == sizeof(signed char))
00180                                 return StrCpy("NUMBER(3)");
00181                         break;
00182                 case TYPE_FLOAT:
00183                         return StrCpy("NUMBER");
00184                 case TYPE_CHAR:
00185                         return StrCpy("CHAR(1)");
00186                 case TYPE_STRING:
00187                         char *len = LongToStr(collen);
00188                         char *res = StrCpy("VARCHAR2(");
00189                         StrCat(res,2,len,")");
00190                         StrFree(len);
00191                         return res;
00192         };
00193         return StrCpy("UNKNOWN");
00194 };

Generated on Sun Jul 14 20:51:14 2002 for POLiTe by doxygen1.2.16