11package io .github .cotide .dapper .repository ;
22
3- import io .github .cotide .dapper .core .attr .PrimaryKey ;
43import io .github .cotide .dapper .core .unit .Sql2oCache ;
54import io .github .cotide .dapper .core .unit .Sql2oUnitOfWork ;
65import io .github .cotide .dapper .basic .collections .PageList ;
76import io .github .cotide .dapper .basic .domain .Entity ;
8- import io .github .cotide .dapper .core .attr .Ignore ;
97import io .github .cotide .dapper .core .unit .Sql2oUtils ;
108import io .github .cotide .dapper .core .utility .Guard ;
119import io .github .cotide .dapper .exceptions .PrimaryKeyException ;
1210import io .github .cotide .dapper .exceptions .SqlBuildException ;
1311import io .github .cotide .dapper .query .Sql ;
14- import io .github .cotide .dapper .repository . SqlQueryBase ;
12+ import io .github .cotide .dapper .query . operations . Update ;
1513import io .github .cotide .dapper .repository .inter .IRepository ;
1614
1715import java .lang .reflect .Field ;
@@ -49,8 +47,30 @@ public TEntity update(TEntity entity) {
4947 return executeUpdate (entity );
5048 }
5149
50+ /**
51+ * 更新实体
52+ *
53+ * @param entity
54+ * @param tEntityUpdate
55+ * @return
56+ */
5257 @ Override
53- public Boolean delete (TEntity entity ) {
58+ public TEntity update (TEntity entity , Update <TEntity > tEntityUpdate ) {
59+ return executeUpdate (entity ,tEntityUpdate );
60+ }
61+
62+ /**
63+ * 创建更新对象
64+ *
65+ * @return
66+ */
67+ @ Override
68+ public Update <TEntity > createUpdate () {
69+ return new Update <>(modelClass );
70+ }
71+
72+ @ Override
73+ public boolean delete (TEntity entity ) {
5474 return executeDelete (entity );
5575 }
5676
@@ -105,7 +125,7 @@ public PageList<TEntity> getPageList(int pageIndex, int pageSize, String sql, Ob
105125 * @param object
106126 * @return 影响行数
107127 */
108- private < TEntity extends Entity > TEntity executeInsert (TEntity object ) {
128+ private TEntity executeInsert (TEntity object ) {
109129
110130 Guard .isNotNull (object ,"object" );
111131 Class <?> modelClass = object .getClass ();
@@ -173,14 +193,24 @@ private <TEntity extends Entity> TEntity executeInsert(TEntity object) {
173193 }
174194 }
175195
196+ private TEntity executeUpdate (TEntity object )
197+ {
198+ Update <TEntity > update = this .createUpdate () ;
199+
200+ for (Field field : Sql2oCache .computeNoKeyModelFields (modelClass ))
201+ {
202+ update .set (Sql2oCache .getColumnName (field ),Sql2oUtils .getValue (object ,field ));
203+ }
204+ return executeUpdate (object ,update );
205+ }
176206
177207 /**
178- * 新增
179- *
180- * @param object
181- * @return 影响行数
208+ * 更新
209+ * @param object 修改对象
210+ * @param updateOperations 修改参数
211+ * @return
182212 */
183- private < T extends Entity > T executeUpdate (T object ) {
213+ private TEntity executeUpdate (TEntity object , Update < TEntity > updateOperations ) {
184214
185215 Guard .isNotNull (object ,"object" );
186216 Class <?> modelClass = object .getClass ();
@@ -196,11 +226,23 @@ private <T extends Entity> T executeUpdate(T object) {
196226 }
197227 StringBuffer sb = new StringBuffer ();
198228 List <Object > values = new ArrayList <>();
199- for (Field field : Sql2oCache .computeNoKeyModelFields (modelClass ))
229+
230+ if (updateOperations == null || !updateOperations .isHaveOps ())
200231 {
201- sb .append (String .format (",%s = ?" , Sql2oCache .getColumnName (field )));
202- values .add (Sql2oUtils .getValue (object ,field ));
232+ throw new SqlBuildException ("Update Operations cannot be null" );
233+ }
234+
235+ Map <String ,Object > map = updateOperations .getOps ();
236+ for (Map .Entry <String ,Object > entry : map .entrySet ())
237+ {
238+ sb .append (String .format (",%s = ?" , entry .getKey ()));
239+ values .add (entry .getValue ());
203240 }
241+ // for (Field field : Sql2oCache.computeNoKeyModelFields(modelClass))
242+ // {
243+ // sb.append(String.format(",%s = ?", Sql2oCache.getColumnName(field)));
244+ // values.add(Sql2oUtils.getValue(object,field));
245+ // }
204246 try {
205247 String updateSql = String .format ("UPDATE %s SET %s WHERE %s = ?" ,
206248 tableName , sb .substring (1 ), pkColumn );
@@ -230,10 +272,9 @@ private <T extends Entity> T executeUpdate(T object) {
230272 /**
231273 * 删除
232274 * @param object
233- * @param <T>
234275 * @return
235276 */
236- private < T extends Entity > boolean executeDelete (T object ){
277+ private boolean executeDelete (TEntity object ){
237278
238279 Guard .isNotNull (object ,"object" );
239280 Class <?> modelClass = object .getClass ();
0 commit comments