1 package org.apache.torque.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.math.BigDecimal;
24 import java.sql.Connection;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.sql.Statement;
29 import java.sql.Types;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.torque.Column;
38 import org.apache.torque.Database;
39 import org.apache.torque.TooManyRowsException;
40 import org.apache.torque.Torque;
41 import org.apache.torque.TorqueException;
42 import org.apache.torque.adapter.Adapter;
43 import org.apache.torque.adapter.IDMethod;
44 import org.apache.torque.criteria.FromElement;
45 import org.apache.torque.map.ColumnMap;
46 import org.apache.torque.map.MapHelper;
47 import org.apache.torque.map.TableMap;
48 import org.apache.torque.oid.IdGenerator;
49 import org.apache.torque.om.NumberKey;
50 import org.apache.torque.om.ObjectKey;
51 import org.apache.torque.om.SimpleKey;
52 import org.apache.torque.om.StringKey;
53 import org.apache.torque.om.mapper.RecordMapper;
54 import org.apache.torque.sql.Query;
55 import org.apache.torque.sql.SqlBuilder;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class BasePeerImpl<T> implements Serializable
77 {
78
79
80
81 private static final long serialVersionUID = -7702123730779032381L;
82
83
84 private static final Log log = LogFactory.getLog(BasePeerImpl.class);
85
86
87 private RecordMapper<T> recordMapper = null;
88
89
90 private TableMap tableMap = null;
91
92
93 private String databaseName = null;
94
95
96
97
98 public BasePeerImpl()
99 {
100 super();
101 }
102
103
104
105
106
107
108
109
110 public BasePeerImpl(RecordMapper<T> recordMapper, TableMap tableMap, String databaseName)
111 {
112 this();
113 setRecordMapper(recordMapper);
114 setTableMap(tableMap);
115 setDatabaseName(databaseName);
116 }
117
118
119
120
121
122
123 public void setRecordMapper(RecordMapper<T> recordMapper)
124 {
125 this.recordMapper = recordMapper;
126 }
127
128
129
130
131
132
133 public RecordMapper<T> getRecordMapper() throws TorqueException
134 {
135 if (recordMapper == null)
136 {
137 throw new TorqueException("No record mapper injected");
138 }
139
140 return recordMapper;
141 }
142
143
144
145
146
147
148 public void setTableMap(TableMap tableMap)
149 {
150 this.tableMap = tableMap;
151 }
152
153
154
155
156
157
158 public TableMap getTableMap() throws TorqueException
159 {
160 if (tableMap == null)
161 {
162 throw new TorqueException("No table map injected");
163 }
164
165 return tableMap;
166 }
167
168
169
170
171
172
173 public void setDatabaseName(String databaseName)
174 {
175 this.databaseName = databaseName;
176 }
177
178
179
180
181
182
183 public String getDatabaseName() throws TorqueException
184 {
185 if (databaseName == null)
186 {
187 throw new TorqueException("No database name injected");
188 }
189
190 return databaseName;
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 public List<T> resultSet2Objects(
207 java.sql.ResultSet resultSet,
208 Adapter adapter)
209 throws TorqueException
210 {
211 try
212 {
213 List<T> result = new ArrayList<T>();
214 RecordMapper<T> mapper = getRecordMapper();
215 while (resultSet.next())
216 {
217 result.add(mapper.processRow(resultSet, 0));
218 }
219 return result;
220 }
221 catch (SQLException e)
222 {
223 if (adapter != null)
224 {
225 throw adapter.toTorqueException(e);
226 }
227 throw new TorqueException(e);
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241 @Deprecated
242 public String[] initCriteriaKeys(
243 String tableName,
244 String[] columnNames)
245 {
246 String[] keys = new String[columnNames.length];
247 for (int i = 0; i < columnNames.length; i++)
248 {
249 keys[i] = tableName + "." + columnNames[i].toUpperCase();
250 }
251 return keys;
252 }
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 @Deprecated
274 public int deleteAll(
275 Connection con,
276 String table,
277 String column,
278 int value)
279 throws TorqueException
280 {
281 Statement statement = null;
282 try
283 {
284 statement = con.createStatement();
285
286 StringBuffer query = new StringBuffer();
287 query.append("DELETE FROM ")
288 .append(table)
289 .append(" WHERE ")
290 .append(column)
291 .append(" = ")
292 .append(value);
293
294 return statement.executeUpdate(query.toString());
295 }
296 catch (SQLException e)
297 {
298 throw new TorqueException(e);
299 }
300 finally
301 {
302 if (statement != null)
303 {
304 try
305 {
306 statement.close();
307 }
308 catch (SQLException e)
309 {
310 throw new TorqueException(e);
311 }
312 }
313 }
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 @Deprecated
336 public int deleteAll(String table, String column, int value)
337 throws TorqueException
338 {
339 Connection con = null;
340 try
341 {
342 con = Transaction.begin(Torque.getDefaultDB());
343 int result = deleteAll(con, table, column, value);
344 Transaction.commit(con);
345 con = null;
346 return result;
347 }
348 finally
349 {
350 if (con != null)
351 {
352 Transaction.safeRollback(con);
353 }
354 }
355 }
356
357
358
359
360
361
362
363
364
365
366
367 public int doDelete(org.apache.torque.criteria.Criteria criteria)
368 throws TorqueException
369 {
370 Connection connection = null;
371 try
372 {
373 setDbName(criteria);
374 connection = Transaction.begin(criteria.getDbName());
375 int deletedRows = doDelete(criteria, connection);
376 Transaction.commit(connection);
377 connection = null;
378 return deletedRows;
379 }
380 finally
381 {
382 if (connection != null)
383 {
384 Transaction.safeRollback(connection);
385 }
386 }
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401 public int doDelete(
402 org.apache.torque.criteria.Criteria criteria,
403 Connection connection)
404 throws TorqueException
405 {
406 correctBooleans(criteria);
407 setDbName(criteria);
408
409 Query query = SqlBuilder.buildQuery(criteria);
410 query.setType(Query.Type.DELETE);
411
412 String fullTableName;
413 if (tableMap == null)
414 {
415 fullTableName = SqlBuilder.guessFullTableFromCriteria(criteria);
416 }
417 else
418 {
419 fullTableName = SqlBuilder.getFullTableName(
420 tableMap.getName(),
421 criteria.getDbName());
422 }
423 boolean ownTableAdded = false;
424 for (FromElement fromElement : query.getFromClause())
425 {
426
427
428 if (fullTableName.equalsIgnoreCase(fromElement.getFromExpression()))
429 {
430 ownTableAdded = true;
431 break;
432 }
433 }
434 if (!ownTableAdded)
435 {
436 query.getFromClause().add(new FromElement(fullTableName));
437 }
438 String sql = query.toString();
439
440 PreparedStatement preparedStatement = null;
441 try
442 {
443 preparedStatement = connection.prepareStatement(sql);
444 List<Object> replacements = setPreparedStatementReplacements(
445 preparedStatement,
446 query.getPreparedStatementReplacements(),
447 0);
448 long startTime = System.currentTimeMillis();
449 log.debug("Executing delete " + sql
450 + ", parameters = "
451 + replacements);
452
453 int affectedRows = preparedStatement.executeUpdate();
454 long queryEndTime = System.currentTimeMillis();
455 log.trace("delete took " + (queryEndTime - startTime)
456 + " milliseconds");
457
458 preparedStatement.close();
459 preparedStatement = null;
460 return affectedRows;
461 }
462 catch (SQLException e)
463 {
464 final String dbName = criteria.getDbName();
465 final Adapter adapter = Torque.getAdapter(dbName);
466 if (adapter != null)
467 {
468 throw adapter.toTorqueException(e);
469 }
470 throw new TorqueException(e);
471 }
472 finally
473 {
474 if (preparedStatement != null)
475 {
476 try
477 {
478 preparedStatement.close();
479 }
480 catch (SQLException e)
481 {
482 log.warn("error closing prepared statement", e);
483 }
484 }
485 }
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 @Deprecated
504 protected int doDelete(Criteria criteria) throws TorqueException
505 {
506 Connection con = null;
507 try
508 {
509 con = Transaction.begin(criteria.getDbName());
510 int result = doDelete(criteria, con);
511 Transaction.commit(con);
512 con = null;
513 return result;
514 }
515 finally
516 {
517 if (con != null)
518 {
519 Transaction.safeRollback(con);
520 }
521 }
522 }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 @Deprecated
541 protected int doDelete(Criteria criteria, Connection con)
542 throws TorqueException
543 {
544 if (criteria.values().isEmpty())
545 {
546 throw new TorqueException("No conditions found in Criteria");
547 }
548 Criteria.Criterion criterion
549 = criteria.values().iterator().next();
550
551 TableMap tableMapFromCriteria = MapHelper.getTableMap(
552 criterion.getColumn(), criteria, null);
553 if (tableMapFromCriteria == null)
554 {
555 throw new TorqueException("Unqualified column name in criteria"
556 + " or table name not found in database map");
557 }
558
559 Query query = SqlBuilder.buildQuery(criteria);
560 query.setType(Query.Type.DELETE);
561
562 String fullTableName = null;
563 if (tableMap != null)
564 {
565 fullTableName = SqlBuilder.getFullTableName(
566 tableMap.getName(),
567 criteria.getDbName());
568 }
569 else
570 {
571 Column column = criteria.values().iterator().next().getColumn();
572 fullTableName = SqlBuilder.getFullTableName(
573 column.getFullTableName(),
574 criteria.getDbName());
575 }
576
577 boolean ownTableAdded = false;
578 for (FromElement fromElement : query.getFromClause())
579 {
580
581
582 if (fullTableName.equalsIgnoreCase(fromElement.getFromExpression()))
583 {
584 ownTableAdded = true;
585 break;
586 }
587 }
588 if (!ownTableAdded)
589 {
590 query.getFromClause().add(new FromElement(fullTableName));
591 }
592 String sql = query.toString();
593
594 PreparedStatement preparedStatement = null;
595 try
596 {
597 preparedStatement = con.prepareStatement(sql);
598 List<Object> replacements = setPreparedStatementReplacements(
599 preparedStatement,
600 query.getPreparedStatementReplacements(),
601 0);
602 long startTime = System.currentTimeMillis();
603 log.debug("Executing delete " + sql
604 + ", parameters = "
605 + replacements);
606
607 int affectedRows = preparedStatement.executeUpdate();
608 long queryEndTime = System.currentTimeMillis();
609 log.trace("delete took " + (queryEndTime - startTime)
610 + " milliseconds");
611
612 preparedStatement.close();
613 preparedStatement = null;
614 return affectedRows;
615 }
616 catch (SQLException e)
617 {
618 final String dbName = criteria.getDbName();
619 final Adapter adapter = Torque.getAdapter(dbName);
620 if (adapter != null)
621 {
622 throw adapter.toTorqueException(e);
623 }
624 throw new TorqueException(e);
625 }
626 finally
627 {
628 if (preparedStatement != null)
629 {
630 try
631 {
632 preparedStatement.close();
633 }
634 catch (SQLException e)
635 {
636 log.warn("error closing prepared statement", e);
637 }
638 }
639 }
640 }
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663 public ObjectKey doInsert(ColumnValues insertValues)
664 throws TorqueException
665 {
666 String databaseNameFromInsertValues = insertValues.getDbName();
667 if (databaseNameFromInsertValues == null)
668 {
669 databaseNameFromInsertValues = getDatabaseName();
670 }
671 Connection connection = null;
672 try
673 {
674 connection = Transaction.begin(databaseNameFromInsertValues);
675 ObjectKey id = doInsert(insertValues, connection);
676 Transaction.commit(connection);
677 connection = null;
678 return id;
679 }
680 finally
681 {
682 if (connection != null)
683 {
684 Transaction.safeRollback(connection);
685 }
686 }
687 }
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711 public ObjectKey doInsert(
712 ColumnValues insertValues,
713 Connection connection)
714 throws TorqueException
715 {
716 if (insertValues == null)
717 {
718 throw new TorqueException("insertValues is null");
719 }
720 if (connection == null)
721 {
722 throw new TorqueException("connection is null");
723 }
724 String databaseNameFromInsertValues = insertValues.getDbName();
725 if (databaseNameFromInsertValues == null)
726 {
727 databaseNameFromInsertValues = getDatabaseName();
728 }
729 Database database = Torque.getDatabase(databaseNameFromInsertValues);
730 Object keyInfo = getIdMethodInfo();
731 IdGenerator keyGen = database.getIdGenerator(
732 getTableMap().getPrimaryKeyMethod());
733
734 SimpleKey id = null;
735
736
737 ColumnMap primaryKey = null;
738 if (keyGen != null)
739 {
740
741 primaryKey = getTableMap().getPrimaryKey();
742
743
744
745 if (keyGen.isPriorToInsert() && primaryKey != null
746 && !insertValues.containsKey(
747 primaryKey))
748 {
749 id = getId(primaryKey, keyGen, connection, keyInfo);
750 insertValues.put(
751 primaryKey,
752 new JdbcTypedValue(id.getValue(), id.getJdbcType()));
753 }
754 }
755
756 List<String> columnNames = new ArrayList<String>();
757 List<JdbcTypedValue> replacementObjects
758 = new ArrayList<JdbcTypedValue>();
759 for (Map.Entry<Column, JdbcTypedValue> columnValue
760 : insertValues.entrySet())
761 {
762 Column column = columnValue.getKey();
763 columnNames.add(column.getColumnName());
764 JdbcTypedValue value = columnValue.getValue();
765 replacementObjects.add(value);
766 }
767
768 String fullTableName = SqlBuilder.getFullTableName(
769 getTableMap().getName(),
770 databaseNameFromInsertValues);
771 StringBuilder query = new StringBuilder("INSERT INTO ")
772 .append(fullTableName)
773 .append("(")
774 .append(StringUtils.join(columnNames, ","))
775 .append(") VALUES (");
776 for (int i = 0; i < columnNames.size(); ++i)
777 {
778 if (i != 0)
779 {
780 query.append(",");
781 }
782 query.append("?");
783 }
784 query.append(")");
785
786 PreparedStatement preparedStatement = null;
787 try
788 {
789 preparedStatement = connection.prepareStatement(query.toString());
790 int position = 1;
791 for (JdbcTypedValue replacementObject : replacementObjects)
792 {
793 Object value = replacementObject.getValue();
794 if (value != null)
795 {
796 if (replacementObject.getJdbcType() != Types.BLOB
797 && replacementObject.getJdbcType() != Types.CLOB)
798 {
799 preparedStatement.setObject(
800 position,
801 value,
802 replacementObject.getJdbcType());
803 }
804 else
805 {
806 preparedStatement.setObject(
807 position,
808 value);
809 }
810 }
811 else
812 {
813 preparedStatement.setNull(
814 position,
815 replacementObject.getJdbcType());
816 }
817 position++;
818 }
819 long startTime = System.currentTimeMillis();
820 log.debug("Executing insert " + query.toString()
821 + " using parameters " + replacementObjects);
822
823 preparedStatement.executeUpdate();
824 long queryEndTime = System.currentTimeMillis();
825 log.trace("insert took " + (queryEndTime - startTime)
826 + " milliseconds");
827
828 preparedStatement.close();
829 preparedStatement = null;
830 }
831 catch (SQLException e)
832 {
833 final String dbName = insertValues.getDbName();
834 final Adapter adapter = Torque.getAdapter(dbName);
835 if (adapter != null)
836 {
837 throw adapter.toTorqueException(e);
838 }
839 throw new TorqueException(e);
840 }
841 finally
842 {
843 if (preparedStatement != null)
844 {
845 try
846 {
847 preparedStatement.close();
848 }
849 catch (SQLException e)
850 {
851 log.warn("error closing prepared statement", e);
852 }
853 }
854 }
855
856
857
858 if (keyGen != null && keyGen.isPostInsert()
859 && primaryKey != null
860 && !insertValues.containsKey(
861 primaryKey))
862 {
863 id = getId(primaryKey, keyGen, connection, keyInfo);
864 }
865
866 return id;
867 }
868
869
870
871
872
873
874
875
876
877 private Object getIdMethodInfo()
878 throws TorqueException
879 {
880 IDMethod idMethod = tableMap.getPrimaryKeyMethod();
881 if (IDMethod.NATIVE == idMethod)
882 {
883 Adapter adapter = Torque.getAdapter(getDatabaseName());
884 if (adapter == null)
885 {
886 throw new TorqueException(
887 "missing adapter configuration for database "
888 + getDatabaseName()
889 + "check the Torque configuration");
890 }
891 idMethod = adapter.getIDMethodType();
892 }
893 Object keyInfo = tableMap.getPrimaryKeyMethodInfo(idMethod);
894 return keyInfo;
895 }
896
897
898
899
900
901
902
903
904
905
906
907
908 private SimpleKey getId(
909 ColumnMap pk,
910 IdGenerator keyGen,
911 Connection con,
912 Object keyInfo)
913 throws TorqueException
914 {
915 SimpleKey id = null;
916
917 if (pk != null && keyGen != null)
918 {
919 if (pk.getType() instanceof Number)
920 {
921 id = new NumberKey(
922 keyGen.getIdAsBigDecimal(con, keyInfo));
923 }
924 else
925 {
926 id = new StringKey(keyGen.getIdAsString(con, keyInfo));
927 }
928 }
929
930 return id;
931 }
932
933
934
935
936
937
938
939 public void addSelectColumns(org.apache.torque.criteria.Criteria criteria)
940 {
941 ColumnMap[] columns = this.tableMap.getColumns();
942
943 for (ColumnMap c : columns)
944 {
945 criteria.addSelectColumn(c);
946 }
947 }
948
949
950
951
952
953
954
955
956
957
958
959 @Deprecated
960 public void addSelectColumns(Criteria criteria)
961 {
962 ColumnMap[] columns = this.tableMap.getColumns();
963
964 for (ColumnMap c : columns)
965 {
966 criteria.addSelectColumn(c);
967 }
968 }
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983 @Deprecated
984 public List<T> doSelect(Criteria criteria)
985 throws TorqueException
986 {
987 if (criteria.getSelectColumns().size() == 0)
988 {
989 addSelectColumns(criteria);
990 }
991 setDbName(criteria);
992
993 return doSelect(
994 criteria,
995 getRecordMapper());
996 }
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008 public List<T> doSelect(org.apache.torque.criteria.Criteria criteria)
1009 throws TorqueException
1010 {
1011 if (criteria.getSelectColumns().size() == 0)
1012 {
1013 addSelectColumns(criteria);
1014 }
1015 setDbName(criteria);
1016
1017 return doSelect(
1018 criteria,
1019 getRecordMapper());
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038 @Deprecated
1039 public List<T> doSelect(
1040 Criteria criteria,
1041 Connection connection)
1042 throws TorqueException
1043 {
1044 if (criteria.getSelectColumns().size() == 0)
1045 {
1046 addSelectColumns(criteria);
1047 }
1048 setDbName(criteria);
1049
1050 return doSelect(
1051 criteria,
1052 getRecordMapper(),
1053 connection);
1054 }
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 public List<T> doSelect(
1069 org.apache.torque.criteria.Criteria criteria,
1070 Connection connection)
1071 throws TorqueException
1072 {
1073 if (criteria.getSelectColumns().size() == 0)
1074 {
1075 addSelectColumns(criteria);
1076 }
1077 setDbName(criteria);
1078
1079 return doSelect(
1080 criteria,
1081 getRecordMapper(),
1082 connection);
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095 public T doSelectSingleRecord(org.apache.torque.criteria.Criteria criteria)
1096 throws TorqueException
1097 {
1098 List<T> recordList = doSelect(criteria);
1099 T record = null;
1100 if (recordList.size() > 1)
1101 {
1102 throw new TooManyRowsException("Criteria " + criteria
1103 + " matched more than one record");
1104 }
1105 if (!recordList.isEmpty())
1106 {
1107 record = recordList.get(0);
1108 }
1109 return record;
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124 public T doSelectSingleRecord(
1125 org.apache.torque.criteria.Criteria criteria,
1126 Connection connection)
1127 throws TorqueException
1128 {
1129 List<T> recordList = doSelect(criteria, connection);
1130 T record = null;
1131 if (recordList.size() > 1)
1132 {
1133 throw new TooManyRowsException("Criteria " + criteria
1134 + " matched more than one record");
1135 }
1136 if (!recordList.isEmpty())
1137 {
1138 record = recordList.get(0);
1139 }
1140 return record;
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 @Deprecated
1159 public <TT> List<TT> doSelect(
1160 Criteria criteria,
1161 RecordMapper<TT> mapper)
1162 throws TorqueException
1163 {
1164 Connection connection = null;
1165 try
1166 {
1167 connection = Transaction.begin(criteria.getDbName());
1168
1169 List<TT> result = doSelect(
1170 criteria,
1171 mapper,
1172 connection);
1173
1174 Transaction.commit(connection);
1175 connection = null;
1176 return result;
1177 }
1178 finally
1179 {
1180 if (connection != null)
1181 {
1182 Transaction.safeRollback(connection);
1183 }
1184 }
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198 public <TT> List<TT> doSelect(
1199 org.apache.torque.criteria.Criteria criteria,
1200 RecordMapper<TT> mapper)
1201 throws TorqueException
1202 {
1203 Connection connection = null;
1204 try
1205 {
1206 connection = Transaction.begin(criteria.getDbName());
1207
1208 List<TT> result = doSelect(
1209 criteria,
1210 mapper,
1211 connection);
1212
1213 Transaction.commit(connection);
1214 connection = null;
1215 return result;
1216 }
1217 finally
1218 {
1219 if (connection != null)
1220 {
1221 Transaction.safeRollback(connection);
1222 }
1223 }
1224 }
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 public List<T> doSelect(String query)
1236 throws TorqueException
1237 {
1238 return doSelect(
1239 query,
1240 getRecordMapper(),
1241 getDatabaseName());
1242 }
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 public List<T> doSelect(
1257 String query,
1258 String dbName,
1259 Connection connection)
1260 throws TorqueException
1261 {
1262 return doSelect(
1263 query,
1264 getRecordMapper(),
1265 dbName,
1266 connection);
1267 }
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282 public <TT> List<TT> doSelect(
1283 String query,
1284 RecordMapper<TT> mapper,
1285 String dbName)
1286 throws TorqueException
1287 {
1288 Connection connection = null;
1289
1290 try
1291 {
1292 connection = Transaction.begin((dbName == null)
1293 ? Torque.getDefaultDB()
1294 : dbName);
1295
1296 List<TT> result = doSelect(
1297 query,
1298 mapper,
1299 dbName,
1300 connection);
1301
1302 Transaction.commit(connection);
1303 connection = null;
1304 return result;
1305 }
1306 finally
1307 {
1308 if (connection != null)
1309 {
1310 Transaction.safeRollback(connection);
1311 }
1312 }
1313 }
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329 public <TT> List<TT> doSelect(
1330 String query,
1331 RecordMapper<TT> mapper,
1332 String dbName,
1333 Connection connection)
1334 throws TorqueException
1335 {
1336 if (connection == null)
1337 {
1338 throw new NullPointerException("connection is null");
1339 }
1340
1341 List<TT> result = new ArrayList<TT>();
1342 Statement statement = null;
1343 ResultSet resultSet = null;
1344 try
1345 {
1346 statement = connection.createStatement();
1347 long startTime = System.currentTimeMillis();
1348 log.debug("Executing query " + query);
1349
1350 resultSet = statement.executeQuery(query.toString());
1351 long queryEndTime = System.currentTimeMillis();
1352 log.trace("query took " + (queryEndTime - startTime)
1353 + " milliseconds");
1354
1355 while (resultSet.next())
1356 {
1357 TT rowResult = mapper.processRow(resultSet, 0);
1358 result.add(rowResult);
1359 }
1360 long mappingEndTime = System.currentTimeMillis();
1361 log.trace("mapping took " + (mappingEndTime - queryEndTime)
1362 + " milliseconds");
1363 }
1364 catch (SQLException e)
1365 {
1366 final Adapter adapter = Torque.getAdapter(dbName);
1367 if (adapter != null)
1368 {
1369 throw adapter.toTorqueException(e);
1370 }
1371 throw new TorqueException(e);
1372 }
1373 finally
1374 {
1375 if (resultSet != null)
1376 {
1377 try
1378 {
1379 resultSet.close();
1380 }
1381 catch (SQLException e)
1382 {
1383 log.warn("error closing resultSet", e);
1384 }
1385 }
1386 if (statement != null)
1387 {
1388 try
1389 {
1390 statement.close();
1391 }
1392 catch (SQLException e)
1393 {
1394 log.warn("error closing statement", e);
1395 }
1396 }
1397 }
1398 return result;
1399 }
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418 @Deprecated
1419 public <TT> List<TT> doSelect(
1420 Criteria criteria,
1421 RecordMapper<TT> mapper,
1422 Connection connection)
1423 throws TorqueException
1424 {
1425 correctBooleans(criteria);
1426
1427 Query query = SqlBuilder.buildQuery(criteria);
1428 if (query.getFromClause().isEmpty())
1429 {
1430 String tableName = SqlBuilder.getFullTableName(
1431 getTableMap().getName(),
1432 criteria.getDbName());
1433 query.getFromClause().add(new FromElement(tableName));
1434 }
1435
1436 PreparedStatement statement = null;
1437 ResultSet resultSet = null;
1438 try
1439 {
1440 statement = connection.prepareStatement(query.toString());
1441
1442 List<Object> replacements = setPreparedStatementReplacements(
1443 statement,
1444 query.getPreparedStatementReplacements(),
1445 0);
1446
1447 long startTime = System.currentTimeMillis();
1448 log.debug("Executing query " + query
1449 + ", parameters = "
1450 + replacements);
1451
1452 resultSet = statement.executeQuery();
1453 long queryEndTime = System.currentTimeMillis();
1454 log.trace("query took " + (queryEndTime - startTime)
1455 + " milliseconds");
1456
1457 long offset;
1458 Database database = Torque.getDatabase(criteria.getDbName());
1459 if (database.getAdapter().supportsNativeOffset())
1460 {
1461 offset = 0;
1462 }
1463 else
1464 {
1465 offset = criteria.getOffset();
1466 }
1467
1468 long limit;
1469 if (database.getAdapter().supportsNativeLimit())
1470 {
1471 limit = -1;
1472 }
1473 else
1474 {
1475 if (database.getAdapter().supportsNativeOffset())
1476 {
1477 limit = criteria.getLimit();
1478 }
1479 else
1480 {
1481 if (criteria.getLimit() == -1)
1482 {
1483 limit = criteria.getLimit();
1484 }
1485 else
1486 {
1487 limit = offset + criteria.getLimit();
1488 }
1489 }
1490 }
1491
1492 List<TT> result = new ArrayList<TT>();
1493 int rowNumber = 0;
1494 while (resultSet.next())
1495 {
1496 if (rowNumber < offset)
1497 {
1498 rowNumber++;
1499 continue;
1500 }
1501 if (limit >= 0 && rowNumber >= limit)
1502 {
1503 break;
1504 }
1505
1506 TT rowResult = mapper.processRow(resultSet, 0);
1507 result.add(rowResult);
1508
1509 rowNumber++;
1510 }
1511 long mappingEndTime = System.currentTimeMillis();
1512 log.trace("mapping took " + (mappingEndTime - queryEndTime)
1513 + " milliseconds");
1514
1515 if (criteria.isSingleRecord() && result.size() > 1)
1516 {
1517 throw new TooManyRowsException(
1518 "Criteria expected single Record and "
1519 + "Multiple Records were selected");
1520 }
1521 return result;
1522 }
1523 catch (SQLException e)
1524 {
1525 String dbName = criteria.getDbName();
1526 final Adapter adapter = Torque.getAdapter(dbName);
1527 if (adapter != null)
1528 {
1529 throw adapter.toTorqueException(e);
1530 }
1531 throw new TorqueException(e);
1532 }
1533 finally
1534 {
1535 if (resultSet != null)
1536 {
1537 try
1538 {
1539 resultSet.close();
1540 }
1541 catch (SQLException e)
1542 {
1543 log.warn("error closing resultSet", e);
1544 }
1545 }
1546 if (statement != null)
1547 {
1548 try
1549 {
1550 statement.close();
1551 }
1552 catch (SQLException e)
1553 {
1554 log.warn("error closing statement", e);
1555 }
1556 }
1557 }
1558 }
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573 public <TT> List<TT> doSelect(
1574 org.apache.torque.criteria.Criteria criteria,
1575 RecordMapper<TT> mapper,
1576 Connection connection)
1577 throws TorqueException
1578 {
1579 correctBooleans(criteria);
1580
1581 Query query = SqlBuilder.buildQuery(criteria);
1582 if (query.getFromClause().isEmpty())
1583 {
1584 String tableName = SqlBuilder.getFullTableName(
1585 getTableMap().getName(),
1586 criteria.getDbName());
1587 query.getFromClause().add(new FromElement(tableName));
1588 }
1589
1590 PreparedStatement statement = null;
1591 ResultSet resultSet = null;
1592 try
1593 {
1594 statement = connection.prepareStatement(query.toString());
1595 if (query.getFetchSize() != null)
1596 {
1597 statement.setFetchSize(query.getFetchSize());
1598 }
1599
1600 List<Object> replacements = setPreparedStatementReplacements(
1601 statement,
1602 query.getPreparedStatementReplacements(),
1603 0);
1604
1605 long startTime = System.currentTimeMillis();
1606 log.debug("Executing query " + query
1607 + ", parameters = "
1608 + replacements);
1609
1610 resultSet = statement.executeQuery();
1611 long queryEndTime = System.currentTimeMillis();
1612 log.trace("query took " + (queryEndTime - startTime)
1613 + " milliseconds");
1614
1615 long offset;
1616 Database database = Torque.getDatabase(criteria.getDbName());
1617 if (database.getAdapter().supportsNativeOffset())
1618 {
1619 offset = 0;
1620 }
1621 else
1622 {
1623 offset = criteria.getOffset();
1624 }
1625
1626 long limit;
1627 if (database.getAdapter().supportsNativeLimit())
1628 {
1629 limit = -1;
1630 }
1631 else
1632 {
1633 if (database.getAdapter().supportsNativeOffset())
1634 {
1635 limit = criteria.getLimit();
1636 }
1637 else
1638 {
1639 if (criteria.getLimit() == -1)
1640 {
1641 limit = criteria.getLimit();
1642 }
1643 else
1644 {
1645 limit = offset + criteria.getLimit();
1646 }
1647 }
1648 }
1649
1650 List<TT> result = new ArrayList<TT>();
1651 int rowNumber = 0;
1652 while (resultSet.next())
1653 {
1654 if (rowNumber < offset)
1655 {
1656 rowNumber++;
1657 continue;
1658 }
1659 if (limit >= 0 && rowNumber >= limit)
1660 {
1661 break;
1662 }
1663
1664 TT rowResult = mapper.processRow(resultSet, 0);
1665 result.add(rowResult);
1666
1667 rowNumber++;
1668 }
1669 long mappingEndTime = System.currentTimeMillis();
1670 log.trace("mapping took " + (mappingEndTime - queryEndTime)
1671 + " milliseconds");
1672
1673 if (criteria.isSingleRecord() && result.size() > 1)
1674 {
1675 throw new TooManyRowsException(
1676 "Criteria expected single Record and "
1677 + "Multiple Records were selected");
1678 }
1679 return result;
1680 }
1681 catch (SQLException e)
1682 {
1683 String dbName = criteria.getDbName();
1684 final Adapter adapter = Torque.getAdapter(dbName);
1685 if (adapter != null)
1686 {
1687 throw adapter.toTorqueException(e);
1688 }
1689 throw new TorqueException(e);
1690 }
1691 finally
1692 {
1693 if (resultSet != null)
1694 {
1695 try
1696 {
1697 resultSet.close();
1698 }
1699 catch (SQLException e)
1700 {
1701 log.warn("error closing resultSet", e);
1702 }
1703 }
1704 if (statement != null)
1705 {
1706 try
1707 {
1708 statement.close();
1709 }
1710 catch (SQLException e)
1711 {
1712 log.warn("error closing statement", e);
1713 }
1714 }
1715 }
1716 }
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729 public <TT> TT doSelectSingleRecord(
1730 org.apache.torque.criteria.Criteria criteria,
1731 RecordMapper<TT> mapper)
1732 throws TorqueException
1733 {
1734 List<TT> resultList = doSelect(criteria, mapper);
1735 TT result = null;
1736 if (resultList.size() > 1)
1737 {
1738 throw new TooManyRowsException("Criteria " + criteria
1739 + " matched more than one record");
1740 }
1741 if (!resultList.isEmpty())
1742 {
1743 result = resultList.get(0);
1744 }
1745 return result;
1746 }
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760 public <TT> TT doSelectSingleRecord(
1761 org.apache.torque.criteria.Criteria criteria,
1762 RecordMapper<TT> mapper,
1763 Connection connection)
1764 throws TorqueException
1765 {
1766 List<TT> resultList = doSelect(
1767 criteria,
1768 mapper,
1769 connection);
1770 TT result = null;
1771 if (resultList.size() > 1)
1772 {
1773 throw new TooManyRowsException("Criteria " + criteria
1774 + " matched more than one record");
1775 }
1776 if (!resultList.isEmpty())
1777 {
1778 result = resultList.get(0);
1779 }
1780 return result;
1781 }
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806 public int doUpdate(ColumnValues updateValues)
1807 throws TorqueException
1808 {
1809 String databaseNameFromUpdateValues = updateValues.getDbName();
1810 if (databaseNameFromUpdateValues == null)
1811 {
1812 databaseNameFromUpdateValues = getDatabaseName();
1813 }
1814 Connection connection = null;
1815 try
1816 {
1817 connection = Transaction.begin(databaseNameFromUpdateValues);
1818 int result = doUpdate(updateValues, connection);
1819 Transaction.commit(connection);
1820 connection = null;
1821 return result;
1822 }
1823 finally
1824 {
1825 if (connection != null)
1826 {
1827 Transaction.safeRollback(connection);
1828 }
1829 }
1830 }
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856 public int doUpdate(
1857 ColumnValues updateValues,
1858 Connection connection)
1859 throws TorqueException
1860 {
1861 ColumnMap pk = getTableMap().getPrimaryKey();
1862 org.apache.torque.criteria.Criteria selectCriteria = null;
1863
1864 if (pk != null && updateValues.containsKey(pk.getSqlExpression()))
1865 {
1866 selectCriteria = new org.apache.torque.criteria.Criteria();
1867 selectCriteria.where(pk,
1868 updateValues.remove(pk.getSqlExpression()));
1869 }
1870 else
1871 {
1872 throw new TorqueException("No PK specified for database update");
1873 }
1874
1875 return doUpdate(selectCriteria, updateValues, connection);
1876 }
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895 @Deprecated
1896 public int doUpdate(
1897 Criteria selectCriteria,
1898 ColumnValues updateValues)
1899 throws TorqueException
1900 {
1901 String databaseNameFromUpdateValues = updateValues.getDbName();
1902 if (databaseNameFromUpdateValues == null)
1903 {
1904 databaseNameFromUpdateValues = getDatabaseName();
1905 }
1906 Connection connection = null;
1907 try
1908 {
1909 connection = Transaction.begin(databaseNameFromUpdateValues);
1910 int result = doUpdate(selectCriteria, updateValues, connection);
1911 Transaction.commit(connection);
1912 connection = null;
1913 return result;
1914 }
1915 finally
1916 {
1917 if (connection != null)
1918 {
1919 Transaction.safeRollback(connection);
1920 }
1921 }
1922 }
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937 public int doUpdate(
1938 org.apache.torque.criteria.Criteria selectCriteria,
1939 ColumnValues updateValues)
1940 throws TorqueException
1941 {
1942 String databaseNameFromUpdateValues = updateValues.getDbName();
1943 if (databaseNameFromUpdateValues == null)
1944 {
1945 databaseNameFromUpdateValues = getDatabaseName();
1946 }
1947 Connection connection = null;
1948 try
1949 {
1950 connection = Transaction.begin(databaseNameFromUpdateValues);
1951 int result = doUpdate(selectCriteria, updateValues, connection);
1952 Transaction.commit(connection);
1953 connection = null;
1954 return result;
1955 }
1956 finally
1957 {
1958 if (connection != null)
1959 {
1960 Transaction.safeRollback(connection);
1961 }
1962 }
1963 }
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982 @Deprecated
1983 public int doUpdate(
1984 Criteria criteria,
1985 ColumnValues updateValues,
1986 Connection connection)
1987 throws TorqueException
1988 {
1989 Query query = SqlBuilder.buildQuery(criteria);
1990 query.setType(Query.Type.UPDATE);
1991
1992 query.getFromClause().clear();
1993 String fullTableName = SqlBuilder.getFullTableName(
1994 getTableMap().getName(),
1995 criteria.getDbName());
1996 query.getFromClause().add(new FromElement(fullTableName));
1997
1998 List<JdbcTypedValue> replacementObjects
1999 = new ArrayList<JdbcTypedValue>();
2000 for (Map.Entry<Column, JdbcTypedValue> updateValue
2001 : updateValues.entrySet())
2002 {
2003 Column column = updateValue.getKey();
2004 query.getSelectClause().add(column.getColumnName());
2005 replacementObjects.add(updateValue.getValue());
2006 }
2007
2008 PreparedStatement preparedStatement = null;
2009 try
2010 {
2011 preparedStatement = connection.prepareStatement(query.toString());
2012 int position = 1;
2013 for (JdbcTypedValue replacementObject : replacementObjects)
2014 {
2015 Object value = replacementObject.getValue();
2016 if (value != null)
2017 {
2018 preparedStatement.setObject(position, value);
2019 }
2020 else
2021 {
2022 preparedStatement.setNull(
2023 position,
2024 replacementObject.getJdbcType());
2025 }
2026 position++;
2027 }
2028 List<Object> replacements = setPreparedStatementReplacements(
2029 preparedStatement,
2030 query.getPreparedStatementReplacements(),
2031 position - 1);
2032 long startTime = System.currentTimeMillis();
2033 log.debug("Executing update " + query.toString()
2034 + " using update parameters " + replacementObjects
2035 + " and query parameters "
2036 + replacements);
2037
2038 int affectedRows = preparedStatement.executeUpdate();
2039 long queryEndTime = System.currentTimeMillis();
2040 log.trace("update took " + (queryEndTime - startTime)
2041 + " milliseconds");
2042
2043 preparedStatement.close();
2044 preparedStatement = null;
2045 return affectedRows;
2046 }
2047 catch (SQLException e)
2048 {
2049 String dbName = updateValues.getDbName();
2050 final Adapter adapter = Torque.getAdapter(dbName);
2051 if (adapter != null)
2052 {
2053 throw adapter.toTorqueException(e);
2054 }
2055 throw new TorqueException(e);
2056 }
2057 finally
2058 {
2059 if (preparedStatement != null)
2060 {
2061 try
2062 {
2063 preparedStatement.close();
2064 }
2065 catch (SQLException e)
2066 {
2067 log.warn("error closing prepared statement", e);
2068 }
2069 }
2070 }
2071 }
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086 public int doUpdate(
2087 org.apache.torque.criteria.Criteria criteria,
2088 ColumnValues updateValues,
2089 Connection connection)
2090 throws TorqueException
2091 {
2092 Query query = SqlBuilder.buildQuery(criteria);
2093 query.setType(Query.Type.UPDATE);
2094
2095 query.getFromClause().clear();
2096 String fullTableName = SqlBuilder.getFullTableName(
2097 getTableMap().getName(),
2098 criteria.getDbName());
2099 query.getFromClause().add(new FromElement(fullTableName));
2100
2101 List<JdbcTypedValue> replacementObjects
2102 = new ArrayList<JdbcTypedValue>();
2103 for (Map.Entry<Column, JdbcTypedValue> updateValue
2104 : updateValues.entrySet())
2105 {
2106 Column column = updateValue.getKey();
2107 query.getSelectClause().add(column.getColumnName());
2108 replacementObjects.add(updateValue.getValue());
2109 }
2110
2111 PreparedStatement preparedStatement = null;
2112 try
2113 {
2114 preparedStatement = connection.prepareStatement(query.toString());
2115 int position = 1;
2116 for (JdbcTypedValue replacementObject : replacementObjects)
2117 {
2118 Object value = replacementObject.getValue();
2119 if (value != null)
2120 {
2121 preparedStatement.setObject(position, value);
2122 }
2123 else
2124 {
2125 preparedStatement.setNull(
2126 position,
2127 replacementObject.getJdbcType());
2128 }
2129 position++;
2130 }
2131 List<Object> replacements = setPreparedStatementReplacements(
2132 preparedStatement,
2133 query.getPreparedStatementReplacements(),
2134 position - 1);
2135 long startTime = System.currentTimeMillis();
2136 log.debug("Executing update " + query.toString()
2137 + " using update parameters " + replacementObjects
2138 + " and query parameters "
2139 + replacements);
2140
2141 int affectedRows = preparedStatement.executeUpdate();
2142 long queryEndTime = System.currentTimeMillis();
2143 log.trace("update took " + (queryEndTime - startTime)
2144 + " milliseconds");
2145
2146 preparedStatement.close();
2147 preparedStatement = null;
2148 return affectedRows;
2149 }
2150 catch (SQLException e)
2151 {
2152 final String dbName = updateValues.getDbName();
2153 final Adapter adapter = Torque.getAdapter(dbName);
2154 if (adapter != null)
2155 {
2156 throw adapter.toTorqueException(e);
2157 }
2158 throw new TorqueException(e);
2159 }
2160 finally
2161 {
2162 if (preparedStatement != null)
2163 {
2164 try
2165 {
2166 preparedStatement.close();
2167 }
2168 catch (SQLException e)
2169 {
2170 log.warn("error closing prepared statement", e);
2171 }
2172 }
2173 }
2174 }
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186 public int executeStatement(String statementString) throws TorqueException
2187 {
2188 return executeStatement(statementString, Torque.getDefaultDB());
2189 }
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205 public int executeStatement(String statementString, String dbName)
2206 throws TorqueException
2207 {
2208 Connection con = null;
2209 try
2210 {
2211 con = Transaction.begin(dbName);
2212 int rowCount = executeStatement(statementString, dbName,con);
2213 Transaction.commit(con);
2214 con = null;
2215 return rowCount;
2216 }
2217 finally
2218 {
2219 if (con != null)
2220 {
2221 Transaction.safeRollback(con);
2222 }
2223 }
2224 }
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241 public int executeStatement(
2242 String statementString,
2243 String dbName,
2244 Connection con)
2245 throws TorqueException
2246 {
2247 int rowCount = -1;
2248 Statement statement = null;
2249 try
2250 {
2251 statement = con.createStatement();
2252 rowCount = statement.executeUpdate(statementString);
2253 }
2254 catch (SQLException e)
2255 {
2256 throw new TorqueException(e);
2257 }
2258 finally
2259 {
2260 if (statement != null)
2261 {
2262 try
2263 {
2264 statement.close();
2265 }
2266 catch (SQLException e)
2267 {
2268 throw new TorqueException(e);
2269 }
2270 }
2271 }
2272 return rowCount;
2273 }
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287 private List<Object> setPreparedStatementReplacements(
2288 PreparedStatement statement,
2289 List<Object> replacements,
2290 int offset)
2291 throws SQLException
2292 {
2293 List<Object> result = new ArrayList<Object>(replacements.size());
2294 int i = 1 + offset;
2295 for (Object param : replacements)
2296 {
2297 if (param instanceof java.sql.Timestamp)
2298 {
2299 statement.setTimestamp(i, (java.sql.Timestamp) param);
2300 result.add(param);
2301 }
2302 else if (param instanceof java.sql.Date)
2303 {
2304 statement.setDate(i, (java.sql.Date) param);
2305 result.add(param);
2306 }
2307 else if (param instanceof java.util.Date)
2308 {
2309 java.sql.Timestamp sqlDate = new java.sql.Timestamp(
2310 ((java.util.Date) param).getTime());
2311 statement.setTimestamp(i, sqlDate);
2312 result.add(sqlDate);
2313 }
2314 else if (param instanceof NumberKey)
2315 {
2316 BigDecimal bigDecimal = ((NumberKey) param).getBigDecimal();
2317 statement.setBigDecimal(i, bigDecimal);
2318 result.add(bigDecimal);
2319 }
2320 else if (param instanceof Integer)
2321 {
2322 statement.setInt(i, ((Integer) param).intValue());
2323 result.add(param);
2324 }
2325 else if (param instanceof Long)
2326 {
2327 statement.setLong(i, ((Long) param).longValue());
2328 result.add(param);
2329 }
2330 else if (param instanceof BigDecimal)
2331 {
2332 statement.setBigDecimal(i, (BigDecimal) param);
2333 result.add(param);
2334 }
2335 else if (param instanceof Boolean)
2336 {
2337 statement.setBoolean(i, ((Boolean) param).booleanValue());
2338 result.add(param);
2339 }
2340 else
2341 {
2342 statement.setString(i, param.toString());
2343 result.add(param.toString());
2344 }
2345 ++i;
2346 }
2347 return result;
2348 }
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365 @Deprecated
2366 public void correctBooleans(Criteria criteria)
2367 throws TorqueException
2368 {
2369 for (Object criterionObject : criteria.values())
2370 {
2371 Criteria.Criterion criterion = (Criteria.Criterion) criterionObject;
2372 correctBooleans(criteria, criterion);
2373 }
2374 }
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392 @Deprecated
2393 private void correctBooleans(
2394 Criteria criteria,
2395 Criteria.Criterion criterion)
2396 throws TorqueException
2397 {
2398 Column column = criterion.getColumn();
2399 TableMap tableMapFromCriteria = MapHelper.getTableMap(
2400 column,
2401 criteria,
2402 tableMap);
2403
2404 if (tableMapFromCriteria != null)
2405 {
2406 String columnName = column.getColumnName();
2407 ColumnMap columnMap = tableMapFromCriteria.getColumn(columnName);
2408 if (columnMap != null)
2409 {
2410 if ("BOOLEANINT".equals(columnMap.getTorqueType()))
2411 {
2412 replaceBooleanValues(
2413 criterion,
2414 Integer.valueOf(1),
2415 Integer.valueOf(0));
2416 }
2417 else if ("BOOLEANCHAR".equals(columnMap.getTorqueType()))
2418 {
2419 replaceBooleanValues(criterion, "Y", "N");
2420 }
2421 }
2422 }
2423 for (Criteria.Criterion attachedCriterion : criterion.getClauses())
2424 {
2425 correctBooleans(criteria, attachedCriterion);
2426 }
2427 }
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442 public void correctBooleans(
2443 org.apache.torque.criteria.Criteria criteria)
2444 throws TorqueException
2445 {
2446 correctBooleans(
2447 criteria,
2448 criteria.getTopLevelCriterion());
2449 }
2450
2451 private void correctBooleans(
2452 org.apache.torque.criteria.Criteria criteria,
2453 org.apache.torque.criteria.Criterion criterion)
2454 throws TorqueException
2455 {
2456 if (criterion == null)
2457 {
2458 return;
2459 }
2460 if (criterion.isComposite())
2461 {
2462 for (org.apache.torque.criteria.Criterion part
2463 : criterion.getParts())
2464 {
2465 correctBooleans(criteria, part);
2466 }
2467 return;
2468 }
2469
2470 Object possibleColumn = criterion.getLValue();
2471 TableMap tableMapForColumn = MapHelper.getTableMap(
2472 possibleColumn,
2473 criteria,
2474 tableMap);
2475
2476 if (tableMapForColumn == null)
2477 {
2478 return;
2479 }
2480 String columnName = ((Column) possibleColumn).getColumnName();
2481 ColumnMap columnMap = tableMapForColumn.getColumn(columnName);
2482 if (columnMap != null)
2483 {
2484 if ("BOOLEANINT".equals(columnMap.getTorqueType()))
2485 {
2486 replaceBooleanValues(
2487 criterion,
2488 Integer.valueOf(1),
2489 Integer.valueOf(0));
2490 }
2491 else if ("BOOLEANCHAR".equals(columnMap.getTorqueType()))
2492 {
2493 replaceBooleanValues(criterion, "Y", "N");
2494 }
2495 }
2496 }
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509 @Deprecated
2510 private void replaceBooleanValues(
2511 Criteria.Criterion criterion,
2512 Object trueValue,
2513 Object falseValue)
2514 {
2515
2516
2517 Criteria.Criterion[] attachedCriterions
2518 = criterion.getAttachedCriterion();
2519 for (int i = 0; i < attachedCriterions.length; ++i)
2520 {
2521 Object criterionValue
2522 = attachedCriterions[i].getValue();
2523 if (criterionValue instanceof Boolean)
2524 {
2525 Boolean booleanValue = (Boolean) criterionValue;
2526 attachedCriterions[i].setValue(
2527 Boolean.TRUE.equals(booleanValue)
2528 ? trueValue
2529 : falseValue);
2530 }
2531
2532 }
2533 }
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545 private void replaceBooleanValues(
2546 org.apache.torque.criteria.Criterion criterion,
2547 Object trueValue,
2548 Object falseValue)
2549 {
2550 Object rValue = criterion.getRValue();
2551 if (rValue instanceof Boolean)
2552 {
2553 Boolean booleanValue = (Boolean) rValue;
2554 criterion.setRValue(
2555 Boolean.TRUE.equals(booleanValue)
2556 ? trueValue
2557 : falseValue);
2558 }
2559 Object lValue = criterion.getLValue();
2560 if (lValue instanceof Boolean)
2561 {
2562 Boolean booleanValue = (Boolean) lValue;
2563 criterion.setLValue(
2564 Boolean.TRUE.equals(booleanValue)
2565 ? trueValue
2566 : falseValue);
2567 }
2568 }
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582 public void correctBooleans(
2583 ColumnValues columnValues)
2584 throws TorqueException
2585 {
2586 for (Map.Entry<Column, JdbcTypedValue> entry : columnValues.entrySet())
2587 {
2588 String columnName = entry.getKey().getColumnName();
2589 ColumnMap column = getTableMap().getColumn(columnName);
2590 if (column != null)
2591 {
2592 JdbcTypedValue columnValue = entry.getValue();
2593 if ("BOOLEANINT".equals(column.getTorqueType()))
2594 {
2595 if (Boolean.TRUE.equals(columnValue.getValue()))
2596 {
2597 entry.setValue(new JdbcTypedValue(1, Types.INTEGER));
2598 }
2599 else if (Boolean.FALSE.equals(columnValue.getValue()))
2600 {
2601 entry.setValue(new JdbcTypedValue(0, Types.INTEGER));
2602 }
2603 else if (columnValue.getValue() == null)
2604 {
2605 entry.setValue(new JdbcTypedValue(null, Types.INTEGER));
2606 }
2607 }
2608 else if ("BOOLEANCHAR".equals(column.getTorqueType()))
2609 {
2610 if (Boolean.TRUE.equals(columnValue.getValue()))
2611 {
2612 entry.setValue(new JdbcTypedValue("Y", Types.CHAR));
2613 }
2614 else if (Boolean.FALSE.equals(columnValue.getValue()))
2615 {
2616 entry.setValue(new JdbcTypedValue("N", Types.CHAR));
2617 }
2618 else if (columnValue.getValue() == null)
2619 {
2620 entry.setValue(new JdbcTypedValue(null, Types.CHAR));
2621 }
2622 }
2623 }
2624 }
2625 }
2626
2627
2628
2629
2630
2631
2632
2633 protected void setDbName(org.apache.torque.criteria.Criteria crit)
2634 throws TorqueException
2635 {
2636 if (crit.getDbName() == null)
2637 {
2638 crit.setDbName(getDatabaseName());
2639 }
2640 }
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652 @Deprecated
2653 protected void setDbName(Criteria crit) throws TorqueException
2654 {
2655 if (crit.getDbName() == null)
2656 {
2657 crit.setDbName(getDatabaseName());
2658 }
2659 }
2660 }