This commit is contained in:
@@ -646,8 +646,8 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
try {
|
||||
if (!info.isVirtualEntity()) {
|
||||
CharSequence join = node.createSQLJoin(this, info);
|
||||
CharSequence where = node.createSQLExpress(info, null);
|
||||
String sql = "DELETE FROM " + info.getTable() + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||
CharSequence where = node.createSQLExpress(this, info, null);
|
||||
String sql = "DELETE FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " delete sql=" + sql);
|
||||
final Statement stmt = conn.createStatement();
|
||||
stmt.execute(sql);
|
||||
@@ -797,7 +797,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
private <T> void updateColumn(Connection conn, final EntityInfo<T> info, Serializable id, String column, Serializable value) {
|
||||
try {
|
||||
if (!info.isVirtualEntity()) {
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(column) + " = "
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + info.getSQLColumn(null, column) + " = "
|
||||
+ formatToString(value) + " WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||
final Statement stmt = conn.createStatement();
|
||||
@@ -849,7 +849,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
private <T> void updateColumnIncrement(Connection conn, final EntityInfo<T> info, Serializable id, String column, long incvalue) {
|
||||
try {
|
||||
if (!info.isVirtualEntity()) {
|
||||
String col = info.getSQLColumn(column);
|
||||
String col = info.getSQLColumn(null, column);
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " + (" + incvalue
|
||||
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||
@@ -903,7 +903,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
private <T> void updateColumnAnd(Connection conn, final EntityInfo<T> info, Serializable id, String column, long andvalue) {
|
||||
try {
|
||||
if (!info.isVirtualEntity()) {
|
||||
String col = info.getSQLColumn(column);
|
||||
String col = info.getSQLColumn(null, column);
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " & (" + andvalue
|
||||
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||
@@ -957,7 +957,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
private <T> void updateColumnOr(Connection conn, final EntityInfo<T> info, Serializable id, String column, long orvalue) {
|
||||
try {
|
||||
if (!info.isVirtualEntity()) {
|
||||
String col = info.getSQLColumn(column);
|
||||
String col = info.getSQLColumn(null, column);
|
||||
String sql = "UPDATE " + info.getTable() + " SET " + col + " = " + col + " | (" + orvalue
|
||||
+ ") WHERE " + info.getPrimarySQLColumn() + " = " + formatToString(id);
|
||||
if (debug.get()) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
|
||||
@@ -1020,7 +1020,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
attrs.add(attr);
|
||||
if (!virtual) {
|
||||
if (setsql.length() > 0) setsql.append(',');
|
||||
setsql.append(info.getSQLColumn(col)).append(" = ").append(formatToString(attr.get(value)));
|
||||
setsql.append(info.getSQLColumn(null, col)).append(" = ").append(formatToString(attr.get(value)));
|
||||
}
|
||||
}
|
||||
if (!virtual) {
|
||||
@@ -1091,7 +1091,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
}
|
||||
}
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(this, info, bean);
|
||||
final String sql = "SELECT " + reckon.getColumn((column == null || column.isEmpty() ? "*" : ("a." + column))) + " FROM " + info.getTable() + " a"
|
||||
+ (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||
if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
|
||||
@@ -1138,9 +1138,9 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
return cache.getMapResult(keyColumn, reckon, reckonColumn, node, bean);
|
||||
}
|
||||
}
|
||||
final String sqlkey = info.getSQLColumn(keyColumn);
|
||||
final String sqlkey = info.getSQLColumn(null, keyColumn);
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(this, info, bean);
|
||||
final String sql = "SELECT a." + sqlkey + ", " + reckon.getColumn((reckonColumn == null || reckonColumn.isEmpty() ? "*" : ("a." + reckonColumn)))
|
||||
+ " FROM " + info.getTable() + " 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);
|
||||
@@ -1225,7 +1225,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
try {
|
||||
final SelectColumn sels = selects;
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(this, 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);
|
||||
@@ -1283,8 +1283,8 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
final Connection conn = createReadSQLConnection();
|
||||
try {
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final String sql = "SELECT COUNT(a." + info.getPrimarySQLColumn() + ") FROM " + info.getTable() + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(this, info, bean);
|
||||
final String sql = "SELECT COUNT(" + info.getPrimarySQLColumn("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();
|
||||
@@ -1537,7 +1537,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
|
||||
final SelectColumn sels = selects;
|
||||
final List<T> list = new ArrayList();
|
||||
final CharSequence join = node == null ? null : node.createSQLJoin(this, info);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(info, bean);
|
||||
final CharSequence where = node == null ? null : node.createSQLExpress(this, info, bean);
|
||||
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + (join == null ? "" : join)
|
||||
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
|
||||
if (debug.get() && info.isLoggable(Level.FINEST))
|
||||
|
||||
@@ -213,9 +213,9 @@ public final class EntityInfo<T> {
|
||||
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.updateSQL = "UPDATE " + table + " SET " + updatesb + " WHERE " + getPrimarySQLColumn(null) + " = ?";
|
||||
this.deleteSQL = "DELETE FROM " + table + " WHERE " + getPrimarySQLColumn(null) + " = ?";
|
||||
this.querySQL = "SELECT * FROM " + table + " WHERE " + getPrimarySQLColumn(null) + " = ?";
|
||||
} else {
|
||||
this.insertSQL = null;
|
||||
this.updateSQL = null;
|
||||
@@ -305,9 +305,9 @@ public final class EntityInfo<T> {
|
||||
String[] sub = item.split("\\s+");
|
||||
if (flag) sb.append(',');
|
||||
if (sub.length < 2 || sub[1].equalsIgnoreCase("ASC")) {
|
||||
sb.append("a.").append(getSQLColumn(sub[0])).append(" ASC");
|
||||
sb.append(getSQLColumn("a", sub[0])).append(" ASC");
|
||||
} else {
|
||||
sb.append("a.").append(getSQLColumn(sub[0])).append(" DESC");
|
||||
sb.append(getSQLColumn("a", sub[0])).append(" DESC");
|
||||
}
|
||||
flag = true;
|
||||
}
|
||||
@@ -318,13 +318,18 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
|
||||
//根据field字段名获取数据库对应的字段名
|
||||
public String getSQLColumn(String fieldname) {
|
||||
return this.aliasmap == null ? fieldname : aliasmap.getOrDefault(fieldname, fieldname);
|
||||
public String getSQLColumn(String tabalis, String fieldname) {
|
||||
return this.aliasmap == null ? (tabalis == null ? fieldname : (tabalis + '.' + fieldname))
|
||||
: (tabalis == null ? aliasmap.getOrDefault(fieldname, fieldname) : (tabalis + '.' + aliasmap.getOrDefault(fieldname, fieldname)));
|
||||
}
|
||||
|
||||
public String getPrimarySQLColumn() {
|
||||
return getSQLColumn(null, this.primary.field());
|
||||
}
|
||||
|
||||
//数据库字段名
|
||||
public String getPrimarySQLColumn() {
|
||||
return getSQLColumn(this.primary.field());
|
||||
public String getPrimarySQLColumn(String tabalis) {
|
||||
return getSQLColumn(tabalis, this.primary.field());
|
||||
}
|
||||
|
||||
public Map<String, Attribute<T, Serializable>> getAttributes() {
|
||||
@@ -339,7 +344,7 @@ public final class EntityInfo<T> {
|
||||
T obj = creator.create();
|
||||
for (Attribute<T, Serializable> attr : queryAttributes) {
|
||||
if (sels == null || sels.validate(attr.field())) {
|
||||
Serializable o = (Serializable) set.getObject(this.getSQLColumn(attr.field()));
|
||||
Serializable o = (Serializable) set.getObject(this.getSQLColumn(null, attr.field()));
|
||||
if (o != null) {
|
||||
Class t = attr.type();
|
||||
if (t == short.class) {
|
||||
|
||||
@@ -91,8 +91,7 @@ final class FilterBeanNode extends FilterNode {
|
||||
final String jc = joinCol.column().isEmpty() ? secinfo.getPrimary().field() : joinCol.column();
|
||||
if (first) {
|
||||
joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable())
|
||||
.append(" ").append(alias).append(" ON a.").append(secinfo.getSQLColumn(jc)).append(" = ")
|
||||
.append(alias).append(".").append(secinfo.getSQLColumn(jc));
|
||||
.append(" ").append(alias).append(" ON ").append(secinfo.getSQLColumn("a", jc)).append(" = ").append(secinfo.getSQLColumn(alias, jc));
|
||||
}
|
||||
newnode.foreignCache = secinfo.getCache();
|
||||
newnode.tabalis = alias;
|
||||
@@ -181,9 +180,7 @@ final class FilterBeanNode extends FilterNode {
|
||||
if (type.isArray() || Collection.class.isAssignableFrom(type)) {
|
||||
if (Range.class.isAssignableFrom(type.getComponentType())) {
|
||||
if (AND != exp) exp = OR;
|
||||
} else {
|
||||
if (NOTIN != exp) exp = IN;
|
||||
}
|
||||
} else if (NOTIN != exp) exp = IN;
|
||||
} else if (Range.class.isAssignableFrom(type)) {
|
||||
if (NOTBETWEEN != exp) exp = BETWEEN;
|
||||
}
|
||||
|
||||
@@ -5,13 +5,73 @@
|
||||
*/
|
||||
package com.wentch.redkale.source;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.function.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class FilterJoinNode extends FilterNode {
|
||||
|
||||
private String tabalis;
|
||||
|
||||
private Class joinClass;
|
||||
|
||||
private String joinColumn;
|
||||
|
||||
public FilterJoinNode() {
|
||||
}
|
||||
|
||||
protected FilterJoinNode(Class joinClass, String joinColumn, String column, Serializable value) {
|
||||
this(joinClass, joinColumn, column, null, value);
|
||||
}
|
||||
|
||||
protected FilterJoinNode(Class joinClass, String joinColumn, String column, FilterExpress express, Serializable value) {
|
||||
this.joinClass = joinClass;
|
||||
this.joinColumn = joinColumn;
|
||||
this.column = column;
|
||||
this.express = express;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static FilterNode create(Class joinClass, String joinColumn, String column, Serializable value) {
|
||||
return new FilterJoinNode(joinClass, joinColumn, column, value);
|
||||
}
|
||||
|
||||
public static FilterNode create(Class joinClass, String joinColumn, String column, FilterExpress express, Serializable value) {
|
||||
return new FilterJoinNode(joinClass, joinColumn, column, express, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T> CharSequence createSQLExpress(final Function<Class, EntityInfo> func, final EntityInfo<T> info, final FilterBean bean) {
|
||||
if (this.joinClass == null) return super.createSQLExpress(func, info, bean);
|
||||
return super.createSQLExpress(func, func.apply(joinClass), bean);
|
||||
}
|
||||
|
||||
public Class getJoinClass() {
|
||||
return joinClass;
|
||||
}
|
||||
|
||||
public void setJoinClass(Class joinClass) {
|
||||
this.joinClass = joinClass;
|
||||
}
|
||||
|
||||
public String getJoinColumn() {
|
||||
return joinColumn;
|
||||
}
|
||||
|
||||
public void setJoinColumn(String joinColumn) {
|
||||
this.joinColumn = joinColumn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTabalis() {
|
||||
return tabalis;
|
||||
}
|
||||
|
||||
public void setTabalis(String tabalis) {
|
||||
this.tabalis = tabalis;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -155,15 +155,16 @@ public class FilterNode {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static FilterNode create(String column, Serializable value) {
|
||||
return create(column, null, value);
|
||||
}
|
||||
|
||||
public static FilterNode create(String column, FilterExpress express, Serializable value) {
|
||||
return new FilterNode(column, express, value);
|
||||
}
|
||||
|
||||
protected final <T> CharSequence createSQLExpress(final EntityInfo<T> info, final FilterBean bean) {
|
||||
/**
|
||||
* 该方法需要重载
|
||||
*
|
||||
* @param <T>
|
||||
* @param func
|
||||
* @param info
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
protected <T> CharSequence createSQLExpress(final Function<Class, EntityInfo> func, final EntityInfo<T> info, final FilterBean bean) {
|
||||
CharSequence sb0 = createElementSQLExpress(info, bean);
|
||||
if (this.nodes == null) return sb0;
|
||||
final StringBuilder rs = new StringBuilder();
|
||||
@@ -174,7 +175,7 @@ public class FilterNode {
|
||||
rs.append(sb0);
|
||||
}
|
||||
for (FilterNode node : this.nodes) {
|
||||
CharSequence f = node.createSQLExpress(info, bean);
|
||||
CharSequence f = node.createSQLExpress(func, info, bean);
|
||||
if (f == null || f.length() < 3) continue;
|
||||
if (more) rs.append(or ? " OR " : " AND ");
|
||||
rs.append(f);
|
||||
@@ -185,23 +186,29 @@ public class FilterNode {
|
||||
return rs;
|
||||
}
|
||||
|
||||
public static FilterNode create(String column, Serializable value) {
|
||||
return create(column, null, value);
|
||||
}
|
||||
|
||||
public static FilterNode create(String column, FilterExpress express, Serializable value) {
|
||||
return new FilterNode(column, express, value);
|
||||
}
|
||||
|
||||
protected final <T> CharSequence createElementSQLExpress(final EntityInfo<T> info, final FilterBean bean) {
|
||||
if (column == null) return null;
|
||||
final String tabalis = getTabalis();
|
||||
final String talis = getTabalis() == null ? "a" : getTabalis();
|
||||
if (express == ISNULL || express == ISNOTNULL) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (tabalis != null) sb.append(tabalis).append('.');
|
||||
sb.append(info.getSQLColumn(column)).append(' ').append(express.value());
|
||||
sb.append(info.getSQLColumn(talis, column)).append(' ').append(express.value());
|
||||
return sb;
|
||||
}
|
||||
final CharSequence val = formatToString(express, getElementValue(bean));
|
||||
if (val == null) return null;
|
||||
StringBuilder sb = new StringBuilder(32);
|
||||
if (tabalis != null) sb.append(tabalis).append('.');
|
||||
if (express == IGNORECASELIKE || express == IGNORECASENOTLIKE) {
|
||||
sb.append("LOWER(").append(info.getSQLColumn(column)).append(')');
|
||||
sb.append("LOWER(").append(info.getSQLColumn(talis, column)).append(')');
|
||||
} else {
|
||||
sb.append(info.getSQLColumn(column));
|
||||
sb.append(info.getSQLColumn(talis, column));
|
||||
}
|
||||
sb.append(' ');
|
||||
switch (express) {
|
||||
@@ -794,7 +801,7 @@ public class FilterNode {
|
||||
return toString(null);
|
||||
}
|
||||
|
||||
public String toString(final FilterBean bean) {
|
||||
protected String toString(final FilterBean bean) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (nodes == null) {
|
||||
if (column != null) {
|
||||
|
||||
Reference in New Issue
Block a user