MyFirstUDRKit/src/MyFirstUDRKit.cpp

360 lines
7.0 KiB
C++
Raw Normal View History

2023-01-20 13:00:07 +01:00
/*
* 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
*
*/
#define FB_UDR_STATUS_TYPE ::Firebird::ThrowStatusWrapper
2023-01-26 16:35:33 +01:00
//#include <ibase.h>
//#include <firebird/UdrCppEngine.h>
2023-01-24 19:23:36 +01:00
#include "MyFirstUDRKit.h"
2023-01-20 13:00:07 +01:00
#include <cassert>
2023-01-20 19:36:51 +01:00
#include <cstdio>
#include <cstdlib>
2023-01-24 19:23:36 +01:00
#include <iostream>
#include <fstream>
#include <vector>
2023-01-20 13:00:07 +01:00
#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
2023-01-20 19:36:51 +01:00
# include <ctime>
2023-01-20 13:00:07 +01:00
# endif
#endif
#ifdef HAVE_SYS_TIMEB_H
# include <sys/timeb.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#include <limits>
2023-01-24 19:23:36 +01:00
using namespace std;
2023-01-20 13:00:07 +01:00
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 integer,
* adenominator integer
* ) returns double precision
2023-01-25 15:58:51 +01:00
* external name 'MyFirstUDRKit!MFK_div!Divide anumerator by adenominator'
* engine udr;
***/
2023-01-25 15:58:51 +01:00
FB_UDR_BEGIN_FUNCTION(MFK_div)
2023-01-23 15:31:41 +01:00
// Divide integer anumerator 1 by integer adenominator using the stdlib
FB_UDR_MESSAGE(InMessage,
(FB_INTEGER, anumerator)
(FB_INTEGER, adenominator)
);
FB_UDR_MESSAGE(OutMessage,
(FB_DOUBLE, 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 = div(in->anumerator, in->adenominator).quot;
}
else
{
2023-01-24 19:23:36 +01:00
// 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};
FbException::check(isc_exception_integer_divide_by_zero, status, StatusVector);
}
}
}
FB_UDR_END_FUNCTION
2023-01-20 13:00:07 +01:00
/*** DDL
create function flagged (
2023-01-23 13:43:18 +01:00
flags integer,
flag integer
2023-01-20 13:00:07 +01:00
) returns integer
2023-01-25 15:58:51 +01:00
external name 'MyFirstUDRKit!MFK_flagged!How is this function used?'
2023-01-20 13:00:07 +01:00
engine udr;
***/
FB_UDR_BEGIN_FUNCTION(MFK_flagged)
FB_UDR_MESSAGE(InMessage,
(FB_INTEGER, flags)
(FB_INTEGER, flag)
);
FB_UDR_MESSAGE(OutMessage,
(FB_INTEGER, result)
);
FB_UDR_EXECUTE_FUNCTION
{
// Original code
// if ( flags == NULL ) {
// return 0;
// }
2023-01-24 15:59:51 +01:00
if ( in->flagsNull != 0 ) {
out->resultNull = FB_TRUE;
out->result = 0;
}
else
{
out->resultNull = FB_FALSE;
// Original code
// ISC_UINT64 i = ( 1ULL << *flag );
// return ( *flags & i ) ? 1 : 0;
ISC_UINT64 i = ( 1ULL << in->flag );
out->result = ( in->flags & i ) ? 1 : 0;
}
2023-01-20 13:00:07 +01:00
}
FB_UDR_END_FUNCTION
2023-01-24 19:23:36 +01:00
2023-01-20 21:53:23 +01:00
/*** DDL
2023-01-26 16:23:43 +01:00
* create or alter function LoadBlobFromFile (
* afilename varchar(8191),
* ablob BLOB
* ) returns bigint
* external name 'MyFirstUDRKit!MFKLoadBlobFromFile!Load file and save to Blob'
* engine udr;
***/
FB_UDR_BEGIN_FUNCTION (MFK_LoadBlobFromFile)
//BEGIN
2023-01-20 21:53:23 +01:00
FB_UDR_MESSAGE(InMessage,
2023-01-23 16:19:11 +01:00
(FB_CHAR(8191), afilename)
2023-01-26 16:23:43 +01:00
(FB_BLOB, ablob)
2023-01-20 21:53:23 +01:00
);
FB_UDR_MESSAGE(OutMessage,
(FB_BIGINT, result)
);
2023-01-24 19:23:36 +01:00
AutoRelease<IAttachment> att;
AutoRelease<ITransaction> tra;
2023-01-26 16:23:43 +01:00
AutoRelease<IBlob> Blob;
2023-01-24 19:23:36 +01:00
2023-01-20 21:53:23 +01:00
FB_UDR_EXECUTE_FUNCTION
{
2023-01-23 16:20:30 +01:00
// Test Input
2023-01-24 19:23:36 +01:00
if (in->afilenameNull != 0) {
2023-01-23 16:20:30 +01:00
out->resultNull = FB_TRUE;
out->result = 0;
}
2023-01-24 19:23:36 +01:00
ifstream File ( in->afilename.str , ios_base::in | ios_base::binary );
if (! File.is_open()) {
out->result = -2;
return;
}
2023-01-26 16:23:43 +01:00
// This is wrong - the calling application should pass in a blob handle
AutoRelease<IBlob> Blob(att->createBlob(status, tra, &in->ablob, 0, nullptr));
2023-01-24 19:23:36 +01:00
if (Blob == nullptr) {
out->result = -1;
return;
}
vector<char> Buffer (MaxSegmentSize, 0);
streamsize DataSize;
2023-01-26 16:23:43 +01:00
while ( File.good() ) {
2023-01-24 19:23:36 +01:00
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();
2023-01-20 21:53:23 +01:00
}
FB_UDR_END_FUNCTION
2023-01-26 16:35:00 +01:00
//END
2023-01-20 21:53:23 +01:00
2023-01-26 16:35:00 +01:00
/***
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
2023-01-20 19:37:07 +01:00
/*** DDL
create function BillDate (
d integer,
date
) returns date
2023-01-23 13:43:18 +01:00
external name 'my_first_udr_kit!MFK_BillDate!What does BillDate do?'
2023-01-20 19:37:07 +01:00
engine udr;
***/
2023-01-26 16:35:00 +01:00
/*
FB_UDR_BEGIN_FUNCTION(MFK_BillDate)
2023-01-20 19:37:07 +01:00
2023-01-26 16:35:00 +01:00
FB_UDR_MESSAGE(InMessage,
(FB_INTEGER, d)
(FB_INTEGER, date)
);
FB_UDR_MESSAGE(OutMessage,
(FB_DATE, result)
);
FB_UDR_END_FUNCTION
*/