// ============================================================================= // ============================================================================= int32_t writeBlobFile(string filename, const char *buffer, uint32_t filesize /*bytes*/) { // // Write a blob from the buffer to a file // ofstream *outFile = new ofstream(); outFile->open(filename.c_str(), ios::binary); if ( outFile->is_open() == false){ cout << "*Could not open File " << filename.c_str() << endl; return 0; } // Write cout << "*Writing blob to file " << filename.c_str() << endl; outFile->write((char*)buffer, filesize); outFile->close(); return 1; } // ============================================================================= // ============================================================================= int32_t selectBlob() { // // Read a blob from the DB and save it into a file // string query = "SELECT "; query += arguments.pSelectField.c_str(); query += " FROM "; query += arguments.pSelectTableBlob.c_str(); char* buffer = new char[pMaxBufferSize]; cout << "*Reading blob" << endl; try { db->calcRowCount(query.c_str()); if ( db->getRowCount() > 1 ) { cout << "*Error: More than one row to be returned!" << endl; return 0; } if ( db->getRowCount() < 1 ) { cout << "*Error: No row to be returned!" << endl; return 0; } db->createTransaction(); db->setSQL(query.c_str()); db->executeQuery(); db->getRow(); uint32_t bytes = db->getRowValueAsBlob(0, buffer); cout << "*Got Blob, size " << bytes << endl; if ( bytes > (uint32_t)pMaxBufferSize ) { cout << "*Error: Blob is bigger than 1MB (pMaxBufferSize)!" << endl; return 0; } if ( buffer ) { // Dump data to screen /* cout << "dumping blob: |"; for (uint32_t i = 0; i < bytes; ++i) cout << (int)buffer[i] << "|"; cout << endl; */ } if (!buffer) cout << "*Buffer is Null" << endl; writeBlobFile(arguments.pBlobFileName, buffer, bytes); db->freeResult(); } catch (SQLException ea) { cout << ea.what(); } return 1; } // ============================================================================= // ============================================================================= uint32_t readBlobFile(string filename, char *buffer) { // // Read a blob from a file into the buffer // ifstream *inFile = new ifstream(); inFile->open(filename.c_str(), ios::binary); if ( inFile->is_open() == false){ cout << "*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); cout << "*Reading blob from file " << filename.c_str() << endl; inFile->read((char*)tmpbuffer, filesize); inFile->close(); memcpy(buffer, tmpbuffer, filesize); // delete [] tmpbuffer; return filesize; // bytes } // ============================================================================= // ============================================================================= int32_t updateBlob() { string query = "SELECT "; query += arguments.pSelectField.c_str(); query += " FROM "; query += arguments.pSelectTableBlob.c_str(); query += " FOR UPDATE"; char* buffer = new char[pMaxBufferSize]; cout << "*Updating blob" << endl; try { db->createTransaction(); db->setSQL(query.c_str()); db->executeQuery(); db->getRow(); uint32_t bytes = readBlobFile(arguments.pBlobFileName.c_str(), buffer); cout << "*Filesize is " << bytes << endl; if ( bytes > (uint32_t)pMaxBufferSize ) { cout << "*Error: File is bigger than 1MB (pMaxBufferSize)!" << endl; return 0; } /* 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(); } return 1; }