1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.impl;
17
18 import com.ibatis.common.beans.Probe;
19 import com.ibatis.common.beans.ProbeFactory;
20 import com.ibatis.common.jdbc.exception.NestedSQLException;
21 import com.ibatis.common.util.PaginatedList;
22 import com.ibatis.sqlmap.client.SqlMapException;
23 import com.ibatis.sqlmap.client.event.RowHandler;
24 import com.ibatis.sqlmap.engine.cache.CacheKey;
25 import com.ibatis.sqlmap.engine.cache.CacheModel;
26 import com.ibatis.sqlmap.engine.exchange.DataExchangeFactory;
27 import com.ibatis.sqlmap.engine.execution.BatchException;
28 import com.ibatis.sqlmap.engine.execution.DefaultSqlExecutor;
29 import com.ibatis.sqlmap.engine.execution.SqlExecutor;
30 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
31 import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
32 import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
33 import com.ibatis.sqlmap.engine.mapping.statement.InsertStatement;
34 import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
35 import com.ibatis.sqlmap.engine.mapping.statement.PaginatedDataList;
36 import com.ibatis.sqlmap.engine.mapping.statement.SelectKeyStatement;
37 import com.ibatis.sqlmap.engine.scope.SessionScope;
38 import com.ibatis.sqlmap.engine.scope.StatementScope;
39 import com.ibatis.sqlmap.engine.transaction.Transaction;
40 import com.ibatis.sqlmap.engine.transaction.TransactionException;
41 import com.ibatis.sqlmap.engine.transaction.TransactionManager;
42 import com.ibatis.sqlmap.engine.transaction.TransactionState;
43 import com.ibatis.sqlmap.engine.transaction.user.UserProvidedTransaction;
44 import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
45
46 import java.sql.Connection;
47 import java.sql.SQLException;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Map;
52
53 import javax.sql.DataSource;
54
55
56
57
58 public class SqlMapExecutorDelegate {
59
60
61 private static final Probe PROBE = ProbeFactory.getProbe();
62
63
64 private boolean lazyLoadingEnabled = true;
65
66
67 private boolean cacheModelsEnabled = true;
68
69
70 private boolean enhancementEnabled = false;
71
72
73 private boolean useColumnLabel = true;
74
75
76 private boolean forceMultipleResultSetSupport;
77
78
79 private TransactionManager txManager;
80
81
82 private HashMap mappedStatements;
83
84
85 private HashMap cacheModels;
86
87
88 private HashMap resultMaps;
89
90
91 private HashMap parameterMaps;
92
93
94 protected SqlExecutor sqlExecutor;
95
96
97 private TypeHandlerFactory typeHandlerFactory;
98
99
100 private DataExchangeFactory dataExchangeFactory;
101
102
103 private ResultObjectFactory resultObjectFactory;
104
105
106 private boolean statementCacheEnabled = true;
107
108
109
110
111 public SqlMapExecutorDelegate() {
112 mappedStatements = new HashMap<>();
113 cacheModels = new HashMap<>();
114 resultMaps = new HashMap<>();
115 parameterMaps = new HashMap<>();
116
117 sqlExecutor = new DefaultSqlExecutor();
118 typeHandlerFactory = new TypeHandlerFactory();
119 dataExchangeFactory = new DataExchangeFactory(typeHandlerFactory);
120 }
121
122
123
124
125
126
127
128 public void setCustomExecutor(String sqlExecutorClass) {
129 try {
130 Class factoryClass = Class.forName(sqlExecutorClass);
131 sqlExecutor = (SqlExecutor) factoryClass.getDeclaredConstructor().newInstance();
132 } catch (Exception e) {
133 throw new SqlMapException(
134 "Error instantiating " + sqlExecutorClass + ". Please check the class given in properties file. Cause: " + e,
135 e);
136 }
137 }
138
139
140
141
142
143
144
145
146 @Deprecated
147 public int getMaxTransactions() {
148 return -1;
149 }
150
151
152
153
154
155
156 public DataExchangeFactory getDataExchangeFactory() {
157 return dataExchangeFactory;
158 }
159
160
161
162
163
164
165 public TypeHandlerFactory getTypeHandlerFactory() {
166 return typeHandlerFactory;
167 }
168
169
170
171
172
173
174 public boolean isLazyLoadingEnabled() {
175 return lazyLoadingEnabled;
176 }
177
178
179
180
181
182
183
184 public void setLazyLoadingEnabled(boolean lazyLoadingEnabled) {
185 this.lazyLoadingEnabled = lazyLoadingEnabled;
186 }
187
188
189
190
191
192
193 public boolean isCacheModelsEnabled() {
194 return cacheModelsEnabled;
195 }
196
197
198
199
200
201
202
203 public void setCacheModelsEnabled(boolean cacheModelsEnabled) {
204 this.cacheModelsEnabled = cacheModelsEnabled;
205 }
206
207
208
209
210
211
212 public boolean isEnhancementEnabled() {
213 return enhancementEnabled;
214 }
215
216
217
218
219
220
221
222 public void setEnhancementEnabled(boolean enhancementEnabled) {
223 this.enhancementEnabled = enhancementEnabled;
224 }
225
226
227
228
229
230
231 public boolean isUseColumnLabel() {
232 return useColumnLabel;
233 }
234
235
236
237
238
239
240
241 public void setUseColumnLabel(boolean useColumnLabel) {
242 this.useColumnLabel = useColumnLabel;
243 }
244
245
246
247
248
249
250 public TransactionManager getTxManager() {
251 return txManager;
252 }
253
254
255
256
257
258
259
260 public void setTxManager(TransactionManager txManager) {
261 this.txManager = txManager;
262 }
263
264
265
266
267
268
269
270 public void addMappedStatement(MappedStatement ms) {
271 if (mappedStatements.containsKey(ms.getId())) {
272 throw new SqlMapException("There is already a statement named " + ms.getId() + " in this SqlMap.");
273 }
274 ms.setBaseCacheKey(hashCode());
275 mappedStatements.put(ms.getId(), ms);
276 }
277
278
279
280
281
282
283 public Iterator getMappedStatementNames() {
284 return mappedStatements.keySet().iterator();
285 }
286
287
288
289
290
291
292
293
294
295 public MappedStatement getMappedStatement(String id) {
296 MappedStatement ms = (MappedStatement) mappedStatements.get(id);
297 if (ms == null) {
298 throw new SqlMapException("There is no statement named " + id + " in this SqlMap.");
299 }
300 return ms;
301 }
302
303
304
305
306
307
308
309 public void addCacheModel(CacheModel model) {
310 cacheModels.put(model.getId(), model);
311 }
312
313
314
315
316
317
318 public Iterator getCacheModelNames() {
319 return cacheModels.keySet().iterator();
320 }
321
322
323
324
325
326
327
328
329
330 public CacheModel getCacheModel(String id) {
331 CacheModel model = (CacheModel) cacheModels.get(id);
332 if (model == null) {
333 throw new SqlMapException("There is no cache model named " + id + " in this SqlMap.");
334 }
335 return model;
336 }
337
338
339
340
341
342
343
344 public void addResultMap(ResultMap map) {
345 resultMaps.put(map.getId(), map);
346 }
347
348
349
350
351
352
353 public Iterator getResultMapNames() {
354 return resultMaps.keySet().iterator();
355 }
356
357
358
359
360
361
362
363
364
365 public ResultMap getResultMap(String id) {
366 ResultMap map = (ResultMap) resultMaps.get(id);
367 if (map == null) {
368 throw new SqlMapException("There is no result map named " + id + " in this SqlMap.");
369 }
370 return map;
371 }
372
373
374
375
376
377
378
379 public void addParameterMap(ParameterMap map) {
380 parameterMaps.put(map.getId(), map);
381 }
382
383
384
385
386
387
388 public Iterator getParameterMapNames() {
389 return parameterMaps.keySet().iterator();
390 }
391
392
393
394
395
396
397
398
399
400 public ParameterMap getParameterMap(String id) {
401 ParameterMap map = (ParameterMap) parameterMaps.get(id);
402 if (map == null) {
403 throw new SqlMapException("There is no parameter map named " + id + " in this SqlMap.");
404 }
405 return map;
406 }
407
408
409
410
411 public void flushDataCache() {
412 Iterator models = cacheModels.values().iterator();
413 while (models.hasNext()) {
414 ((CacheModel) models.next()).flush();
415 }
416 }
417
418
419
420
421
422
423
424 public void flushDataCache(String id) {
425 CacheModel model = getCacheModel(id);
426 if (model != null) {
427 model.flush();
428 }
429 }
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 public Object insert(SessionScope sessionScope, String id, Object param) throws SQLException {
448 Object generatedKey = null;
449
450 MappedStatement ms = getMappedStatement(id);
451 Transaction trans = getTransaction(sessionScope);
452 boolean autoStart = trans == null;
453
454 try {
455 trans = autoStartTransaction(sessionScope, autoStart, trans);
456
457 SelectKeyStatement selectKeyStatement = null;
458 if (ms instanceof InsertStatement) {
459 selectKeyStatement = ((InsertStatement) ms).getSelectKeyStatement();
460 }
461
462
463
464
465 Object oldKeyValue = null;
466 String keyProperty = null;
467 boolean resetKeyValueOnFailure = false;
468 if (selectKeyStatement != null && !selectKeyStatement.isRunAfterSQL()) {
469 keyProperty = selectKeyStatement.getKeyProperty();
470 oldKeyValue = PROBE.getObject(param, keyProperty);
471 generatedKey = executeSelectKey(sessionScope, trans, ms, param);
472 resetKeyValueOnFailure = true;
473 }
474
475 StatementScope statementScope = beginStatementScope(sessionScope, ms);
476 try {
477 ms.executeUpdate(statementScope, trans, param);
478 } catch (SQLException e) {
479
480
481
482 if (resetKeyValueOnFailure) {
483 PROBE.setObject(param, keyProperty, oldKeyValue);
484 }
485
486 throw e;
487 } finally {
488 endStatementScope(statementScope);
489 }
490
491 if (selectKeyStatement != null && selectKeyStatement.isRunAfterSQL()) {
492 generatedKey = executeSelectKey(sessionScope, trans, ms, param);
493 }
494
495 autoCommitTransaction(sessionScope, autoStart);
496 } finally {
497 autoEndTransaction(sessionScope, autoStart);
498 }
499
500 return generatedKey;
501 }
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 private Object executeSelectKey(SessionScope sessionScope, Transaction trans, MappedStatement ms, Object param)
521 throws SQLException {
522 Object generatedKey = null;
523 StatementScope statementScope;
524 InsertStatement insert = (InsertStatement) ms;
525 SelectKeyStatement selectKeyStatement = insert.getSelectKeyStatement();
526 if (selectKeyStatement != null) {
527 statementScope = beginStatementScope(sessionScope, selectKeyStatement);
528 try {
529 generatedKey = selectKeyStatement.executeQueryForObject(statementScope, trans, param, null);
530 String keyProp = selectKeyStatement.getKeyProperty();
531 if (keyProp != null) {
532 PROBE.setObject(param, keyProp, generatedKey);
533 }
534 } finally {
535 endStatementScope(statementScope);
536 }
537 }
538 return generatedKey;
539 }
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556 public int update(SessionScope sessionScope, String id, Object param) throws SQLException {
557 int rows = 0;
558
559 MappedStatement ms = getMappedStatement(id);
560 Transaction trans = getTransaction(sessionScope);
561 boolean autoStart = trans == null;
562
563 try {
564 trans = autoStartTransaction(sessionScope, autoStart, trans);
565
566 StatementScope statementScope = beginStatementScope(sessionScope, ms);
567 try {
568 rows = ms.executeUpdate(statementScope, trans, param);
569 } finally {
570 endStatementScope(statementScope);
571 }
572
573 autoCommitTransaction(sessionScope, autoStart);
574 } finally {
575 autoEndTransaction(sessionScope, autoStart);
576 }
577
578 return rows;
579 }
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596 public int delete(SessionScope sessionScope, String id, Object param) throws SQLException {
597 return update(sessionScope, id, param);
598 }
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615 public Object queryForObject(SessionScope sessionScope, String id, Object paramObject) throws SQLException {
616 return queryForObject(sessionScope, id, paramObject, null);
617 }
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636 public Object queryForObject(SessionScope sessionScope, String id, Object paramObject, Object resultObject)
637 throws SQLException {
638 Object object = null;
639
640 MappedStatement ms = getMappedStatement(id);
641 Transaction trans = getTransaction(sessionScope);
642 boolean autoStart = trans == null;
643
644 try {
645 trans = autoStartTransaction(sessionScope, autoStart, trans);
646
647 StatementScope statementScope = beginStatementScope(sessionScope, ms);
648 try {
649 object = ms.executeQueryForObject(statementScope, trans, paramObject, resultObject);
650 } finally {
651 endStatementScope(statementScope);
652 }
653
654 autoCommitTransaction(sessionScope, autoStart);
655 } finally {
656 autoEndTransaction(sessionScope, autoStart);
657 }
658
659 return object;
660 }
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677 public List queryForList(SessionScope sessionScope, String id, Object paramObject) throws SQLException {
678 return queryForList(sessionScope, id, paramObject, SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS);
679 }
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700 public List queryForList(SessionScope sessionScope, String id, Object paramObject, int skip, int max)
701 throws SQLException {
702 List list = null;
703
704 MappedStatement ms = getMappedStatement(id);
705 Transaction trans = getTransaction(sessionScope);
706 boolean autoStart = trans == null;
707
708 try {
709 trans = autoStartTransaction(sessionScope, autoStart, trans);
710
711 StatementScope statementScope = beginStatementScope(sessionScope, ms);
712 try {
713 list = ms.executeQueryForList(statementScope, trans, paramObject, skip, max);
714 } finally {
715 endStatementScope(statementScope);
716 }
717
718 autoCommitTransaction(sessionScope, autoStart);
719 } finally {
720 autoEndTransaction(sessionScope, autoStart);
721 }
722
723 return list;
724 }
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741 public void queryWithRowHandler(SessionScope sessionScope, String id, Object paramObject, RowHandler rowHandler)
742 throws SQLException {
743
744 MappedStatement ms = getMappedStatement(id);
745 Transaction trans = getTransaction(sessionScope);
746 boolean autoStart = trans == null;
747
748 try {
749 trans = autoStartTransaction(sessionScope, autoStart, trans);
750
751 StatementScope statementScope = beginStatementScope(sessionScope, ms);
752 try {
753 ms.executeQueryWithRowHandler(statementScope, trans, paramObject, rowHandler);
754 } finally {
755 endStatementScope(statementScope);
756 }
757
758 autoCommitTransaction(sessionScope, autoStart);
759 } finally {
760 autoEndTransaction(sessionScope, autoStart);
761 }
762
763 }
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784 @Deprecated
785 public PaginatedList queryForPaginatedList(SessionScope sessionScope, String id, Object paramObject, int pageSize)
786 throws SQLException {
787 return new PaginatedDataList(sessionScope.getSqlMapExecutor(), id, paramObject, pageSize);
788 }
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807 public Map queryForMap(SessionScope sessionScope, String id, Object paramObject, String keyProp) throws SQLException {
808 return queryForMap(sessionScope, id, paramObject, keyProp, null);
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830 public Map queryForMap(SessionScope sessionScope, String id, Object paramObject, String keyProp, String valueProp)
831 throws SQLException {
832 Map map = new HashMap<>();
833
834 List list = queryForList(sessionScope, id, paramObject);
835
836 for (Object object : list) {
837 Object key = PROBE.getObject(object, keyProp);
838 Object value = null;
839 if (valueProp == null) {
840 value = object;
841 } else {
842 value = PROBE.getObject(object, valueProp);
843 }
844 map.put(key, value);
845 }
846
847 return map;
848 }
849
850
851
852
853
854
855
856
857
858
859
860 public void startTransaction(SessionScope sessionScope) throws SQLException {
861 try {
862 txManager.begin(sessionScope);
863 } catch (TransactionException e) {
864 throw new NestedSQLException("Could not start transaction. Cause: " + e, e);
865 }
866 }
867
868
869
870
871
872
873
874
875
876
877
878
879 public void startTransaction(SessionScope sessionScope, int transactionIsolation) throws SQLException {
880 try {
881 txManager.begin(sessionScope, transactionIsolation);
882 } catch (TransactionException e) {
883 throw new NestedSQLException("Could not start transaction. Cause: " + e, e);
884 }
885 }
886
887
888
889
890
891
892
893
894
895
896 public void commitTransaction(SessionScope sessionScope) throws SQLException {
897 try {
898
899 if (sessionScope.isInBatch()) {
900 executeBatch(sessionScope);
901 }
902 sqlExecutor.cleanup(sessionScope);
903 txManager.commit(sessionScope);
904 } catch (TransactionException e) {
905 throw new NestedSQLException("Could not commit transaction. Cause: " + e, e);
906 }
907 }
908
909
910
911
912
913
914
915
916
917
918 public void endTransaction(SessionScope sessionScope) throws SQLException {
919 try {
920 try {
921 sqlExecutor.cleanup(sessionScope);
922 } finally {
923 txManager.end(sessionScope);
924 }
925 } catch (TransactionException e) {
926 throw new NestedSQLException("Error while ending transaction. Cause: " + e, e);
927 }
928 }
929
930
931
932
933
934
935
936 public void startBatch(SessionScope sessionScope) {
937 sessionScope.setInBatch(true);
938 }
939
940
941
942
943
944
945
946
947
948
949
950
951 public int executeBatch(SessionScope sessionScope) throws SQLException {
952 sessionScope.setInBatch(false);
953 return sqlExecutor.executeBatch(sessionScope);
954 }
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970 public List executeBatchDetailed(SessionScope sessionScope) throws SQLException, BatchException {
971 sessionScope.setInBatch(false);
972 return sqlExecutor.executeBatchDetailed(sessionScope);
973 }
974
975
976
977
978
979
980
981
982
983 public void setUserProvidedTransaction(SessionScope sessionScope, Connection userConnection) {
984 if (sessionScope.getTransactionState() == TransactionState.STATE_USER_PROVIDED) {
985 sessionScope.recallTransactionState();
986 }
987 if (userConnection != null) {
988 Connection conn = userConnection;
989 sessionScope.saveTransactionState();
990 sessionScope.setTransaction(new UserProvidedTransaction(conn));
991 sessionScope.setTransactionState(TransactionState.STATE_USER_PROVIDED);
992 } else {
993 sessionScope.setTransaction(null);
994 sessionScope.closePreparedStatements();
995 sessionScope.cleanup();
996 }
997 }
998
999
1000
1001
1002
1003
1004 public DataSource getDataSource() {
1005 DataSource ds = null;
1006 if (txManager != null) {
1007 ds = txManager.getConfig().getDataSource();
1008 }
1009 return ds;
1010 }
1011
1012
1013
1014
1015
1016
1017 public SqlExecutor getSqlExecutor() {
1018 return sqlExecutor;
1019 }
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 public Transaction getTransaction(SessionScope sessionScope) {
1030 return sessionScope.getTransaction();
1031 }
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 protected void autoEndTransaction(SessionScope sessionScope, boolean autoStart) throws SQLException {
1047 if (autoStart) {
1048 sessionScope.getSqlMapTxMgr().endTransaction();
1049 }
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 protected void autoCommitTransaction(SessionScope sessionScope, boolean autoStart) throws SQLException {
1064 if (autoStart) {
1065 sessionScope.getSqlMapTxMgr().commitTransaction();
1066 }
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 protected Transaction autoStartTransaction(SessionScope sessionScope, boolean autoStart, Transaction trans)
1085 throws SQLException {
1086 Transaction transaction = trans;
1087 if (autoStart) {
1088 sessionScope.getSqlMapTxMgr().startTransaction();
1089 transaction = getTransaction(sessionScope);
1090 }
1091 return transaction;
1092 }
1093
1094 @Override
1095 public boolean equals(Object obj) {
1096 return this == obj;
1097 }
1098
1099 @Override
1100 public int hashCode() {
1101 CacheKey key = new CacheKey();
1102 if (txManager != null) {
1103 key.update(txManager);
1104 if (txManager.getConfig().getDataSource() != null) {
1105 key.update(txManager.getConfig().getDataSource());
1106 }
1107 }
1108 key.update(System.identityHashCode(this));
1109 return key.hashCode();
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122 protected StatementScope beginStatementScope(SessionScope sessionScope, MappedStatement mappedStatement) {
1123 StatementScope statementScope = new StatementScope(sessionScope);
1124 sessionScope.incrementRequestStackDepth();
1125 mappedStatement.initRequest(statementScope);
1126 return statementScope;
1127 }
1128
1129
1130
1131
1132
1133
1134
1135 protected void endStatementScope(StatementScope statementScope) {
1136 statementScope.getSession().decrementRequestStackDepth();
1137 }
1138
1139
1140
1141
1142
1143
1144 protected SessionScope beginSessionScope() {
1145 return new SessionScope();
1146 }
1147
1148
1149
1150
1151
1152
1153
1154 protected void endSessionScope(SessionScope sessionScope) {
1155 sessionScope.cleanup();
1156 }
1157
1158
1159
1160
1161
1162
1163 public ResultObjectFactory getResultObjectFactory() {
1164 return resultObjectFactory;
1165 }
1166
1167
1168
1169
1170
1171
1172
1173 public void setResultObjectFactory(ResultObjectFactory resultObjectFactory) {
1174 this.resultObjectFactory = resultObjectFactory;
1175 }
1176
1177
1178
1179
1180
1181
1182 public boolean isStatementCacheEnabled() {
1183 return statementCacheEnabled;
1184 }
1185
1186
1187
1188
1189
1190
1191
1192 public void setStatementCacheEnabled(boolean statementCacheEnabled) {
1193 this.statementCacheEnabled = statementCacheEnabled;
1194 }
1195
1196
1197
1198
1199
1200
1201 public boolean isForceMultipleResultSetSupport() {
1202 return forceMultipleResultSetSupport;
1203 }
1204
1205
1206
1207
1208
1209
1210
1211 public void setForceMultipleResultSetSupport(boolean forceMultipleResultSetSupport) {
1212 this.forceMultipleResultSetSupport = forceMultipleResultSetSupport;
1213 }
1214 }