diff --git a/src/main/java/org/redkale/source/AbstractDataSource.java b/src/main/java/org/redkale/source/AbstractDataSource.java index 99651807e..937e01fb1 100644 --- a/src/main/java/org/redkale/source/AbstractDataSource.java +++ b/src/main/java/org/redkale/source/AbstractDataSource.java @@ -365,15 +365,10 @@ public abstract class AbstractDataSource extends AbstractService implements Data * * @param 泛型 * @param action 操作 - * @param async 是否异步 * @param entitys 对象集合 * - * @return CompletableFuture */ - protected CompletableFuture checkEntity(String action, boolean async, T... entitys) { - if (entitys.length < 1) { - return null; - } + protected void checkEntity(String action, T... entitys) { Class clazz = null; for (T val : entitys) { if (clazz == null) { @@ -381,18 +376,14 @@ public abstract class AbstractDataSource extends AbstractService implements Data if (clazz.getAnnotation(Entity.class) == null && clazz.getAnnotation(javax.persistence.Entity.class) == null) { throw new SourceException("Entity Class " + clazz + " must be on Annotation @Entity"); } - continue; - } - if (clazz != val.getClass()) { - if (async) { - CompletableFuture future = new CompletableFuture<>(); - future.completeExceptionally(new RuntimeException("DataSource." + action + " must the same Class Entity, but diff is " + clazz + " and " + val.getClass())); - return future; - } + } else if (clazz != val.getClass()) { throw new SourceException("DataSource." + action + " must the same Class Entity, but diff is " + clazz + " and " + val.getClass()); } } - return null; + } + + protected CompletableFuture supplyAsync(Supplier supplier) { + return CompletableFuture.supplyAsync(supplier, getExecutor()); } protected DefaultBatchInfo parseBatchInfo(DefaultDataBatch batch, Function func) { diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index 63379dc20..86ba26ef3 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -202,7 +202,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture deleteDB(EntityInfo info, Flipper flipper, String... sqls) { + protected CompletableFuture deleteDBAsync(final EntityInfo info, Flipper flipper, final String... sqls) { + return supplyAsync(() -> deleteDB(info, flipper, sqls)); + } + + @Override + protected int deleteDB(EntityInfo info, Flipper flipper, String... sqls) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -250,7 +255,7 @@ public class DataJdbcSource extends DataSqlSource { } conn.commit(); slowLog(s, sqls); - return CompletableFuture.completedFuture(c); + return c; } catch (SQLException e) { if (conn != null) { try { @@ -273,16 +278,16 @@ public class DataJdbcSource extends DataSqlSource { st.executeBatch(); } st.close(); - return CompletableFuture.completedFuture(0); + return 0; } catch (SQLException e2) { - return CompletableFuture.failedFuture(e2); + throw new SourceException(e2); } } } else { - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { writePool.offerConnection(conn); @@ -291,7 +296,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture clearTableDB(EntityInfo info, final String[] tables, String... sqls) { + protected CompletableFuture clearTableDBAsync(EntityInfo info, final String[] tables, String... sqls) { + return supplyAsync(() -> clearTableDB(info, tables, sqls)); + } + + @Override + protected int clearTableDB(EntityInfo info, final String[] tables, String... sqls) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -315,7 +325,7 @@ public class DataJdbcSource extends DataSqlSource { stmt.close(); conn.commit(); slowLog(s, sqls); - return CompletableFuture.completedFuture(c); + return c; } catch (SQLException e) { if (conn != null) { try { @@ -324,9 +334,9 @@ public class DataJdbcSource extends DataSqlSource { } } if (isTableNotExist(info, e.getSQLState())) { - return CompletableFuture.completedFuture(-1); + return -1; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { writePool.offerConnection(conn); @@ -335,7 +345,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture dropTableDB(EntityInfo info, String[] tables, String... sqls) { + protected CompletableFuture dropTableDBAsync(EntityInfo info, final String[] tables, String... sqls) { + return supplyAsync(() -> dropTableDB(info, tables, sqls)); + } + + @Override + protected int dropTableDB(EntityInfo info, String[] tables, String... sqls) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -365,7 +380,7 @@ public class DataJdbcSource extends DataSqlSource { } } slowLog(s, sqls); - return CompletableFuture.completedFuture(c); + return c; } catch (SQLException e) { if (conn != null) { try { @@ -374,9 +389,9 @@ public class DataJdbcSource extends DataSqlSource { } } if (isTableNotExist(info, e.getSQLState())) { - return CompletableFuture.completedFuture(-1); + return -1; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { writePool.offerConnection(conn); @@ -394,7 +409,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture insertDB(EntityInfo info, T... entitys) { + protected CompletableFuture insertDBAsync(EntityInfo info, T... entitys) { + return supplyAsync(() -> insertDB(info, entitys)); + } + + @Override + protected int insertDB(EntityInfo info, T... entitys) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -645,7 +665,7 @@ public class DataJdbcSource extends DataSqlSource { }); slowLog(s, presqls.toArray(new String[presqls.size()])); } - return CompletableFuture.completedFuture(c); + return c; } catch (SQLException e) { if (conn != null) { try { @@ -653,7 +673,7 @@ public class DataJdbcSource extends DataSqlSource { } catch (SQLException se) { } } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { writePool.offerConnection(conn); @@ -662,7 +682,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture updateEntityDB(EntityInfo info, T... entitys) { + protected CompletableFuture updateEntityDBAsync(EntityInfo info, T... entitys) { + return supplyAsync(() -> updateEntityDB(info, entitys)); + } + + @Override + protected int updateEntityDB(EntityInfo info, T... entitys) { Connection conn = null; final long s = System.currentTimeMillis(); String presql = null; @@ -726,11 +751,11 @@ public class DataJdbcSource extends DataSqlSource { } } //表不存在,更新条数为0 - return CompletableFuture.completedFuture(0); + return 0; } else { String tableName = parseNotExistTableName(se); if (tableName == null || prepareInfos == null) { - return CompletableFuture.failedFuture(se); + throw new SourceException(se); } for (PreparedStatement stmt : prestmts) { stmt.close(); @@ -739,7 +764,7 @@ public class DataJdbcSource extends DataSqlSource { String[] oldTables = prepareInfos.keySet().toArray(new String[prepareInfos.size()]); List notExistTables = checkNotExistTables(conn, oldTables, tableName); if (notExistTables.isEmpty()) { - return CompletableFuture.failedFuture(se); + throw new SourceException(se); } for (String t : notExistTables) { prepareInfos.remove(t); @@ -748,7 +773,7 @@ public class DataJdbcSource extends DataSqlSource { logger.log(Level.FINE, "update entitys, old-tables: " + Arrays.toString(oldTables) + ", new-tables: " + prepareInfos.keySet()); } if (prepareInfos.isEmpty()) { //分表全部不存在 - return CompletableFuture.completedFuture(0); + return 0; } prestmts = createUpdatePreparedStatements(conn, info, prepareInfos, entitys); int c1 = 0; @@ -830,7 +855,9 @@ public class DataJdbcSource extends DataSqlSource { }); slowLog(s, presqls.toArray(new String[presqls.size()])); } - return CompletableFuture.completedFuture(c); + return c; + } catch (SourceException se) { + throw se; } catch (SQLException e) { if (conn != null) { try { @@ -838,7 +865,7 @@ public class DataJdbcSource extends DataSqlSource { } catch (SQLException se) { } } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { writePool.offerConnection(conn); @@ -847,7 +874,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture updateColumnDB(EntityInfo info, Flipper flipper, SqlInfo sql) { //String sql, boolean prepared, Object... blobs) { + protected CompletableFuture updateColumnDBAsync(EntityInfo info, Flipper flipper, SqlInfo sql) { + return supplyAsync(() -> updateColumnDB(info, flipper, sql)); + } + + @Override + protected int updateColumnDB(EntityInfo info, Flipper flipper, SqlInfo sql) { //String sql, boolean prepared, Object... blobs) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -886,7 +918,7 @@ public class DataJdbcSource extends DataSqlSource { prestmt.close(); conn.commit(); slowLog(s, sql.sql); - return CompletableFuture.completedFuture(c); + return c; } else { if (info.isLoggable(logger, Level.FINEST, sql.sql)) { logger.finest(info.getType().getSimpleName() + " update sql=" + sql); @@ -896,7 +928,7 @@ public class DataJdbcSource extends DataSqlSource { stmt.close(); conn.commit(); slowLog(s, sql.sql); - return CompletableFuture.completedFuture(c); + return c; } } catch (SQLException e) { if (conn != null) { @@ -923,12 +955,12 @@ public class DataJdbcSource extends DataSqlSource { } catch (SQLException e2) { } } - return CompletableFuture.completedFuture(0); + return 0; } else { - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { writePool.offerConnection(conn); @@ -937,7 +969,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture> getNumberMapDB(EntityInfo info, String sql, FilterFuncColumn... columns) { + protected CompletableFuture> getNumberMapDBAsync(EntityInfo info, String sql, FilterFuncColumn... columns) { + return supplyAsync(() -> getNumberMapDB(info, sql, columns)); + } + + @Override + protected Map getNumberMapDB(EntityInfo info, String sql, FilterFuncColumn... columns) { Connection conn = null; final Map map = new HashMap<>(); final long s = System.currentTimeMillis(); @@ -962,7 +999,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); stmt.close(); slowLog(s, sql); - return CompletableFuture.completedFuture(map); + return map; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -983,9 +1020,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(map); + return map; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -994,7 +1031,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture getNumberResultDB(EntityInfo info, String sql, Number defVal, String column) { + protected CompletableFuture getNumberResultDBAsync(EntityInfo info, String sql, Number defVal, String column) { + return supplyAsync(() -> getNumberResultDB(info, sql, defVal, column)); + } + + @Override + protected Number getNumberResultDB(EntityInfo info, String sql, Number defVal, String column) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -1012,7 +1054,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); stmt.close(); slowLog(s, sql); - return CompletableFuture.completedFuture(rs); + return rs; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -1033,9 +1075,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(defVal); + return defVal; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1044,7 +1086,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture> queryColumnMapDB(EntityInfo info, String sql, String keyColumn) { + protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, String sql, String keyColumn) { + return supplyAsync(() -> queryColumnMapDB(info, sql, keyColumn)); + } + + @Override + protected Map queryColumnMapDB(EntityInfo info, String sql, String keyColumn) { Connection conn = null; final long s = System.currentTimeMillis(); Map rs = new LinkedHashMap<>(); @@ -1061,7 +1108,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); stmt.close(); slowLog(s, sql); - return CompletableFuture.completedFuture(rs); + return rs; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -1082,9 +1129,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(rs); + return rs; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1093,7 +1140,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture> queryColumnMapDB(EntityInfo info, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { + protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { + return supplyAsync(() -> queryColumnMapDB(info, sql, funcNodes, groupByColumns)); + } + + @Override + protected Map queryColumnMapDB(EntityInfo info, String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { Connection conn = null; Map rs = new LinkedHashMap<>(); final long s = System.currentTimeMillis(); @@ -1125,7 +1177,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); stmt.close(); slowLog(s, sql); - return CompletableFuture.completedFuture(rs); + return rs; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -1146,9 +1198,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(rs); + return rs; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1157,7 +1209,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture findDB(EntityInfo info, String sql, boolean onlypk, SelectColumn selects) { + protected CompletableFuture findDBAsync(EntityInfo info, String sql, boolean onlypk, SelectColumn selects) { + return supplyAsync(() -> findDB(info, sql, onlypk, selects)); + } + + @Override + protected T findDB(EntityInfo info, String sql, boolean onlypk, SelectColumn selects) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -1170,7 +1227,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); ps.close(); slowLog(s, sql); - return CompletableFuture.completedFuture(rs); + return rs; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -1191,9 +1248,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(null); + return null; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1202,7 +1259,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture findColumnDB(EntityInfo info, String sql, boolean onlypk, String column, Serializable defValue) { + protected CompletableFuture findColumnDBAsync(EntityInfo info, String sql, boolean onlypk, String column, Serializable defValue) { + return supplyAsync(() -> findColumnDB(info, sql, onlypk, column, defValue)); + } + + @Override + protected Serializable findColumnDB(EntityInfo info, String sql, boolean onlypk, String column, Serializable defValue) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -1219,7 +1281,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); ps.close(); slowLog(s, sql); - return CompletableFuture.completedFuture(val == null ? defValue : val); + return val == null ? defValue : val; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -1240,9 +1302,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(null); + return defValue; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1251,7 +1313,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture existsDB(EntityInfo info, String sql, boolean onlypk) { + protected CompletableFuture existsDBAsync(EntityInfo info, String sql, boolean onlypk) { + return supplyAsync(() -> existsDB(info, sql, onlypk)); + } + + @Override + protected boolean existsDB(EntityInfo info, String sql, boolean onlypk) { Connection conn = null; final long s = System.currentTimeMillis(); try { @@ -1266,7 +1333,7 @@ public class DataJdbcSource extends DataSqlSource { logger.finest(info.getType().getSimpleName() + " exists (" + rs + ") sql=" + sql); } slowLog(s, sql); - return CompletableFuture.completedFuture(rs); + return rs; } catch (SQLException e) { if (isTableNotExist(info, e.getSQLState())) { if (info.getTableStrategy() == null) { @@ -1287,9 +1354,9 @@ public class DataJdbcSource extends DataSqlSource { } } } - return CompletableFuture.completedFuture(false); + return false; } - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1298,7 +1365,12 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture> querySheetDB(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)); + } + + @Override + protected Sheet querySheetDB(EntityInfo info, final boolean readCache, boolean needTotal, final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { Connection conn = null; final long s = System.currentTimeMillis(); final SelectColumn sels = selects; @@ -1334,20 +1406,20 @@ public class DataJdbcSource extends DataSqlSource { } catch (SQLException e2) { } } - return CompletableFuture.completedFuture(new Sheet<>(0, new ArrayList())); + return new Sheet<>(0, new ArrayList()); } else if (tables != null && tables.length == 1) { //只查一个不存在的分表 - return CompletableFuture.completedFuture(new Sheet<>(0, new ArrayList())); + return new Sheet<>(0, new ArrayList()); } else if (tables != null && tables.length > 1) { //多分表查询中一个或多个分表不存在 String tableName = parseNotExistTableName(se); if (tableName == null) { - return CompletableFuture.failedFuture(se); + throw new SourceException(se); } String[] oldTables = tables; List notExistTables = checkNotExistTables(conn, tables, tableName); if (notExistTables.isEmpty()) { - return CompletableFuture.failedFuture(se); + throw new SourceException(se); } for (String t : notExistTables) { tables = Utility.remove(tables, t); @@ -1356,7 +1428,7 @@ public class DataJdbcSource extends DataSqlSource { logger.log(Level.FINE, "query sheet, old-tables: " + Arrays.toString(oldTables) + ", new-tables: " + Arrays.toString(tables)); } if (tables.length == 0) { //分表全部不存在 - return CompletableFuture.completedFuture(new Sheet<>(0, new ArrayList())); + return new Sheet<>(0, new ArrayList()); } //重新查询一次 @@ -1365,13 +1437,15 @@ public class DataJdbcSource extends DataSqlSource { countSql = sqls[1]; return executeQuerySheet(info, needTotal, flipper, sels, s, conn, mysqlOrPgsql, listSql, countSql); } else { - return CompletableFuture.failedFuture(se); + throw new SourceException(se); } } - return CompletableFuture.failedFuture(se); + throw new SourceException(se); } + } catch (SourceException se) { + throw se; } catch (Exception e) { - return CompletableFuture.failedFuture(e); + throw new SourceException(e); } finally { if (conn != null) { readPool.offerConnection(conn); @@ -1379,7 +1453,7 @@ public class DataJdbcSource extends DataSqlSource { } } - private CompletableFuture> executeQuerySheet(EntityInfo info, boolean needTotal, Flipper flipper, SelectColumn sels, + private Sheet executeQuerySheet(EntityInfo info, boolean needTotal, Flipper flipper, SelectColumn sels, long s, Connection conn, boolean mysqlOrPgsql, String listSql, String countSql) throws SQLException { final List list = new ArrayList(); if (mysqlOrPgsql) { //sql可以带limit、offset @@ -1402,7 +1476,7 @@ public class DataJdbcSource extends DataSqlSource { ps.close(); } slowLog(s, listSql); - return CompletableFuture.completedFuture(new Sheet<>(total, list)); + return new Sheet<>(total, list); } else { //conn.setReadOnly(true); PreparedStatement ps = conn.prepareStatement(listSql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); @@ -1441,7 +1515,7 @@ public class DataJdbcSource extends DataSqlSource { set.close(); ps.close(); slowLog(s, listSql); - return CompletableFuture.completedFuture(new Sheet<>(total, list)); + return new Sheet<>(total, list); } } diff --git a/src/main/java/org/redkale/source/DataMemorySource.java b/src/main/java/org/redkale/source/DataMemorySource.java index 6962f4c84..8e4bfb4b1 100644 --- a/src/main/java/org/redkale/source/DataMemorySource.java +++ b/src/main/java/org/redkale/source/DataMemorySource.java @@ -108,7 +108,7 @@ public class DataMemorySource extends DataSqlSource implements SearchSource { @Override protected boolean isAsync() { - return true; + return false; } @Override @@ -117,72 +117,142 @@ public class DataMemorySource extends DataSqlSource implements SearchSource { } @Override - protected CompletableFuture insertDB(EntityInfo info, T... entitys) { + protected int insertDB(EntityInfo info, T... entitys) { + return 0; + } + + @Override + protected int deleteDB(EntityInfo info, Flipper flipper, String... sqls) { + return 0; + } + + @Override + protected int clearTableDB(EntityInfo info, String[] tables, String... sqls) { + return 0; + } + + @Override + protected int dropTableDB(EntityInfo info, String[] tables, String... sqls) { + return 0; + } + + @Override + protected int updateEntityDB(EntityInfo info, T... entitys) { + return 0; + } + + @Override + protected int updateColumnDB(EntityInfo info, Flipper flipper, SqlInfo sql) { + return 0; + } + + @Override + protected Map getNumberMapDB(EntityInfo info, String sql, FilterFuncColumn... columns) { + return null; + } + + @Override + protected Number getNumberResultDB(EntityInfo info, String sql, Number defVal, String column) { + return defVal; + } + + @Override + protected Map queryColumnMapDB(EntityInfo info, String sql, String keyColumn) { + return null; + } + + @Override + protected Map queryColumnMapDB(final EntityInfo info, final String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { + return null; + } + + @Override + protected T findDB(EntityInfo info, String sql, boolean onlypk, SelectColumn selects) { + return null; + } + + @Override + protected Serializable findColumnDB(EntityInfo info, String sql, boolean onlypk, String column, Serializable defValue) { + return null; + } + + @Override + protected boolean existsDB(EntityInfo info, String sql, boolean onlypk) { + return false; + } + + @Override + protected Sheet querySheetDB(EntityInfo info, final boolean readCache, boolean needTotal, final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { + return new Sheet<>(0, new ArrayList()); + } + + @Override + protected CompletableFuture insertDBAsync(EntityInfo info, T... entitys) { return CompletableFuture.completedFuture(0); } @Override - protected CompletableFuture deleteDB(EntityInfo info, Flipper flipper, String... sqls) { + protected CompletableFuture deleteDBAsync(EntityInfo info, Flipper flipper, String... sqls) { return CompletableFuture.completedFuture(0); } @Override - protected CompletableFuture clearTableDB(EntityInfo info, String[] tables, String... sqls) { + protected CompletableFuture clearTableDBAsync(EntityInfo info, String[] tables, String... sqls) { return CompletableFuture.completedFuture(0); } @Override - protected CompletableFuture dropTableDB(EntityInfo info, String[] tables, String... sqls) { + protected CompletableFuture dropTableDBAsync(EntityInfo info, String[] tables, String... sqls) { return CompletableFuture.completedFuture(0); } @Override - protected CompletableFuture updateEntityDB(EntityInfo info, T... entitys) { + protected CompletableFuture updateEntityDBAsync(EntityInfo info, T... entitys) { return CompletableFuture.completedFuture(0); } @Override - protected CompletableFuture updateColumnDB(EntityInfo info, Flipper flipper, SqlInfo sql) { + protected CompletableFuture updateColumnDBAsync(EntityInfo info, Flipper flipper, SqlInfo sql) { return CompletableFuture.completedFuture(0); } @Override - protected CompletableFuture> getNumberMapDB(EntityInfo info, String sql, FilterFuncColumn... columns) { + protected CompletableFuture> getNumberMapDBAsync(EntityInfo info, String sql, FilterFuncColumn... columns) { return CompletableFuture.completedFuture(null); } @Override - protected CompletableFuture getNumberResultDB(EntityInfo info, String sql, Number defVal, String column) { + protected CompletableFuture getNumberResultDBAsync(EntityInfo info, String sql, Number defVal, String column) { return CompletableFuture.completedFuture(defVal); } @Override - protected CompletableFuture> queryColumnMapDB(EntityInfo info, String sql, String keyColumn) { + protected CompletableFuture> queryColumnMapDBAsync(EntityInfo info, String sql, String keyColumn) { return CompletableFuture.completedFuture(null); } @Override - protected CompletableFuture> queryColumnMapDB(final EntityInfo info, final String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { + protected CompletableFuture> queryColumnMapDBAsync(final EntityInfo info, final String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { return CompletableFuture.completedFuture(null); } @Override - protected CompletableFuture findDB(EntityInfo info, String sql, boolean onlypk, SelectColumn selects) { + protected CompletableFuture findDBAsync(EntityInfo info, String sql, boolean onlypk, SelectColumn selects) { return CompletableFuture.completedFuture(null); } @Override - protected CompletableFuture findColumnDB(EntityInfo info, String sql, boolean onlypk, String column, Serializable defValue) { + protected CompletableFuture findColumnDBAsync(EntityInfo info, String sql, boolean onlypk, String column, Serializable defValue) { return CompletableFuture.completedFuture(null); } @Override - protected CompletableFuture existsDB(EntityInfo info, String sql, boolean onlypk) { + protected CompletableFuture existsDBAsync(EntityInfo info, String sql, boolean onlypk) { return CompletableFuture.completedFuture(false); } @Override - protected CompletableFuture> querySheetDB(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 CompletableFuture.completedFuture(new Sheet<>(0, new ArrayList())); } diff --git a/src/main/java/org/redkale/source/DataSqlSource.java b/src/main/java/org/redkale/source/DataSqlSource.java index 7a6af58cd..5889e0b44 100644 --- a/src/main/java/org/redkale/source/DataSqlSource.java +++ b/src/main/java/org/redkale/source/DataSqlSource.java @@ -73,7 +73,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi }; protected final BiFunction> fullloader = (s, i) - -> ((CompletableFuture) querySheetDB(i, false, false, false, null, null, (FilterNode) null)).thenApply(e -> e == null ? new ArrayList() : e.list(true)); + -> ((CompletableFuture) querySheetDBAsync(i, false, false, false, null, null, (FilterNode) null)).thenApply(e -> e == null ? new ArrayList() : e.list(true)); //超过多少毫秒视为较慢, 会打印警告级别的日志, 默认值: 2000 protected long slowmsWarn; @@ -145,12 +145,10 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return; } //不支持读写分离模式的动态切换 - if (readConfProps == writeConfProps - && (events[0].name().startsWith("read.") || events[0].name().startsWith("write."))) { + if (readConfProps == writeConfProps && (events[0].name().startsWith("read.") || events[0].name().startsWith("write."))) { throw new SourceException("DataSource(name=" + resourceName() + ") not support to change to read/write separation mode"); } - if (readConfProps != writeConfProps - && (!events[0].name().startsWith("read.") && !events[0].name().startsWith("write."))) { + if (readConfProps != writeConfProps && (!events[0].name().startsWith("read.") && !events[0].name().startsWith("write."))) { throw new SourceException("DataSource(name=" + resourceName() + ") not support to change to non read/write separation mode"); } @@ -163,7 +161,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi String newValue = decryptProperty(event.name(), event.newValue().toString()); allEvents.add(ResourceEvent.create(event.name(), newValue, event.oldValue())); newProps.put(event.name(), newValue); - sb.append("DataSource(name=").append(resourceName()).append(") change '").append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); + sb.append("DataSource(name=").append(resourceName()).append(") change '") + .append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); } updateOneResourceChange(newProps, allEvents.toArray(new ResourceEvent[allEvents.size()])); for (ResourceEvent event : allEvents) { @@ -188,7 +187,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi writeEvents.add(ResourceEvent.create(newName, newValue, event.oldValue())); newWriteProps.put(event.name(), newValue); } - sb.append("DataSource(name=").append(resourceName()).append(") change '").append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); + sb.append("DataSource(name=").append(resourceName()).append(") change '") + .append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); } if (!readEvents.isEmpty()) { updateReadResourceChange(newReadProps, readEvents.toArray(new ResourceEvent[readEvents.size()])); @@ -559,10 +559,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi @Local protected boolean isTableNotExist(EntityInfo info, String sqlCode) { - if (sqlCode == null || sqlCode.isEmpty()) { - return false; - } - return tableNotExistSqlstates.contains(';' + sqlCode + ';'); + return sqlCode != null && !sqlCode.isEmpty() && tableNotExistSqlstates.contains(';' + sqlCode + ';'); } @Local @@ -667,46 +664,116 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi protected abstract String prepareParamSign(int index); //插入纪录 - protected abstract CompletableFuture insertDB(final EntityInfo info, T... entitys); + protected abstract CompletableFuture insertDBAsync(final EntityInfo info, T... entitys); //删除记录 - protected abstract CompletableFuture deleteDB(final EntityInfo info, Flipper flipper, final String... sqls); + protected abstract CompletableFuture deleteDBAsync(final EntityInfo info, Flipper flipper, final String... sqls); //清空表 - protected abstract CompletableFuture clearTableDB(final EntityInfo info, String[] tables, final String... sqls); + protected abstract CompletableFuture clearTableDBAsync(final EntityInfo info, String[] tables, final String... sqls); //删除表 - protected abstract CompletableFuture dropTableDB(final EntityInfo info, String[] tables, final String... sqls); + protected abstract CompletableFuture dropTableDBAsync(final EntityInfo info, String[] tables, final String... sqls); //更新纪录 - protected abstract CompletableFuture updateEntityDB(final EntityInfo info, T... entitys); + protected abstract CompletableFuture updateEntityDBAsync(final EntityInfo info, T... entitys); //更新纪录 - protected abstract CompletableFuture updateColumnDB(final EntityInfo info, Flipper flipper, final SqlInfo sql); + protected abstract CompletableFuture updateColumnDBAsync(final EntityInfo info, Flipper flipper, final SqlInfo sql); //查询Number Map数据 - protected abstract CompletableFuture> getNumberMapDB(final EntityInfo info, final String sql, final FilterFuncColumn... columns); + protected abstract CompletableFuture> getNumberMapDBAsync(final EntityInfo info, final String sql, final FilterFuncColumn... columns); //查询Number数据 - protected abstract CompletableFuture getNumberResultDB(final EntityInfo info, final String sql, final Number defVal, final String column); + protected abstract CompletableFuture getNumberResultDBAsync(final EntityInfo info, final String sql, final Number defVal, final String column); //查询Map数据 - protected abstract CompletableFuture> queryColumnMapDB(final EntityInfo info, final String sql, final String keyColumn); + protected abstract CompletableFuture> queryColumnMapDBAsync(final EntityInfo info, final String sql, final String keyColumn); //查询Map数据 - protected abstract CompletableFuture> queryColumnMapDB(final EntityInfo info, final String sql, final ColumnNode[] funcNodes, final String[] groupByColumns); + protected abstract CompletableFuture> queryColumnMapDBAsync(final EntityInfo info, final String sql, final ColumnNode[] funcNodes, final String[] groupByColumns); //查询单条记录 - protected abstract CompletableFuture findDB(final EntityInfo info, final String sql, final boolean onlypk, final SelectColumn selects); + protected abstract CompletableFuture findDBAsync(final EntityInfo info, final String sql, final boolean onlypk, final SelectColumn selects); //查询单条记录的单个字段 - protected abstract CompletableFuture findColumnDB(final EntityInfo info, final String sql, final boolean onlypk, final String column, final Serializable defValue); + protected abstract CompletableFuture findColumnDBAsync(final EntityInfo info, final String sql, final boolean onlypk, final String column, final Serializable defValue); //判断记录是否存在 - protected abstract CompletableFuture existsDB(final EntityInfo info, final String sql, final boolean onlypk); + protected abstract CompletableFuture existsDBAsync(final EntityInfo info, final String sql, final boolean onlypk); //查询一页数据 - protected abstract CompletableFuture> querySheetDB(final EntityInfo info, final boolean readcache, final boolean needtotal, final boolean distinct, final SelectColumn selects, final Flipper flipper, final FilterNode node); + protected abstract CompletableFuture> querySheetDBAsync(final EntityInfo info, final boolean readcache, final boolean needtotal, final boolean distinct, final SelectColumn selects, final Flipper flipper, final FilterNode node); + + //插入纪录 + protected int insertDB(final EntityInfo info, T... entitys) { + return insertDBAsync(info, entitys).join(); + } + + //删除记录 + protected int deleteDB(final EntityInfo info, Flipper flipper, final String... sqls) { + return deleteDBAsync(info, flipper, sqls).join(); + } + + //清空表 + protected int clearTableDB(final EntityInfo info, String[] tables, final String... sqls) { + return clearTableDBAsync(info, tables, sqls).join(); + } + + //删除表 + protected int dropTableDB(final EntityInfo info, String[] tables, final String... sqls) { + return dropTableDBAsync(info, tables, sqls).join(); + } + + //更新纪录 + protected int updateEntityDB(final EntityInfo info, T... entitys) { + return updateEntityDBAsync(info, entitys).join(); + } + + //更新纪录 + protected int updateColumnDB(final EntityInfo info, Flipper flipper, final SqlInfo sql) { + return updateColumnDBAsync(info, flipper, sql).join(); + } + + //查询Number Map数据 + protected Map getNumberMapDB(final EntityInfo info, final String sql, final FilterFuncColumn... columns) { + return (Map) getNumberMapDBAsync(info, sql, columns).join(); + } + + //查询Number数据 + protected Number getNumberResultDB(final EntityInfo info, final String sql, final Number defVal, final String column) { + return getNumberResultDBAsync(info, sql, defVal, column).join(); + } + + //查询Map数据 + protected Map queryColumnMapDB(final EntityInfo info, final String sql, final String keyColumn) { + return (Map) queryColumnMapDBAsync(info, sql, keyColumn).join(); + } + + //查询Map数据 + protected Map queryColumnMapDB(final EntityInfo info, final String sql, final ColumnNode[] funcNodes, final String[] groupByColumns) { + return (Map) queryColumnMapDBAsync(info, sql, funcNodes, groupByColumns).join(); + } + + //查询单条记录 + protected T findDB(final EntityInfo info, final String sql, final boolean onlypk, final SelectColumn selects) { + return findDBAsync(info, sql, onlypk, selects).join(); + } + + //查询单条记录的单个字段 + protected Serializable findColumnDB(final EntityInfo info, final String sql, final boolean onlypk, final String column, final Serializable defValue) { + return findColumnDBAsync(info, sql, onlypk, column, defValue).join(); + } + + //判断记录是否存在 + protected boolean existsDB(final EntityInfo info, final String sql, final boolean onlypk) { + return existsDBAsync(info, sql, onlypk).join(); + } + + //查询一页数据 + protected Sheet querySheetDB(final EntityInfo info, final boolean readcache, final boolean needtotal, final boolean distinct, final SelectColumn selects, final Flipper flipper, final FilterNode node) { + return querySheetDBAsync(info, readcache, needtotal, distinct, selects, flipper, node).join(); + } protected CharSequence createSQLJoin(FilterNode node, final Function func, final boolean update, final Map joinTabalis, final Set haset, final EntityInfo info) { return node == null ? null : node.createSQLJoin(func, update, joinTabalis, haset, info); @@ -763,10 +830,10 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } protected CharSequence formatValueToString(final EntityInfo info, Object value) { + if (value == null) { + return null; + } if ("mysql".equals(dbtype)) { - if (value == null) { - return null; - } if (value instanceof CharSequence) { return new StringBuilder().append('\'').append(value.toString().replace("\\", "\\\\").replace("'", "\\'")).append('\'').toString(); } else if (!(value instanceof Number) && !(value instanceof java.util.Date) @@ -798,18 +865,20 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (entitys.length == 0) { return 0; } - checkEntity("insert", false, entitys); + checkEntity("insert", entitys); final EntityInfo info = loadEntityInfo((Class) entitys[0].getClass()); if (isOnlyCache(info)) { return insertCache(info, entitys); } - return insertDB(info, entitys).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - insertCache(info, entitys); - } - }).join(); + if (isAsync()) { + int rs = insertDBAsync(info, entitys).join(); + insertCache(info, entitys); + return rs; + } else { + int rs = insertDB(info, entitys); + insertCache(info, entitys); + return rs; + } } @Override @@ -817,16 +886,21 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (entitys.length == 0) { return CompletableFuture.completedFuture(0); } - CompletableFuture future = checkEntity("insert", true, entitys); - if (future != null) { - return future; - } + checkEntity("insert", entitys); final EntityInfo info = loadEntityInfo((Class) entitys[0].getClass()); if (isOnlyCache(info)) { return CompletableFuture.completedFuture(insertCache(info, entitys)); } if (isAsync()) { - return insertDB(info, entitys).whenComplete((rs, t) -> { + return insertDBAsync(info, entitys).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + insertCache(info, entitys); + } + }); + } else { + return supplyAsync(() -> insertDB(info, entitys)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -834,13 +908,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> insertDB(info, entitys).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - insertCache(info, entitys); - } - }); } protected int insertCache(final EntityInfo info, T... entitys) { @@ -869,7 +936,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (entitys.length == 0) { return -1; } - checkEntity("delete", false, entitys); + checkEntity("delete", entitys); final Class clazz = (Class) entitys[0].getClass(); final EntityInfo info = loadEntityInfo(clazz); final Attribute primary = info.getPrimary(); @@ -886,10 +953,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (entitys.length == 0) { return CompletableFuture.completedFuture(-1); } - CompletableFuture future = checkEntity("delete", true, entitys); - if (future != null) { - return future; - } + checkEntity("delete", entitys); final Class clazz = (Class) entitys[0].getClass(); final EntityInfo info = loadEntityInfo(clazz); final Attribute primary = info.getPrimary(); @@ -910,13 +974,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return deleteCache(info, -1, pks); } - return deleteCompose(info, pks).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - deleteCache(info, rs, pks); - } - }).join(); + String[] sqls = deleteSql(info, pks); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " delete sql=" + Arrays.toString(sqls)); + } + if (isAsync()) { + int rs = deleteDBAsync(info, null, sqls).join(); + deleteCache(info, rs, pks); + return rs; + } else { + int rs = deleteDB(info, null, sqls); + deleteCache(info, rs, pks); + return rs; + } } @Override @@ -928,8 +998,20 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(deleteCache(info, -1, pks)); } + String[] sqls = deleteSql(info, pks); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " delete sql=" + Arrays.toString(sqls)); + } if (isAsync()) { - return deleteCompose(info, pks).whenComplete((rs, t) -> { + return deleteDBAsync(info, null, sqls).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + deleteCache(info, rs, pks); + } + }); + } else { + return supplyAsync(() -> deleteDB(info, null, sqls)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -937,13 +1019,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> deleteCompose(info, pks).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - deleteCache(info, rs, pks); - } - }); } @Override @@ -962,13 +1037,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return deleteCache(info, -1, flipper, node); } - return this.deleteCompose(info, flipper, node).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - deleteCache(info, rs, flipper, node); - } - }).join(); + String[] sqls = deleteSql(info, flipper, node); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " delete sql=" + Arrays.toString(sqls)); + } + if (isAsync()) { + int rs = deleteDBAsync(info, flipper, sqls).join(); + deleteCache(info, rs, flipper, sqls); + return rs; + } else { + int rs = deleteDB(info, flipper, sqls); + deleteCache(info, rs, flipper, sqls); + return rs; + } } @Override @@ -977,8 +1058,20 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(deleteCache(info, -1, flipper, node)); } + String[] sqls = deleteSql(info, flipper, node); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " delete sql=" + Arrays.toString(sqls)); + } if (isAsync()) { - return this.deleteCompose(info, flipper, node).whenComplete((rs, t) -> { + return deleteDBAsync(info, flipper, sqls).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + deleteCache(info, rs, flipper, node); + } + }); + } else { + return supplyAsync(() -> deleteDB(info, flipper, sqls)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -986,40 +1079,23 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.deleteCompose(info, flipper, node).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - deleteCache(info, rs, flipper, node); - } - }); } - protected CompletableFuture deleteCompose(final EntityInfo info, final Serializable... pks) { - String sql = deleteSql(info, pks); - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " delete sql=" + sql); - } - return deleteDB(info, null, sql); - } - - protected CompletableFuture deleteCompose(final EntityInfo info, final Flipper flipper, final FilterNode node) { - return deleteDB(info, flipper, deleteSql(info, flipper, node)); - } - - protected String deleteSql(final EntityInfo info, final Serializable... pks) { + protected String[] deleteSql(final EntityInfo info, final Serializable... pks) { if (pks.length == 1) { - return "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pks[0], sqlFormatter); - } - String sql = "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + " IN ("; - for (int i = 0; i < pks.length; i++) { - if (i > 0) { - sql += ','; + String sql = "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pks[0], sqlFormatter); + return new String[]{sql}; + } else { + String sql = "DELETE FROM " + info.getTable(pks[0]) + " WHERE " + info.getPrimarySQLColumn() + " IN ("; + for (int i = 0; i < pks.length; i++) { + if (i > 0) { + sql += ','; + } + sql += info.formatSQLValue(info.getPrimarySQLColumn(), pks[i], sqlFormatter); } - sql += info.formatSQLValue(info.getPrimarySQLColumn(), pks[i], sqlFormatter); + sql += ")"; + return new String[]{sql}; } - sql += ")"; - return sql; } protected String[] deleteSql(final EntityInfo info, final Flipper flipper, final FilterNode node) { @@ -1036,8 +1112,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi StringBuilder join2 = null; if (join != null) { String joinstr = join.toString(); - join1 = multisplit('[', ']', ",", new StringBuilder(), joinstr, 0); - join2 = multisplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); + join1 = multiSplit('[', ']', ",", new StringBuilder(), joinstr, 0); + join2 = multiSplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); } if (pgsql && flipper != null && flipper.getLimit() > 0) { @@ -1078,13 +1154,20 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return clearTableCache(info, node); } - return this.clearTableCompose(info, node).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - clearTableCache(info, node); - } - }).join(); + final String[] tables = info.getTables(node); + String[] sqls = clearTableSql(info, tables, node); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " clearTable sql=" + Arrays.toString(sqls)); + } + if (isAsync()) { + int rs = clearTableDBAsync(info, tables, sqls).join(); + clearTableCache(info, node); + return rs; + } else { + int rs = clearTableDB(info, tables, sqls); + clearTableCache(info, node); + return rs; + } } @Override @@ -1093,8 +1176,21 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(clearTableCache(info, node)); } + final String[] tables = info.getTables(node); + String[] sqls = clearTableSql(info, tables, node); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " clearTable sql=" + Arrays.toString(sqls)); + } if (isAsync()) { - return this.clearTableCompose(info, node).whenComplete((rs, t) -> { + return clearTableDBAsync(info, tables, sqls).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + clearTableCache(info, node); + } + }); + } else { + return supplyAsync(() -> clearTableDB(info, tables, sqls)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1102,33 +1198,14 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.clearTableCompose(info, node).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - clearTableCache(info, node); - } - }); } - protected CompletableFuture clearTableCompose(final EntityInfo info, final FilterNode node) { - final String[] tables = info.getTables(node); - if (tables.length == 1) { - String sql = "TRUNCATE TABLE " + tables[0]; - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " clearTable sql=" + sql); - } - return clearTableDB(info, tables, sql); - } else { - List sqls = new ArrayList<>(); - for (String table : tables) { - sqls.add("TRUNCATE TABLE " + table); - } - if (info.isLoggable(logger, Level.FINEST, sqls.get(0))) { - logger.finest(info.getType().getSimpleName() + " clearTable sqls=" + sqls); - } - return clearTableDB(info, tables, sqls.toArray(new String[sqls.size()])); + protected String[] clearTableSql(final EntityInfo info, String[] tables, final FilterNode node) { + List sqls = new ArrayList<>(); + for (String table : tables) { + sqls.add("TRUNCATE TABLE " + table); } + return sqls.toArray(new String[sqls.size()]); } //----------------------------- dropTableCompose ----------------------------- @@ -1138,13 +1215,20 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return dropTableCache(info, node); } - return this.dropTableCompose(info, node).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - dropTableCache(info, node); - } - }).join(); + final String[] tables = info.getTables(node); + String[] sqls = clearTableSql(info, tables, node); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " dropTable sql=" + Arrays.toString(sqls)); + } + if (isAsync()) { + int rs = dropTableDBAsync(info, tables, sqls).join(); + dropTableCache(info, node); + return rs; + } else { + int rs = dropTableDB(info, tables, sqls); + dropTableCache(info, node); + return rs; + } } @Override @@ -1153,8 +1237,21 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(dropTableCache(info, node)); } + final String[] tables = info.getTables(node); + String[] sqls = clearTableSql(info, tables, node); + if (info.isLoggable(logger, Level.FINEST, sqls[0])) { + logger.finest(info.getType().getSimpleName() + " dropTable sql=" + Arrays.toString(sqls)); + } if (isAsync()) { - return this.dropTableCompose(info, node).whenComplete((rs, t) -> { + return dropTableDBAsync(info, tables, sqls).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + dropTableCache(info, node); + } + }); + } else { + return supplyAsync(() -> dropTableDB(info, tables, sqls)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1162,34 +1259,14 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.dropTableCompose(info, node).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - dropTableCache(info, node); - } - }); } - protected CompletableFuture dropTableCompose(final EntityInfo info, final FilterNode node) { - if (node == null) { - final String table = info.getOriginTable(); - String sql = "DROP TABLE IF EXISTS " + table; - //if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " dropTable sql=" + sql); - return dropTableDB(info, new String[]{table}, sql); - } else { - final String[] tables = info.getTables(node); - if (tables.length == 1) { - String sql = "DROP TABLE IF EXISTS " + tables[0]; - return dropTableDB(info, tables, sql); - } else { - List sqls = new ArrayList<>(); - for (String table : tables) { - sqls.add("DROP TABLE IF EXISTS " + table); - } - return dropTableDB(info, tables, sqls.toArray(new String[sqls.size()])); - } + protected String[] dropTableSql(final EntityInfo info, String[] tables, final FilterNode node) { + List sqls = new ArrayList<>(); + for (String table : tables) { + sqls.add("DROP TABLE IF EXISTS " + table); } + return sqls.toArray(new String[sqls.size()]); } protected int clearTableCache(final EntityInfo info, FilterNode node) { @@ -1229,7 +1306,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return count >= 0 ? count : c; } - protected static StringBuilder multisplit(char ch1, char ch2, String split, StringBuilder sb, String str, int from) { + protected static StringBuilder multiSplit(char ch1, char ch2, String split, StringBuilder sb, String str, int from) { if (str == null) { return sb; } @@ -1245,7 +1322,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi sb.append(split); } sb.append(str.substring(pos1 + 1, pos2)); - return multisplit(ch1, ch2, split, sb, str, pos2 + 1); + return multiSplit(ch1, ch2, split, sb, str, pos2 + 1); } //---------------------------- update ---------------------------- @@ -1262,19 +1339,21 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (entitys.length == 0) { return -1; } - checkEntity("update", false, entitys); + checkEntity("update", entitys); final Class clazz = (Class) entitys[0].getClass(); final EntityInfo info = loadEntityInfo(clazz); if (isOnlyCache(info)) { return updateCache(info, -1, entitys); } - return updateEntityDB(info, entitys).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, entitys); - } - }).join(); + if (isAsync()) { + int rs = updateEntityDBAsync(info, entitys).join(); + updateCache(info, rs, entitys); + return rs; + } else { + int rs = updateEntityDB(info, entitys); + updateCache(info, rs, entitys); + return rs; + } } @Override @@ -1282,17 +1361,22 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (entitys.length == 0) { return CompletableFuture.completedFuture(-1); } - CompletableFuture future = checkEntity("update", true, entitys); - if (future != null) { - return future; - } + checkEntity("update", entitys); final Class clazz = (Class) entitys[0].getClass(); final EntityInfo info = loadEntityInfo(clazz); if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, entitys)); } if (isAsync()) { - return updateEntityDB(info, entitys).whenComplete((rs, t) -> { + return updateEntityDBAsync(info, entitys).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, entitys); + } + }); + } else { + return supplyAsync(() -> updateEntityDB(info, entitys)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1300,13 +1384,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> updateEntityDB(info, entitys).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, entitys); - } - }); } /** @@ -1326,13 +1403,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, pk, column, colval); } - return updateColumnCompose(info, pk, column, colval).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, pk, column, colval); - } - }).join(); + + SqlInfo sql = updateColumnSql(info, pk, column, colval); + if (isAsync()) { + int rs = updateColumnDBAsync(info, null, sql).join(); + updateCache(info, rs, pk, column, colval); + return rs; + } else { + int rs = updateColumnDB(info, null, sql); + updateCache(info, rs, pk, column, colval); + return rs; + } } @Override @@ -1341,8 +1422,18 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, pk, column, colval)); } + + SqlInfo sql = updateColumnSql(info, pk, column, colval); if (isAsync()) { - return updateColumnCompose(info, pk, column, colval).whenComplete((rs, t) -> { + return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, pk, column, colval); + } + }); + } else { + return supplyAsync(() -> updateColumnDB(info, null, sql)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1350,22 +1441,9 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> updateColumnCompose(info, pk, column, colval).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, pk, column, colval); - } - }); } - protected CompletableFuture updateColumnCompose(final EntityInfo info, Serializable pk, String column, final Serializable colval) { - Attribute attr = info.getAttribute(column); - SqlInfo sql = updateSql(info, pk, column, colval); - return updateColumnDB(info, null, sql); - } - - protected SqlInfo updateSql(final EntityInfo info, Serializable pk, String column, final Serializable colval) { + protected SqlInfo updateColumnSql(final EntityInfo info, Serializable pk, String column, final Serializable colval) { Attribute attr = info.getAttribute(column); Serializable val = getSQLAttrValue(info, attr, colval); if (val instanceof byte[]) { @@ -1393,13 +1471,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, column, colval, node); } - return this.updateColumnCompose(info, column, colval, node).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, column, colval, node); - } - }).join(); + + SqlInfo sql = updateColumnSql(info, column, colval, node); + if (isAsync()) { + int rs = updateColumnDBAsync(info, null, sql).join(); + updateCache(info, rs, column, colval, node); + return rs; + } else { + int rs = updateColumnDB(info, null, sql); + updateCache(info, rs, column, colval, node); + return rs; + } } @Override @@ -1408,8 +1490,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, column, colval, node)); } + SqlInfo sql = updateColumnSql(info, column, colval, node); if (isAsync()) { - return this.updateColumnCompose(info, column, colval, node).whenComplete((rs, t) -> { + return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, column, colval, node); + } + }); + } else { + return supplyAsync(() -> updateColumnDB(info, null, sql)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1417,22 +1508,9 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, column, colval, node).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, column, colval, node); - } - }); } - protected CompletableFuture updateColumnCompose(final EntityInfo info, final String column, final Serializable colval, final FilterNode node) { - Attribute attr = info.getAttribute(column); - SqlInfo sql = updateSql(info, column, colval, node); - return updateColumnDB(info, null, sql); - } - - protected SqlInfo updateSql(final EntityInfo info, final String column, final Serializable colval, final FilterNode node) { + protected SqlInfo updateColumnSql(final EntityInfo info, final String column, final Serializable colval, final FilterNode node) { Map joinTabalis = node.getJoinTabalis(); CharSequence join = node.createSQLJoin(this, true, joinTabalis, new HashSet<>(), info); CharSequence where = node.createSQLExpress(this, info, joinTabalis); @@ -1441,8 +1519,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi StringBuilder join2 = null; if (join != null) { String joinstr = join.toString(); - join1 = multisplit('[', ']', ",", new StringBuilder(), joinstr, 0); - join2 = multisplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); + join1 = multiSplit('[', ']', ",", new StringBuilder(), joinstr, 0); + join2 = multiSplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); } Attribute attr = info.getAttribute(column); Serializable val = getSQLAttrValue(info, attr, colval); @@ -1497,13 +1575,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, pk, values); } - return this.updateColumnCompose(info, pk, values).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, pk, values); - } - }).join(); + + SqlInfo sql = updateColumnSql(info, pk, values); + if (isAsync()) { + int rs = updateColumnDBAsync(info, null, sql).join(); + updateCache(info, rs, pk, values); + return rs; + } else { + int rs = updateColumnDB(info, null, sql); + updateCache(info, rs, pk, values); + return rs; + } } @Override @@ -1515,8 +1597,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, pk, values)); } + SqlInfo sql = updateColumnSql(info, pk, values); if (isAsync()) { - return this.updateColumnCompose(info, pk, values).whenComplete((rs, t) -> { + return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, pk, values); + } + }); + } else { + return supplyAsync(() -> updateColumnDB(info, null, sql)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1524,21 +1615,9 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, pk, values).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, pk, values); - } - }); } - protected CompletableFuture updateColumnCompose(final EntityInfo info, final Serializable pk, final ColumnValue... values) { - SqlInfo sql = updateSql(info, pk, values); - return updateColumnDB(info, null, sql); - } - - protected SqlInfo updateSql(final EntityInfo info, final Serializable pk, final ColumnValue... values) { + protected SqlInfo updateColumnSql(final EntityInfo info, final Serializable pk, final ColumnValue... values) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; @@ -1580,13 +1659,16 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, node, flipper, values); } - return this.updateColumnCompose(info, node, flipper, values).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, node, flipper, values); - } - }).join(); + SqlInfo sql = updateColumnSql(info, node, flipper, values); + if (isAsync()) { + int rs = updateColumnDBAsync(info, null, sql).join(); + updateCache(info, rs, node, flipper, values); + return rs; + } else { + int rs = updateColumnDB(info, null, sql); + updateCache(info, rs, node, flipper, values); + return rs; + } } @Override @@ -1598,8 +1680,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, node, flipper, values)); } + SqlInfo sql = updateColumnSql(info, node, flipper, values); if (isAsync()) { - return this.updateColumnCompose(info, node, flipper, values).whenComplete((rs, t) -> { + return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, node, flipper, values); + } + }); + } else { + return supplyAsync(() -> updateColumnDB(info, null, sql)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1607,21 +1698,9 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, node, flipper, values).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, node, flipper, values); - } - }); } - protected CompletableFuture updateColumnCompose(final EntityInfo info, final FilterNode node, final Flipper flipper, final ColumnValue... values) { - SqlInfo sql = updateSql(info, node, flipper, values); - return updateColumnDB(info, flipper, sql); - } - - protected SqlInfo updateSql(final EntityInfo info, final FilterNode node, final Flipper flipper, final ColumnValue... values) { + protected SqlInfo updateColumnSql(final EntityInfo info, final FilterNode node, final Flipper flipper, final ColumnValue... values) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; @@ -1659,8 +1738,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi StringBuilder join2 = null; if (join != null) { String joinstr = join.toString(); - join1 = multisplit('[', ']', ",", new StringBuilder(), joinstr, 0); - join2 = multisplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); + join1 = multiSplit('[', ']', ",", new StringBuilder(), joinstr, 0); + join2 = multiSplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); } String sql; String[] tables = info.getTables(node); @@ -1727,13 +1806,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, false, entity, null, selects); } - return this.updateColumnCompose(info, false, entity, null, selects).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, false, entity, null, selects); - } - }).join(); + + SqlInfo sql = updateColumnSql(info, false, entity, null, selects); + if (isAsync()) { + int rs = updateColumnDBAsync(info, null, sql).join(); + updateCache(info, rs, false, entity, null, selects); + return rs; + } else { + int rs = updateColumnDB(info, null, sql); + updateCache(info, rs, false, entity, null, selects); + return rs; + } } @Override @@ -1750,8 +1833,18 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, false, entity, null, selects)); } + + SqlInfo sql = updateColumnSql(info, false, entity, null, selects); if (isAsync()) { - return this.updateColumnCompose(info, false, entity, null, selects).whenComplete((rs, t) -> { + return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, false, entity, null, selects); + } + }); + } else { + return supplyAsync(() -> updateColumnDB(info, null, sql)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1759,13 +1852,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, false, entity, null, selects).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, false, entity, null, selects); - } - }); } @Override @@ -1782,13 +1868,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, true, entity, node, selects); } - return this.updateColumnCompose(info, true, entity, node, selects).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, true, entity, node, selects); - } - }).join(); + + SqlInfo sql = updateColumnSql(info, true, entity, node, selects); + if (isAsync()) { + int rs = updateColumnDBAsync(info, null, sql).join(); + updateCache(info, rs, true, entity, node, selects); + return rs; + } else { + int rs = updateColumnDB(info, null, sql); + updateCache(info, rs, true, entity, node, selects); + return rs; + } } @Override @@ -1805,8 +1895,18 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return CompletableFuture.completedFuture(updateCache(info, -1, true, entity, node, selects)); } + + SqlInfo sql = updateColumnSql(info, true, entity, node, selects); if (isAsync()) { - return this.updateColumnCompose(info, true, entity, node, selects).whenComplete((rs, t) -> { + return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { + if (t != null) { + errorCompleteConsumer.accept(rs, t); + } else { + updateCache(info, rs, true, entity, node, selects); + } + }); + } else { + return supplyAsync(() -> updateColumnDB(info, null, sql)).whenComplete((rs, t) -> { if (t != null) { errorCompleteConsumer.accept(rs, t); } else { @@ -1814,21 +1914,9 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } }); } - return CompletableFuture.supplyAsync(() -> this.updateColumnCompose(info, true, entity, node, selects).join(), getExecutor()).whenComplete((rs, t) -> { - if (t != null) { - errorCompleteConsumer.accept(rs, t); - } else { - updateCache(info, rs, true, entity, node, selects); - } - }); } - protected CompletableFuture updateColumnCompose(final EntityInfo info, final boolean needNode, final T entity, final FilterNode node, final SelectColumn selects) { - SqlInfo sql = updateSql(info, needNode, entity, node, selects); - return updateColumnDB(info, null, sql); - } - - protected SqlInfo updateSql(final EntityInfo info, final boolean needNode, final T entity, final FilterNode node, final SelectColumn selects) { + protected SqlInfo updateColumnSql(final EntityInfo info, final boolean needNode, final T entity, final FilterNode node, final SelectColumn selects) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; @@ -1864,8 +1952,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi StringBuilder join2 = null; if (join != null) { String joinstr = join.toString(); - join1 = multisplit('[', ']', ",", new StringBuilder(), joinstr, 0); - join2 = multisplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); + join1 = multiSplit('[', ']', ",", new StringBuilder(), joinstr, 0); + join2 = multiSplit('{', '}', " AND ", new StringBuilder(), joinstr, 0); } String sql; String[] tables = info.getTables(node); @@ -1992,7 +2080,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi String column = info.getPrimary().field(); int c = 0; for (Serializable id : pks) { - Sheet sheet = querySheetCompose(false, true, false, clazz, null, FLIPPER_ONE, FilterNode.create(column, id)).join(); + Sheet sheet = querySheet(false, true, false, clazz, null, FLIPPER_ONE, FilterNode.create(column, id)); T value = sheet.isEmpty() ? null : sheet.list().get(0); if (value != null) { c += cache.update(value); @@ -2017,7 +2105,16 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return map; } } - return (Map) getNumberMapCompose(info, node, columns).join(); + final String[] tables = info.getTables(node); + String sql = getNumberMapSql(info, tables, node, columns); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " getNumberMap sql=" + sql); + } + if (isAsync()) { + return (Map) getNumberMapDBAsync(info, sql, columns).join(); + } else { + return getNumberMapDB(info, sql, columns); + } } @Override @@ -2035,13 +2132,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(map); } } - if (isAsync()) { - return getNumberMapCompose(info, node, columns); + final String[] tables = info.getTables(node); + String sql = getNumberMapSql(info, tables, node, columns); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " getNumberMap sql=" + sql); + } + if (isAsync()) { + return getNumberMapDBAsync(info, sql, columns); + } else { + return supplyAsync(() -> getNumberMapDB(info, sql, columns)); } - return CompletableFuture.supplyAsync(() -> (Map) getNumberMapCompose(info, node, columns).join(), getExecutor()); } - protected CompletableFuture> getNumberMapCompose(final EntityInfo info, final FilterNode node, final FilterFuncColumn... columns) { + protected String getNumberMapSql(final EntityInfo info, final String[] tables, final FilterNode node, final FilterFuncColumn... columns) { final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final Set haset = new HashSet<>(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, haset, info); @@ -2055,12 +2158,9 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi sb.append(ffc.func.getColumn((col == null || col.isEmpty() ? "*" : info.getSQLColumn("a", col)))); } } - final String sql = "SELECT " + sb + " FROM " + info.getTable(node) + " a" + final String sql = "SELECT " + sb + " FROM " + tables[0] + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " getnumbermap sql=" + sql); - } - return getNumberMapDB(info, sql, columns); + return sql; } @Local @@ -2088,14 +2188,24 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi //------------------------ getNumberResultCompose ----------------------- @Override public Number getNumberResult(final Class entityClass, final FilterFunc func, final Number defVal, final String column, final FilterNode node) { - final EntityInfo info = loadEntityInfo(entityClass); + final EntityInfo info = loadEntityInfo(entityClass); final EntityCache cache = info.getCache(); if (cache != null && (isOnlyCache(info) || cache.isFullLoaded())) { if (node == null || isCacheUseable(node, this)) { return cache.getNumberResult(func, defVal, column, node); } } - return getNumberResultCompose(info, entityClass, func, defVal, column, node).join(); + + final String[] tables = info.getTables(node); + String sql = getNumberResultSql(info, entityClass, tables, func, defVal, column, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " getNumberResult sql=" + sql); + } + if (isAsync()) { + return getNumberResultDBAsync(info, sql, defVal, column).join(); + } else { + return getNumberResultDB(info, sql, defVal, column); + } } @Override @@ -2107,23 +2217,26 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(cache.getNumberResult(func, defVal, column, node)); } } - if (isAsync()) { - return getNumberResultCompose(info, entityClass, func, defVal, column, node); + final String[] tables = info.getTables(node); + String sql = getNumberResultSql(info, entityClass, tables, func, defVal, column, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " getNumberResult sql=" + sql); + } + if (isAsync()) { + return getNumberResultDBAsync(info, sql, defVal, column); + } else { + return supplyAsync(() -> getNumberResultDB(info, sql, defVal, column)); } - return CompletableFuture.supplyAsync(() -> getNumberResultCompose(info, entityClass, func, defVal, column, node).join(), getExecutor()); } - protected CompletableFuture getNumberResultCompose(final EntityInfo info, final Class entityClass, final FilterFunc func, final Number defVal, final String column, final FilterNode node) { + protected String getNumberResultSql(final EntityInfo info, final Class entityClass, final String[] tables, final FilterFunc func, final Number defVal, final String column, final FilterNode node) { final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final Set haset = new HashSet<>(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, haset, info); final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); - final String sql = "SELECT " + func.getColumn((column == null || column.isEmpty() ? "*" : info.getSQLColumn("a", column))) + " FROM " + info.getTable(node) + " a" + final String sql = "SELECT " + func.getColumn((column == null || column.isEmpty() ? "*" : info.getSQLColumn("a", column))) + " FROM " + tables[0] + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(entityClass.getSimpleName() + " getNumberResult sql=" + sql); - } - return getNumberResultDB(info, sql, defVal, column); + return sql; } @Local @@ -2151,7 +2264,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return cache.queryColumnMap(keyColumn, func, funcColumn, node); } } - return (Map) queryColumnMapCompose(info, keyColumn, func, funcColumn, node).join(); + + final String[] tables = info.getTables(node); + String sql = queryColumnMapSql(info, tables, keyColumn, func, funcColumn, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " queryColumnMap sql=" + sql); + } + if (isAsync()) { + return (Map) queryColumnMapDBAsync(info, sql, keyColumn).join(); + } else { + return queryColumnMapDB(info, sql, keyColumn); + } } @Override @@ -2163,13 +2286,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(cache.queryColumnMap(keyColumn, func, funcColumn, node)); } } - if (isAsync()) { - return queryColumnMapCompose(info, keyColumn, func, funcColumn, node); + final String[] tables = info.getTables(node); + String sql = queryColumnMapSql(info, tables, keyColumn, func, funcColumn, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " queryColumnMap sql=" + sql); + } + if (isAsync()) { + return queryColumnMapDBAsync(info, sql, keyColumn); + } else { + return supplyAsync(() -> queryColumnMapDB(info, sql, keyColumn)); } - return CompletableFuture.supplyAsync(() -> (Map) queryColumnMapCompose(info, keyColumn, func, funcColumn, node).join(), getExecutor()); } - protected CompletableFuture> queryColumnMapCompose(final EntityInfo info, final String keyColumn, final FilterFunc func, final String funcColumn, FilterNode node) { + protected String queryColumnMapSql(final EntityInfo info, final String[] tables, final String keyColumn, final FilterFunc func, final String funcColumn, FilterNode node) { final String keySqlColumn = info.getSQLColumn(null, keyColumn); final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final Set haset = new HashSet<>(); @@ -2177,7 +2306,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); final String funcSqlColumn = func == null ? info.getSQLColumn("a", funcColumn) : func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : info.getSQLColumn("a", funcColumn))); - String[] tables = info.getTables(node); String joinAndWhere = (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); String sql; if (tables.length == 1) { @@ -2194,10 +2322,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } sql = "SELECT a." + keySqlColumn + ", " + funcSqlColumn + " FROM (" + (union) + ") a"; } - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " querycolumnmap sql=" + sql); - } - return queryColumnMapDB(info, sql, keyColumn); + return sql; + //return queryColumnMapDBAsync(info, sql, keyColumn); } @Local @@ -2239,7 +2365,16 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return cache.queryColumnMap(funcNodes, groupByColumns, node); } } - return (Map) queryColumnMapCompose(info, funcNodes, groupByColumns, node).join(); + final String[] tables = info.getTables(node); + String sql = queryColumnMapSql(info, tables, funcNodes, groupByColumns, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " queryColumnMap sql=" + sql); + } + if (isAsync()) { + return (Map) queryColumnMapDBAsync(info, sql, funcNodes, groupByColumns).join(); + } else { + return queryColumnMapDB(info, sql, funcNodes, groupByColumns); + } } @Override @@ -2251,13 +2386,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(cache.queryColumnMap(funcNodes, groupByColumns, node)); } } - if (isAsync()) { - return queryColumnMapCompose(info, funcNodes, groupByColumns, node); + final String[] tables = info.getTables(node); + String sql = queryColumnMapSql(info, tables, funcNodes, groupByColumns, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " queryColumnMap sql=" + sql); + } + if (isAsync()) { + return queryColumnMapDBAsync(info, sql, funcNodes, groupByColumns); + } else { + return supplyAsync(() -> queryColumnMapDB(info, sql, funcNodes, groupByColumns)); } - return CompletableFuture.supplyAsync(() -> (Map) queryColumnMapCompose(info, funcNodes, groupByColumns, node).join(), getExecutor()); } - protected CompletableFuture> queryColumnMapCompose(final EntityInfo info, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { + protected String queryColumnMapSql(final EntityInfo info, final String[] tables, final ColumnNode[] funcNodes, final String[] groupByColumns, final FilterNode node) { final StringBuilder groupBySqlColumns = new StringBuilder(); if (groupByColumns != null && groupByColumns.length > 0) { for (int i = 0; i < groupByColumns.length; i++) { @@ -2283,7 +2424,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, haset, info); final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); - String[] tables = info.getTables(node); String joinAndWhere = (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); String sql; if (tables.length == 1) { @@ -2314,10 +2454,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (groupBySqlColumns.length() > 0) { sql += " GROUP BY " + groupBySqlColumns; } - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " querycolumnmap sql=" + sql); - } - return queryColumnMapDB(info, sql, funcNodes, groupByColumns); + return sql; } @Local @@ -2404,7 +2541,15 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return rs; } } - return findCompose(info, selects, pk).join(); + String sql = findSql(info, selects, pk); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " find sql=" + sql); + } + if (isAsync()) { + return findDBAsync(info, sql, true, selects).join(); + } else { + return findDB(info, sql, true, selects); + } } @Override @@ -2417,19 +2562,24 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(rs); } } - if (isAsync()) { - return findCompose(info, selects, pk); + String sql = findSql(info, selects, pk); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " find sql=" + sql); + } + if (isAsync()) { + return findDBAsync(info, sql, true, selects); + } else { + return supplyAsync(() -> findDB(info, sql, true, selects)); } - return CompletableFuture.supplyAsync(() -> findCompose(info, selects, pk).join(), getExecutor()); } - protected CompletableFuture findCompose(final EntityInfo info, final SelectColumn selects, Serializable pk) { + protected String findSql(final EntityInfo info, final SelectColumn selects, Serializable pk) { String column = info.getPrimarySQLColumn(); final String sql = "SELECT " + info.getQueryColumns(null, selects) + " FROM " + info.getTable(pk) + " WHERE " + column + "=" + info.formatSQLValue(column, pk, sqlFormatter); if (info.isLoggable(logger, Level.FINEST, sql)) { logger.finest(info.getType().getSimpleName() + " find sql=" + sql); } - return findDB(info, sql, true, selects); + return sql; } @Override @@ -2439,7 +2589,16 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (cache != null && cache.isFullLoaded() && (node == null || isCacheUseable(node, this))) { return cache.find(selects, node); } - return this.findCompose(info, selects, node).join(); + final String[] tables = info.getTables(node); + String sql = findSql(info, tables, selects, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " find sql=" + sql); + } + if (isAsync()) { + return findDBAsync(info, sql, false, selects).join(); + } else { + return findDB(info, sql, false, selects); + } } @Override @@ -2449,17 +2608,22 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (cache != null && cache.isFullLoaded() && (node == null || isCacheUseable(node, this))) { return CompletableFuture.completedFuture(cache.find(selects, node)); } - if (isAsync()) { - return this.findCompose(info, selects, node); + final String[] tables = info.getTables(node); + String sql = findSql(info, tables, selects, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " find sql=" + sql); + } + if (isAsync()) { + return findDBAsync(info, sql, false, selects); + } else { + return supplyAsync(() -> findDB(info, sql, false, selects)); } - return CompletableFuture.supplyAsync(() -> this.findCompose(info, selects, node).join(), getExecutor()); } - protected CompletableFuture findCompose(final EntityInfo info, final SelectColumn selects, final FilterNode node) { + protected String findSql(final EntityInfo info, final String[] tables, final SelectColumn selects, final FilterNode node) { final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info); final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); - String[] tables = info.getTables(node); String joinAndWhere = (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); String sql; if (tables.length == 1) { @@ -2474,10 +2638,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } sql = "SELECT " + info.getQueryColumns("a", selects) + " FROM (" + (union) + ") a"; } - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " find sql=" + sql); - } - return findDB(info, sql, false, selects); + return sql; } @Local @@ -2499,7 +2660,15 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return val; } } - return findColumnCompose(info, column, defValue, pk).join(); + String sql = findColumnSql(info, column, defValue, pk); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " findColumn sql=" + sql); + } + if (isAsync()) { + return findColumnDBAsync(info, sql, true, column, defValue).join(); + } else { + return findColumnDB(info, sql, true, column, defValue); + } } @Override @@ -2512,18 +2681,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(val); } } - if (isAsync()) { - return findColumnCompose(info, column, defValue, pk); + String sql = findColumnSql(info, column, defValue, pk); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " findColumn sql=" + sql); + } + if (isAsync()) { + return findColumnDBAsync(info, sql, true, column, defValue); + } else { + return supplyAsync(() -> findColumnDB(info, sql, true, column, defValue)); } - return CompletableFuture.supplyAsync(() -> findColumnCompose(info, column, defValue, pk).join(), getExecutor()); } - protected CompletableFuture findColumnCompose(final EntityInfo info, String column, final Serializable defValue, final Serializable pk) { - final String sql = "SELECT " + info.getSQLColumn(null, column) + " FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter); - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " find sql=" + sql); - } - return findColumnDB(info, sql, true, column, defValue); + protected String findColumnSql(final EntityInfo info, String column, final Serializable defValue, final Serializable pk) { + return "SELECT " + info.getSQLColumn(null, column) + " FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter); } @Override @@ -2536,7 +2706,16 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return val; } } - return this.findColumnCompose(info, column, defValue, node).join(); + final String[] tables = info.getTables(node); + String sql = findColumnSql(info, tables, column, defValue, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " findColumn sql=" + sql); + } + if (isAsync()) { + return findColumnDBAsync(info, sql, false, column, defValue).join(); + } else { + return findColumnDB(info, sql, false, column, defValue); + } } @Override @@ -2549,17 +2728,22 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(val); } } - if (isAsync()) { - return this.findColumnCompose(info, column, defValue, node); + final String[] tables = info.getTables(node); + String sql = findColumnSql(info, tables, column, defValue, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " findColumn sql=" + sql); + } + if (isAsync()) { + return findColumnDBAsync(info, sql, false, column, defValue); + } else { + return supplyAsync(() -> findColumnDB(info, sql, false, column, defValue)); } - return CompletableFuture.supplyAsync(() -> this.findColumnCompose(info, column, defValue, node).join(), getExecutor()); } - protected CompletableFuture findColumnCompose(final EntityInfo info, String column, final Serializable defValue, final FilterNode node) { + protected String findColumnSql(final EntityInfo info, String[] tables, String column, final Serializable defValue, final FilterNode node) { final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info); final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); - String[] tables = info.getTables(node); String joinAndWhere = (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); String sql; if (tables.length == 1) { @@ -2574,10 +2758,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } sql = "SELECT " + info.getSQLColumn("a", column) + " FROM (" + (union) + ") a"; } - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " find sql=" + sql); - } - return findColumnDB(info, sql, false, column, defValue); + return sql; } @Local @@ -2604,7 +2785,15 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return rs; } } - return existsCompose(info, pk).join(); + String sql = existsSql(info, pk); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " exists sql=" + sql); + } + if (isAsync()) { + return existsDBAsync(info, sql, true).join(); + } else { + return existsDB(info, sql, true); + } } @Override @@ -2617,18 +2806,19 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(rs); } } - if (isAsync()) { - return existsCompose(info, pk); - } - return CompletableFuture.supplyAsync(() -> existsCompose(info, pk).join(), getExecutor()); - } - - protected CompletableFuture existsCompose(final EntityInfo info, Serializable pk) { - final String sql = "SELECT COUNT(*) FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter); + String sql = existsSql(info, pk); if (info.isLoggable(logger, Level.FINEST, sql)) { logger.finest(info.getType().getSimpleName() + " exists sql=" + sql); } - return existsDB(info, sql, true); + if (isAsync()) { + return existsDBAsync(info, sql, true); + } else { + return supplyAsync(() -> existsDB(info, sql, true)); + } + } + + protected String existsSql(final EntityInfo info, Serializable pk) { + return "SELECT COUNT(*) FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter); } @Override @@ -2641,7 +2831,16 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return rs; } } - return this.existsCompose(info, node).join(); + final String[] tables = info.getTables(node); + String sql = existsSql(info, tables, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " exists sql=" + sql); + } + if (isAsync()) { + return existsDBAsync(info, sql, false).join(); + } else { + return existsDB(info, sql, false); + } } @Override @@ -2654,17 +2853,23 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(rs); } } - if (isAsync()) { - return this.existsCompose(info, node); + + final String[] tables = info.getTables(node); + String sql = existsSql(info, tables, node); + if (info.isLoggable(logger, Level.FINEST, sql)) { + logger.finest(info.getType().getSimpleName() + " exists sql=" + sql); + } + if (isAsync()) { + return existsDBAsync(info, sql, false); + } else { + return supplyAsync(() -> existsDB(info, sql, false)); } - return CompletableFuture.supplyAsync(() -> this.existsCompose(info, node).join(), getExecutor()); } - protected CompletableFuture existsCompose(final EntityInfo info, FilterNode node) { + protected String existsSql(final EntityInfo info, String[] tables, FilterNode node) { final Map joinTabalis = node == null ? null : node.getJoinTabalis(); final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info); final CharSequence where = node == null ? null : node.createSQLExpress(this, info, joinTabalis); - String[] tables = info.getTables(node); String joinAndWhere = (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); String sql; if (tables.length == 1) { @@ -2679,10 +2884,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } sql = "SELECT COUNT(" + info.getPrimarySQLColumn("a") + ") FROM (" + (union) + ") a"; } - if (info.isLoggable(logger, Level.FINEST, sql)) { - logger.finest(info.getType().getSimpleName() + " exists sql=" + sql); - } - return existsDB(info, sql, false); + return sql; } @Local @@ -2892,49 +3094,84 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi @Override public Set querySet(final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { - return new LinkedHashSet<>(querySheetCompose(true, false, true, clazz, selects, flipper, node).join().list(true)); + if (isAsync()) { + return querySheetAsync(true, false, true, clazz, selects, flipper, node).thenApply((rs) -> new LinkedHashSet<>(rs.list(true))).join(); + } else { + return new LinkedHashSet<>(querySheet(true, false, true, clazz, selects, flipper, node).list(true)); + } } @Override public CompletableFuture> querySetAsync(final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { - return querySheetCompose(true, false, true, clazz, selects, flipper, node).thenApply((rs) -> new LinkedHashSet<>(rs.list(true))); + if (isAsync()) { + return querySheetAsync(true, false, true, clazz, selects, flipper, node).thenApply((rs) -> new LinkedHashSet<>(rs.list(true))); + } else { + return supplyAsync(() -> querySheet(true, false, true, clazz, selects, flipper, node)).thenApply((rs) -> new LinkedHashSet<>(rs.list(true))); + } } @Override public List queryList(final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { - return querySheetCompose(true, false, false, clazz, selects, flipper, node).join().list(true); + if (isAsync()) { + return querySheetAsync(true, false, false, clazz, selects, flipper, node).thenApply((rs) -> rs.list(true)).join(); + } else { + return querySheet(true, false, false, clazz, selects, flipper, node).list(true); + } } @Override public CompletableFuture> queryListAsync(final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { - return querySheetCompose(true, false, false, clazz, selects, flipper, node).thenApply((rs) -> rs.list(true)); + if (isAsync()) { + return querySheetAsync(true, false, false, clazz, selects, flipper, node).thenApply((rs) -> rs.list(true)); + } else { + return supplyAsync(() -> querySheet(true, false, false, clazz, selects, flipper, node)).thenApply((rs) -> rs.list(true)); + } } @Override public Sheet querySheet(final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { - return querySheetCompose(true, true, false, clazz, selects, flipper, node).join(); + if (isAsync()) { + return querySheetAsync(true, true, false, clazz, selects, flipper, node).join(); + } else { + return querySheet(true, true, false, clazz, selects, flipper, node); + } } @Override public CompletableFuture> querySheetAsync(final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { if (isAsync()) { - return querySheetCompose(true, true, false, clazz, selects, flipper, node); + return querySheetAsync(true, true, false, clazz, selects, flipper, node); + } else { + return supplyAsync(() -> querySheet(true, true, false, clazz, selects, flipper, node)); } - return CompletableFuture.supplyAsync(() -> querySheetCompose(true, true, false, clazz, selects, flipper, node).join(), getExecutor()); } - protected CompletableFuture> querySheetCompose(final boolean readcache, final boolean needtotal, final boolean distinct, final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { + protected Sheet querySheet(final boolean readCache, final boolean needTotal, final boolean distinct, final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { final EntityInfo info = loadEntityInfo(clazz); final EntityCache cache = info.getCache(); - if (readcache && cache != null && cache.isFullLoaded()) { + if (readCache && cache != null && cache.isFullLoaded()) { if (node == null || isCacheUseable(node, this)) { if (info.isLoggable(logger, Level.FINEST, " cache query predicate = ")) { logger.finest(clazz.getSimpleName() + " cache query predicate = " + (node == null ? null : createPredicate(node, cache))); } - return CompletableFuture.completedFuture(cache.querySheet(needtotal, distinct, selects, flipper, node)); + return cache.querySheet(needTotal, distinct, selects, flipper, node); } } - return querySheetDB(info, readcache, needtotal, distinct, selects, flipper, node); + return querySheetDB(info, readCache, needTotal, distinct, selects, flipper, node); + } + + protected CompletableFuture> querySheetAsync(final boolean readCache, final boolean needTotal, final boolean distinct, final Class clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node) { + final EntityInfo info = loadEntityInfo(clazz); + final EntityCache cache = info.getCache(); + if (readCache && cache != null && cache.isFullLoaded()) { + if (node == null || isCacheUseable(node, this)) { + if (info.isLoggable(logger, Level.FINEST, " cache query predicate = ")) { + logger.finest(clazz.getSimpleName() + " cache query predicate = " + (node == null ? null : createPredicate(node, cache))); + } + return CompletableFuture.completedFuture(cache.querySheet(needTotal, distinct, selects, flipper, node)); + } + } + return querySheetDBAsync(info, readCache, needTotal, distinct, selects, flipper, node); } protected static class SqlInfo {