Lua の UDSF: 例 2

この例では、ストリングに含まれている固有の文字の数をカウントして結果を返す unique_chars という関数を作成します。

コード

以下のコードを /tmp/udsf_example2.nzl ファイルに書き込みます。

-- count the number of distinct letters in a string
-- USAGE: unique_chars('abc123zzzz')

function unique_chars( str )
      count = 0
      chars = {}

      for ch in string.gmatch(str, "." ) do
        if chars[ch] == null then
          chars[ch] = 1
          count = count + 1
        end
      end

      return count
end

function getType()
      return udf
end

function getName()
      return "unique_chars"
end

関数
      getArgs()
      return {{ "", varchar(any) }}
end

function getResult()
      return "integer"
end

function getComment()
      return "A Sizer Example using UDSF"
end

圧縮

/tmp/udsf_example2.nzl ファイルを /tmp/udsf_example2.tar という名前のファイルに圧縮します。

デプロイメント

以下の CLPPlus コマンドを実行して、UDX を Db2® インスタンスにデプロイします。
SQL> IDA DEPLOYUDX SRC /tmp/udsf_example2.tar LANGUAGE lua PROJECT example2

実行

この関数を照会で使用します。以下に例を示します。
values unique_chars('abc123zzzz')
以下の結果が返されます。
1
-----------
          7