From f09a323b20fbf43ac8da120973880c3bbf7a23bd Mon Sep 17 00:00:00 2001 From: Redkale Date: Sun, 1 Jan 2023 20:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96DataJdbcSource?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/redkale/source/DataJdbcSource.java | 207 ++++++++++++------ .../org/redkale/source/DataMemorySource.java | 4 +- .../org/redkale/source/DataSqlSource.java | 145 +++++------- 3 files changed, 200 insertions(+), 156 deletions(-) diff --git a/src/main/java/org/redkale/source/DataJdbcSource.java b/src/main/java/org/redkale/source/DataJdbcSource.java index 16c3ec006..936469057 100644 --- a/src/main/java/org/redkale/source/DataJdbcSource.java +++ b/src/main/java/org/redkale/source/DataJdbcSource.java @@ -999,62 +999,166 @@ public class DataJdbcSource extends DataSqlSource { } @Override - protected CompletableFuture updateColumnDBAsync(EntityInfo info, Flipper flipper, SqlInfo sql) { + protected CompletableFuture updateColumnDBAsync(EntityInfo info, Flipper flipper, UpdateSqlInfo sql) { return supplyAsync(() -> updateColumnDB(info, flipper, sql)); } @Override - protected int updateColumnDB(EntityInfo info, Flipper flipper, SqlInfo sql) { //String sql, boolean prepared, Object... blobs) { + protected int updateColumnDB(EntityInfo info, Flipper flipper, UpdateSqlInfo sql) { //String sql, boolean prepared, Object... blobs) { Connection conn = null; final long s = System.currentTimeMillis(); try { conn = writePool.pollConnection(); conn.setReadOnly(false); conn.setAutoCommit(false); - if (sql.blobs != null || sql.tables != null) { - final PreparedStatement prestmt = conn.prepareStatement(sql.sql); - int c = 0; - if (sql.tables == null) { - int index = 0; - for (byte[] param : sql.blobs) { - Blob blob = conn.createBlob(); - blob.setBytes(1, param); - prestmt.setBlob(++index, blob); - } - c = prestmt.executeUpdate(); - } else { - for (String table : sql.tables) { + int c = -1; + String firstTable = null; + try { + if (sql.blobs != null || sql.tables != null) { + if (sql.tables == null) { + final PreparedStatement prestmt = conn.prepareStatement(sql.sql); int index = 0; - if (sql.blobs != null) { - for (byte[] param : sql.blobs) { - Blob blob = conn.createBlob(); - blob.setBytes(1, param); - prestmt.setBlob(++index, blob); + for (byte[] param : sql.blobs) { + Blob blob = conn.createBlob(); + blob.setBytes(1, param); + prestmt.setBlob(++index, blob); + } + if (info.isLoggable(logger, Level.FINEST, sql.sql)) { + logger.finest(info.getType().getSimpleName() + " updateColumn sql=" + sql); + } + c = prestmt.executeUpdate(); + prestmt.close(); + conn.commit(); + slowLog(s, sql.sql); + return c; + } else { + firstTable = sql.tables[0]; + List prestmts = new ArrayList<>(); + String[] sqls = new String[sql.tables.length]; + for (int i = 0; i < sql.tables.length; i++) { + sqls[i] = i == 0 ? sql.sql : sql.sql.replaceFirst(firstTable, sql.tables[i]); + PreparedStatement prestmt = conn.prepareStatement(sqls[i]); + int index = 0; + if (sql.blobs != null) { + for (byte[] param : sql.blobs) { + Blob blob = conn.createBlob(); + blob.setBytes(1, param); + prestmt.setBlob(++index, blob); + } + } + prestmt.addBatch(); + prestmts.add(prestmt); + } + if (info.isLoggable(logger, Level.FINEST, sql.sql)) { + logger.finest(info.getType().getSimpleName() + " updateColumn sql=" + Arrays.toString(sqls)); + } + int c1 = 0; + for (PreparedStatement prestmt : prestmts) { + int[] cs = prestmt.executeBatch(); + for (int cc : cs) { + c1 += cc; + } + prestmt.close(); + } + c = c1; + conn.commit(); + slowLog(s, sqls); + } + return c; + } else { + if (info.isLoggable(logger, Level.FINEST, sql.sql)) { + logger.finest(info.getType().getSimpleName() + " updateColumn sql=" + sql); + } + final Statement stmt = conn.createStatement(); + c = stmt.executeUpdate(sql.sql); + stmt.close(); + conn.commit(); + slowLog(s, sql.sql); + return c; + } + } catch (SQLException se) { + conn.rollback(); + if (isTableNotExist(info, se.getSQLState())) { + if (info.getTableStrategy() == null) { + String[] tableSqls = createTableSqls(info); + if (tableSqls != null) { + try { + Statement st = conn.createStatement(); + if (tableSqls.length == 1) { + st.execute(tableSqls[0]); + } else { + for (String tableSql : tableSqls) { + st.addBatch(tableSql); + } + st.executeBatch(); + } + st.close(); + } catch (SQLException e2) { } } - prestmt.setString(++index, table); - prestmt.addBatch(); - } - int[] cs = prestmt.executeBatch(); - for (int cc : cs) { - c += cc; + //表不存在,更新条数为0 + return 0; + } else if (sql.tables == null) { + //单一分表不存在 + return 0; + } else { + String tableName = parseNotExistTableName(se); + if (tableName == null) { + throw new SourceException(se); + } + + String[] oldTables = sql.tables; + List notExistTables = checkNotExistTables(conn, oldTables, tableName); + if (notExistTables.isEmpty()) { + throw new SourceException(se); + } + for (String t : notExistTables) { + sql.tables = Utility.remove(sql.tables, t); + } + if (logger.isLoggable(Level.FINE)) { + logger.log(Level.FINE, "updateColumn, old-tables: " + Arrays.toString(oldTables) + ", new-tables: " + Arrays.toString(sql.tables)); + } + if (sql.tables.length == 0) { //分表全部不存在 + return 0; + } + List prestmts = new ArrayList<>(); + String[] sqls = new String[sql.tables.length]; + for (int i = 0; i < sql.tables.length; i++) { + sqls[i] = sql.sql.replaceFirst(firstTable, sql.tables[i]); + PreparedStatement prestmt = conn.prepareStatement(sqls[i]); + int index = 0; + if (sql.blobs != null) { + for (byte[] param : sql.blobs) { + Blob blob = conn.createBlob(); + blob.setBytes(1, param); + prestmt.setBlob(++index, blob); + } + } + prestmt.addBatch(); + prestmts.add(prestmt); + } + if (info.isLoggable(logger, Level.FINEST, sql.sql)) { + logger.finest(info.getType().getSimpleName() + " updateColumn sql=" + Arrays.toString(sqls)); + } + int c1 = 0; + for (PreparedStatement prestmt : prestmts) { + int[] cs = prestmt.executeBatch(); + for (int cc : cs) { + c1 += cc; + } + prestmt.close(); + } + c = c1; + conn.commit(); + slowLog(s, sqls); + return c; } + } else { + throw se; } - prestmt.close(); - conn.commit(); - slowLog(s, sql.sql); - return c; - } else { - if (info.isLoggable(logger, Level.FINEST, sql.sql)) { - logger.finest(info.getType().getSimpleName() + " update sql=" + sql); - } - final Statement stmt = conn.createStatement(); - int c = stmt.executeUpdate(sql.sql); - stmt.close(); - conn.commit(); - slowLog(s, sql.sql); - return c; } + } catch (SourceException sex) { + throw sex; } catch (SQLException e) { if (conn != null) { try { @@ -1062,29 +1166,6 @@ public class DataJdbcSource extends DataSqlSource { } catch (SQLException se) { } } - if (isTableNotExist(info, e.getSQLState())) { - if (info.getTableStrategy() == null) { - String[] tableSqls = createTableSqls(info); - if (tableSqls != null) { - try { - Statement st = conn.createStatement(); - if (tableSqls.length == 1) { - st.execute(tableSqls[0]); - } else { - for (String tableSql : tableSqls) { - st.addBatch(tableSql); - } - st.executeBatch(); - } - st.close(); - } catch (SQLException e2) { - } - } - return 0; - } else { - throw new SourceException(e); - } - } throw new SourceException(e); } finally { if (conn != null) { diff --git a/src/main/java/org/redkale/source/DataMemorySource.java b/src/main/java/org/redkale/source/DataMemorySource.java index b83898450..2eb4b43dd 100644 --- a/src/main/java/org/redkale/source/DataMemorySource.java +++ b/src/main/java/org/redkale/source/DataMemorySource.java @@ -142,7 +142,7 @@ public class DataMemorySource extends DataSqlSource implements SearchSource { } @Override - protected int updateColumnDB(EntityInfo info, Flipper flipper, SqlInfo sql) { + protected int updateColumnDB(EntityInfo info, Flipper flipper, UpdateSqlInfo sql) { return 0; } @@ -212,7 +212,7 @@ public class DataMemorySource extends DataSqlSource implements SearchSource { } @Override - protected CompletableFuture updateColumnDBAsync(EntityInfo info, Flipper flipper, SqlInfo sql) { + protected CompletableFuture updateColumnDBAsync(EntityInfo info, Flipper flipper, UpdateSqlInfo sql) { return CompletableFuture.completedFuture(0); } diff --git a/src/main/java/org/redkale/source/DataSqlSource.java b/src/main/java/org/redkale/source/DataSqlSource.java index d37408991..bac8a0f00 100644 --- a/src/main/java/org/redkale/source/DataSqlSource.java +++ b/src/main/java/org/redkale/source/DataSqlSource.java @@ -679,7 +679,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi protected abstract CompletableFuture updateEntityDBAsync(final EntityInfo info, T... entitys); //更新纪录 - protected abstract CompletableFuture updateColumnDBAsync(final EntityInfo info, Flipper flipper, final SqlInfo sql); + protected abstract CompletableFuture updateColumnDBAsync(final EntityInfo info, Flipper flipper, final UpdateSqlInfo sql); //查询Number Map数据 protected abstract CompletableFuture> getNumberMapDBAsync(final EntityInfo info, String[] tables, final String sql, final FilterFuncColumn... columns); @@ -731,7 +731,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } //更新纪录 - protected int updateColumnDB(final EntityInfo info, Flipper flipper, final SqlInfo sql) { + protected int updateColumnDB(final EntityInfo info, Flipper flipper, final UpdateSqlInfo sql) { return updateColumnDBAsync(info, flipper, sql).join(); } @@ -1410,7 +1410,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return updateCache(info, -1, pk, column, colval); } - SqlInfo sql = updateColumnSql(info, pk, column, colval); + UpdateSqlInfo sql = updateColumnSql(info, pk, column, colval); if (isAsync()) { int rs = updateColumnDBAsync(info, null, sql).join(); updateCache(info, rs, pk, column, colval); @@ -1429,7 +1429,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(updateCache(info, -1, pk, column, colval)); } - SqlInfo sql = updateColumnSql(info, pk, column, colval); + UpdateSqlInfo sql = updateColumnSql(info, pk, column, colval); if (isAsync()) { return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { if (t != null) { @@ -1449,13 +1449,13 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } } - protected SqlInfo updateColumnSql(final EntityInfo info, Serializable pk, String column, final Serializable colval) { + protected UpdateSqlInfo 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[]) { - return new SqlInfo("UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + "=" + prepareParamSign(1) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter), (byte[]) val); + return new UpdateSqlInfo("UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + "=" + prepareParamSign(1) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter), (byte[]) val); } else { - return new SqlInfo("UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + "=" + return new UpdateSqlInfo("UPDATE " + info.getTable(pk) + " SET " + info.getSQLColumn(null, column) + "=" + info.formatSQLValue(column, val, sqlFormatter) + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter)); } } @@ -1478,7 +1478,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return updateCache(info, -1, column, colval, node); } - SqlInfo sql = updateColumnSql(info, column, colval, node); + UpdateSqlInfo sql = updateColumnSql(info, column, colval, node); if (isAsync()) { int rs = updateColumnDBAsync(info, null, sql).join(); updateCache(info, rs, column, colval, node); @@ -1496,7 +1496,7 @@ 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); + UpdateSqlInfo sql = updateColumnSql(info, column, colval, node); if (isAsync()) { return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { if (t != null) { @@ -1516,7 +1516,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } } - protected SqlInfo updateColumnSql(final EntityInfo info, final String column, final Serializable colval, final FilterNode node) { + protected UpdateSqlInfo 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); @@ -1534,31 +1534,17 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi String[] tables = info.getTables(node); String sql; if (val instanceof byte[]) { - if (tables.length == 1) { - sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) - + " SET " + info.getSQLColumn(alias, column) + "=" + prepareParamSign(1) - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - } else { - sql = "UPDATE " + prepareParamSign(2) + " a " + (join1 == null ? "" : (", " + join1)) - + " SET " + info.getSQLColumn(alias, column) + "=" + prepareParamSign(1) - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - } - return new SqlInfo(sql, tables.length == 1 ? null : tables, (byte[]) val); + sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + + " SET " + info.getSQLColumn(alias, column) + "=" + prepareParamSign(1) + + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) + : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); + return new UpdateSqlInfo(sql, tables.length == 1 ? null : tables, (byte[]) val); } else { - if (tables.length == 1) { - sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) - + " SET " + info.getSQLColumn(alias, column) + "=" + info.formatSQLValue(val, sqlFormatter) - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - } else { - sql = "UPDATE " + prepareParamSign(1) + " a " + (join1 == null ? "" : (", " + join1)) - + " SET " + info.getSQLColumn(alias, column) + "=" + info.formatSQLValue(val, sqlFormatter) - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - } - return new SqlInfo(sql, tables.length == 1 ? null : tables); + sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + + " SET " + info.getSQLColumn(alias, column) + "=" + info.formatSQLValue(val, sqlFormatter) + + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) + : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); + return new UpdateSqlInfo(sql, tables.length == 1 ? null : tables); } } @@ -1582,7 +1568,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return updateCache(info, -1, pk, values); } - SqlInfo sql = updateColumnSql(info, pk, values); + UpdateSqlInfo sql = updateColumnSql(info, pk, values); if (isAsync()) { int rs = updateColumnDBAsync(info, null, sql).join(); updateCache(info, rs, pk, values); @@ -1603,7 +1589,7 @@ 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); + UpdateSqlInfo sql = updateColumnSql(info, pk, values); if (isAsync()) { return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { if (t != null) { @@ -1623,7 +1609,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } } - protected SqlInfo updateColumnSql(final EntityInfo info, final Serializable pk, final ColumnValue... values) { + protected UpdateSqlInfo updateColumnSql(final EntityInfo info, final Serializable pk, final ColumnValue... values) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; @@ -1653,7 +1639,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi throw new SourceException("update non column-value array"); } String sql = "UPDATE " + info.getTable(pk) + " SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(info.getPrimarySQLColumn(), pk, sqlFormatter); - return new SqlInfo(sql, blobs); + return new UpdateSqlInfo(sql, blobs); } @Override @@ -1665,7 +1651,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (isOnlyCache(info)) { return updateCache(info, -1, node, flipper, values); } - SqlInfo sql = updateColumnSql(info, node, flipper, values); + UpdateSqlInfo sql = updateColumnSql(info, node, flipper, values); if (isAsync()) { int rs = updateColumnDBAsync(info, null, sql).join(); updateCache(info, rs, node, flipper, values); @@ -1686,7 +1672,7 @@ 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); + UpdateSqlInfo sql = updateColumnSql(info, node, flipper, values); if (isAsync()) { return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { if (t != null) { @@ -1706,7 +1692,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } } - protected SqlInfo updateColumnSql(final EntityInfo info, final FilterNode node, final Flipper flipper, final ColumnValue... values) { + protected UpdateSqlInfo updateColumnSql(final EntityInfo info, final FilterNode node, final Flipper flipper, final ColumnValue... values) { StringBuilder setsql = new StringBuilder(); List blobs = null; int index = 0; @@ -1752,33 +1738,18 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi if (pgsql && flipper != null && flipper.getLimit() > 0) { String wherestr = ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - if (tables.length == 1) { - sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql - + " WHERE " + info.getPrimarySQLColumn() + " IN (SELECT " + info.getPrimaryColumn() + " FROM " + tables[0] - + wherestr + info.createSQLOrderby(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit() + ")"; - } else { - String sign = prepareParamSign(++index); - sql = "UPDATE " + sign + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql - + " WHERE " + info.getPrimarySQLColumn() + " IN (SELECT " + info.getPrimaryColumn() + " FROM " + sign - + wherestr + info.createSQLOrderby(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit() + ")"; - } + sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql + + " WHERE " + info.getPrimarySQLColumn() + " IN (SELECT " + info.getPrimaryColumn() + " FROM " + tables[0] + + wherestr + info.createSQLOrderby(flipper) + " OFFSET 0 LIMIT " + flipper.getLimit() + ")"; + } else { - if (tables.length == 1) { - sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))) - + info.createSQLOrderby(flipper) - + (("mysql".equals(dbtype()) && flipper != null && flipper.getLimit() > 0) ? (" LIMIT " + flipper.getLimit()) : ""); - } else { - String sign = prepareParamSign(++index); - sql = "UPDATE " + sign + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))) - + info.createSQLOrderby(flipper) - + (("mysql".equals(dbtype()) && flipper != null && flipper.getLimit() > 0) ? (" LIMIT " + flipper.getLimit()) : ""); - } + sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql + + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) + : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))) + + info.createSQLOrderby(flipper) + + (("mysql".equals(dbtype()) && flipper != null && flipper.getLimit() > 0) ? (" LIMIT " + flipper.getLimit()) : ""); } - return new SqlInfo(sql, tables.length == 1 ? null : tables, blobs); + return new UpdateSqlInfo(sql, tables.length == 1 ? null : tables, blobs); } //返回不存在的字段名,null表示字段都合法; @@ -1813,7 +1784,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return updateCache(info, -1, false, entity, null, selects); } - SqlInfo sql = updateColumnSql(info, false, entity, null, selects); + UpdateSqlInfo sql = updateColumnSql(info, false, entity, null, selects); if (isAsync()) { int rs = updateColumnDBAsync(info, null, sql).join(); updateCache(info, rs, false, entity, null, selects); @@ -1840,7 +1811,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(updateCache(info, -1, false, entity, null, selects)); } - SqlInfo sql = updateColumnSql(info, false, entity, null, selects); + UpdateSqlInfo sql = updateColumnSql(info, false, entity, null, selects); if (isAsync()) { return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { if (t != null) { @@ -1875,7 +1846,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return updateCache(info, -1, true, entity, node, selects); } - SqlInfo sql = updateColumnSql(info, true, entity, node, selects); + UpdateSqlInfo sql = updateColumnSql(info, true, entity, node, selects); if (isAsync()) { int rs = updateColumnDBAsync(info, null, sql).join(); updateCache(info, rs, true, entity, node, selects); @@ -1902,7 +1873,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return CompletableFuture.completedFuture(updateCache(info, -1, true, entity, node, selects)); } - SqlInfo sql = updateColumnSql(info, true, entity, node, selects); + UpdateSqlInfo sql = updateColumnSql(info, true, entity, node, selects); if (isAsync()) { return updateColumnDBAsync(info, null, sql).whenComplete((rs, t) -> { if (t != null) { @@ -1922,7 +1893,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } } - protected SqlInfo updateColumnSql(final EntityInfo info, final boolean needNode, final T entity, final FilterNode node, final SelectColumn selects) { + protected UpdateSqlInfo 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; @@ -1963,22 +1934,14 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } String sql; String[] tables = info.getTables(node); - if (tables.length == 1) { - sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - return new SqlInfo(sql, blobs); - } else { - String sign = prepareParamSign(++index); - sql = "UPDATE " + sign + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql - + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) - : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); - return new SqlInfo(sql, tables, blobs); - } + sql = "UPDATE " + tables[0] + " a " + (join1 == null ? "" : (", " + join1)) + " SET " + setsql + + ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2)) + : (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))); + return new UpdateSqlInfo(sql, tables.length == 1 ? null : tables, blobs); } else { final Serializable id = (Serializable) info.getSQLValue(info.getPrimary(), entity); String sql = "UPDATE " + info.getTable(id) + " a SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + "=" + info.formatSQLValue(id, sqlFormatter); - return new SqlInfo(sql, blobs); + return new UpdateSqlInfo(sql, blobs); } } @@ -3185,23 +3148,23 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi return querySheetDBAsync(info, readCache, needTotal, distinct, selects, flipper, node); } - protected static class SqlInfo { + protected static class UpdateSqlInfo { public String sql; //prepare-sql时表名参数只能是最后一个 - public String[] tables; + public String[] tables; //存在值则长度必然大于1,sql为[0]构建的sql public List blobs; //要么null,要么有内容,不能是empty-list - public SqlInfo(String sql, byte[]... blobs) { + public UpdateSqlInfo(String sql, byte[]... blobs) { this(sql, null, blobs); } - public SqlInfo(String sql, List blobs) { + public UpdateSqlInfo(String sql, List blobs) { this(sql, null, blobs); } - public SqlInfo(String sql, String[] tables, byte[]... blobs) { + public UpdateSqlInfo(String sql, String[] tables, byte[]... blobs) { this.sql = sql; this.tables = tables; if (blobs.length > 0) { @@ -3212,10 +3175,10 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi } } - public SqlInfo(String sql, String[] tables, List blobs) { + public UpdateSqlInfo(String sql, String[] tables, List blobs) { this.sql = sql; this.tables = tables; - this.blobs = blobs.isEmpty() ? null : blobs; + this.blobs = blobs == null || blobs.isEmpty() ? null : blobs; } }