This commit is contained in:
Redkale
2017-05-18 16:52:44 +08:00
parent ba928b389b
commit 01ea7f07f5

View File

@@ -43,6 +43,15 @@ public final class EntityInfo<T> {
//Entity构建器 //Entity构建器
private final Creator<T> creator; private final Creator<T> creator;
//Entity构建器参数
private final String[] constructorParameters;
//Entity构建器参数Attribute
private final Attribute<T, Serializable>[] constructorAttributes;
//Entity构建器参数Attribute
private final Attribute<T, Serializable>[] unconstructorAttributes;
//主键 //主键
final Attribute<T, Serializable> primary; final Attribute<T, Serializable> primary;
@@ -202,6 +211,13 @@ public final class EntityInfo<T> {
this.tableStrategy = dts; this.tableStrategy = dts;
this.creator = Creator.create(type); this.creator = Creator.create(type);
Creator.ConstructorParameters cp = null;
try {
cp = this.creator.getClass().getMethod("create", Object[].class).getAnnotation(Creator.ConstructorParameters.class);
} catch (Exception e) {
logger.severe(type + " cannot find ConstructorParameters Creator", e);
}
this.constructorParameters = (cp == null || cp.value().length < 1) ? null : cp.value();
Attribute idAttr0 = null; Attribute idAttr0 = null;
Map<String, String> aliasmap0 = null; Map<String, String> aliasmap0 = null;
Class cltmp = type; Class cltmp = type;
@@ -271,6 +287,22 @@ public final class EntityInfo<T> {
this.queryAttributes = queryattrs.toArray(new Attribute[queryattrs.size()]); this.queryAttributes = queryattrs.toArray(new Attribute[queryattrs.size()]);
this.insertAttributes = insertattrs.toArray(new Attribute[insertattrs.size()]); this.insertAttributes = insertattrs.toArray(new Attribute[insertattrs.size()]);
this.updateAttributes = updateattrs.toArray(new Attribute[updateattrs.size()]); this.updateAttributes = updateattrs.toArray(new Attribute[updateattrs.size()]);
if (this.constructorParameters == null) {
this.constructorAttributes = null;
this.unconstructorAttributes = null;
} else {
this.constructorAttributes = new Attribute[this.constructorParameters.length];
List<Attribute<T, Serializable>> unconstructorAttrs = new ArrayList<>();
for (Attribute<T, Serializable> attr : queryAttributes) {
int pos = Arrays.binarySearch(this.constructorParameters, attr.field());
if (pos >= 0) {
this.constructorAttributes[pos] = attr;
} else {
unconstructorAttrs.add(attr);
}
}
this.unconstructorAttributes = unconstructorAttrs.toArray(new Attribute[unconstructorAttrs.size()]);
}
if (table != null) { if (table != null) {
StringBuilder insertsb = new StringBuilder(); StringBuilder insertsb = new StringBuilder();
StringBuilder insertsb2 = new StringBuilder(); StringBuilder insertsb2 = new StringBuilder();
@@ -630,9 +662,30 @@ public final class EntityInfo<T> {
* @throws SQLException SQLException * @throws SQLException SQLException
*/ */
protected T getValue(final SelectColumn sels, final ResultSet set) throws SQLException { protected T getValue(final SelectColumn sels, final ResultSet set) throws SQLException {
T obj = creator.create(); T obj;
for (Attribute<T, Serializable> attr : queryAttributes) { Attribute<T, Serializable>[] attrs = this.queryAttributes;
if (this.constructorParameters == null) {
obj = creator.create();
} else {
Object[] cps = new Object[this.constructorParameters.length];
for (int i = 0; i < this.constructorAttributes.length; i++) {
Attribute<T, Serializable> attr = this.constructorAttributes[i];
if (sels == null || sels.test(attr.field())) { if (sels == null || sels.test(attr.field())) {
cps[i] = getFieldValue(attr, set);
}
}
obj = creator.create(cps);
attrs = this.unconstructorAttributes;
}
for (Attribute<T, Serializable> attr : attrs) {
if (sels == null || sels.test(attr.field())) {
attr.set(obj, getFieldValue(attr, set));
}
}
return obj;
}
protected Serializable getFieldValue(Attribute<T, Serializable> attr, final ResultSet set) throws SQLException {
final Class t = attr.type(); final Class t = attr.type();
Serializable o; Serializable o;
if (t == byte[].class) { if (t == byte[].class) {
@@ -682,9 +735,6 @@ public final class EntityInfo<T> {
} }
} }
} }
attr.set(obj, o); return o;
}
}
return obj;
} }
} }