Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
pers.clare.hisql.util.ResultSetUtil Maven / Gradle / Ivy
package pers.clare.hisql.util;
import pers.clare.hisql.function.FieldSetter;
import pers.clare.hisql.function.ResultSetValueConverter;
import pers.clare.hisql.store.SQLStore;
import pers.clare.hisql.support.ResultSetConverter;
import java.lang.reflect.Constructor;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
public class ResultSetUtil {
private ResultSetUtil() {
}
public static String[] getNames(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount();
int i;
String[] names = new String[count];
for (i = 0; i < count; ) {
names[i] = metaData.getColumnLabel(++i);
}
return names;
}
public static T to(ResultSetConverter resultSetConverter, ResultSet rs, Class clazz) throws SQLException {
if (rs.next()) {
return getValue(resultSetConverter, rs, 1, clazz);
}
return null;
}
public static Map toMap(ResultSetConverter resultSetConverter, ResultSet rs, Class valueClass) throws SQLException {
if (rs.next()) {
return toMap(resultSetConverter, rs, valueClass, getNames(rs));
}
return null;
}
public static Set toSet(ResultSetConverter resultSetConverter, ResultSet rs, Class clazz) throws SQLException {
Set result = new HashSet<>();
while (rs.next()) {
result.add(getValue(resultSetConverter, rs, 1, clazz));
}
return result;
}
public static Set> toMapSet(ResultSetConverter resultSetConverter, ResultSet rs, Class valueClass) throws SQLException {
return toMapCollection(resultSetConverter, rs, valueClass, new HashSet<>());
}
public static List> toMapList(ResultSetConverter resultSetConverter, ResultSet rs, Class valueClass) throws SQLException {
return toMapCollection(resultSetConverter, rs, valueClass, new ArrayList<>());
}
public static List toList(ResultSetConverter resultSetConverter, ResultSet rs, Class clazz) throws SQLException {
List result = new ArrayList<>();
while (rs.next()) {
result.add(getValue(resultSetConverter, rs, 1, clazz));
}
return result;
}
public static T toInstance(ResultSet rs, SQLStore sqlStore) throws Exception {
FieldSetter[] fields = toFields(rs.getMetaData(), sqlStore.getFieldSetMap());
if (rs.next()) {
return buildInstance(rs, sqlStore.getConstructor(), fields);
}
return null;
}
public static Set toSetInstance(ResultSet rs, SQLStore sqlStore) throws Exception {
Set result = new HashSet<>();
FieldSetter[] fields = toFields(rs.getMetaData(), sqlStore.getFieldSetMap());
while (rs.next()) {
result.add(buildInstance(rs, sqlStore.getConstructor(), fields));
}
return result;
}
public static List toInstances(ResultSet rs, SQLStore sqlStore) throws Exception {
List list = new ArrayList<>();
FieldSetter[] fields = toFields(rs.getMetaData(), sqlStore.getFieldSetMap());
while (rs.next()) {
list.add(buildInstance(rs, sqlStore.getConstructor(), fields));
}
return list;
}
@SuppressWarnings("unchecked")
private static Map toMap(
ResultSetConverter resultSetConverter
, ResultSet rs
, Class valueClass
, String[] names
) throws SQLException {
Map map = new HashMap<>(names.length);
int i = 1;
if (valueClass == Object.class) {
for (String name : names) {
map.put(name, (T) rs.getObject(i++));
}
} else {
for (String name : names) {
map.put(name, getValue(resultSetConverter, rs, i++, valueClass));
}
}
return map;
}
private static >> C toMapCollection(
ResultSetConverter resultSetConverter
, ResultSet rs
, Class valueClass
, C collection
) throws SQLException {
String[] names = getNames(rs);
while (rs.next()) {
collection.add(toMap(resultSetConverter, rs, valueClass, names));
}
return collection;
}
private static T buildInstance(ResultSet rs, Constructor constructor, FieldSetter[] fields) throws Exception {
T target = constructor.newInstance();
int i = 1;
for (FieldSetter field : fields) {
if (field != null) field.apply(target, rs, i);
i++;
}
return target;
}
private static FieldSetter[] toFields(ResultSetMetaData metaData, Map fieldMap) throws Exception {
int l = metaData.getColumnCount();
FieldSetter[] fields = new FieldSetter[l];
for (int i = 0; i < l; i++) {
fields[i] = fieldMap.get(metaData.getColumnLabel(i + 1));
}
return fields;
}
@SuppressWarnings("unchecked")
public static T getValue(ResultSetConverter resultSetConverter, ResultSet rs, int index, Class clazz) throws SQLException {
ResultSetValueConverter valueConverter = resultSetConverter.get(clazz);
if (valueConverter == null) {
if (clazz == Object.class) {
return (T) rs.getObject(index);
} else {
return rs.getObject(index, clazz);
}
} else {
return valueConverter.apply(rs, index);
}
}
}