MyFirstUDRKit/src/MyFirstUDRKit.cpp

420 lines
9.1 KiB
C++

/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Adriano dos Santos Fernandes
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2008 Adriano dos Santos Fernandes <adrianosf@gmail.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* Paul Reeves, 2023
*
*/
#include "MyFirstUDRKit.h"
#include <ibase.h>
#include <firebird/UdrCppEngine.h>
#include <firebird/Message.h>
#include "UdrCppTemplates.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <memory>
#ifdef HAVE_MATH_H
#include <math.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <ctime>
# endif
#endif
#ifdef HAVE_SYS_TIMEB_H
# include <sys/timeb.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#include <limits>
#ifdef _WIN32
#include <windows.h>
#define SYSERROR() GetLastError()
#else
#include <cerrno>
#define SYSERROR() errno
#endif
using namespace Firebird;
// First, let's start with a function from the UdfBackwardCompatibility
// library and add some additional comments.
// We will also use self-docmenting variable names
// //////////////////////////////////////////////////
/***
* create function div (
* anumerator bigint,
* adenominator bigint
* ) returns bigint
* external name 'MyFirstUDRKit!MFK_div!Divide anumerator by adenominator'
* engine udr;
***/
FB_UDR_BEGIN_FUNCTION(MFK_div)
// Divide integer anumerator 1 by integer adenominator using the stdlib
// Note this differs from the example that shifts with firebird as it returns
// a bigint, not a double precision
//BEGIN
FB_UDR_MESSAGE(InMessage,
(FB_BIGINT, anumerator)
(FB_BIGINT, adenominator)
);
FB_UDR_MESSAGE(OutMessage,
(FB_BIGINT, result)
);
FB_UDR_EXECUTE_FUNCTION
{
if (in->anumeratorNull || in->adenominatorNull)
{
out->resultNull = FB_TRUE;
out->result = 0;
}
else
{
out->resultNull = FB_FALSE;
if (in->adenominator) {
out->result = std::div(in->anumerator, in->adenominator).quot;
}
else
{
out->result = int( 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};
FbException::check(isc_exception_integer_divide_by_zero, status, StatusVector);
}
}
}
FB_UDR_END_FUNCTION
//END
/*** DDL
create function flagged (
flags integer,
flag integer
) returns integer
external name 'MyFirstUDRKit!MFK_flagged!Check if flag is set in flags. flag is zero-based.'
engine udr;
***/
FB_UDR_BEGIN_FUNCTION (MFK_flagged)
//BEGIN
FB_UDR_MESSAGE(InMessage,
(FB_INTEGER, flags)
(FB_INTEGER, flag)
);
FB_UDR_MESSAGE(OutMessage,
(FB_INTEGER, result)
);
FB_UDR_EXECUTE_FUNCTION
{
if ( in->flagsNull != 0 ) {
out->resultNull = FB_TRUE;
out->result = 0;
}
else
{
out->resultNull = FB_FALSE;
ISC_UINT64 i = ( 1ULL << in->flag );
out->result = ( in->flags & i ) ? 1 : 0;
}
}
FB_UDR_END_FUNCTION
//END
/*** DDL
* create or alter function LoadBlobFromFile (
* afilename varchar(8191),
* ablob BLOB
* ) returns bigint
* external name 'MyFirstUDRKit!MFKLoadBlobFromFile!Load file and save to Blob'
* engine udr;
***/
// NOTE This function would be better as a procedure as that would allow it
// to return the bytes written to the blob.
FB_UDR_BEGIN_FUNCTION (MFK_LoadBlobFromFile)
//BEGIN
FB_UDR_MESSAGE(InMessage,
(FB_VARCHAR(8191), afilename)
);
FB_UDR_MESSAGE(OutMessage,
(FB_BLOB, ablob)
);
AutoRelease<IAttachment> att;
AutoRelease<ITransaction> tra;
ISC_STATUS_ARRAY statusVector = {0};
FB_UDR_EXECUTE_FUNCTION
{
// Test Input
if (in->afilenameNull != 0) {
out->ablobNull = FB_TRUE;
return;
}
std::ifstream FileReader;
FileReader.open( in->afilename.str , std::ifstream::binary | std::ifstream::in );
if (! FileReader.is_open()) {
// if (! FileReader.good()) {
out->ablobNull = FB_TRUE;
return;
}
try {
att.reset(context->getAttachment(status));
tra.reset(context->getTransaction(status));
AutoRelease<IBlob> blob(att->createBlob(status, tra, &out->ablob, 0, nullptr));
if (blob == nullptr) {
out->ablobNull = FB_TRUE;
return;
}
std::vector<std::uint8_t> Buffer (MaxSegmentSize, 0);
std::streamsize DataSize;
while ( FileReader.good() ) {
FileReader.read( (char *)Buffer.data(), Buffer.size() );
DataSize = FileReader.gcount();
blob->putSegment( status, DataSize, Buffer.data() );
// Perhaps test for badbit here?
}
if (FileReader.bad()) { // Something went wrong
// What to do?
out->ablobNull = FB_TRUE;
// What do we do with the partially written blob? Is cancel enough?
blob->cancel( status );
}
// ****** IT IS VITAL TO SET THIS IF WRITING TO BLOB WAS SUCCESSFUL ******* //
out->ablobNull = FB_FALSE;
blob->close( status );
blob->release();
Buffer.clear();
FileReader.close();
}
catch ( const FbException& error )
{
throw Firebird::FbException(status, statusVector);
}
catch (...) {
throw std::runtime_error("Error writing file to BLOB.");
}
}
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_VARCHAR(8191), afilename)
(FB_BLOB, ablob)
);
FB_UDR_MESSAGE(OutMessage,
(FB_BIGINT, result)
);
AutoRelease<IAttachment> att;
AutoRelease<ITransaction> tra;
AutoRelease<IBlob> blob;
ISC_STATUS_ARRAY statusVector = {0};
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!
* This should be done prior to calling this function.
*/
std::ofstream FileWriter ( in->afilename.str , std::ofstream::binary | std::ofstream::out );
if (! FileWriter.is_open()) {
// if (! FileWriter.good()) {
out->resultNull = FB_TRUE;
// Convert system error to a negative value and assign to result
out->result = ( SYSERROR() * -1 );
// important to set out->resultNull to false here otherwise the error
// code willbe hidden and NULL returned to the firebird engine. This
// is because the test for out->resultNull has precedence.
if ( out->result < 0 ) {
out->resultNull = FB_FALSE;
}
return;
}
// This might be useful but doesn't work because it requires libc++ and we compile with libstdc++ on linux
// else {
// std::filesystem::permissions ( in->afilename.str, std::filesystem::perms::owner_all | std::filesystem::perms::group_all,
// std::filesystem::perm_options::add );
// }
att.reset(context->getAttachment(status));
tra.reset(context->getTransaction(status));
blob.reset(att->openBlob(status, tra, &in->ablob, 0, nullptr));
try {
if (blob == nullptr) {
out->resultNull = FB_TRUE;
out->result = -1;
return;
}
std::vector<std::uint8_t> Buffer (MaxSegmentSize, 0);
unsigned BytesRead = 0;
unsigned BufferSize = 0;
for (bool Eof = false; !Eof; )
{
switch ( blob->getSegment ( status, MaxSegmentSize, Buffer.data(), &BytesRead))
{
case IStatus::RESULT_OK:
case IStatus::RESULT_SEGMENT:
{
BufferSize = Buffer.size();
if ( BufferSize < BytesRead) {
BytesRead = BufferSize;
}
if ( BytesRead < MaxSegmentSize) {
Buffer.resize( BytesRead);
}
FileWriter.write( (char *) Buffer.data(), Buffer.size() );
out->result += BytesRead;
continue;
}
default:
{
blob->close( status ); // will close interface
blob->release();
Eof = true;
break;
}
}
}
// If we have got this far then we have written the blob.
// out->resultNULL ***must*** be set to false or else the function will return NULL
out->resultNull = FB_FALSE;
Buffer.clear();
FileWriter.close();
}
catch ( const FbException& error )
{
throw Firebird::FbException(status, statusVector);
}
catch (...) {
throw std::runtime_error("Error writing BLOB to file.");
}
}
FB_UDR_END_FUNCTION
//END
/*** DDL
create function BillDate (
d integer,
date
) returns date
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
*/