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

cOracleConnection.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 // OCI Interface Header
00016 #include <lOracle.h>    //OCI interface for database emulation
00017 
00018 // Common POLiTe Header(s)
00019 #include <lDefs.h>
00020 #include <lTypes.h>
00021 #include <lTrace.h>
00022 
00023 // Own Header
00024 #include <cOracleConnection.h>
00025 
00026 // Other POLiTe Header(s)
00027 #include <cOracleCursor.h>
00028 
00029 #ifdef NO_DATABASE_PRESENT
00030         long _OidGenerator = 0;
00031         long _VersionGenerator = 0;
00032         // thiose variables are there temporary
00033         // if the database connectivity is not available
00034 #endif
00035 
00036 OracleConnection::OracleConnection(
00037         class Database *DB,
00038         const char *UserName,
00039         const char *Password,
00040         const int LocHandle,
00041         const int GlobHandle
00042         ): Connection(DB,UserName,Password,LocHandle,GlobHandle)
00043 //Creates a new database connection
00044 {
00045         #ifdef C_ORACLECONNECTION_TRACE
00046                 logmsg("OracleConnection::OracleConnection() invoked");
00047         #endif
00048 
00049         int i;
00050         char *addr;
00051 
00052         addr = (char *)&_HDA;
00053         for (i = 0; i < HDA_SIZE; i++)
00054                 addr[i] = '\0';
00055         addr = (char *)&_LDA;
00056         for (i = 0; i < sizeof(Lda_Def); i++)
00057                 addr[i] = '\0';
00058         _DefaultCursor = new OracleCursor(this);
00059 
00060         #ifdef C_ORACLECONNECTION_TRACE
00061                 logmsg("OracleConnection::OracleConnection() finished");
00062         #endif
00063 };
00064 
00065 OracleConnection::~OracleConnection()
00066 //OracleConnection object destructor
00067 {
00068         #ifdef C_ORACLECONNECTION_TRACE
00069                 logmsg("OracleConnection::~OracleConnection() invoked and finished");
00070         #endif
00071 }
00072 
00073 bool OracleConnection::_Sql(const char * const SqlCommand)
00074 {
00075         #ifdef C_ORACLECONNECTION_TRACE
00076                 logmsg(TL_INFO_SQL,"O7DbC::_Sql(\"%s\") invoked",SqlCommand);
00077         #endif
00078 
00079         if (_DefaultCursor == NULL)
00080                 throw ObjLibException_ConnectionError();
00081         _Open();
00082         _Prepare(SqlCommand);
00083         _Execute();
00084         _Close();
00085         return true;
00086 };
00087 
00088 bool OracleConnection::_Commit()
00089 {
00090         #ifdef C_ORACLECONNECTION_TRACE
00091                 logmsg(TL_INFO_SQL,"O7DbC::_Commit() invoked");
00092         #endif
00093         if (ocom((Lda_Def *)&_LDA))
00094                 throw ObjLibException_ConnectionError();
00095         return true;
00096 };
00097 
00098 bool OracleConnection::_Rollback()
00099 {
00100         #ifdef C_ORACLECONNECTION_TRACE
00101                 logmsg(TL_INFO_SQL,"O7DbC::_Rollback() invoked");
00102         #endif
00103         if (orol((Lda_Def *)&_LDA))
00104                 throw ObjLibException_ConnectionError();
00105         return true;
00106 };
00107 
00108 bool OracleConnection::_Savepoint(const char * const Name)
00109 {
00110         #ifdef C_ORACLECONNECTION_TRACE
00111                 logmsg(TL_INFO_SQL,"O7DbC::_Savepoint(\"%s\") invoked",Name);
00112         #endif
00113         char *stmt = NULL;
00114         StrCat(stmt,2,"SAVEPOINT ",Name);
00115         _Sql(stmt);
00116         StrFree(stmt);
00117         return true;
00118 };
00119 
00120 bool OracleConnection::_RollbackToSavepoint(const char * const Name)
00121 {
00122         #ifdef C_ORACLECONNECTION_TRACE
00123                 logmsg(TL_INFO_SQL,"O7DbC::_RollbackToSavepoint(\"%s\") invoked",Name);
00124         #endif
00125         char *stmt = NULL;
00126         StrCat(stmt,2,"ROLLBACK TO SAVEPOINT ",Name);
00127         _Sql(stmt);
00128         StrFree(stmt);
00129         return true;
00130 };
00131 
00132 bool OracleConnection::Sql(const char * const SqlCommand)
00133 {
00134         #ifdef C_ORACLECONNECTION_TRACE
00135                 logmsg("OracleConnection::Sql(\"%s\") invoked", SqlCommand);
00136         #endif
00137         return Connection::Sql(SqlCommand);
00138 };
00139 
00140 bool OracleConnection::Commit()
00141 //Commit database, after storing modified objects
00142 {
00143         #ifdef C_ORACLECONNECTION_TRACE
00144                 logmsg("OracleConnection::Commit() invoked");
00145         #endif
00146         return(Connection::Commit());
00147 };
00148 
00149 bool OracleConnection::Rollback()
00150 //Rollback database, then destroy all copies in memory
00151 {
00152         #ifdef C_ORACLECONNECTION_TRACE
00153                 logmsg("OracleConnection::Rollback() invoked");
00154         #endif
00155         return(Connection::Rollback());
00156 };
00157 
00158 bool OracleConnection::Savepoint(const char * const Name)
00159 //Create savepoint, after storing all modified objects
00160 {
00161         #ifdef C_ORACLECONNECTION_TRACE
00162                 logmsg("OracleConnection::Savepoint(\"%s\") invoked",Name);
00163         #endif
00164         return(Connection::Savepoint(Name));
00165 };
00166 
00167 bool OracleConnection::RollbackToSavepoint(const char * const Name)
00168 //Rollback to savepoint, then destroy all data in memory
00169 {
00170         #ifdef C_ORACLECONNECTION_TRACE
00171                 logmsg("OracleConnection::RollbackToSavepoint(\"%s\") invoked",Name);
00172         #endif
00173         return(Connection::RollbackToSavepoint(Name));
00174 };
00175 
00176 bool OracleConnection::Disconnect()
00177 //Commit work and disconnect from database
00178 {
00179         int oci_ret; //return value of the OCI call
00180 
00181         #ifdef C_ORACLECONNECTION_TRACE
00182                 logmsg("OracleConnection::Disconnect() invoked");
00183         #endif
00184         if (!Commit())
00185                 return(false);
00186         //Commit work release
00187         oci_ret = ologof((Lda_Def *)&_LDA);
00188         if (oci_ret) {
00189                 #ifdef C_ORACLECONNECTION_TRACE
00190                         logmsg(TL_ERROR,"Disconnect(): ologof returns %d", oci_ret);
00191                 #endif
00192                 throw ObjLibException_ConnectionError();
00193         };
00194         return(Connection::Disconnect());
00195 };
00196 
00197 bool OracleConnection::Abort()
00198 //Rollback work and disconnect from database
00199 {
00200         int oci_ret; //return value of the OCI call
00201 
00202         #ifdef C_ORACLECONNECTION_TRACE
00203                 logmsg("OracleConnection::Abort() invoked");
00204         #endif
00205         if (!Rollback())
00206                 return(false);
00207         //Rollback work release
00208         oci_ret = ologof((Lda_Def *)&_LDA);
00209         if (oci_ret) {
00210                 #ifdef C_ORACLECONNECTION_TRACE
00211                         logmsg(TL_ERROR,"Abort(): ologof returns %d", oci_ret);
00212                 #endif
00213                 throw ObjLibException_ConnectionError();
00214     };
00215         return(Connection::Abort());
00216 };
00217 
00218 bool OracleConnection::Autocommit(bool ac)
00219 {
00220         int oci_ret; //return value of the OCI call
00221         #ifdef C_ORACLECONNECTION_TRACE
00222                 logmsg("bool OracleConnection::Autocommit(bool) invoked");
00223         #endif
00224         if (ac)
00225                 oci_ret = ocon((Lda_Def *)&_LDA);
00226         else
00227                 oci_ret = ocof((Lda_Def *)&_LDA);
00228         if (oci_ret)
00229         {
00230                 #ifdef C_ORACLECONNECTION_TRACE
00231                         logmsg(TL_ERROR,"Autocommit(): ocon/ocof returns %d", oci_ret);
00232                 #endif
00233                 throw ObjLibException_ConnectionError();
00234     };
00235         return true;
00236 };
00237 
00238 //Obtaining of the new cursor
00239 Cursor *OracleConnection::_GetNewCursor()
00240 {
00241         Cursor *_retval = new OracleCursor(this);
00242         if (_retval == NULL)
00243                 throw ObjLibException_NoMemory();
00244         return _retval;
00245 };
00246 
00247 
00248 bool OracleConnection::_NextOID(long int &nxtoid, long int &nxtsn)
00249 // generates new OID and Serial Number for new object
00250 {
00251         bool retval;
00252 
00253         retval = _Open();
00254         retval = retval && _Prepare(
00255                 "SELECT OID_GENERATOR.NEXTVAL, VERSION_GENERATOR.NEXTVAL FROM DUAL"
00256                 // VERSION_GENERATOR is a DB SEQUENCE
00257                 );
00258         retval = retval && _Execute();
00259         retval = retval && _PreFetchBind(1,&nxtoid,sizeof(long int),'l');
00260         retval = retval && _PreFetchBind(2,&nxtsn,sizeof(long int),'l');
00261         retval = retval && _FetchNext();
00262         retval = retval && _Close();
00263         #ifdef NO_DATABASE_PRESENT
00264                 nxtsn = ++_VersionGenerator;
00265         #endif // NO_DATABASE_PRESENT
00266         return retval;
00267 };
00268 
00269 bool OracleConnection::_NextSN(long int curoid, long int &nxtsn)
00270 // generates new Serial Number for object
00271 {
00272         bool retval;
00273 
00274         retval = _Open();
00275         retval = retval && _Prepare(
00276                 "SELECT VERSION_GENERATOR.NEXTVAL FROM DUAL"
00277                 // VERSION_GENERATOR is a DB SEQUENCE
00278                 );
00279         retval = retval && _Execute();
00280         retval = retval && _PreFetchBind(1,&nxtsn,sizeof(long int),'l');
00281         retval = retval && _FetchNext();
00282         retval = retval && _Close();
00283         #ifdef NO_DATABASE_PRESENT
00284                 nxtsn = ++_VersionGenerator;
00285         #endif // NO_DATABASE_PRESENT
00286         return retval;
00287 };

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