#include "OracleDB.h" OracleDB::OracleDB() throw (SQLException) { // // Constructor: Create the Oracle environment. // fenv = Environment::createEnvironment(Environment::DEFAULT); fconn = NULL; fresSet = NULL; frowCount = 0; ffieldCount = 0; } OracleDB::~OracleDB() throw (SQLException) { // // Destructor: Disconnect (if connected) and terminate the environment. // if ( fconn != NULL ) disconnect(); if ( fenv != NULL ) Environment::terminateEnvironment(fenv); } void OracleDB::connect(string user, string password, string connectString) throw (SQLException) { // // Connect to the DB using the specified details // fconn = fenv->createConnection(user.c_str(), password.c_str(), connectString.c_str()); } bool OracleDB::isConnected() { // // Determine if we are connected // bool retval = false; if ( fconn != NULL ) retval = true; return retval; } void OracleDB::disconnect() throw (SQLException) { // // Disconnect // fenv->terminateConnection(fconn); fconn = NULL; Environment::terminateEnvironment(fenv); fenv = NULL; } void OracleDB::executeUpdate(string query) throw (SQLException) { // // Update a table. No rows are returned // fstat = fconn->createStatement(query.c_str()); fstat->executeUpdate(); fconn->terminateStatement(fstat); } void OracleDB::calcRowCount(string query) throw (SQLException) { // // Calculate the number of rows in the result of this query. Oracle has no // get rows function. This is to rebuild this function by selecting COUNT(*). // //string inputquery = query; //cout << "Input " << inputquery << endl; string::size_type loc1 = query.find("SELECT", 0); string::size_type loc2 = query.find("FROM", 0); query.replace(loc1+7, loc2-loc1-8, "COUNT(*)"); //cout << "NewQuery " << query << endl; fstat = fconn->createStatement(query); //cout << "Created Statement" << endl; fresSet = fstat->executeQuery(); //cout << "Execute Statement" << endl; fresSet->next(); frowCount = fresSet->getInt(1); fstat->closeResultSet(fresSet); //cout << "Close Res Set" << endl; fconn->terminateStatement(fstat); //cout << "Terminate Statement" << endl; } void OracleDB::executeQuery(string query) throw (SQLException) { // // Execute a query. Rows are returned // fstat = fconn->createStatement(query); fstat->setPrefetchMemorySize(1024*1024*64); //32MB cache fresSet = fstat->executeQuery(); newQuery = true; } void OracleDB::executeQuery() throw (SQLException) { // // Query table with parameterized query. Set variables before. // fresSet = fstat->executeQuery(); } void OracleDB::freeResult() throw (SQLException) { // // Free Result // fstat->closeResultSet(fresSet); fconn->terminateStatement(fstat); } void OracleDB::getRow() throw (SQLException) { // // Get next row // fresSet->next(); } int32_t OracleDB::getRowCount() throw (SQLException) { // // Return the number of rows for the selected query // return frowCount; } int32_t OracleDB::getFieldCount() throw (SQLException) { // // Return the number of fields per row for the selected query // if ( newQuery == true ) { fieldInfo = new vector( fresSet->getColumnListMetaData() ); //fieldCount = fieldInfo.getInt(MetaData::ATTR_NUM_COLS); ffieldCount = fieldInfo->size(); //correct ??? newQuery = false; delete fieldInfo; } return ffieldCount; } void OracleDB::printFieldNames() throw (SQLException) { // //TODO: Implement print32_t field names, this code here (from the oracle manual) is not working // /* fieldInfo = new vector(fresSet->getColumnListMetaData()); for(int32_t pos = 0; pos < fieldInfo->size(); pos++){ cout << pos << " | " << fieldInfo[pos].getString(MetaData::ATTR_OBJ_NAME) << endl; } */ } int32_t OracleDB::getRowValueAsInt(int32_t colum) throw (SQLException) { // // get row value and return it as integer. // return fresSet->getInt(colum + 1); } string OracleDB::getRowValueAsString(int32_t colum) throw (SQLException) { // // get row value and return it as string // return fresSet->getString(colum + 1); } string OracleDB::getRowValueDateAsString(int32_t colum) throw (SQLException) { // // get row value (which is a date) and return it as string // Timestamp ts; ts = fresSet->getTimestamp(colum + 1); return ts.toText("dd.mm.yyyy hh:mi:ss [tzh:tzm]", 0); } float OracleDB::getRowValueAsFloat(int32_t colum) throw (SQLException) { // // get row value and return it as float. // return fresSet->getFloat(colum + 1); } double OracleDB::getRowValueAsDouble(int32_t colum) throw (SQLException) { // // get row value and return it as double. // return fresSet->getDouble(colum + 1); } uint32_t OracleDB::getRowValueAsBlob(int32_t column, char *buffer) throw (SQLException) { // // get row value, which is blob, and return its data in the buffer. // uint32_t size; Blob blob = fresSet->getBlob(column + 1); if ( blob.isNull() ) return 0; else { blob.open(OCCI_LOB_READONLY); size = blob.length(); //cout << "OracleDB::getRowValueAsBlob: Size of the blob is: " << size << endl; if (!size) return 0; Stream *instream = blob.getStream(1,0); char *tmpbuffer = new char[size]; memset(tmpbuffer, 0, size); //buffer = new char[size]; // does not work ... why? memset(buffer, 0, size); instream->readBuffer(tmpbuffer, size); blob.closeStream(instream); blob.close(); /* cout << "dumping blob: |"; for (uint32_t i = 0; i < size; ++i) cout << (int)buffer[i] << "|"; cout << endl; */ memcpy(buffer, tmpbuffer, size); // seems to work delete [] tmpbuffer; } return size; } void OracleDB::createTransaction() throw (SQLException) { // // Start transaction by creating the statement object. // fstat = fconn->createStatement(); } void OracleDB::setSQL(string query) throw (SQLException) { // // Prepare a parameterized statements. It will be able to accept input using parameters. // fstat->setSQL(query.c_str()); } void OracleDB::setSQLgetRowCount(string query) throw (SQLException) { // // Prepare a parameterized statements. It will be able to accept input using parameters. // string::size_type loc1 = query.find("SELECT", 0); string::size_type loc2 = query.find("FROM", 0); query.replace(loc1+7, loc2-loc1-8, "COUNT(*)"); fstat->setSQL(query.c_str()); } void OracleDB::setString(uint32_t paramIndex, const string val) throw (SQLException) { // // Set a parameter as string // fstat->setString(paramIndex+1, val); } void OracleDB::setNumber(uint32_t paramIndex, const int32_t val) throw (SQLException) { // // Set a parameter as number // fstat->setNumber(paramIndex+1, val); } void OracleDB::setDate(uint32_t paramIndex, const Date val) throw (SQLException) { // // Set a parameter as date // fstat->setDate(paramIndex+1, val); } void OracleDB::setFloat(uint32_t paramIndex, const float val) throw (SQLException) { // // Set a parameter as float // fstat->setFloat(paramIndex+1, val); } void OracleDB::setDouble(uint32_t paramIndex, const double val) throw (SQLException) { // // Set a parametOracleDB::getRowValueAsBlob: er as double // fstat->setDouble(paramIndex+1, val); } void OracleDB::setBlob(uint32_t paramIndex, const char *buffer, uint32_t bytes) throw (SQLException) { // // Set a parameter as blob // Blob blob = fresSet->getBlob(paramIndex+1); if ( blob.isNull() ) { cout << "OracleDB::setBlob: Null blob" << endl; return; } else { blob.open(OCCI_LOB_READWRITE); //cout << "OracleDB::setBlob: Size of blob in DB is: " << blob.length() << endl; blob.trim(0); // needed ... ??? maybe slow //cout << "OracleDB::setBlob: Size of blob in DB is: " << blob.length() << endl; Stream *outstream = blob.getStream(1, 0); outstream->writeBuffer((char*)buffer, bytes); char *c = (char *)""; outstream->writeLastBuffer(c, 0); blob.closeStream(outstream); /* int32_t size = blob.length(); cout << "Length of the blob is: " << size << endl; Stream *instream = blob.getStream(1, 0); char *tmpbuffer = new char[size]; memset(tmpbuffer, 0, size); instream->readBuffer(tmpbuffer, size); cout << "dumping blob: "; for ( int32_t i = 0; i < size; ++i ) cout << (int)tmpbuffer[i] << "|"; cout << endl; delete [] tmpbuffer; blob.closeStream(instream); */ blob.close (); } } void OracleDB::executeUpdate() throw (SQLException) { // // Update table with parameterized query. Set variables before. No rows are returned. // fstat->executeUpdate(); } void OracleDB::deleteTransaction() throw (SQLException) { // // Terminate the statement and stop the transaction. // fconn->terminateStatement(fstat); }