diff --git a/src/MyFirstUDRKit.cpp b/src/MyFirstUDRKit.cpp index 7e5711b..4cde255 100644 --- a/src/MyFirstUDRKit.cpp +++ b/src/MyFirstUDRKit.cpp @@ -29,6 +29,8 @@ #include #include +#include + #include "UdrCppTemplates.h" #include @@ -183,7 +185,7 @@ FB_UDR_MESSAGE(OutMessage, AutoRelease att; AutoRelease tra; -AutoRelease blob; +ISC_STATUS_ARRAY statusVector = {0}; FB_UDR_EXECUTE_FUNCTION { @@ -193,57 +195,58 @@ FB_UDR_EXECUTE_FUNCTION return; } + std::ifstream FileReader; FileReader.open( in->afilename.str , std::ifstream::binary ); if (! FileReader.is_open()) { - out->result = -2; + out->ablobNull = FB_TRUE; return; } + + try { att.reset(context->getAttachment(status)); tra.reset(context->getTransaction(status)); - blob.reset(att->openBlob(status, tra, &in->ablob, 0, nullptr)); - + AutoRelease blob(att->createBlob(status, tra, &out->ablob, 0, nullptr)); if (blob == nullptr) { - out->result = -1; + out->ablobNull = FB_TRUE; return; } - std::vector Buffer (MaxSegmentSize, 0); + std::vector Buffer (MaxSegmentSize, 0); std::streamsize DataSize; while ( FileReader.good() ) { - FileReader.read( Buffer.data(), Buffer.size() ); + FileReader.read( (char *)Buffer.data(), Buffer.size() ); DataSize = FileReader.gcount(); - // fail seems to be here... - // Probably need to declare buffer differently. blob->putSegment( status, DataSize, Buffer.data() ); - out->result += DataSize; // Perhaps test for badbit here? } if (FileReader.bad()) { // Something went wrong // What to do? - out->resultNull = FB_TRUE; + out->ablobNull = FB_TRUE; // What do we do with the partially written blob? Is cancel enough? blob->cancel( status ); - // Should we reset result to 0? - } - blob->close( status ); - blob->release(); - Buffer.clear(); + // ****** IT IS VITAL TO SET THIS IF WRITING TO BLOB SUCCEEDED ******** + out->ablobNull = FB_FALSE; + + blob->close( status ); + blob->release(); + Buffer.clear(); + FileReader.close(); } - catch (...) + catch ( const FbException& error ) { + throw Firebird::FbException(status, statusVector); + } + catch (...) { // This generates an unrecognised C++ exception, which is insufficient. throw std::runtime_error("Error writing stream to BLOB."); } - FileReader.close(); - out->result = 1; - }