SODA APIに関連するコール仕様の作成

この章の前半の「SODA for In-Database JavaScriptの開始」の項で、インライン・コール仕様を使用してMLE SODA APIを呼び出す方法の例を示します。次の短い例は、MLEモジュールでSODAを使用する方法を示しています。

Example 8-22 SODA for In-Database JavaScriptの使用

この例で使用されているemployeesCollectionの作成方法の詳細は、例8-8を参照してください。

CREATE OR REPLACE MLE MODULE end_to_end_demo
LANGUAGE JAVASCRIPT AS

/**
 * Example for a private function used to open and return a SodaCollection
 *
 * @param {string} collectionName the name of the collection to open
 * @returns {SodaCollection} the collection handle
 * @throws Error if the collection cannot be opened
 */
function openAndCheckCollection(collectionName){
  
  const col = soda.openCollection(collectionName);
  if(col === null){
    throw new Error(`invalid collection name: ${collectionName}`);
  }

  return col;
}

/**
 * Top-level (public) function demonstrating how to use a QBE to
 * filter documents in a collection.
 *
 * @param {number} departmentId the numeric department ID
 * @returns {number} the number of employees found in departmentId
 */
export function simpleSodaDemo(departmentId){
  
  if(departmentId === undefined || isNaN(departmentId)){
    throw new Error('please provide a valid numeric department ID');
  }

  const col = openAndCheckCollection('employeesCollection');

  const numDocs = col.find()
                     .filter({"departmentId": departmentId})
                     .count();

  return numDocs;
}
/

モジュールの作成後、コール仕様を作成する必要があります。モジュールは単一のパブリック関数を備えているため、スタンドアロン関数で十分です:

CREATE OR REPLACE FUNCTION simple_soda_demo(
  "departmentId" NUMBER
) RETURN NUMBER
AUTHID current_user
AS MLE MODULE end_to_end_demo
SIGNATURE 'simpleSodaDemo';
/

これで、関数を呼び出すためのすべてが整いました:

select simple_soda_demo(30);

結果:

SIMPLE_SODA_DEMO(30)
--------------------
                   6