/*
 * $Id: MySQLDriverTest.java,v 1.2 2005/10/26 16:24:38 rampil Exp $
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 *
 */
package org.logical_paradox.rss.rcm.accessor.mysql;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.logical_paradox.common.io.FileUtils;
import org.logical_paradox.rss.rcm.df.DataFilterFactory;

/**
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.2 $
 */
public class MySQLDriverTest {
	public static final String openstr = "jdbc:mysql://localhost/rssdev?user=rss_ap&password=rss_ap";
	public static final String filename = "./rcm/contentsid.xls";
	public static final void main(String[] args) throws Exception {
		// read test file
		File file = new File(filename);
		byte[] contents = FileUtils.binaryReadFully(file);

		Class.forName("com.mysql.jdbc.Driver").newInstance();
		Connection con = DriverManager.getConnection(openstr);

		byte[] barray = DataFilterFactory.getFilter(DataFilterFactory.GZIP).compressContents(contents);
		ByteArrayInputStream bi = new ByteArrayInputStream(barray);
		PreparedStatement st = con.prepareStatement("insert into contents values(null,?,?)");
		st.setString(1, "http://www.google.com/");
		st.setBinaryStream(2, bi, barray.length);
		st.executeUpdate();

		st.close();

		// 読み込み
		Statement statement = con.createStatement();
		ResultSet rs = statement.executeQuery("select contents from contents limit 0,1");		
		if(rs.next()) {
			Blob blob = rs.getBlob("contents");
			System.out.println("compressed size = " + blob.length());
			// ファイルへ書き出し
			FileOutputStream fo = new FileOutputStream("./rcm/test.xls");
			long bytes = blob.length();
			fo.write(DataFilterFactory.getFilter(DataFilterFactory.GZIP).decompressContents(blob.getBytes(1L, (int)bytes)));
			fo.close();
		}
		rs.close();
		statement.close();
		con.close();
	}
}
