MyFirstUDRKit/src/MyFirstUDRKit.cpp
2023-01-25 09:54:09 +01:00

273 lines
5.4 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
*
*/
#define FB_UDR_STATUS_TYPE ::Firebird::ThrowStatusWrapper
#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>
#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>
using namespace std;
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
* external name 'udf_compat!UC_div!Divide anumerator by adenominator'
* engine udr;
***/
FB_UDR_BEGIN_FUNCTION(UC_div)
// 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
{
// 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
/*** DDL
create function flagged (
flags integer,
flag integer
) returns integer
external name 'my_first_udr_kit!MFK_flagged!How is this function used?'
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;
// }
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;
}
}
FB_UDR_END_FUNCTION
/*** DDL
create or alter function LoadBlobFromFile (
afilename varchar(8191),
ablob BLOB
) returns bigint
external name 'my_first_udr_kit!MFK_Load_Blob_From_File!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_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) {
out->resultNull = FB_TRUE;
out->result = 0;
}
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();
}
FB_UDR_END_FUNCTION
/*** 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