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 <firebird/UdrCppEngine.h>
#include "MyFirstUDRKit.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#ifdef HAVE_MATH_H
#include <math.h>
@ -63,6 +67,7 @@
#include <limits>
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<double>::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<IAttachment> att;
AutoRelease<ITransaction> 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<IBlob> Blob(att->createBlob(status, tra, &out->ablob, 0, nullptr));
if (Blob == nullptr) {
out->result = -1;
return;
}
vector<char> 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();
}