From 051016f423e265b73ec1c001722bac64e6c242bf Mon Sep 17 00:00:00 2001 From: kamhung <22250530@qq.com> Date: Wed, 25 Nov 2015 16:50:46 +0800 Subject: [PATCH] --- .../redkale/source/DataDefaultSource.java | 90 ++++++++++++++++--- .../wentch/redkale/source/EntityCache.java | 57 +++++++++++- 2 files changed, 130 insertions(+), 17 deletions(-) diff --git a/src/com/wentch/redkale/source/DataDefaultSource.java b/src/com/wentch/redkale/source/DataDefaultSource.java index 0727869da..83ffb8303 100644 --- a/src/com/wentch/redkale/source/DataDefaultSource.java +++ b/src/com/wentch/redkale/source/DataDefaultSource.java @@ -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 findByColumn(final Class clazz, final String column, final Serializable key) { - List 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 find(final Class clazz, final FilterBean bean) { - List list = queryList(clazz, FLIPPER_ONE, bean); - return list.isEmpty() ? null : list.get(0); + return find(clazz, null, null, bean); } @Override public T find(final Class clazz, final FilterNode node) { - List list = queryList(clazz, FLIPPER_ONE, node); - return list.isEmpty() ? null : list.get(0); + return find(clazz, null, node, null); + } + + private T find(final Class clazz, final SelectColumn selects, FilterNode node, final FilterBean bean) { + final EntityInfo info = loadEntityInfo(clazz); + final EntityCache 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 boolean exists(Class clazz, Serializable pk) { - return find(clazz, pk) != null; + final EntityInfo info = loadEntityInfo(clazz); + final EntityCache 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 boolean exists(final Class clazz, final FilterNode node) { - return find(clazz, node) != null; + return exists(clazz, node, null); } @Override public boolean exists(final Class clazz, final FilterBean bean) { - return find(clazz, bean) != null; + return exists(clazz, null, bean); + } + + private boolean exists(final Class clazz, FilterNode node, final FilterBean bean) { + final EntityInfo info = loadEntityInfo(clazz); + final EntityCache 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---------------------------- diff --git a/src/com/wentch/redkale/source/EntityCache.java b/src/com/wentch/redkale/source/EntityCache.java index c5ebaef4e..2b59a69ea 100644 --- a/src/com/wentch/redkale/source/EntityCache.java +++ b/src/com/wentch/redkale/source/EntityCache.java @@ -121,6 +121,55 @@ public final class EntityCache { 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 filter = node == null ? null : node.createPredicate(this, bean); + Stream stream = this.list.stream(); + if (filter != null) stream = stream.filter(filter); + Optional 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 filter = node == null ? null : node.createPredicate(this, bean); + Stream stream = this.list.stream(); + if (filter != null) stream = stream.filter(filter); + return stream.findFirst().isPresent(); + } + public boolean exists(final Predicate filter) { return (filter != null) && this.list.stream().filter(filter).findFirst().isPresent(); } @@ -376,7 +425,7 @@ public final class EntityCache { 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 T updateColumnAnd(final Serializable id, Attribute attr, final long andvalue) { @@ -384,7 +433,7 @@ public final class EntityCache { 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 T updateColumnIncrement(final Serializable id, Attribute attr, final long incvalue) { @@ -392,10 +441,10 @@ public final class EntityCache { 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 T updateColumnIncrAndOr(final Serializable id, Attribute attr, final T rs, Number numb) { + private T updateColumnIncrAndOr(Attribute attr, final T rs, Number numb) { final Class ft = attr.type(); if (ft == int.class || ft == Integer.class) { numb = numb.intValue();