001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.stmt;
024:
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.lang.reflect.Method;
028: import com.mchange.v2.coalesce.*;
029:
030: final class MemoryCoalescedStatementCacheKey extends StatementCacheKey {
031: //MT: not thread-safe, but protected within the find() method
032: // by StatementCacheKey.class lock
033: final static Coalescer keyCoalescer = CoalescerFactory
034: .createCoalescer(true, false);
035:
036: static StatementCacheKey _find(Connection pcon,
037: Method stmtProducingMethod, Object[] args) {
038: ///BEGIN FIND LOGIC///
039: String stmtText = (String) args[0];
040: boolean is_callable = stmtProducingMethod.getName().equals(
041: "prepareCall");
042: int result_set_type;
043: int result_set_concurrency;
044:
045: int[] columnIndexes;
046: String[] columnNames;
047: Integer autogeneratedKeys;
048: Integer resultSetHoldability;
049:
050: if (args.length == 1) {
051: result_set_type = ResultSet.TYPE_FORWARD_ONLY;
052: result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
053: columnIndexes = null;
054: columnNames = null;
055: autogeneratedKeys = null;
056: resultSetHoldability = null;
057: } else if (args.length == 2) {
058: Class[] argTypes = stmtProducingMethod.getParameterTypes();
059: if (argTypes[1].isArray()) {
060: Class baseType = argTypes[1].getComponentType();
061: if (baseType == int.class) //second arg is columnIndexes
062: {
063: result_set_type = ResultSet.TYPE_FORWARD_ONLY;
064: result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
065: columnIndexes = (int[]) args[1];
066: columnNames = null;
067: autogeneratedKeys = null;
068: resultSetHoldability = null;
069: } else if (baseType == String.class) {
070: result_set_type = ResultSet.TYPE_FORWARD_ONLY;
071: result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
072: columnIndexes = null;
073: columnNames = (String[]) args[1];
074: autogeneratedKeys = null;
075: resultSetHoldability = null;
076: } else
077: throw new IllegalArgumentException(
078: "c3p0 probably needs to be updated for some new "
079: + "JDBC spec! As of JDBC3, we expect two arg statement "
080: + "producing methods where the second arg is either "
081: + "an int, int array, or String array.");
082: } else //it should be a boxed int, autogeneratedKeys
083: {
084: result_set_type = ResultSet.TYPE_FORWARD_ONLY;
085: result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
086: columnIndexes = null;
087: columnNames = null;
088: autogeneratedKeys = (Integer) args[1];
089: resultSetHoldability = null;
090: }
091: } else if (args.length == 3) {
092: result_set_type = ((Integer) args[1]).intValue();
093: result_set_concurrency = ((Integer) args[2]).intValue();
094: columnIndexes = null;
095: columnNames = null;
096: autogeneratedKeys = null;
097: resultSetHoldability = null;
098: } else if (args.length == 4) {
099: result_set_type = ((Integer) args[1]).intValue();
100: result_set_concurrency = ((Integer) args[2]).intValue();
101: columnIndexes = null;
102: columnNames = null;
103: autogeneratedKeys = null;
104: resultSetHoldability = (Integer) args[3];
105: } else
106: throw new IllegalArgumentException(
107: "Unexpected number of args to "
108: + stmtProducingMethod.getName());
109: ///END FIND LOGIC///
110:
111: StatementCacheKey uncanonical = new MemoryCoalescedStatementCacheKey(
112: pcon, stmtText, is_callable, result_set_type,
113: result_set_concurrency, columnIndexes, columnNames,
114: autogeneratedKeys, resultSetHoldability);
115: return (StatementCacheKey) keyCoalescer.coalesce(uncanonical);
116: }
117:
118: MemoryCoalescedStatementCacheKey(Connection physicalConnection,
119: String stmtText, boolean is_callable, int result_set_type,
120: int result_set_concurrency, int[] columnIndexes,
121: String[] columnNames, Integer autogeneratedKeys,
122: Integer resultSetHoldability) {
123: super (physicalConnection, stmtText, is_callable,
124: result_set_type, result_set_concurrency, columnIndexes,
125: columnNames, autogeneratedKeys, resultSetHoldability);
126: }
127:
128: public boolean equals(Object o) {
129: return StatementCacheKey.equals(this , o);
130: }
131:
132: public int hashCode() {
133: return StatementCacheKey.hashCode(this);
134: }
135: }
|