ユーザー定義関数のインプリメント

ユーザー定義関数をインプリメントするようにアプリケーション・プログラムをコーディングできます。

手順

汎用プログラミング・インターフェース情報の開始ユーザー定義関数をインプリメントするには、次のようにします。

  1. インプリメンターは、ユーザー定義関数をインプリメントするプログラムをコーディングします。 ここでは、インプリメンターは、以下の外部ユーザー定義関数を C 言語でコーディングし、その関数に C_SALARY という名前を付けるものとします。
    /**********************************************************************
    * This routine accepts an employee serial number and a percent raise. *
    * If the employee is a manager, the raise is not applied.  Otherwise, *
    * the new salary is computed, truncated if it exceeds the employee's  *
    * manager's salary, and then applied to the database.                 *
    **********************************************************************/
     void C_SALARY                         /* main routine               */
     ( char        *employeeSerial         /* in: employee serial no.    */
       decimal     *percentRaise           /* in: percentage raise       */
       decimal     *newSalary,             /* out: employee's new salary */
       short int   *niEmployeeSerial       /* in: indic var, empl ser    */
       short int   *niPercentRaise         /* in: indic var, % raise     */
       short int   *niNewSalary,           /* out: indic var, new salary */
       char        *sqlstate,              /* out: SQLSTATE              */
       char        *fnName,                /* in: family name of function*/
       char        *specificName,          /* in: specific name of func  */
       char        *message                /* out: diagnostic message    */
     )
     {
       EXEC SQL BEGIN DECLARE SECTION;
       char        hvEMPNO-7-;             /* host var for empl serial   */
       decimal     hvSALARY;               /* host var for empl salary   */
       char        hvWORKDEPT-3-;          /* host var for empl dept no. */
       decimal     hvManagerSalary;        /* host var,emp's mgr's salary*/
       EXEC SQL END DECLARE SECTION;
     
       sqlstate = 0;
       memset( message,0,70 );
       /*******************************************************************
       * Copy the employee's serial into a host variable                  *
       *******************************************************************/
       strcpy( hvEMPNO,employeeSerial );
       /*******************************************************************
       * Get the employee's work department and current salary            *
       *******************************************************************/
       EXEC SQL SELECT  WORKDEPT, SALARY
                  INTO :hvWORKDEPT, :hvSALARY
                  FROM  EMP
                 WHERE  EMPNO = :hvEMPNO;
       /*******************************************************************
       * See if the employee is a manager                                 *
       *******************************************************************/
       EXEC SQL SELECT  DEPTNO
                  INTO :hvWORKDEPT
                  FROM  DEPT
                 WHERE  MGRNO = :hvEMPNO;
       /*******************************************************************
       * If the employee is a manager, do not apply the raise             *
       *******************************************************************/
       if( SQLCODE == 0 )
         {
           newSalary = hvSALARY;
         }
       /*******************************************************************
       * Otherwise, compute and apply the raise such that it does not     *
       * exceed the employee's manager's salary                           *
       *******************************************************************/
       else
         {
           /***************************************************************
           * Get the employee's manager's salary                          *
           ***************************************************************/
           EXEC SQL SELECT  SALARY
                      INTO :hvManagerSalary
                      FROM  EMP
                     WHERE  EMPNO = (SELECT MGRNO
                                       FROM DSN8610.DEPT
                                      WHERE DEPTNO = :hvWORKDEPT);
           /***************************************************************
           * Compute proposed raise for the employee                      *
           ***************************************************************/
           newSalary = hvSALARY * (1 + percentRaise/100);
           /***************************************************************
           * Don't let the proposed raise exceed the manager's salary     *
           ***************************************************************/
           if( newSalary > hvManagerSalary
             newSalary = hvManagerSalary;
           /***************************************************************
           * Apply the raise                                              *
           ***************************************************************/
           hvSALARY = newSalary;
           EXEC SQL UPDATE  EMP
                       SET  SALARY = :hvSALARY
                     WHERE  EMPNO  = :hvEMPNO;
         }
     
       return;
    } /* end C_SALARY */

    インプリメンターは、EMP 表に対する UPDATE 特権を持っている必要があります。 関数 C_SALARY に 対する EXECUTE 特権があるユーザーは、この表に対する UPDATE 特権は 必要ありません。

  2. このプログラムには SQL が含まれているので、インプリメンターは以下の ステップを行います。
    1. このユーザー定義関数をインプリメントするプログラムをプリコンパイルする。
    2. DSNRLI (RRS 接続機能) を使用してこのユーザー定義関数をリンク・エディットし、プログラムのロード・モジュールに C_SALARY という名前を付ける。
    3. DBRM をバインドしてパッケージ MYCOLLID.C_SALARY に入れる。

    上記のステップを終えると、インプリメンターは関数パッケージ所有者 となります。

  3. 次に、インプリメンターは、定義者にユーザー定義関数パッケージに対する EXECUTE 特権を付与します。
    GRANT EXECUTE ON PACKAGE MYCOLLID.C_SALARY
     TO definer

    パッケージ所有者であるインプリメンターは、他のユーザーに EXECUTE 特権を付与することができ、付与されたユーザーはパッケージ内のコードを実行することができます。 例えば、以下のようになります。

    GRANT EXECUTE ON PACKAGE MYCOLLID.C_SALARY
          TO other_user
    汎用プログラミング・インターフェース情報の終了