This commit is contained in:
@@ -598,8 +598,7 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
if (keys.length == 0) return;
|
||||
try {
|
||||
if (!info.isVirtualEntity()) {
|
||||
String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn()
|
||||
+ " IN " + formatToString(keys);
|
||||
String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn() + " IN " + formatToString(keys);
|
||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
|
||||
final Statement stmt = conn.createStatement();
|
||||
stmt.execute(sql);
|
||||
@@ -1020,8 +1019,7 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
}
|
||||
}
|
||||
if (!virtual) {
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + setsql
|
||||
+ " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||
if (debug.get()) logger.finest(value.getClass().getSimpleName() + ": " + sql);
|
||||
final Statement stmt = conn.createStatement();
|
||||
stmt.execute(sql);
|
||||
@@ -1199,35 +1197,101 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
|
||||
@Override
|
||||
public <T> T findByColumn(final Class<T> clazz, final String column, final Serializable key) {
|
||||
List<T> list = queryList(clazz, FLIPPER_ONE, column, key);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
return find(clazz, null, FilterNode.create(column, key), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T find(final Class<T> clazz, final FilterBean bean) {
|
||||
List<T> list = queryList(clazz, FLIPPER_ONE, bean);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
return find(clazz, null, null, bean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T find(final Class<T> clazz, final FilterNode node) {
|
||||
List<T> list = queryList(clazz, FLIPPER_ONE, node);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
return find(clazz, null, node, null);
|
||||
}
|
||||
|
||||
private <T> T find(final Class<T> clazz, final SelectColumn selects, FilterNode node, final FilterBean bean) {
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityCache<T> cache = info.getCache();
|
||||
if (node == null && bean != null) node = loadFilterBeanNode(bean.getClass());
|
||||
if (cache != null && cache.isFullLoaded() && (node == null || node.isCacheUseable())) return cache.find(selects, node, bean);
|
||||
|
||||
final Connection conn = createReadSQLConnection();
|
||||
try {
|
||||
final SelectColumn sels = selects;
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql);
|
||||
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
final ResultSet set = ps.executeQuery();
|
||||
T rs = set.next() ? info.getValue(sels, set) : null;
|
||||
set.close();
|
||||
ps.close();
|
||||
return rs;
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} finally {
|
||||
closeSQLConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean exists(Class<T> clazz, Serializable pk) {
|
||||
return find(clazz, pk) != null;
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityCache<T> cache = info.getCache();
|
||||
if (cache != null && cache.isFullLoaded()) return cache.exists(pk);
|
||||
|
||||
final Connection conn = createReadSQLConnection();
|
||||
try {
|
||||
final String sql = "SELECT COUNT(*) FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(pk);
|
||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " exists sql=" + sql);
|
||||
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
final ResultSet set = ps.executeQuery();
|
||||
boolean rs = set.next() ? (set.getInt(1) > 0) : false;
|
||||
set.close();
|
||||
ps.close();
|
||||
return rs;
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} finally {
|
||||
closeSQLConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean exists(final Class<T> clazz, final FilterNode node) {
|
||||
return find(clazz, node) != null;
|
||||
return exists(clazz, node, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean exists(final Class<T> clazz, final FilterBean bean) {
|
||||
return find(clazz, bean) != null;
|
||||
return exists(clazz, null, bean);
|
||||
}
|
||||
|
||||
private <T> boolean exists(final Class<T> clazz, FilterNode node, final FilterBean bean) {
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityCache<T> cache = info.getCache();
|
||||
if (node == null && bean != null) node = loadFilterBeanNode(bean.getClass());
|
||||
if (cache != null && cache.isFullLoaded() && (node == null || node.isCacheUseable())) return cache.exists(node, bean);
|
||||
|
||||
final Connection conn = createReadSQLConnection();
|
||||
try {
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final String sql = "SELECT COUNT(a.*) FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " exists sql=" + sql);
|
||||
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
|
||||
final ResultSet set = ps.executeQuery();
|
||||
boolean rs = set.next() ? (set.getInt(1) > 0) : false;
|
||||
set.close();
|
||||
ps.close();
|
||||
return rs;
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} finally {
|
||||
closeSQLConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------list set----------------------------
|
||||
|
||||
@@ -121,6 +121,55 @@ public final class EntityCache<T> {
|
||||
return t;
|
||||
}
|
||||
|
||||
public T find(final SelectColumn selects, FilterNode node) {
|
||||
return find(selects, node, null);
|
||||
}
|
||||
|
||||
public T find(final SelectColumn selects, FilterNode node, final FilterBean bean) {
|
||||
if (node == null && bean != null) node = FilterBeanNode.load(bean.getClass());
|
||||
final Predicate<T> filter = node == null ? null : node.createPredicate(this, bean);
|
||||
Stream<T> stream = this.list.stream();
|
||||
if (filter != null) stream = stream.filter(filter);
|
||||
Optional<T> opt = stream.findFirst();
|
||||
if (!opt.isPresent()) return null;
|
||||
if (selects == null) return (needcopy ? reproduce.copy(this.creator.create(), opt.get()) : opt.get());
|
||||
T rs = opt.get();
|
||||
T t = this.creator.create();
|
||||
for (Attribute attr : this.info.attributes) {
|
||||
if (selects.validate(attr.field())) attr.set(t, attr.get(rs));
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public boolean exists(Serializable id) {
|
||||
if (id == null) return false;
|
||||
final Class atype = this.primary.type();
|
||||
if (id.getClass() != atype && id instanceof Number) {
|
||||
if (atype == int.class || atype == Integer.class) {
|
||||
id = ((Number) id).intValue();
|
||||
} else if (atype == long.class || atype == Long.class) {
|
||||
id = ((Number) id).longValue();
|
||||
} else if (atype == short.class || atype == Short.class) {
|
||||
id = ((Number) id).shortValue();
|
||||
} else if (atype == float.class || atype == Float.class) {
|
||||
id = ((Number) id).floatValue();
|
||||
} else if (atype == byte.class || atype == Byte.class) {
|
||||
id = ((Number) id).byteValue();
|
||||
} else if (atype == double.class || atype == Double.class) {
|
||||
id = ((Number) id).doubleValue();
|
||||
}
|
||||
}
|
||||
return this.map.containsKey(id);
|
||||
}
|
||||
|
||||
public boolean exists(FilterNode node, final FilterBean bean) {
|
||||
if (node == null && bean != null) node = FilterBeanNode.load(bean.getClass());
|
||||
final Predicate<T> filter = node == null ? null : node.createPredicate(this, bean);
|
||||
Stream<T> stream = this.list.stream();
|
||||
if (filter != null) stream = stream.filter(filter);
|
||||
return stream.findFirst().isPresent();
|
||||
}
|
||||
|
||||
public boolean exists(final Predicate<T> filter) {
|
||||
return (filter != null) && this.list.stream().filter(filter).findFirst().isPresent();
|
||||
}
|
||||
@@ -376,7 +425,7 @@ public final class EntityCache<T> {
|
||||
T rs = this.map.get(id);
|
||||
if (rs == null) return rs;
|
||||
Number numb = (Number) attr.get(rs);
|
||||
return updateColumnIncrAndOr(id, attr, rs, (numb == null) ? orvalue : (numb.longValue() | orvalue));
|
||||
return updateColumnIncrAndOr(attr, rs, (numb == null) ? orvalue : (numb.longValue() | orvalue));
|
||||
}
|
||||
|
||||
public <V> T updateColumnAnd(final Serializable id, Attribute<T, V> attr, final long andvalue) {
|
||||
@@ -384,7 +433,7 @@ public final class EntityCache<T> {
|
||||
T rs = this.map.get(id);
|
||||
if (rs == null) return rs;
|
||||
Number numb = (Number) attr.get(rs);
|
||||
return updateColumnIncrAndOr(id, attr, rs, (numb == null) ? 0 : (numb.longValue() & andvalue));
|
||||
return updateColumnIncrAndOr(attr, rs, (numb == null) ? 0 : (numb.longValue() & andvalue));
|
||||
}
|
||||
|
||||
public <V> T updateColumnIncrement(final Serializable id, Attribute<T, V> attr, final long incvalue) {
|
||||
@@ -392,10 +441,10 @@ public final class EntityCache<T> {
|
||||
T rs = this.map.get(id);
|
||||
if (rs == null) return rs;
|
||||
Number numb = (Number) attr.get(rs);
|
||||
return updateColumnIncrAndOr(id, attr, rs, (numb == null) ? incvalue : (numb.longValue() + incvalue));
|
||||
return updateColumnIncrAndOr(attr, rs, (numb == null) ? incvalue : (numb.longValue() + incvalue));
|
||||
}
|
||||
|
||||
private <V> T updateColumnIncrAndOr(final Serializable id, Attribute<T, V> attr, final T rs, Number numb) {
|
||||
private <V> T updateColumnIncrAndOr(Attribute<T, V> attr, final T rs, Number numb) {
|
||||
final Class ft = attr.type();
|
||||
if (ft == int.class || ft == Integer.class) {
|
||||
numb = numb.intValue();
|
||||
|
||||
Reference in New Issue
Block a user