Subject | Wrting my first UDF for Firebird v1.5 |
---|---|
Author | SoftTech |
Post date | 2012-12-17T16:12:24Z |
Greetings All,
I had to create a UDF using Delphi that can be imported into Firebird.
Currently using v1.5 but am studying up on how to move to the latest version
of Firebird.
Using this article as my guide I created my first UDF:
http://www.firebirdsql.org/en/writing-udfs-for-interbase/
The UDF will take a SQL statement with parameters and replace the parameters
with the actual value, and return a SQL statement that can then be used with
EXECUTE STATEMENT.
The UDF works great.
My concern and question is since I'm using v1.5 of Firebird and I did
include IB_Util in my uses clause do I need to use "function
ib_util_malloc(l: integer): pointer; cdecl; external 'ib_util.dll';"?
Or does anyone see anything wrong with my UDF that I should be made aware
of?
Does ib_util.dll need to be in the windows\system directory on the office
server?
Here is my UDF:
DPR Source:
****************************************************
library Softtech;
uses
SysUtils,
Classes,
SQLFunctions in 'SQLFunctions.pas';
{$R *.RES}
exports SQLCondParamRepl;
begin
end.
****************************************************
Unit Source:
****************************************************
{-----------------------------------------------------------------------------
Project Name:
Project Dir : C:\Delphi 5\DLLs\Softtech UDF\
Created : 17-Dec-2012
Unit Name : SQLFunctions
Purpose :
Author : Michael G. Tuttle
History :
-----------------------------------------------------------------------------}
unit SQLFunctions;
interface
//* IB_Util.pas must be in the library path
uses Sysutils, IB_Util;
function SQLCondParamRepl(SQLCommandText: PChar; var AcctID, CaseID, DebtNo:
Integer): PChar; cdecl; export;
implementation
{ Use this in the script editor of Database Workbench to install this
function into Firebird:
declare external function f_SQLCondParamRepl
cString(32760), Integer, Integer, Integer
returns cstring(32760) free_it
entry_point 'SQLCondParamRepl' module_name 'softtech';
Test it with this:
SELECT F_SQLCONDPARAMREPL('SELECT D.LIST_DATE FROM DEBT D WHERE D.ACCT_ID
= :Param1 AND D.CASE_ID = :Param2 AND D.DEBT_NO = :Param3', :V_ACCT_ID,
:V_CASE_ID, :V_DEBT_NO) from RDB$Database
Or from within a stored procedure:
RESULT = F_SQLCONDPARAMREPL('SELECT D.LIST_DATE FROM DEBT D WHERE
D.ACCT_ID = :Param1 AND D.CASE_ID = :Param2 AND D.DEBT_NO = :Param3',
:V_ACCT_ID, :V_CASE_ID, :V_DEBT_NO);
EXECUTE STATEMENT}
function SQLCondParamRepl(SQLCommandText: PChar; var AcctID, CaseID, DebtNo:
Integer): PChar; cdecl; export;
var
CT: String;
begin
CT := String(SQLCommandtext);
CT := StringReplace(CT, ':Param1', IntToStr(AcctID), [rfReplaceAll,
rfIgnoreCase]);
CT := StringReplace(CT, ':Param2', IntToStr(CaseID), [rfReplaceAll,
rfIgnoreCase]);
if DebtNo > 0 then
CT := StringReplace(CT, ':Param3', IntToStr(DebtNo), [rfReplaceAll,
rfIgnoreCase]);
Result := PChar(CT);
end;
end.
****************************************************
I had to create a UDF using Delphi that can be imported into Firebird.
Currently using v1.5 but am studying up on how to move to the latest version
of Firebird.
Using this article as my guide I created my first UDF:
http://www.firebirdsql.org/en/writing-udfs-for-interbase/
The UDF will take a SQL statement with parameters and replace the parameters
with the actual value, and return a SQL statement that can then be used with
EXECUTE STATEMENT.
The UDF works great.
My concern and question is since I'm using v1.5 of Firebird and I did
include IB_Util in my uses clause do I need to use "function
ib_util_malloc(l: integer): pointer; cdecl; external 'ib_util.dll';"?
Or does anyone see anything wrong with my UDF that I should be made aware
of?
Does ib_util.dll need to be in the windows\system directory on the office
server?
Here is my UDF:
DPR Source:
****************************************************
library Softtech;
uses
SysUtils,
Classes,
SQLFunctions in 'SQLFunctions.pas';
{$R *.RES}
exports SQLCondParamRepl;
begin
end.
****************************************************
Unit Source:
****************************************************
{-----------------------------------------------------------------------------
Project Name:
Project Dir : C:\Delphi 5\DLLs\Softtech UDF\
Created : 17-Dec-2012
Unit Name : SQLFunctions
Purpose :
Author : Michael G. Tuttle
History :
-----------------------------------------------------------------------------}
unit SQLFunctions;
interface
//* IB_Util.pas must be in the library path
uses Sysutils, IB_Util;
function SQLCondParamRepl(SQLCommandText: PChar; var AcctID, CaseID, DebtNo:
Integer): PChar; cdecl; export;
implementation
{ Use this in the script editor of Database Workbench to install this
function into Firebird:
declare external function f_SQLCondParamRepl
cString(32760), Integer, Integer, Integer
returns cstring(32760) free_it
entry_point 'SQLCondParamRepl' module_name 'softtech';
Test it with this:
SELECT F_SQLCONDPARAMREPL('SELECT D.LIST_DATE FROM DEBT D WHERE D.ACCT_ID
= :Param1 AND D.CASE_ID = :Param2 AND D.DEBT_NO = :Param3', :V_ACCT_ID,
:V_CASE_ID, :V_DEBT_NO) from RDB$Database
Or from within a stored procedure:
RESULT = F_SQLCONDPARAMREPL('SELECT D.LIST_DATE FROM DEBT D WHERE
D.ACCT_ID = :Param1 AND D.CASE_ID = :Param2 AND D.DEBT_NO = :Param3',
:V_ACCT_ID, :V_CASE_ID, :V_DEBT_NO);
EXECUTE STATEMENT}
function SQLCondParamRepl(SQLCommandText: PChar; var AcctID, CaseID, DebtNo:
Integer): PChar; cdecl; export;
var
CT: String;
begin
CT := String(SQLCommandtext);
CT := StringReplace(CT, ':Param1', IntToStr(AcctID), [rfReplaceAll,
rfIgnoreCase]);
CT := StringReplace(CT, ':Param2', IntToStr(CaseID), [rfReplaceAll,
rfIgnoreCase]);
if DebtNo > 0 then
CT := StringReplace(CT, ':Param3', IntToStr(DebtNo), [rfReplaceAll,
rfIgnoreCase]);
Result := PChar(CT);
end;
end.
****************************************************