Add SaveBlobToFile

This commit is contained in:
Paul Reeves 2023-01-26 16:35:00 +01:00
parent 331a27bf82
commit 609837361d
1 changed files with 101 additions and 13 deletions

View File

@ -242,8 +242,95 @@ FB_UDR_EXECUTE_FUNCTION
FB_UDR_END_FUNCTION
//END
/***
create or alter function SaveBlobToFile (
afilename varchar(8191),
ablob BLOB
) returns bigint
external name 'MyFirstUDRKit!MFK_SaveBlobToFile!Save blob to file'
engine udr;
***/
FB_UDR_BEGIN_FUNCTION(MFK_SaveBlobToFile)
//BEGIN
FB_UDR_MESSAGE(InMessage,
(FB_CHAR(8191), afilename)
(FB_BLOB, ablob)
);
FB_UDR_MESSAGE(OutMessage,
(FB_BIGINT, result)
);
AutoRelease<IAttachment> att;
AutoRelease<ITransaction> tra;
AutoRelease<IBlob> Blob;
FB_UDR_EXECUTE_FUNCTION
{
// Test Input
if (in->afilenameNull != 0 || in->ablobNull != 0 ) {
out->resultNull = FB_TRUE;
out->result = 0;
}
/*
* DOC NOTE - We do not test for existence of the file here!
*/
fstream File ( in->afilename.str , fstream::out | fstream::binary | fstream::app );
if (! File.is_open()) {
out->result = -2;
return;
}
Blob.reset(att->openBlob(status, tra, &in->ablob, 0, nullptr));
if (Blob == nullptr) {
out->result = -1;
return;
}
vector<char> Buffer (MaxSegmentSize, 0);
unsigned BytesRead = 0;
for (bool Eof = false; !Eof; )
{
switch (Blob->getSegment( status, MaxSegmentSize, Buffer.data(), &BytesRead))
{
case IStatus::RESULT_OK:
case IStatus::RESULT_SEGMENT:
{
File.write( Buffer.data(), Buffer.size() );
out->result += BytesRead;
continue;
}
default:
{
Blob->close( status ); // will close interface
Blob.release();
Eof = true;
break;
}
}
}
Buffer.clear();
File.close();
}
FB_UDR_END_FUNCTION
//END
/*** DDL
create function BillDate (
d integer,
@ -252,19 +339,20 @@ FB_UDR_END_FUNCTION
external name 'my_first_udr_kit!MFK_BillDate!What does BillDate do?'
engine udr;
***/
// FB_UDR_BEGIN_FUNCTION(MFK_BillDate)
//
// FB_UDR_MESSAGE(InMessage,
// (FB_INTEGER, d)
// (FB_INTEGER, date)
// );
// FB_UDR_MESSAGE(OutMessage,
// (FB_DATE, result)
// );
//
//
// FB_UDR_END_FUNCTION
/*
FB_UDR_BEGIN_FUNCTION(MFK_BillDate)
FB_UDR_MESSAGE(InMessage,
(FB_INTEGER, d)
(FB_INTEGER, date)
);
FB_UDR_MESSAGE(OutMessage,
(FB_DATE, result)
);
FB_UDR_END_FUNCTION
*/