package electrosphere.game.server.db;

import com.google.gson.Gson;
import electrosphere.logger.LoggerInterface;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author amaterasu
 */
public class DatabaseResult {
    boolean isQuery = false;
    boolean isStatement = false;
    boolean succeeded = false;
    boolean hasResultSet = false;
    String code;
    ResultSet rs;
    private static Gson deserializer;
    
    static {
        deserializer = new Gson();
    }
    
    
    public static DatabaseResult createQuery(String code){
        DatabaseResult rVal = new DatabaseResult();
        rVal.isQuery = true;
        rVal.code = code;
        return rVal;
    }
    
    public static DatabaseResult createStatement(String code){
        DatabaseResult rVal = new DatabaseResult();
        rVal.isStatement = true;
        rVal.code = code;
        return rVal;
    }
    
    public void addResultSet(ResultSet rs){
        this.rs = rs;
    }
    
    public ResultSet getResultSet(){
        return rs;
    }
    
    public boolean hasResultSet(){
        return hasResultSet;
    }
    
    public boolean isQuery(){
        return isQuery;
    }
    
    public boolean isStatement(){
        return isStatement;
    }
    
    public boolean success(){
        return succeeded;
    }
    
    public <T>T deserializeFirstResult(Class<T> className){
        T rVal = null;
        try {
            rs.getString(0);
            rVal = deserializer.fromJson(rs.getString(0), className);
        } catch (SQLException ex) {
            LoggerInterface.loggerFileIO.ERROR("Failure to deserialize result", ex);
        }
        return rVal;
    }
    
    
    public void logRawResult(){
    }
}
