0001: package org.julp;
0002:
0003: import java.lang.reflect.InvocationTargetException;
0004: import java.lang.reflect.Method;
0005: import javax.naming.*;
0006: import javax.sql.*;
0007: import java.sql.*;
0008: import java.util.*;
0009:
0010: public class DBServices extends Object implements java.io.Serializable,
0011: Cloneable {
0012:
0013: public static final char NONE = 'N';
0014: public static final char ROLLBACK = 'R';
0015: public static final char COMMIT = 'C';
0016:
0017: protected boolean connectionClosed = false;
0018: protected transient Context context;
0019: protected transient DataSource dataSource;
0020: protected transient Connection connection;
0021: protected transient Statement statement;
0022: protected transient PreparedStatement preparedStatement;
0023: protected transient CallableStatement callStatement = null;
0024: protected transient ResultSet resultSet;
0025:
0026: protected Properties connectionProperties = null;
0027: protected String dataSourceName = null;
0028: protected String cursorName = null;
0029: protected int maxFieldSize = 0;
0030: protected int maxRows = 0;
0031: protected int queryTimeout = 0;
0032: protected int fetchDirection = 0;
0033: protected int fetchSize = 0;
0034: //TYPE_FORWARD_ONLY = 1003, TYPE_SCROLL_INSENSITIVE = 1004, TYPE_SCROLL_SENSITIVE = 1005
0035: protected int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
0036: //CONCUR_READ_ONLY, CONCUR_UPDATABLE
0037: protected int concurrency = ResultSet.CONCUR_READ_ONLY;
0038: protected boolean readOnly = false;
0039: protected boolean closeStatementAfterExecute = true;
0040: protected boolean escapeProcessing = true;
0041: protected boolean tranInProccess = false;
0042: protected boolean resetDataSource = false;
0043: protected boolean debug = false;
0044: protected boolean proccessWarnings = false;
0045: protected boolean proccessStoredProc = false;
0046: protected boolean closeResultSet = true;
0047: protected char proccessTransactionOnCloseConnection = NONE;
0048: protected int maxRowsToProccesInTransaction = 0; // How many rows to update/insert/delete in transaction
0049: protected int rowsAffectedInTransaction = 0;
0050: protected String connectionId = "<none>";
0051: protected String placeHolder = null;
0052: protected int maxNumberOfParams = -1;
0053: protected transient List resultSets = null;
0054: protected List warnings = null;
0055: //indicators how to handle ResultSet(s) from Stored Procedures and Functions
0056: public static final int IGNORE_RESULT = 0; // don't proccess ResultSet(s)
0057: public static final int RESULT_AS_DATA_HOLDERS_LIST = 1;
0058: public static final int RESULT_AS_VECTOR = 2; // Vector of Vectors
0059: public static final int RESULT_AS_MAP = 3; // Map of Lists
0060: public static final int RESULT_AS_ROWSET = 4; // CachedRowSetImpl
0061:
0062: protected boolean cacheStatements = false;
0063: protected int maxStatementCacheSize = 20; // must be set before statementsCache init, default: 20
0064: protected List statementsId; // cached statements id (sql String)
0065: protected List statementsCache; // cached statements
0066: protected List statementsCacheCounter; // cached statements usage counter
0067: protected String cachedRowSetClassName = "com.sun.rowset.CachedRowSetImpl"; // set another implementation...
0068: // if EJBContext is not null it is indication that this is EJB Contaner managed transaction and
0069: // instead of connection.rollback() there will be EJBContext.setRollbackOnly
0070: // and no connection.setAutoCommit(false), no connection.commit().
0071: protected Object EJBContext = null;
0072:
0073: public DBServices() {
0074: if (Boolean.getBoolean("debug-julp")
0075: || Boolean.getBoolean("debug-" + getClass().getName())) {
0076: debug = true;
0077: }
0078: }
0079:
0080: /** Getter for property context.
0081: * @return Value of property context.
0082: *
0083: */
0084: public javax.naming.Context getContext() {
0085: if (context == null) {
0086: try {
0087: context = new InitialContext();
0088: if (debug)
0089: System.out.println("julp ============= "
0090: + new java.util.Date() + " "
0091: + this .getClass() + "::" + this
0092: + "::getContext()::context: " + context
0093: + " \n");
0094: } catch (NamingException ne) {
0095: throw new RuntimeException(ne);
0096: }
0097: }
0098: return context;
0099: }
0100:
0101: /** Setter for property context.
0102: * @param context New value of property context.
0103: *
0104: */
0105: public void setContext(javax.naming.Context context) {
0106: this .context = context;
0107: }
0108:
0109: /** Getter for property connection.
0110: * @return Value of property connection.
0111: *
0112: */
0113: public java.sql.Connection getConnection() throws SQLException {
0114: if (debug)
0115: System.out.println("julp ============= "
0116: + new java.util.Date() + " " + this .getClass()
0117: + "::" + this + "::getConnection()::connection 1: "
0118: + connection + "::isTranInProccess(): "
0119: + tranInProccess + " \n");
0120: if (isTranInProccess()) {
0121: return this .connection;
0122: }
0123: if (connection == null || connection.isClosed()) {
0124: //if (connection == null || this.isConnectionClosed()){ //Sybase bug workaround??
0125: dataSource = this .getDataSource();
0126: if (debug)
0127: System.out.println("julp ============= "
0128: + new java.util.Date() + " " + this .getClass()
0129: + "::" + this
0130: + "::getConnection()::dataSource 1: "
0131: + dataSource + " \n");
0132: connection = dataSource.getConnection();
0133: //connection.rollback(); //Sybase bug workaround??
0134: if (debug)
0135: System.out.println("julp ============= "
0136: + new java.util.Date() + " " + this .getClass()
0137: + "::" + this
0138: + "::getConnection()::connection 2: "
0139: + connection + " \n");
0140: }
0141: if (isTranInProccess()) {
0142: if (debug)
0143: System.out.println("julp ============= "
0144: + new java.util.Date() + " " + this .getClass()
0145: + "::" + this
0146: + "::getConnection()::isTranInProccess(): "
0147: + isTranInProccess() + "::connectionId: "
0148: + connectionId + "\n");
0149: if (!this .connectionId.equals(connection.toString())) {
0150: throw new SQLException(
0151: "Connection was closed while in transaction");
0152: }
0153: }
0154: if (isProccessWarnings()) {
0155: populateWarnings(connection.getWarnings());
0156: connection.clearWarnings();
0157: }
0158: return connection;
0159: }
0160:
0161: /** Setter for property connection.
0162: * @param connection New value of property connection.
0163: *
0164: */
0165: public void setConnection(java.sql.Connection connection) {
0166: this .connection = connection;
0167: }
0168:
0169: /** Getter for property dataSource.
0170: * @return Value of property dataSource.
0171: *
0172: */
0173: public javax.sql.DataSource getDataSource() throws SQLException {
0174: try {
0175: if (dataSource == null || isResetDataSource()) {
0176: if (dataSourceName == null) {
0177: throw new SQLException("DataSource name is missing");
0178: } else if (dataSourceName.indexOf("/") == -1) {
0179: dataSource = (DataSource) this .getContext().lookup(
0180: "java:comp/env/jdbc/" + dataSourceName);
0181: } else {
0182: dataSource = (DataSource) this .getContext().lookup(
0183: dataSourceName);
0184: }
0185: }
0186: } catch (NamingException ne) {
0187: throw new RuntimeException(ne.getMessage());
0188: }
0189: if (debug)
0190: System.out.println("julp ============= "
0191: + new java.util.Date() + " " + this .getClass()
0192: + "::" + this + "::getDataSource()::dataSource: "
0193: + dataSource + " \n");
0194: return dataSource;
0195: }
0196:
0197: /** Setter for property dataSource.
0198: * @param dataSource New value of property dataSource.
0199: *
0200: */
0201: public void setDataSource(javax.sql.DataSource dataSource) {
0202: this .dataSource = dataSource;
0203: }
0204:
0205: protected void prepareStatement(String sql) throws SQLException {
0206: this .preparedStatement = this .getConnection().prepareStatement(
0207: sql, getResultSetType(), getConcurrency());
0208: if (debug)
0209: System.out.println("julp ============= "
0210: + new java.util.Date() + " " + this .getClass()
0211: + "::" + this + "::prepareStatement()::sql: \n"
0212: + sql + " \n");
0213: this .preparedStatement.setEscapeProcessing(this
0214: .getEscapeProcessing());
0215: this .preparedStatement.setFetchSize(this .getFetchSize());
0216: this .preparedStatement.setMaxRows(this .getMaxRows());
0217: this .preparedStatement.setMaxFieldSize(this .getMaxFieldSize());
0218: this .preparedStatement.setQueryTimeout(this .getQueryTimeout());
0219: if (this .getCursorName() != null) {
0220: this .preparedStatement.setCursorName(this .getCursorName());
0221: }
0222: }
0223:
0224: /** Getter for property preparedStatement.
0225: * @return Value of property preparedStatement.
0226: *
0227: */
0228: public PreparedStatement getPreparedStatement(String sql,
0229: Collection params) throws SQLException {
0230: if (sql == null || sql.trim().length() == 0) {
0231: throw new SQLException("SQL String is empty or null");
0232: }
0233: if (debug)
0234: System.out.println("julp ============= "
0235: + new java.util.Date() + " " + this .getClass()
0236: + "::" + this
0237: + "::=============== getPreparedStatement(): \n"
0238: + sql + " ::params: " + params + " \n");
0239: int idx = -1;
0240: if (cacheStatements) { // PreparedStatement to be reused, otherwise must be closed and set to null
0241: idx = statementsId.indexOf(sql);
0242: if (idx >= 0) {
0243: Object obj = statementsCache.get(idx);
0244: if (debug) {
0245: System.out
0246: .println("julp ============= "
0247: + new java.util.Date()
0248: + " "
0249: + this .getClass()
0250: + "::"
0251: + this
0252: + "::=============== getPreparedStatement() ============ found cached preparedStatement: \n"
0253: + "idx: "
0254: + idx
0255: + " "
0256: + sql
0257: + "::"
0258: + (obj == null ? "<null>" : obj
0259: .toString()
0260: + obj.getClass()) + " \n");
0261: System.out
0262: .println("julp ============= statementsId: "
0263: + statementsId);
0264: System.out
0265: .println("julp ============= statementsCache: "
0266: + statementsCache);
0267: System.out
0268: .println("julp ============= statementsCacheCounter: "
0269: + statementsCacheCounter);
0270: }
0271: preparedStatement = (PreparedStatement) statementsCache
0272: .get(idx);
0273: if (debug)
0274: System.out
0275: .println("julp ============= "
0276: + new java.util.Date()
0277: + " "
0278: + this .getClass()
0279: + "::"
0280: + this
0281: + "::=============== getPreparedStatement() ============ found cached preparedStatement: \n"
0282: + sql + "::" + preparedStatement
0283: + " \n");
0284: if (preparedStatement == null) { // for some reson like garbage collected? than add again
0285: statementsId.remove(idx);
0286: statementsCache.remove(idx);
0287: idx = -1;
0288: } else {
0289: preparedStatement.clearParameters();
0290: }
0291: }
0292: }
0293: if (idx == -1) {
0294: prepareStatement(sql);
0295: }
0296: Iterator paramsIter = params.iterator();
0297: int parmIdx = 1;
0298: while (paramsIter.hasNext()) {
0299: Object param = paramsIter.next();
0300: if (param != null) {
0301: if (param.getClass().getName().equals("java.util.Date")) {
0302: param = new java.sql.Date(((java.util.Date) param)
0303: .getTime());
0304: }
0305: preparedStatement.setObject(parmIdx, param);
0306: } else {
0307: preparedStatement.setNull(parmIdx, Types.JAVA_OBJECT);
0308: }
0309: parmIdx++;
0310: }
0311: if (cacheStatements) {
0312: if (idx == -1) {
0313: idx = addCachedStatement(sql, preparedStatement);
0314: statementsCacheCounter.add(new Integer(1));
0315: } else {
0316: // How often this preparedStatement was called?
0317: Object count = statementsCacheCounter.get(idx);
0318: if (count == null) {
0319: statementsCacheCounter.add(new Integer(1));
0320: } else {
0321: statementsCacheCounter.set(idx, new Integer(
0322: ((Integer) count).intValue() + 1));
0323: }
0324: }
0325: }
0326: if (isProccessWarnings()) {
0327: populateWarnings(preparedStatement.getWarnings());
0328: preparedStatement.clearWarnings();
0329: }
0330: return preparedStatement;
0331: }
0332:
0333: /** Setter for property preparedStatement.
0334: * @param preparedStatement New value of property preparedStatement.
0335: *
0336: */
0337: public void setPreparedStatement(
0338: java.sql.PreparedStatement preparedStatement) {
0339: this .preparedStatement = preparedStatement;
0340: }
0341:
0342: /**************************************************************************/
0343:
0344: public DataHolder[] getResultAsDataHoldersArray(String sql,
0345: Collection params, boolean populateColumnNames)
0346: throws SQLException {
0347: return (DataHolder[]) getResultAsDataHoldersList(sql, params,
0348: populateColumnNames).toArray(new DataHolder[0]);
0349: }
0350:
0351: public DataHolder[] getResultAsDataHoldersArray(String sql,
0352: boolean populateColumnNames) throws SQLException {
0353: return (DataHolder[]) getResultAsDataHoldersList(sql,
0354: populateColumnNames).toArray(new DataHolder[0]);
0355: }
0356:
0357: public DataHolder[] getResultAsDataHoldersArray(String sql)
0358: throws SQLException {
0359: return (DataHolder[]) getResultAsDataHoldersList(sql).toArray(
0360: new DataHolder[0]);
0361: }
0362:
0363: public DataHolder[] getResultAsDataHoldersArray(String sql,
0364: Collection params) throws SQLException {
0365: return (DataHolder[]) getResultAsDataHoldersList(sql, params)
0366: .toArray(new DataHolder[0]);
0367: }
0368:
0369: public DataHolder[] getResultsAsDataHoldersArray(String sql,
0370: Collection params) throws SQLException {
0371: return (DataHolder[]) getResultsAsDataHoldersList(sql, params,
0372: true).toArray(new DataHolder[0]);
0373: }
0374:
0375: public DataHolder[] getResultsAsDataHoldersArray(String sql,
0376: Collection params, boolean populateColumnNames)
0377: throws SQLException {
0378: return (DataHolder[]) getResultsAsDataHoldersList(sql, params,
0379: populateColumnNames).toArray(new DataHolder[0]);
0380: }
0381:
0382: public DataHolder[] getResultsAsDataHoldersArray(String sql,
0383: Collection params, int maxNumberOfParams, String placeHolder)
0384: throws SQLException {
0385: return (DataHolder[]) getResultsAsDataHoldersList(sql, params,
0386: maxNumberOfParams, placeHolder, true).toArray(
0387: new DataHolder[0]);
0388: }
0389:
0390: public DataHolder[] getResultsAsDataHoldersArray(String sql,
0391: Collection params, int maxNumberOfParams,
0392: String placeHolder, boolean populateColumnNames)
0393: throws SQLException {
0394: return (DataHolder[]) getResultsAsDataHoldersList(sql, params,
0395: maxNumberOfParams, placeHolder, populateColumnNames)
0396: .toArray(new DataHolder[0]);
0397: }
0398:
0399: /**************************************************************************/
0400:
0401: public Object getSingleValue(String sql) throws SQLException {
0402: Object value = null;
0403: getResultSet(sql);
0404: if (this .resultSet.next()) {
0405: value = this .resultSet.getObject(1);
0406: } else {
0407: throw new SQLException("No data");
0408: }
0409: if (isProccessWarnings()) {
0410: populateWarnings(this .resultSet.getWarnings());
0411: this .resultSet.clearWarnings();
0412: }
0413: return value;
0414: }
0415:
0416: public Object getSingleValue(String sql, Collection params)
0417: throws SQLException {
0418: Object value = null;
0419: getResultSet(sql, params);
0420: if (this .resultSet.next()) {
0421: value = this .resultSet.getObject(1);
0422: } else {
0423: throw new SQLException("No data");
0424: }
0425: if (isProccessWarnings()) {
0426: populateWarnings(this .resultSet.getWarnings());
0427: this .resultSet.clearWarnings();
0428: }
0429: return value;
0430: }
0431:
0432: /**************************************************************************/
0433:
0434: public List getSingleColumnResultAsList(String sql)
0435: throws SQLException {
0436: getResultSet(sql);
0437: List rows = new ArrayList();
0438: while (this .resultSet.next()) {
0439: rows.add(this .resultSet.getObject(1));
0440: }
0441: if (isProccessWarnings()) {
0442: populateWarnings(this .resultSet.getWarnings());
0443: this .resultSet.clearWarnings();
0444: }
0445: return rows;
0446: }
0447:
0448: public List getSingleColumnResultAsList(String sql,
0449: Collection params) throws SQLException {
0450: getResultSet(sql, params);
0451: List rows = new ArrayList();
0452: while (this .resultSet.next()) {
0453: rows.add(this .resultSet.getObject(1));
0454: }
0455: if (isProccessWarnings()) {
0456: populateWarnings(this .resultSet.getWarnings());
0457: this .resultSet.clearWarnings();
0458: }
0459: return rows;
0460: }
0461:
0462: public List getSingleColumnResultsAsList(String sql,
0463: Collection params, int maxNumberOfParams, String placeHolder)
0464: throws SQLException {
0465: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0466: List rows = new ArrayList();
0467: for (int i = 0; i < resultSets.size(); i++) {
0468: while (((ResultSet) resultSets.get(i)).next()) {
0469: rows.add(((ResultSet) resultSets.get(i)).getObject(1));
0470: }
0471: if (isProccessWarnings()) {
0472: populateWarnings(((ResultSet) resultSets.get(i))
0473: .getWarnings());
0474: ((ResultSet) resultSets.get(i)).clearWarnings();
0475: }
0476: }
0477: return rows;
0478: }
0479:
0480: public List getSingleColumnResultsAsList(String sql, List params)
0481: throws SQLException {
0482: getResultSets(sql, params);
0483: List rows = new ArrayList();
0484: for (int i = 0; i < resultSets.size(); i++) {
0485: while (((ResultSet) resultSets.get(i)).next()) {
0486: rows.add(((ResultSet) resultSets.get(i)).getObject(1));
0487: }
0488: if (isProccessWarnings()) {
0489: populateWarnings(((ResultSet) resultSets.get(i))
0490: .getWarnings());
0491: ((ResultSet) resultSets.get(i)).clearWarnings();
0492: }
0493: }
0494: return rows;
0495: }
0496:
0497: /**************************************************************************/
0498:
0499: public Map getTwoColumnsResultAsMap(String sql) throws SQLException {
0500: getResultSet(sql);
0501: Map rows = new LinkedHashMap();
0502: while (this .resultSet.next()) {
0503: rows.put(this .resultSet.getObject(1), this .resultSet
0504: .getObject(2));
0505: }
0506: if (isProccessWarnings()) {
0507: populateWarnings(this .resultSet.getWarnings());
0508: this .resultSet.clearWarnings();
0509: }
0510: return rows;
0511: }
0512:
0513: public Map getTwoColumnsResultAsMap(String sql, Collection params)
0514: throws SQLException {
0515: getResultSet(sql, params);
0516: Map rows = new LinkedHashMap();
0517: while (this .resultSet.next()) {
0518: rows.put(this .resultSet.getObject(1), this .resultSet
0519: .getObject(2));
0520: }
0521: return rows;
0522: }
0523:
0524: public Map getTwoColumnsResultsAsMap(String sql, Collection params,
0525: int maxNumberOfParams, String placeHolder)
0526: throws SQLException {
0527: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0528: Map rows = new LinkedHashMap();
0529: for (int i = 0; i < resultSets.size(); i++) {
0530: while (((ResultSet) resultSets.get(i)).next()) {
0531: rows.put(this .resultSet.getObject(1), this .resultSet
0532: .getObject(2));
0533: }
0534: if (isProccessWarnings()) {
0535: populateWarnings(((ResultSet) resultSets.get(i))
0536: .getWarnings());
0537: ((ResultSet) resultSets.get(i)).clearWarnings();
0538: }
0539: }
0540: return rows;
0541: }
0542:
0543: public Map getTwoColumnsResultsAsMap(String sql, List params)
0544: throws SQLException {
0545: getResultSets(sql, params);
0546: Map rows = new LinkedHashMap();
0547: for (int i = 0; i < resultSets.size(); i++) {
0548: while (((ResultSet) resultSets.get(i)).next()) {
0549: rows.put(this .resultSet.getObject(1), this .resultSet
0550: .getObject(2));
0551: }
0552: if (isProccessWarnings()) {
0553: populateWarnings(((ResultSet) resultSets.get(i))
0554: .getWarnings());
0555: ((ResultSet) resultSets.get(i)).clearWarnings();
0556: }
0557: }
0558: return rows;
0559: }
0560:
0561: /**************************************************************************/
0562:
0563: public List getResultAsValueObjectList(String sql)
0564: throws SQLException {
0565: getResultSet(sql);
0566: List rows = new ArrayList();
0567: while (this .resultSet.next()) {
0568: rows.add(new ValueObject(this .resultSet.getObject(1),
0569: this .resultSet.getString(2)));
0570: }
0571: if (isProccessWarnings()) {
0572: populateWarnings(this .resultSet.getWarnings());
0573: this .resultSet.clearWarnings();
0574: }
0575: return rows;
0576: }
0577:
0578: public List getResultAsValueObjectList(String sql, Collection params)
0579: throws SQLException {
0580: getResultSet(sql, params);
0581: List rows = new ArrayList();
0582: while (this .resultSet.next()) {
0583: rows.add(new ValueObject(this .resultSet.getObject(1),
0584: this .resultSet.getString(2)));
0585: }
0586: return rows;
0587: }
0588:
0589: public List getResultsAsValueObjectList(String sql,
0590: Collection params, int maxNumberOfParams, String placeHolder)
0591: throws SQLException {
0592: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0593: List rows = new ArrayList();
0594: for (int i = 0; i < resultSets.size(); i++) {
0595: while (((ResultSet) resultSets.get(i)).next()) {
0596: rows.add(new ValueObject(this .resultSet.getObject(1),
0597: this .resultSet.getString(2)));
0598: }
0599: if (isProccessWarnings()) {
0600: populateWarnings(((ResultSet) resultSets.get(i))
0601: .getWarnings());
0602: ((ResultSet) resultSets.get(i)).clearWarnings();
0603: }
0604: }
0605: return rows;
0606: }
0607:
0608: public List getResultsAsValueObjectList(String sql, List params)
0609: throws SQLException {
0610: getResultSets(sql, params);
0611: List rows = new ArrayList();
0612: for (int i = 0; i < resultSets.size(); i++) {
0613: while (((ResultSet) resultSets.get(i)).next()) {
0614: rows.add(new ValueObject(this .resultSet.getObject(1),
0615: this .resultSet.getString(2)));
0616: }
0617: if (isProccessWarnings()) {
0618: populateWarnings(((ResultSet) resultSets.get(i))
0619: .getWarnings());
0620: ((ResultSet) resultSets.get(i)).clearWarnings();
0621: }
0622: }
0623: return rows;
0624: }
0625:
0626: /**************************************************************************/
0627:
0628: public List getResultAsDataHoldersList(String sql,
0629: Collection params, boolean populateColumnNames)
0630: throws SQLException {
0631: getResultSet(sql, params);
0632: ResultSetMetaData rsmd = this .resultSet.getMetaData();
0633: int colCount = rsmd.getColumnCount();
0634: List rows = new ArrayList();
0635: String[] colNames = new String[colCount];
0636: Map dups = new HashMap();
0637: if (populateColumnNames) {
0638: // rename columns if there are duplicate names
0639: for (int col = 1; col <= colCount; col++) {
0640: String colName = rsmd.getColumnName(col);
0641: if (!dups.containsKey(colName)) {
0642: dups.put(colName, new Integer(0));
0643: } else {
0644: Integer num = (Integer) dups.get(colName);
0645: int intValue = num.intValue();
0646: num = new Integer(intValue + 1);
0647: dups.put(colName, num);
0648: colName = colName + num;
0649: }
0650: colNames[col - 1] = colName;
0651: }
0652: }
0653: while (this .resultSet.next()) {
0654: DataHolder dataHolder = new DataHolder(colCount);
0655: for (int col = 1; col <= colCount; col++) {
0656: if (populateColumnNames) {
0657: dataHolder.setFieldNameAndValue(col,
0658: colNames[col - 1], this .resultSet
0659: .getObject(col));
0660: } else {
0661: dataHolder.setObject(col, this .resultSet
0662: .getObject(col));
0663: }
0664: }
0665: rows.add(dataHolder);
0666: }
0667: if (isProccessWarnings()) {
0668: populateWarnings(this .resultSet.getWarnings());
0669: this .resultSet.clearWarnings();
0670: }
0671: return rows;
0672: }
0673:
0674: public List getResultAsDataHoldersList(String sql,
0675: boolean populateColumnNames) throws SQLException {
0676: getResultSet(sql);
0677: ResultSetMetaData rsmd = this .resultSet.getMetaData();
0678: int colCount = rsmd.getColumnCount();
0679: List rows = new ArrayList();
0680: String[] colNames = new String[colCount];
0681: Map dups = new HashMap();
0682: if (populateColumnNames) {
0683: // rename columns if there are duplicate names
0684: for (int col = 1; col <= colCount; col++) {
0685: String colName = rsmd.getColumnName(col);
0686: if (!dups.containsKey(colName)) {
0687: dups.put(colName, new Integer(0));
0688: } else {
0689: Integer num = (Integer) dups.get(colName);
0690: int intValue = num.intValue();
0691: num = new Integer(intValue + 1);
0692: dups.put(colName, num);
0693: colName = colName + num;
0694: }
0695: colNames[col - 1] = colName;
0696: }
0697: }
0698: while (this .resultSet.next()) {
0699: DataHolder dataHolder = new DataHolder(colCount);
0700: for (int col = 1; col <= colCount; col++) {
0701: if (populateColumnNames) {
0702: dataHolder.setFieldNameAndValue(col,
0703: colNames[col - 1], this .resultSet
0704: .getObject(col));
0705: } else {
0706: dataHolder.setObject(col, this .resultSet
0707: .getObject(col));
0708: }
0709: }
0710: rows.add(dataHolder);
0711: }
0712: if (isProccessWarnings()) {
0713: populateWarnings(this .resultSet.getWarnings());
0714: this .resultSet.clearWarnings();
0715: }
0716: return rows;
0717: }
0718:
0719: public List getResultAsDataHoldersList(String sql)
0720: throws SQLException {
0721: return getResultAsDataHoldersList(sql, true);
0722: }
0723:
0724: public List getResultAsDataHoldersList(String sql, Collection params)
0725: throws SQLException {
0726: return getResultAsDataHoldersList(sql, params, true);
0727: }
0728:
0729: public List getResultsAsDataHoldersList(String sql,
0730: Collection params) throws SQLException {
0731: return getResultsAsDataHoldersList(sql, params, true);
0732: }
0733:
0734: public List getResultsAsDataHoldersList(String sql,
0735: Collection params, int maxNumberOfParams, String placeHolder)
0736: throws SQLException {
0737: return getResultsAsDataHoldersList(sql, params,
0738: maxNumberOfParams, placeHolder, true);
0739: }
0740:
0741: public List getResultsAsDataHoldersList(String sql,
0742: Collection params, int maxNumberOfParams,
0743: String placeHolder, boolean populateColumnNames)
0744: throws SQLException {
0745: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0746: ResultSetMetaData rsmd = ((ResultSet) this .resultSets.get(0))
0747: .getMetaData();
0748: int colCount = rsmd.getColumnCount();
0749: List rows = new ArrayList();
0750: String[] colNames = new String[colCount];
0751: Map dups = new HashMap();
0752: // rename columns if there are duplicate names
0753: for (int col = 1; col <= colCount; col++) {
0754: String colName = rsmd.getColumnName(col);
0755: if (!dups.containsKey(colName)) {
0756: dups.put(colName, new Integer(0));
0757: } else {
0758: Integer num = (Integer) dups.get(colName);
0759: int intValue = num.intValue();
0760: num = new Integer(intValue + 1);
0761: dups.put(colName, num);
0762: colName = colName + num;
0763: }
0764: colNames[col - 1] = colName;
0765: }
0766: for (int i = 0; i < resultSets.size(); i++) {
0767: while (((ResultSet) resultSets.get(i)).next()) {
0768: DataHolder dataHolder = new DataHolder(colCount);
0769: for (int col = 1; col <= colCount; col++) {
0770: if (populateColumnNames) {
0771: Object value = ((ResultSet) resultSets.get(i))
0772: .getObject(col);
0773: dataHolder.setFieldNameAndValue(col,
0774: colNames[col - 1],
0775: ((ResultSet) resultSets.get(i))
0776: .getObject(col));
0777: } else {
0778: dataHolder.setObject(col,
0779: ((ResultSet) resultSets.get(i))
0780: .getObject(col));
0781: }
0782: }
0783: rows.add(dataHolder);
0784: }
0785: if (isProccessWarnings()) {
0786: populateWarnings(((ResultSet) resultSets.get(i))
0787: .getWarnings());
0788: ((ResultSet) resultSets.get(i)).clearWarnings();
0789: }
0790: }
0791: return rows;
0792: }
0793:
0794: public List getResultsAsDataHoldersList(String sql,
0795: Collection params, boolean populateColumnNames)
0796: throws SQLException {
0797: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0798: ResultSetMetaData rsmd = ((ResultSet) this .resultSets.get(0))
0799: .getMetaData();
0800: int colCount = rsmd.getColumnCount();
0801: List rows = new ArrayList();
0802: String[] colNames = new String[colCount];
0803: Map dups = new HashMap();
0804: // rename columns if there are duplicate names
0805: for (int col = 1; col <= colCount; col++) {
0806: String colName = rsmd.getColumnName(col);
0807: if (!dups.containsKey(colName)) {
0808: dups.put(colName, new Integer(0));
0809: } else {
0810: Integer num = (Integer) dups.get(colName);
0811: int intValue = num.intValue();
0812: num = new Integer(intValue + 1);
0813: dups.put(colName, num);
0814: colName = colName + num;
0815: }
0816: colNames[col - 1] = colName;
0817: }
0818: for (int i = 0; i < resultSets.size(); i++) {
0819: while (((ResultSet) resultSets.get(i)).next()) {
0820: DataHolder dataHolder = new DataHolder(colCount);
0821: for (int col = 1; col <= colCount; col++) {
0822: if (populateColumnNames) {
0823: Object value = ((ResultSet) resultSets.get(i))
0824: .getObject(col);
0825: dataHolder.setFieldNameAndValue(col,
0826: colNames[col - 1],
0827: ((ResultSet) resultSets.get(i))
0828: .getObject(col));
0829: } else {
0830: dataHolder.setObject(col,
0831: ((ResultSet) resultSets.get(i))
0832: .getObject(col));
0833: }
0834: }
0835: rows.add(dataHolder);
0836: }
0837: if (isProccessWarnings()) {
0838: populateWarnings(((ResultSet) resultSets.get(i))
0839: .getWarnings());
0840: ((ResultSet) resultSets.get(i)).clearWarnings();
0841: }
0842: }
0843: return rows;
0844: }
0845:
0846: /**************************************************************************/
0847:
0848: /** @return Vector of Vectors */
0849: public Vector getResultsAsVector(String sql, Collection params,
0850: int maxNumberOfParams, String placeHolder)
0851: throws SQLException {
0852: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0853: int colCount = ((ResultSet) this .resultSets.get(0))
0854: .getMetaData().getColumnCount();
0855: Vector rows = new Vector();
0856: for (int i = 0; i < resultSets.size(); i++) {
0857: while (((ResultSet) resultSets.get(i)).next()) {
0858: Vector row = new Vector(colCount);
0859: for (int col = 1; col <= colCount; col++) {
0860: Object value = ((ResultSet) resultSets.get(i))
0861: .getObject(col);
0862: row.add(value);
0863: }
0864: rows.add(row);
0865: }
0866: if (isProccessWarnings()) {
0867: populateWarnings(((ResultSet) resultSets.get(i))
0868: .getWarnings());
0869: ((ResultSet) resultSets.get(i)).clearWarnings();
0870: }
0871: }
0872: return rows;
0873: }
0874:
0875: /** @return Vector of Vectors */
0876: public Vector getResultsAsVector(String sql, Collection params)
0877: throws SQLException {
0878: getResultSets(sql, params);
0879: int colCount = ((ResultSet) this .resultSets.get(0))
0880: .getMetaData().getColumnCount();
0881: Vector rows = new Vector();
0882: for (int i = 0; i < resultSets.size(); i++) {
0883: while (((ResultSet) resultSets.get(i)).next()) {
0884: Vector row = new Vector(colCount);
0885: for (int col = 1; col <= colCount; col++) {
0886: Object value = ((ResultSet) resultSets.get(i))
0887: .getObject(col);
0888: row.add(value);
0889: }
0890: rows.add(row);
0891: }
0892: if (isProccessWarnings()) {
0893: populateWarnings(((ResultSet) resultSets.get(i))
0894: .getWarnings());
0895: ((ResultSet) resultSets.get(i)).clearWarnings();
0896: }
0897: }
0898: return rows;
0899: }
0900:
0901: public Vector getResultAsVector(String sql) throws SQLException {
0902: getResultSet(sql);
0903: int colCount = this .resultSet.getMetaData().getColumnCount();
0904: Vector rows = new Vector();
0905: while (this .resultSet.next()) {
0906: Vector row = new Vector(colCount);
0907: for (int col = 1; col <= colCount; col++) {
0908: row.add(this .resultSet.getObject(col));
0909: }
0910: rows.add(row);
0911: }
0912: if (isProccessWarnings()) {
0913: populateWarnings(this .resultSet.getWarnings());
0914: this .resultSet.clearWarnings();
0915: }
0916: return rows;
0917: }
0918:
0919: /** @return Vector of Vectors */
0920: public Vector getResultAsVector(String sql, Collection params)
0921: throws SQLException {
0922: getResultSet(sql, params);
0923: int colCount = this .resultSet.getMetaData().getColumnCount();
0924: Vector rows = new Vector();
0925: while (this .resultSet.next()) {
0926: Vector row = new Vector(colCount);
0927: for (int col = 1; col <= colCount; col++) {
0928: row.add(this .resultSet.getObject(col));
0929: }
0930: rows.add(row);
0931: }
0932: if (isProccessWarnings()) {
0933: populateWarnings(this .resultSet.getWarnings());
0934: this .resultSet.clearWarnings();
0935: }
0936: return rows;
0937: }
0938:
0939: /**************************************************************************/
0940:
0941: /** @return Map of Lists. Each key is column name and each value is List of column values */
0942: public Map getResultsAsMap(String sql, Collection params)
0943: throws SQLException {
0944: getResultSets(sql, params);
0945: ResultSetMetaData rsmd = ((ResultSet) this .resultSets.get(0))
0946: .getMetaData();
0947: int colCount = rsmd.getColumnCount();
0948: Map columns = new LinkedHashMap(colCount);
0949: List[] column = new ArrayList[colCount];
0950: for (int i = 0; i < column.length; i++) {
0951: column[i] = new ArrayList();
0952: }
0953: for (int i = 0; i < resultSets.size(); i++) {
0954: while (((ResultSet) resultSets.get(i)).next()) {
0955: for (int col = 1; col <= colCount; col++) {
0956: Object value = ((ResultSet) resultSets.get(i))
0957: .getObject(col);
0958: column[col - 1].add(value);
0959: }
0960: }
0961: if (isProccessWarnings()) {
0962: populateWarnings(((ResultSet) resultSets.get(i))
0963: .getWarnings());
0964: ((ResultSet) resultSets.get(i)).clearWarnings();
0965: }
0966: }
0967: Map dups = new HashMap();
0968: for (int col = 1; col <= colCount; col++) {
0969: String colName = rsmd.getColumnName(col);
0970: if (!dups.containsKey(colName)) {
0971: dups.put(colName, new Integer(0));
0972: } else {
0973: Integer i = (Integer) dups.get(colName);
0974: int intValue = i.intValue();
0975: i = new Integer(intValue + 1);
0976: dups.put(colName, i);
0977: colName = colName + i;
0978: }
0979: columns.put(colName, column[col - 1]);
0980: }
0981: return columns;
0982: }
0983:
0984: /** @return Map of Lists. Each key is column name and each value is List of column values */
0985: public Map getResultsAsMap(String sql, Collection params,
0986: int maxNumberOfParams, String placeHolder)
0987: throws SQLException {
0988: getResultSets(sql, params, maxNumberOfParams, placeHolder);
0989: ResultSetMetaData rsmd = ((ResultSet) this .resultSets.get(0))
0990: .getMetaData();
0991: int colCount = rsmd.getColumnCount();
0992: Map columns = new LinkedHashMap(colCount);
0993: List[] column = new ArrayList[colCount];
0994: for (int i = 0; i < column.length; i++) {
0995: column[i] = new ArrayList();
0996: }
0997: for (int i = 0; i < resultSets.size(); i++) {
0998: while (((ResultSet) resultSets.get(i)).next()) {
0999: for (int col = 1; col <= colCount; col++) {
1000: Object value = ((ResultSet) resultSets.get(i))
1001: .getObject(col);
1002: column[col - 1].add(value);
1003: }
1004: }
1005: if (isProccessWarnings()) {
1006: populateWarnings(((ResultSet) resultSets.get(i))
1007: .getWarnings());
1008: ((ResultSet) resultSets.get(i)).clearWarnings();
1009: }
1010: }
1011: Map dups = new HashMap();
1012: for (int col = 1; col <= colCount; col++) {
1013: String colName = rsmd.getColumnName(col);
1014: if (!dups.containsKey(colName)) {
1015: dups.put(colName, new Integer(0));
1016: } else {
1017: Integer i = (Integer) dups.get(colName);
1018: int intValue = i.intValue();
1019: i = new Integer(intValue + 1);
1020: dups.put(colName, i);
1021: colName = colName + i;
1022: }
1023: columns.put(colName, column[col - 1]);
1024: }
1025: return columns;
1026: }
1027:
1028: /** @return Map of Lists. Each key is column name and each value is List of column values */
1029: public Map getResultAsMap(String sql, Collection params)
1030: throws SQLException {
1031: getResultSet(sql, params);
1032: ResultSetMetaData rsmd = this .resultSet.getMetaData();
1033: int colCount = rsmd.getColumnCount();
1034: Map columns = new LinkedHashMap(colCount);
1035: List[] column = new ArrayList[colCount];
1036: for (int i = 0; i < column.length; i++) {
1037: column[i] = new ArrayList();
1038: }
1039: while (this .resultSet.next()) {
1040: for (int col = 1; col <= colCount; col++) {
1041: Object value = this .resultSet.getObject(col);
1042: column[col - 1].add(value);
1043: }
1044: }
1045: Map dups = new HashMap();
1046: for (int col = 1; col <= colCount; col++) {
1047: String colName = rsmd.getColumnName(col);
1048: if (!dups.containsKey(colName)) {
1049: dups.put(colName, new Integer(0));
1050: } else {
1051: Integer i = (Integer) dups.get(colName);
1052: int intValue = i.intValue();
1053: i = new Integer(intValue + 1);
1054: dups.put(colName, i);
1055: colName = colName + i;
1056: }
1057: columns.put(colName, column[col - 1]);
1058: }
1059: if (isProccessWarnings()) {
1060: populateWarnings(this .resultSet.getWarnings());
1061: this .resultSet.clearWarnings();
1062: }
1063: return columns;
1064: }
1065:
1066: /** @return Map of Lists. Each key is column name and each value is List of column values */
1067: public Map getResultAsMap(String sql) throws SQLException {
1068: getResultSet(sql);
1069: ResultSetMetaData rsmd = this .resultSet.getMetaData();
1070: int colCount = rsmd.getColumnCount();
1071: Map columns = new LinkedHashMap(colCount);
1072: List[] column = new ArrayList[colCount];
1073: for (int i = 0; i < column.length; i++) {
1074: column[i] = new ArrayList();
1075: }
1076: while (this .resultSet.next()) {
1077: for (int col = 1; col <= colCount; col++) {
1078: Object value = this .resultSet.getObject(col);
1079: column[col - 1].add(value);
1080: }
1081: }
1082: Map dups = new HashMap();
1083: for (int col = 1; col <= colCount; col++) {
1084: String colName = rsmd.getColumnName(col);
1085: if (!dups.containsKey(colName)) {
1086: dups.put(colName, new Integer(0));
1087: } else {
1088: Integer i = (Integer) dups.get(colName);
1089: int intValue = i.intValue();
1090: i = new Integer(intValue + 1);
1091: dups.put(colName, i);
1092: colName = colName + i;
1093: }
1094: columns.put(colName, column[col - 1]);
1095: }
1096: if (isProccessWarnings()) {
1097: populateWarnings(this .resultSet.getWarnings());
1098: this .resultSet.clearWarnings();
1099: }
1100: return columns;
1101: }
1102:
1103: /**************************************************************************/
1104:
1105: /** Getter for property resultSet.
1106: * @return Value of property resultSet.
1107: *
1108: */
1109: public java.sql.ResultSet getResultSet(String sql)
1110: throws SQLException {
1111: if (resultSet != null && isCloseResultSet()) {
1112: if (debug)
1113: System.out.println("julp ============= "
1114: + new java.util.Date() + " " + this .getClass()
1115: + "::" + this
1116: + "::getResultSet()::closing ResultSet: "
1117: + resultSet + " \n");
1118: resultSet.close();
1119: }
1120: resultSet = this .getStatement(sql).executeQuery(sql);
1121: if (debug)
1122: System.out.println("julp ============= "
1123: + new java.util.Date() + " " + this .getClass()
1124: + "::" + this + "::getResultSet()::sql: \n" + sql
1125: + " \n");
1126: if (isProccessWarnings()) {
1127: populateWarnings(resultSet.getWarnings());
1128: this .resultSet.clearWarnings();
1129: }
1130: return resultSet;
1131: }
1132:
1133: public java.sql.ResultSet getResultSet(String sql, Collection params)
1134: throws SQLException {
1135: if (resultSet != null && isCloseResultSet()) {
1136: resultSet.close();
1137: if (debug)
1138: System.out.println("julp ============= "
1139: + new java.util.Date() + " " + this .getClass()
1140: + "::" + this
1141: + "::getResultSet()::closing ResultSet: "
1142: + resultSet + " \n");
1143: }
1144: resultSet = this .getPreparedStatement(sql, params)
1145: .executeQuery();
1146: if (debug)
1147: System.out.println("julp ============= "
1148: + new java.util.Date() + " " + this .getClass()
1149: + "::" + this + "::getResultSet(): " + resultSet
1150: + " \n");
1151: if (isProccessWarnings()) {
1152: populateWarnings(resultSet.getWarnings());
1153: }
1154: return resultSet;
1155: }
1156:
1157: public java.sql.ResultSet[] getResultSets(String sql,
1158: Collection params, int maxNumberOfParams, String placeHolder)
1159: throws SQLException {
1160: this .maxNumberOfParams = maxNumberOfParams;
1161: this .placeHolder = placeHolder;
1162: return getResultSets(sql, params);
1163: }
1164:
1165: public java.sql.ResultSet[] getResultSets(String sql,
1166: Collection params) throws SQLException {
1167: /**
1168: * This convinient method to built "IN" sql string if number of arguments for "IN" is greater than
1169: * max number of parameters database can handle. It will create several ResultSet's.
1170: * The argument list which is greater then max number of parameters database can handle MUST be last in the params.
1171: * Also placeHolder and maxNumberOfParams must be set.
1172: *
1173: * Example:
1174: * String sql = "SELECT * FROM CUSTOMER WHERE LAST_NAME <> ? AND CUSTOMER_ID IN (:#)";
1175: * Collection argsList = new ArrayList();
1176: * List custId = new ArrayList();
1177: * custId.add(new Integer(0));
1178: * custId.add(new Integer(1));
1179: * custId.add(new Integer(2));
1180: * custId.add(new Integer(3));
1181: * custId.add(new Integer(4));
1182: * custId.add(new Integer(5));
1183: * custId.add(new Integer(6));
1184: * custId.add(new Integer(7));
1185: * argsList.add("John Doh");
1186: * argsList.add(custId);
1187: * factory.getDBServices().setMaxNumberOfParams(3);
1188: * factory.getDBServices().setPlaceHolder(":#");
1189: * factory.load(factory.getDBServices().getResultSets(sql, argsList));
1190: * factory.getDBServices().release(true);
1191: */
1192: closeResults();
1193: resultSets = new ArrayList();
1194: Collection newArgsList = new ArrayList();
1195: String newSql = null;
1196: Collection newArgsList1 = null;
1197: boolean loaded = false;
1198: boolean closeResultOrig = this .closeResultSet;
1199: this .closeResultSet = false;
1200: int compoundParameters = 0;
1201: int paramsSize = params.size();
1202: Iterator testParamsIter = params.iterator();
1203: while (testParamsIter.hasNext()) {
1204: Object param = testParamsIter.next();
1205: if (debug)
1206: System.out.println("julp ============= "
1207: + new java.util.Date() + " " + this .getClass()
1208: + "::" + this + "::getResultSets()::param:"
1209: + param + "::instanceof java.util.Collection: "
1210: + (param instanceof java.util.Collection)
1211: + " \n");
1212: if (param instanceof java.util.Collection) { // this is compound parameter
1213: compoundParameters++;
1214: }
1215: }
1216: String[] placeholdersTest = sql.split(this .placeHolder);
1217: if (debug)
1218: System.out.println("julp ============= "
1219: + new java.util.Date() + " " + this .getClass()
1220: + "::" + this + "::getResultSets()::sql: \n" + sql
1221: + "::placeholdersTest: "
1222: + Arrays.asList(placeholdersTest) + " \n");
1223: if (debug)
1224: System.out.println("julp ============= "
1225: + new java.util.Date() + " " + this .getClass()
1226: + "::" + this + "::getResultSets()::sql: \n" + sql
1227: + "::compoundParameters: " + compoundParameters
1228: + "::placeholdersTest.length: "
1229: + placeholdersTest.length + " \n");
1230: if ((placeholdersTest.length - 1) != compoundParameters) {
1231: throw new SQLException(
1232: "Number of placeholders in sql is not equal to number of compound parameters");
1233: }
1234: try {
1235: Iterator iter1 = params.iterator();
1236: int paramsCount = 0;
1237: while (iter1.hasNext()) {
1238: Object this Arg = iter1.next();
1239: if (this Arg instanceof java.util.Collection) { // this is compound parameter
1240: if (maxNumberOfParams < 0) {
1241: throw new SQLException(
1242: "Invalid maxNumberOfParams: "
1243: + maxNumberOfParams);
1244: }
1245: List list = new ArrayList((Collection) this Arg);
1246: int this ParmSize = list.size();
1247: if (this ParmSize > maxNumberOfParams) {
1248: if (paramsCount < (paramsSize - 1)) {
1249: throw new SQLException(
1250: "Only the last entry can have more arguments then Max Number of Parameters");
1251: }
1252: newSql = sql;
1253: StringBuffer sb = new StringBuffer();
1254: Iterator iter3 = list.iterator();
1255: int count = 0;
1256: int total = 0;
1257: List temp = new ArrayList();
1258: Object paramValue = null;
1259: for (int i = 0; i < this ParmSize; i++) {
1260: if (count < (maxNumberOfParams)) {
1261: paramValue = list.get(i);
1262: sb.append("?,");
1263: temp.add(paramValue);
1264: if (i == (this ParmSize - 1)) {
1265: int idx = sb.length() - 1;
1266: char c = sb.charAt(idx);
1267: if (c == ',') {
1268: sb.deleteCharAt(idx);
1269: }
1270: newSql = sql.replaceFirst(
1271: placeHolder, sb.toString());
1272: newArgsList1 = new ArrayList(
1273: newArgsList);
1274: newArgsList1.addAll(temp);
1275: if (debug)
1276: System.out
1277: .println("julp ============= "
1278: + new java.util.Date()
1279: + " "
1280: + this
1281: .getClass()
1282: + "::"
1283: + this
1284: + "::getResultSets()::sql: "
1285: + newSql
1286: + "::args: "
1287: + newArgsList1
1288: + " \n");
1289: resultSets.add(this
1290: .getPreparedStatement(
1291: newSql,
1292: newArgsList1)
1293: .executeQuery());// warnings??
1294: loaded = true;
1295: break;
1296: }
1297: count++;
1298: } else if (count == (maxNumberOfParams)) {
1299: int idx = sb.length() - 1;
1300: char c = sb.charAt(idx);
1301: if (c == ',') {
1302: sb.deleteCharAt(idx);
1303: }
1304: newSql = sql.replaceFirst(placeHolder,
1305: sb.toString());
1306: newArgsList1 = new ArrayList(
1307: newArgsList);
1308: newArgsList1.addAll(temp);
1309: if (debug)
1310: System.out
1311: .println("julp ============= "
1312: + new java.util.Date()
1313: + " "
1314: + this .getClass()
1315: + "::"
1316: + this
1317: + "::getResultSets()::sql: "
1318: + newSql
1319: + "::args: "
1320: + newArgsList1
1321: + " \n");
1322: resultSets.add(this
1323: .getPreparedStatement(newSql,
1324: newArgsList1)
1325: .executeQuery());// warnings??
1326: loaded = true;
1327: temp.clear();
1328: sb.setLength(0);
1329: newSql = sql;
1330: count = 0;
1331: i--;
1332: }
1333: }
1334: } else {
1335: StringBuffer sb = new StringBuffer();
1336: Iterator iter2 = list.iterator();
1337: while (iter2.hasNext()) {
1338: sb.append("?,");
1339: newArgsList.add(iter2.next());
1340: }
1341: sb.deleteCharAt(sb.lastIndexOf(","));
1342: sql = sql.replaceFirst(placeHolder, sb
1343: .toString());
1344: }
1345: } else { // this is regular parameter
1346: newArgsList.add(this Arg);
1347: }
1348: paramsCount++;
1349: }
1350: if (!loaded) {
1351: if (debug)
1352: System.out.println("julp ============= "
1353: + new java.util.Date() + " "
1354: + this .getClass() + "::" + this
1355: + "::getResultSets()::sql: \n" + sql
1356: + "::args: " + newArgsList + " \n");
1357: resultSets.add(this .getPreparedStatement(sql,
1358: newArgsList).executeQuery()); // warnings??
1359: }
1360: } catch (Exception e) {
1361: e.printStackTrace();
1362: throw new SQLException(e.getMessage());
1363: }
1364: setCloseResultSet(closeResultOrig);
1365: return (java.sql.ResultSet[]) resultSets
1366: .toArray(new ResultSet[0]);
1367: }
1368:
1369: public List executeStoredProcedure(String sql, Parameters params,
1370: int proccessResult) throws SQLException {
1371: if (debug)
1372: System.out.println("julp ============= "
1373: + new java.util.Date() + " " + this .getClass()
1374: + "::" + this
1375: + "::executeStoredProcedure()::sql: \n" + sql
1376: + "\n");
1377: List returnValue = new ArrayList();
1378: try {
1379: callStatement = getConnection().prepareCall(sql);
1380: for (int i = 1; i <= params.getParameterCount(); i++) {
1381: int parameterMode = params.getParameterMode(i);
1382: if (debug)
1383: System.out
1384: .println("julp ============= "
1385: + new java.util.Date()
1386: + " "
1387: + this .getClass()
1388: + "::"
1389: + this
1390: + "::executeStoredProcedure()::parameterMode: "
1391: + parameterMode + "::paramIndex: "
1392: + i + "::paramType: "
1393: + params.getParameterType(i)
1394: + "::parameterName: "
1395: + params.getParameterName(i)
1396: + "::parameterValue: "
1397: + params.getParameter(i) + "\n");
1398: if (parameterMode == Parameters.parameterModeIn) {
1399: callStatement.setObject(i, params.getParameter(i));
1400: } else if (parameterMode == Parameters.parameterModeOut) {
1401: callStatement.registerOutParameter(i, params
1402: .getParameterType(i));
1403: } else if (parameterMode == Parameters.parameterModeInOut) {
1404: callStatement.registerOutParameter(i, params
1405: .getParameterType(i));
1406: callStatement.setObject(i, params.getParameter(i));
1407: }
1408: }
1409:
1410: ResultSet result = null;
1411: int rowsAffected = 0;
1412: boolean hasResult = callStatement.execute();
1413: if (debug)
1414: System.out.println("julp ============= "
1415: + new java.util.Date() + " " + this .getClass()
1416: + "::" + this
1417: + "::executeStoredProcedure()::hasResult: "
1418: + hasResult + "\n");
1419:
1420: // procces ResultSet(s) and/or updateCount
1421: while (hasResult || rowsAffected != -1) {
1422: if (hasResult == true) {
1423: result = callStatement.getResultSet();
1424: try {
1425: if (proccessResult == IGNORE_RESULT) {
1426: // do nothing
1427: } else if (proccessResult == RESULT_AS_DATA_HOLDERS_LIST) {
1428: ResultSetMetaData rsmd = result
1429: .getMetaData();
1430: int colCount = rsmd.getColumnCount();
1431: List rows = new ArrayList();
1432: String[] colNames = new String[colCount];
1433: Map dups = new HashMap();
1434: // rename columns if there are duplicate names
1435: for (int col = 1; col <= colCount; col++) {
1436: String colName = rsmd
1437: .getColumnName(col);
1438: if (!dups.containsKey(colName)) {
1439: dups.put(colName, new Integer(0));
1440: } else {
1441: Integer num = (Integer) dups
1442: .get(colName);
1443: int intValue = num.intValue();
1444: num = new Integer(intValue + 1);
1445: dups.put(colName, num);
1446: colName = colName + num;
1447: }
1448: colNames[col - 1] = colName;
1449: }
1450: while (result.next()) {
1451: DataHolder dataHolder = new DataHolder(
1452: colCount);
1453: for (int col = 1; col <= colCount; col++) {
1454: dataHolder.setFieldNameAndValue(
1455: col, colNames[col - 1],
1456: result.getObject(col));
1457: }
1458: rows.add(dataHolder);
1459: }
1460: if (isProccessWarnings()) {
1461: populateWarnings(result.getWarnings());
1462: result.clearWarnings();
1463: }
1464: returnValue.add(rows);
1465: } else if (proccessResult == RESULT_AS_VECTOR) {
1466: int colCount = result.getMetaData()
1467: .getColumnCount();
1468: Vector rows = new Vector();
1469: while (result.next()) {
1470: Vector row = new Vector(colCount);
1471: for (int col = 1; col <= colCount; col++) {
1472: row.add(result.getObject(col));
1473: }
1474: rows.add(row);
1475: }
1476: if (isProccessWarnings()) {
1477: populateWarnings(result.getWarnings());
1478: result.clearWarnings();
1479: }
1480: returnValue.add(rows);
1481: } else if (proccessResult == RESULT_AS_MAP) {
1482: ResultSetMetaData rsmd = result
1483: .getMetaData();
1484: int colCount = rsmd.getColumnCount();
1485: Map columns = new LinkedHashMap(colCount);
1486: List[] column = new ArrayList[colCount];
1487: for (int i = 0; i < column.length; i++) {
1488: column[i] = new ArrayList();
1489: }
1490: while (result.next()) {
1491: for (int col = 1; col <= colCount; col++) {
1492: Object value = result
1493: .getObject(col);
1494: column[col - 1].add(value);
1495: }
1496: }
1497: Map dups = new HashMap();
1498: for (int col = 1; col <= colCount; col++) {
1499: String colName = rsmd
1500: .getColumnName(col);
1501: if (!dups.containsKey(colName)) {
1502: dups.put(colName, new Integer(0));
1503: } else {
1504: Integer i = (Integer) dups
1505: .get(colName);
1506: int intValue = i.intValue();
1507: i = new Integer(intValue + 1);
1508: dups.put(colName, i);
1509: colName = colName + i;
1510: }
1511: columns.put(colName, column[col - 1]);
1512: }
1513: if (isProccessWarnings()) {
1514: populateWarnings(result.getWarnings());
1515: result.clearWarnings();
1516: }
1517: returnValue.add(columns);
1518:
1519: } else if (proccessResult == RESULT_AS_ROWSET) {
1520: javax.sql.rowset.CachedRowSet crs = (javax.sql.rowset.CachedRowSet) Class
1521: .forName(cachedRowSetClassName)
1522: .newInstance();
1523: crs.populate(result);
1524: returnValue.add(crs);
1525: }
1526: } finally {
1527: if (isProccessWarnings()) {
1528: populateWarnings(result.getWarnings());
1529: result.clearWarnings();
1530: }
1531: if (debug)
1532: System.out
1533: .println("julp ============= "
1534: + new java.util.Date()
1535: + " "
1536: + this .getClass()
1537: + "::"
1538: + this
1539: + "::executeStoredProcedure()::closing ResultSet: "
1540: + result + " \n");
1541: result.close();
1542: result = null;
1543: }
1544: } else {
1545: rowsAffected = callStatement.getUpdateCount();
1546: if (rowsAffected != -1) {
1547: returnValue.add(new Integer(rowsAffected));
1548: }
1549: }
1550: hasResult = callStatement.getMoreResults();
1551: }
1552:
1553: // proccess OUT and/or INOUT parameters
1554: for (int i = 1; i <= params.getParameterCount(); i++) {
1555: int parameterMode = params.getParameterMode(i);
1556: if (parameterMode == Parameters.parameterModeOut
1557: || parameterMode == Parameters.parameterModeInOut) {
1558: Object value = callStatement.getObject(i);
1559: params.setParameter(i, value);
1560: }
1561: }
1562: if (isProccessWarnings()) {
1563: populateWarnings(callStatement.getWarnings());
1564: this .callStatement.clearWarnings();
1565: }
1566: returnValue.add(params);
1567: } catch (Exception e) {
1568: e.printStackTrace();
1569: throw new SQLException(e.getMessage());
1570: } finally {
1571: if (isCloseStatementAfterExecute()) {
1572: if (debug)
1573: System.out
1574: .println("julp ============= "
1575: + new java.util.Date()
1576: + " "
1577: + this .getClass()
1578: + "::"
1579: + this
1580: + "::executeStoredProcedure()::closing CallableStatement: "
1581: + callStatement + " \n");
1582: if (callStatement != null) {
1583: callStatement.close();
1584: callStatement = null;
1585: }
1586: }
1587: }
1588: return returnValue;
1589: }
1590:
1591: public int[] executeBatch(String sql, Collection batch)
1592: throws SQLException {
1593: int[] rowsAffected;
1594: try {
1595: this .prepareStatement(sql);
1596: Iterator batchIter = batch.iterator();
1597: while (batchIter.hasNext()) {
1598: Collection params = (Collection) batchIter.next();
1599: Iterator paramsIter = params.iterator();
1600: int parmIdx = 1;
1601: while (paramsIter.hasNext()) {
1602: Object param = paramsIter.next();
1603: if (param != null) {
1604: if (param.getClass().getName().equals(
1605: "java.util.Date")) {
1606: param = new java.sql.Date(
1607: ((java.util.Date) param).getTime());
1608: }
1609: preparedStatement.setObject(parmIdx, param);
1610: } else {
1611: preparedStatement.setNull(parmIdx,
1612: Types.JAVA_OBJECT);
1613: }
1614: parmIdx++;
1615: }
1616: preparedStatement.addBatch();
1617: }
1618: rowsAffected = this .preparedStatement.executeBatch();
1619: if (debug)
1620: System.out.println("julp ============= "
1621: + new java.util.Date() + " " + this .getClass()
1622: + "::" + this + "::executeBatch(" + sql
1623: + ")::batch: " + batch + "::rowsAffected: "
1624: + Arrays.asList(rowsAffected) + "\n");
1625: } finally {
1626: if (isProccessWarnings()) {
1627: populateWarnings(preparedStatement.getWarnings());
1628: this .preparedStatement.clearWarnings();
1629: }
1630: if (isCloseStatementAfterExecute()) {
1631: if (preparedStatement != null) {
1632: if (debug)
1633: System.out
1634: .println("julp ============= "
1635: + new java.util.Date()
1636: + " "
1637: + this .getClass()
1638: + "::"
1639: + this
1640: + "::executeBatch()::closing PreparedStatement: "
1641: + preparedStatement + " \n");
1642: preparedStatement.close();
1643: preparedStatement = null;
1644: }
1645: }
1646: }
1647: return rowsAffected;
1648: }
1649:
1650: public int[] executeBatch(Collection batch) throws SQLException {
1651: int[] rowsAffected;
1652: createStatement();
1653: try {
1654: Iterator batchIter = batch.iterator();
1655: while (batchIter.hasNext()) {
1656: String sql = (String) batchIter.next();
1657: this .statement.addBatch(sql);
1658: }
1659: rowsAffected = this .statement.executeBatch();
1660: if (debug)
1661: System.out.println("julp ============= "
1662: + new java.util.Date() + " " + this .getClass()
1663: + "::" + this + "::executeBatch()::batch: "
1664: + batch + "::rowsAffected: "
1665: + Arrays.asList(rowsAffected) + "\n");
1666: } finally {
1667: if (isProccessWarnings()) {
1668: populateWarnings(statement.getWarnings());
1669: this .statement.clearWarnings();
1670: }
1671: if (isCloseStatementAfterExecute()) {
1672: if (statement != null) {
1673: if (debug)
1674: System.out
1675: .println("julp ============= "
1676: + new java.util.Date()
1677: + " "
1678: + this .getClass()
1679: + "::"
1680: + this
1681: + "::executeBatch()::closing Statement: "
1682: + statement + " \n");
1683: statement.close();
1684: statement = null;
1685: }
1686: }
1687: }
1688: return rowsAffected;
1689: }
1690:
1691: public int execute(String sql) throws SQLException {
1692: int rowsAffected = -1;
1693: try {
1694: rowsAffected = this .getStatement(sql).executeUpdate(sql);
1695: } finally {
1696: if (isProccessWarnings()) {
1697: populateWarnings(statement.getWarnings());
1698: this .statement.clearWarnings();
1699: }
1700: if (isCloseStatementAfterExecute()) {
1701: if (statement != null) {
1702: if (debug)
1703: System.out
1704: .println("julp ============= "
1705: + new java.util.Date()
1706: + " "
1707: + this .getClass()
1708: + "::"
1709: + this
1710: + "::executeBatch()::closing Statement: "
1711: + statement + " \n");
1712: statement.close();
1713: statement = null;
1714: }
1715: }
1716: }
1717: this .setRowsAffectedInTransaction(rowsAffectedInTransaction
1718: + rowsAffected);
1719: if (debug)
1720: System.out.println("julp ============= "
1721: + new java.util.Date() + " " + this .getClass()
1722: + "::" + this + "::execute()::sql: \n" + sql
1723: + "::rowsAffected: " + rowsAffected + "\n");
1724: return rowsAffected;
1725: }
1726:
1727: public int execute(String sql, Collection params)
1728: throws SQLException {
1729: int rowsAffected = -1;
1730: try {
1731: rowsAffected = this .getPreparedStatement(sql, params)
1732: .executeUpdate();
1733: } finally {
1734: if (isProccessWarnings()) {
1735: populateWarnings(statement.getWarnings());
1736: this .statement.clearWarnings();
1737: }
1738: if (isCloseStatementAfterExecute()) {
1739: if (preparedStatement != null) {
1740: if (debug)
1741: System.out
1742: .println("julp ============= "
1743: + new java.util.Date()
1744: + " "
1745: + this .getClass()
1746: + "::"
1747: + this
1748: + "::executeBatch()::closing PreparedStatement: "
1749: + preparedStatement + " \n");
1750: preparedStatement.close();
1751: preparedStatement = null;
1752: }
1753: }
1754: }
1755: this .setRowsAffectedInTransaction(rowsAffectedInTransaction
1756: + rowsAffected);
1757: if (debug)
1758: System.out.println("julp ============= "
1759: + new java.util.Date() + " " + this .getClass()
1760: + "::" + this + "::execute()::sql: \n" + sql
1761: + " params: " + params + "::rowsAffected: "
1762: + rowsAffected + "\n");
1763: return rowsAffected;
1764: }
1765:
1766: /** Setter for property resultSet.
1767: * @param resultSet New value of property resultSet.
1768: *
1769: */
1770: public void setResultSet(java.sql.ResultSet resultSet) {
1771: this .resultSet = resultSet;
1772: }
1773:
1774: protected void createStatement() throws SQLException {
1775: statement = this .getConnection().createStatement(
1776: getResultSetType(), getConcurrency());
1777: statement.setEscapeProcessing(this .getEscapeProcessing());
1778: statement.setFetchSize(this .getFetchSize());
1779: statement.setMaxRows(this .getMaxRows());
1780: statement.setMaxFieldSize(this .getMaxFieldSize());
1781: statement.setQueryTimeout(this .getQueryTimeout());
1782: if (this .getCursorName() != null) {
1783: statement.setCursorName(this .getCursorName());
1784: }
1785: }
1786:
1787: /** Getter for property statement.
1788: * @return Value of property statement.
1789: *
1790: */
1791: public java.sql.Statement getStatement(String sql)
1792: throws SQLException {
1793: if (sql == null || sql.trim().length() == 0) {
1794: throw new SQLException("SQL String is empty or null");
1795: }
1796: int idx = -1;
1797: if (cacheStatements) { // Statement to be reused, otherwise must be closed and set to null
1798: idx = statementsId.indexOf(sql);
1799: if (idx >= 0) {
1800: statement = (Statement) statementsCache.get(idx);
1801: if (debug)
1802: System.out
1803: .println("julp ============= "
1804: + new java.util.Date()
1805: + " "
1806: + this .getClass()
1807: + "::"
1808: + this
1809: + "::=============== getStatement() ============ found cached statement: \n"
1810: + sql + "::" + statement + " \n");
1811: if (statement == null) {
1812: statementsId.remove(idx);
1813: statementsCache.remove(idx); //??
1814: idx = -1;
1815: }
1816: }
1817: }
1818: if (idx == -1) { // Do not use cache or this statement is not cached yet
1819: createStatement();
1820: }
1821: if (cacheStatements) {
1822: if (idx == -1) {
1823: idx = addCachedStatement(sql, statement);
1824: statementsCacheCounter.add(new Integer(1));
1825: } else {
1826: // How often this statement was called?
1827: Object count = statementsCacheCounter.get(idx);
1828: if (count == null) {
1829: statementsCacheCounter.add(new Integer(1));
1830: } else {
1831: statementsCacheCounter.set(idx, new Integer(
1832: ((Integer) count).intValue() + 1));
1833: }
1834: }
1835: }
1836: if (isProccessWarnings()) {
1837: populateWarnings(statement.getWarnings());
1838: statement.clearWarnings();
1839: }
1840: return statement;
1841: }
1842:
1843: protected void closeResults() throws SQLException {
1844: if (resultSets != null) {
1845: for (int i = 0; i < resultSets.size(); i++) {
1846: Object obj = resultSets.get(i);
1847: if (obj != null) {
1848: ((ResultSet) obj).close();
1849: obj = null;
1850: }
1851: }
1852: resultSets = null;
1853: }
1854: }
1855:
1856: /** Setter for property statement.
1857: * @param statement New value of property statement.
1858: *
1859: */
1860: public void setStatement(java.sql.Statement statement) {
1861: this .statement = statement;
1862: }
1863:
1864: public void release(boolean closeConnection) throws SQLException {
1865: if (debug)
1866: System.out
1867: .println("julp ============= "
1868: + new java.util.Date()
1869: + " "
1870: + this .getClass()
1871: + "::"
1872: + this
1873: + "::=============== RELEASE ============ closeConnection: "
1874: + closeConnection + " \n");
1875: if (resultSet != null) {
1876: resultSet.close();
1877: resultSet = null;
1878: }
1879: closeResults();
1880: if (!cacheStatements) {
1881: if (statement != null) {
1882: statement.close();
1883: statement = null;
1884: }
1885: if (preparedStatement != null) {
1886: preparedStatement.close();
1887: preparedStatement = null;
1888: }
1889: if (callStatement != null) {
1890: callStatement.close();
1891: callStatement = null;
1892: }
1893: }
1894: if (warnings != null) {
1895: warnings.clear();
1896: }
1897: if (connection != null) {
1898: if (closeConnection == true) {
1899: clearStatementCache();
1900: if (proccessTransactionOnCloseConnection == ROLLBACK) {
1901: connection.rollback();
1902: } else if (proccessTransactionOnCloseConnection == COMMIT) {
1903: connection.commit();
1904: }
1905: connection.close();
1906: setConnectionClosed(true);
1907: connection = null;
1908: this .setTranInProccess(false);
1909: this .setConnectionId("<none>");
1910: } else {
1911: //do nothing
1912: }
1913: }
1914: }
1915:
1916: public void beginTran() throws SQLException {
1917: if (EJBContext == null) {
1918: if (debug)
1919: System.out
1920: .println("julp ============= "
1921: + new java.util.Date()
1922: + " "
1923: + this .getClass()
1924: + "::"
1925: + this
1926: + "::=============== BEGIN TRAN ============ previous connectionId: "
1927: + connectionId + " \n");
1928: this .setRowsAffectedInTransaction(0);
1929: this .getConnection().setAutoCommit(false);
1930: this .setConnectionId(this .connection.toString());
1931: this .setTranInProccess(true);
1932: if (isProccessWarnings()) {
1933: populateWarnings(this .connection.getWarnings());
1934: this .connection.clearWarnings();
1935: }
1936: if (debug)
1937: System.out
1938: .println("julp ============= "
1939: + new java.util.Date()
1940: + " "
1941: + this .getClass()
1942: + "::"
1943: + this
1944: + "::=============== BEGIN TRAN ============ current connectionId: "
1945: + connectionId + " \n");
1946: } else {
1947: if (debug)
1948: System.out.println("julp ============= "
1949: + new java.util.Date() + " " + this .getClass()
1950: + "::" + this
1951: + "::=============== EJBContext: " + EJBContext
1952: + " \n");
1953: }
1954: }
1955:
1956: public void commitTran() throws SQLException {
1957: if (EJBContext == null) {
1958: if (debug)
1959: System.out
1960: .println("julp ============= "
1961: + new java.util.Date()
1962: + " "
1963: + this .getClass()
1964: + "::"
1965: + this
1966: + "::=============== COMMIT TRAN ============= current connectionId: "
1967: + connectionId + " \n");
1968: this .getConnection().commit();
1969: if (debug)
1970: System.out
1971: .println("julp ============= "
1972: + new java.util.Date()
1973: + " "
1974: + this .getClass()
1975: + "::"
1976: + this
1977: + "::=============== COMMITTED ================\n");
1978: this .getConnection().setAutoCommit(true);//?? Sybase bug
1979: this .setTranInProccess(false);
1980: this .setConnectionId("<none>");
1981: if (isProccessWarnings()) {
1982: populateWarnings(this .connection.getWarnings());
1983: this .connection.clearWarnings();
1984: }
1985: } else {
1986: if (debug)
1987: System.out.println("julp ============= "
1988: + new java.util.Date() + " " + this .getClass()
1989: + "::" + this
1990: + "::=============== EJBContext: " + EJBContext
1991: + " \n");
1992: }
1993: }
1994:
1995: public void rollbackTran() throws SQLException {
1996: if (EJBContext != null) {
1997: try {
1998: if (debug)
1999: System.out
2000: .println("julp ============= "
2001: + new java.util.Date()
2002: + " "
2003: + this .getClass()
2004: + "::"
2005: + this
2006: + "::=============== EJBContext: "
2007: + EJBContext
2008: + ".setRollbackOnly ============= connectionId: "
2009: + connectionId + " \n");
2010: Method m = EJBContext.getClass().getMethod(
2011: "setRollbackOnly", new Class[] {});
2012: m.invoke(EJBContext, new Object[] {});
2013: } catch (InvocationTargetException ete) {
2014: throw new SQLException(ete.getTargetException()
2015: .getMessage());
2016: } catch (Exception e) {
2017: throw new SQLException(e.getMessage());
2018: }
2019: } else {
2020: if (debug)
2021: System.out
2022: .println("julp ============= "
2023: + new java.util.Date()
2024: + " "
2025: + this .getClass()
2026: + "::"
2027: + this
2028: + "::=============== ROLLBACK TRAN ============= connectionId: "
2029: + connectionId + " \n");
2030: this .getConnection().rollback();
2031: if (debug)
2032: System.out
2033: .println("julp ============= "
2034: + new java.util.Date()
2035: + " "
2036: + this .getClass()
2037: + "::"
2038: + this
2039: + "::=============== AFTER ROLLBACK =========== connectionId: "
2040: + connectionId + " \n");
2041: this .getConnection().setAutoCommit(true);
2042: this .setTranInProccess(false);
2043: this .setConnectionId("<none>");
2044: if (isProccessWarnings()) {
2045: populateWarnings(this .connection.getWarnings());
2046: this .connection.clearWarnings();
2047: }
2048: }
2049: }
2050:
2051: /** Getter for property dataSourceName.
2052: * @return Value of property dataSourceName.
2053: *
2054: */
2055: public java.lang.String getDataSourceName() {
2056: return dataSourceName;
2057: }
2058:
2059: /** Setter for property dataSourceName.
2060: * @param dataSourceName New value of property dataSourceName.
2061: *
2062: */
2063: public void setDataSourceName(java.lang.String dataSourceName) {
2064: this .dataSourceName = dataSourceName;
2065: }
2066:
2067: /** Getter for property fetchSize.
2068: * @return Value of property fetchSize.
2069: *
2070: */
2071: public int getFetchSize() {
2072: return fetchSize;
2073: }
2074:
2075: /** Setter for property fetchSize.
2076: * @param fetchSize New value of property fetchSize.
2077: *
2078: */
2079: public void setFetchSize(int fetchSize) {
2080: this .fetchSize = fetchSize;
2081: }
2082:
2083: /** Getter for property fetchDirection.
2084: * @return Value of property fetchDirection.
2085: *
2086: */
2087: public int getFetchDirection() {
2088: return fetchDirection;
2089: }
2090:
2091: /** Setter for property fetchDirection.
2092: * @param fetchDirection New value of property fetchDirection.
2093: *
2094: */
2095: public void setFetchDirection(int fetchDirection) {
2096: this .fetchDirection = fetchDirection;
2097: }
2098:
2099: /** Getter for property queryTimeout.
2100: * @return Value of property queryTimeout.
2101: *
2102: */
2103: public int getQueryTimeout() {
2104: return queryTimeout;
2105: }
2106:
2107: /** Setter for property queryTimeout.
2108: * @param queryTimeout New value of property queryTimeout.
2109: *
2110: */
2111: public void setQueryTimeout(int queryTimeout) {
2112: this .queryTimeout = queryTimeout;
2113: }
2114:
2115: /** Getter for property maxRows.
2116: * @return Value of property maxRows.
2117: *
2118: */
2119: public int getMaxRows() {
2120: return maxRows;
2121: }
2122:
2123: /** Setter for property maxRows.
2124: * @param maxRows New value of property maxRows.
2125: *
2126: */
2127: public void setMaxRows(int maxRows) {
2128: this .maxRows = maxRows;
2129: }
2130:
2131: /** Getter for property maxFieldSize.
2132: * @return Value of property maxFieldSize.
2133: *
2134: */
2135: public int getMaxFieldSize() {
2136: return maxFieldSize;
2137: }
2138:
2139: /** Setter for property maxFieldSize.
2140: * @param maxFieldSize New value of property maxFieldSize.
2141: *
2142: */
2143: public void setMaxFieldSize(int maxFieldSize) {
2144: this .maxFieldSize = maxFieldSize;
2145: }
2146:
2147: /** Getter for property escapeProcessing.
2148: * @return Value of property escapeProcessing.
2149: *
2150: */
2151: public boolean getEscapeProcessing() {
2152: return escapeProcessing;
2153: }
2154:
2155: /** Setter for property escapeProcessing.
2156: * @param escapeProcessing New value of property escapeProcessing.
2157: *
2158: */
2159: public void setEscapeProcessing(boolean escapeProcessing) {
2160: this .escapeProcessing = escapeProcessing;
2161: }
2162:
2163: /** Getter for property resultSetType.
2164: * @return Value of property resultSetType.
2165: *
2166: */
2167: public int getResultSetType() {
2168: return resultSetType;
2169: }
2170:
2171: /** Setter for property resultSetType.
2172: * @param resultSetType New value of property resultSetType.
2173: *
2174: */
2175: public void setResultSetType(int resultSetType) {
2176: this .resultSetType = resultSetType;
2177: }
2178:
2179: /** Getter for property concurrency.
2180: * @return Value of property concurrency.
2181: *
2182: */
2183: public int getConcurrency() {
2184: return concurrency;
2185: }
2186:
2187: /** Setter for property concurrency.
2188: * @param concurrency New value of property concurrency.
2189: *
2190: */
2191: public void setConcurrency(int concurrency) {
2192: this .concurrency = concurrency;
2193: }
2194:
2195: /** Getter for property connectionClosed.
2196: * @return Value of property connectionClosed.
2197: *
2198: */
2199: public boolean isConnectionClosed() {
2200: return connectionClosed;
2201: }
2202:
2203: /** Setter for property connectionClosed.
2204: * @param connectionClosed New value of property connectionClosed.
2205: *
2206: */
2207: protected void setConnectionClosed(boolean connectionClosed) {
2208: this .connectionClosed = connectionClosed;
2209: }
2210:
2211: /** Getter for property maxRowsToProccesInTransaction.
2212: * @return Value of property maxRowsToProccesInTransaction.
2213: *
2214: */
2215: public int getMaxRowsToProccesInTransaction() {
2216: return maxRowsToProccesInTransaction;
2217: }
2218:
2219: /** Setter for property maxRowsToProccesInTransaction.
2220: * @param maxRowsToProccesInTransaction New value of property maxRowsToProccesInTransaction.
2221: *
2222: */
2223: public void setMaxRowsToProccesInTransaction(
2224: int maxRowsToProccesInTransaction) throws SQLException {
2225: if (maxRowsToProccesInTransaction < 0) {
2226: throw new SQLException(
2227: "Invalid maxRowsToProccesInTransaction value");
2228: }
2229: this .maxRowsToProccesInTransaction = maxRowsToProccesInTransaction;
2230: }
2231:
2232: /** Getter for property rowsAffectedInTransaction.
2233: * @return Value of property rowsAffectedInTransaction.
2234: *
2235: */
2236: public int getRowsAffectedInTransaction() {
2237: return rowsAffectedInTransaction;
2238: }
2239:
2240: /** Setter for property rowsAffectedInTransaction.
2241: * @param rowsAffectedInTransaction New value of property rowsAffectedInTransaction.
2242: *
2243: */
2244: public void setRowsAffectedInTransaction(
2245: int rowsAffectedInTransaction) throws SQLException {
2246: this .rowsAffectedInTransaction = rowsAffectedInTransaction;
2247: if (this .maxRowsToProccesInTransaction != 0
2248: && this .rowsAffectedInTransaction >= this .maxRowsToProccesInTransaction) {
2249: try {
2250: this .commitTran();
2251: this .release(false);
2252: this .beginTran();
2253: } catch (SQLException e) {
2254: this .rollbackTran();
2255: throw e;
2256: }
2257: }
2258: }
2259:
2260: /** Getter for property connectionProperties.
2261: * @return Value of property connectionProperties.
2262: *
2263: */
2264: public java.util.Properties getConnectionProperties() {
2265: return connectionProperties;
2266: }
2267:
2268: /** Setter for property connectionProperties.
2269: * @param connectionProperties New value of property connectionProperties.
2270: *
2271: */
2272: public void setConnectionProperties(
2273: java.util.Properties connectionProperties) {
2274: this .connectionProperties = connectionProperties;
2275: }
2276:
2277: /** Getter for property closeStatementAfterExecute.
2278: * @return Value of property closeStatementAfterExecute.
2279: *
2280: */
2281: public boolean isCloseStatementAfterExecute() {
2282: return closeStatementAfterExecute;
2283: }
2284:
2285: /** Setter for property closeStatementAfterExecute.
2286: * @param closeStatementAfterExecute New value of property closeStatementAfterExecute.
2287: *
2288: */
2289: public void setCloseStatementAfterExecute(
2290: boolean closeStatementAfterExecute) {
2291: this .closeStatementAfterExecute = closeStatementAfterExecute;
2292: }
2293:
2294: /** Getter for property connectionId.
2295: * @return Value of property connectionId.
2296: *
2297: */
2298: public java.lang.String getConnectionId() {
2299: return connectionId;
2300: }
2301:
2302: /** Setter for property connectionId.
2303: * @param connectionId New value of property connectionId.
2304: *
2305: */
2306: public void setConnectionId(java.lang.String connectionId) {
2307: this .connectionId = connectionId;
2308: }
2309:
2310: /** Getter for property tranInProccess.
2311: * @return Value of property tranInProccess.
2312: *
2313: */
2314: public boolean isTranInProccess() {
2315: return tranInProccess;
2316: }
2317:
2318: /** Setter for property tranInProccess.
2319: * @param tranInProccess New value of property tranInProccess.
2320: *
2321: */
2322: public void setTranInProccess(boolean tranInProccess) {
2323: this .tranInProccess = tranInProccess;
2324: }
2325:
2326: /**
2327: * Getter for property debug.
2328: * @return Value of property debug.
2329: */
2330: public boolean isDebug() {
2331: return debug;
2332: }
2333:
2334: /**
2335: * Setter for property debug.
2336: * @param debug New value of property debug.
2337: */
2338: public void setDebug(boolean debug) {
2339: this .debug = debug;
2340: }
2341:
2342: /**
2343: * Getter for property resetDataSource.
2344: * @return Value of property resetDataSource.
2345: */
2346: public boolean isResetDataSource() {
2347: return resetDataSource;
2348: }
2349:
2350: /**
2351: * Setter for property resetDataSource.
2352: * @param resetDataSource New value of property resetDataSource.
2353: */
2354: public void setResetDataSource(boolean resetDataSource) {
2355: this .resetDataSource = resetDataSource;
2356: }
2357:
2358: public java.lang.String getPlaceHolder() {
2359: return placeHolder;
2360: }
2361:
2362: public void setPlaceHolder(java.lang.String placeHolder) {
2363: this .placeHolder = placeHolder;
2364: }
2365:
2366: public int getMaxNumberOfParams() {
2367: return maxNumberOfParams;
2368: }
2369:
2370: public void setMaxNumberOfParams(int maxNumberOfParams) {
2371: this .maxNumberOfParams = maxNumberOfParams;
2372: }
2373:
2374: public List getWarnings() {
2375: return (warnings == null) ? new ArrayList() : warnings;
2376: }
2377:
2378: public void setProccessWarnings(boolean proccessWarnings) {
2379: if (proccessWarnings) {
2380: warnings = new ArrayList();
2381: } else {
2382: warnings.clear();
2383: warnings = null;
2384: }
2385: this .proccessWarnings = proccessWarnings;
2386: }
2387:
2388: public boolean isProccessWarnings() {
2389: return this .proccessWarnings;
2390: }
2391:
2392: /** Make sure to read warnings before release() */
2393: public void populateWarnings(SQLWarning warn) {
2394: while (warn != null) {
2395: DataHolder holder = new DataHolder(3);
2396: holder
2397: .setFieldNameAndValue(1, "message", warn
2398: .getMessage());
2399: holder.setFieldNameAndValue(2, "SQLState", warn
2400: .getSQLState());
2401: holder.setFieldNameAndValue(3, "vendorErrorCode",
2402: new Integer(warn.getErrorCode()));
2403: warnings.add(holder);
2404: warn = warn.getNextWarning();
2405: }
2406: }
2407:
2408: //==========================================================================
2409:
2410: public boolean isCacheStatements() {
2411: return cacheStatements;
2412: }
2413:
2414: public void clearStatementCache() {
2415: if (statementsCache != null) {
2416: try {
2417: Iterator iter = statementsCache.iterator();
2418: while (iter.hasNext()) {
2419: Object obj = iter.next();
2420: if (obj != null) {
2421: ((Statement) obj).close();
2422: }
2423: }
2424: statementsCache.clear();
2425: statementsCacheCounter.clear();
2426: statementsId.clear();
2427: if (!cacheStatements) {
2428: statementsCacheCounter = null;
2429: statementsCache = null;
2430: statementsId = null;
2431: }
2432: } catch (SQLException sqle) {
2433: sqle.printStackTrace();
2434: }
2435: }
2436: }
2437:
2438: public void setCacheStatements(boolean cacheStatements) {
2439: if (debug)
2440: System.out
2441: .println("julp ============= "
2442: + new java.util.Date()
2443: + " "
2444: + this .getClass()
2445: + "::"
2446: + this
2447: + "::=============== setCacheStatements() ============= cacheStatements: "
2448: + cacheStatements + " \n");
2449: if (cacheStatements) {
2450: if (statementsCache == null) {
2451: statementsCache = new ArrayList(maxStatementCacheSize);
2452: statementsId = new ArrayList(maxStatementCacheSize);
2453: statementsCacheCounter = new ArrayList(
2454: maxStatementCacheSize);
2455: }
2456: setCloseStatementAfterExecute(false);
2457: } else {
2458: clearStatementCache();
2459: }
2460: this .cacheStatements = cacheStatements;
2461: }
2462:
2463: public int getMaxStatementCacheSize() {
2464: return this .maxStatementCacheSize;
2465: }
2466:
2467: public void setMaxStatementCacheSize(int maxStatementCacheSize) {
2468: this .maxStatementCacheSize = maxStatementCacheSize;
2469: }
2470:
2471: protected synchronized int addCachedStatement(String statementId,
2472: PreparedStatement statementToCache) throws SQLException {
2473: if (debug)
2474: System.out
2475: .println("julp ============= "
2476: + new java.util.Date()
2477: + " "
2478: + this .getClass()
2479: + "::"
2480: + this
2481: + "::=============== addCachedStatement() ============= statementId: "
2482: + statementId + "::statementToCache: "
2483: + statementToCache + "\n");
2484: if (statementsId.isEmpty()) {
2485: statementsId.add(statementId);
2486: statementsCache.add(statement);
2487: return 0;
2488: }
2489: int idx = statementsId.indexOf(statementId);
2490: if (idx < 0) {
2491: if (statementsId.size() + 1 == maxStatementCacheSize) {
2492: // find and remove the list often required Statement
2493: int iterCount = 0;
2494: int theListOftenRequiredStatementIndex = 0;
2495: int theListOftenRequiredStatement = ((Integer) statementsCacheCounter
2496: .get(0)).intValue();
2497: Iterator iter = statementsCacheCounter.iterator();
2498: while (iter.hasNext()) {
2499: int currValue = ((Integer) iter.next()).intValue();
2500: if (currValue < theListOftenRequiredStatement) {
2501: theListOftenRequiredStatement = currValue;
2502: theListOftenRequiredStatementIndex = iterCount;
2503: }
2504: iterCount++;
2505: }
2506: statementsId.remove(theListOftenRequiredStatementIndex);
2507: if (debug)
2508: System.out
2509: .println("julp ============= "
2510: + new java.util.Date()
2511: + " "
2512: + this .getClass()
2513: + "::"
2514: + this
2515: + "::addCachedStatement()::closing PreparedStatement: "
2516: + ((Statement) statementsCache
2517: .get(theListOftenRequiredStatementIndex))
2518: + " \n");
2519: ((Statement) statementsCache
2520: .get(theListOftenRequiredStatementIndex))
2521: .close();
2522: statementsCache
2523: .remove(theListOftenRequiredStatementIndex);
2524: statementsCacheCounter
2525: .remove(theListOftenRequiredStatementIndex);
2526: if (debug)
2527: System.out
2528: .println("julp ============= "
2529: + new java.util.Date()
2530: + " "
2531: + this .getClass()
2532: + "::"
2533: + this
2534: + "::=============== addCachedStatement()::remove ============= theListOftenRequiredStatementIndex: "
2535: + theListOftenRequiredStatementIndex
2536: + "\n");
2537: }
2538: statementsId.add(statementId);
2539: if (debug)
2540: System.out
2541: .println("julp ============= "
2542: + new java.util.Date()
2543: + " "
2544: + this .getClass()
2545: + "::"
2546: + this
2547: + "::=============== addCachedStatement()::statementToCache: "
2548: + statementToCache + " "
2549: + statementToCache.getClass() + "\n");
2550: if (debug)
2551: System.out
2552: .println("julp ============= "
2553: + new java.util.Date()
2554: + " "
2555: + this .getClass()
2556: + "::"
2557: + this
2558: + "::=============== addCachedStatement()::statementToCache instanceof PreparedStatement: "
2559: + (statementToCache instanceof PreparedStatement)
2560: + "\n");
2561: if (statementToCache instanceof PreparedStatement) {
2562: statementsCache
2563: .add((PreparedStatement) statementToCache);
2564: } else {
2565: statementsCache.add(statementToCache);
2566: }
2567: idx = statementsId.size() - 1;
2568: } else {
2569: // statement is already in cache, do nothing
2570: }
2571: return idx;
2572: }
2573:
2574: protected synchronized int addCachedStatement(String statementId,
2575: Statement statementToCache) throws SQLException {
2576: if (debug)
2577: System.out
2578: .println("julp ============= "
2579: + new java.util.Date()
2580: + " "
2581: + this .getClass()
2582: + "::"
2583: + this
2584: + "::=============== addCachedStatement() ============= statementId: "
2585: + statementId + "::statementToCache: "
2586: + statementToCache + "\n");
2587: if (statementsId.isEmpty()) {
2588: statementsId.add(statementId);
2589: statementsCache.add(statement);
2590: return 0;
2591: }
2592: int idx = statementsId.indexOf(statementId);
2593: if (idx < 0) {
2594: if (statementsId.size() + 1 == maxStatementCacheSize) {
2595: // find and remove the list often required Statement
2596: int iterCount = 0;
2597: int theListOftenRequiredStatementIndex = 0;
2598: int theListOftenRequiredStatement = ((Integer) statementsCacheCounter
2599: .get(0)).intValue();
2600: Iterator iter = statementsCacheCounter.iterator();
2601: while (iter.hasNext()) {
2602: int currValue = ((Integer) iter.next()).intValue();
2603: if (currValue < theListOftenRequiredStatement) {
2604: theListOftenRequiredStatement = currValue;
2605: theListOftenRequiredStatementIndex = iterCount;
2606: }
2607: iterCount++;
2608: }
2609: statementsId.remove(theListOftenRequiredStatementIndex);
2610: if (debug)
2611: System.out
2612: .println("julp ============= "
2613: + new java.util.Date()
2614: + " "
2615: + this .getClass()
2616: + "::"
2617: + this
2618: + "::addCachedStatement()::closing Statement: "
2619: + ((Statement) statementsCache
2620: .get(theListOftenRequiredStatementIndex))
2621: + " \n");
2622: ((Statement) statementsCache
2623: .get(theListOftenRequiredStatementIndex))
2624: .close();
2625: statementsCache
2626: .remove(theListOftenRequiredStatementIndex);
2627: statementsCacheCounter
2628: .remove(theListOftenRequiredStatementIndex);
2629: if (debug)
2630: System.out
2631: .println("julp ============= "
2632: + new java.util.Date()
2633: + " "
2634: + this .getClass()
2635: + "::"
2636: + this
2637: + "::=============== addCachedStatement()::remove ============= theListOftenRequiredStatementIndex: "
2638: + theListOftenRequiredStatementIndex
2639: + "\n");
2640: }
2641: statementsId.add(statementId);
2642: if (debug)
2643: System.out
2644: .println("julp ============= "
2645: + new java.util.Date()
2646: + " "
2647: + this .getClass()
2648: + "::"
2649: + this
2650: + "::=============== addCachedStatement()::statementToCache: "
2651: + statementToCache + " "
2652: + statementToCache.getClass() + "\n");
2653: if (debug)
2654: System.out
2655: .println("julp ============= "
2656: + new java.util.Date()
2657: + " "
2658: + this .getClass()
2659: + "::"
2660: + this
2661: + "::=============== addCachedStatement()::statementToCache instanceof PreparedStatement: "
2662: + (statementToCache instanceof PreparedStatement)
2663: + "\n");
2664: if (statementToCache instanceof PreparedStatement) {
2665: statementsCache
2666: .add((PreparedStatement) statementToCache);
2667: } else {
2668: statementsCache.add(statementToCache);
2669: }
2670: idx = statementsId.size() - 1;
2671: } else {
2672: // statement is already in cache, do nothing
2673: }
2674: return idx;
2675: }
2676:
2677: public String getCachedRowSetClassName() {
2678: return cachedRowSetClassName;
2679: }
2680:
2681: public void setCachedRowSetClassName(String cachedRowSetClassName) {
2682: this .cachedRowSetClassName = cachedRowSetClassName;
2683: }
2684:
2685: public void setCloseResultSet(boolean closeResultSet) {
2686: this .closeResultSet = closeResultSet;
2687: }
2688:
2689: public boolean isCloseResultSet() {
2690: return this .closeResultSet;
2691: }
2692:
2693: public String getCursorName() {
2694: return cursorName;
2695: }
2696:
2697: public void setCursorName(String cursorName) {
2698: this .cursorName = cursorName;
2699: }
2700:
2701: public char getProccessTransactionOnCloseConnection() {
2702: return proccessTransactionOnCloseConnection;
2703: }
2704:
2705: public void setProccessTransactionOnCloseConnection(
2706: char proccessTransactionOnCloseConnection) {
2707: this .proccessTransactionOnCloseConnection = proccessTransactionOnCloseConnection;
2708: }
2709:
2710: public Object getEJBContext() {
2711: return EJBContext;
2712: }
2713:
2714: public void setEJBContext(Object EJBContext) {
2715: this.EJBContext = EJBContext;
2716: }
2717: }
|