This commit is contained in:
Redkale
2016-08-24 16:08:12 +08:00
parent d2a3291f13
commit bb77681445
4 changed files with 47 additions and 22 deletions

View File

@@ -348,7 +348,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
synchronized (info) { synchronized (info) {
if (!info.initedPrimaryValue) { //初始化最大主键值 if (!info.initedPrimaryValue) { //初始化最大主键值
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable()); ResultSet rs = stmt.executeQuery("SELECT MAX(" + info.getPrimarySQLColumn() + ") FROM " + info.getTable(values[0]));
if (rs.next()) { if (rs.next()) {
if (primaryType == int.class) { if (primaryType == int.class) {
int v = rs.getInt(1) / info.allocationSize; int v = rs.getInt(1) / info.allocationSize;
@@ -527,12 +527,18 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
if (keys.length == 0) return; if (keys.length == 0) return;
try { try {
if (!info.isVirtualEntity()) { if (!info.isVirtualEntity()) {
String sql = "DELETE FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn() + " IN " + FilterNode.formatToString(keys);
if (debug.get()) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
stmt.execute(sql); String[] sqls = new String[keys.length];
int index = -1;
for (Serializable key : keys) {
String sql = "DELETE FROM " + info.getTable(keys) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(key);
sqls[++index] = sql;
if (debug.get()) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
stmt.addBatch(sql);
}
stmt.executeBatch();
stmt.close(); stmt.close();
if (writeListener != null) writeListener.delete(sql); if (writeListener != null) writeListener.delete(sqls);
} }
//------------------------------------ //------------------------------------
final EntityCache<T> cache = info.getCache(); final EntityCache<T> cache = info.getCache();
@@ -573,7 +579,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
Map<Class, String> joinTabalis = node.getJoinTabalis(); Map<Class, String> joinTabalis = node.getJoinTabalis();
CharSequence join = node.createSQLJoin(this, joinTabalis, info); CharSequence join = node.createSQLJoin(this, joinTabalis, info);
CharSequence where = node.createSQLExpress(info, joinTabalis); CharSequence where = node.createSQLExpress(info, joinTabalis);
String sql = "DELETE " + (this.readPool.isMysql() ? "a" : "") + " FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); String sql = "DELETE " + (this.readPool.isMysql() ? "a" : "") + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
if (debug.get()) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
stmt.execute(sql); stmt.execute(sql);
@@ -723,7 +729,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
private <T> void updateColumn(Connection conn, final EntityInfo<T> info, Serializable id, String column, Serializable value) { private <T> void updateColumn(Connection conn, final EntityInfo<T> info, Serializable id, String column, Serializable value) {
try { try {
if (!info.isVirtualEntity()) { if (!info.isVirtualEntity()) {
String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(null, column) + " = " String sql = "UPDATE " + info.getTable(id) + " SET " + info.getSQLColumn(null, column) + " = "
+ FilterNode.formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id); + FilterNode.formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id);
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -780,7 +786,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
CharSequence join = node.createSQLJoin(this, joinTabalis, info); CharSequence join = node.createSQLJoin(this, joinTabalis, info);
CharSequence where = node.createSQLExpress(info, joinTabalis); CharSequence where = node.createSQLExpress(info, joinTabalis);
String sql = "UPDATE " + info.getTable() + " a SET " + info.getSQLColumn("a", column) + " = " String sql = "UPDATE " + info.getTable(node) + " a SET " + info.getSQLColumn("a", column) + " = "
+ FilterNode.formatToString(value) + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); + FilterNode.formatToString(value) + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -835,7 +841,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
try { try {
if (!info.isVirtualEntity()) { if (!info.isVirtualEntity()) {
String col = info.getSQLColumn(null, column); String col = info.getSQLColumn(null, column);
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " + (" + incvalue String sql = "UPDATE " + info.getTable(id) + " SET " + col + " = " + col + " + (" + incvalue
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id); + ") WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id);
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -891,7 +897,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
try { try {
if (!info.isVirtualEntity()) { if (!info.isVirtualEntity()) {
String col = info.getSQLColumn(null, column); String col = info.getSQLColumn(null, column);
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " & (" + andvalue String sql = "UPDATE " + info.getTable(id) + " SET " + col + " = " + col + " & (" + andvalue
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id); + ") WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id);
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -947,7 +953,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
try { try {
if (!info.isVirtualEntity()) { if (!info.isVirtualEntity()) {
String col = info.getSQLColumn(null, column); String col = info.getSQLColumn(null, column);
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " | (" + orvalue String sql = "UPDATE " + info.getTable(id) + " SET " + col + " = " + col + " | (" + orvalue
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id); + ") WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id);
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -1014,7 +1020,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
} }
} }
if (!virtual) { if (!virtual) {
String sql = "UPDATE " + info.getTable() + " SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id); String sql = "UPDATE " + info.getTable(id) + " SET " + setsql + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(id);
if (debug.get()) logger.finest(bean.getClass().getSimpleName() + ": " + sql); if (debug.get()) logger.finest(bean.getClass().getSimpleName() + ": " + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
stmt.execute(sql); stmt.execute(sql);
@@ -1082,7 +1088,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
CharSequence join = node.createSQLJoin(this, joinTabalis, info); CharSequence join = node.createSQLJoin(this, joinTabalis, info);
CharSequence where = node.createSQLExpress(info, joinTabalis); CharSequence where = node.createSQLExpress(info, joinTabalis);
String sql = "UPDATE " + info.getTable() + " a SET " + setsql String sql = "UPDATE " + info.getTable(node) + " a SET " + setsql
+ (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
@@ -1158,7 +1164,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis(); final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info); final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis); final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
final String sql = "SELECT " + func.getColumn((column == null || column.isEmpty() ? "*" : ("a." + column))) + " FROM " + info.getTable() + " a" final String sql = "SELECT " + func.getColumn((column == null || column.isEmpty() ? "*" : ("a." + column))) + " FROM " + info.getTable(node) + " a"
+ (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
final PreparedStatement prestmt = conn.prepareStatement(sql); final PreparedStatement prestmt = conn.prepareStatement(sql);
@@ -1221,7 +1227,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info); final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
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." + sqlkey + ", " + func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : ("a." + funcColumn))) final String sql = "SELECT a." + sqlkey + ", " + func.getColumn((funcColumn == null || funcColumn.isEmpty() ? "*" : ("a." + funcColumn)))
+ " FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + " GROUP BY a." + sqlkey; + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + " GROUP BY a." + sqlkey;
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
final PreparedStatement prestmt = conn.prepareStatement(sql); final PreparedStatement prestmt = conn.prepareStatement(sql);
Map<K, N> rs = new LinkedHashMap<>(); Map<K, N> rs = new LinkedHashMap<>();
@@ -1280,7 +1286,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final SelectColumn sels = selects; final SelectColumn sels = selects;
final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk); final String sql = "SELECT * FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk);
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql); 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 PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
@@ -1359,7 +1365,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis(); final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info); final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
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() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); final String sql = "SELECT a.* FROM " + info.getTable(node) + " 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); 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 PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
@@ -1391,7 +1397,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final String sql = "SELECT COUNT(*) FROM " + info.getTable() + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk); final String sql = "SELECT COUNT(*) FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk);
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " exists sql=" + sql); 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 PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
@@ -1435,7 +1441,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis(); final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info); final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis); final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
final String sql = "SELECT COUNT(" + info.getPrimarySQLColumn("a") + ") FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)); final String sql = "SELECT COUNT(" + info.getPrimarySQLColumn("a") + ") FROM " + info.getTable(node) + " 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); 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 PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
@@ -1808,7 +1814,7 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis(); final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info); final CharSequence join = node == null ? null : node.createSQLJoin(this, joinTabalis, info);
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() + " 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 ? "" : (" LIMIT " + flipper.getOffset() + "," + flipper.getLimit())));

View File

@@ -295,7 +295,15 @@ public final class EntityInfo<T> {
return table == null; return table == null;
} }
public String getTable() { public String getTable(Serializable primary) {
return table;
}
public String getTable(FilterNode node) {
return table;
}
public String getTable(T bean) {
return table; return table;
} }

View File

@@ -277,7 +277,7 @@ public class FilterJoinNode extends FilterNode {
if (node.joinClass == null) return null; if (node.joinClass == null) return null;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String[] joinColumns = node.joinColumns; String[] joinColumns = node.joinColumns;
sb.append(" INNER JOIN ").append(node.joinEntity.getTable()).append(" ").append(joinTabalis.get(node.joinClass)) sb.append(" INNER JOIN ").append(node.joinEntity.getTable(node)).append(" ").append(joinTabalis.get(node.joinClass))
.append(" ON ").append(info.getSQLColumn("a", joinColumns[0])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), joinColumns[0])); .append(" ON ").append(info.getSQLColumn("a", joinColumns[0])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), joinColumns[0]));
for (int i = 1; i < joinColumns.length; i++) { for (int i = 1; i < joinColumns.length; i++) {
sb.append(" AND ").append(info.getSQLColumn("a", joinColumns[i])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), joinColumns[i])); sb.append(" AND ").append(info.getSQLColumn("a", joinColumns[i])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), joinColumns[i]));

View File

@@ -77,6 +77,17 @@ public class FilterNode {
this.value = val; this.value = val;
} }
public Serializable findValue(final String col) {
if (this.column.equals(col)) return this.value;
if (this.nodes == null) return null;
for (FilterNode n : this.nodes) {
if (n == null) continue;
Serializable val = n.findValue(col);
if (val != null) return val;
}
return null;
}
public final FilterNode and(FilterNode node) { public final FilterNode and(FilterNode node) {
return any(node, false); return any(node, false);
} }