This commit is contained in:
kamhung
2015-12-03 12:13:22 +08:00
parent ba0cc5c5a6
commit a211a18bc4
6 changed files with 156 additions and 153 deletions

View File

@@ -1402,7 +1402,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
}
/**
* 根据过滤对象FilterBean查询对象集合 对象只填充或排除SelectColumn指定的字段
* 根据过滤对象FilterBean查询对象集合 对象只填充或排除SelectField指定的字段
*
* @param <T>
* @param clazz
@@ -1502,7 +1502,7 @@ public final class DataDefaultSource implements DataSource, Nameable, Function<C
}
/**
* 根据过滤对象FilterBean和翻页对象Flipper查询一页的数据 对象只填充或排除SelectColumn指定的字段
* 根据过滤对象FilterBean和翻页对象Flipper查询一页的数据 对象只填充或排除SelectField指定的字段
*
* @param <T>
* @param clazz

View File

@@ -6,7 +6,6 @@
package com.wentch.redkale.source;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
@@ -54,13 +53,12 @@ public final class EntityCache<T> {
this.primary = info.primary;
this.needcopy = true;
this.reproduce = Reproduce.create(type, type, (m) -> {
char[] mn = m.getName().substring(3).toCharArray();
if (mn.length < 2 || Character.isLowerCase(mn[1])) mn[0] = Character.toLowerCase(mn[0]);
try {
Field f = type.getDeclaredField(new String(mn));
return f.getAnnotation(Transient.class) != null;
Transient t = type.getDeclaredField(m).getAnnotation(Transient.class);
if (t != null) return false;
return true;
} catch (Exception e) {
return false;
return true;
}
});
}
@@ -102,7 +100,7 @@ public final class EntityCache<T> {
if (selects == null) return (needcopy ? reproduce.copy(this.creator.create(), rs) : rs);
T t = this.creator.create();
for (Attribute attr : this.info.attributes) {
if (selects.validate(attr.field())) attr.set(t, attr.get(rs));
if (selects.test(attr.field())) attr.set(t, attr.get(rs));
}
return t;
}
@@ -122,7 +120,7 @@ public final class EntityCache<T> {
T rs = opt.get();
T t = this.creator.create();
for (Attribute attr : this.info.attributes) {
if (selects.validate(attr.field())) attr.set(t, attr.get(rs));
if (selects.test(attr.field())) attr.set(t, attr.get(rs));
}
return t;
}
@@ -324,7 +322,7 @@ public final class EntityCache<T> {
} else {
final List<Attribute<T, Serializable>> attrs = new ArrayList<>();
info.forEachAttribute((k, v) -> {
if (selects.validate(k)) attrs.add(v);
if (selects.test(k)) attrs.add(v);
});
Consumer<? super T> action = x -> {
final T item = creator.create();

View File

@@ -343,7 +343,7 @@ public final class EntityInfo<T> {
public T getValue(final SelectColumn sels, final ResultSet set) throws SQLException {
T obj = creator.create();
for (Attribute<T, Serializable> attr : queryAttributes) {
if (sels == null || sels.validate(attr.field())) {
if (sels == null || sels.test(attr.field())) {
Serializable o = (Serializable) set.getObject(this.getSQLColumn(null, attr.field()));
if (o != null) {
Class t = attr.type();

View File

@@ -1,139 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.wentch.redkale.source;
import java.util.*;
/**
*
* @author zhangjx
*/
public final class SelectColumn {
private String[] startWithColumns;
private String[] endWithColumns;
private String[] columns;
private boolean excludable; //是否排除
public SelectColumn() {
}
private SelectColumn(String[] columns0, boolean excludable) {
this.excludable = excludable;
final int len = columns0.length;
if (len < 1) return;
boolean reg = false;
int slen = 0, elen = 0;
for (String col : columns0) {
if (col.charAt(0) == '^') {
slen++;
reg = true;
} else if (col.charAt(col.length() - 1) == '$') {
elen++;
reg = true;
}
}
if (reg) {
if (slen == len) {
this.startWithColumns = new String[len];
for (int i = 0; i < len; i++) {
this.startWithColumns[i] = columns0[i].substring(1);
}
} else if (elen == len) {
this.endWithColumns = new String[len];
for (int i = 0; i < len; i++) {
this.endWithColumns[i] = columns0[i].substring(0, columns0[i].length() - 1);
}
} else {
List<String> starts = new ArrayList<>();
List<String> ends = new ArrayList<>();
List<String> strings = new ArrayList<>();
for (String col : columns0) {
if (col.charAt(0) == '^') {
starts.add(col.substring(1));
} else if (col.charAt(col.length() - 1) == '$') {
ends.add(col.substring(0, col.length() - 1));
} else {
strings.add(col);
}
}
if (!starts.isEmpty()) this.startWithColumns = starts.toArray(new String[starts.size()]);
if (!ends.isEmpty()) this.endWithColumns = ends.toArray(new String[ends.size()]);
if (!strings.isEmpty()) this.columns = strings.toArray(new String[strings.size()]);
}
} else {
this.columns = columns0;
}
}
/**
* class中的字段名
*
* @param columns
* @return
*/
public static SelectColumn createIncludes(String... columns) {
return new SelectColumn(columns, false);
}
/**
* class中的字段名
*
* @param columns
* @return
*/
public static SelectColumn createExcludes(String... columns) {
return new SelectColumn(columns, true);
}
public boolean validate(String column) {
if (this.columns != null) {
for (String col : this.columns) {
if (col.equalsIgnoreCase(column)) return !excludable;
}
}
if (this.startWithColumns != null) {
for (String col : this.startWithColumns) {
if (column.startsWith(col)) return !excludable;
}
}
if (this.endWithColumns != null) {
for (String col : this.endWithColumns) {
if (column.endsWith(col)) return !excludable;
}
}
return excludable;
}
public boolean isEmpty() {
return this.columns == null || this.columns.length == 0;
}
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
}
public boolean isExcludable() {
return excludable;
}
public void setExcludable(boolean excludable) {
this.excludable = excludable;
}
@Override
public String toString() {
return "SelectColumn{" + "columns=" + Arrays.toString(columns) + ", excludable=" + excludable + '}';
}
}

View File

@@ -14,7 +14,7 @@ public interface Reproduce<D, S> {
}
@SuppressWarnings("unchecked")
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass, final Predicate<java.lang.reflect.Method> excludeSetterPredicate) {
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass, final Predicate<String> columnPredicate) {
// ------------------------------------------------------------------------------
final String supDynName = Reproduce.class.getName().replace('.', '/');
final String destName = destClass.getName().replace('.', '/');
@@ -60,7 +60,15 @@ public interface Reproduce<D, S> {
boolean is = getter.getName().startsWith("is");
try {
setter = destClass.getMethod(getter.getName().replaceFirst(is ? "is" : "get", "set"), getter.getReturnType());
if (excludeSetterPredicate != null && excludeSetterPredicate.test(setter)) continue;
if (columnPredicate != null) {
String col = setter.getName().substring(3);
if (col.length() < 2 || Character.isLowerCase(col.charAt(1))) {
char[] cs = col.toCharArray();
cs[0] = Character.toLowerCase(cs[0]);
col = new String(cs);
}
if (!columnPredicate.test(col)) continue;
}
} catch (Exception e) {
continue;
}

View File

@@ -0,0 +1,136 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.wentch.redkale.util;
import java.util.*;
import java.util.function.*;
import java.util.regex.*;
/**
*
* @author zhangjx
*/
public class SelectColumn implements Predicate<String> {
private Pattern[] patterns;
private String[] columns;
private boolean excludable; //是否排除
public SelectColumn() {
}
protected SelectColumn(final String[] columns0, final boolean excludable) {
this.excludable = excludable;
final int len = columns0.length;
if (len < 1) return;
Pattern[] regs = null;
String[] cols = null;
int regcount = 0;
int colcount = 0;
for (String col : columns0) {
boolean reg = false;
for (int i = 0; i < col.length(); i++) {
char ch = col.charAt(i);
if (ch == '^' || ch == '$' || ch == '*' || ch == '?' || ch == '+' || ch == '[' || ch == '(') {
reg = true;
break;
}
}
if (reg) {
if (regs == null) regs = new Pattern[len];
regs[regcount++] = Pattern.compile(col);
} else {
if (cols == null) cols = new String[len];
cols[colcount++] = col;
}
}
if (regs != null) {
if (regcount == len) {
this.patterns = regs;
} else {
this.patterns = Arrays.copyOf(regs, regcount);
}
}
if (cols != null) {
if (colcount == len) {
this.columns = cols;
} else {
this.columns = Arrays.copyOf(cols, colcount);
}
}
}
/**
* class中的字段名
*
* @param columns
* @return
*/
public static SelectColumn createIncludes(String... columns) {
return new SelectColumn(columns, false);
}
/**
* class中的字段名
*
* @param columns
* @return
*/
public static SelectColumn createExcludes(String... columns) {
return new SelectColumn(columns, true);
}
@Override
public boolean test(final String column) {
if (this.columns != null) {
for (String col : this.columns) {
if (col.equalsIgnoreCase(column)) return !excludable;
}
}
if (this.patterns != null) {
for (Pattern reg : this.patterns) {
if (reg.matcher(column).find()) return !excludable;
}
}
return excludable;
}
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
}
public boolean isExcludable() {
return excludable;
}
public void setExcludable(boolean excludable) {
this.excludable = excludable;
}
public Pattern[] getPatterns() {
return patterns;
}
public void setPatterns(Pattern[] patterns) {
this.patterns = patterns;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName()).append("{excludable=").append(excludable);
if (columns != null) sb.append(", columns=").append(Arrays.toString(columns));
if (patterns != null) sb.append(", patterns=").append(Arrays.toString(patterns));
return sb.append('}').toString();
}
}