package zmo.technology.lite.processors;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import zmo.technology.lite.core.ZeroAppSecret;
import zmo.technology.lite.core.ZeroProcessor;
import zmo.technology.lite.core.ZeroRequestGuider;

public class ZeroAppSecretProcessor extends ZeroProcessor {
    
    protected ZeroAppSecretProcessor(ZeroRequestGuider guider) {
        super(guider);
    }

    public static ZeroAppSecretProcessor build(ZeroRequestGuider guider) {
        return new ZeroAppSecretProcessor(guider);
    }

    private PreparedStatement fetchAppSecretPrepared() throws SQLException {
        final String fetchAppSecret = "SELECT * FROM zero_appsecret WHERE app_id = ?";
        if (!existsPreparedStatement(fetchAppSecret))
            registeyPreparedStatement(fetchAppSecret);
        return preparedStatement(fetchAppSecret);
    }

    public ZeroAppSecret fetchAppSecret(String appID) throws SQLException {
        fetchAppSecretPrepared().setString(1, appID);
        ResultSet set = fetchAppSecretPrepared().executeQuery();
        if (!set.next())
            throw new SQLException(" app `" + appID + "` not found");
        return new ZeroAppSecret(set);
    }

}
