Add basic implementation of Blob_Save

This commit is contained in:
Paul Reeves 2023-01-24 19:23:36 +01:00
parent cd5e0df1a5
commit f10de10371

View File

@ -29,10 +29,14 @@
#include <ibase.h> #include <ibase.h>
#include <firebird/UdrCppEngine.h> #include <firebird/UdrCppEngine.h>
#include "MyFirstUDRKit.h"
#include <cassert> #include <cassert>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#ifdef HAVE_MATH_H #ifdef HAVE_MATH_H
#include <math.h> #include <math.h>
@ -63,6 +67,7 @@
#include <limits> #include <limits>
using namespace std;
using namespace Firebird; using namespace Firebird;
@ -107,6 +112,7 @@ FB_UDR_EXECUTE_FUNCTION
} }
else else
{ {
// This is old style - surely we can do better?
out->result = std::numeric_limits<double>::infinity(); out->result = std::numeric_limits<double>::infinity();
ISC_STATUS_ARRAY StatusVector = {isc_arg_gds, isc_arith_except, ISC_STATUS_ARRAY StatusVector = {isc_arg_gds, isc_arith_except,
isc_arg_gds, isc_exception_integer_divide_by_zero, isc_arg_end}; 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 FB_UDR_END_FUNCTION
/*** DDL /*** DDL
create function Blob_Save ( create function Blob_Save (
afilename varchar(8191), afilename varchar(8191),
ablobcallback BLOB ablob BLOB
) returns bigint ) returns bigint
external name 'my_first_udr_kit!MFK_Blob_Save!Load file and save to Blob' external name 'my_first_udr_kit!MFK_Blob_Save!Load file and save to Blob'
engine udr; engine udr;
***/ ***/
static const size_t MaxSegmentSize = 65535;
FB_UDR_BEGIN_FUNCTION(MFK_Blob_Save) FB_UDR_BEGIN_FUNCTION(MFK_Blob_Save)
FB_UDR_MESSAGE(InMessage, FB_UDR_MESSAGE(InMessage,
(FB_CHAR(8191), afilename) (FB_CHAR(8191), afilename)
(FB_BLOB, ablobcallback)
); );
FB_UDR_MESSAGE(OutMessage, FB_UDR_MESSAGE(OutMessage,
(FB_BLOB, ablob)
(FB_BIGINT, result) (FB_BIGINT, result)
); );
AutoRelease<IAttachment> att;
AutoRelease<ITransaction> tra;
FB_UDR_EXECUTE_FUNCTION FB_UDR_EXECUTE_FUNCTION
{ {
// Test Input // Test Input
if ((in->afilenameNull != 0) || in->ablobcallbackNull != 0 /* || in->ablobcallback.blob_handle == NULL */ ) { if (in->afilenameNull != 0) {
out->resultNull = FB_TRUE; out->resultNull = FB_TRUE;
out->result = 0; out->result = 0;
} }
// open for write & as a binary...
// FILE* file_ = fopen(s, "wb"); ifstream File ( in->afilename.str , ios_base::in | ios_base::binary );
// if (file_ == NULL) { if (! File.is_open()) {
// ISC_INT64* res = MALLOC(sizeof(ISC_INT64)); out->result = -2;
// *res = -2; return;
// return res; }
// }
// // allocate buffer size to equal with max segment size AutoRelease<IBlob> Blob(att->createBlob(status, tra, &out->ablob, 0, nullptr));
// ISC_UCHAR* buffer = malloc(b->blob_max_segment); if (Blob == nullptr) {
// if (!buffer) { out->result = -1;
// ISC_INT64* res = MALLOC(sizeof(ISC_INT64)); return;
// *res = -1; }
// return res;
// } vector<char> Buffer (MaxSegmentSize, 0);
// /* see BLOBCALLBACK in ibase.h */ streamsize DataSize;
// ISC_INT64* res = MALLOC(sizeof(ISC_INT64));
// *res = 0; while ( !File.good() ) {
// ISC_USHORT len; File.read( Buffer.data(), Buffer.size() );
// while (b->blob_get_segment(b->blob_handle, buffer, b->blob_max_segment, &len)) { DataSize = File.gcount();
// *res += fwrite(buffer, 1, len, file_); // write buffer into given file.. (size as per 1 Byte for an element) Blob->putSegment( status, DataSize, Buffer.data() );
// } out->result += DataSize;
// free(buffer); // Perhaps test for badbit here?
// fclose(file_); }
// return res; 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();
} }