From 8f3ae4b5a7c709260e96f141b92ab93d0acd630c Mon Sep 17 00:00:00 2001 From: redkale Date: Mon, 3 Apr 2023 21:39:36 +0800 Subject: [PATCH] =?UTF-8?q?jdbc=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/redkale/source/DataJdbcSource.java | 133 ++++++++++++++++-- 1 file changed, 120 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index d08a1e7c6..a535bc0d6 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -12,6 +12,7 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.function.Function; import java.util.logging.Level; +import java.util.stream.Stream; import org.redkale.annotation.AutoLoad; import org.redkale.annotation.ResourceListener; import org.redkale.annotation.ResourceType; @@ -121,7 +122,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return false; } - protected List createInsertEntityPreparedStatements(final Connection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { + protected List prepareInsertEntityStatements(Connection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; final List prestmts = new ArrayList<>(); for (Map.Entry> en : prepareInfos.entrySet()) { @@ -136,7 +137,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmts; } - protected PreparedStatement createInsertEntityPreparedStatement(Connection conn, String sql, EntityInfo info, T... entitys) throws SQLException { + protected PreparedStatement prepareInsertEntityStatement(Connection conn, String sql, EntityInfo info, T... entitys) throws SQLException { Attribute[] attrs = info.insertAttributes; final PreparedStatement prestmt = conn.prepareStatement(sql); for (final T value : entitys) { @@ -146,7 +147,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmt; } - protected List createUpdateEntityPreparedStatements(final Connection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { + protected List prepareUpdateEntityStatements(Connection conn, EntityInfo info, Map> prepareInfos, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; final List prestmts = new ArrayList<>(); @@ -163,7 +164,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { return prestmts; } - protected PreparedStatement createUpdateEntityPreparedStatement(Connection conn, String prepareSQL, EntityInfo info, T... entitys) throws SQLException { + protected PreparedStatement prepareUpdateEntityStatement(Connection conn, String prepareSQL, EntityInfo info, T... entitys) throws SQLException { Attribute primary = info.primary; Attribute[] attrs = info.updateAttributes; final PreparedStatement prestmt = conn.prepareStatement(prepareSQL); @@ -333,10 +334,10 @@ public class DataJdbcSource extends AbstractDataSqlSource { Attribute[] attrs = info.insertAttributes; if (info.getTableStrategy() == null) { //单库单表 presql = info.getInsertQuestionPrepareSQL(entitys[0]); - prestmt = createInsertEntityPreparedStatement(conn, presql, info, entitys); + prestmt = prepareInsertEntityStatement(conn, presql, info, entitys); } else { //分库分表 prepareInfos = getInsertQuestionPrepareInfo(info, entitys); - prestmts = createInsertEntityPreparedStatements(conn, info, prepareInfos, entitys); + prestmts = prepareInsertEntityStatements(conn, info, prepareInfos, entitys); } try { if (info.getTableStrategy() == null) { //单库单表 @@ -485,7 +486,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } if (info.getTableStrategy() == null) { //单库单表 prestmt.close(); - prestmt = createInsertEntityPreparedStatement(conn, presql, info, entitys); + prestmt = prepareInsertEntityStatement(conn, presql, info, entitys); int c1 = 0; int[] cs = prestmt.executeBatch(); for (int cc : cs) { @@ -497,7 +498,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { for (PreparedStatement stmt : prestmts) { stmt.close(); } - prestmts = createInsertEntityPreparedStatements(conn, info, prepareInfos, entitys); + prestmts = prepareInsertEntityStatements(conn, info, prepareInfos, entitys); int c1 = 0; for (PreparedStatement stmt : prestmts) { int[] cs = stmt.executeBatch(); @@ -1081,6 +1082,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { private int updateEntityDB(final boolean batch, final Connection conn, final EntityInfo info, T... entitys) throws SQLException { final long s = System.currentTimeMillis(); String presql = null; + String caseSql = null; PreparedStatement prestmt = null; List prestmts = null; Map> prepareInfos = null; @@ -1088,8 +1090,25 @@ public class DataJdbcSource extends AbstractDataSqlSource { final Attribute[] attrs = info.updateAttributes; try { if (info.getTableStrategy() == null) { - presql = info.getUpdateQuestionPrepareSQL(entitys[0]); - prestmt = createUpdateEntityPreparedStatement(conn, presql, info, entitys); + caseSql = info.getUpdateQuestionPrepareCaseSQL(entitys); + if (caseSql == null) { + presql = info.getUpdateQuestionPrepareSQL(entitys[0]); + prestmt = prepareUpdateEntityStatement(conn, presql, info, entitys); + } else { + presql = caseSql; + prestmt = conn.prepareStatement(presql); + int len = entitys.length; + final Attribute primary = info.getPrimary(); + Attribute otherAttr = attrs[0]; + //UPDATE twointrecord SET randomNumber = ( CASE WHEN id = ? THEN ? WHEN id = ? THEN ? WHEN id = ? THEN ? END ) WHERE id IN (?,?,?) + for (int i = 0; i < entitys.length; i++) { + Serializable pk = primary.get(entitys[i]); + prestmt.setObject(i * 2 + 1, pk); //1 3 5 + prestmt.setObject(i * 2 + 2, getEntityAttrValue(info, otherAttr, entitys[i])); //2 4 6 + prestmt.setObject(len * 2 + i + 1, pk); //7 8 9 + } + prestmt.addBatch(); + } int c1 = 0; int[] pc = prestmt.executeBatch(); for (int p : pc) { @@ -1101,7 +1120,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { prestmt.close(); } else { prepareInfos = getUpdateQuestionPrepareInfo(info, entitys); - prestmts = createUpdateEntityPreparedStatements(conn, info, prepareInfos, entitys); + prestmts = prepareUpdateEntityStatements(conn, info, prepareInfos, entitys); int c1 = 0; for (PreparedStatement stmt : prestmts) { int[] cs = stmt.executeBatch(); @@ -1164,7 +1183,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { if (prepareInfos.isEmpty()) { //分表全部不存在 return 0; } - prestmts = createUpdateEntityPreparedStatements(conn, info, prepareInfos, entitys); + prestmts = prepareUpdateEntityStatements(conn, info, prepareInfos, entitys); int c1 = 0; for (PreparedStatement stmt : prestmts) { int[] cs = stmt.executeBatch(); @@ -1183,7 +1202,7 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } - if (info.isLoggable(logger, Level.FINEST)) { //打印调试信息 + if (info.isLoggable(logger, Level.FINEST) && caseSql == null) { //打印调试信息 Attribute primary = info.getPrimary(); if (info.getTableStrategy() == null) { char[] sqlchars = presql.toCharArray(); @@ -2118,13 +2137,101 @@ public class DataJdbcSource extends AbstractDataSqlSource { } } + @Override + public List findsList(Class clazz, Stream pks) { + final EntityInfo info = loadEntityInfo(clazz); + Serializable[] ids = pks.toArray(serialArrayFunc); + if (info.getTableStrategy() == null) { + Connection conn = null; + final long s = System.currentTimeMillis(); + try { + conn = readPool.pollConnection(); + final List list = new ArrayList(); + try { + String prepareSQL = info.getFindQuestionPrepareSQL(ids[0]); + PreparedStatement ps = conn.prepareStatement(prepareSQL); + for (Serializable pk : ids) { + ps.setObject(1, pk); + } + ResultSet set = ps.executeQuery(); + final DataResultSet rr = createDataResultSet(info, set); + while (set.next()) { + list.add(getEntityValue(info, null, rr)); + } + set.close(); + ps.close(); + slowLog(s, prepareSQL); + return list; + } catch (SQLException se) { + if (isTableNotExist(info, se.getSQLState())) { + return list; + } + throw new SourceException(se); + } + } catch (SourceException se) { + throw se; + } catch (Exception e) { + throw new SourceException(e); + } finally { + if (conn != null) { + readPool.offerConnection(conn); + } + } + } else { + return queryList(info.getType(), null, null, FilterNode.create(info.getPrimarySQLColumn(), FilterExpress.IN, ids)); + } + } + + @Override + public CompletableFuture> findsListAsync(final Class clazz, final Stream pks) { + return supplyAsync(() -> findsList(clazz, pks)); + } + @Override 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)); } + protected Sheet querySheetFullListDB(EntityInfo info) { + Connection conn = null; + final long s = System.currentTimeMillis(); + try { + conn = readPool.pollConnection(); + final List list = new ArrayList(); + try { + String prepareSQL = info.getAllQueryPrepareSQL(); + PreparedStatement ps = conn.prepareStatement(prepareSQL); + ResultSet set = ps.executeQuery(); + final DataResultSet rr = createDataResultSet(info, set); + while (set.next()) { + list.add(getEntityValue(info, null, rr)); + } + set.close(); + ps.close(); + slowLog(s, prepareSQL); + return Sheet.asSheet(list); + } catch (SQLException se) { + if (isTableNotExist(info, se.getSQLState())) { + return Sheet.asSheet(list); + } + throw new SourceException(se); + } + } catch (SourceException se) { + throw se; + } catch (Exception e) { + throw new SourceException(e); + } finally { + if (conn != null) { + readPool.offerConnection(conn); + } + } + } + @Override protected Sheet querySheetDB(EntityInfo info, final boolean readCache, boolean needTotal, final boolean distinct, SelectColumn selects, Flipper flipper, FilterNode node) { + if (!needTotal && !distinct && selects == null && flipper == null && node == null && info.getTableStrategy() == null) { + return querySheetFullListDB(info); + } Connection conn = null; final long s = System.currentTimeMillis(); final SelectColumn sels = selects;