Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Writing UDFs for Firebird Embedded SQL Server

, , 23 Oct 2009
We will describe how to create your own native Firebird extension and show some approaches how to use it in managed code applications
UDFSample.zip
UDFSample
Firebird
fbembed.dll
firebird.msg
FirebirdSql.Data.FirebirdClient.dll
GDS32.DLL
ib_util.dll
icudt30.dll
icuin30.dll
icuuc30.dll
include
lib
ib_util_ms.lib
MainApp
Properties
SampleUdf
#include <windows.h>
#include <vector>
#include <string>
#include "ibase.h"
#include "ib_util.h"
//------------------------------------------------------------------------------------
typedef void (__stdcall * FCallback)(const char * message);
FCallback g_messageCallback = 0;
FCallback g_errorCallback = 0;
//------------------------------------------------------------------------------------
#define ON_MESSAGE(mess) { if(g_messageCallback) g_messageCallback(mess); }
#define ON_ERROR(mess) { if(g_errorCallback) g_errorCallback(mess); }
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) void RegisterCallbacks(FCallback messageCallback, FCallback errorCallback)
{
	g_messageCallback = messageCallback;
	g_errorCallback = errorCallback;
}
//------------------------------------------------------------------------------------
class SampleParser
{
	std::vector<char> _buffer;
	size_t _pathLen;
	char * _name;
public:
	SampleParser(std::vector<char> & buffer)
	{
		_buffer.swap(buffer);
		char * path = (char*)&_buffer.at(0);
		_pathLen = strlen(path);
		if(_pathLen < 1 ||  _pathLen >= _buffer.size())
			throw std::exception("Invalid buffer format");
		_name = strrchr(path, '\\');
		if(!_name) _name = path;
		else ++_name;
		ON_MESSAGE("--SampleParser created--")
	}
	~SampleParser()
	{
		ON_MESSAGE("--SampleParser destroyed--")
	}

	char * GetName() { return _name; }
	char * GetFullPath() { return (char*)&_buffer.at(0); }
	__int64 * GetCreationTime() { return (__int64*)&_buffer.at(_pathLen + 1); }
	int * GetAttributes() { return (int*)&_buffer.at(_pathLen + 1 + sizeof(__int64)); }
	__int64 * GetSize() { return (__int64*)&_buffer.at(_pathLen + 1 + sizeof(__int64) + sizeof(int)); }

};
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) SampleParser * SampleUdf_CreateParser(BLOBCALLBACK data)
{
	try
	{
		std::vector<char> buffer(data->blob_total_length);
		ISC_UCHAR * p = (ISC_UCHAR*)&buffer.at(0);
		for(ISC_LONG i=0; i < data->blob_number_segments; ++i)
		{
			ISC_USHORT length = 0;
			data->blob_get_segment(data->blob_handle, p, data->blob_max_segment, &length);
			p+= length;
		}
		return new SampleParser(buffer);
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) int SampleUdf_DestroyParser(int * ptr)
{
	try
	{
		SampleParser * self = (SampleParser*)(*ptr);
		delete self;
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) char * SampleUdf_GetName(int * ptr)
{
	try
	{
		SampleParser * self = (SampleParser*)(*ptr);
		return self->GetName();
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) char * SampleUdf_GetFullPath(int * ptr)
{
	try
	{
		SampleParser * self = (SampleParser*)(*ptr);
		return self->GetFullPath();
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) ISC_TIMESTAMP * SampleUdf_GetCreationTime(int * ptr)
{
	try
	{
		SampleParser * self = (SampleParser*)(*ptr);

		FILETIME localTime;
		if(!::FileTimeToLocalFileTime((const FILETIME*)self->GetCreationTime(), &localTime))
			return 0;
		SYSTEMTIME st;
		if(!::FileTimeToSystemTime(&localTime, &st))
			return 0;

		ISC_TIMESTAMP * timeStamp = (ISC_TIMESTAMP*)ib_util_malloc(sizeof(ISC_TIMESTAMP));

		timeStamp->timestamp_time = (st.wHour * 3600000 + st.wMinute * 60000 + st.wSecond * 1000 + st.wMilliseconds) * 10;

		WORD day = st.wDay;
		WORD month = st.wMonth;
		WORD year = st.wYear;

		//some magic calculations from ADO.NET Provider code
		if (month > 2)
			month -= 3;
		else
		{
			month += 9;
			year -= 1;
		}
		WORD c = year / 100;
		WORD ya = year - 100 * c;

		timeStamp->timestamp_date = ((146097 * c) / 4 + (1461 * ya) / 4 + (153 * month + 2) / 5 + day + 1721119 - 2400001);
		return timeStamp;
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) int * SampleUdf_GetAttributes(int * ptr)
{
	try
	{
		SampleParser * self = (SampleParser*)(*ptr);
		return self->GetAttributes();
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) __int64 * SampleUdf_GetSize(int * ptr)
{
	try
	{
		SampleParser * self = (SampleParser*)(*ptr);
		return self->GetSize();
	}
	catch(std::exception & ex)
	{
		ON_ERROR(ex.what());
	}
	catch(...)
	{
		ON_ERROR("Unknown error");
	}
	return 0;
}
//------------------------------------------------------------------------------------
BOOL APIENTRY DllMain( HMODULE hModule,
					  DWORD  ul_reason_for_call,
					  LPVOID lpReserved
					  )
{
	return TRUE;
}
//------------------------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Ivan_Komarov

Ukraine Ukraine
No Biography provided

Apriorit Inc
Apriorit Inc.
Ukraine Ukraine
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.
 
Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.
 
Official site http://www.apriorit.com
Group type: Organisation

31 members

Follow on   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 24 Oct 2009
Article Copyright 2009 by Ivan_Komarov, Apriorit Inc
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid