Skip to content

Commit f7b4bcb

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 11819f9 + 6d0d3e8 commit f7b4bcb

6 files changed

Lines changed: 224 additions & 100 deletions

File tree

src/org/nutz/dao/impl/DaoSupport.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.nutz.lang.Lang;
3333
import org.nutz.lang.Mirror;
3434
import org.nutz.lang.Strings;
35-
import org.nutz.lang.util.Callback;
3635
import org.nutz.log.Log;
3736
import org.nutz.log.Logs;
3837

@@ -224,11 +223,7 @@ public void invoke(Connection conn) throws Exception {
224223

225224
if(!isLazy)
226225
{
227-
holder = new EntityHolder(this.expert, new Callback<ConnCallback>() {
228-
public void invoke(ConnCallback obj) {
229-
run(obj);
230-
}
231-
});
226+
holder = new EntityHolder(this.expert, dataSource);
232227
holder.maker = createEntityMaker();
233228
}
234229
setRunner(runner);

src/org/nutz/dao/impl/EntityHolder.java

Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package org.nutz.dao.impl;
22

3-
import java.sql.Connection;
43
import java.util.HashSet;
54
import java.util.Map;
6-
import java.util.Map.Entry;
75
import java.util.Set;
86
import java.util.concurrent.ConcurrentHashMap;
97

10-
import org.nutz.dao.ConnCallback;
8+
import javax.sql.DataSource;
9+
1110
import org.nutz.dao.entity.Entity;
1211
import org.nutz.dao.entity.EntityMaker;
13-
import org.nutz.dao.impl.entity.NutEntity;
14-
import org.nutz.dao.impl.entity.field.NutMappingField;
12+
import org.nutz.dao.impl.entity.MapEntityMaker;
1513
import org.nutz.dao.jdbc.JdbcExpert;
16-
import org.nutz.dao.jdbc.Jdbcs;
1714
import org.nutz.json.Json;
1815
import org.nutz.json.JsonFormat;
1916
import org.nutz.lang.Lang;
20-
import org.nutz.lang.Mirror;
21-
import org.nutz.lang.eject.EjectFromMap;
22-
import org.nutz.lang.inject.InjectToMap;
23-
import org.nutz.lang.util.Callback;
2417

2518
/**
2619
* 封装一些获取实体对象的帮助函数
@@ -36,12 +29,16 @@ public class EntityHolder {
3629

3730
private Map<Class<?>, Entity<?>> map;
3831

39-
protected Callback<ConnCallback> connCallback;
32+
protected DataSource dataSource;
33+
34+
protected MapEntityMaker mapEntityMaker;
4035

41-
public EntityHolder(JdbcExpert expert, Callback<ConnCallback> connCallback) {
36+
public EntityHolder(JdbcExpert expert, DataSource dataSource) {
4237
this.expert = expert;
43-
this.connCallback = connCallback;
38+
this.dataSource = dataSource;
4439
this.map = new ConcurrentHashMap<Class<?>, Entity<?>>();
40+
mapEntityMaker = new MapEntityMaker();
41+
mapEntityMaker.init(dataSource, expert, this);
4542
}
4643

4744
public void set(Entity<?> en) {
@@ -79,87 +76,8 @@ public <T> Entity<T> getEntity(Class<T> classOfT) {
7976
return (Entity<T>) re;
8077
}
8178

82-
@SuppressWarnings({"rawtypes", "unchecked"})
8379
public <T extends Map<String, ?>> Entity<T> makeEntity(String tableName, T map) {
84-
final NutEntity<T> en = new NutEntity(map.getClass());
85-
en.setTableName(tableName);
86-
en.setViewName(tableName);
87-
boolean check = false;
88-
for (Entry<String, ?> entry : map.entrySet()) {
89-
String key = entry.getKey();
90-
// 是实体补充描述吗?
91-
if (key.startsWith("#")) {
92-
en.getMetas().put(key.substring(1), entry.getValue().toString());
93-
continue;
94-
}
95-
// 以 "." 开头的字段,不是实体字段
96-
else if (key.startsWith(".")) {
97-
continue;
98-
}
99-
100-
// 是实体字段
101-
Object value = entry.getValue();
102-
Mirror<?> mirror = Mirror.me(value);
103-
NutMappingField ef = new NutMappingField(en);
104-
105-
while (true) {
106-
if (key.startsWith("+")) {
107-
ef.setAsAutoIncreasement();
108-
if (mirror != null && mirror.isIntLike())
109-
ef.setAsId();
110-
key = key.substring(1);
111-
}
112-
else if (key.startsWith("!")) {
113-
ef.setAsNotNull();
114-
key = key.substring(1);
115-
}
116-
else if (key.startsWith("*")) {
117-
key = key.substring(1);
118-
if (mirror != null && mirror.isIntLike())
119-
ef.setAsId();
120-
else
121-
ef.setAsName();
122-
} else {
123-
break;
124-
}
125-
}
126-
ef.setName(key);
127-
128-
ef.setType(null == value ? Object.class : value.getClass());
129-
ef.setColumnName(key);
130-
131-
// 猜测一下数据库类型
132-
Jdbcs.guessEntityFieldColumnType(ef);
133-
ef.setAdaptor(expert.getAdaptor(ef));
134-
if (mirror != null)
135-
ef.setType(mirror.getType());
136-
ef.setInjecting(new InjectToMap(key)); // 这里比较纠结,回设的时候应该用什么呢?
137-
ef.setEjecting(new EjectFromMap(entry.getKey()));
138-
139-
if (ef.isAutoIncreasement()
140-
&& ef.isId()
141-
&& expert.isSupportAutoIncrement()
142-
&& !expert.isSupportGeneratedKeys()) {
143-
en.addAfterInsertMacro(expert.fetchPojoId(en, ef));
144-
}
145-
146-
en.addMappingField(ef);
147-
148-
if (mirror != null && !check)
149-
check = mirror.isEnum();
150-
}
151-
en.checkCompositeFields(null);
152-
153-
// 最后在数据库中验证一下实体各个字段
154-
if (check)
155-
connCallback.invoke(new ConnCallback() {
156-
public void invoke(Connection conn) throws Exception {
157-
expert.setupEntityField(conn, en);
158-
}
159-
});
160-
161-
// 搞定返回
162-
return en;
80+
return mapEntityMaker.make(tableName, map);
16381
}
16482

16583
/**
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package org.nutz.dao.impl.entity;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
import java.util.Map;
6+
import java.util.Map.Entry;
7+
8+
import javax.sql.DataSource;
9+
10+
import org.nutz.dao.entity.Entity;
11+
import org.nutz.dao.entity.annotation.ColType;
12+
import org.nutz.dao.impl.EntityHolder;
13+
import org.nutz.dao.impl.entity.field.NutMappingField;
14+
import org.nutz.dao.jdbc.JdbcExpert;
15+
import org.nutz.dao.jdbc.Jdbcs;
16+
import org.nutz.dao.jdbc.ValueAdaptor;
17+
import org.nutz.lang.Mirror;
18+
import org.nutz.lang.eject.EjectFromMap;
19+
import org.nutz.lang.inject.InjectToMap;
20+
import org.nutz.log.Log;
21+
import org.nutz.log.Logs;
22+
23+
public class MapEntityMaker {
24+
25+
private static final Log log = Logs.get();
26+
27+
protected JdbcExpert expert;
28+
29+
protected DataSource dataSource;
30+
31+
@SuppressWarnings({"unchecked", "rawtypes"})
32+
public <T extends Map<String, ?>> Entity<T> make(String tableName, T map) {
33+
final NutEntity<T> en = new NutEntity(map.getClass());
34+
en.setTableName(tableName);
35+
en.setViewName(tableName);
36+
boolean check = false;
37+
for (Entry<String, ?> entry : map.entrySet()) {
38+
String key = entry.getKey();
39+
// 是实体补充描述吗?
40+
if (key.startsWith("#")) {
41+
en.getMetas().put(key.substring(1), entry.getValue().toString());
42+
continue;
43+
}
44+
// 以 "." 开头的字段,不是实体字段
45+
else if (key.startsWith(".")) {
46+
continue;
47+
}
48+
49+
// 是实体字段
50+
Object value = entry.getValue();
51+
Mirror<?> mirror = Mirror.me(value);
52+
NutMappingField ef = new NutMappingField(en);
53+
54+
while (true) {
55+
if (key.startsWith("+")) {
56+
ef.setAsAutoIncreasement();
57+
if (mirror != null && mirror.isIntLike())
58+
ef.setAsId();
59+
key = key.substring(1);
60+
} else if (key.startsWith("!")) {
61+
ef.setAsNotNull();
62+
key = key.substring(1);
63+
} else if (key.startsWith("*")) {
64+
key = key.substring(1);
65+
if (mirror != null && mirror.isIntLike())
66+
ef.setAsId();
67+
else
68+
ef.setAsName();
69+
} else {
70+
break;
71+
}
72+
}
73+
ef.setName(key);
74+
ef.setColumnName(key);
75+
76+
// 类型是啥呢?
77+
if (map.containsKey("." + key + ".type")) {
78+
ef.setType((Class) map.get("." + key + ".type"));
79+
} else {
80+
ef.setType(null == value ? Object.class : value.getClass());
81+
}
82+
// ColType是啥呢?
83+
if (map.containsKey("." + key + ".coltype")) {
84+
ef.setColumnType((ColType) map.get("." + key + ".coltype"));
85+
} else {
86+
// 猜测一下数据库类型
87+
Jdbcs.guessEntityFieldColumnType(ef);
88+
}
89+
// 适配器类型是啥呢?
90+
if (map.containsKey("." + key + ".adaptor")) {
91+
ef.setAdaptor((ValueAdaptor) map.get("." + key + ".adapter"));
92+
} else {
93+
ef.setAdaptor(expert.getAdaptor(ef));
94+
}
95+
ef.setInjecting(new InjectToMap(key)); // 这里比较纠结,回设的时候应该用什么呢?
96+
ef.setEjecting(new EjectFromMap(entry.getKey()));
97+
98+
if (ef.isAutoIncreasement()
99+
&& ef.isId()
100+
&& expert.isSupportAutoIncrement()
101+
&& !expert.isSupportGeneratedKeys()) {
102+
en.addAfterInsertMacro(expert.fetchPojoId(en, ef));
103+
}
104+
105+
en.addMappingField(ef);
106+
107+
if (mirror != null && !check)
108+
check = mirror.isEnum();
109+
}
110+
en.checkCompositeFields(null);
111+
112+
// 最后在数据库中验证一下实体各个字段
113+
if (check) {
114+
Connection conn = null;
115+
try {
116+
try {
117+
conn = dataSource.getConnection();
118+
expert.setupEntityField(conn, en);
119+
}
120+
finally {
121+
if (conn != null)
122+
conn.close();
123+
}
124+
}
125+
catch (SQLException e) {
126+
log.debug(e.getMessage(), e);
127+
}
128+
}
129+
130+
// 搞定返回
131+
return en;
132+
}
133+
134+
public void init(DataSource datasource, JdbcExpert expert, EntityHolder holder) {
135+
this.expert = expert;
136+
this.dataSource = datasource;
137+
}
138+
139+
}

test/org/nutz/dao/test/entity/DynamicEntityParsingTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import static org.junit.Assert.*;
44

5-
import org.junit.Test;
5+
import java.sql.Timestamp;
66

7+
import org.junit.Test;
8+
import org.nutz.dao.Cnd;
79
import org.nutz.dao.TableName;
810
import org.nutz.dao.entity.Entity;
11+
import org.nutz.dao.entity.Record;
912
import org.nutz.dao.impl.entity.field.ManyManyLinkField;
1013
import org.nutz.dao.test.DaoCase;
1114
import org.nutz.dao.test.meta.Tank;
15+
import org.nutz.dao.test.meta.issue1286.Issue1286;
16+
import org.nutz.http.Request.METHOD;
17+
import org.nutz.lang.random.R;
1218

1319
public class DynamicEntityParsingTest extends DaoCase {
1420

@@ -25,4 +31,15 @@ public void tank_many_many_link_test() {
2531
pojos.dropPlatoon(1);
2632
}
2733

34+
@Test
35+
public void test_issue_1286() {
36+
dao.create(Issue1286.class, true);
37+
Record record = new Record();
38+
record.set(".table", "t_issue_1286");
39+
record.set("*+id", 0).set("name", R.UU32()).set("method", METHOD.POST).set("t", null);
40+
record.set(".t.type", Timestamp.class);
41+
dao.insert(record);
42+
record = dao.fetch("t_issue_1286", Cnd.NEW());
43+
assertEquals("POST", record.get("method"));
44+
}
2845
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.nutz.dao.test.meta.issue1286;
2+
3+
import java.sql.Timestamp;
4+
5+
import org.nutz.dao.entity.annotation.Id;
6+
import org.nutz.dao.entity.annotation.Name;
7+
import org.nutz.dao.entity.annotation.Table;
8+
import org.nutz.http.Request.METHOD;
9+
10+
@Table("t_issue_1286")
11+
public class Issue1286 {
12+
13+
@Id
14+
private int id;
15+
16+
@Name
17+
private String name;
18+
19+
private METHOD method;
20+
21+
private Timestamp t;
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public METHOD getMethod() {
40+
return method;
41+
}
42+
43+
public void setMethod(METHOD method) {
44+
this.method = method;
45+
}
46+
47+
public Timestamp getT() {
48+
return t;
49+
}
50+
51+
public void setT(Timestamp t) {
52+
this.t = t;
53+
}
54+
}

test/org/nutz/json/JsonTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,5 +1047,6 @@ public void test_issue_1285() throws IOException {
10471047
assertEquals("post", map.keySet().iterator().next());
10481048
assertEquals(METHOD.valueOf("POST"), map.values().iterator().next());
10491049
assertEquals(METHOD.valueOf("POST"), map.get("post"));
1050+
Json.fromJson(METHOD.class, "'POST'");
10501051
}
10511052
}

0 commit comments

Comments
 (0)