Skip to content

Commit

Permalink
up jdbcutil
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Sep 2, 2024
1 parent 2f40356 commit 1fe997f
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 129 deletions.
121 changes: 66 additions & 55 deletions rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import com.zaxxer.hikari.pool.ProxyConnection;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.bean.DateTime;
import org.rx.core.*;
import org.rx.core.StringBuilder;
import org.rx.exception.InvalidException;
import org.rx.io.EntityQueryLambda;
import org.rx.third.guava.CaseFormat;
import org.rx.util.function.BiAction;
import org.rx.util.function.BiFunc;
import org.rx.util.function.TripleFunc;

import javax.sql.DataSource;
import java.io.InputStream;
Expand All @@ -28,6 +29,8 @@
import static org.rx.core.Extends.as;
import static org.rx.core.Sys.fromJson;
import static org.rx.core.Sys.toJsonObject;
import static org.rx.io.EntityQueryLambda.TO_UNDERSCORE_COLUMN_MAPPING;
import static org.rx.io.EntityQueryLambda.TO_UNDERSCORE_TABLE_MAPPING;

public class JdbcUtil {
static final String HINT_PREFIX = "/*", HINT_SUFFIX = "*/";
Expand Down Expand Up @@ -202,13 +205,10 @@ 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 final BiFunc<String, String> TO_CAMEL_COLUMN_MAPPING = p -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, p);

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

@SneakyThrows
Expand Down Expand Up @@ -243,75 +243,86 @@ public static <T> List<T> readAs(@NonNull ResultSet resultSet, @NonNull Type typ
return list;
}

public static <T> int buildInsertSql(String tableName, @NonNull T po, BiFunc<String, String> columnMapping) {
public static <T> String buildInsertSql(T po) {
return buildInsertSql(po, TO_UNDERSCORE_TABLE_MAPPING, TO_UNDERSCORE_COLUMN_MAPPING, null);
}

public static <T> String buildInsertSql(@NonNull T po, BiFunc<Class<?>, String> tableMapping, BiFunc<String, String> columnMapping, TripleFunc<String, Object, Object> valueMapping) {
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;
List<String> columns = new ArrayList<>(row.size()), values = new ArrayList<>(row.size());
if (columnMapping != null) {
for (String k : row.keySet()) {
String nk = columnMapping.apply(k);
if (nk == null) {
continue;
}
columns.add("`" + nk + "`");
Object val = row.get(k);
if (valueMapping != null) {
val = valueMapping.apply(nk, val);
}
values.add(EntityQueryLambda.toValueString(val));
}
if (nk.equals(k)) {
continue;
} else {
for (String k : row.keySet()) {
columns.add("`" + k + "`");
Object val = row.get(k);
if (valueMapping != null) {
val = valueMapping.apply(k, val);
}
values.add(EntityQueryLambda.toValueString(val));
}
row.put(nk, row.remove(k));
}

Class<?> poType = po.getClass();
return new StringBuilder(128)
.appendMessageFormat(Constants.SQL_INSERT,
tableMapping != null ? tableMapping.apply(poType) : poType.getSimpleName(),
String.join(",", columns), String.join(",", values)).toString();
}

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> String buildUpdateSql(T po, EntityQueryLambda<T> query) {
return buildUpdateSql(po, query, TO_UNDERSCORE_TABLE_MAPPING, TO_UNDERSCORE_COLUMN_MAPPING, null);
}

public static <T> int buildInsertSql(String tableName, @NonNull T po, BiFunc<String, String> columnMapping) {
public static <T> String buildUpdateSql(@NonNull T po, @NonNull EntityQueryLambda<T> query, BiFunc<Class<?>, String> tableMapping, BiFunc<String, String> columnMapping, TripleFunc<String, Object, Object> valueMapping) {
JSONObject row = toJsonObject(po);
if (row.isEmpty()) {
throw new InvalidException("Type {} hasn't any getters", po.getClass());
}
query.setColumnMapping(columnMapping);

for (String k : new HashSet<>(row.keySet())) {
String nk = columnMapping.apply(k);
if (nk == null) {
row.remove(k);
continue;
List<String> colVals = new ArrayList<>(row.size());
if (columnMapping != null) {
for (String k : row.keySet()) {
String nk = columnMapping.apply(k);
if (nk == null) {
continue;
}
Object val = row.get(k);
if (valueMapping != null) {
val = valueMapping.apply(nk, val);
}
colVals.add("`" + nk + "`=" + EntityQueryLambda.toValueString(val));
}
if (nk.equals(k)) {
continue;
} else {
for (String k : row.keySet()) {
Object val = row.get(k);
if (valueMapping != null) {
val = valueMapping.apply(k, val);
}
colVals.add("`" + k + "`=" + EntityQueryLambda.toValueString(val));
}
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;
Class<?> poType = po.getClass();
return new StringBuilder(128)
.appendMessageFormat(Constants.SQL_UPDATE, tableMapping != null ? tableMapping.apply(poType) : poType.getSimpleName(),
String.join(",", colVals))
.append(query).toString();
}
}
6 changes: 5 additions & 1 deletion rxlib-x/src/main/java/org/rx/redis/RedisCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ public int size() {
}

public RedisCache(String redisUrl) {
client = create(redisUrl);
this(create(redisUrl));
}

public RedisCache(@NonNull RedissonClient redissonClient) {
client = redissonClient;
}

@Override
Expand Down
43 changes: 39 additions & 4 deletions rxlib-x/src/test/java/org/rx/jdbc/TestJdbc.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
package org.rx.jdbc;

import lombok.Data;
import org.junit.jupiter.api.Test;
import org.rx.bean.DateTime;
import org.rx.io.EntityQueryLambda;

import java.util.Date;

public class TestJdbc {
@Data
public static class PoUser {
Long id;
String userName;
String pwd;
Integer age;
Date createAt;
Date modifyAt;
}

@Test
public void jdbcExec() {
JdbcExecutor d = new JdbcExecutor("jdbc:mysql://", "", "bG1hbG1#");
JdbcUtil.print(d.executeQuery("select * from emr.t_third_api_record\n" +
"# where third_order_id = 'A01202402201715030375693'\n" +
"order by updated_time desc"));
// JdbcExecutor d = new JdbcExecutor("jdbc:mysql://", "", "bG1hbG1#");
// JdbcUtil.print(d.executeQuery("select * from emr.t_third_api_record\n" +
// "# where third_order_id = 'A01202402201715030375693'\n" +
// "order by updated_time desc"));

PoUser po = new PoUser();
po.setId(1L);
po.setUserName("rocky");
po.setAge(16);
po.setCreateAt(DateTime.now());
po.setModifyAt(new Date());
System.out.println(JdbcUtil.buildInsertSql(po, t -> t.getSimpleName().toUpperCase(), c -> {
switch (c) {
case "id":
return "_id";
}
return c;
}, (c, v) -> {
if (v instanceof Date) {
return "2024-01-01";
}
return v;
}));
System.out.println(JdbcUtil.buildUpdateSql(po, new EntityQueryLambda<>(PoUser.class).eq(PoUser::getId, 1024).eq(PoUser::getUserName, "wyf")));
}
}
13 changes: 6 additions & 7 deletions rxlib/src/main/java/org/rx/bean/DataTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.h2.jdbc.JdbcResultSet;
import org.h2.result.LocalResult;
import org.h2.value.ValueToObjectConverter;
import org.rx.core.Arrays;
import org.rx.core.StringBuilder;
import org.rx.core.*;
import org.rx.exception.InvalidException;
Expand All @@ -20,10 +21,7 @@
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.*;

import static org.rx.core.Extends.as;
import static org.rx.core.Extends.tryAs;
Expand Down Expand Up @@ -183,13 +181,14 @@ public <T> List<T> toList(@NonNull Type type, boolean toLowerCamelColumn) {
List<T> list = new ArrayList<>();
int colSize = columns.size();
Iterator<DataRow> rows = getRows();
Map<String, Object> row = new HashMap<>(colSize);
while (rows.hasNext()) {
row.clear();
List<Object> cells = rows.next().items;
JSONObject j = new JSONObject(colSize);
for (int i = 0; i < colSize; i++) {
j.put(columns.get(i).columnName, cells.get(i));
row.put(columns.get(i).columnName, cells.get(i));
}
list.add(fromJson(j, type));
list.add(fromJson(row, type));
}
return list;
}
Expand Down
16 changes: 1 addition & 15 deletions rxlib/src/main/java/org/rx/bean/IntWaterMark.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,12 @@

import java.io.Serializable;

import static org.rx.core.Extends.require;

@Getter
@Setter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
@AllArgsConstructor
public class IntWaterMark implements Serializable {
private static final long serialVersionUID = -6996645790082139283L;
private int low, high;

public void setLow(int low) {
require(low, low < high);

this.low = low;
}

public void setHigh(int high) {
require(high, high > low);

this.high = high;
}
}
5 changes: 5 additions & 0 deletions rxlib/src/main/java/org/rx/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ enum MetricName {
/**
* do not edit
*/
// String SQL_INSERT = "INSERT INTO ${table} (${columns}) VALUES (${values})";
// String SQL_UPDATE = "UPDATE ${table} SET ${columnValues} WHERE ";
String SQL_INSERT = "INSERT INTO {} ({}) VALUES ({})";
String SQL_UPDATE = "UPDATE {} SET {} WHERE ";

FlagsEnum<TimeoutFlag> TIMER_PERIOD_FLAG = TimeoutFlag.PERIOD.flags();
FlagsEnum<TimeoutFlag> TIMER_SINGLE_FLAG = TimeoutFlag.SINGLE.flags();
FlagsEnum<TimeoutFlag> TIMER_REPLACE_FLAG = TimeoutFlag.REPLACE.flags();
Expand Down
5 changes: 5 additions & 0 deletions rxlib/src/main/java/org/rx/core/CpuWatchman.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.rx.bean.*;
import org.rx.exception.InvalidException;
import org.rx.exception.TraceHandler;
import org.rx.util.BeanMapper;
import org.rx.util.Snowflake;
Expand Down Expand Up @@ -267,6 +268,10 @@ public void register(@NonNull ThreadPoolExecutor pool, @NonNull IntWaterMark wat
if (waterMark.getHigh() > 100) {
waterMark.setHigh(100);
}
if (waterMark.getLow() > waterMark.getHigh()) {
throw new InvalidException("waterMark low > high");
}

holder.put(pool, Tuple.of(waterMark, new int[2]));
}

Expand Down
12 changes: 6 additions & 6 deletions rxlib/src/main/java/org/rx/core/RxConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ClassUtils;
import org.rx.annotation.Metadata;
import org.rx.bean.IntWaterMark;
import org.rx.bean.LogStrategy;
import org.rx.net.Sockets;
import org.springframework.core.env.Environment;
Expand All @@ -35,8 +36,8 @@ public interface ConfigNames {
String THREAD_POOL_INIT_SIZE = "app.threadPool.initSize";
String THREAD_POOL_KEEP_ALIVE_SECONDS = "app.threadPool.keepAliveSeconds";
String THREAD_POOL_QUEUE_CAPACITY = "app.threadPool.queueCapacity";
String THREAD_POOL_LOW_CPU_WATER_MARK = "app.threadPool.lowCpuWaterMark";
String THREAD_POOL_HIGH_CPU_WATER_MARK = "app.threadPool.highCpuWaterMark";
String THREAD_POOL_CPU_WATER_MARK_LOW = "app.threadPool.cpuWaterMark.low";
String THREAD_POOL_CPU_WATER_MARK_HIGH = "app.threadPool.cpuWaterMark.high";
String THREAD_POOL_WATCH_SYSTEM_CPU = "app.threadPool.watchSystemCpu";
String THREAD_POOL_REPLICAS = "app.threadPool.replicas";
String THREAD_POOL_TRACE_NAME = "app.threadPool.traceName";
Expand Down Expand Up @@ -108,8 +109,7 @@ public static class ThreadPoolConfig {
int initSize;
int keepAliveSeconds;
int queueCapacity;
int lowCpuWaterMark;
int highCpuWaterMark;
IntWaterMark cpuWaterMark = new IntWaterMark();
boolean watchSystemCpu;
int replicas;
String traceName;
Expand Down Expand Up @@ -286,8 +286,8 @@ public void refreshFromSystemProperty() {
threadPool.initSize = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_INIT_SIZE, threadPool.initSize);
threadPool.keepAliveSeconds = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_KEEP_ALIVE_SECONDS, threadPool.keepAliveSeconds);
threadPool.queueCapacity = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_QUEUE_CAPACITY, threadPool.queueCapacity);
threadPool.lowCpuWaterMark = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_LOW_CPU_WATER_MARK, threadPool.lowCpuWaterMark);
threadPool.highCpuWaterMark = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_HIGH_CPU_WATER_MARK, threadPool.highCpuWaterMark);
threadPool.cpuWaterMark.setLow(SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_CPU_WATER_MARK_LOW, threadPool.cpuWaterMark.getLow()));
threadPool.cpuWaterMark.setHigh(SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_CPU_WATER_MARK_HIGH, threadPool.cpuWaterMark.getHigh()));
threadPool.watchSystemCpu = SystemPropertyUtil.getBoolean(ConfigNames.THREAD_POOL_WATCH_SYSTEM_CPU, threadPool.watchSystemCpu);
threadPool.replicas = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_REPLICAS, threadPool.replicas);
threadPool.traceName = SystemPropertyUtil.get(ConfigNames.THREAD_POOL_TRACE_NAME);
Expand Down
Loading

0 comments on commit 1fe997f

Please sign in to comment.