11package org .nutz .dao .impl ;
22
3- import java .sql .Connection ;
43import java .util .HashSet ;
54import java .util .Map ;
6- import java .util .Map .Entry ;
75import java .util .Set ;
86import java .util .concurrent .ConcurrentHashMap ;
97
10- import org .nutz .dao .ConnCallback ;
8+ import javax .sql .DataSource ;
9+
1110import org .nutz .dao .entity .Entity ;
1211import 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 ;
1513import org .nutz .dao .jdbc .JdbcExpert ;
16- import org .nutz .dao .jdbc .Jdbcs ;
1714import org .nutz .json .Json ;
1815import org .nutz .json .JsonFormat ;
1916import 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 /**
0 commit comments