151 lines
4.8 KiB
Java
151 lines
4.8 KiB
Java
package com.cubetiqs.sql;
|
|
|
|
import com.cubetiqs.mapper.MapperProvider;
|
|
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.ResultSetMetaData;
|
|
import java.sql.SQLException;
|
|
import java.util.*;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.stream.IntStream;
|
|
|
|
import static java.util.regex.Pattern.compile;
|
|
|
|
public final class ResultSetUtil {
|
|
public static void applyParameterized(PreparedStatement statement, String sql, Object... args) throws SQLException {
|
|
if (args != null && sql.indexOf('?') != -1) {
|
|
for (int index = 0; index < args.length; index++) {
|
|
statement.setObject(index + 1, args[index]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static int getColumnCount(ResultSet rs) throws SQLException {
|
|
return rs.getMetaData().getColumnCount();
|
|
}
|
|
|
|
public static List<String> getColumns(ResultSet rs) throws SQLException {
|
|
List<String> columns = new ArrayList<>();
|
|
ResultSetMetaData metaData = rs.getMetaData();
|
|
int columnCount = metaData.getColumnCount();
|
|
|
|
IntStream.range(0, columnCount).forEach(i -> {
|
|
try {
|
|
columns.add(metaData.getColumnName(i + 1));
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
});
|
|
|
|
return columns;
|
|
}
|
|
|
|
public static List<Object> getDataAsList(ResultSet rs) throws SQLException {
|
|
List<Object> rows = new ArrayList<>();
|
|
List<String> columns = ResultSetUtil.getColumns(rs);
|
|
|
|
while (rs.next()) {
|
|
Map<String, Object> row = new HashMap<>();
|
|
for (String column : columns) {
|
|
row.put(column, rs.getObject(column));
|
|
}
|
|
rows.add(row);
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
public static Object getDataAsObject(ResultSet rs) throws SQLException {
|
|
if (!rs.next()) {
|
|
return null;
|
|
}
|
|
|
|
List<String> columns = getColumns(rs);
|
|
if (columns.size() == 0) {
|
|
return null;
|
|
}
|
|
|
|
|
|
if (columns.size() == 1) {
|
|
return rs.getObject(columns.get(0));
|
|
} else {
|
|
Map<String, Object> row = new HashMap<>();
|
|
for (String column : columns) {
|
|
row.put(column, rs.getObject(column));
|
|
}
|
|
return row;
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static <R> R getDataAsType(MapperProvider<Map<String, Object>, R> mapper, ResultSet rs, Class<R> clazz) throws SQLException {
|
|
if (!rs.next()) {
|
|
return null;
|
|
}
|
|
|
|
List<String> columns = getColumns(rs);
|
|
if (columns.size() == 0) {
|
|
return null;
|
|
}
|
|
|
|
if (columns.size() == 1) {
|
|
String column = columns.get(0);
|
|
if (clazz.isInstance(String.class)) {
|
|
return (R) rs.getString(column);
|
|
} else if (clazz.isInstance(Integer.class)) {
|
|
return (R) Integer.valueOf(rs.getInt(column));
|
|
} else if (clazz.isInstance(Boolean.class)) {
|
|
return (R) Boolean.valueOf(rs.getBoolean(column));
|
|
} else if (clazz.isInstance(Date.class)) {
|
|
return (R) rs.getDate(column);
|
|
} else if (clazz.isInstance(Double.class)) {
|
|
return (R) Double.valueOf(rs.getDouble(column));
|
|
} else {
|
|
return (R) rs.getObject(column);
|
|
}
|
|
} else {
|
|
Map<String, Object> row = new HashMap<>();
|
|
|
|
for (String column : columns) {
|
|
row.put(column, rs.getObject(column));
|
|
}
|
|
|
|
return mapper.transformTo(row, clazz);
|
|
}
|
|
}
|
|
|
|
private static CharSequence escapeRegex(CharSequence text) {
|
|
if (text == null) return null;
|
|
return Pattern.quote(text + "");
|
|
}
|
|
|
|
public static List<String> findValues(String template, CharSequence sequence1, CharSequence sequence2) {
|
|
List<String> data = new ArrayList<>();
|
|
Pattern pattern;
|
|
if (sequence1 != null) {
|
|
if (sequence2 != null) {
|
|
pattern = compile(escapeRegex(sequence1) + "(.*?)" + escapeRegex(sequence2));
|
|
} else {
|
|
pattern = compile(escapeRegex(sequence1) + "([A-Za-z\\d]*)");
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
Matcher matcher = pattern.matcher(template);
|
|
while (matcher.find()) {
|
|
data.add(matcher.group(1));
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public static String replacerPrefix(String template, Map<String, Object> replacement, CharSequence prefix) {
|
|
AtomicReference<String> message = new AtomicReference<>(template);
|
|
replacement.forEach((key, value) -> message.set(message.get().replace(prefix + key, value.toString())));
|
|
return message.get();
|
|
}
|
|
}
|