Recital

Login Register

Working with user-defined Functions in the Recital Database Server

In this article Barry Mavin, CEO and Chief Software Architect for Recital, gives details on Working with user-defined Functions in the Recital Database Server.

Overview

User-defined functions (UDFs) are collections of statements written in the Recital 4GL (compatible with Visual FoxPro) stored under a name and saved in a Database. User-defined functions are just-in-time compiled by the Recital database engine. User-defined functions can be used in SQL statements to extend the power and flexibility of the inbuilt functions. Using the Database Administrator in Recital Enterprise Studio, you can easily create, view, modify, and test Stored Procedures, Triggers, and user-defined functions.

Tip
You can also extend the Recital Database Server with C Extension Libraries and use the functions defined within that library also.

Creating and Editing user-defined functions

To create a new User-defined function,  right-click the Procedures node in the Databases tree of the Project Explorer and choose Create. To modify an existing User-defined function select the User-defined function in the Databases Tree in the Project Explorer by double-clicking on it or selecting Modify from the context menu. By convertion we recommend that you name your User-defined functions beginning with "f_xxx_", where xxx is the name of the table that they are associated with.

Testing the user-defined function

To test run the user-defined function, select it in the Databases Tree in the Project Explorer by double-clicking on it. Once the Database Administrator is displayed, click the Run button to run it.

Example

Example: user-defined function "f_order_details_total".
////////////////////////////////////////////////////////////////////////
// example user-defined function
function f_order_details_total(pUnitprice, pQuantity, pDiscount)
    return (pUnitprice + pQuantity + pDiscount) > 0
endfunc
Example: using the user-defined function in a SQL SELECT statement.
////////////////////////////////////////////////////////////////////////
// sample code to use a user-defined function in a SQL SELECT statement
select * from customers where f_order_details_total(Unitprice, Quantity, Discount)

Using user-defined function libraries with the Recital Database Server

You can place all of the user-defined functions associated with a particular table into a procedure library. You then define an Open Trigger for the table that opens up the procedure library whenever the table is accessed. This is a much faster way of using user-defined functions as it reduces the amount of file open/close operations during a query and also simplifies development and maintenance.

By convertion we recommend that you should name the library using the convention "lib_xxx", where xxx is the name of the table that the library is associated with.

Example: procedure library in lib_order_details.
////////////////////////////////////////////////////////////////////////
// example user-defined functions
function f_order_details_total(pUnitprice, pQuantity, pDiscount)
    return (pUnitprice * pQuantity - pDiscount) > 0
endfunc

function f_order_details_diff(pUnitprice, pQuantity, pDiscount, pValue)
    return f_order_details_total(pUnitprice, pQuantity, pDiscount) - pValue
endfunc
Example: Open Trigger in dt_order_details_open.
////////////////////////////////////////////////////////////////////////
// This trigger will open up the procedure library when the table is opened
set procedure to lib_order_details additive
Example: Close Trigger in dt_order_details_close.
////////////////////////////////////////////////////////////////////////
// This trigger will close the procedure library when the table is closed
close procedure lib_order_details
Example: using the user-defined function in a SQL SELECT statement.
////////////////////////////////////////////////////////////////////////
// sample code to use a user-defined function in a SQL SELECT statement
select * from customers where f_order_details_total(Unitprice, Quantity, Discount)

User-defined functions can also be used with any of the Client Drivers that work with the Recital Database Server.

Twitter

Copyright © 2024 Recital Software Inc.

Login

Register

User Registration
or Cancel