How to create a user-defined function (UDF) to call an inline function or a JavaScript function.
Introduction
If you want to try out the examples in this section, follow the instructions given in Do a Quick Install to install Couchbase Server, configure a cluster, and load a sample dataset. Read the following for further information about the tools available for editing and executing queries:
Global and Scoped User-Defined Functions
A user-defined function can be global or scoped.
-
A global user-defined function is created within the
default:namespace, at the same level as the buckets in your database. A global user-defined function is available to all users. -
A scoped user-defined function is created within a scope, at the same level as the collections within the scope. A scoped user-defined function is only available to users who have access to that bucket and scope.
The name of a user-defined function must be unique within the specified namespace or scope.
User-Defined Function Parameters
When you create a user-defined function, you can specify a list of parameters for any values you need to process or use in your function. If you want to create a user-defined function that can take a variable length list of parameters, you can create a variadic function.
Creating an Inline User-Defined Function
To create a user-defined function that uses inline SQL++, use the Couchbase Web Console or a SQL++ statement.
-
Couchbase Web Console
-
SQL++
To create an inline user-defined function:
-
In the Couchbase Web Console, go to .
-
Under User-Defined Functions, click the + add function link. The Add Function dialog is displayed.
-
In the Namespace drop-down list, select (global) for a global function, or select a bucket and scope for a scoped function.
-
In the Function Name box, enter a name for the user-defined function.
-
In the Parameters box, enter a list of parameters separated by commas (
,) or specify...for a variadic function. -
In the Function Type drop-down list, select Inline.
-
In the Expression box, enter a SQL++ expression as the body of the function.
-
If you specified named parameters for the user-defined function, use the same named parameters as identifiers in the SQL++ expression.
-
If the user-defined function is variadic, any arguments are passed to the SQL++ expression in an array called
args.
-
-
Click Save Function.
To create an inline user-defined function:
-
If required, set the query context for a scoped function, or unset the context for a global function.
-
Use the
CREATE FUNCTIONstatement and specify a name for the function. -
Specify a list of parameter names separated by commas (
,) or specify...for a variadic function. -
Use the braces syntax
{}or theLANGUAGE INLINE ASclause to specify a SQL++ expression as the body of the function.-
If you specified named parameters for the user-defined function, use the same named parameters as identifiers in the SQL++ expression.
-
If the user-defined function is variadic, any arguments are passed to the SQL++ expression in an array called
args.
-
The following query creates an inline SQL++ function in the current query context, using the braces syntax.
CREATE FUNCTION rstr(vString, vLen) { SUBSTR(vString, LENGTH(vString) - vLen, vLen) };
The following query creates an inline SQL++ function in the current query context, using the LANGUAGE INLINE syntax.
CREATE FUNCTION lstr(vString, vLen) LANGUAGE INLINE AS SUBSTR(vString, 0, vLen);
For more information, see CREATE FUNCTION.
Creating a User-Defined Function with SQL++ Managed JavaScript
To create a user-defined function that uses SQL++ managed JavaScript, you must use a SQL++ statement.
-
SQL++
To create a user-defined function with SQL++ managed JavaScript:
-
If required, set the query context for a scoped function, or unset the context for a global function.
-
Use the
CREATE FUNCTIONstatement and specify a name for the function. -
Specify a list of parameter names separated by commas (
,) or specify...for a variadic function. -
Use the
LANGUAGE JAVASCRIPT ASclause to define a JavaScript function. The JavaScript function must have the same name as the SQL++ user-defined function.-
If the user-defined function has named parameters, specify the same number of parameters for the JavaScript function.
-
If the user-defined function is variadic, specify a rest parameter for the JavaScript function, such as
... args.
-
The following query creates a SQL++ managed JavaScript function in the current query context.
CREATE FUNCTION add100(num) LANGUAGE JAVASCRIPT AS
"function add100(param1) {return param1+100;}";
For more information, see CREATE FUNCTION.
Creating a User-Defined Function with a JavaScript Library
If you have created a JavaScript function in a JavaScript library (see Create a JavaScript Library), you must create a SQL++ user-defined function to reference it.
If the JavaScript library is scoped, create a scoped user-defined function in the same scope as the library.
To create a user-defined function that references a JavaScript library, use the Couchbase Web Console or a SQL++ statement.
-
Couchbase Web Console
-
SQL++
To create a user-defined function that references a JavaScript library:
-
In the Couchbase Web Console, go to .
-
Under User-Defined Functions, click the + add function link. The Add Function dialog is displayed.
-
In the Namespace drop-down list, select (global) for a global function, or select a bucket and scope for a scoped function.
-
In the Function Name box, enter a name for the user-defined function.
-
In the Parameters box, enter a list of parameters separated by commas (
,) or specify...for a variadic function. -
In the Function Type drop-down list, select JavaScript.
-
In the JavaScript Library field, select the library which contains the JavaScript function.
-
In the Library Function Name field, enter the name of the JavaScript function.
-
If the user-defined function has named parameters, the JavaScript function should accept the same number of parameters.
-
If the user-defined function is variadic, the JavaScript function should accept a rest parameter, such as
... args.
-
-
Click Save Function.
To create a user-defined function that references a JavaScript library:
-
If required, set the query context for a scoped function, or unset the context for a global function.
-
Use the
CREATE FUNCTIONstatement and specify a name for the function. -
Specify a list of parameter names separated by commas (
,) or specify...for a variadic function. -
Use the
LANGUAGE JAVASCRIPT ASclause to specify the name of the JavaScript function.-
If the user-defined function has named parameters, the JavaScript function should accept the same number of parameters.
-
If the user-defined function is variadic, the JavaScript function should accept a rest parameter, such as
... args.
-
-
Use the
ATkeyword to specify the library which contains the JavaScript function.
The following query creates a user-defined function within the current query context that references a global JavaScript library.
CREATE FUNCTION GetBusinessDays(startDate, endDate)
LANGUAGE JAVASCRIPT as "getBusinessDays"
AT "my-library"; (1)
The following query creates a user-defined function within the current query context that references a scoped JavaScript library in the same query context.
CREATE FUNCTION GetBusinessDays(startDate, endDate)
LANGUAGE JAVASCRIPT as "getBusinessDays"
AT "./my-library"; (1)
For more information, see CREATE FUNCTION.
Related Links
Reference:
-
User-Defined Functions — using user-defined functions (UDFs) in SQL++ statements
Administrator guides: