1 / 16

Firebird / Interbase UDF

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

fathia
Download Presentation

Firebird / Interbase UDF

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Firebird / Interbase UDF • What are UDF’s • User Defined Functions are DLL’s that perform specialised functions inside the Firebird / Interbase engine.

  2. 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

  3. 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

  4. 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

  5. Firebird / Interbase UDF • Criteria for writing UDF’s • Control remains with UDF until completion

  6. Firebird / Interbase UDF • Criteria for writing UDF’s • Control remains with UDF until completion • Should be regarded as a critical section

  7. 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

  8. 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

  9. 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)

  10. 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

  11. 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

  12. 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)

  13. 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

  14. 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

  15. 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;

  16. 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

More Related