MLE JavaScript SQLドライバからのPL/SQLの起動
MLE JavaScriptドライバを使用して、PL/SQLからファンクションおよびプロシージャをコールします。
Oracle DatabaseのAPIの大部分は、PL/SQLで提供されます。これは問題ではありません。JavaScriptからPL/SQLを簡単にコールできます。MLE JavaScript SQLドライバを使用したPL/SQLの起動は、SQL文のコールに似ています。
例7-12 JavaScriptからのPL/SQLのコール
CREATE OR REPLACE MLE MODULE plsql_js_mod
LANGUAGE JAVASCRIPT AS
/**
* Read the current values for module and action and return them as
* a JavaScript object. Typically set before processing starts to
* allow you to restore the values if needed.
* @returns an object containing module and action
*/
function preserveModuleAction(){
//Preserve old module and action. DBMS_APPLICATION_INFO provides
// current module and action as OUT binds
let result = session.execute(
`BEGIN
DBMS_APPLICATION_INFO.READ_MODULE(
:l_module,
:l_action
);
END;`,
{
l_module: {
dir: oracledb.BIND_OUT,
type: oracledb.STRING
},
l_action: {
dir: oracledb.BIND_OUT,
type: oracledb.STRING
}
}
);
// Their value can be assigned to JavaScript variables
const currentModule = result.outBinds.l_module;
const currentAction = result.outBinds.l_action;
// ... and returned to the caller
return {
module: currentModule,
action: currentAction
}
}
/**
* Set module and action using DBMS_APPLICATION_INFO
* @param theModule the module name to set
* @param theAction the name of the action to set
*/
function setModuleAction(theModule, theAction){
session.execute(
`BEGIN
DBMS_APPLICATION_INFO.SET_MODULE(
:module,
:action
);
END;`,
[
theModule,
theAction
]
);
}
/**
* The only public function in this module simulates some heavy
* processing for which module and action are set using the built-in
* DBMS_APPLICATION_INFO package.
*/
export function plsqlExample(){
// preserve the values for module and action before we begin
const moduleAction = preserveModuleAction();
// set the new values to reflect the function's execution
// within the module
setModuleAction(
'plsql_js_mod',
'plsqlExample()'
)
// Simulate some intensive processing... While this is ongoing
// module and action in v$session should have changed to the
// values set earlier. You can check using
// SELECT module, action FROM v$session WHERE module = 'plsql_js_mod'
session.execute(
`BEGIN
DBMS_SESSION.SLEEP(60);
END;`
);
// and finally reset the values to what they were before
setModuleAction(
moduleAction.module,
moduleAction.action
);
}
/
この例は、以前の例より少し詳しく説明されており、共通機能を独自の(プライベート)ファンクションに分割しています。OUT
変数の使用は、DBMS_APPLICATION_INFO
へのpreserveModuleAction()
のコールで確認できます。値は、result.outBinds
を使用して取得できます。
モジュールおよびアクションの現在の値をローカル変数に格納した後、追加の匿名PL/SQLブロックが起動され、複雑なデータ処理をシミュレートする60秒のスリープ・サイクルに入る前に、まずモジュールおよびアクションが設定されます。シミュレートされたデータ処理ルーチンが終了すると、名前付きIN
バインド変数を使用して、モジュールとアクションが元の値にリセットされます。バインド変数を使用する方が、文字列連結よりも安全です。
モジュールとアクションを設定することは、進行中のアクティビティをデータベースに通知する有効な手段であり、これにより、パフォーマンス・レポートでアクティビティをグループ化しやすくなります。