JavaScript ESモジュールを23c FREEにインポートし、SQLクエリで使用する方法 (2023/04/28)
JavaScript ESモジュールを23c FREEにインポートし、SQLクエリで使用する方法 (2023/04/28)
投稿者: Loic Lefevre | Principal Product Manager chez Oracle
Oracle Databaseには、21cリリース以降、カーネルに直接組み込まれたJavaScript用のMultiLingual Engine (MLE)があります。MLEでは、Oracle Database内でJavaScriptコードを実行できます。これは、GraalVMによって動作します。
JavaScriptサポートはOracle 21cで最初に導入され、特にApplication Express開発者をターゲットにし、追加のプログラミング言語を使用できます。Oracle 23cでは、JavaScript ESモジュール(ECMAScriptモジュールの場合はESM)の作成を含めるように機能セットが大幅に拡張されました。)
設定
注意: 次のスクリプトは、sqlplusを使用してOracle database 23c FREE - Developer Releaseで実行されます。
まず、Podmanを使用して23c FREE Developer Release Dockerイメージを開始します。
# create a folder where we'll download our ES Modules |
mkdir mle |
# start the container |
podman run -d --name oracle -e ORACLE_PASSWORD=free -p 1521:1521 \ |
--mount=type=bind,src=./mle,dst=/home/oracle/mle,relabel=shared gvenzl/oracle-free:23.2.0-full |
# wait a couple of seconds-minutes depending on whether it's the first time being started
このコマンドは、SYSおよびSYSTEMパスワードを(小文字で)無料として構成し、ローカル・ポート1521を使用してFREEPDB1プラガブル・データベースに接続でき、ローカル・フォルダmleを/home/oracle/mleとしてコンテナにマウントします。
次に、Developerという名前の新しいユーザーを作成し、開始のための適切な権限を付与します。
-- Running these commands as SYS or SYSTEM |
-- sqlplus sys/free@localhost:1521/freepdb1 as sysdba |
-- create the user and grant privileges |
create user developer identified by "free"; |
alter user developer quota unlimited on users; -- no quota issue on USERS tablespace |
grant DB_DEVELOPER_ROLE to developer; -- new role to ease quick setup for developers |
grant execute on javascript to developer; -- allows running JavaScript |
-- create the directory to access filesystem where we'll download our ES Modules |
create directory mle_dir as '/home/oracle/mle'; |
grant read on directory mle_dir to developer;
Chance.js ESモジュールのインストール
この記事の残りの部分では、MIT Licenseの下のVictor QuinnのChance.jsモジュールに焦点を当てます。このモジュールは、「乱数、文字、文字列、名前、アドレス、ダイス、その他ほとんどすべてのものを生成する」多くの方法を提供するため、データを操作する場合、非常に興味深いものです。
書き込み時に使用可能な最新バージョンは1.1.11ですので、データベースで使用できるようにmleフォルダにダウンロードしましょう。
curl -Lo ./mle/chance.js https://cdn.jsdelivr.net/npm/chance@1.1.11/+esm
今度は、開発者ユーザーとして接続し、実際の操作を実行し、(コンテナ内から、またはコンテナ外からSQLclやSQL Developerなどの他のツールを使用して)sqlplusに接続します。
# Run sqlplus from inside the container:
podman exec -it oracle /bin/bash
sqlplus developer/free@localhost:1521/freepdb1
そして開発者として接続したら、次を実行します:
-- prevents variable substitution because of &
SET VERIFY OFF
-- prevents && interpretation by sqlplus
SET DEFINE OFF
-- creates the MLE module named chance_module
create or replace mle module chance_module language javascript version '1.1.11' using bfile( mle_dir, 'chance.js' );
/
-- creates the corresponding MLE environment that will help for managing depencies at runtime
create or replace mle env chance_module_env imports ('chance' module chance_module);
-- creates a new 'extended' module that will export properly the functions for future usage in PL/SQL
create or replace mle module chance_extended language javascript version '1.1.11' as
import Chance from 'chance';
const chance = new Chance();
const chanceTypes = Object.keys(Object.getPrototypeOf(chance));
// inspired from Fony
function valid(type) {
return chanceTypes.indexOf(type) !== -1;
}
function getArrayValue(definition) {
if (definition.length !== 2) {
return null;
}
const type = definition[0];
const count = definition[1];
if (!valid(type) || typeof count !== "number" || count === 0) {
return null;
}
return new Array(count).fill(null).map(function() {
return getValue(type);
});
}
function getValue(type) {
if (Array.isArray(type)) {
return getArrayValue(type);
}
if (typeof type === "object" && !Array.isArray(type) && type != null) {
var result = createData(type);
return result._$_chance ? result._$_chance : result;
}
try {
return chance[type]();
} catch (exception) {
return null;
}
}
function createData(template) {
const output = {};
Object.keys(template).map(function(key, index) {
if (typeof key === "string" && key.charAt(0) === '$' && valid(key.substring(1))) {
output['_$_chance'] = chance[key.substring(1)](template[key]);
} else {
output[key] = getValue(template[key]);
}
});
return output;
}
export function syllable() { return chance.syllable(); }
export function cpf() { return chance.cpf(); }
export function android_id() { return chance.android_id(); }
export function apple_token() { return chance.apple_token(); }
export function bb_pin() { return chance.bb_pin(); }
export function wp7_anid() { return chance.wp7_anid(); }
export function wp8_anid2() { return chance.wp8_anid2(); }
export function company() { return chance.company(); }
export function fbid() { return chance.fbid(); }
export function google_analytics() { return chance.google_analytics(); }
export function hashtag() { return chance.hashtag(); }
export function ip() { return chance.ip(); }
export function ipv6() { return chance.ipv6(); }
export function klout() { return chance.klout(); }
export function tld() { return chance.tld(); }
export function twitter() { return chance.twitter(); }
export function areacode() { return chance.areacode(); }
export function city() { return chance.city(); }
export function postal() { return chance.postal(); }
export function postcode() { return chance.postcode(); }
export function ampm() { return chance.ampm(); }
export function hammertime() { return chance.hammertime(); }
export function millisecond() { return chance.millisecond(); }
export function second() { return chance.second(); }
export function minute() { return chance.minute(); }
export function timestamp() { return chance.timestamp(); }
export function timezone() { return chance.timezone(); }
export function currency() { return chance.currency(); }
export function currency_pair() { return chance.currency_pair(); }
export function exp_year() { return chance.exp_year(); }
export function coin() { return chance.coin(); }
export function d4() { return chance.d4(); }
export function d6() { return chance.d6(); }
export function d8() { return chance.d8(); }
export function d10() { return chance.d10(); }
export function d12() { return chance.d12(); }
export function d20() { return chance.d20(); }
export function d30() { return chance.d30(); }
export function d100() { return chance.d100(); }
export function bool(doc) { return chance.bool(doc); }
export function falsy(doc) { const v = chance.falsy(doc); return v == null ? 'null': v.toString(); }
export function animal(doc) { return chance.animal(doc); }
export function character(doc) { return chance.character(doc); }
export function floating(doc) { return chance.floating(doc); }
export function integer(doc) { return chance.integer(doc); }
export function natural(doc) { return chance.natural(doc); }
export function prime(doc) { return chance.prime(doc); }
export function is_prime(doc) { return chance.is_prime(doc); }
export function hex(doc) { return chance.hex(doc); }
export function letter(doc) { return chance.letter(doc); }
export function string(doc) { return chance.string(doc); }
export function buffer(doc) { return chance.buffer(doc); }
export function capitalize(doc) { return chance.capitalize(doc); }
export function mixin(doc) { return chance.mixin(doc); }
export function n(doc) { return chance.n(doc); }
export function pad(doc) { return chance.pad(doc); }
export function pick(doc) { return chance.pick(doc); }
export function pickone(doc) { return chance.pickone(doc); }
export function pickset(doc) { return chance.pickset(doc); }
export function shuffle(doc) { return chance.shuffle(doc); }
export function paragraph(doc) { return chance.paragraph(doc); }
export function sentence(doc) { return chance.sentence(doc); }
export function word(doc) { return chance.word(doc); }
export function age(doc) { return chance.age(doc); }
export function birthday(doc) { return chance.birthday(doc); }
export function cnpj(doc) { return chance.cnpj(doc); }
export function first(doc) { return chance.first(doc); }
export function profession(doc) { return chance.profession(doc); }
export function gender(doc) { return chance.gender(doc); }
export function last(doc) { return chance.last(doc); }
export function israelId(doc) { return chance.israelId(doc); }
export function mrz(doc) { return chance.mrz(doc); }
export function name(doc) { return chance.name(doc); }
export function prefix(doc) { return chance.prefix(doc); }
export function name_prefix(doc) { return chance.name_prefix(doc); }
export function HIDN(doc) { return chance.HIDN(doc); }
export function ssn(doc) { return chance.ssn(doc); }
export function name_suffixes(doc) { return chance.name_suffixes(doc); }
export function suffix(doc) { return chance.suffix(doc); }
export function name_suffix(doc) { return chance.name_suffix(doc); }
export function nationalities(doc) { return chance.nationalities(doc); }
export function nationality(doc) { return chance.nationality(doc); }
export function zodiac(doc) { return chance.zodiac(doc); }
export function avatar(doc) { return chance.avatar(doc); }
export function color(doc) { return chance.color(doc); }
export function domain(doc) { return chance.domain(doc); }
export function email(doc) { return chance.email(doc); }
export function mac(doc) { return chance.mac(doc); }
export function semver(doc) { return chance.semver(doc); }
export function url(doc) { return chance.url(doc); }
export function port(doc) { return chance.port(doc); }
export function locale(doc) { return chance.locale(doc); }
export function locales(doc) { return chance.locales(doc); }
export function address(doc) { return chance.address(doc); }
export function altitude(doc) { return chance.altitude(doc); }
export function coordinates(doc) { return chance.coordinates(doc); }
export function countries(doc) { return chance.countries(doc); }
export function country(doc) { return chance.country(doc); }
export function depth(doc) { return chance.depth(doc); }
export function geohash(doc) { return chance.geohash(doc); }
export function geojson(doc) { return chance.geojson(doc); }
export function latitude(doc) { return chance.latitude(doc); }
export function longitude(doc) { return chance.longitude(doc); }
export function phone(doc) { return chance.phone(doc); }
export function counties(doc) { return chance.counties(doc); }
export function county(doc) { return chance.county(doc); }
export function province(doc) { return chance.province(doc); }
export function state(doc) { return chance.state(doc); }
export function street(doc) { return chance.street(doc); }
export function zip(doc) { return chance.zip(doc); }
export function date(doc) { return chance.date(doc); }
export function hour(doc) { return chance.hour(doc); }
export function month(doc) { return chance.month(doc); }
export function weekday(doc) { return chance.weekday(doc); }
export function year(doc) { return parseInt(chance.year(doc)); }
export function cc(doc) { return chance.cc(doc); }
export function cc_type(doc) { return chance.cc_type(doc); }
export function currency_types(doc) { return chance.currency_types(doc); }
export function dollar(doc) { return chance.dollar(doc); }
export function euro(doc) { return chance.euro(doc); }
export function exp(doc) { return chance.exp(doc); }
export function exp_month(doc) { return chance.exp_month(doc); }
export function vat(doc) { return chance.vat(doc); }
export function iban(doc) { return chance.iban(doc); }
export function it_vat(doc) { return chance.it_vat(doc); }
export function cf(doc) { return chance.cf(doc); }
export function pl_pesel(doc) { return chance.pl_pesel(doc); }
export function pl_nip(doc) { return chance.pl_nip(doc); }
export function pl_regon(doc) { return chance.pl_regon(doc); }
export function note(doc) { return chance.note(doc); }
export function midi_note(doc) { return chance.midi_note(doc); }
export function chord_quality(doc) { return chance.chord_quality(doc); }
export function chord(doc) { return chance.chord(doc); }
export function tempo(doc) { return chance.tempo(doc); }
export function rpg(doc) { return chance.rpg(doc); }
export function guid(doc) { return chance.guid(doc); }
export function hash(doc) { return chance.hash(doc); }
export function luhn_check(doc) { return chance.luhn_check(doc); }
export function luhn_calculate(doc) { return chance.luhn_calculate(doc); }
export function md5(doc) { return chance.md5(doc); }
export function file(doc) { return chance.file(doc); }
export function fileWithContent(doc) { return chance.fileWithContent(doc); }
export function get(doc) { return chance.get(doc); }
export function mac_address(doc) { return chance.mac_address(doc); }
export function normal(doc) { return chance.normal(doc); }
export function normal_pool(doc) { return chance.normal_pool(doc); }
export function radio(doc) { return chance.radio(doc); }
export function set(doc) { return chance.set(doc); }
export function tv(doc) { return chance.tv(doc); }
export function emotion(doc) { return chance.emotion(doc); }
export function blueimp_md5(doc) { return chance.blueimp_md5(doc); }
export function template(doc) {
return createData(doc);
}
/
これまでは、Chance.jsモジュールと、Oracle Databaseのデータ型(ネイティブJSONおよび新しい23c BOOLEANを含む)と互換性のあるシグネチャを使用して各ファンクションを適切にエクスポートする拡張モジュールをインストールしました。
また、次の設定フェーズに役立つMLE環境も作成しました。PL/SQLファンクションを使用してSQLエンジンをJavaScript MLEと統合し、さらに具体的には、JavaScriptインポートを管理する方法です。
-- Installing a PL/SQL package to make Chance functions accessible in SQL
-- See how the chance_module_env MLE environment is being used so that dependencies resolution goes well
create or replace package chance as
function address( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'address';
function address return varchar2 as mle module chance_extended env chance_module_env signature 'address()';
function age return number as mle module chance_extended env chance_module_env signature 'age()';
function age( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'age';
function altitude return number as mle module chance_extended env chance_module_env signature 'altitude()';
function altitude( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'altitude';
function ampm( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'ampm';
function ampm return varchar2 as mle module chance_extended env chance_module_env signature 'ampm()';
function android_id( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'android_id';
function android_id return varchar2 as mle module chance_extended env chance_module_env signature 'android_id()';
function animal( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'animal';
function animal return varchar2 as mle module chance_extended env chance_module_env signature 'animal()';
function apple_token( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'apple_token';
function apple_token return varchar2 as mle module chance_extended env chance_module_env signature 'apple_token()';
function areacode return varchar2 as mle module chance_extended env chance_module_env signature 'areacode()';
function areacode( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'areacode';
function avatar( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'avatar';
function avatar return varchar2 as mle module chance_extended env chance_module_env signature 'avatar()';
function bb_pin return varchar2 as mle module chance_extended env chance_module_env signature 'bb_pin()';
function bb_pin( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'bb_pin';
function birthday( p_doc in json ) return date as mle module chance_extended env chance_module_env signature 'birthday';
function birthday return date as mle module chance_extended env chance_module_env signature 'birthday()';
function bool( p_doc in json ) return boolean as mle module chance_extended env chance_module_env signature 'bool';
function bool return boolean as mle module chance_extended env chance_module_env signature 'bool()';
function cc return varchar2 as mle module chance_extended env chance_module_env signature 'cc()';
function cc( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'cc';
function cc_type return varchar2 as mle module chance_extended env chance_module_env signature 'cc_type()';
function cc_type( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'cc_type';
function cf( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'cf';
function cf return varchar2 as mle module chance_extended env chance_module_env signature 'cf()';
function character return varchar2 as mle module chance_extended env chance_module_env signature 'character()';
function character( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'character';
function city( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'city';
function city return varchar2 as mle module chance_extended env chance_module_env signature 'city()';
function coin( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'coin';
function coin return varchar2 as mle module chance_extended env chance_module_env signature 'coin()';
function color( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'color';
function color return varchar2 as mle module chance_extended env chance_module_env signature 'color()';
function company return varchar2 as mle module chance_extended env chance_module_env signature 'company()';
function company( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'company';
function country return varchar2 as mle module chance_extended env chance_module_env signature 'country()';
function country( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'country';
function county return varchar2 as mle module chance_extended env chance_module_env signature 'county()';
function county( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'county';
function cpf return varchar2 as mle module chance_extended env chance_module_env signature 'cpf()';
function cpf( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'cpf';
function currency return json as mle module chance_extended env chance_module_env signature 'currency()';
function currency( p_doc in json ) return json as mle module chance_extended env chance_module_env signature 'currency';
function currency_pair( p_doc in json ) return json as mle module chance_extended env chance_module_env signature 'currency_pair';
function currency_pair return json as mle module chance_extended env chance_module_env signature 'currency_pair()';
function d10( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd10';
function d10 return number as mle module chance_extended env chance_module_env signature 'd10()';
function d100 return number as mle module chance_extended env chance_module_env signature 'd100()';
function d100( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd100';
function d12 return number as mle module chance_extended env chance_module_env signature 'd12()';
function d12( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd12';
function d20( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd20';
function d20 return number as mle module chance_extended env chance_module_env signature 'd20()';
function d30( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd30';
function d30 return number as mle module chance_extended env chance_module_env signature 'd30()';
function d4( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd4';
function d4 return number as mle module chance_extended env chance_module_env signature 'd4()';
function d6 return number as mle module chance_extended env chance_module_env signature 'd6()';
function d6( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd6';
function d8 return number as mle module chance_extended env chance_module_env signature 'd8()';
function d8( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'd8';
function "date" return date as mle module chance_extended env chance_module_env signature 'date()';
function "date"( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'date';
function "date"( p_doc in json, p_will_be_date in boolean ) return date as mle module chance_extended env chance_module_env signature 'date';
function depth return number as mle module chance_extended env chance_module_env signature 'depth()';
function depth( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'depth';
function dollar return varchar2 as mle module chance_extended env chance_module_env signature 'dollar()';
function dollar( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'dollar';
function domain( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'domain';
function domain return varchar2 as mle module chance_extended env chance_module_env signature 'domain()';
function email return varchar2 as mle module chance_extended env chance_module_env signature 'email()';
function email( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'email';
function euro return varchar2 as mle module chance_extended env chance_module_env signature 'euro()';
function euro( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'euro';
function exp return varchar2 as mle module chance_extended env chance_module_env signature 'exp()';
function exp( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'exp';
function exp_month( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'exp_month';
function exp_month return varchar2 as mle module chance_extended env chance_module_env signature 'exp_month()';
function exp_year( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'exp_year';
function exp_year return varchar2 as mle module chance_extended env chance_module_env signature 'exp_year()';
function falsy return varchar2 as mle module chance_extended env chance_module_env signature 'falsy()';
function falsy( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'falsy';
function fbid return varchar2 as mle module chance_extended env chance_module_env signature 'fbid()';
function fbid( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'fbid';
function first( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'first';
function first return varchar2 as mle module chance_extended env chance_module_env signature 'first()';
function floating( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'floating';
function floating return number as mle module chance_extended env chance_module_env signature 'floating()';
function gender return varchar2 as mle module chance_extended env chance_module_env signature 'gender()';
function gender( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'gender';
function geohash return varchar2 as mle module chance_extended env chance_module_env signature 'geohash()';
function geohash( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'geohash';
function geojson return json as mle module chance_extended env chance_module_env signature 'geojson()';
function geojson( p_doc in json ) return json as mle module chance_extended env chance_module_env signature 'geojson';
function google_analytics( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'google_analytics';
function google_analytics return varchar2 as mle module chance_extended env chance_module_env signature 'google_analytics()';
function guid( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'guid';
function guid return varchar2 as mle module chance_extended env chance_module_env signature 'guid()';
function hammertime return number as mle module chance_extended env chance_module_env signature 'hammertime()';
function hammertime( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'hammertime';
function hash return varchar2 as mle module chance_extended env chance_module_env signature 'hash()';
function hash( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'hash';
function hashtag( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'hashtag';
function hashtag return varchar2 as mle module chance_extended env chance_module_env signature 'hashtag()';
function hour( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'hour';
function hour return number as mle module chance_extended env chance_module_env signature 'hour()';
function iban return varchar2 as mle module chance_extended env chance_module_env signature 'iban()';
function iban( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'iban';
function "integer" return number as mle module chance_extended env chance_module_env signature 'integer()';
function "integer"( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'integer';
function ip( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'ip';
function ip return varchar2 as mle module chance_extended env chance_module_env signature 'ip()';
function ipv6( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'ipv6';
function ipv6 return varchar2 as mle module chance_extended env chance_module_env signature 'ipv6()';
function klout( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'klout';
function klout return number as mle module chance_extended env chance_module_env signature 'klout()';
function last return varchar2 as mle module chance_extended env chance_module_env signature 'last()';
function last( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'last';
function latitude return number as mle module chance_extended env chance_module_env signature 'latitude()';
function latitude( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'latitude';
function letter( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'letter';
function letter return varchar2 as mle module chance_extended env chance_module_env signature 'letter()';
function locale return varchar2 as mle module chance_extended env chance_module_env signature 'locale()';
function locale( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'locale';
function longitude( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'longitude';
function longitude return number as mle module chance_extended env chance_module_env signature 'longitude()';
function millisecond return number as mle module chance_extended env chance_module_env signature 'millisecond()';
function millisecond( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'millisecond';
function minute( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'minute';
function minute return number as mle module chance_extended env chance_module_env signature 'minute()';
function month( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'month';
function month return varchar2 as mle module chance_extended env chance_module_env signature 'month()';
function name return varchar2 as mle module chance_extended env chance_module_env signature 'name()';
function name( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'name';
function natural( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'natural';
function natural return number as mle module chance_extended env chance_module_env signature 'natural()';
function normal( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'normal';
function normal return number as mle module chance_extended env chance_module_env signature 'normal()';
function paragraph( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'paragraph';
function paragraph return varchar2 as mle module chance_extended env chance_module_env signature 'paragraph()';
function phone return varchar2 as mle module chance_extended env chance_module_env signature 'phone()';
function phone( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'phone';
function postal return varchar2 as mle module chance_extended env chance_module_env signature 'postal()';
function postal( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'postal';
function postcode return varchar2 as mle module chance_extended env chance_module_env signature 'postcode()';
function postcode( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'postcode';
function prefix( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'prefix';
function prefix return varchar2 as mle module chance_extended env chance_module_env signature 'prefix()';
function prime( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'prime';
function prime return number as mle module chance_extended env chance_module_env signature 'prime()';
function profession return varchar2 as mle module chance_extended env chance_module_env signature 'profession()';
function profession( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'profession';
function province( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'province';
function province return varchar2 as mle module chance_extended env chance_module_env signature 'province()';
function radio( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'radio';
function radio return varchar2 as mle module chance_extended env chance_module_env signature 'radio()';
function rpg( p_doc in varchar2 ) return json as mle module chance_extended env chance_module_env signature 'rpg';
function second( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'second';
function second return number as mle module chance_extended env chance_module_env signature 'second()';
function sentence return varchar2 as mle module chance_extended env chance_module_env signature 'sentence()';
function sentence( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'sentence';
function ssn return varchar2 as mle module chance_extended env chance_module_env signature 'ssn()';
function ssn( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'ssn';
function state return varchar2 as mle module chance_extended env chance_module_env signature 'state()';
function state( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'state';
function street( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'street';
function street return varchar2 as mle module chance_extended env chance_module_env signature 'street()';
function string( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'string';
function string return varchar2 as mle module chance_extended env chance_module_env signature 'string()';
function suffix return varchar2 as mle module chance_extended env chance_module_env signature 'suffix()';
function suffix( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'suffix';
function syllable( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'syllable';
function syllable return varchar2 as mle module chance_extended env chance_module_env signature 'syllable()';
function timestamp return number as mle module chance_extended env chance_module_env signature 'timestamp()';
function timestamp( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'timestamp';
function timezone return json as mle module chance_extended env chance_module_env signature 'timezone()';
function timezone( p_doc in json ) return json as mle module chance_extended env chance_module_env signature 'timezone';
function tld return varchar2 as mle module chance_extended env chance_module_env signature 'tld()';
function tld( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'tld';
function tv( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'tv';
function tv return varchar2 as mle module chance_extended env chance_module_env signature 'tv()';
function twitter( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'twitter';
function twitter return varchar2 as mle module chance_extended env chance_module_env signature 'twitter()';
function url( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'url';
function url return varchar2 as mle module chance_extended env chance_module_env signature 'url()';
function word return varchar2 as mle module chance_extended env chance_module_env signature 'word()';
function word( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'word';
function wp7_anid( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'wp7_anid';
function wp7_anid return varchar2 as mle module chance_extended env chance_module_env signature 'wp7_anid()';
function wp8_anid2( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'wp8_anid2';
function wp8_anid2 return varchar2 as mle module chance_extended env chance_module_env signature 'wp8_anid2()';
function year( p_doc in json ) return number as mle module chance_extended env chance_module_env signature 'year';
function year return number as mle module chance_extended env chance_module_env signature 'year()';
function zip( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'zip';
function zip return varchar2 as mle module chance_extended env chance_module_env signature 'zip()';
function zodiac( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'zodiac';
function zodiac return varchar2 as mle module chance_extended env chance_module_env signature 'zodiac()';
function template( p_doc in json ) return json as mle module chance_extended env chance_module_env signature 'template';
end;
/
以上です。
これで、すべての新しい関数を使用してランダム・データを生成できます。次の例を参照してください。
SQLでのChance.js関数の使用
最初の例では、非常に基本的なデータ生成に焦点を当てます。
-- Generate a random string
select chance.string;
STRING
______________
^2nZ*5n*Xhv
-- Generate a random string with 5 chars
select chance.string( json { 'length': 5 } );
CHANCE.STRING(JSON{'LENGTH':5})
__________________________________
@AgM3
複数の行が必要な場合は、次のトリックを利用できます。
-- Generate 7 random strings with 10 chars
select chance.string( json { 'length': 10 } ) connect by level <= 7;
CHANCE.STRING(JSON{'LENGTH':10})
___________________________________
#^lgZ*6r!i
N7alrlEznY
g(2@6A3c4o
NB9M4WXsMk
^uI*jJG1oM
T6bBsYKfFd
bMv]69L]WQ
-- Generate 5 prime numbers (between 0 to 10000)
select chance.prime connect by level <= 5;
PRIME
________
5591
2113
9239
9479
587
-- Generate a random credit card number (Mastercard)
select chance.cc( json { 'type': 'Mastercard' } );
CHANCE.CC(JSON{'TYPE':'MASTERCARD'})
_______________________________________
5199094555584914
-- Generate 2 random dates (which includes time for Oracle database)
-- first one is a DATE, second one is a formatted string (or varchar2)
select chance."date", to_char( chance."date", 'yyyy-mm-dd hh24:mi:ss' );
date TO_CHAR(CHANCE."DATE",'YYYY-MM-DDHH24:MI:SS')
___________ ________________________________________________
15/09/49 2039-01-29 09:33:45
DATEはOracleデータ型であるため、関数と区別するために小文字の呼出しを使用する必要があることに注意してください。このため、二重引用符は必須で、ここでは小文字を使用する必要があります(整数など)。前の例では、ローカライズされたセッション(フランス)のため、最初の日付は2049年9月15日です。
-- Generate a random date as a string (or varchar2)
-- here, the format is the one from Chance: american (28th of January 2024, MM/DD/YYYY)
select chance."date"( json{ 'string': true } );
CHANCE."DATE"(JSON{'STRING':TRUE})
_____________________________________
1/28/2024
-- Generate a random date as a string (or varchar2), but this time with format DD/MM/YYYY
select chance."date"( json{ 'string': true, 'american': false } );
CHANCE."DATE"(JSON{'STRING':TRUE,'AMERICAN':FALSE})
______________________________________________________
15/2/2103
-- Generate a random date with a fixed year
select chance."date"( json{ 'year': 2023 } );
ORA-04156: Unable to convert MLE value to database CHAR value. Reason:
unsupported type for value:2023-03-08T02:45:14.246Z.
ここでは、JavaScriptとPL/SQL間の相互運用性に関するいくつかの制限または柔軟性(視点に応じて)を確認できます。
JSONパラメータを渡すとき、特定のデータ・ジェネレータの拡張生成モードを要求できます。ただし、PL/SQLファンクションのシグネチャは、
PL/SQLが厳密に型指定されているため、互換性があります。したがって、ここではそのケースの3番目の署名があります。
function "date" return date as mle module chance_extended env chance_module_env signature 'date()';
function "date"( p_doc in json ) return varchar2 as mle module chance_extended env chance_module_env signature 'date';
-- This one will help:
function "date"( p_doc in json, p_will_be_date in boolean ) return date as mle module chance_extended env chance_module_env signature 'date';
2番目のパラメータがある場合、結果の型をVARCHAR2ではなくOracleデータベースのDATEにすることができます。
-- Generate a random date with a fixed year
-- Passing true as the second argument (p_will_be_date) so that
-- the returned JavaScript date is properly casted into a DATE
select chance."date"( json{ 'year': 2023 }, true );
CHANCE."DATE"(JSON{'YEAR':2023},TRUE)
________________________________________
21/02/23
-- Generate a random address
select chance.address;
ADDRESS
_________________
66 Bomcu Drive
-- Generate a random timezone
select chance.timezone;
TIMEZONE
__________________________________________________________________________________________________________________________
{"name":"Samoa Standard Time","abbr":"SST","offset":13,"isdst":false,"text":"(UTC+13:00) Samoa","utc":["Pacific/Apia"]}
ランダムなデータを簡単に生成できるようになりました。それはあなた次第ですが、最後の単語です!
SQLでの拡張Chance.js関数の使用
上記の拡張MLEモジュールを見てみると、いくつかの追加コードが表示されている可能性があります。実際、JavaScriptデータ・ジェネレータを検索している間に、[Safia Abdalla](https://github.com/captainsafia)のFony.js (MIT License)が`JSON template`の興味深い概念を提供していることがわかりました。そのため、JSONドキュメントをパラメータとして使用するジェネレータのカスタマイズに関して、Chance.jsが提供する追加の可能性を利用できるように、統合して改善することにしました。その結果、`template()`関数を使用して、JSONテンプレートに基づいてランダムなJSONドキュメントを生成できるようになりました。
次にいくつかの例を示します。
-- Generate a JSON document containing tags as an array of 3 words
select chance.template( json {'tags': ['word', 3]} );
CHANCE.TEMPLATE(JSON{'TAGS':['WORD',3]})
___________________________________________
{"tags":["ehugu","luf","fiksojuh"]}
-- Generate a JSON document containing random information for a person
select chance.template( json { 'name': 'name', 'age': 'age', 'address': 'address' } );
CHANCE.TEMPLATE(JSON{'NAME':'NAME','AGE':'AGE','ADDRESS':'ADDRESS'})
_______________________________________________________________________
{"address":"1904 Luwe Ridge","age":38,"name":"Cory Hopkins"}
JSONフィールドの順序は考慮されません。template()関数が最新のES標準で順序を保証しなくなったObject.keys()関数を利用するため、これは予想されます。しかし、それは本当に問題ではありませんよね。
-- Generate 2 random JSON documents for fictious races
-- We display the JSON document formatted for better readability
select json_serialize( chance.template( json {'raceId': { '$natural': {'min': 1, 'max': 99999} },
'name': 'name',
'laps': { '$natural': {'min': 5, 'max': 42} },
'date': 'date',
'podium': {} } ) pretty ) as new_races
connect by level <= 2;
NEW_RACES
__________________________________________
{
"date" : "2056-11-14T13:26:53.751000Z",
"laps" : 35,
"name" : "Scott Wilkins",
"podium" :
{
},
"raceId" : 64315
}
{
"date" : "2060-10-11T13:18:00.826000Z",
"laps" : 5,
"name" : "Wesley Ramsey",
"podium" :
{
},
"raceId" : 84434
}
これにより、23c FREEドキュメントからJSON Relational Duality Viewsの例を移入するための興味深い機能がもたらされます。
まとめ
JavaScript MultiLingual Engineのおかげで、Oracle database 23c FREE Developer Release内にESモジュールをインポートする方法を確認しました。また、依存関係の解決がMLE環境を使用してどのように機能するかも確認しました。
ご覧のとおり、可能性は無限であり、単純な値とJSONドキュメントの両方に対してそのようなデータ・ジェネレータのメリットが得られます。これは、Oracle Application Express (APEX)アプリケーション、デモ、ユニット・テスト用のランダム・データなどに使用できます。
そして今、あなた次第です!
学習を続けるためのリソース
Jeff SmithがMLE JSモジュールおよびスニペットに関するブログ記事で述べたように、23c JavaScript開発者ガイド(ドキュメント)を読むことを強くお薦めします。
また、ここで使用したコマンドの一部を盗んだOracle Database 23c Free - Developer ReleaseのJavaScriptコミュニティ・モジュールの使用に関するMartin Bachのブログ投稿を確認することもできます。
私たちがモニターする23c無料コミュニティ・フォーラムとフィードバックを提供できる場所に関するフィードバックを忘れないでください。
感謝
Martin BachとLucas Victor Braun-Lohrerが、それぞれOracle LabsのJavaScript MLEとプログラム・マネージャーのプロダクト・マネージャーを務めることなく、ここで強調された可能性を発見することはなかったでしょう。ありがとうございます。
コメント
コメントを投稿