io.github.shanqiang.sp.input.JdbcFetcher Maven / Gradle / Ivy
The newest version!
package io.github.shanqiang.sp.input;
import io.github.shanqiang.exception.InconsistentColumnSizeException;
import io.github.shanqiang.exception.UnknownTypeException;
import io.github.shanqiang.table.Table;
import io.github.shanqiang.table.TableBuilder;
import io.github.shanqiang.table.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import static java.util.Objects.requireNonNull;
public abstract class JdbcFetcher {
protected Connection connection;
protected final Map columnTypeMap;
protected JdbcFetcher(Map columnTypeMap, Connection connection) {
this.columnTypeMap = requireNonNull(columnTypeMap);
if (columnTypeMap.size() < 1) {
throw new IllegalArgumentException();
}
this.connection = connection;
}
public Table fetch(String sql) throws SQLException {
TableBuilder tableBuilder = new TableBuilder(columnTypeMap);
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.getMetaData().getColumnCount() != columnTypeMap.size()) {
throw new InconsistentColumnSizeException();
}
while (resultSet.next()) {
int i = 0;
for (Type type : columnTypeMap.values()) {
int i1 = i + 1;
switch (type) {
case INT:
tableBuilder.append(i, resultSet.getInt(i1));
break;
case BIGINT:
tableBuilder.append(i, resultSet.getLong(i1));
break;
case DOUBLE:
tableBuilder.append(i, resultSet.getDouble(i1));
break;
case VARBYTE:
tableBuilder.append(i, resultSet.getString(i1));
break;
default:
throw new UnknownTypeException(type.name());
}
i++;
}
}
resultSet.close();
preparedStatement.close();
return tableBuilder.build();
}
public void close() throws SQLException {
connection.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy