2011年2月6日日曜日

H2 DatabaseとJRedisでkey-valueを設定する関数を作成する

H2 DatabaseとJRedisでkey-valueを設定する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_set as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import org.jredis.ri.alphazero.*;
import org.jredis.ri.alphazero.support.*;
@CODE
Integer jredis_set(String host, Integer intPort,
String key, String value)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int port = 6379;
if( intPort != null ){
port = intPort.intValue();
}
if( key == null ){
throw new Exception("key is not specified.");
}
if( value == null ){
throw new Exception("value is not specified.");
}

JRedisClient jrc = new JRedisClient(host, port);
jrc.set(key, value);
return new Integer(0);
}
$$

実行例
select jredis_set('192.168.1.25', 6379, 'key1', 'test')

※以下のjarをCLASSPATH環境変数に追加
jredis-core-all-a.0-SNAPSHOT-jar-with-dependencies.jar

○動作環境
JDK6 Update23, H2 Database 1.3.149 (2011-01-07), JRedis2.0.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

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月5日土曜日

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを削除する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを削除する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_rmdir as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_rmdir(String host, String user,
String password, String path)
throws Exception
{
if( host == null ){
throw new Exception("server is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null )password = "";
if( path == null ){
throw new Exception("path is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
try
{
// サーバに接続
conn = new com.trilead.ssh2.Connection(host);
conn.connect();
if( !conn.authenticateWithPassword(user, password) ){
throw new Exception("authentication failed.");
}

// SFTP client作成
sftpc = new SFTPv3Client(conn);
sftpc.setCharset("UTF-8");

// ディレクトリ削除
sftpc.rmdir(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_rmdir('192.168.1.25', 'user', 'password', 'test_dir')


※以下のjarをCLASSPATH環境変数に追加
orion-ssh2-214.jar

○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07)

○関連情報
H2 Database上でOrion SSH2を使用するユーザー定義関数のまとめ
・H2 Databaseに関する他の記事はこちらを参照してください。

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月3日木曜日

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを作成する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでディレクトリを作成する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_mkdir as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_mkdir(String host, String user,
String password, String path, Integer permission)
throws Exception
{
if( host == null ){
throw new Exception("server is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null )password = "";
if( path == null ){
throw new Exception("path is not specified.");
}
if( permission == null ){
throw new Exception("permission is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
try
{
// サーバに接続
conn = new com.trilead.ssh2.Connection(host);
conn.connect();
if( !conn.authenticateWithPassword(user, password) ){
throw new Exception("authentication failed.");
}

// SFTP client作成
sftpc = new SFTPv3Client(conn);
sftpc.setCharset("UTF-8");

// ディレクトリ作成
sftpc.mkdir(path, permission);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_mkdir('192.168.1.25', 'user', 'password', 'test_dir', 0x777)


※以下のjarをCLASSPATH環境変数に追加
orion-ssh2-214.jar

○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07)

○関連情報
H2 Database上でOrion SSH2を使用するユーザー定義関数のまとめ
・H2 Databaseに関する他の記事はこちらを参照してください。

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年2月1日火曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルのパーミッションを取得する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでファイルのパーミッションを取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_get_permissions as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
String orionssh2_get_permissions(String host, String user,
String password, String path)
throws Exception
{
if( host == null ){
throw new Exception("server is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null )password = "";
if( path == null ){
throw new Exception("path is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
SFTPv3FileAttributes fa = null;
try
{
// サーバに接続
conn = new com.trilead.ssh2.Connection(host);
conn.connect();
if( !conn.authenticateWithPassword(user, password) ){
throw new Exception("authentication failed.");
}

// SFTP client作成
sftpc = new SFTPv3Client(conn);
sftpc.setCharset("UTF-8");

// ファイルの情報を取得
fa = sftpc.lstat(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return fa.getOctalPermissions();
}
$$


実行例
select orionssh2_get_permissions('192.168.1.25', 'user', 'password', './sf.png')


※以下のjarをCLASSPATH環境変数に追加
orion-ssh2-214.jar

○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07)

○関連情報
H2 Database上でOrion SSH2を使用するユーザー定義関数のまとめ
・H2 Databaseに関する他の記事はこちらを参照してください。