/*
 * Main.java
 *
 */

package commitInProc;

import java.sql.*;

/**
 * Test <test purpose>
 * 
 * @author dag.wanvik@sun.com
 */
public class Main {
    
    Connection con = null;

    /** Creates a new instance of Main */
    public Main() {
    }


    private void dropProcedure() {
        try {
            Statement st = con.createStatement();
            st.execute("drop procedure f2");
            st.close();
        } catch (SQLException sqlE) {
            // expected
        } catch (Exception e) {
            System.out.println("Error dropping procedure: " + e.toString());
        }
    }

    
    private void doSingleDriver(){
        try {
            String driver = "org.apache.derby.jdbc.EmbeddedDriver";
            System.out.println("* Testing with " + driver);

            Class.forName(driver);
            con = DriverManager.getConnection
                ("jdbc:derby:test1;create=true");

            con.setAutoCommit(false);

            dropProcedure();

            Statement st = con.createStatement();


            st.execute("create procedure f2()" +
                       "  dynamic result sets 1 language java parameter style java" +
                       "  external name 'commitInProc.Main.f2'" +
                       "  modifies sql data");

            st.execute("call f2()");

            ResultSet prs = st.getResultSet();
            if (prs == null) {
                System.err.println("Got no result set from call to f2");
            } else {
                prs.next();
                System.err.println("Got: " + prs.getString(1));
            }

            st.execute("drop procedure f2");

            con.rollback();
            con.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
 

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Main m = new Main();
        m.doSingleDriver();
    }
    

    public static void f2(ResultSet[] rs1)
    {
        Connection conn = null;
        Statement stm = null;
        try {
            conn = DriverManager.getConnection("jdbc:default:connection");
            stm = conn.createStatement();

            conn.commit(); // after svn 602991(trunk), this breaks dynamic result set
                           // i.e. we get null back from st.getResultSet() above.

            ResultSet rs = stm.executeQuery("values current_user");
            rs1[0] = rs;
        } catch (SQLException e) {
            System.err.println(e);
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

// Local Variables: ***
// indent-tabs-mode: nil ***
// fill-column: 79 ***
// End: ***
