This commit is contained in:
@@ -48,6 +48,11 @@ public class DataSourceService implements DataSource, Service, AutoCloseable {
|
|||||||
return source.delete(clazz, node);
|
return source.delete(clazz, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> int delete(final Class<T> clazz, final Flipper flipper, FilterNode node) {
|
||||||
|
return source.delete(clazz, flipper, node);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> int update(T... values) {
|
public <T> int update(T... values) {
|
||||||
return source.update(values);
|
return source.update(values);
|
||||||
|
|||||||
@@ -547,17 +547,31 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
public <T> int delete(Class<T> clazz, FilterNode node) {
|
public <T> int delete(Class<T> clazz, FilterNode node) {
|
||||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||||
if (info.isVirtualEntity()) {
|
if (info.isVirtualEntity()) {
|
||||||
return delete(null, info, node);
|
return delete(null, info, null, node);
|
||||||
}
|
}
|
||||||
Connection conn = createWriteSQLConnection();
|
Connection conn = createWriteSQLConnection();
|
||||||
try {
|
try {
|
||||||
return delete(conn, info, node);
|
return delete(conn, info, null, node);
|
||||||
} finally {
|
} finally {
|
||||||
closeSQLConnection(conn);
|
closeSQLConnection(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> int delete(final Connection conn, final EntityInfo<T> info, final FilterNode node) {
|
@Override
|
||||||
|
public <T> int delete(Class<T> clazz, final Flipper flipper, FilterNode node) {
|
||||||
|
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||||
|
if (info.isVirtualEntity()) {
|
||||||
|
return delete(null, info, flipper, node);
|
||||||
|
}
|
||||||
|
Connection conn = createWriteSQLConnection();
|
||||||
|
try {
|
||||||
|
return delete(conn, info, flipper, node);
|
||||||
|
} finally {
|
||||||
|
closeSQLConnection(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> int delete(final Connection conn, final EntityInfo<T> info, final Flipper flipper, final FilterNode node) {
|
||||||
int c = -1;
|
int c = -1;
|
||||||
try {
|
try {
|
||||||
if (!info.isVirtualEntity()) {
|
if (!info.isVirtualEntity()) {
|
||||||
@@ -574,7 +588,8 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
}
|
}
|
||||||
String sql = "DELETE " + (this.readPool.isMysql() ? "a" : "") + " FROM " + info.getTable(node) + " a" + (join1 == null ? "" : (", " + join1))
|
String sql = "DELETE " + (this.readPool.isMysql() ? "a" : "") + " FROM " + info.getTable(node) + " a" + (join1 == null ? "" : (", " + join1))
|
||||||
+ ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2))
|
+ ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2))
|
||||||
: (" WHERE " + where + (join2 == null ? "" : (" AND " + join2))));
|
: (" WHERE " + where + (join2 == null ? "" : (" AND " + join2)))) + info.createSQLOrderby(flipper)
|
||||||
|
+ ((flipper == null || flipper.getLimit() < 1) ? "" : (" LIMIT " + flipper.getLimit()));
|
||||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
|
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
|
||||||
final Statement stmt = conn.createStatement();
|
final Statement stmt = conn.createStatement();
|
||||||
c = stmt.executeUpdate(sql);
|
c = stmt.executeUpdate(sql);
|
||||||
@@ -583,7 +598,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
//------------------------------------
|
//------------------------------------
|
||||||
final EntityCache<T> cache = info.getCache();
|
final EntityCache<T> cache = info.getCache();
|
||||||
if (cache == null) return c;
|
if (cache == null) return c;
|
||||||
Serializable[] ids = cache.delete(node);
|
Serializable[] ids = cache.delete(flipper, node);
|
||||||
if (cacheListener != null) cacheListener.deleteCache(info.getType(), ids);
|
if (cacheListener != null) cacheListener.deleteCache(info.getType(), ids);
|
||||||
return c >= 0 ? c : (ids == null ? 0 : ids.length);
|
return c >= 0 ? c : (ids == null ? 0 : ids.length);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@@ -941,7 +956,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
+ ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2))
|
+ ((where == null || where.length() == 0) ? (join2 == null ? "" : (" WHERE " + join2))
|
||||||
: (" WHERE " + where + (join2 == null ? "" : (" AND " + join2))));
|
: (" WHERE " + where + (join2 == null ? "" : (" AND " + join2))));
|
||||||
//注:LIMIT 仅支持MySQL 且在多表关联式会异常, 该BUG尚未解决
|
//注:LIMIT 仅支持MySQL 且在多表关联式会异常, 该BUG尚未解决
|
||||||
sql += info.createSQLOrderby(flipper) + (flipper == null ? "" : (" LIMIT " + flipper.getLimit()));
|
sql += info.createSQLOrderby(flipper) + ((flipper == null || flipper.getLimit() < 1) ? "" : (" LIMIT " + flipper.getLimit()));
|
||||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||||
final Statement stmt = conn.createStatement();
|
final Statement stmt = conn.createStatement();
|
||||||
c = stmt.executeUpdate(sql);
|
c = stmt.executeUpdate(sql);
|
||||||
@@ -1697,12 +1712,13 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
|||||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
|
||||||
final String sql = "SELECT a.* FROM " + info.getTable(node) + " a" + (join == null ? "" : join)
|
final String sql = "SELECT a.* FROM " + info.getTable(node) + " a" + (join == null ? "" : join)
|
||||||
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
|
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
|
||||||
if (debug.get() && info.isLoggable(Level.FINEST))
|
if (debug.get() && info.isLoggable(Level.FINEST)) {
|
||||||
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.getOffset() + "," + flipper.getLimit())));
|
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null || flipper.getLimit() < 1 ? "" : (" LIMIT " + flipper.getOffset() + "," + flipper.getLimit())));
|
||||||
|
}
|
||||||
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||||
final ResultSet set = ps.executeQuery();
|
final ResultSet set = ps.executeQuery();
|
||||||
if (flipper != null && flipper.getOffset() > 0) set.absolute(flipper.getOffset());
|
if (flipper != null && flipper.getOffset() > 0) set.absolute(flipper.getOffset());
|
||||||
final int limit = flipper == null ? Integer.MAX_VALUE : flipper.getLimit();
|
final int limit = flipper == null || flipper.getLimit() < 1 ? Integer.MAX_VALUE : flipper.getLimit();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ public interface DataSource {
|
|||||||
|
|
||||||
public <T> int delete(final Class<T> clazz, final FilterNode node);
|
public <T> int delete(final Class<T> clazz, final FilterNode node);
|
||||||
|
|
||||||
|
public <T> int delete(final Class<T> clazz, final Flipper flipper, final FilterNode node);
|
||||||
|
|
||||||
//------------------------update---------------------------
|
//------------------------update---------------------------
|
||||||
/**
|
/**
|
||||||
* 更新对象, 必须是Entity对象
|
* 更新对象, 必须是Entity对象
|
||||||
|
|||||||
@@ -350,7 +350,8 @@ public final class EntityCache<T> {
|
|||||||
Stream<T> stream = this.list.stream();
|
Stream<T> stream = this.list.stream();
|
||||||
if (filter != null) stream = stream.filter(filter);
|
if (filter != null) stream = stream.filter(filter);
|
||||||
if (comparator != null) stream = stream.sorted(comparator);
|
if (comparator != null) stream = stream.sorted(comparator);
|
||||||
if (flipper != null) stream = stream.skip(flipper.getOffset()).limit(flipper.getLimit());
|
if (flipper != null && flipper.getOffset() > 0) stream = stream.skip(flipper.getOffset());
|
||||||
|
if (flipper != null && flipper.getLimit() > 0) stream = stream.limit(flipper.getLimit());
|
||||||
final List<T> rs = new ArrayList<>();
|
final List<T> rs = new ArrayList<>();
|
||||||
if (selects == null) {
|
if (selects == null) {
|
||||||
Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.apply(creator.create(), x) : x);
|
Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.apply(creator.create(), x) : x);
|
||||||
@@ -400,9 +401,14 @@ public final class EntityCache<T> {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Serializable[] delete(final FilterNode node) {
|
public Serializable[] delete(final Flipper flipper, final FilterNode node) {
|
||||||
if (node == null || this.list.isEmpty()) return new Serializable[0];
|
if (node == null || this.list.isEmpty()) return new Serializable[0];
|
||||||
Object[] rms = this.list.stream().filter(node.createPredicate(this)).toArray();
|
final Comparator<T> comparator = createComparator(flipper);
|
||||||
|
Stream<T> stream = this.list.stream().filter(node.createPredicate(this));
|
||||||
|
if (comparator != null) stream = stream.sorted(comparator);
|
||||||
|
if (flipper != null && flipper.getOffset() > 0) stream = stream.skip(flipper.getOffset());
|
||||||
|
if (flipper != null && flipper.getLimit() > 0) stream = stream.limit(flipper.getLimit());
|
||||||
|
Object[] rms = stream.toArray();
|
||||||
Serializable[] ids = new Serializable[rms.length];
|
Serializable[] ids = new Serializable[rms.length];
|
||||||
int i = -1;
|
int i = -1;
|
||||||
for (Object o : rms) {
|
for (Object o : rms) {
|
||||||
@@ -483,7 +489,7 @@ public final class EntityCache<T> {
|
|||||||
Stream<T> stream = this.list.stream();
|
Stream<T> stream = this.list.stream();
|
||||||
final Comparator<T> comparator = createComparator(flipper);
|
final Comparator<T> comparator = createComparator(flipper);
|
||||||
if (comparator != null) stream = stream.sorted(comparator);
|
if (comparator != null) stream = stream.sorted(comparator);
|
||||||
if (flipper != null) stream = stream.limit(flipper.getLimit());
|
if (flipper != null && flipper.getLimit() > 0) stream = stream.limit(flipper.getLimit());
|
||||||
T[] rms = stream.filter(node.createPredicate(this)).toArray(len -> (T[]) Array.newInstance(type, len));
|
T[] rms = stream.filter(node.createPredicate(this)).toArray(len -> (T[]) Array.newInstance(type, len));
|
||||||
for (T rs : rms) {
|
for (T rs : rms) {
|
||||||
synchronized (rs) {
|
synchronized (rs) {
|
||||||
|
|||||||
Reference in New Issue
Block a user