This commit is contained in:
@@ -448,7 +448,7 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
} else {
|
||||
char[] sqlchars = sql.toCharArray();
|
||||
sqls = new String[values.length];
|
||||
String[] ps = new String[attrs.length];
|
||||
CharSequence[] ps = new CharSequence[attrs.length];
|
||||
int index = 0;
|
||||
for (final T value : values) {
|
||||
int i = 0;
|
||||
@@ -525,7 +525,7 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
|
||||
public <T> void insertCache(Class<T> clazz, T... values) {
|
||||
if (values.length == 0) return;
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityCache<T> cache = info.getCache();
|
||||
if (cache == null) return;
|
||||
for (T value : values) {
|
||||
@@ -721,7 +721,7 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
} else {
|
||||
char[] sqlchars = info.updateSQL.toCharArray();
|
||||
sqls = new String[values.length];
|
||||
String[] ps = new String[attrs.length];
|
||||
CharSequence[] ps = new CharSequence[attrs.length];
|
||||
int index = 0;
|
||||
for (final T value : values) {
|
||||
int i = 0;
|
||||
@@ -1175,26 +1175,44 @@ public final class DataDefaultSource implements DataSource, Nameable {
|
||||
|
||||
@Override
|
||||
public <T> T find(Class<T> clazz, final SelectColumn selects, Serializable pk) {
|
||||
String column = loadEntityInfo(clazz).getPrimary().field();
|
||||
Sheet<T> sheet = querySheet(clazz, selects, FLIPPER_ONE, FilterNode.create(column, pk));
|
||||
return sheet.isEmpty() ? null : sheet.list().get(0);
|
||||
final EntityInfo<T> info = loadEntityInfo(clazz);
|
||||
final EntityCache<T> cache = info.getCache();
|
||||
if (cache != null && cache.isFullLoaded()) return cache.find(selects, pk);
|
||||
|
||||
final Connection conn = createReadSQLConnection();
|
||||
try {
|
||||
final SelectColumn sels = selects;
|
||||
final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(pk);
|
||||
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> T findByColumn(Class<T> clazz, String column, Serializable key) {
|
||||
return find(clazz, FilterNode.create(column, key));
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T find(final Class<T> clazz, final FilterBean bean) {
|
||||
Sheet<T> sheet = querySheet(clazz, FLIPPER_ONE, bean);
|
||||
return sheet.isEmpty() ? null : sheet.list().get(0);
|
||||
List<T> list = queryList(clazz, FLIPPER_ONE, bean);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T find(final Class<T> clazz, final FilterNode node) {
|
||||
Sheet<T> sheet = querySheet(clazz, FLIPPER_ONE, node);
|
||||
return sheet.isEmpty() ? null : sheet.list().get(0);
|
||||
List<T> list = queryList(clazz, FLIPPER_ONE, node);
|
||||
return list.isEmpty() ? null : list.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -109,6 +109,18 @@ public final class EntityCache<T> {
|
||||
return rs == null ? null : (needcopy ? reproduce.copy(this.creator.create(), rs) : rs);
|
||||
}
|
||||
|
||||
public T find(final SelectColumn selects, final Serializable id) {
|
||||
if (id == null) return null;
|
||||
T rs = map.get(id);
|
||||
if (rs == null) return null;
|
||||
if (selects == null) return (needcopy ? reproduce.copy(this.creator.create(), rs) : rs);
|
||||
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(final Predicate<T> filter) {
|
||||
return (filter != null) && this.list.stream().filter(filter).findFirst().isPresent();
|
||||
}
|
||||
|
||||
@@ -44,7 +44,9 @@ public final class EntityInfo<T> {
|
||||
|
||||
//key是field的name, 不是sql字段。
|
||||
//存放所有与数据库对应的字段, 包括主键
|
||||
private final HashMap<String, Attribute<T, Serializable>> attributes = new HashMap<>();
|
||||
private final HashMap<String, Attribute<T, Serializable>> attributeMap = new HashMap<>();
|
||||
|
||||
final Attribute<T, Serializable>[] attributes;
|
||||
|
||||
//key是field的name, value是Column的别名,即数据库表的字段名
|
||||
//只有field.name 与 Column.name不同才存放在aliasmap里.
|
||||
@@ -187,11 +189,12 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
queryattrs.add(attr);
|
||||
fields.add(fieldname);
|
||||
attributes.put(fieldname, attr);
|
||||
attributeMap.put(fieldname, attr);
|
||||
}
|
||||
} while ((cltmp = cltmp.getSuperclass()) != Object.class);
|
||||
this.primary = idAttr0;
|
||||
this.aliasmap = aliasmap0;
|
||||
this.attributes = attributeMap.values().toArray(new Attribute[attributeMap.size()]);
|
||||
this.queryAttributes = queryattrs.toArray(new Attribute[queryattrs.size()]);
|
||||
this.insertAttributes = insertattrs.toArray(new Attribute[insertattrs.size()]);
|
||||
this.updateAttributes = updateattrs.toArray(new Attribute[updateattrs.size()]);
|
||||
@@ -270,12 +273,12 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
|
||||
public void forEachAttribute(BiConsumer<String, Attribute<T, Serializable>> action) {
|
||||
this.attributes.forEach(action);
|
||||
this.attributeMap.forEach(action);
|
||||
}
|
||||
|
||||
public Attribute<T, Serializable> getAttribute(String fieldname) {
|
||||
if (fieldname == null) return null;
|
||||
return this.attributes.get(fieldname);
|
||||
return this.attributeMap.get(fieldname);
|
||||
}
|
||||
|
||||
public Attribute<T, Serializable> getUpdateAttribute(String fieldname) {
|
||||
@@ -325,14 +328,14 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
|
||||
public Map<String, Attribute<T, Serializable>> getAttributes() {
|
||||
return attributes;
|
||||
return attributeMap;
|
||||
}
|
||||
|
||||
public boolean isLoggable(Level l) {
|
||||
return l.intValue() >= this.logLevel;
|
||||
}
|
||||
|
||||
public T getValue(SelectColumn sels, ResultSet set) throws SQLException {
|
||||
public T getValue(final SelectColumn sels, final ResultSet set) throws SQLException {
|
||||
T obj = creator.create();
|
||||
for (Attribute<T, Serializable> attr : queryAttributes) {
|
||||
if (sels == null || sels.validate(attr.field())) {
|
||||
|
||||
@@ -204,7 +204,7 @@ public class FilterNode {
|
||||
sb.append(info.getSQLColumn(column)).append(' ').append(express.value());
|
||||
return sb;
|
||||
}
|
||||
final StringBuilder val = formatToString(express, getElementValue(bean));
|
||||
final CharSequence val = formatToString(express, getElementValue(bean));
|
||||
if (val == null) return null;
|
||||
StringBuilder sb = new StringBuilder(32);
|
||||
if (tabalis != null) sb.append(tabalis).append('.');
|
||||
@@ -824,14 +824,14 @@ public class FilterNode {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected static String formatToString(Object value) {
|
||||
StringBuilder sb = formatToString(null, value);
|
||||
protected static CharSequence formatToString(Object value) {
|
||||
CharSequence sb = formatToString(null, value);
|
||||
return sb == null ? null : sb.toString();
|
||||
}
|
||||
|
||||
private static StringBuilder formatToString(FilterExpress express, Object value) {
|
||||
private static CharSequence formatToString(FilterExpress express, Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof Number) return new StringBuilder().append(value);
|
||||
if (value instanceof Number) return String.valueOf(value);
|
||||
if (value instanceof CharSequence) {
|
||||
if (express == LIKE || express == NOTLIKE) {
|
||||
value = "%" + value + '%';
|
||||
@@ -889,7 +889,7 @@ public class FilterNode {
|
||||
}
|
||||
return sb.append(')');
|
||||
}
|
||||
return new StringBuilder().append(value);
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public final Serializable getValue() {
|
||||
|
||||
Reference in New Issue
Block a user