How to Create Inline JavaScript Functions in Oracle

Write short JavaScript functions directly in CREATE FUNCTION with MLE, call them from SQL, and know when a full module is the better choice.

For a short piece of JavaScript, a whole MLE module and a separate call specification are more than you need. An inline MLE function puts the JavaScript body directly inside CREATE FUNCTION, between double braces, and is called from SQL and PL/SQL like any other function.

Code for This Guide

The main examples are in the examples/schema-objects folder of the Oracle Database 26ai code repository on GitHub, each with its output. They use NIMBUS, the sample schema of a fictional airline, which you can install with the scripts in the setup/nimbus folder.

They come from Oracle Database 26ai SQL and PL/SQL Book.

Syntax

create [or replace] function name (params) return type
  as mle language javascript
{{
  JavaScript statements, ending with return
}};
/

The PL/SQL parameters are available as JavaScript variables with their names in uppercase, such as P_TEXT, unless they were declared in double quotes.

Turn Names into URL Slugs

The function below normalizes accented characters, lowercases the text, and replaces everything else with hyphens, a task that regular expressions in JavaScript handle compactly.

Example:

create function slugify (p_text varchar2) return varchar2
  as mle language javascript
{{
  // the parameter P_TEXT is a JavaScript variable, named in uppercase
  return P_TEXT.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
               .toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
}};
/
select city, slugify(airport_name) as slug
from   airports
where  airport_code in ('GRU', 'ORD');

Output:

Function SLUGIFY compiled

CITY         SLUG
____________ ____________________________________
São Paulo    sao-paulo-guarulhos-international
Chicago      chicago-o-hare-international

São Paulo Guarulhos International becomes sao-paulo-guarulhos-international: normalize('NFD') separates the accent from the letter, and the first replace removes it. The example drops the function afterward.

Inline Functions Compared with Modules

Inline functionMLE module
Code lives inThe function itselfA separate module object
Shares codeNoYes, several exported functions and imports
Best forShort, self-contained functionsLibraries and larger code

Things to Know

  • Inline functions need the same privileges as modules: CREATE MLE and EXECUTE ON JAVASCRIPT.
  • Use uppercase parameter names in the JavaScript, or quote them in the declaration to keep their case.
  • Errors in the JavaScript appear when the function is called, so test it right after creating it.

Related Guides

Conclusion

Inline MLE functions put JavaScript directly in CREATE FUNCTION, with the PL/SQL parameters as uppercase JavaScript variables. Use them for short functions where JavaScript is the natural language, and modules when code is shared.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted