2011年3月12日土曜日

H2 DatabaseでSchemaCrawlerの情報を取得する関数を作成する

H2 DatabaseでSchemaCrawlerの情報を取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists sc_get_scinfo as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import schemacrawler.schema.*;
import schemacrawler.schemacrawler.*;
import schemacrawler.utility.*;
@CODE
ResultSet sc_get_scinfo(String jdbcDriver, String url,
String user, String password)
throws Exception
{
if( jdbcDriver == null ){
throw new Exception("jdbcDriver is not specified.");
}
if( url == null ){
throw new Exception("url is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null ){
throw new Exception("password is not specified.");
}
Class.forName(jdbcDriver);
Connection conn = null;
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 4096, 0);
rs.addColumn("VALUE", Types.VARCHAR, 4096, 0);
try
{
conn = DriverManager.getConnection(url, user, password);
SchemaCrawlerOptions options = new SchemaCrawlerOptions();
Database db = SchemaCrawlerUtility.getDatabase(conn, options);
SchemaCrawlerInfo sci = db.getSchemaCrawlerInfo();
rs.addRow("About", sci.getSchemaCrawlerAbout());
rs.addRow("ProductName", sci.getSchemaCrawlerProductName());
rs.addRow("Version", sci.getSchemaCrawlerVersion());
}
finally
{
if( conn != null )conn.close();
}
return rs;
}
$$

実行例
select * from
sc_get_scinfo('org.postgresql.Driver',
'jdbc:postgresql://localhost:5432/postgres',
'postgres', 'postgres');

※以下のjarをCLASSPATH環境変数に追加
schemacrawler-8.5.1.jar, schemacrawler-postgresql-8.5.1.jar

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

○関連情報
・SchemaCrawlerのウェブサイト
http://schemacrawler.sourceforge.net/
・H2 Databaseに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿