From 076c26079393c2c1f4fe3c9a06268c078311c828 Mon Sep 17 00:00:00 2001 From: redkale Date: Thu, 11 Jan 2024 21:42:44 +0800 Subject: [PATCH] DataNativeSqlInfo --- .../org/redkale/source/DataJdbcSource.java | 122 ++++++++++++------ .../org/redkale/source/DataNativeSqlInfo.java | 3 +- 2 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index d84566bb2..6028dec5e 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -20,7 +20,6 @@ import org.redkale.inject.ResourceEvent; import org.redkale.service.Local; import static org.redkale.source.DataSources.*; import org.redkale.util.*; -import org.redkale.annotation.ResourceChanged; /** * DataSource的JDBC实现类 @@ -148,7 +147,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { return false; } - protected List prepareInsertEntityStatements(JdbcConnection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { + protected List prepareInsertEntityStatements(JdbcConnection conn, + EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; final List prestmts = new ArrayList<>(); for (Map.Entry> en : prepareInfos.entrySet()) { @@ -165,7 +165,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmts; } - protected PreparedStatement prepareInsertEntityStatement(JdbcConnection conn, String sql, EntityInfo info, T... entitys) throws SQLException { + protected PreparedStatement prepareInsertEntityStatement(JdbcConnection conn, String sql, + EntityInfo info, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; final PreparedStatement prestmt = info.isAutoGenerated() ? conn.prepareUpdateStatement(sql, Statement.RETURN_GENERATED_KEYS) @@ -177,7 +178,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmt; } - protected List prepareUpdateEntityStatements(JdbcConnection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { + protected List prepareUpdateEntityStatements(JdbcConnection conn, + EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; final List prestmts = new ArrayList<>(); @@ -194,7 +196,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmts; } - protected PreparedStatement prepareUpdateEntityStatement(JdbcConnection conn, String prepareSQL, EntityInfo info, T... entitys) throws SQLException { + protected PreparedStatement prepareUpdateEntityStatement(JdbcConnection conn, + String prepareSQL, EntityInfo info, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; final PreparedStatement prestmt = conn.prepareUpdateStatement(prepareSQL); @@ -206,7 +209,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmt; } - protected int bindStatementParameters(JdbcConnection conn, PreparedStatement prestmt, EntityInfo info, Attribute[] attrs, T entity) throws SQLException { + protected int bindStatementParameters(JdbcConnection conn, PreparedStatement prestmt, + EntityInfo info, Attribute[] attrs, T entity) throws SQLException { int i = 0; for (Attribute attr : attrs) { Object val = getEntityAttrValue(info, attr, entity); @@ -340,7 +344,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - private int insertDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, T... entitys) throws SQLException { + private int insertDBStatement(List stmtsRef, JdbcConnection conn, EntityInfo info, T... entitys) throws SQLException { final long s = System.currentTimeMillis(); int c = 0; String presql = null; @@ -479,7 +483,9 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { stmt = conn.createUpdateStatement(); for (String newCatalog : newCatalogs) { - stmt.addBatch(("postgresql".equals(dbtype()) ? "CREATE SCHEMA IF NOT EXISTS " : "CREATE DATABASE IF NOT EXISTS ") + newCatalog); + stmt.addBatch(("postgresql".equals(dbtype()) + ? "CREATE SCHEMA IF NOT EXISTS " + : "CREATE DATABASE IF NOT EXISTS ") + newCatalog); } stmt.executeBatch(); conn.offerUpdateStatement(stmt); @@ -648,12 +654,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture deleteDBAsync(final EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, final String... sqls) { + protected CompletableFuture deleteDBAsync(final EntityInfo info, String[] tables, + Flipper flipper, FilterNode node, Map> pkmap, final String... sqls) { return supplyAsync(() -> deleteDB(info, tables, flipper, node, pkmap, sqls)); } @Override - protected int deleteDB(EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, String... sqls) { + protected int deleteDB(EntityInfo info, String[] tables, Flipper flipper, + FilterNode node, Map> pkmap, String... sqls) { JdbcConnection conn = null; List stmtsRef = new ArrayList<>(); try { @@ -670,7 +678,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - private int deleteDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, String[] tables, Flipper flipper, FilterNode node, Map> pkmap, String... sqls) throws SQLException { + private int deleteDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, + String[] tables, Flipper flipper, FilterNode node, Map> pkmap, String... sqls) throws SQLException { final long s = System.currentTimeMillis(); Statement stmt = null; try { @@ -731,7 +740,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } if (logger.isLoggable(Level.FINE)) { - logger.log(Level.FINE, "delete, old-tables: " + Arrays.toString(oldTables) + ", new-tables: " + (pkmap != null ? pkmap.keySet() : Arrays.toString(tables))); + logger.log(Level.FINE, "delete, old-tables: " + Arrays.toString(oldTables) + + ", new-tables: " + (pkmap != null ? pkmap.keySet() : Arrays.toString(tables))); } if ((pkmap != null ? pkmap.size() : tables.length) == 0) { //分表全部不存在 return 0; @@ -907,7 +917,9 @@ public class DataJdbcSource extends AbstractDataSqlSource { c = stmt.executeUpdate(copyTableSql); } else { //需要先建库 String newCatalog = newTable.substring(0, newTable.indexOf('.')); - String catalogSql = ("postgresql".equals(dbtype()) ? "CREATE SCHEMA IF NOT EXISTS " : "CREATE DATABASE IF NOT EXISTS ") + newCatalog; + String catalogSql = ("postgresql".equals(dbtype()) + ? "CREATE SCHEMA IF NOT EXISTS " + : "CREATE DATABASE IF NOT EXISTS ") + newCatalog; try { if (info.isLoggable(logger, Level.FINEST, catalogSql)) { logger.finest(info.getType().getSimpleName() + " createCatalog sql=" + catalogSql); @@ -1066,7 +1078,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - private int updateEntityDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, T... entitys) throws SQLException { + private int updateEntityDBStatement(List stmtsRef, JdbcConnection conn, EntityInfo info, T... entitys) throws SQLException { final long s = System.currentTimeMillis(); String presql = null; PreparedStatement prestmt = null; @@ -1245,7 +1257,9 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - private int updateColumnDBStatement(List stmtsRef, final JdbcConnection conn, final EntityInfo info, Flipper flipper, UpdateSqlInfo sql) throws SQLException { //String sql, boolean prepared, Object... blobs) { + private int updateColumnDBStatement(List stmtsRef, final JdbcConnection conn, + final EntityInfo info, Flipper flipper, UpdateSqlInfo sql) throws SQLException { //String sql, boolean prepared, Object... blobs) { + final long s = System.currentTimeMillis(); int c = -1; String firstTable = null; @@ -1355,7 +1369,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { sql.tables = Utility.remove(sql.tables, t); } if (logger.isLoggable(Level.FINE)) { - logger.log(Level.FINE, "updateColumn, old-tables: " + Arrays.toString(oldTables) + ", new-tables: " + Arrays.toString(sql.tables)); + logger.log(Level.FINE, "updateColumn, old-tables: " + Arrays.toString(oldTables) + + ", new-tables: " + Arrays.toString(sql.tables)); } if (sql.tables.length == 0) { //分表全部不存在 return 0; @@ -1397,12 +1412,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture> getNumberMapDBAsync(EntityInfo info, String[] tables, String sql, FilterNode node, FilterFuncColumn... columns) { + protected CompletableFuture> getNumberMapDBAsync(EntityInfo info, + String[] tables, String sql, FilterNode node, FilterFuncColumn... columns) { return supplyAsync(() -> getNumberMapDB(info, tables, sql, node, columns)); } @Override - protected Map getNumberMapDB(EntityInfo info, String[] tables, String sql, FilterNode node, FilterFuncColumn... columns) { + protected Map getNumberMapDB(EntityInfo info, + String[] tables, String sql, FilterNode node, FilterFuncColumn... columns) { JdbcConnection conn = null; final Map map = new HashMap<>(); final long s = System.currentTimeMillis(); @@ -1499,12 +1516,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture getNumberResultDBAsync(EntityInfo info, String[] tables, String sql, FilterFunc func, Number defVal, String column, FilterNode node) { + protected CompletableFuture getNumberResultDBAsync(EntityInfo info, String[] tables, + String sql, FilterFunc func, Number defVal, String column, FilterNode node) { return supplyAsync(() -> getNumberResultDB(info, tables, sql, func, defVal, column, node)); } @Override - protected Number getNumberResultDB(EntityInfo info, String[] tables, String sql, FilterFunc func, Number defVal, String column, FilterNode node) { + protected Number getNumberResultDB(EntityInfo info, String[] tables, + String sql, FilterFunc func, Number defVal, String column, FilterNode node) { JdbcConnection conn = null; final long s = System.currentTimeMillis(); Statement stmt = null; @@ -1547,7 +1566,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { tables = Utility.remove(tables, t); } if (logger.isLoggable(Level.FINE)) { - logger.log(Level.FINE, "getNumberResult, old-tables: " + Arrays.toString(oldTables) + ", new-tables: " + Arrays.toString(tables)); + logger.log(Level.FINE, "getNumberResult, old-tables: " + Arrays.toString(oldTables) + + ", new-tables: " + Arrays.toString(tables)); } if (tables.length == 0) { //分表全部不存在 return defVal; @@ -1587,12 +1607,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, String[] tables, String sql, String keyColumn, FilterFunc func, String funcColumn, FilterNode node) { + protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, + String[] tables, String sql, String keyColumn, FilterFunc func, String funcColumn, FilterNode node) { return supplyAsync(() -> queryColumnMapDB(info, tables, sql, keyColumn, func, funcColumn, node)); } @Override - protected Map queryColumnMapDB(EntityInfo info, String[] tables, String sql, String keyColumn, FilterFunc func, String funcColumn, FilterNode node) { + protected Map queryColumnMapDB(EntityInfo info, String[] tables, + String sql, String keyColumn, FilterFunc func, String funcColumn, FilterNode node) { JdbcConnection conn = null; final long s = System.currentTimeMillis(); Map rs = new LinkedHashMap<>(); @@ -1673,12 +1695,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, String[] tables, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { + protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, + String[] tables, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { return supplyAsync(() -> queryColumnMapDB(info, tables, sql, funcNodes, groupByColumns, node)); } @Override - protected Map queryColumnMapDB(EntityInfo info, String[] tables, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { + protected Map queryColumnMapDB(EntityInfo info, String[] tables, + String sql, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { JdbcConnection conn = null; Map rs = new LinkedHashMap<>(); final long s = System.currentTimeMillis(); @@ -1818,12 +1842,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture findDBAsync(EntityInfo info, String[] tables, String sql, boolean onlypk, SelectColumn selects, Serializable pk, FilterNode node) { + protected CompletableFuture findDBAsync(EntityInfo info, String[] tables, String sql, boolean onlypk, + SelectColumn selects, Serializable pk, FilterNode node) { return supplyAsync(() -> findDB(info, tables, sql, onlypk, selects, pk, node)); } @Override - protected T findDB(EntityInfo info, String[] tables, String sql, boolean onlypk, SelectColumn selects, Serializable pk, FilterNode node) { + protected T findDB(EntityInfo info, String[] tables, String sql, boolean onlypk, + SelectColumn selects, Serializable pk, FilterNode node) { JdbcConnection conn = null; final long s = System.currentTimeMillis(); PreparedStatement prestmt = null; @@ -1896,12 +1922,14 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture findColumnDBAsync(EntityInfo info, final String[] tables, String sql, boolean onlypk, String column, Serializable defValue, Serializable pk, FilterNode node) { + protected CompletableFuture findColumnDBAsync(EntityInfo info, final String[] tables, + String sql, boolean onlypk, String column, Serializable defValue, Serializable pk, FilterNode node) { return supplyAsync(() -> findColumnDB(info, tables, sql, onlypk, column, defValue, pk, node)); } @Override - protected Serializable findColumnDB(EntityInfo info, String[] tables, String sql, boolean onlypk, String column, Serializable defValue, Serializable pk, FilterNode node) { + protected Serializable findColumnDB(EntityInfo info, String[] tables, + String sql, boolean onlypk, String column, Serializable defValue, Serializable pk, FilterNode node) { JdbcConnection conn = null; final long s = System.currentTimeMillis(); PreparedStatement prestmt = null; @@ -1983,7 +2011,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture existsDBAsync(EntityInfo info, final String[] tables, String sql, boolean onlypk, Serializable pk, FilterNode node) { + protected CompletableFuture existsDBAsync(EntityInfo info, final String[] tables, + String sql, boolean onlypk, Serializable pk, FilterNode node) { return supplyAsync(() -> existsDB(info, tables, sql, onlypk, pk, node)); } @@ -2117,7 +2146,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected CompletableFuture> querySheetDBAsync(EntityInfo info, final boolean readCache, boolean needTotal, final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { + protected CompletableFuture> querySheetDBAsync(EntityInfo info, final boolean readCache, + boolean needTotal, final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { return supplyAsync(() -> querySheetDB(info, readCache, needTotal, distinct, selects, flipper, node)); } @@ -2156,7 +2186,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - protected Sheet querySheetDB(EntityInfo info, final boolean readCache, boolean needTotal, final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { + protected Sheet querySheetDB(EntityInfo info, final boolean readCache, boolean needTotal, + final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { if (!needTotal && !distinct && selects == null && flipper == null && node == null && info.getTableStrategy() == null) { return querySheetFullListDB(info); } @@ -2310,14 +2341,16 @@ public class DataJdbcSource extends AbstractDataSqlSource { String listSubSql; StringBuilder union = new StringBuilder(); if (tables.length == 1) { - listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + " FROM " + tables[0] + " a" + joinAndWhere; + listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + + " FROM " + tables[0] + " a" + joinAndWhere; } else { int b = 0; for (String table : tables) { if (union.length() > 0) { union.append(" UNION ALL "); } - union.append("SELECT ").append(info.getQueryColumns("a", selects)).append(" FROM ").append(table).append(" a").append(joinAndWhere); + union.append("SELECT ").append(info.getQueryColumns("a", selects)) + .append(" FROM ").append(table).append(" a").append(joinAndWhere); } listSubSql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getQueryColumns("a", selects) + " FROM (" + (union) + ") a"; } @@ -2329,15 +2362,18 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } else { if (readCache && info.isLoggable(logger, Level.FINEST, listSql)) { - logger.finest(info.getType().getSimpleName() + " query sql=" + listSql + (flipper == null || flipper.getLimit() < 1 ? "" : (" LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset()))); + logger.finest(info.getType().getSimpleName() + " query sql=" + listSql + + (flipper == null || flipper.getLimit() < 1 ? "" : (" LIMIT " + flipper.getLimit() + " OFFSET " + flipper.getOffset()))); } } if (mysqlOrPgsql && needTotal) { String countSubSql; if (tables.length == 1) { - countSubSql = "SELECT " + (distinct ? "DISTINCT COUNT(" + info.getQueryColumns("a", selects) + ")" : "COUNT(*)") + " FROM " + tables[0] + " a" + joinAndWhere; + countSubSql = "SELECT " + (distinct ? "DISTINCT COUNT(" + info.getQueryColumns("a", selects) + ")" : "COUNT(*)") + + " FROM " + tables[0] + " a" + joinAndWhere; } else { - countSubSql = "SELECT " + (distinct ? "DISTINCT COUNT(" + info.getQueryColumns("a", selects) + ")" : "COUNT(*)") + " FROM (" + (union) + ") a"; + countSubSql = "SELECT " + (distinct ? "DISTINCT COUNT(" + info.getQueryColumns("a", selects) + ")" : "COUNT(*)") + + " FROM (" + (union) + ") a"; } countSql = countSubSql; if (readCache && info.isLoggable(logger, Level.FINEST, countSql)) { @@ -2498,7 +2534,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { conn.setAutoCommit(true); if (logger.isLoggable(Level.FINEST)) { - logger.finest("executeQuery sql=" + sql); + logger.finest("nativeQuery sql=" + sql); } final Statement stmt = conn.createQueryStatement(); if (consumer != null) { @@ -2526,7 +2562,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { conn.setAutoCommit(true); if (logger.isLoggable(Level.FINEST)) { - logger.finest("executeQuery sql=" + sinfo.getNativeSql()); + logger.finest("nativeQuery sql=" + sinfo.getNativeSql()); } V rs; if (sinfo.isEmptyNamed()) { @@ -2570,7 +2606,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { try { conn.setAutoCommit(true); if (logger.isLoggable(Level.FINEST)) { - logger.finest("executeQuery sql=" + sinfo.nativeSql); + logger.finest("nativeQuerySheet sql=" + sinfo.nativeSql); } long total = -1; List list; @@ -2633,7 +2669,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { } @Override - public CompletableFuture nativeQueryAsync(String sql, BiConsumer consumer, Function handler, Map params) { + public CompletableFuture nativeQueryAsync(String sql, BiConsumer consumer, + Function handler, Map params) { return supplyAsync(() -> nativeQuery(sql, consumer, handler, params)); } @@ -2912,7 +2949,8 @@ public class DataJdbcSource extends AbstractDataSqlSource { for (ResourceEvent event : events) { if (event.name().equals(DATA_SOURCE_URL) || event.name().endsWith("." + DATA_SOURCE_URL)) { newUrl = event.newValue().toString(); - } else if (event.name().equals(DATA_SOURCE_CONNECT_TIMEOUT_SECONDS) || event.name().endsWith("." + DATA_SOURCE_CONNECT_TIMEOUT_SECONDS)) { + } else if (event.name().equals(DATA_SOURCE_CONNECT_TIMEOUT_SECONDS) + || event.name().endsWith("." + DATA_SOURCE_CONNECT_TIMEOUT_SECONDS)) { newConnectTimeoutSeconds = Integer.decode(event.newValue().toString()); } else if (event.name().equals(DATA_SOURCE_USER) || event.name().endsWith("." + DATA_SOURCE_USER)) { newUser = event.newValue().toString(); diff --git a/src/main/java/org/redkale/source/DataNativeSqlInfo.java b/src/main/java/org/redkale/source/DataNativeSqlInfo.java index ec9f6884d..55f745884 100644 --- a/src/main/java/org/redkale/source/DataNativeSqlInfo.java +++ b/src/main/java/org/redkale/source/DataNativeSqlInfo.java @@ -28,6 +28,7 @@ public class DataNativeSqlInfo { //sql类型 protected SqlMode sqlMode; + //根参数名, 如bean.userid、bean.username的根参数名为: bean protected final List rootParamNames = new ArrayList<>(); @ConvertDisabled @@ -52,6 +53,6 @@ public class DataNativeSqlInfo { } public enum SqlMode { - SELECT, INSERT, DELETE, UPDATE, UPSERT, OTHERS; + SELECT, INSERT, DELETE, UPDATE, OTHERS; } }