How to extend the SQL++ query language by adding your own user-defined functions.
Introduction
SQL++ includes built-in operations and functions for data manipulation. User-defined functions enable you to create your own extensions to the language.
With user-defined functions, you can:
-
Create reusable, domain-specific functions for use in your applications.
-
Execute complex logic that may be difficult to do in SQL++.
-
Migrate from Relational Database Management System (RDBMS) stored procedures.
User-Defined Functions in Couchbase Server
User-defined functions can be categorized based on the language used to define them, and the location where the function definitions are stored.
| Type | Language | Stored |
|---|---|---|
SQL++ |
Internally |
|
JavaScript |
Internally |
|
JavaScript |
In JavaScript libraries |
User-defined functions written in JavaScript support the ECMAScript standard, with some restrictions and extensions. For more information, see JavaScript Functions for Query Reference.
After you create your user-defined functions, you can call them like any other SQL++ function. For more information, see Call a User-Defined Function.
Inline SQL++ Functions
An inline SQL++ function is a user-defined function that executes a SQL++ expression. The SQL++ expression is stored internally by the Query Service.
For more information, see Create an Inline User-Defined Function.
SQL++ Managed JavaScript Functions
A SQL++ managed JavaScript function is a user-defined function that executes a JavaScript function. The JavaScript function is stored internally by the Query Service. You do not need to create a JavaScript library to create and use SQL++ managed JavaScript functions.
If you create a user-defined function this way, you cannot group related functions or change access restrictions for multiple related functions at once.
For more information, see Creating a User-Defined Function with SQL++ Managed JavaScript.
JavaScript Library Functions
A JavaScript library function is a user-defined function that executes a JavaScript function. The JavaScript function is stored in a JavaScript library.
A JavaScript library is a collection of JavaScript functions. JavaScript libraries keep your JavaScript functions organized and allow you to set access controls across multiple functions at once.
To create a JavaScript library function, you must first create a JavaScript library and add JavaScript functions to that library. For more information, see Create a JavaScript Library.
After you create a JavaScript library, you must create user-defined functions to use the JavaScript functions in that library. The user-defined function creates a link between the JavaScript function in your library and SQL++, letting you call your JavaScript code. For more information, see Creating a User-Defined Function with a JavaScript Library.