优化DataJdbcSource

This commit is contained in:
Redkale
2023-01-01 20:50:29 +08:00
parent 6f6c3f70ff
commit f09a323b20
3 changed files with 200 additions and 156 deletions

View File

@@ -999,62 +999,166 @@ public class DataJdbcSource extends DataSqlSource {
}
@Override
protected <T> CompletableFuture<Integer> updateColumnDBAsync(EntityInfo<T> info, Flipper flipper, SqlInfo sql) {
protected <T> CompletableFuture<Integer> updateColumnDBAsync(EntityInfo<T> info, Flipper flipper, UpdateSqlInfo sql) {
return supplyAsync(() -> updateColumnDB(info, flipper, sql));
}
@Override
protected <T> int updateColumnDB(EntityInfo<T> info, Flipper flipper, SqlInfo sql) { //String sql, boolean prepared, Object... blobs) {
protected <T> int updateColumnDB(EntityInfo<T> 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<PreparedStatement> 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<String> 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<PreparedStatement> 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) {

View File

@@ -142,7 +142,7 @@ public class DataMemorySource extends DataSqlSource implements SearchSource {
}
@Override
protected <T> int updateColumnDB(EntityInfo<T> info, Flipper flipper, SqlInfo sql) {
protected <T> int updateColumnDB(EntityInfo<T> info, Flipper flipper, UpdateSqlInfo sql) {
return 0;
}
@@ -212,7 +212,7 @@ public class DataMemorySource extends DataSqlSource implements SearchSource {
}
@Override
protected <T> CompletableFuture<Integer> updateColumnDBAsync(EntityInfo<T> info, Flipper flipper, SqlInfo sql) {
protected <T> CompletableFuture<Integer> updateColumnDBAsync(EntityInfo<T> info, Flipper flipper, UpdateSqlInfo sql) {
return CompletableFuture.completedFuture(0);
}

View File

@@ -679,7 +679,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
protected abstract <T> CompletableFuture<Integer> updateEntityDBAsync(final EntityInfo<T> info, T... entitys);
//更新纪录
protected abstract <T> CompletableFuture<Integer> updateColumnDBAsync(final EntityInfo<T> info, Flipper flipper, final SqlInfo sql);
protected abstract <T> CompletableFuture<Integer> updateColumnDBAsync(final EntityInfo<T> info, Flipper flipper, final UpdateSqlInfo sql);
//查询Number Map数据
protected abstract <T, N extends Number> CompletableFuture<Map<String, N>> getNumberMapDBAsync(final EntityInfo<T> info, String[] tables, final String sql, final FilterFuncColumn... columns);
@@ -731,7 +731,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
}
//更新纪录
protected <T> int updateColumnDB(final EntityInfo<T> info, Flipper flipper, final SqlInfo sql) {
protected <T> int updateColumnDB(final EntityInfo<T> 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 <T> SqlInfo updateColumnSql(final EntityInfo<T> info, Serializable pk, String column, final Serializable colval) {
protected <T> UpdateSqlInfo updateColumnSql(final EntityInfo<T> 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 <T> SqlInfo updateColumnSql(final EntityInfo<T> info, final String column, final Serializable colval, final FilterNode node) {
protected <T> UpdateSqlInfo updateColumnSql(final EntityInfo<T> info, final String column, final Serializable colval, final FilterNode node) {
Map<Class, String> 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 <T> SqlInfo updateColumnSql(final EntityInfo<T> info, final Serializable pk, final ColumnValue... values) {
protected <T> UpdateSqlInfo updateColumnSql(final EntityInfo<T> info, final Serializable pk, final ColumnValue... values) {
StringBuilder setsql = new StringBuilder();
List<byte[]> 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 <T> SqlInfo updateColumnSql(final EntityInfo<T> info, final FilterNode node, final Flipper flipper, final ColumnValue... values) {
protected <T> UpdateSqlInfo updateColumnSql(final EntityInfo<T> info, final FilterNode node, final Flipper flipper, final ColumnValue... values) {
StringBuilder setsql = new StringBuilder();
List<byte[]> 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 <T> SqlInfo updateColumnSql(final EntityInfo<T> info, final boolean needNode, final T entity, final FilterNode node, final SelectColumn selects) {
protected <T> UpdateSqlInfo updateColumnSql(final EntityInfo<T> info, final boolean needNode, final T entity, final FilterNode node, final SelectColumn selects) {
StringBuilder setsql = new StringBuilder();
List<byte[]> 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; //存在值则长度必然大于1sql为[0]构建的sql
public List<byte[]> 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<byte[]> blobs) {
public UpdateSqlInfo(String sql, List<byte[]> 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<byte[]> blobs) {
public UpdateSqlInfo(String sql, String[] tables, List<byte[]> blobs) {
this.sql = sql;
this.tables = tables;
this.blobs = blobs.isEmpty() ? null : blobs;
this.blobs = blobs == null || blobs.isEmpty() ? null : blobs;
}
}