Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / SQL

Writing UDFs for Firebird Embedded SQL Server

,
Rate me:
Please Sign up or sign in to vote.
4.92/5 (19 votes)
23 Oct 2009CPOL4 min read 40K   698   29  
We will describe how to create your own native Firebird extension and show some approaches how to use it in managed code applications
DECLARE EXTERNAL FUNCTION CreateParser
	BLOB
    RETURNS INTEGER BY VALUE
  ENTRY_POINT 'SampleUdf_CreateParser'  MODULE_NAME 'SampleUdf'
#
DECLARE EXTERNAL FUNCTION DestroyParser
	INTEGER
	RETURNS INTEGER BY VALUE
  ENTRY_POINT 'SampleUdf_DestroyParser'  MODULE_NAME 'SampleUdf'
#
DECLARE EXTERNAL FUNCTION GetName
	INTEGER
	RETURNS CSTRING(256)
  ENTRY_POINT 'SampleUdf_GetName'  MODULE_NAME 'SampleUdf'
#
DECLARE EXTERNAL FUNCTION GetFullPath
	INTEGER
	RETURNS CSTRING(256)
  ENTRY_POINT 'SampleUdf_GetFullPath'  MODULE_NAME 'SampleUdf'
#
DECLARE EXTERNAL FUNCTION GetCreationTime
	INTEGER
	RETURNS TIMESTAMP FREE_IT
  ENTRY_POINT 'SampleUdf_GetCreationTime'  MODULE_NAME 'SampleUdf'
#
DECLARE EXTERNAL FUNCTION GetAttributes
	INTEGER
	RETURNS INTEGER
  ENTRY_POINT 'SampleUdf_GetAttributes'  MODULE_NAME 'SampleUdf'
#
DECLARE EXTERNAL FUNCTION GetSize
	INTEGER
	RETURNS BIGINT
  ENTRY_POINT 'SampleUdf_GetSize'  MODULE_NAME 'SampleUdf'
#
CREATE TABLE RowDataTable (
	Id INTEGER NOT NULL PRIMARY KEY
  , Data BLOB
  )
#
CREATE TABLE FSTable
 (
	Id INTEGER NOT NULL PRIMARY KEY
  , FileName VARCHAR(256)
  , FullPath VARCHAR(256)
  , CreationTime TIMESTAMP 
  , Attributes INTEGER
  , FileSize BIGINT
  )
#
CREATE GENERATOR RowDataTable_Generator
#
CREATE GENERATOR FSTable_Generator
#
CREATE PROCEDURE AddRawData
(
  Data BLOB
)
RETURNS (newId INTEGER)
AS
BEGIN
    newId = GEN_ID(RowDataTable_Generator, 1);
    INSERT INTO RowDataTable (Id, Data) VALUES (:newId, :Data);
    SUSPEND;
END
#
CREATE PROCEDURE TransferData
RETURNS (counter INTEGER)
AS
DECLARE VARIABLE tmp INTEGER;
DECLARE VARIABLE parserPtr INTEGER;
DECLARE VARIABLE Data BLOB;
DECLARE VARIABLE FileName VARCHAR(256);
DECLARE VARIABLE FullPath VARCHAR(256);
DECLARE VARIABLE CreationTime TIMESTAMP;
DECLARE VARIABLE Attributes INTEGER;
DECLARE VARIABLE FileSize BIGINT;
BEGIN
	counter = 0;
	FOR SELECT Data FROM RowDataTable INTO :Data DO	BEGIN
		parserPtr = CreateParser(:Data);
		IF (parserPtr IS NOT NULL) THEN BEGIN
			FileName = GetName(:parserPtr);
			FullPath = GetFullPath(:parserPtr);
			CreationTime = GetCreationTime(:parserPtr);
			Attributes = GetAttributes(:parserPtr);
			FileSize = GetSize(:parserPtr);
			
			tmp = GEN_ID(FSTable_Generator, 1);
			INSERT INTO FSTable (Id, FileName, FullPath, CreationTime, Attributes, FileSize)
			VALUES (:tmp, :FileName, :FullPath, :CreationTime, :Attributes, :FileSize);
			counter = counter + 1;

			tmp = DestroyParser(:parserPtr);
		END
	END
	SUSPEND;
END

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)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions