/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.operations;

import database.UserGame;
import database.connection.DatabaseConnection;
import database.encoding.Encode;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import woaley.Woaley;

/**
 *
 * @author Ani
 */
public class ReadPlayerTable {

    public static UserGame heuristicForState(Woaley state, String table) throws SQLException, IOException{
        return heuristicForState(new Encode(state).getCode(), table);
    }

    public static UserGame heuristicForState(String code, String table) throws SQLException, IOException{
        UserGame state = null;

        PreparedStatement pstmt = new DatabaseConnection().getConnection().
                prepareStatement("SELECT code, heuristic FROM " + table +
                " where code = ?");
        pstmt.setString(1, code);
        ResultSet rs = pstmt.executeQuery();

        while (rs.next()){
            state = new UserGame(rs.getString("code"), rs.getInt("heuristic"));
        }
        return state;
    }

        public static UserGame heuristicForState(Connection con, String code, String table) throws SQLException, IOException{
        UserGame state = null;

        PreparedStatement pstmt = con.prepareStatement("SELECT code, heuristic FROM " + table +
                " where code = ?");
        pstmt.setString(1, code);
        ResultSet rs = pstmt.executeQuery();

        while (rs.next()){
            state = new UserGame(rs.getString("code"), rs.getInt("heuristic"));
        }
        return state;
    }


    
}
