2011年1月27日木曜日

Apache Derbyで指定されたパスが絶対パスかどうかを返す関数を作成する

Apache Derbyで指定されたパスが絶対パスかどうかを返す関数を作成するには、以下の手順を実行します。

1.以下のクラスをantでコンパイルします。
FsIsAbsoluteFunction.java
package com.serverarekore.derby;
import java.io.*;
import java.sql.*;
import java.net.*;

public class FsIsAbsoluteFunction
{
public static Integer fs_is_absolute(String path)
{
if( path == null )return null;
File file = new File(path);
return new Integer(file.isAbsolute()?1:0);
}
}

build.xmlファイル例
<project name="FsIsAbsoluteFunctions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:\share\derby_funcs\FsIsAbsoluteFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.derbyにjarをインストール
call sqlj.install_jar('c:\share\derby_funcs\FsIsAbsoluteFunction.jar', 'APP.func4', 0);


3.クラスパスに通す
call syscs_util.syscs_set_database_property('derby.database.classpath', 'APP.func4');
※複数のjarの場合は'APP.func1:APP.func2'のように「:」で区切る。

4.関数を作成
create function fs_is_absolute(path varchar(1024)) returns int
parameter style java no sql language java
external name 'com.serverarekore.derby.FsIsAbsoluteFunction.fs_is_absolute';


5.実行
select fs_is_absolute('c:\'), 
fs_is_absolute('.\test.txt') from sysibm.sysdummy1;
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿