EntityInfo增加getQueryColumns方法

This commit is contained in:
Redkale
2017-12-06 18:58:30 +08:00
parent 463269a796
commit 511ee8a6df
2 changed files with 24 additions and 4 deletions

View File

@@ -84,7 +84,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
if (writeprop.isEmpty()) writeprop = readprop;
this.initByProperties(unitName, readprop, writeprop);
}
//构造前调用
protected void preConstruct(String unitName, Properties readprop, Properties writeprop) {
}
@@ -1495,7 +1495,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
final Connection conn = createReadSQLConnection();
try {
final SelectColumn sels = selects;
final String sql = "SELECT * FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk);
final String sql = "SELECT " + info.getQueryColumns(null, selects) + " FROM " + info.getTable(pk) + " WHERE " + info.getPrimarySQLColumn() + " = " + FilterNode.formatToString(pk);
if (info.isLoggable(logger, Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql);
conn.setReadOnly(true);
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
@@ -1572,7 +1572,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info);
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
final String sql = "SELECT a.* FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
final String sql = "SELECT " + info.getQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) + ((where == null || where.length() == 0) ? "" : (" WHERE " + where));
if (info.isLoggable(logger, Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql);
conn.setReadOnly(true);
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
@@ -2298,7 +2298,7 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
final CharSequence join = node == null ? null : node.createSQLJoin(this, false, joinTabalis, new HashSet<>(), info);
final CharSequence where = node == null ? null : node.createSQLExpress(info, joinTabalis);
final String sql = "SELECT a.* FROM " + info.getTable(node) + " a" + (join == null ? "" : join)
final String sql = "SELECT " + info.getQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join)
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
if (info.isLoggable(logger, Level.FINEST)) {
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null || flipper.getLimit() < 1 ? "" : (" LIMIT " + flipper.getOffset() + "," + flipper.getLimit())));

View File

@@ -434,6 +434,26 @@ public final class EntityInfo<T> {
return deleteSQL.replace("${newtable}", getTable(bean));
}
/**
* 获取查询字段列表
*
* @param tabalis 表别名
* @param selects 过滤字段
*
* @return String
*/
public CharSequence getQueryColumns(String tabalis, SelectColumn selects) {
if (selects == null) return tabalis == null ? "*" : (tabalis + ".*");
StringBuilder sb = new StringBuilder();
for (Attribute attr : this.attributes) {
if (!selects.test(attr.field())) continue;
if (sb.length() > 0) sb.append(',');
sb.append(getSQLColumn(tabalis, attr.field()));
}
if (sb.length() == 0) sb.append('*');
return sb;
}
/**
* 根据主键值获取Entity的表名
*