1 64 package com.jcorporate.expresso.core.dataobjects.jdbc; 65 66 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 67 import com.jcorporate.expresso.core.dataobjects.BaseDataObject; 68 import com.jcorporate.expresso.core.dataobjects.DataException; 69 import com.jcorporate.expresso.core.dataobjects.DataExecutorInterface; 70 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData; 71 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 72 import com.jcorporate.expresso.core.dataobjects.DataQueryInterface; 73 import com.jcorporate.expresso.core.db.DBConnection; 74 import com.jcorporate.expresso.core.db.DBConnectionPool; 75 import com.jcorporate.expresso.core.db.DBException; 76 import com.jcorporate.expresso.core.db.config.JDBCConfig; 77 import com.jcorporate.expresso.core.dbobj.DBField; 78 import com.jcorporate.expresso.core.dbobj.DBObjectDef; 79 import com.jcorporate.expresso.core.misc.Base64; 80 import com.jcorporate.expresso.core.misc.ConfigJdbc; 81 import com.jcorporate.expresso.core.misc.ConfigManager; 82 import com.jcorporate.expresso.core.misc.ConfigurationException; 83 import com.jcorporate.expresso.core.misc.DateTime; 84 import com.jcorporate.expresso.core.misc.StringUtil; 85 import com.jcorporate.expresso.core.registry.RequestRegistry; 86 import com.jcorporate.expresso.core.security.CryptoManager; 87 import com.jcorporate.expresso.kernel.exception.ChainedException; 88 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 89 import org.apache.log4j.Logger; 90 91 import java.io.InputStream ; 92 import java.sql.CallableStatement ; 93 import java.util.ArrayList ; 94 import java.util.HashMap ; 95 import java.util.Iterator ; 96 97 105 106 abstract public class JDBCDataObject extends BaseDataObject { 107 108 private static transient final Logger log = Logger.getLogger(JDBCDataObject.class); 109 110 public static int LONGBINARY_READ_DEFAULT_SIZE = 262144; 111 112 116 transient private DBConnectionPool myPool = null; 117 118 119 124 private String mappedDataContext = null; 125 126 130 protected String customWhereClause = null; 131 132 136 protected boolean appendCustomWhere = false; 137 138 143 protected String dbKey = null; 144 145 146 149 protected ArrayList recordSet = null; 150 151 152 158 transient protected String myClassName = getClass().getName(); 159 160 163 protected HashMap distinctFields = null; 164 165 166 169 public HashMap retrieveFields = null; 170 171 180 volatile transient protected static ConcurrentReaderHashMap sMetadataMap 181 = new ConcurrentReaderHashMap(); 182 183 187 protected boolean anyFieldsToRetrieve = false; 188 189 190 194 transient protected DBConnection localConnection = null; 195 196 197 203 transient protected int offsetRecord = 0; 204 205 208 transient protected int maxRecords = 0; 209 210 214 transient protected ArrayList sortKeys = null; 215 216 220 transient protected ArrayList myUpdates = null; 221 222 223 227 public boolean anyFieldsDistinct = false; 228 229 235 protected boolean caseSensitiveQuery = true; 236 237 238 241 transient static private JDBCUtil sJdbcUtil = JDBCUtil.getInstance(); 242 243 244 250 transient private static DataExecutorInterface sDataExecutor = new JDBCExecutor(); 251 252 258 transient private static DataQueryInterface sDataQueryObject = new JDBCQuery(); 259 260 261 264 public JDBCDataObject() { 265 } 266 267 268 277 public String getMappedDataContext() { 278 279 if (mappedDataContext == null) { 280 this.setDataContext(RequestRegistry.getDataContext()); 281 if (log.isDebugEnabled()) { 282 log.debug("Setting Database Context from Request Registry "); 283 } 284 } 285 return mappedDataContext; 286 } 287 288 289 295 public DBConnectionPool getConnectionPool() throws DataException { 296 try { 297 if (myPool == null) { 298 if (this.getMappedDataContext() == null) { 299 this.setDataContext("default"); 300 } 301 String myDataContext = this.getMappedDataContext(); 302 myPool = DBConnectionPool.getInstance(myDataContext); 303 } 304 } catch (DBException ex) { 305 throw new DataException("Error getting Connection Pool", ex); 306 } 307 308 return myPool; 309 } 310 311 317 protected void setDBConnectionPool(DBConnectionPool newPool) { 318 myPool = newPool; 319 } 320 321 333 public String getSerialForm(DataFieldMetaData theField) 334 throws DataException { 335 try { 336 if (theField.isEncrypted()) { 337 return Base64.encodeNoPadding(CryptoManager.getInstance() 338 .getStringEncryption().encryptString(this 339 .getDataField(theField.getName()).asString())); 340 } else { 341 String result = getDataField(theField.getName()).asString(); 342 if (theField.isBooleanType()) { 343 try { 345 boolean nativeBoolean = ConfigManager.getContext( 346 getMappedDataContext()).getJdbc().isNativeBool(); 347 if (!nativeBoolean) { 348 if ("true".equalsIgnoreCase(result)) { 350 result = "Y"; 351 } 352 if ("false".equalsIgnoreCase(result)) { 353 result = "N"; 354 } 355 } 356 } catch (ConfigurationException ce) { 357 throw new DataException(ce); 358 } 359 } 360 return result; 361 } 362 } catch (ChainedException e) { 363 throw new DataException("Error getting Serialized Form for field: " 364 + theField.getName(), e); 365 } 366 } 367 368 369 382 public synchronized void setDBName(String newOther) 383 throws DBException { 384 385 386 if (StringUtil.notNull(newOther).equals("")) { 387 newOther = "default"; 388 } 389 390 391 if (log.isDebugEnabled()) { 392 log.debug("Object '" + myClassName + "' requesting db '" + 393 newOther + "'"); 394 } 395 String mappedContext = newOther; 396 dbKey = newOther; 397 398 if (!"com.jcorporate.expresso.services.dbobj.DBOtherMap".equals(myClassName)) { 399 String otherdbname = StringUtil.notNull(ConfigManager.getOtherDbLocation(newOther, myClassName)); 400 401 if (!otherdbname.equals("")) { 402 mappedContext = otherdbname; 403 404 if (log.isDebugEnabled()) { 405 log.debug("Object '" + myClassName + 406 "' mapped to database '" + dbKey + "'"); 407 } 408 } 409 } 410 411 myPool = null; 413 414 this.setMappedDataContext(mappedContext); 415 } 416 417 418 423 public final DataObjectMetaData getMetaData() { 424 return getDef(); 425 } 426 427 433 public final JDBCObjectMetaData getJDBCMetaData() { 434 return (JDBCObjectMetaData) getMetaData(); 435 } 436 437 444 protected final DBObjectDef getDef() { 445 DBObjectDef myDef = (DBObjectDef) sMetadataMap.get(this.myClassName); 446 447 return myDef; 448 } 449 450 451 459 protected DBObjectDef constructNewMetaData() throws DBException { 460 return new DBObjectDef(); 461 } 462 463 464 469 public boolean checkZeroUpdate() { 470 return getDef().checkZeroUpdate(); 471 } 472 473 474 480 protected void setOriginalDBName(String newOriginalName) { 481 mappedDataContext = newOriginalName; 482 } 483 484 protected void setMappedDataContext(String newMappedName) { 485 mappedDataContext = newMappedName; 486 } 487 488 505 public void setCaseSensitiveQuery(boolean caseSensitiveQuery) { 506 507 this.caseSensitiveQuery = caseSensitiveQuery; 508 } 509 510 518 public String selectFieldString(String fieldName) 519 throws DBException { 520 DataFieldMetaData oneField = getFieldMetaData(fieldName); 521 522 try { 523 JDBCConfig myConfig = null; 524 526 if (oneField.isDateOnlyType()) { 527 myConfig = ConfigManager.getJdbcRequired(getDataContext()); 528 529 if (!StringUtil.notNull(myConfig.getDateSelectFunction()).equals("")) { 530 return StringUtil.replace(myConfig.getDateSelectFunction(), "%s", 531 fieldName); 532 } 533 } 534 if (oneField.isTimeType()) { 535 myConfig = ConfigManager.getJdbcRequired(getDataContext()); 536 537 if (!StringUtil.notNull(myConfig.getTimeSelectFunction()).equals("")) { 538 return StringUtil.replace(myConfig.getTimeSelectFunction(), "%s", 539 fieldName); 540 } 541 } 542 if (oneField.isDateTimeType()) { 543 myConfig = ConfigManager.getJdbcRequired(getDataContext()); 544 545 if (!StringUtil.notNull(myConfig.getDateTimeSelectFunction()).equals("")) { 546 return StringUtil.replace(myConfig.getDateTimeSelectFunction(), "%s", 547 fieldName); 548 } 549 } 550 } catch (ConfigurationException ce) { 551 throw new DBException(ce); 552 } 553 554 return fieldName; 555 } 556 557 558 568 public String quoteIfNeeded(String fieldName, String rangeString) 569 throws DBException { 570 DataFieldMetaData oneField = getFieldMetaData(fieldName); 571 if (oneField == null) { 572 throw new DBException("(" + this.myClassName + 573 ") No such field as " + fieldName); 574 } 575 576 boolean noTrim = false; 577 if (!oneField.isMasked() && !isGlobalMasked()) { 578 try { 579 noTrim = ConfigManager.getJdbcRequired(this.getMappedDataContext()).isStringNotTrim(); 580 } catch (ConfigurationException ce) { 581 throw new DataException(ce); 582 } 583 } 584 585 String fieldValue = getSerialForm(oneField); 586 587 588 if (fieldValue == null) { 589 return null; 590 } 591 592 if (rangeString != null) { 593 fieldValue = fieldValue.substring(rangeString.length()); 594 } 595 596 597 if (fieldValue == null) { 598 return null; 599 } 600 601 if ((oneField.isBooleanType() || "bit".equalsIgnoreCase(oneField.getTypeString())) 602 && fieldValue.length() == 0) { 603 return null; 604 } 605 606 if (oneField.isNumericType()) { 607 if (fieldValue.length() == 0) { 608 return null; 609 } 610 611 return fieldValue.trim(); 612 } 613 614 615 if (oneField.isQuotedTextType()) { 616 if (rangeString != null) { 617 return fieldValue; 618 } 619 620 if (fieldValue.length() == 0) { 624 return "''"; 625 } 626 627 FastStringBuffer returnValue = FastStringBuffer.getInstance(); 628 String returnString = null; 629 try { 630 String value = ""; 631 if (noTrim) { 632 value = fieldValue; 633 } else { 634 value = fieldValue.trim(); 635 } 636 returnValue.append("\'"); 637 returnValue.append(this.getConnectionPool().getEscapeHandler().escapeString(value)); 639 returnValue.append("\'"); 640 returnString = returnValue.toString(); 641 } finally { 642 returnValue.release(); 643 returnValue = null; 644 } 645 return returnString; 646 } 647 648 if (oneField.isDateType()) { 649 if (rangeString != null) { 650 return fieldValue; 651 } 652 FastStringBuffer returnValue = FastStringBuffer.getInstance(); 653 String returnString = null; 654 try { 655 returnValue.append("\'"); 656 returnValue.append(fieldValue); 657 returnValue.append("\'"); 658 returnString = returnValue.toString(); 659 } finally { 660 returnValue.release(); 661 returnValue = null; 662 } 663 return returnString; 664 } 665 666 if (oneField.isBooleanType()) { 671 try { 672 boolean nativeBoolean = ConfigManager.getContext(this.getDataContext()).getJdbc().isNativeBool(); 673 674 if (!nativeBoolean) { 675 FastStringBuffer returnValue = FastStringBuffer.getInstance(); 676 String returnString = null; 677 try { 678 returnValue.append("\'"); 679 returnValue.append(fieldValue.trim()); 680 returnValue.append("\'"); 681 returnString = returnValue.toString(); 682 } finally { 683 returnValue.release(); 684 returnValue = null; 685 } 686 return returnString; 687 } 688 } catch (ConfigurationException ce) { 689 throw new DBException(ce); 690 } 691 } 692 693 if (noTrim) { 694 return fieldValue; 695 } else { 696 return fieldValue.trim(); 697 } 698 } 699 700 709 public synchronized void setConnection(DBConnection newConnection) 710 throws DBException { 711 setConnection(newConnection, newConnection.getDataContext()); 712 } 713 714 731 public synchronized void setConnection(DBConnection newConnection, 732 String setupTablesContext) throws DBException { 733 localConnection = newConnection; 734 this.setDataContext(setupTablesContext); 735 } 736 737 738 753 public DBConnection createAndExecuteSearch(java.util.ArrayList retrievedFieldList) 754 throws DBException { 755 boolean needComma = false; 756 757 if (recordSet == null) { 758 recordSet = new ArrayList (); 759 } else { 760 recordSet.clear(); 761 } 762 763 myUpdates = null; 764 765 DBConnection myConnection = null; 766 JDBCObjectMetaData myMetadata = this.getJDBCMetaData(); 767 FastStringBuffer myStatement = FastStringBuffer.getInstance(); 768 try { 769 if (localConnection != null) { 770 myConnection = localConnection; 771 } else { 772 myConnection = this.getConnectionPool().getConnection(this.myClassName); 773 } 774 775 myStatement.append("SELECT "); 776 777 if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_SELECT && 778 (offsetRecord > 0 || maxRecords > 0)) { 779 780 String limitStub = makeLimitationStub(myConnection); 782 783 myStatement.append(" "); 784 myStatement.append(limitStub); 785 myStatement.append(" "); 786 } 787 788 if (anyFieldsDistinct) { 789 String oneFieldName = null; 790 myStatement.append(" "); 791 myStatement.append(getConnectionPool().getDistinctRowsetKeyword()); 792 myStatement.append(" "); 793 794 for (Iterator i = getDistinctFieldArrayList().iterator(); 795 i.hasNext();) { 796 oneFieldName = (String ) i.next(); 797 retrievedFieldList.add(oneFieldName); 798 799 if (needComma) { 800 myStatement.append(", "); 801 } 802 803 myStatement.append(selectFieldString(oneFieldName)); 804 needComma = true; 805 } 806 } else if (anyFieldsToRetrieve) { 807 String oneFieldName = null; 808 809 for (Iterator i = getFieldsToRetrieveIterator(); i.hasNext();) { 810 oneFieldName = (String ) i.next(); 811 812 if (needComma) { 813 myStatement.append(", "); 814 } 815 816 retrievedFieldList.add(oneFieldName); 817 myStatement.append(selectFieldString(oneFieldName)); 818 needComma = true; 819 } 820 } else { 821 for (Iterator i = myMetadata.getAllFieldsMap().values().iterator(); 822 i.hasNext();) { 823 DBField oneField = (DBField) i.next(); 824 825 if (!oneField.isVirtual() && !oneField.isBinaryObjectType()) { 826 if (needComma) { 827 myStatement.append(", "); 828 } 829 830 retrievedFieldList.add(oneField.getName()); 831 myStatement.append(selectFieldString(oneField.getName())); 832 needComma = true; 833 } 834 835 } 836 837 } 838 839 840 myStatement.append(" FROM "); 841 myStatement.append(myMetadata.getTargetSQLTable(this.getDataContext())); 842 if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_TABLE && 843 (offsetRecord > 0 || maxRecords > 0)) { 844 845 String limitStub = makeLimitationStub(myConnection); 847 myStatement.append(" "); 848 myStatement.append(limitStub); 849 myStatement.append(" "); 850 } 851 852 String whereClause; 853 854 if (customWhereClause != null) { 855 if (appendCustomWhere) { 856 whereClause = buildWhereClause(true) + customWhereClause; 857 appendCustomWhere = false; 858 } else { 859 whereClause = customWhereClause; 860 } 861 myStatement.append(whereClause); 862 customWhereClause = null; 863 } else { 864 whereClause = buildWhereClause(true); 865 myStatement.append(whereClause); 866 } 867 if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_WHERE && 868 (offsetRecord > 0 || maxRecords > 0)) { 869 870 String limitStub = makeLimitationStub(myConnection); 872 873 if (whereClause.length() > 0) { 874 myStatement.append(" AND"); 875 } 876 877 myStatement.append(" "); 878 myStatement.append(limitStub); 879 myStatement.append(" "); 880 } 881 882 if (sortKeys != null && sortKeys.size() > 0) { 883 myStatement.append(" ORDER BY "); 884 885 boolean needComma2 = false; 886 887 for (Iterator i = sortKeys.iterator(); i.hasNext();) { 888 if (needComma2) { 889 myStatement.append(", "); 890 } 891 892 myStatement.append((String ) i.next()); 893 needComma2 = true; 894 } 895 if (myConnection.getLimitationPosition() == DBConnection.LIMITATION_AFTER_ORDER_BY && 896 (offsetRecord > 0 || maxRecords > 0)) { 897 myStatement.append(" "); 898 myStatement.append(makeLimitationStub(myConnection)); 899 } 900 } 901 902 myConnection.execute(myStatement.toString()); 903 return myConnection; 904 } catch (DBException ex) { 905 if (myConnection != null && localConnection == null) { 906 myConnection.release(); 907 } 908 log.error("Error building and executing search statement", ex); 909 throw ex; 910 } finally { 911 myStatement.release(); 912 } 913 } 914 915 927 public void loadFromConnection(JDBCDataObject myObj, 928 DBConnection myConnection, ArrayList retrievedFieldList) throws DBException { 929 int i = 1; 930 String oneFieldName = null; 931 Object tmpData = null; 932 933 for (Iterator it = retrievedFieldList.iterator(); 934 it.hasNext();) { 935 oneFieldName = (String ) it.next(); 936 DataFieldMetaData oneDBField = getFieldMetaData(oneFieldName); 937 938 try { 939 if (oneDBField.isDateType()) { 942 tmpData = getCustomStringFieldValue(myConnection, oneDBField.getName()); 943 } else { 944 if (!oneDBField.isLongBinaryType() && !oneDBField.isLongCharacterType()) { 945 if (myConnection.isStringNotTrim()) { 946 tmpData = myConnection.getStringNoTrim(i); 947 } else { 948 tmpData = myConnection.getString(i); 949 } 950 } else { 951 if (oneDBField.isLongBinaryType()) { 952 tmpData = null; 953 InputStream is = myConnection.getBinaryStream(i); 954 if (is != null) { 955 byte[] bstr = new byte[LONGBINARY_READ_DEFAULT_SIZE]; 956 int j = is.read(bstr); 957 if (j > 0) { 958 byte[] content = new byte[j]; 959 System.arraycopy(bstr, 0, content, 0, j); 960 tmpData = content; 961 } 962 } 963 } else { 964 tmpData = myConnection.getStringNoTrim(i); 965 } 966 } 967 968 } 969 } catch (DBException de) { 970 throw new DBException("Error retrieving field '" + 971 oneFieldName, de); 972 } catch (Exception de) { 973 throw new DBException("Not DBException Error retrieving field '" + 974 oneFieldName, de); 975 } 976 977 i++; 978 myObj.set(oneFieldName, tmpData); 979 } 980 981 982 myObj.setDataContext(this.getDataContext()); 983 myObj.setStatus(BaseDataObject.STATUS_CURRENT); 984 } 985 986 1023 protected String makeLimitationStub(DBConnection theConnection) { 1024 String limit = theConnection.getLimitationSyntax(); 1025 int offset = this.getOffsetRecord(); 1026 int maxrec = this.getMaxRecords(); 1027 int endrec = offset + maxrec - 1; 1028 limit = StringUtil.replace(limit, "%offset%", Integer.toString(offset)); 1029 limit = StringUtil.replace(limit, "%maxrecords%", 1030 Integer.toString(maxrec)); 1031 1032 limit = StringUtil.replace(limit, "%endrecord%", 1034 Integer.toString(endrec)); 1035 1036 return limit; 1037 } 1038 1039 1049 public ArrayList getDistinctFieldArrayList() 1050 throws DBException { 1051 ArrayList arl = new ArrayList (); 1052 1053 if (distinctFields == null) { 1054 return arl; 1055 } 1056 for (Iterator i = this.getMetaData().getFieldListArray().iterator(); i.hasNext();) { 1057 String fieldName = (String ) i.next(); 1058 1059 if (distinctFields.containsKey(fieldName)) { 1060 arl.add(fieldName); 1061 } 1062 } 1063 1064 return arl; 1065 } 1066 1067 1076 public Iterator getFieldsToRetrieveIterator() 1077 throws DBException { 1078 1079 if (retrieveFields == null) { 1081 return new HashMap().keySet().iterator(); 1082 } 1083 1084 return retrieveFields.keySet().iterator(); 1085 } 1086 1087 1096 public String buildWhereClause(boolean useAllFields) 1097 throws DBException { 1098 1099 return sJdbcUtil.buildWhereClause(this, useAllFields); 1100 } 1101 1102 1103 1114 protected FastStringBuffer buildWhereClauseBuffer(boolean useAllFields, 1115 FastStringBuffer allocatedBuffer) 1116 throws DBException { 1117 1118 try { 1119 return sJdbcUtil.buildWhereClauseBuffer(this, useAllFields, allocatedBuffer); 1120 } catch (DataException ex) { 1121 throw new DBException(ex.getMessage()); 1122 } 1123 } 1124 1125 1130 protected JDBCUtil getJDBCUtil() { 1131 return sJdbcUtil; 1132 } 1133 1134 1141 public DataExecutorInterface getExecutor() { 1142 return sDataExecutor; 1143 } 1144 1145 1152 public DataQueryInterface getQueryInterface() { 1153 return sDataQueryObject; 1154 } 1155 1156 1165 public String getCustomStringFieldValue(DBConnection connection, String oneFieldName) 1166 throws DBException { 1167 ConfigJdbc myConfig = null; 1168 String oneFieldValue = null; 1169 DataFieldMetaData fieldMetadata = this.getFieldMetaData(oneFieldName); 1170 try { 1171 myConfig = ConfigManager.getJdbcRequired(getDataContext()); 1172 } catch (ConfigurationException ce) { 1173 throw new DBException(ce); 1174 } 1175 if (fieldMetadata.isDateTimeType()) { 1176 if (StringUtil.notNull(myConfig.getDateTimeSelectFormat()).length() > 0) { 1177 oneFieldValue = DateTime.getDateTimeForDB(connection.getTimestamp(oneFieldName), dbKey); 1178 } else { 1179 oneFieldValue = connection.getString(oneFieldName); 1180 } 1181 } 1182 if (fieldMetadata.isTimeType()) { 1183 if (!StringUtil.notNull(myConfig.getTimeSelectFormat()).equals("")) { 1184 oneFieldValue = DateTime.getTimeForDB(connection.getTime(oneFieldName), dbKey); 1185 } else { 1186 oneFieldValue = connection.getString(oneFieldName); 1187 } 1188 } 1189 if (fieldMetadata.isDateOnlyType()) { 1190 if (!StringUtil.notNull(myConfig.getDateSelectFormat()).equals("")) { 1191 oneFieldValue = DateTime.getDateForDB(connection.getDate(oneFieldName), dbKey); 1192 } else { 1193 oneFieldValue = connection.getString(oneFieldName); 1194 } 1195 } 1196 return oneFieldValue; 1197 } 1198 1199 1200 1207 public DBConnection getLocalConnection() { 1208 return localConnection; 1209 } 1210 1211 1212 1222 public DBConnection createAndRunStoreProcedure(java.util.ArrayList retrievedFieldList) 1223 throws DBException { 1224 if (recordSet == null) { 1225 recordSet = new ArrayList (); 1226 } else { 1227 recordSet.clear(); 1228 } 1229 1230 myUpdates = null; 1231 1232 DBConnection myConnection = null; 1233 FastStringBuffer myStatement = FastStringBuffer.getInstance(); 1234 try { 1235 if (localConnection != null) { 1236 myConnection = localConnection; 1237 } else { 1238 myConnection = this.getConnectionPool().getConnection(this.myClassName); 1239 } 1240 1241 int nbParams = this.getDef().getFieldListArray().size(); 1242 if (this.getDef().isReturningValue()) { 1243 myStatement.append("{? = call "); 1244 nbParams--; 1245 } else { 1246 myStatement.append("{call "); 1247 } 1248 myStatement.append(this.getJDBCMetaData().getTargetTable()); 1249 1250 if (nbParams > 0) { 1251 myStatement.append("(?"); 1252 for (int i = 1; i < nbParams; i++) { 1253 myStatement.append(", ?"); 1254 } 1255 myStatement.append(")"); 1256 } 1257 myStatement.append("}"); 1258 1259 CallableStatement theStroreProcedureStatement = myConnection.createCallableStatement( 1260 myStatement.toString()); 1261 sJdbcUtil.buildStoreProcedureCallableStatement(this, theStroreProcedureStatement); 1262 myConnection.executeProcedure(); 1263 1264 return myConnection; 1265 } catch (DBException ex) { 1266 if (myConnection != null && localConnection == null) { 1267 myConnection.release(); 1268 } 1269 log.error("Error building and running store procedure statement", ex); 1270 throw ex; 1271 } finally { 1272 myStatement.release(); 1273 } 1274 } 1275 1276 1285 protected synchronized void addInParam(String inFieldName) 1286 throws DBException { 1287 getDef().addInParam(inFieldName); 1288 } 1289 1290 1299 protected synchronized void addOutParam(String outFieldName) 1300 throws DBException { 1301 getDef().addOutParam(outFieldName); 1302 } 1303 1304 1310 public synchronized void setTargetStoreProcedure(String theStoreProcedure) 1311 throws DBException { 1312 getDef().setTargetStoreProcedure(theStoreProcedure); 1313 } 1314 1315 1320 public void runStoredProcedure() 1321 throws DBException { 1322 1323 this.getExecutor().runStoreProcedure(this); 1324 1325 if (getDef().isLoggingEnabled()) { 1326 myUpdates = null; 1327 } 1328 1329 this.setStatus(BaseDataObject.STATUS_CURRENT); 1330 } 1331 1332 1333 1341 protected synchronized ArrayList runStoredProcedureAndRetrieveList() 1342 throws DBException { 1343 1344 ArrayList retrievedFieldList = new ArrayList (); 1345 1346 DBConnection myConnection = null; 1347 try { 1348 myConnection = this.createAndRunStoreProcedure(retrievedFieldList); 1349 1350 int recordCount = 0; 1351 int retrieveCount = 0; 1352 1353 while (myConnection.next()) { 1354 recordCount++; 1355 retrieveCount++; 1356 1357 if (retrieveCount < offsetRecord && offsetRecord > 0 && 1360 myConnection.getLimitationPosition() == DBConnection.LIMITATION_DISABLED) { 1361 continue; 1362 } else if (retrieveCount == offsetRecord && offsetRecord > 0 && 1363 myConnection.getLimitationPosition() == DBConnection.LIMITATION_DISABLED) { 1364 recordCount = 0; continue; } 1367 if ((recordCount > maxRecords) && (maxRecords > 0)) { 1368 this.setAttribute("More Records", "Y"); 1369 break; 1370 } 1371 1372 JDBCDataObject myObj = (JDBCDataObject) this.getClass().newInstance(); this.loadFromConnection(myObj, myConnection, retrievedFieldList); 1375 recordSet.add(myObj); 1376 } 1377 } catch (DBException de) { 1378 log.error("Error performing runStoreProcedureAndRetrieveList", de); 1379 throw new DBException(de); 1380 } catch (Throwable t) { 1381 log.error("Error performing runStoreProcedureAndRetrieveList", t); 1382 throw new DBException("Error performing runStoreProcedureAndRetrieveList", t); 1383 } finally { 1384 if (localConnection == null) { 1385 if (myConnection != null) { 1386 this.getConnectionPool().release(myConnection); 1387 } 1388 } 1389 } 1390 1391 return recordSet; 1392 } 1393} 1394 | Popular Tags |