180 likes | 472 Views
Firebird / Interbase UDF. What are UDF’s User Defined Functions are DLL’s that perform specialised functions inside the Firebird / Interbase engine. Firebird / Interbase UDF. What are UDF’s
E N D
Firebird / Interbase UDF • What are UDF’s • User Defined Functions are DLL’s that perform specialised functions inside the Firebird / Interbase engine.
Firebird / Interbase UDF • What are UDF’s • User Defined Functions are DLL’s that perform specialised functions inside the Firebird / Interbase engine. • Why do we need UDF’s • The Firebird is very limited in the range of functions available out of the box
Firebird / Interbase UDF • What are UDF’s • User Defined Functions are DLL’s that perform specialised functions inside the Firebird / Interbase engine. • Why do we need UDF’s • The Firebird is very limited in the range of functions available out of the box • Every application area has its own specific requirementse.g. Rounding a date back to a given day in the week Presenting a date as just the month name and year
Firebird / Interbase UDF • What are UDF’s • User Defined Functions are DLL’s that perform specialised functions inside the Firebird / Interbase engine. • Why do we need UDF’s • The Firebird is very limited in the range of functions available out of the box • Every application area has its own specific requirementse.g. Rounding a date back to a given day in the week Presenting a date as just the month name and year • String handling functionality is difficult to achieve in pure SQL
Firebird / Interbase UDF • Criteria for writing UDF’s • Control remains with UDF until completion
Firebird / Interbase UDF • Criteria for writing UDF’s • Control remains with UDF until completion • Should be regarded as a critical section
Firebird / Interbase UDF • Criteria for writing UDF’s • Control remains with UDF until completion • Should be regarded as a critical section • No state is preserved and the code should be thread safe
Firebird / Interbase UDF • Criteria for writing UDF’s • Control remains with UDF until completion • Should be regarded as a critical section • No state is preserved and the code should be thread safe • No database activity should take place as the DLL should run quickly to completion
Firebird / Interbase UDF • Key Issues when creating UDF’s • Most important - function must be exported and declared as cdeclFunction will work if cdecl is omitted, but the stack will be mashed on return from the function (DLL)
Firebird / Interbase UDF • Key Issues when creating UDF’s • Most important - function must be exported and declared as cdeclFunction will work if cdecl is omitted, but the stack will be mashed on return from the function (DLL) • Memory management is the next critical issuePassing integers / reals is the simple case
Firebird / Interbase UDF • Key Issues when creating UDF’s • Most important - function must be exported and declared as cdeclFunction will work if cdecl is omitted, but the stack will be mashed on return from the function (DLL) • Memory management is the next critical issuePassing integers / reals is the simple case • Strings can be passed two ways – • Via the ShareMem unit in the BorlandMM.dll • Via pchar or ShortString parameters
Firebird / Interbase UDF • Key Issues when creating UDF’s • Memory allocation for strings – only needed when returning string resultsib_util_malloc(size_of_memory_needed)
Firebird / Interbase UDF • Key Issues when creating UDF’s • Memory allocation for strings – only needed when returning string resultsib_util_malloc(size_of_memory_needed)Must have ib_util in the uses clause
Firebird / Interbase UDF • Key Issues when creating UDF’s • Timestamps are handled via the TM record structure. This must be defined as a typeTM = record tm_sec : integer; // Seconds tm_min : integer; // Minutes tm_hour : integer; // Hour (0--23) tm_mday : integer; // Day of month (1--31) tm_mon : integer; // Month (0--11) tm_year : integer; // Year (calendar year minus 1900) tm_wday : integer; // Weekday (0--6) Sunday = 0) tm_yday : integer; // Day of year (0--365) tm_isdst : integer; // 0 if daylight savings time is not in effect) end;PTM = ^TM
Firebird / Interbase UDF • Key Issues when creating UDF’s • Timestamps are defined as the typePISC_TIMESTAMPExample of two different function declarations function Date_align(varib_date: PISC_TIMESTAMP; varwk_end, day_no:integer): PISC_TIMESTAMP; cdecl; export; function Date_mn_yr(varib_date: PISC_TIMESTAMP): pchar; cdecl; export; • Needed units in the uses clauseuses ib_util, IBHeader, Sysutils;
Firebird / Interbase UDF • Key Issues when creating UDF’s • Returning result by value or by pointerHow the function returns the result must be declared in the function definition in the actual database where the function is declaredTwo key wordsFREE_IT use when value returned on stack (malloc)BY VALUE use when simple value is returnedcontrol how the stack is cleaned after the execution of the function. Critical to get this right else memory leaks and or exceptions will occur