Skip to content

Commit

Permalink
Update JdbcUtil.java
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Sep 2, 2024
1 parent 08c8892 commit 2f40356
Showing 1 changed file with 124 additions and 5 deletions.
129 changes: 124 additions & 5 deletions rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package org.rx.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson2.JSONObject;
import com.mysql.jdbc.MySQLConnection;
import com.mysql.jdbc.StringUtils;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.ProxyConnection;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.core.Linq;
import org.rx.core.Reflects;
import org.rx.bean.DateTime;
import org.rx.core.*;
import org.rx.core.StringBuilder;
import org.rx.core.Strings;
import org.rx.exception.InvalidException;
import org.rx.third.guava.CaseFormat;
import org.rx.util.function.BiAction;
import org.rx.util.function.BiFunc;

import javax.sql.DataSource;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Collections;
import java.util.Map;
import java.util.*;

import static org.rx.core.Extends.as;
import static org.rx.core.Sys.fromJson;
import static org.rx.core.Sys.toJsonObject;

public class JdbcUtil {
static final String HINT_PREFIX = "/*", HINT_SUFFIX = "*/";
Expand Down Expand Up @@ -195,4 +201,117 @@ public static void print(ResultSet resultSet) {
}
}
}

//// static byte PREFER_COLUMN_NAME = 1;
//// boolean preferColumnName, boolean toLowerCamelColumn
static final BiFunc<String, String> LOWER_CAMEL_COLUMN_MAPPING = p -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, p);
static final Object DBNull = new Object();

public static <T> List<T> readAs(ResultSet resultSet, Type type) {
return readAs(resultSet, type, LOWER_CAMEL_COLUMN_MAPPING, null);
}

@SneakyThrows
public static <T> List<T> readAs(@NonNull ResultSet resultSet, @NonNull Type type, BiFunc<String, String> columnMapping, BiAction<Map<String, Object>> rowMapping) {
List<T> list = new ArrayList<>();
try (ResultSet rs = resultSet) {
ResultSetMetaData metaData = rs.getMetaData();
int colSize = metaData.getColumnCount();
List<String> columns = new ArrayList<>(colSize);
if (columnMapping != null) {
for (int i = 1; i <= colSize; i++) {
columns.add(columnMapping.invoke(metaData.getColumnLabel(i)));
}
} else {
for (int i = 1; i <= colSize; i++) {
columns.add(metaData.getColumnLabel(i));
}
}

JSONObject row = new JSONObject(colSize);
while (rs.next()) {
row.clear();
for (int i = 0; i < colSize; ) {
row.put(columns.get(i), rs.getObject(++i));
}
if (rowMapping != null) {
rowMapping.invoke(row);
}
list.add(fromJson(row, type));
}
}
return list;
}

public static <T> int buildInsertSql(String tableName, @NonNull T po, BiFunc<String, String> columnMapping) {
JSONObject row = toJsonObject(po);
if (row.isEmpty()) {
throw new InvalidException("Type {} hasn't any getters", po.getClass());
}

List<String> columns = new ArrayList<>(row.size()),values = new ArrayList<>(row.size());
for (String k : new HashSet<>(row.keySet())) {
String nk = columnMapping.apply(k);
if (nk == null) {
row.remove(k);
continue;
}
if (nk.equals(k)) {
continue;
}
row.put(nk, row.remove(k));
}


for (String col : row.keySet()) {
columns
}

StringBuilder buf = new StringBuilder(128)
.appendMessageFormat(Constants.SQL_INSERT, tableName,String.join(",",row.keySet()),Linq.from());
boolean first = true;
for (Map.Entry<String, Object> entry : row.entrySet()) {
if (first) {
first = false;
} else {
buf.append(',');
}
Object val = entry.getValue();
buf.appendMessageFormat(" `{}`={}", entry.getKey(), DBNull == val ? "NULL" : val.toString());
}
return 1;
}

public static <T> int buildInsertSql(String tableName, @NonNull T po, BiFunc<String, String> columnMapping) {
JSONObject row = toJsonObject(po);
if (row.isEmpty()) {
throw new InvalidException("Type {} hasn't any getters", po.getClass());
}

for (String k : new HashSet<>(row.keySet())) {
String nk = columnMapping.apply(k);
if (nk == null) {
row.remove(k);
continue;
}
if (nk.equals(k)) {
continue;
}
row.put(nk, row.remove(k));
}

StringBuilder buf = new StringBuilder(32)
.appendMessageFormat("UPDATE {} SET", tableName);
boolean first = true;
for (Map.Entry<String, Object> entry : row.entrySet()) {
if (first) {
first = false;
} else {
buf.append(',');
}
Object val = entry.getValue();
buf.appendMessageFormat(" `{}`={}", entry.getKey(), DBNull == val ? "NULL" : val.toString());
}
return 1;
}
}

0 comments on commit 2f40356

Please sign in to comment.