org.r10r.sqlify.resultparser.SingleResultParser Maven / Gradle / Ivy
The newest version!
package org.r10r.sqlify.resultparser;
import org.r10r.sqlify.rowparser.RowParsers;
import java.sql.ResultSet;
import org.r10r.sqlify.SqlifyException;
import org.r10r.sqlify.rowparser.RowParser;
public class SingleResultParser implements ResultParser {
private final RowParser rowParser;
private SingleResultParser(RowParser rowParser) {
this.rowParser = rowParser;
}
/**
* Define your own RowParser. Full flexibility.
*
* @param The type the RowParser will return
* @param rowParser The RowParser to use
* @return The ListResultParser that will use the defined RowParser to parse
* rows.
*/
public static SingleResultParser of(RowParser rowParser) {
return new SingleResultParser(rowParser);
}
public static SingleResultParser of(Class clazz) {
RowParser rowParser = RowParsers.getDefaultParserFor(clazz);
return new SingleResultParser(rowParser);
}
@Override
public T parseResultSet(ResultSet resultSet) throws Exception {
if (resultSet.next()) {
return rowParser.parse(resultSet);
} else {
throw new SqlifyException("Ops. Could not parse single result, or there was no result.");
}
}
}