改版EntityXInfo

This commit is contained in:
地平线
2015-05-30 15:55:53 +08:00
parent d6dfcb7058
commit 5d8b492503
5 changed files with 273 additions and 155 deletions

View File

@@ -65,7 +65,7 @@ public final class DataJDBCSource implements DataSource {
final List<Class> cacheClasses = new ArrayList<>(); final List<Class> cacheClasses = new ArrayList<>();
@Resource(name = "property.datasource.nodeid") @Resource(name = "property.datasource.nodeid")
private int nodeid; int nodeid;
@Resource @Resource
DataSQLListener writeListener; DataSQLListener writeListener;
@@ -339,6 +339,10 @@ public final class DataJDBCSource implements DataSource {
} }
} }
private <T> EntityInfo<T> loadEntityInfo(Class<T> clazz) {
return EntityInfo.load(clazz, this);
}
/** /**
* 将entity的对象全部加载到Cache中去如果clazz没有被@javax.persistence.Cacheable注解则不做任何事 * 将entity的对象全部加载到Cache中去如果clazz没有被@javax.persistence.Cacheable注解则不做任何事
* <p> * <p>
@@ -386,16 +390,16 @@ public final class DataJDBCSource implements DataSource {
if (values.length == 0) return; if (values.length == 0) return;
try { try {
final Class<T> clazz = (Class<T>) values[0].getClass(); final Class<T> clazz = (Class<T>) values[0].getClass();
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
final String sql = info.insert.sql; final String sql = info.insertSQL;
if (debug.get()) logger.finest(clazz.getSimpleName() + " insert sql=" + sql); if (debug.get()) logger.finest(clazz.getSimpleName() + " insert sql=" + sql);
final PreparedStatement prestmt = info.autoGenerated final PreparedStatement prestmt = info.autoGenerated
? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql); ? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);
final Class primaryType = info.getPrimaryType(); final Class primaryType = info.getPrimary().type();
final Attribute primary = info.getPrimary(); final Attribute primary = info.getPrimary();
final boolean distributed = info.distributed; final boolean distributed = info.distributed;
Attribute<T, ?>[] attrs = info.insert.attributes; Attribute<T, ?>[] attrs = info.insertAttributes;
String[] sqls = null; String[] sqls = null;
if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) { if (distributed && !info.initedPrimaryValue && primaryType.isPrimitive()) {
synchronized (info) { synchronized (info) {
@@ -415,7 +419,7 @@ public final class DataJDBCSource implements DataSource {
stmt.close(); stmt.close();
if (info.distributeTables != null) { if (info.distributeTables != null) {
for (final Class t : info.distributeTables) { for (final Class t : info.distributeTables) {
EntityXInfo<T> infox = EntityXInfo.load(this, t); EntityInfo<T> infox = loadEntityInfo(t);
stmt = conn.createStatement(); stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT MAX(" + infox.getPrimarySQLColumn() + ") FROM " + infox.getTable()); rs = stmt.executeQuery("SELECT MAX(" + infox.getPrimarySQLColumn() + ") FROM " + infox.getTable());
if (rs.next()) { if (rs.next()) {
@@ -501,8 +505,8 @@ public final class DataJDBCSource implements DataSource {
public <T> void insertCache(T... values) { public <T> void insertCache(T... values) {
if (values.length == 0) return; if (values.length == 0) return;
final EntityXInfo<T> info = EntityXInfo.load(this, (Class<T>) values[0].getClass()); final EntityInfo<T> info = EntityInfo.load((Class<T>) values[0].getClass(), this);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
for (T value : values) { for (T value : values) {
cache.insert(value); cache.insert(value);
@@ -541,8 +545,8 @@ public final class DataJDBCSource implements DataSource {
private <T> void delete(final Connection conn, T... values) { private <T> void delete(final Connection conn, T... values) {
if (values.length == 0) return; if (values.length == 0) return;
final Class clazz = values[0].getClass(); final Class clazz = values[0].getClass();
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final Attribute primary = info.inner.getPrimary(); final Attribute primary = info.getPrimary();
Serializable[] ids = new Serializable[values.length]; Serializable[] ids = new Serializable[values.length];
int i = 0; int i = 0;
for (final T value : values) { for (final T value : values) {
@@ -582,7 +586,7 @@ public final class DataJDBCSource implements DataSource {
} }
private <T> void delete(final Connection conn, Class<T> clazz, Serializable... ids) { private <T> void delete(final Connection conn, Class<T> clazz, Serializable... ids) {
deleteByColumn(conn, clazz, EntityXInfo.load(this, clazz).getPrimaryField(), ids); deleteByColumn(conn, clazz, loadEntityInfo(clazz).getPrimary().field(), ids);
} }
/** /**
@@ -620,7 +624,7 @@ public final class DataJDBCSource implements DataSource {
private <T> void deleteByColumn(final Connection conn, Class<T> clazz, String column, Serializable... keys) { private <T> void deleteByColumn(final Connection conn, Class<T> clazz, String column, Serializable... keys) {
if (keys.length == 0) return; if (keys.length == 0) return;
try { try {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column); String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column);
if (keys.length == 1 && !keys[0].getClass().isArray()) { if (keys.length == 1 && !keys[0].getClass().isArray()) {
sql += " = " + formatToString(keys[0]); sql += " = " + formatToString(keys[0]);
@@ -653,7 +657,7 @@ public final class DataJDBCSource implements DataSource {
stmt.close(); stmt.close();
if (writeListener != null) writeListener.delete(name, sql); if (writeListener != null) writeListener.delete(name, sql);
//------------------------------------ //------------------------------------
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
final Attribute<T, ?> attr = info.getAttribute(column); final Attribute<T, ?> attr = info.getAttribute(column);
final Serializable[] keys2 = keys; final Serializable[] keys2 = keys;
@@ -702,7 +706,7 @@ public final class DataJDBCSource implements DataSource {
private <T> void deleteByTwoColumn(final Connection conn, Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) { private <T> void deleteByTwoColumn(final Connection conn, Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) {
try { try {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column1) + " = " + formatToString(key1) String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column1) + " = " + formatToString(key1)
+ " AND " + info.getSQLColumn(column2) + " = " + formatToString(key2); + " AND " + info.getSQLColumn(column2) + " = " + formatToString(key2);
if (debug.get()) logger.finest(clazz.getSimpleName() + " delete sql=" + sql); if (debug.get()) logger.finest(clazz.getSimpleName() + " delete sql=" + sql);
@@ -711,7 +715,7 @@ public final class DataJDBCSource implements DataSource {
stmt.close(); stmt.close();
if (writeListener != null) writeListener.delete(name, sql); if (writeListener != null) writeListener.delete(name, sql);
//------------------------------------ //------------------------------------
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
final Attribute<T, ?> attr1 = info.getAttribute(column1); final Attribute<T, ?> attr1 = info.getAttribute(column1);
final Attribute<T, ?> attr2 = info.getAttribute(column2); final Attribute<T, ?> attr2 = info.getAttribute(column2);
@@ -724,8 +728,8 @@ public final class DataJDBCSource implements DataSource {
public <T> void deleteCache(Class<T> clazz, Serializable... ids) { public <T> void deleteCache(Class<T> clazz, Serializable... ids) {
if (ids.length == 0) return; if (ids.length == 0) return;
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
for (Serializable id : ids) { for (Serializable id : ids) {
cache.delete(id); cache.delete(id);
@@ -764,10 +768,10 @@ public final class DataJDBCSource implements DataSource {
private <T> void update(final Connection conn, T... values) { private <T> void update(final Connection conn, T... values) {
try { try {
Class clazz = values[0].getClass(); Class clazz = values[0].getClass();
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
if (debug.get()) logger.finest(clazz.getSimpleName() + " update sql=" + info.update.sql); if (debug.get()) logger.finest(clazz.getSimpleName() + " update sql=" + info.updateSQL);
final PreparedStatement prestmt = conn.prepareStatement(info.update.sql); final PreparedStatement prestmt = conn.prepareStatement(info.updateSQL);
Attribute<T, ?>[] attrs = info.update.attributes; Attribute<T, ?>[] attrs = info.updateAttributes;
String[] sqls = null; String[] sqls = null;
if (writeListener == null) { if (writeListener == null) {
for (final T value : values) { for (final T value : values) {
@@ -778,7 +782,7 @@ public final class DataJDBCSource implements DataSource {
prestmt.addBatch(); prestmt.addBatch();
} }
} else { } else {
char[] sqlchars = info.update.sql.toCharArray(); char[] sqlchars = info.updateSQL.toCharArray();
sqls = new String[values.length]; sqls = new String[values.length];
String[] ps = new String[attrs.length]; String[] ps = new String[attrs.length];
int index = 0; int index = 0;
@@ -807,7 +811,7 @@ public final class DataJDBCSource implements DataSource {
prestmt.close(); prestmt.close();
if (writeListener != null) writeListener.update(name, sqls); if (writeListener != null) writeListener.update(name, sqls);
//--------------------------------------------------- //---------------------------------------------------
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
for (final T value : values) { for (final T value : values) {
cache.update(value); cache.update(value);
@@ -854,7 +858,7 @@ public final class DataJDBCSource implements DataSource {
private <T> void updateColumn(Connection conn, Class<T> clazz, Serializable id, String column, Serializable value) { private <T> void updateColumn(Connection conn, Class<T> clazz, Serializable id, String column, Serializable value) {
try { try {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(column) + " = " + formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id); String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(column) + " = " + formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
if (debug.get()) logger.finest(clazz.getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(clazz.getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -862,7 +866,7 @@ public final class DataJDBCSource implements DataSource {
stmt.close(); stmt.close();
if (writeListener != null) writeListener.update(name, sql); if (writeListener != null) writeListener.update(name, sql);
//--------------------------------------------------- //---------------------------------------------------
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
T rs = cache.update(id, (Attribute<T, Serializable>) info.getAttribute(column), value); T rs = cache.update(id, (Attribute<T, Serializable>) info.getAttribute(column), value);
if (cacheListener != null) cacheListener.update(name, clazz, rs); if (cacheListener != null) cacheListener.update(name, clazz, rs);
@@ -909,7 +913,7 @@ public final class DataJDBCSource implements DataSource {
private <T> void updateColumnIncrement(Connection conn, Class<T> clazz, Serializable id, String column, long incvalue) { private <T> void updateColumnIncrement(Connection conn, Class<T> clazz, Serializable id, String column, long incvalue) {
try { try {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
String col = info.getSQLColumn(column); String col = info.getSQLColumn(column);
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " + (" + incvalue + ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id); String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " + (" + incvalue + ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
if (debug.get()) logger.finest(clazz.getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(clazz.getSimpleName() + " update sql=" + sql);
@@ -918,7 +922,7 @@ public final class DataJDBCSource implements DataSource {
stmt.close(); stmt.close();
if (writeListener != null) writeListener.update(name, sql); if (writeListener != null) writeListener.update(name, sql);
//--------------------------------------------------- //---------------------------------------------------
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
Attribute<T, Object> attr = (Attribute<T, Object>) info.getAttribute(column); Attribute<T, Object> attr = (Attribute<T, Object>) info.getAttribute(column);
T value = cache.updateColumnIncrement(id, attr, incvalue); T value = cache.updateColumnIncrement(id, attr, incvalue);
@@ -964,7 +968,7 @@ public final class DataJDBCSource implements DataSource {
if (columns.length < 1) return; if (columns.length < 1) return;
try { try {
final Class<T> clazz = (Class<T>) value.getClass(); final Class<T> clazz = (Class<T>) value.getClass();
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
StringBuilder setsql = new StringBuilder(); StringBuilder setsql = new StringBuilder();
Attribute<T, ?>[] attrs = new Attribute[columns.length]; Attribute<T, ?>[] attrs = new Attribute[columns.length];
int i = - 1; int i = - 1;
@@ -983,7 +987,7 @@ public final class DataJDBCSource implements DataSource {
stmt.close(); stmt.close();
if (writeListener != null) writeListener.update(name, sql); if (writeListener != null) writeListener.update(name, sql);
//--------------------------------------------------- //---------------------------------------------------
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
cache.update(value, attrs); cache.update(value, attrs);
if (cacheListener != null) cacheListener.update(name, clazz, value); if (cacheListener != null) cacheListener.update(name, clazz, value);
@@ -994,8 +998,8 @@ public final class DataJDBCSource implements DataSource {
public <T> void updateCache(Class<T> clazz, T... values) { public <T> void updateCache(Class<T> clazz, T... values) {
if (values.length == 0) return; if (values.length == 0) return;
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
for (T value : values) { for (T value : values) {
cache.update(value); cache.update(value);
@@ -1003,8 +1007,8 @@ public final class DataJDBCSource implements DataSource {
} }
public <T> void reloadCache(Class<T> clazz, Serializable... ids) { public <T> void reloadCache(Class<T> clazz, Serializable... ids) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache == null) return; if (cache == null) return;
for (Serializable id : ids) { for (Serializable id : ids) {
T value = find(clazz, null, false, id); T value = find(clazz, null, false, id);
@@ -1081,15 +1085,15 @@ public final class DataJDBCSource implements DataSource {
private <T> Number getSingleResult(final ReckonType type, final Class<T> entityClass, final String column, FilterBean bean) { private <T> Number getSingleResult(final ReckonType type, final Class<T> entityClass, final String column, FilterBean bean) {
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final EntityXInfo<T> info = EntityXInfo.load(this, entityClass); final EntityInfo<T> info = loadEntityInfo(entityClass);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache != null && cache.isFullLoaded()) { if (cache != null && cache.isFullLoaded()) {
Predicate<T> filter = null; Predicate<T> filter = null;
boolean valid = true; boolean valid = true;
if (bean != null) { if (bean != null) {
FilterInfo finfo = FilterInfo.load(bean.getClass(), this); FilterInfo finfo = FilterInfo.load(bean.getClass(), this);
valid = finfo.isValidCacheJoin(); valid = finfo.isValidCacheJoin();
if (valid) filter = finfo.getFilterPredicate(info.inner, bean); if (valid) filter = finfo.getFilterPredicate(info, bean);
} }
if (valid) return cache.getSingleResult(type, column == null ? null : info.getAttribute(column), filter); if (valid) return cache.getSingleResult(type, column == null ? null : info.getAttribute(column), filter);
} }
@@ -1131,24 +1135,21 @@ public final class DataJDBCSource implements DataSource {
} }
private <T> T find(Class<T> clazz, final SelectColumn selects, boolean readcache, Serializable pk) { private <T> T find(Class<T> clazz, final SelectColumn selects, boolean readcache, Serializable pk) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (readcache && cache != null) { if (readcache && cache != null) {
T r = cache.find(pk); T r = cache.find(pk);
if (r != null || cache.isFullLoaded()) return r; if (r != null || cache.isFullLoaded()) return r;
} }
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + info.query.sql.replace("?", String.valueOf(pk))); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + info.querySQL.replace("?", String.valueOf(pk)));
final PreparedStatement prestmt = conn.prepareStatement(info.query.sql); final PreparedStatement prestmt = conn.prepareStatement(info.querySQL);
prestmt.setObject(1, pk); prestmt.setObject(1, pk);
T rs = null; T rs = null;
ResultSet set = prestmt.executeQuery(); ResultSet set = prestmt.executeQuery();
if (set.next()) { if (set.next()) {
rs = info.createInstance(); rs = info.getValue(selects, set);
for (AttributeX attr : info.query.attributes) {
attr.setValue(selects, rs, set);
}
} }
set.close(); set.close();
prestmt.close(); prestmt.close();
@@ -1170,7 +1171,7 @@ public final class DataJDBCSource implements DataSource {
*/ */
@Override @Override
public <T> T[] find(Class<T> clazz, Serializable... ids) { public <T> T[] find(Class<T> clazz, Serializable... ids) {
EntityXInfo<T> info = EntityXInfo.load(this, clazz); EntityInfo<T> info = loadEntityInfo(clazz);
return findByColumn(clazz, info, null, info.getPrimarySQLColumn(), ids); return findByColumn(clazz, info, null, info.getPrimarySQLColumn(), ids);
} }
@@ -1185,7 +1186,7 @@ public final class DataJDBCSource implements DataSource {
*/ */
@Override @Override
public <T> T findByColumn(Class<T> clazz, String column, Serializable key) { public <T> T findByColumn(Class<T> clazz, String column, Serializable key) {
EntityXInfo<T> info = EntityXInfo.load(this, clazz); EntityInfo<T> info = loadEntityInfo(clazz);
return findByColumn(clazz, info, null, info.getSQLColumn(column), key)[0]; return findByColumn(clazz, info, null, info.getSQLColumn(column), key)[0];
} }
@@ -1202,8 +1203,8 @@ public final class DataJDBCSource implements DataSource {
*/ */
@Override @Override
public <T> T findByTwoColumn(Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) { public <T> T findByTwoColumn(Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache != null) { if (cache != null) {
final Attribute<T, ?> attr1 = info.getAttribute(column1); final Attribute<T, ?> attr1 = info.getAttribute(column1);
final Attribute<T, ?> attr2 = info.getAttribute(column2); final Attribute<T, ?> attr2 = info.getAttribute(column2);
@@ -1213,17 +1214,15 @@ public final class DataJDBCSource implements DataSource {
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column1) + " = ? AND " + info.getSQLColumn(column2) + " = ?"; final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column1) + " = ? AND " + info.getSQLColumn(column2) + " = ?";
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql.replaceFirst("\\?", String.valueOf(key1)).replaceFirst("\\?", String.valueOf(key2))); if (debug.get() && info.isLoggable(Level.FINEST))
logger.finest(clazz.getSimpleName() + " find sql=" + sql.replaceFirst("\\?", String.valueOf(key1)).replaceFirst("\\?", String.valueOf(key2)));
final PreparedStatement prestmt = conn.prepareStatement(sql); final PreparedStatement prestmt = conn.prepareStatement(sql);
prestmt.setObject(1, key1); prestmt.setObject(1, key1);
prestmt.setObject(2, key2); prestmt.setObject(2, key2);
T rs = null; T rs = null;
ResultSet set = prestmt.executeQuery(); ResultSet set = prestmt.executeQuery();
if (set.next()) { if (set.next()) {
rs = info.createInstance(); rs = info.getValue(null, set);
for (AttributeX attr : info.query.attributes) {
attr.setValue(null, rs, set);
}
} }
set.close(); set.close();
prestmt.close(); prestmt.close();
@@ -1276,8 +1275,8 @@ public final class DataJDBCSource implements DataSource {
@Override @Override
public <T> T[] findByColumn(Class<T> clazz, final SelectColumn selects, String column, Serializable... keys) { public <T> T[] findByColumn(Class<T> clazz, final SelectColumn selects, String column, Serializable... keys) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache != null) { if (cache != null) {
Attribute<T, ?> idattr = info.getAttribute(column); Attribute<T, ?> idattr = info.getAttribute(column);
List<T> list = cache.queryList(selects, (x) -> Arrays.binarySearch(keys, idattr.get(x)) >= 0, null); List<T> list = cache.queryList(selects, (x) -> Arrays.binarySearch(keys, idattr.get(x)) >= 0, null);
@@ -1299,7 +1298,7 @@ public final class DataJDBCSource implements DataSource {
return findByColumn(clazz, info, selects, info.getSQLColumn(column), keys); return findByColumn(clazz, info, selects, info.getSQLColumn(column), keys);
} }
private <T> T[] findByColumn(Class<T> clazz, final EntityXInfo<T> info, final SelectColumn selects, String sqlcolumn, Serializable... keys) { private <T> T[] findByColumn(Class<T> clazz, final EntityInfo<T> info, final SelectColumn selects, String sqlcolumn, Serializable... keys) {
if (keys.length < 1) return (T[]) Array.newInstance(clazz, 0); if (keys.length < 1) return (T[]) Array.newInstance(clazz, 0);
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
@@ -1313,10 +1312,7 @@ public final class DataJDBCSource implements DataSource {
T one = null; T one = null;
ResultSet set = prestmt.executeQuery(); ResultSet set = prestmt.executeQuery();
if (set.next()) { if (set.next()) {
one = info.createInstance(); one = info.getValue(selects, set);
for (AttributeX attr : info.query.attributes) {
attr.setValue(selects, one, set);
}
} }
rs[i] = one; rs[i] = one;
set.close(); set.close();
@@ -1425,8 +1421,8 @@ public final class DataJDBCSource implements DataSource {
* @return * @return
*/ */
protected final <T, V> Collection<V> queryColumnCollection(final boolean set, String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) { protected final <T, V> Collection<V> queryColumnCollection(final boolean set, String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache != null) { if (cache != null) {
Predicate<T> filter = genFilter(info.getAttribute(column), express, key); Predicate<T> filter = genFilter(info.getAttribute(column), express, key);
List<T> list = cache.queryList(SelectColumn.createIncludes(selectedColumn), filter, null); List<T> list = cache.queryList(SelectColumn.createIncludes(selectedColumn), filter, null);
@@ -1506,8 +1502,8 @@ public final class DataJDBCSource implements DataSource {
*/ */
@Override @Override
public <T> List<T> queryList(Class<T> clazz, final SelectColumn selects, String column, FilterExpress express, Serializable key) { public <T> List<T> queryList(Class<T> clazz, final SelectColumn selects, String column, FilterExpress express, Serializable key) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache != null) { if (cache != null) {
Predicate<T> filter = genFilter(info.getAttribute(column), express, key); Predicate<T> filter = genFilter(info.getAttribute(column), express, key);
List<T> rs = cache.queryList(selects, filter, null); List<T> rs = cache.queryList(selects, filter, null);
@@ -1522,11 +1518,7 @@ public final class DataJDBCSource implements DataSource {
final Statement ps = conn.createStatement(); final Statement ps = conn.createStatement();
final ResultSet set = ps.executeQuery(sql); final ResultSet set = ps.executeQuery(sql);
while (set.next()) { while (set.next()) {
final T result = info.createInstance(); list.add(info.getValue(sels, set));
for (AttributeX<T, Object> attr : info.query.attributes) {
attr.setValue(sels, result, set);
}
list.add(result);
} }
set.close(); set.close();
ps.close(); ps.close();
@@ -1538,7 +1530,7 @@ public final class DataJDBCSource implements DataSource {
} }
} }
private String genSQL(String queryColumn, EntityXInfo info, String column, FilterExpress express, Serializable key) { private String genSQL(String queryColumn, EntityInfo info, String column, FilterExpress express, Serializable key) {
String sql = "SELECT " + queryColumn + " FROM " + info.getTable(); String sql = "SELECT " + queryColumn + " FROM " + info.getTable();
if (key instanceof Number) { if (key instanceof Number) {
sql += " WHERE " + info.getSQLColumn(column) + " " + express.value() + " " + key; sql += " WHERE " + info.getSQLColumn(column) + " " + express.value() + " " + key;
@@ -1710,7 +1702,7 @@ public final class DataJDBCSource implements DataSource {
final Sheet<V> rs = new Sheet<>(); final Sheet<V> rs = new Sheet<>();
if (sheet.isEmpty()) return rs; if (sheet.isEmpty()) return rs;
rs.setTotal(sheet.getTotal()); rs.setTotal(sheet.getTotal());
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final Attribute<T, V> selected = (Attribute<T, V>) info.getAttribute(selectedColumn); final Attribute<T, V> selected = (Attribute<T, V>) info.getAttribute(selectedColumn);
final List<V> list = new ArrayList<>(); final List<V> list = new ArrayList<>();
for (T t : sheet.getRows()) { for (T t : sheet.getRows()) {
@@ -1746,18 +1738,18 @@ public final class DataJDBCSource implements DataSource {
*/ */
@Override @Override
public <T> Sheet<T> querySheet(Class<T> clazz, final SelectColumn selects, final Flipper flipper, final FilterBean bean) { public <T> Sheet<T> querySheet(Class<T> clazz, final SelectColumn selects, final Flipper flipper, final FilterBean bean) {
final EntityXInfo<T> info = EntityXInfo.load(this, clazz); final EntityInfo<T> info = loadEntityInfo(clazz);
final EntityCache<T> cache = info.inner.getCache(); final EntityCache<T> cache = info.getCache();
if (cache != null) { if (cache != null) {
Predicate<T> filter = null; Predicate<T> filter = null;
boolean valid = true; boolean valid = true;
if (bean != null) { if (bean != null) {
FilterInfo finfo = FilterInfo.load(bean.getClass(), this); FilterInfo finfo = FilterInfo.load(bean.getClass(), this);
valid = finfo.isValidCacheJoin(); valid = finfo.isValidCacheJoin();
if (valid) filter = finfo.getFilterPredicate(info.inner, bean); if (valid) filter = finfo.getFilterPredicate(info, bean);
} }
if (valid) { if (valid) {
Sheet<T> sheet = cache.querySheet(selects, filter, flipper, FilterInfo.getSortComparator(info.inner, flipper)); Sheet<T> sheet = cache.querySheet(selects, filter, flipper, FilterInfo.getSortComparator(info, flipper));
if (!sheet.isEmpty() || cache.isFullLoaded()) return sheet; if (!sheet.isEmpty() || cache.isFullLoaded()) return sheet;
} }
} }
@@ -1766,7 +1758,8 @@ public final class DataJDBCSource implements DataSource {
final SelectColumn sels = selects; final SelectColumn sels = selects;
final List<T> list = new ArrayList(); final List<T> list = new ArrayList();
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + createWhereExpression(info, flipper, bean); final String sql = "SELECT a.* FROM " + info.getTable() + " a" + createWhereExpression(info, flipper, bean);
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.index() + "," + flipper.getSize()))); if (debug.get() && info.isLoggable(Level.FINEST))
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.index() + "," + flipper.getSize())));
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.index() > 0) set.absolute(flipper.index()); if (flipper != null && flipper.index() > 0) set.absolute(flipper.index());
@@ -1775,11 +1768,7 @@ public final class DataJDBCSource implements DataSource {
long total; long total;
while (set.next()) { while (set.next()) {
i++; i++;
final T result = info.createInstance(); list.add(info.getValue(sels, set));
for (AttributeX<T, Object> attr : info.query.attributes) {
attr.setValue(sels, result, set);
}
list.add(result);
if (limit <= i) break; if (limit <= i) break;
} }
if (flipper != null) { if (flipper != null) {
@@ -1798,7 +1787,7 @@ public final class DataJDBCSource implements DataSource {
} }
} }
private <T> String createWhereExpression(final EntityXInfo<T> info, final Flipper flipper, final FilterBean bean) { private <T> String createWhereExpression(final EntityInfo<T> info, final Flipper flipper, final FilterBean bean) {
if (bean == null && flipper == null) return ""; if (bean == null && flipper == null) return "";
boolean emptySort = flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty(); boolean emptySort = flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty();
StringBuilder where = null; StringBuilder where = null;
@@ -1811,7 +1800,7 @@ public final class DataJDBCSource implements DataSource {
if (emptySort) return where == null ? "" : where.toString(); if (emptySort) return where == null ? "" : where.toString();
if (where == null) where = new StringBuilder(); if (where == null) where = new StringBuilder();
where.append(" ORDER BY "); where.append(" ORDER BY ");
if (info.same && !join) { if (info.isNoAlias() && !join) {
where.append(flipper.getSort()); where.append(flipper.getSort());
} else { } else {
boolean flag = false; boolean flag = false;
@@ -2113,9 +2102,9 @@ public final class DataJDBCSource implements DataSource {
} }
private static class EntityXInfo<T> { private static class EntityXXInfo<T> {
private static final ConcurrentHashMap<Class, EntityXInfo> entityxInfos = new ConcurrentHashMap<>(); private static final ConcurrentHashMap<Class, EntityXXInfo> entityxInfos = new ConcurrentHashMap<>();
private final int nodeid; private final int nodeid;
@@ -2163,7 +2152,7 @@ public final class DataJDBCSource implements DataSource {
} }
} }
public EntityXInfo(DataJDBCSource source, Class<T> type) { public EntityXXInfo(DataJDBCSource source, Class<T> type) {
this.inner = EntityInfo.load(type, source); this.inner = EntityInfo.load(type, source);
this.nodeid = source.nodeid; this.nodeid = source.nodeid;
DistributeTables dt = type.getAnnotation(DistributeTables.class); DistributeTables dt = type.getAnnotation(DistributeTables.class);
@@ -2233,7 +2222,7 @@ public final class DataJDBCSource implements DataSource {
queryattrs.add(attr); queryattrs.add(attr);
} }
} while ((cltmp = cltmp.getSuperclass()) != Object.class); } while ((cltmp = cltmp.getSuperclass()) != Object.class);
AttributeX idxattr = new AttributeX(type, idfieldtype, inner.getPrimary(), inner.getPrimaryField()); AttributeX idxattr = new AttributeX(type, idfieldtype, inner.getPrimary(), inner.getPrimary().field());
updateattrs.add(idxattr); updateattrs.add(idxattr);
this.autoGenerated = auto; this.autoGenerated = auto;
this.delete = new ActionInfo("DELETE FROM " + inner.getTable() + wheresql, idxattr); this.delete = new ActionInfo("DELETE FROM " + inner.getTable() + wheresql, idxattr);
@@ -2259,13 +2248,13 @@ public final class DataJDBCSource implements DataSource {
this.query = new ActionInfo("SELECT * FROM " + inner.getTable() + wheresql, queryattrs); this.query = new ActionInfo("SELECT * FROM " + inner.getTable() + wheresql, queryattrs);
} }
public static <T> EntityXInfo<T> load(DataJDBCSource source, Class<T> clazz) { public static <T> EntityXXInfo<T> load(DataJDBCSource source, Class<T> clazz) {
EntityXInfo rs = entityxInfos.get(clazz); EntityXXInfo rs = entityxInfos.get(clazz);
if (rs != null) return rs; if (rs != null) return rs;
synchronized (entityxInfos) { synchronized (entityxInfos) {
rs = entityxInfos.get(clazz); rs = entityxInfos.get(clazz);
if (rs == null) { if (rs == null) {
rs = new EntityXInfo(source, clazz); rs = new EntityXXInfo(source, clazz);
entityxInfos.put(clazz, rs); entityxInfos.put(clazz, rs);
} }
return rs; return rs;
@@ -2282,7 +2271,7 @@ public final class DataJDBCSource implements DataSource {
public void createPrimaryValue(T src) { public void createPrimaryValue(T src) {
long v = allocationSize > 1 ? (primaryValue.incrementAndGet() * allocationSize + nodeid) : primaryValue.incrementAndGet(); long v = allocationSize > 1 ? (primaryValue.incrementAndGet() * allocationSize + nodeid) : primaryValue.incrementAndGet();
Class p = inner.getPrimaryType(); Class p = inner.getPrimary().type();
if (p == int.class || p == Integer.class) { if (p == int.class || p == Integer.class) {
getPrimary().set(src, (Integer) ((Long) v).intValue()); getPrimary().set(src, (Integer) ((Long) v).intValue());
} else { } else {
@@ -2290,18 +2279,10 @@ public final class DataJDBCSource implements DataSource {
} }
} }
public Class getPrimaryType() { public Attribute<T, Serializable> getPrimary() {
return inner.getPrimaryType();
}
public Attribute<T, Object> getPrimary() {
return inner.getPrimary(); return inner.getPrimary();
} }
public String getPrimaryField() {
return inner.getPrimaryField();
}
public String getTable() { public String getTable() {
return inner.getTable(); return inner.getTable();
} }

View File

@@ -519,7 +519,7 @@ final class DataJPASource implements DataSource {
final CriteriaBuilder builder = manager.getCriteriaBuilder(); final CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaUpdate<T> cd = builder.createCriteriaUpdate(clazz); CriteriaUpdate<T> cd = builder.createCriteriaUpdate(clazz);
cd.set(column, value); cd.set(column, value);
cd.where(builder.equal(cd.from(clazz).get(EntityInfo.load(clazz, this).getPrimaryField()), id)); cd.where(builder.equal(cd.from(clazz).get(EntityInfo.load(clazz, this).getPrimary().field()), id));
manager.createQuery(cd).executeUpdate(); manager.createQuery(cd).executeUpdate();
} }
@@ -565,7 +565,7 @@ final class DataJPASource implements DataSource {
for (String column : columns) { for (String column : columns) {
cd.set(column, info.getAttribute(column).get(value)); cd.set(column, info.getAttribute(column).get(value));
} }
cd.where(builder.equal(cd.from(clazz).get(info.getPrimaryField()), idattr.get(value))); cd.where(builder.equal(cd.from(clazz).get(info.getPrimary().field()), idattr.get(value)));
manager.createQuery(cd).executeUpdate(); manager.createQuery(cd).executeUpdate();
} }
@@ -1037,7 +1037,7 @@ final class DataJPASource implements DataSource {
private <T> List<T> selectList(final Class<T> clazz, final SelectColumn selects, final List<T> list) { private <T> List<T> selectList(final Class<T> clazz, final SelectColumn selects, final List<T> list) {
if (selects == null || selects.isEmpty() || list.isEmpty()) return list; if (selects == null || selects.isEmpty() || list.isEmpty()) return list;
final EntityInfo info = EntityInfo.load(clazz, this); final EntityInfo info = EntityInfo.load(clazz, this);
final Object dftValue = info.getDefaultTypeInstance(); final Object dftValue = info.getCreator().create();
final Map<String, Attribute> map = info.getAttributes(); final Map<String, Attribute> map = info.getAttributes();
final List<Attribute> attrs = new ArrayList<>(); final List<Attribute> attrs = new ArrayList<>();
if (selects.isExcludable()) { if (selects.isExcludable()) {

View File

@@ -5,10 +5,7 @@
*/ */
package com.wentch.redkale.source; package com.wentch.redkale.source;
import com.wentch.redkale.util.Sheet; import com.wentch.redkale.util.*;
import com.wentch.redkale.util.Reproduce;
import com.wentch.redkale.util.Creator;
import com.wentch.redkale.util.Attribute;
import java.io.*; import java.io.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
@@ -36,8 +33,9 @@ final class EntityCache<T> {
private final Creator<T> creator; private final Creator<T> creator;
private final Attribute<T, Object> primary; private final Attribute<T, Serializable> primary;
//key是field的name
private final Map<String, Attribute<T, ?>> attributes; private final Map<String, Attribute<T, ?>> attributes;
private final Reproduce<T, T> reproduce; private final Reproduce<T, T> reproduce;
@@ -45,7 +43,7 @@ final class EntityCache<T> {
private boolean fullloaded; private boolean fullloaded;
public EntityCache(final Class<T> type, Creator<T> creator, public EntityCache(final Class<T> type, Creator<T> creator,
Attribute<T, Object> primary, Map<String, Attribute<T, ?>> attributes) { Attribute<T, Serializable> primary, Map<String, Attribute<T, ?>> attributes) {
this.type = type; this.type = type;
this.creator = creator; this.creator = creator;
this.primary = primary; this.primary = primary;

View File

@@ -7,9 +7,12 @@ package com.wentch.redkale.source;
import com.sun.istack.internal.logging.Logger; import com.sun.istack.internal.logging.Logger;
import com.wentch.redkale.util.*; import com.wentch.redkale.util.*;
import java.io.Serializable;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.sql.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.*; import java.util.logging.*;
import javax.persistence.*; import javax.persistence.*;
@@ -25,32 +28,66 @@ public final class EntityInfo<T> {
private static final Logger logger = Logger.getLogger(EntityInfo.class); private static final Logger logger = Logger.getLogger(EntityInfo.class);
//Entity类的类名
private final Class<T> type; private final Class<T> type;
//类对应的数据表名
private final String table; private final String table;
private final Creator<T> creator; private final Creator<T> creator;
private final Class primaryType; //主键
private final Attribute<T, Serializable> primary;
private final Attribute<T, Object> primary;
private final String primaryFieldName;
private final T defaultTypeInstance;
private final EntityCache<T> cache; private final EntityCache<T> cache;
//key是field的name value是Column的别名即数据库表的字段名
//只有field.name 与 Column.name不同才存放在aliasmap里.
private final Map<String, String> aliasmap;
//key是field的name 不是sql字段。
//存放所有与数据库对应的字段, 包括主键
private final Map<String, Attribute<T, ?>> attributes = new HashMap<>(); private final Map<String, Attribute<T, ?>> attributes = new HashMap<>();
final String querySQL;
private final Attribute<T, Object>[] queryAttributes; //数据库中所有字段
final String insertSQL;
final Attribute<T, Object>[] insertAttributes; //数据库中所有可新增字段
final String updateSQL;
final Attribute<T, Object>[] updateAttributes; //数据库中所有可更新字段
final String deleteSQL;
private final int logLevel;
//---------------------计算主键值----------------------------
private final int nodeid;
final Class[] distributeTables;
final boolean autoGenerated;
final boolean distributed;
boolean initedPrimaryValue = false;
final AtomicLong primaryValue = new AtomicLong(0);
final int allocationSize;
//------------------------------------------------------------
public static <T> EntityInfo<T> load(Class<T> clazz, final DataSource source) { public static <T> EntityInfo<T> load(Class<T> clazz, final DataSource source) {
EntityInfo rs = entityInfos.get(clazz); EntityInfo rs = entityInfos.get(clazz);
if (rs != null) return rs; if (rs != null) return rs;
synchronized (entityInfos) { synchronized (entityInfos) {
rs = entityInfos.get(clazz); rs = entityInfos.get(clazz);
if (rs == null) { if (rs == null) {
final List<Class> cacheClasses = source instanceof DataJDBCSource ? ((DataJDBCSource) source).cacheClasses : null; rs = new EntityInfo(clazz, ((DataJDBCSource) source).nodeid, ((DataJDBCSource) source).cacheClasses);
rs = new EntityInfo(clazz, cacheClasses);
entityInfos.put(clazz, rs); entityInfos.put(clazz, rs);
if (rs.cache != null && source != null) { if (rs.cache != null && source != null) {
AutoLoad auto = clazz.getAnnotation(AutoLoad.class); AutoLoad auto = clazz.getAnnotation(AutoLoad.class);
@@ -66,45 +103,112 @@ public final class EntityInfo<T> {
} }
} }
private EntityInfo(Class<T> type, final List<Class> cacheClasses) { private EntityInfo(Class<T> type, int nodeid, final List<Class> cacheClasses) {
this.type = type; this.type = type;
//---------------------------------------------
this.nodeid = nodeid;
DistributeGenerator.DistributeTables dt = type.getAnnotation(DistributeGenerator.DistributeTables.class);
this.distributeTables = dt == null ? null : dt.value();
LogLevel ll = type.getAnnotation(LogLevel.class);
this.logLevel = ll == null ? Integer.MIN_VALUE : Level.parse(ll.value()).intValue();
//---------------------------------------------
Table t = type.getAnnotation(Table.class); Table t = type.getAnnotation(Table.class);
this.table = (t == null) ? type.getSimpleName().toLowerCase() : (t.catalog().isEmpty()) ? t.name() : (t.catalog() + '.' + t.name()); this.table = (t == null) ? type.getSimpleName().toLowerCase() : (t.catalog().isEmpty()) ? t.name() : (t.catalog() + '.' + t.name());
this.creator = Creator.create(type); this.creator = Creator.create(type);
this.defaultTypeInstance = this.creator.create();
Attribute idAttr0 = null; Attribute idAttr0 = null;
Class primaryType0 = null; Map<String, String> aliasmap0 = null;
String primaryName = null;
Class cltmp = type; Class cltmp = type;
Set<String> fields = new HashSet<>(); Set<String> fields = new HashSet<>();
List<Attribute<T, ?>> queryattrs = new ArrayList<>();
List<String> insertcols = new ArrayList<>();
List<Attribute<T, ?>> insertattrs = new ArrayList<>();
List<String> updatecols = new ArrayList<>();
List<Attribute<T, ?>> updateattrs = new ArrayList<>();
boolean auto = false;
boolean sqldistribute = false;
int allocationSize0 = 0;
do { do {
for (Field field : cltmp.getDeclaredFields()) { for (Field field : cltmp.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) continue; if (Modifier.isStatic(field.getModifiers())) continue;
if (Modifier.isFinal(field.getModifiers())) continue; if (Modifier.isFinal(field.getModifiers())) continue;
if (field.getAnnotation(Transient.class) != null) continue; if (field.getAnnotation(Transient.class) != null) continue;
if (fields.contains(field.getName())) continue; if (fields.contains(field.getName())) continue;
final String fieldname = field.getName();
final Column col = field.getAnnotation(Column.class); final Column col = field.getAnnotation(Column.class);
final String sqlfield = col == null || col.name().isEmpty() ? field.getName() : col.name(); final String sqlfield = col == null || col.name().isEmpty() ? fieldname : col.name();
if (!fieldname.equals(sqlfield)) {
if (aliasmap0 == null) aliasmap0 = new HashMap<>();
aliasmap0.put(fieldname, sqlfield);
}
Attribute attr; Attribute attr;
try { try {
attr = Attribute.create(cltmp, sqlfield, field); attr = Attribute.create(cltmp, field);
} catch (RuntimeException e) { } catch (RuntimeException e) {
continue; continue;
} }
if (field.getAnnotation(javax.persistence.Id.class) != null) { if (field.getAnnotation(javax.persistence.Id.class) != null && idAttr0 == null) {
if (idAttr0 == null) {
idAttr0 = attr; idAttr0 = attr;
primaryType0 = field.getType(); GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
primaryName = field.getName(); auto = gv != null;
if (gv != null && gv.strategy() != GenerationType.IDENTITY) {
throw new RuntimeException(cltmp.getName() + "'s @ID primary not a GenerationType.IDENTITY");
}
DistributeGenerator dg = field.getAnnotation(DistributeGenerator.class);
if (dg != null) {
if (!field.getType().isPrimitive()) throw new RuntimeException(cltmp.getName() + "'s @DistributeGenerator primary must be primitive class type field");
sqldistribute = true;
auto = false;
allocationSize0 = dg.allocationSize();
primaryValue.set(dg.initialValue());
}
if (!auto) {
insertcols.add(sqlfield);
insertattrs.add(attr);
}
} else {
if (col == null || col.insertable()) {
insertcols.add(sqlfield);
insertattrs.add(attr);
}
if (col == null || col.updatable()) {
updatecols.add(sqlfield);
updateattrs.add(attr);
} }
} }
fields.add(field.getName()); queryattrs.add(attr);
attributes.put(field.getName(), attr); fields.add(fieldname);
attributes.put(fieldname, attr);
} }
} while ((cltmp = cltmp.getSuperclass()) != Object.class); } while ((cltmp = cltmp.getSuperclass()) != Object.class);
this.primary = idAttr0; this.primary = idAttr0;
this.primaryType = primaryType0; this.aliasmap = aliasmap0;
this.primaryFieldName = primaryName; {
this.queryAttributes = queryattrs.toArray(new Attribute[queryattrs.size()]);
this.insertAttributes = insertattrs.toArray(new Attribute[insertattrs.size()]);
this.updateAttributes = updateattrs.toArray(new Attribute[updateattrs.size()]);
StringBuilder insertsb = new StringBuilder();
StringBuilder insertsb2 = new StringBuilder();
for (String col : insertcols) {
if (insertsb.length() > 0) insertsb.append(',');
insertsb.append(col);
if (insertsb2.length() > 0) insertsb2.append(',');
insertsb2.append('?');
}
this.insertSQL = "INSERT INTO " + table + "(" + insertsb + ") VALUES(" + insertsb2 + ")";
StringBuilder updatesb = new StringBuilder();
for (String col : updatecols) {
if (updatesb.length() > 0) updatesb.append(',');
updatesb.append(col).append(" = ?");
}
this.updateSQL = "UPDATE " + table + " SET " + updatesb + " WHERE " + getPrimarySQLColumn() + " = ?";
this.deleteSQL = "DELETE FROM " + table + " WHERE " + getPrimarySQLColumn() + " = ?";
this.querySQL = "SELECT * FROM " + table + " WHERE " + getPrimarySQLColumn() + " = ?";
}
this.autoGenerated = auto;
this.distributed = sqldistribute;
this.allocationSize = allocationSize0;
//----------------cache-------------- //----------------cache--------------
Cacheable c = type.getAnnotation(Cacheable.class); Cacheable c = type.getAnnotation(Cacheable.class);
boolean cf = (c == null) ? (cacheClasses != null && cacheClasses.contains(type)) : false; boolean cf = (c == null) ? (cacheClasses != null && cacheClasses.contains(type)) : false;
@@ -115,6 +219,15 @@ public final class EntityInfo<T> {
} }
} }
public void createPrimaryValue(T src) {
long v = allocationSize > 1 ? (primaryValue.incrementAndGet() * allocationSize + nodeid) : primaryValue.incrementAndGet();
if (primary.type() == int.class || primary.type() == Integer.class) {
getPrimary().set(src, (Integer) ((Long) v).intValue());
} else {
getPrimary().set(src, v);
}
}
EntityCache<T> getCache() { EntityCache<T> getCache() {
return cache; return cache;
} }
@@ -131,27 +244,53 @@ public final class EntityInfo<T> {
return table; return table;
} }
public Class getPrimaryType() { public Attribute<T, Serializable> getPrimary() {
return this.primaryType;
}
public Attribute<T, Object> getPrimary() {
return this.primary; return this.primary;
} }
public String getPrimaryField() {
return this.primaryFieldName;
}
public Attribute<T, ?> getAttribute(String fieldname) { public Attribute<T, ?> getAttribute(String fieldname) {
return this.attributes.get(fieldname); return this.attributes.get(fieldname);
} }
public T getDefaultTypeInstance() { public boolean isNoAlias() {
return defaultTypeInstance; return this.aliasmap == null;
}
//根据field字段名获取数据库对应的字段名
public String getSQLColumn(String fieldname) {
return this.aliasmap == null ? fieldname : aliasmap.getOrDefault(fieldname, fieldname);
}
//数据库字段名
public String getPrimarySQLColumn() {
return getSQLColumn(this.primary.field());
} }
public Map<String, Attribute<T, ?>> getAttributes() { public Map<String, Attribute<T, ?>> getAttributes() {
return attributes; return attributes;
} }
public boolean isLoggable(Level l) {
return l.intValue() >= this.logLevel;
}
public T getValue(SelectColumn sels, ResultSet set) throws SQLException {
T obj = creator.create();
for (Attribute<T, Object> attr : queryAttributes) {
if (sels == null || sels.validate(attr.field())) {
Object o = set.getObject(this.getSQLColumn(attr.field()));
if (o != null) {
if (type == long.class) {
o = ((Number) o).longValue();
} else if (type == int.class) {
o = ((Number) o).intValue();
} else if (type == short.class) {
o = ((Number) o).shortValue();
}
}
attr.set(obj, o);
}
}
return obj;
}
} }

View File

@@ -226,7 +226,7 @@ public class FilterNode {
if (o instanceof CharSequence) { if (o instanceof CharSequence) {
sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"'); sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"');
} else { } else {
sb.append('"').append(o).append('"'); sb.append(o);
} }
} }
return sb.append(')').toString(); return sb.append(')').toString();
@@ -241,7 +241,7 @@ public class FilterNode {
if (o instanceof CharSequence) { if (o instanceof CharSequence) {
sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"'); sb.append('"').append(o.toString().replace("\"", "\\\"")).append('"');
} else { } else {
sb.append('"').append(o).append('"'); sb.append(o);
} }
} }
return sb.append(')').toString(); return sb.append(')').toString();