From f10de103715ef48db72220a7bb50ae69eaa83e4a Mon Sep 17 00:00:00 2001 From: Paul Reeves Date: Tue, 24 Jan 2023 19:23:36 +0100 Subject: [PATCH] Add basic implementation of Blob_Save --- src/MyFirstUDRKit.cpp | 82 +++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/src/MyFirstUDRKit.cpp b/src/MyFirstUDRKit.cpp index 8a59269..236707b 100644 --- a/src/MyFirstUDRKit.cpp +++ b/src/MyFirstUDRKit.cpp @@ -29,10 +29,14 @@ #include #include +#include "MyFirstUDRKit.h" #include #include #include +#include +#include +#include #ifdef HAVE_MATH_H #include @@ -63,6 +67,7 @@ #include +using namespace std; using namespace Firebird; @@ -107,6 +112,7 @@ FB_UDR_EXECUTE_FUNCTION } else { +// This is old style - surely we can do better? out->result = std::numeric_limits::infinity(); ISC_STATUS_ARRAY StatusVector = {isc_arg_gds, isc_arith_except, isc_arg_gds, isc_exception_integer_divide_by_zero, isc_arg_end}; @@ -160,56 +166,78 @@ FB_UDR_BEGIN_FUNCTION(MFK_flagged) FB_UDR_END_FUNCTION + + /*** DDL create function Blob_Save ( afilename varchar(8191), - ablobcallback BLOB + ablob BLOB ) returns bigint external name 'my_first_udr_kit!MFK_Blob_Save!Load file and save to Blob' engine udr; ***/ + +static const size_t MaxSegmentSize = 65535; + FB_UDR_BEGIN_FUNCTION(MFK_Blob_Save) FB_UDR_MESSAGE(InMessage, (FB_CHAR(8191), afilename) - (FB_BLOB, ablobcallback) ); FB_UDR_MESSAGE(OutMessage, + (FB_BLOB, ablob) (FB_BIGINT, result) ); + +AutoRelease att; +AutoRelease tra; + FB_UDR_EXECUTE_FUNCTION { // Test Input - if ((in->afilenameNull != 0) || in->ablobcallbackNull != 0 /* || in->ablobcallback.blob_handle == NULL */ ) { + if (in->afilenameNull != 0) { out->resultNull = FB_TRUE; out->result = 0; } - // open for write & as a binary... -// FILE* file_ = fopen(s, "wb"); -// if (file_ == NULL) { -// ISC_INT64* res = MALLOC(sizeof(ISC_INT64)); -// *res = -2; -// return res; -// } -// // allocate buffer size to equal with max segment size -// ISC_UCHAR* buffer = malloc(b->blob_max_segment); -// if (!buffer) { -// ISC_INT64* res = MALLOC(sizeof(ISC_INT64)); -// *res = -1; -// return res; -// } -// /* see BLOBCALLBACK in ibase.h */ -// ISC_INT64* res = MALLOC(sizeof(ISC_INT64)); -// *res = 0; -// ISC_USHORT len; -// while (b->blob_get_segment(b->blob_handle, buffer, b->blob_max_segment, &len)) { -// *res += fwrite(buffer, 1, len, file_); // write buffer into given file.. (size as per 1 Byte for an element) -// } -// free(buffer); -// fclose(file_); -// return res; + + ifstream File ( in->afilename.str , ios_base::in | ios_base::binary ); + if (! File.is_open()) { + out->result = -2; + return; + } + + AutoRelease Blob(att->createBlob(status, tra, &out->ablob, 0, nullptr)); + if (Blob == nullptr) { + out->result = -1; + return; + } + + vector Buffer (MaxSegmentSize, 0); + streamsize DataSize; + + while ( !File.good() ) { + File.read( Buffer.data(), Buffer.size() ); + DataSize = File.gcount(); + Blob->putSegment( status, DataSize, Buffer.data() ); + out->result += DataSize; + // Perhaps test for badbit here? + } + if (File.bad()) { // Something went wrong + // What to do? + out->resultNull = 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(); + File.close(); }