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