package com.mysql.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class MysqlTestLoad {
	private static final String SQL1 = "SELECT \n" +
			"  `browser_version_new`.`id`, \n" +
			"  `browser_version_new`.`browser`, \n" +
			"  `browser_version_new`.`version`\n" +
			"FROM `browser_version_new`";

	public static void main(String[] args) throws Exception {
		testJdbc(SQL1);
	}

	static void testJdbc(String sql) throws Exception {
		MysqlDataSource dataSource = getMysqlDataSource();
		MySQLConnection connection = (MySQLConnection) dataSource.getConnection();

		ResultSet rs = sqlStatement(sql, connection);
//		ResultSet rs = sqlDirect(sql, connection.getIO());
//		ResultSet rs = sqlDirect2(sql, connection.getIO());

		int rows = 0;
		while (rs.next()) {
			++rows;
			System.out.println(rs.getObject(1) + ", " + rs.getObject(2) + ", " + rs.getObject(3));
		}
		System.out.println("Read rows " + rows);
		System.out.println("Warnings: " + connection.getWarnings());
		connection.getIO().dumpPacketRingBuffer();

		if (rows == 0)
			throw new RuntimeException("Loaded " + rows);
	}

	private static ResultSet sqlStatement(String sql, Connection connection) throws SQLException {
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		return statement.executeQuery();
	}

	private static ResultSetInternalMethods sqlDirect(String sql, MysqlIO io) throws Exception {
		return io.sqlQueryDirect(null, sql, null, null, -1,
				ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, false, null, null);
	}

	private static ResultSetInternalMethods sqlDirect2(String sql, MysqlIO io) throws Exception {
		Buffer queryPacket = new Buffer(1024);
		queryPacket.writeByte((byte) MysqlDefs.QUERY);
		queryPacket.writeStringNoNull(sql);
		Buffer resultPacket = io.sendCommand(MysqlDefs.QUERY, null, queryPacket, false, null, 0);
		return io.readAllResults(null, -1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, false, null, resultPacket,
				false, -1L, null);
//		byte[] buffer = read1K(io.mysqlInput);
//		System.out.println(Arrays.toString(buffer));
//		return null;
	}

	private static byte[] read1K(InputStream is) throws IOException {
		int read = 0;
		byte[] buffer = new byte[1024];
		while (read < buffer.length) {
			int r = is.read(buffer, read, buffer.length - read);
			if (r < 0) {
				break;
			}
			read += r;
		}
		return Arrays.copyOf(buffer, read);
	}

	private static MysqlDataSource getMysqlDataSource() throws SQLException {
		MysqlDataSource mysqlDataSource = new MysqlDataSource();

		mysqlDataSource.setUrl("jdbc:mysql://localhost:33060/wow_staging?characterEncoding=utf8&useSSL=false");
		mysqlDataSource.setUser("debug");
		mysqlDataSource.setPassword("WOWdebug");

		mysqlDataSource.setPacketDebugBufferSize(10240);
		mysqlDataSource.setEnablePacketDebug(true);
		return mysqlDataSource;
	}

}
