18.4.8.8 PL/SQLでのBOSS RESTデータ・ソースの使用

BOSS RESTデータ・ソースを使用してAPEXリージョンでデータを表示します

フォーム・リージョンや対話グリッド・リージョンがあるビジネス・オブジェクト・エンドポイントに基づいてBOSS RESTデータ・ソースを使用する場合は、データの挿入、更新および削除もできます。これらのどの機能にも、実現するためにコードを記述する必要はありませんが、APEX_EXECパッケージを使用してプログラムでBOSSエンドポイントと対話する必要がある場合があります。次のようなユースケースがあります:

  • BOSSデータをプログラムで再形成するパイプライン・テーブル・ファンクションを作成。
  • APEXのビジネス・ロジック、ワークフロー、自動化またはバックグラウンド実行チェーン・プロセスにおいてBOSSエンドポイントをプログラムで操作。
  • BOSSエンドポイントとの対話を含む任意のスクリプト処理タスク。

各BOSS RESTデータ・ソースには、一意の識別子があり、APEXによってその静的IDがコールされます。たとえば、BOSSエンドポイント../v1/opportunitiesのRESTデータ・ソースを作成しているAPEX開発者はその静的IDをopportunitiesに構成する可能性があります。これは、APEX_EXECパッケージ内のファンクションを操作するときに使用する識別子です。

BOSSエンドポイントからのデータの取得

BOSSエンドポイントからデータを取得するには、apex_exec.open_rest_source_queryファンクションを使用してapex_exec.t_contextオブジェクト(カーソルと同様に機能する)を作成した後、しばらくの間apex_exec.next_row(ctx) loopを使用してその結果を反復処理します。反復ごとに、apex_exec.get_xxx()ファンクションを使用して現在の問合せ結果行から属性値にアクセスします。完了したら、apex_exec.close()をコールしてそのコンテキストを閉じます。たとえば:

declare
  l_ctx apex_exec.t_context;
  -- other declarations here
begin
  -- Process top five winnable opportunties for salesperson 12345 by amount
  -- get_opportunities_columns() returns apex_exec.t_columns for columns
  -- corresponding to fields: optyId,amount,customerName,closeDate
  l_ctx := apex_exec.open_rest_source_query(
             p_static_id              => 'opportunities',
             p_columns                => get_opportunities_columns,
             p_external_filter_expr   => 'resourceId = 12345 and winPct > 0.80',
             p_external_order_by_expr => 'amount:desc'
             p_max_rows               => 5);
  while apex_exec.next_row(l_ctx) loop
     l_amount := apex_exec.get_number(l_ctx,'AMOUNT');
     l_customer := apex_exec.get_varchar2(l_ctx,'CUSTOMERNAME');
     l_closes := apex_exec.get_date(l_ctx,'CLOSEDATE');
     -- Work with data here...
  end loop;
  apex_exec.close(l_ctx);
exception
  when others then
     apex_exec.close(l_ctx);
     raise;
end;

BOSSエンドポイントを使用したデータの挿入

BOSSエンドポイントを使用してデータを挿入するには、apex_exec.open_rest_source_dml_contextファンクションを使用してapex_exec.t_contextオブジェクト(一時行セットと同様に機能する)を作成します。次に、追加する新しい行ごとに、apex_exec.add_dml_row()をコールしてから、apex_exec.set_value()を使用して必要な各属性の値を設定します。最後に、保留中の変更内容を処理するために、apex_exec.execute_dml()をコールします。完了したら、apex_exec.close()をコールしてそのコンテキストを閉じます。たとえば:

declare
  l_ctx apex_exec.t_context;
  -- other declarations here
begin
  -- Insert two new opportunities
  -- get_opportunities_columns() returns apex_exec.t_columns for columns
  -- corresponding to fields: APEX$ResourceKey,optyId,amount,customerName,closeDate
  l_ctx := apex_exec.open_rest_source_dml_context(
             p_static_id             => 'opportunities',
             p_columns               => get_opportunities_columns,
             p_lost_update_detection => apex_exec.c_lost_update_none);
  apex_exec.add_dml_row(l_ctx, apex_exec.c_dml_operation_insert);
  apex_exec.set_value(l_ctx,'AMOUNT',45000);
  apex_exec.set_value(l_ctx,'CUSTOMERNAME','Tesla');
  apex_exec.set_value(l_ctx,'CLOSEDATE','2024-05-11');
  apex_exec.add_dml_row(l_ctx, apex_exec.c_dml_operation_insert);
  apex_exec.set_value(l_ctx,'AMOUNT',1500000);
  apex_exec.set_value(l_ctx,'CUSTOMERNAME','Apple');
  apex_exec.set_value(l_ctx,'CLOSEDATE','2024-06-30');
  -- Save the changes
  apex_exec.execute_dml(l_ctx);
  apex_exec.close(l_ctx);
exception
  when others then
     apex_exec.close(l_ctx);
     raise;
end;

BOSSエンドポイントを使用した既存の行の更新

BOSSエンドポイントを使用してデータを更新するには、apex_exec.open_rest_source_dml_contextファンクションを使用してapex_exec.t_contextオブジェクト(一時行セットと同様に機能する)を作成します。次に、更新する行ごとに、更新操作定数を指定してapex_exec.add_dml_row()をコールしてから、apex_exec.set_value()を使用して必要な各属性の値を設定します。最後に、保留中の変更内容を処理するために、apex_exec.execute_dml()をコールします。多くの場合、まずその行を取得してその既存の値を考慮するクエリを実行してから、それらの一部を更新します。その場合は、apex_exec.set_values()をコールすると、問い合せた行にあるすべての行値を、更新する行にコピーできます。たとえば:

declare
  l_ctx apex_exec.t_context;
  l_dml_ctx apex_exec.t_context;
  -- other declarations here
begin
  -- Retrieve and update opportunity with resource key 98765
  -- to increase its amount by 10% and close date by one week
  -- get_opportunities_columns() returns apex_exec.t_columns for columns
  -- corresponding to fields: optyId,amount,customerName,closeDate
  l_ctx := apex_exec.open_rest_source_query(
            p_static_id               => 'opportunities',
            p_columns                 => get_opportunities_columns,
            p_external_filter_expr    => 'optyId = 98765'
            p_max_rows                => 1);
  if not apex_exec.next_row(l_ctx) then
    raise_application_error(-20001,'Opportunity 98765 not found');
  end if;
  l_dml_ctx := apex_exec.open_rest_source_dml_context(
                 p_static_id             => 'opportunities',
                 p_columns               => get_opportunities_columns,
                 p_lost_update_detection => apex_exec.c_lost_update_implicit);
  apex_exec.add_dml_row(l_ctx, apex_exec.c_dml_operation_update);
  apex_exec.set_row_version_checksum(
    p_context   => l_dml_context,
    p_checksum  => apex_exec.get_row_version_checksum(l_ctx));
  -- Copy the values of the fetched opportunity into the update row
  apex_exec.set_values(
      p_context         => l_dml_ctx,
      p_source_context  => l_ctx );
  -- Adjust the amount and closeDate appropriately
  apex_exec.set_value(l_dml_ctx,'AMOUNT',
                      apex_exec.get_number(l_dml_ctx,'AMOUNT') * 1.1);
  apex_exec.set_value(l_dml_ctx,'CLOSEDATE',
                      apex_exec.get_date(l_dml_ctx,'CLOSEDATE') + 7);
  -- Save the changes
  apex_exec.execute_dml(l_dml_ctx);
  apex_exec.close(l_dml_ctx);
  apex_exec.close(l_ctx);
exception
  when others then
    apex_exec.close(l_dml_ctx);
    apex_exec.close(l_ctx);
      raise;
end;

BOSSエンドポイントを使用した既存の行の削除

BOSSエンドポイントを使用してデータを削除するには、apex_exec.open_rest_source_dml_contextファンクションを使用してapex_exec.t_contextオブジェクト(一時行セットと同様に機能する)を作成します。次に、削除する行ごとに、削除操作定数を指定してapex_exec.add_dml_row()をコールしてから、apex_exec.set_value()を使用してリソース・キー列APEX$RESOURCEKEYの値を設定します。最後に、保留中の変更内容を処理するために、apex_exec.execute_dml()をコールします。たとえば:

declare
  l_dml_ctx apex_exec.t_context;
  -- other declarations here
begin
  -- Delete opportunity with resource key 98765
  -- get_resource_key_columns returns apex_exec.t_columns for columns
  -- corresponding to fields: APEX$ResourceKey
  l_dml_ctx := apex_exec.open_rest_source_dml_context(
                 p_static_id => 'opportunities',
                 p_columns => get_resource_key_columns,
                 p_lost_update_detection => apex_exec.c_lost_update_implicit);
  apex_exec.add_dml_row(l_dml_ctx, apex_exec.c_dml_operation_delete);
  -- Set the resource key of the row to be deleted
  apex_exec.set_value(l_dml_ctx,'APEX$RESOURCEKEY', '98765');
  -- Save the changes
  apex_exec.execute_dml(l_dml_ctx);
  apex_exec.close(l_dml_ctx);
exception
   when others then
     apex_exec.close(l_dml_ctx);
      raise;
end;

ノート:

行の削除を決定する前にまずその属性について考慮するために行をフェッチする必要がある場合は、前述の更新例に従いますが、そのDML行の追加時に更新操作ではなく削除操作を使用します。最後に、保留中の変更内容を処理するために、apex_exec.execute_dml()をコールします。