package mariadb.jdbc.bugs;
import java.sql.*;

public class MdbBugProcResult8thColFails {

	static Connection conn = null;
	static DatabaseMetaData dbmd = null;

	static String urlPart="jdbc:mariadb://localhost:3306";
	static String driverClass = "org.mariadb.jdbc.Driver";
	
	public static void createProcedure() throws SQLException 
	{
		Statement stmt = conn.createStatement();
		try {
			stmt.execute("DROP PROCEDURE proc1");
		} catch (SQLException se) {
		}
		stmt.execute("CREATE PROCEDURE proc1() BEGIN END");
		stmt.close();
	}

	public static void testGetProcedures() throws SQLException 
	{
		ResultSet rs = dbmd.getProcedures(null, null, "%");
		String procName;
		short procType = java.sql.DatabaseMetaData.procedureResultUnknown;
		while (rs.next()) {
			procName = rs.getString(3);
			System.out.println("Procedure name = " + procName);
			try {
				procType = rs.getShort(8);
			} catch (SQLException e) {
				if (rs.wasNull()) {
					System.out.println("proc type is null");
				}
				System.out.println("8th column of getProcdures failed with error code = " 
					+ e.getErrorCode() + " and error message = " + e.getMessage());
			}
			System.out.println("Procedure type = " + procType);
		}
	}
	
	
	public static void main(String args[]) {
		
		if (args.length != 3) {
			System.out.println("Usage: java MdbBugProcResult8thColFails <dbname> <userid> <password>");
			return;
		}
		String url = urlPart + "/" + args[0];
		try{
			Class.forName(driverClass);
			conn = DriverManager.getConnection(url , args[1], args[2]);
			System.out.println("Connected to " + url);
			
			dbmd = conn.getMetaData();
			
			createProcedure();
			testGetProcedures();
			
			conn.close();
		} catch (ClassNotFoundException cne) {
 			System.out.println("Error: " + driverClass + ":"
						+ cne.toString());
		} catch (SQLException se) {
 			System.out.println("Error: " 
					+ se.toString());
		}
			
	}
	
}
