//#include "Blob.h" #include #include #include "../DB/OracleDB.h" const int32_t maxbuffersize = 1000000; AnyDB *db; uint32_t readFile(string filename, char *buffer) { // ifstream *inFile = new ifstream(); cout << "BlobMain: Opening File " << filename.c_str() << " for read" << endl; inFile->open(filename.c_str(), ios::binary); if ( inFile->is_open() == false){ cout << "BlobMain: Could not open File " << filename.c_str() << endl; return 0; } // get size inFile->seekg(0, ios::end); uint32_t filesize = inFile->tellg(); // read char *tmpbuffer = new char[filesize]; memset(tmpbuffer, 0, filesize); inFile->seekg (0, ios::beg); inFile->read((char*)tmpbuffer, filesize); inFile->close(); memcpy(buffer, tmpbuffer, filesize); // delete [] tmpbuffer; return filesize; // bytes } void writeFile(string filename, const char *buffer, uint32_t filesize /*bytes*/) { // ofstream *outFile = new ofstream(); cout << "BlobMain: Opening File " << filename.c_str() << " for write" << endl; outFile->open(filename.c_str(), ios::binary); if ( outFile->is_open() == false){ cout << "BlobMain: Could not open File " << filename.c_str() << endl; return; } // Write outFile->write((char*)buffer, filesize); outFile->close(); } void readBlob() { string query = "SELECT col3 FROM BTEST WHERE col1 = 7777"; char* buffer = new char[maxbuffersize]; cout << "BlobMain: Reading blob" << endl; try { db->calcRowCount(query.c_str()); cout << "BlobMain: Will get " << db->getRowCount() << " rows" << endl; db->createTransaction(); db->setSQL(query.c_str()); db->executeQuery(); db->getRow(); uint32_t bytes = db->getRowValueAsBlob(0, buffer); cout << "BlobMain: Got Blob, size " << bytes << endl; if ( buffer ) { // Dump data to screen /* cout << "dumping blob: |"; for (uint32_t i = 0; i < bytes; ++i) cout << (int)buffer[i] << "|"; cout << endl; */ } else cout << "BlobMain: Buffer is Null" << endl; writeFile("/home/fee/test2.jpg", buffer, bytes); db->freeResult(); } catch (SQLException ea) { cout << ea.what(); } } void updateBlob() { string query = "SELECT col3 FROM BTEST WHERE col1 = 7777 FOR UPDATE"; char* buffer = new char[maxbuffersize]; string file = "/home/fee/test.jpg"; file = "/home/fee/brujeria-marijuana.jpg"; //file = "/home/fee/DCS-board_routing.txt"; cout << "BlobMain: Updating blob" << endl; try { db->createTransaction(); db->setSQL(query.c_str()); db->executeQuery(); db->getRow(); uint32_t bytes = readFile(file.c_str(), buffer); cout << "BlobMain: Filesize is " << bytes << endl; /* cout << "read data: |"; for ( uint32_t i = 0; i < bytes; i++ ) cout << (int)buffer[i] << "|"; cout << endl; */ db->setBlob(0, buffer, bytes); db->freeResult(); } catch (SQLException ea) { cout << ea.what(); } } int main (void) { string query; char* buffer = new char[maxbuffersize]; try { db = new OracleDB(); db->connect("alice_dcs_tpc", "alice7dcs7tpc", "devdb10"); //db->connect("tpccfg", "tpc", "aldcs031"); cout << "BlobMain: Inserting into table" << endl; query = "INSERT INTO BTEST VALUES (7777, 11001, '1a0100011010', 'HEM')"; db->executeUpdate(query.c_str()); query = "INSERT INTO BTEST VALUES (6666, 11001, '11100', 'SHE')"; db->executeUpdate(query.c_str()); } catch (SQLException ea) { cout << ea.what(); } readBlob(); updateBlob(); readBlob(); try { cout << "BlobMain: Deleting from table" << endl; query = "DELETE BTEST WHERE col1 = 6666"; db->executeUpdate(query.c_str()); query = "DELETE BTEST WHERE col1 = 7777"; db->executeUpdate(query.c_str()); db->disconnect(); } catch (SQLException ea) { cout << ea.what(); } delete buffer; buffer = 0; return 1; }