ラベル ApacheDerby の投稿を表示しています。 すべての投稿を表示
ラベル ApacheDerby の投稿を表示しています。 すべての投稿を表示

2011年2月6日日曜日

Apache Derbyで指定されたパスのファイルを列挙する関数を作成する

Apache Derbyで指定されたパスのファイルを列挙する関数を作成するには、以下の手順を実行します。

1.以下のクラスをantでコンパイルします。
FsListFunction.java
package com.serverarekore.derby;
import java.io.*;
import java.sql.*;
import java.net.*;
// SimpleResultSetのため
import org.h2.tools.*;

public class FsListFunction
{
public static ResultSet fs_list(String path)
{
if( path == null )return null;
File file = new File(path);
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 1024, 0);
String files[] = file.list();
for(int fi=0;fi<files.length;fi++){
rs.addRow(files[fi]);
}

return rs;
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsListFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


※libディレクトリにH2 DatabaseのSimpleResultSetクラスを使用するために
h2-1.3.149.jarをコピーしておきます。

2.derbyにjarをインストール
call sqlj.install_jar('c:\share\derby_funcs\h2-1.3.149.jar', 'APP.func9', 0);
call sqlj.install_jar('c:\share\derby_funcs\FsListFunction.jar', 'APP.func10', 0);

3.クラスパスに通す
call syscs_util.syscs_set_database_property('derby.database.classpath', 
'APP.func9:APP.func10');

4.関数を作成
CREATE FUNCTION fs_list(path varchar(1024))
RETURNS TABLE
(
NAME VARCHAR(1024)
)
LANGUAGE JAVA
PARAMETER STYLE DERBY_JDBC_RESULT_SET
READS SQL DATA
EXTERNAL NAME 'com.serverarekore.derby.FsListFunction.fs_list';

5.実行
select * from table( fs_list('c:\') ) files;

○動作環境
Apache Derby 10.7.1.1, JDK6 Update23, h2-1.3.149
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。

2011年2月4日金曜日

Apache Derbyで指定されたファイルのサイズを返す関数を作成する

Apache Derbyで指定されたファイルのサイズを返す関数を作成するには、以下の手順を実行します。

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

public class FsLengthFunction
{
public static Long fs_length(String path)
{
if( path == null )return null;
File file = new File(path);
return new Long(file.length());
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsLengthFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


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

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


5.実行
select fs_length('c:\share\test.txt') from sysibm.sysdummy1;


○動作環境
Apache Derby 10.7.1.1, JDK6 Update23
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。

2011年2月2日水曜日

Apache Derbyで指定されたパスの最終更新日時を返す関数を作成する

Apache Derbyで指定されたパスの最終更新日時を返す関数を作成するには、以下の手順を実行します。

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

public class FsLastModifiedFunction
{
public static Timestamp fs_last_modified(String path)
{
if( path == null )return null;
File file = new File(path);
return new Timestamp(file.lastModified());
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsLastModifiedFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


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

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


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

2011年1月31日月曜日

Apache Derbyで指定されたパスの親ディレクトリを返す関数を作成する

Apache Derbyで指定されたパスの親ディレクトリを返す関数を作成するには、以下の手順を実行します。

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

public class FsGetParentFunction
{
public static String fs_get_parent(String path)
{
if( path == null )return null;
File file = new File(path);
return file.getParent();
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsGetParentFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


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

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


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

2011年1月29日土曜日

Apache Derbyで指定されたパスがディレクトリかどうかを返す関数を作成する

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

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

public class FsIsDirectoryFunction
{
public static Integer fs_is_directory(String path)
{
if( path == null )return null;
File file = new File(path);
return new Integer(file.isDirectory()?1:0);
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsIsDirectoryFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


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

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


5.実行
select fs_is_directory('c:\windows'), 
fs_is_directory('c:\windows\windows.ini') from sysibm.sysdummy1;


○動作環境
Apache Derby 10.7.1.1, JDK6 Update23
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。

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に関する他の記事はこちらを参照してください。

2011年1月25日火曜日

Apache Derbyで指定されたパスのパーティション/ドライブ容量を返す関数を作成する

Apache Derbyで指定されたパスのパーティション/ドライブ容量を返す関数を作成するには、以下の手順を実行します。

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

public class FsGetTotalSpaceFunction
{
public static Long fs_get_total_space(String path)
{
if( path == null )return null;
File file = new File(path);
return new Long(file.getTotalSpace());
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsGetTotalSpaceFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


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

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


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

2011年1月23日日曜日

Apache Derbyで指定された時間スリープする関数を作成する

Apache Derbyで指定された時間スリープする関数を作成するには、以下の手順を実行します。

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

public class SleepFunction
{
public static Long sleep(Long millis)
throws Exception
{
Thread.sleep(millis);
return millis;
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\SleepFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


3.クラスパスに通す
call syscs_util.syscs_set_database_property('derby.database.classpath', 'APP.func2');

※複数のjarの場合は'APP.func1:APP.func2'のように「:」で区切る。
4.関数を作成
create function sleep(millis bigint) returns bigint
parameter style java no sql language java
external name 'com.serverarekore.derby.SleepFunction.sleep';


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

2011年1月21日金曜日

Apache Derbyでドライブの空き容量を返す関数を作成する

Apache Derbyでドライブの空き容量を返す関数を作成するには、以下の手順を実行します。

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

public class FsGetFreeSpaceFunction
{
public static Long fs_get_free_space(String path)
{
if( path == null )return null;
File file = new File(path);
return new Long(file.getFreeSpace());
}
}

build.xmlファイル例
<project name="DerbyFunctions" 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\FsGetFreeSpaceFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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


3.クラスパスに通す
call syscs_util.syscs_set_database_property('derby.database.classpath', 
'APP.func1');


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


5.実行
select fs_get_free_space('c:\') from sysibm.sysdummy1;


○動作環境
Apache Derby 10.7.1.1, JDK6 Update23
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。