Skip to content

Commit

Permalink
add conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreampie committed Feb 29, 2016
1 parent 5e82e93 commit c1093b8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cn.dreampie.common.entity;

/**
* Created by Dreampie on 16/2/29.
*/
public interface Conversion {

public Object read(Object v);

public Object write(Object v);
}
17 changes: 17 additions & 0 deletions resty-common/src/main/java/cn/dreampie/common/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public abstract class Entity<M extends Entity> {
*/
private Map<String, Object> modifyAttrs = new CaseInsensitiveMap<Object>();

/**
* get Conversion to convert attr
*
* @param attr
* @return
*/
public abstract Conversion getConversion(String attr);

/**
* Return attribute Map.
* Danger! The update method will ignore the attribute if you change it directly.
Expand Down Expand Up @@ -354,6 +362,15 @@ public Object[] getModifyAttrValues(String generatedKey) {
if (generatedKey != null && !generatedKey.isEmpty()) {
attrValueMap.remove(generatedKey);
}

Conversion conversion;
for (Map.Entry<String, Object> attrValueEntry : attrValueMap.entrySet()) {
conversion = getConversion(attrValueEntry.getKey());
if (conversion != null) {
attrValueEntry.setValue(conversion.write(attrValueEntry.getValue()));
}
}

Collection<Object> attrValueCollection = attrValueMap.values();
return attrValueCollection.toArray(new Object[attrValueCollection.size()]);
}
Expand Down
19 changes: 18 additions & 1 deletion resty-orm/src/main/java/cn/dreampie/orm/Base.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.dreampie.orm;

import cn.dreampie.common.entity.Conversion;
import cn.dreampie.common.entity.Entity;
import cn.dreampie.common.entity.exception.EntityException;
import cn.dreampie.common.util.Joiner;
Expand Down Expand Up @@ -52,6 +53,10 @@ protected Class<? extends M> getMClass() {
}
}

public Conversion getConversion(String attr) {
return null;
}

/**
* 获取当前实例数据表的元数据
*
Expand Down Expand Up @@ -764,13 +769,25 @@ public boolean save(List<M> models) {
//参数
Object[][] params = new Object[models.size()][columns.length];

String name;
Object value;
Conversion conversion;

for (int i = 0; i < params.length; i++) {
for (int j = 0; j < params[i].length; j++) {
//如果是自动生成主键 使用生成器生成
if (!generated && columns[j].equals(generatedKey) && models.get(i).get(generatedKey) == null) {
models.get(i).set(columns[j], generator.generateKey());
}
params[i][j] = models.get(i).get(columns[j]);

name = columns[j];
value = models.get(i).get(name);
conversion = models.get(i).getConversion(name);
if (conversion != null) {
value = conversion.write(value);
}

params[i][j] = value;
}
}

Expand Down
11 changes: 10 additions & 1 deletion resty-orm/src/main/java/cn/dreampie/orm/BaseBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.dreampie.orm;

import cn.dreampie.common.entity.Conversion;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand All @@ -21,7 +23,9 @@ public static <T> List<T> build(ResultSet rs, Class<? extends Base> modelClass,
buildLabelNamesAndTypes(rsmd, labelNames, types);

Base entity;
String name;
Object value;
Conversion conversion;

while (rs.next()) {
if (Record.class.isAssignableFrom(modelClass)) {
Expand All @@ -31,7 +35,12 @@ public static <T> List<T> build(ResultSet rs, Class<? extends Base> modelClass,
}
for (int i = 1; i <= columnCount; i++) {
value = rs.getObject(i);
entity.init(labelNames[i], value);
name = labelNames[i];
conversion = entity.getConversion(name);
if (conversion != null) {
value = conversion.read(value);
}
entity.init(name, value);
}
result.add((T) entity);
}
Expand Down

0 comments on commit c1093b8

Please sign in to comment.