From 1fe997f9e19a88d55dc9735dd14f56c5f5ee48d3 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Mon, 2 Sep 2024 15:59:22 +0800 Subject: [PATCH] up jdbcutil --- .../src/main/java/org/rx/jdbc/JdbcUtil.java | 121 ++++++++++-------- .../main/java/org/rx/redis/RedisCache.java | 6 +- .../src/test/java/org/rx/jdbc/TestJdbc.java | 43 ++++++- .../src/main/java/org/rx/bean/DataTable.java | 13 +- .../main/java/org/rx/bean/IntWaterMark.java | 16 +-- .../src/main/java/org/rx/core/Constants.java | 5 + .../main/java/org/rx/core/CpuWatchman.java | 5 + rxlib/src/main/java/org/rx/core/RxConfig.java | 12 +- .../src/main/java/org/rx/core/ThreadPool.java | 15 +-- .../java/org/rx/io/EntityDatabaseImpl.java | 22 ++-- .../java/org/rx/io/EntityQueryLambda.java | 32 +++-- rxlib/src/main/resources/rx.yml | 7 +- 12 files changed, 168 insertions(+), 129 deletions(-) diff --git a/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java b/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java index c2c50395..b40d912b 100644 --- a/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java +++ b/rxlib-x/src/main/java/org/rx/jdbc/JdbcUtil.java @@ -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; @@ -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 = "*/"; @@ -202,13 +205,10 @@ public static void print(ResultSet resultSet) { } } - //// static byte PREFER_COLUMN_NAME = 1; -//// boolean preferColumnName, boolean toLowerCamelColumn - static final BiFunc LOWER_CAMEL_COLUMN_MAPPING = p -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, p); - static final Object DBNull = new Object(); + public static final BiFunc TO_CAMEL_COLUMN_MAPPING = p -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, p); public static List readAs(ResultSet resultSet, Type type) { - return readAs(resultSet, type, LOWER_CAMEL_COLUMN_MAPPING, null); + return readAs(resultSet, type, TO_CAMEL_COLUMN_MAPPING, null); } @SneakyThrows @@ -243,75 +243,86 @@ public static List readAs(@NonNull ResultSet resultSet, @NonNull Type typ return list; } - public static int buildInsertSql(String tableName, @NonNull T po, BiFunc columnMapping) { + public static String buildInsertSql(T po) { + return buildInsertSql(po, TO_UNDERSCORE_TABLE_MAPPING, TO_UNDERSCORE_COLUMN_MAPPING, null); + } + + public static String buildInsertSql(@NonNull T po, BiFunc, String> tableMapping, BiFunc columnMapping, TripleFunc valueMapping) { JSONObject row = toJsonObject(po); if (row.isEmpty()) { throw new InvalidException("Type {} hasn't any getters", po.getClass()); } - List 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 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 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 String buildUpdateSql(T po, EntityQueryLambda query) { + return buildUpdateSql(po, query, TO_UNDERSCORE_TABLE_MAPPING, TO_UNDERSCORE_COLUMN_MAPPING, null); } - public static int buildInsertSql(String tableName, @NonNull T po, BiFunc columnMapping) { + public static String buildUpdateSql(@NonNull T po, @NonNull EntityQueryLambda query, BiFunc, String> tableMapping, BiFunc columnMapping, TripleFunc 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 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 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(); } } diff --git a/rxlib-x/src/main/java/org/rx/redis/RedisCache.java b/rxlib-x/src/main/java/org/rx/redis/RedisCache.java index 29fdc385..b744c070 100644 --- a/rxlib-x/src/main/java/org/rx/redis/RedisCache.java +++ b/rxlib-x/src/main/java/org/rx/redis/RedisCache.java @@ -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 diff --git a/rxlib-x/src/test/java/org/rx/jdbc/TestJdbc.java b/rxlib-x/src/test/java/org/rx/jdbc/TestJdbc.java index 70216328..7cc7691e 100644 --- a/rxlib-x/src/test/java/org/rx/jdbc/TestJdbc.java +++ b/rxlib-x/src/test/java/org/rx/jdbc/TestJdbc.java @@ -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"))); } } diff --git a/rxlib/src/main/java/org/rx/bean/DataTable.java b/rxlib/src/main/java/org/rx/bean/DataTable.java index 5d4c9ae4..95238ed4 100644 --- a/rxlib/src/main/java/org/rx/bean/DataTable.java +++ b/rxlib/src/main/java/org/rx/bean/DataTable.java @@ -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; @@ -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; @@ -183,13 +181,14 @@ public List toList(@NonNull Type type, boolean toLowerCamelColumn) { List list = new ArrayList<>(); int colSize = columns.size(); Iterator rows = getRows(); + Map row = new HashMap<>(colSize); while (rows.hasNext()) { + row.clear(); List 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; } diff --git a/rxlib/src/main/java/org/rx/bean/IntWaterMark.java b/rxlib/src/main/java/org/rx/bean/IntWaterMark.java index 21b22b59..118b690c 100644 --- a/rxlib/src/main/java/org/rx/bean/IntWaterMark.java +++ b/rxlib/src/main/java/org/rx/bean/IntWaterMark.java @@ -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; - } } diff --git a/rxlib/src/main/java/org/rx/core/Constants.java b/rxlib/src/main/java/org/rx/core/Constants.java index 92418f18..208504fe 100644 --- a/rxlib/src/main/java/org/rx/core/Constants.java +++ b/rxlib/src/main/java/org/rx/core/Constants.java @@ -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 TIMER_PERIOD_FLAG = TimeoutFlag.PERIOD.flags(); FlagsEnum TIMER_SINGLE_FLAG = TimeoutFlag.SINGLE.flags(); FlagsEnum TIMER_REPLACE_FLAG = TimeoutFlag.REPLACE.flags(); diff --git a/rxlib/src/main/java/org/rx/core/CpuWatchman.java b/rxlib/src/main/java/org/rx/core/CpuWatchman.java index 918fe5ea..02142a4c 100644 --- a/rxlib/src/main/java/org/rx/core/CpuWatchman.java +++ b/rxlib/src/main/java/org/rx/core/CpuWatchman.java @@ -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; @@ -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])); } diff --git a/rxlib/src/main/java/org/rx/core/RxConfig.java b/rxlib/src/main/java/org/rx/core/RxConfig.java index aa86adb4..1a1dc0e4 100644 --- a/rxlib/src/main/java/org/rx/core/RxConfig.java +++ b/rxlib/src/main/java/org/rx/core/RxConfig.java @@ -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; @@ -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"; @@ -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; @@ -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); diff --git a/rxlib/src/main/java/org/rx/core/ThreadPool.java b/rxlib/src/main/java/org/rx/core/ThreadPool.java index 55a07972..58a3bc4e 100644 --- a/rxlib/src/main/java/org/rx/core/ThreadPool.java +++ b/rxlib/src/main/java/org/rx/core/ThreadPool.java @@ -267,19 +267,6 @@ public FutureTaskAdapter(Runnable runnable, T result) { static final FastThreadLocal CONTINUE_FLAG = new FastThreadLocal<>(); private static final FastThreadLocal COMPLETION_RETURNED_VALUE = new FastThreadLocal<>(); static final String POOL_NAME_PREFIX = "℞Threads-"; - static final IntWaterMark DEFAULT_CPU_WATER_MARK = new IntWaterMark() { - private static final long serialVersionUID = 4308886582647381475L; - - @Override - public int getLow() { - return RxConfig.INSTANCE.threadPool.lowCpuWaterMark; - } - - @Override - public int getHigh() { - return RxConfig.INSTANCE.threadPool.highCpuWaterMark; - } - }; static final Map> taskLockMap = new ConcurrentHashMap<>(8); static final Map> taskSerialMap = new ConcurrentHashMap<>(); @@ -434,7 +421,7 @@ private static int checkCapacity(int capacity) { public void dynamicSizeByCpuLoad(IntWaterMark cpuWaterMark) { if (cpuWaterMark == null) { - cpuWaterMark = DEFAULT_CPU_WATER_MARK; + cpuWaterMark = RxConfig.INSTANCE.threadPool.cpuWaterMark; } CpuWatchman.INSTANCE.register(this, cpuWaterMark); } diff --git a/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java b/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java index 24590790..ae47b042 100644 --- a/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java +++ b/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java @@ -111,11 +111,11 @@ public SqlMeta(String primaryKey, Map> columns log.info("Load H2 driver {}.{}", driver.getMajorVersion(), driver.getMinorVersion()); } - static String columnName(Field field, DbColumn dbColumn, boolean autoUnderscoreColumnName) { + static String columnName(Field field, DbColumn dbColumn, BiFunc columnMapping) { if (dbColumn != null && !dbColumn.name().isEmpty()) { return dbColumn.name(); } - return autoUnderscoreColumnName ? CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, field.getName()) + return columnMapping != null ? columnMapping.apply(field.getName()) : field.getName(); } @@ -126,7 +126,9 @@ static String columnName(Field field, DbColumn dbColumn, boolean autoUnderscoreC final int maxConnections; final Set> mappedEntityTypes = ConcurrentHashMap.newKeySet(); @Setter - boolean autoUnderscoreColumnName; + BiFunc columnMapping = EntityQueryLambda.TO_UNDERSCORE_COLUMN_MAPPING; + @Setter + BiFunc, String> tableMapping = EntityQueryLambda.TO_UNDERSCORE_TABLE_MAPPING; @Setter boolean autoRollbackOnError; @Setter @@ -326,7 +328,7 @@ public long count(EntityQueryLambda query) { query.limit = null; } SqlMeta meta = getMeta(query.entityType); - query.setAutoUnderscoreColumnName(autoUnderscoreColumnName); + query.setColumnMapping(columnMapping); StringBuilder sql = new StringBuilder(meta.selectSql); replaceSelectColumns(sql, "COUNT(*)"); @@ -358,7 +360,7 @@ public boolean exists(EntityQueryLambda query) { query.offset = null; } SqlMeta meta = getMeta(query.entityType); - query.setAutoUnderscoreColumnName(autoUnderscoreColumnName); + query.setColumnMapping(columnMapping); StringBuilder sql = new StringBuilder(meta.selectSql); replaceSelectColumns(sql, "1"); @@ -411,7 +413,7 @@ public T findOne(EntityQueryLambda query) { @Override public List findBy(EntityQueryLambda query) { SqlMeta meta = getMeta(query.entityType); - query.setAutoUnderscoreColumnName(autoUnderscoreColumnName); + query.setColumnMapping(columnMapping); StringBuilder sql = new StringBuilder(meta.selectSql); List params = new ArrayList<>(); @@ -449,7 +451,7 @@ public void dropIndex(Class entityType, String fieldName) { Field field = Reflects.getFieldMap(entityType).get(fieldName); DbColumn dbColumn = field.getAnnotation(DbColumn.class); String tableName = tableName(entityType); - String colName = columnName(field, dbColumn, autoUnderscoreColumnName); + String colName = columnName(field, dbColumn, columnMapping); String sql = String.format("DROP INDEX %s ON %s;", indexName(tableName, colName), tableName); executeUpdate(sql); } @@ -458,7 +460,7 @@ public void createIndex(Class entityType, String fieldName) { Field field = Reflects.getFieldMap(entityType).get(fieldName); DbColumn dbColumn = field.getAnnotation(DbColumn.class); String tableName = tableName(entityType); - String colName = columnName(field, dbColumn, autoUnderscoreColumnName); + String colName = columnName(field, dbColumn, columnMapping); String index = dbColumn != null && (dbColumn.index() == DbColumn.IndexKind.UNIQUE_INDEX_ASC || dbColumn.index() == DbColumn.IndexKind.UNIQUE_INDEX_DESC) ? "UNIQUE " : Strings.EMPTY; String desc = dbColumn != null && (dbColumn.index() == DbColumn.IndexKind.INDEX_DESC @@ -498,7 +500,7 @@ public void createMapping(Class... entityTypes) { continue; } DbColumn dbColumn = field.getAnnotation(DbColumn.class); - String colName = columnName(field, dbColumn, autoUnderscoreColumnName); + String colName = columnName(field, dbColumn, columnMapping); Tuple tuple = Tuple.of(field, dbColumn); columns.put(colName, tuple); @@ -570,7 +572,7 @@ public String tableName(Class entityType) { if (n != null) { return n; } - return autoUnderscoreColumnName ? CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, entityType.getSimpleName()) + return tableMapping != null ? tableMapping.apply(entityType) : entityType.getSimpleName(); } diff --git a/rxlib/src/main/java/org/rx/io/EntityQueryLambda.java b/rxlib/src/main/java/org/rx/io/EntityQueryLambda.java index af9f3737..11ee06f5 100644 --- a/rxlib/src/main/java/org/rx/io/EntityQueryLambda.java +++ b/rxlib/src/main/java/org/rx/io/EntityQueryLambda.java @@ -48,12 +48,12 @@ static void pkClaus(StringBuilder sql, String pk) { } final Class entityType; - @Setter - boolean autoUnderscoreColumnName; final ArrayList> conditions = new ArrayList<>(); final List, Order>> orders = new ArrayList<>(); boolean orderByRand; Integer limit, offset; + @Setter + BiFunc columnMapping; public EntityQueryLambda limit(int limit) { this.limit = limit; @@ -171,7 +171,7 @@ public String toString() { } public String toString(List params) { - return resolve(conditions, params, orders, orderByRand, autoUnderscoreColumnName, limit, offset); + return resolve(conditions, params, orders, orderByRand, limit, offset, columnMapping); } static List sharding(List result, EntityQueryLambda lambda) { @@ -208,8 +208,8 @@ static List sharding(List result, EntityQueryLambda lambda) { } static String resolve(ArrayList> conditions, List params, - List, Order>> orders, boolean orderByRand, boolean autoUnderscoreColumnName, - Integer limit, Integer offset) { + List, Order>> orders, boolean orderByRand, Integer limit, Integer offset, + BiFunc columnMapping) { StringBuilder b = new StringBuilder(128); boolean isParam = params != null; for (BiTuple condition : conditions) { @@ -223,7 +223,7 @@ static String resolve(ArrayList> conditio case LE: case LIKE: case NOT_LIKE: { - String colName = resolveColumnName(condition.left, autoUnderscoreColumnName); + String colName = resolveColumnName(condition.left, columnMapping); if (!b.isEmpty()) { b.append(OP_AND); } @@ -239,7 +239,7 @@ static String resolve(ArrayList> conditio break; case IN: case NOT_IN: { - String colName = resolveColumnName(condition.left, autoUnderscoreColumnName); + String colName = resolveColumnName(condition.left, columnMapping); if (!b.isEmpty()) { b.append(OP_AND); } @@ -255,7 +255,7 @@ static String resolve(ArrayList> conditio break; case BETWEEN: case NOT_BETWEEN: { - String colName = resolveColumnName(condition.left, autoUnderscoreColumnName); + String colName = resolveColumnName(condition.left, columnMapping); Object[] p = (Object[]) condition.right; if (!b.isEmpty()) { b.append(OP_AND); @@ -280,7 +280,7 @@ static String resolve(ArrayList> conditio if (!b.isEmpty()) { b.append(OP_AND); } - b.appendFormat(op.format, resolve(l, params, null, orderByRand, autoUnderscoreColumnName, limit, offset), r.toString(params)); + b.appendFormat(op.format, resolve(l, params, null, orderByRand, limit, offset, columnMapping), r.toString(params)); break; } } @@ -290,7 +290,7 @@ static String resolve(ArrayList> conditio } else if (!CollectionUtils.isEmpty(orders)) { b.append(ORDER_BY); for (Tuple, Order> bi : orders) { - String colName = resolveColumnName(bi.left, autoUnderscoreColumnName); + String colName = resolveColumnName(bi.left, columnMapping); b.appendFormat("%s %s,", colName, bi.right); } b.setLength(b.length() - 1); @@ -310,13 +310,17 @@ static String resolve(ArrayList> conditio return b.toString(); } - static String resolveColumnName(Object fn, boolean autoUnderscoreColumnName) { + static String resolveColumnName(Object fn, BiFunc columnMapping) { String propName = Reflects.resolveProperty((BiFunc) fn); - return autoUnderscoreColumnName ? CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, propName) - : propName; + return columnMapping != null ? columnMapping.apply(propName) : propName; +// return autoUnderscoreColumnName ? CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, propName) +// : propName; } - static String toValueString(Object val) { + public static final BiFunc TO_UNDERSCORE_COLUMN_MAPPING = p -> CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, p); + public static final BiFunc, String> TO_UNDERSCORE_TABLE_MAPPING = p -> CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, p.getSimpleName()); + + public static String toValueString(Object val) { if (val == null) { return DB_NULL; } diff --git a/rxlib/src/main/resources/rx.yml b/rxlib/src/main/resources/rx.yml index 0723af97..7d08c772 100644 --- a/rxlib/src/main/resources/rx.yml +++ b/rxlib/src/main/resources/rx.yml @@ -11,8 +11,9 @@ app: initSize: 0 keepAliveSeconds: 600 queueCapacity: 0 - lowCpuWaterMark: 40 - highCpuWaterMark: 70 + cpuWaterMark: + low: 40 + high: 70 watchSystemCpu: false replicas: 2 maxTraceDepth: 5 @@ -20,7 +21,7 @@ app: cpuLoadWarningThreshold: 80 samplingPeriod: 3000 samplingTimes: 2 - minDynamicSize: 2 + minDynamicSize: 1 maxDynamicSize: 1000 resizeQuantity: 2 cache: