KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > stmt > StatementCacheKey


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.stmt;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import com.mchange.v1.util.ArrayUtils;
31 import com.mchange.v2.lang.ObjectUtils;
32
33 abstract class StatementCacheKey
34 {
35     static final int SIMPLE = 0;
36     static final int MEMORY_COALESCED = 1;
37     static final int VALUE_IDENTITY = 2;
38
39     //NOTE: subclasses rely upon their _find logic being protected by StatementCacheKey.class' lock!
40
public synchronized static StatementCacheKey find( Connection JavaDoc pcon, Method JavaDoc stmtProducingMethod, Object JavaDoc[] args )
41     {
42     switch ( VALUE_IDENTITY )
43         {
44         case SIMPLE:
45         return SimpleStatementCacheKey._find( pcon, stmtProducingMethod, args );
46         case MEMORY_COALESCED:
47         return MemoryCoalescedStatementCacheKey._find( pcon, stmtProducingMethod, args );
48         case VALUE_IDENTITY:
49         return ValueIdentityStatementCacheKey._find( pcon, stmtProducingMethod, args );
50         default:
51         throw new InternalError JavaDoc("StatementCacheKey.find() is misconfigured.");
52         }
53     }
54
55     //MT: instances are treated as immutable once they
56
// have been initialized and handed to
57
// a client. (Factories may reinitialize
58
// instances that never get released to
59
// clients -- those factories must prevent
60
// concurrent access to these recycled,
61
// nascent keys.)
62
Connection JavaDoc physicalConnection;
63     String JavaDoc stmtText;
64     boolean is_callable;
65     int result_set_type;
66     int result_set_concurrency;
67
68     int[] columnIndexes; //jdbc3, null means default
69
String JavaDoc[] columnNames; //jdbc3, null means default
70

71     Integer JavaDoc autogeneratedKeys; //jdbc3, null means driver default, which the spec does not sepcify
72
Integer JavaDoc resultSetHoldability; //jdbc3, null means driver default, which the spec does not sepcify
73

74     StatementCacheKey()
75     {}
76
77     StatementCacheKey( Connection JavaDoc physicalConnection,
78                String JavaDoc stmtText,
79                boolean is_callable,
80                int result_set_type,
81                int result_set_concurrency,
82                int[] columnIndexes,
83                String JavaDoc[] columnNames,
84                Integer JavaDoc autogeneratedKeys,
85                Integer JavaDoc resultSetHoldability )
86     {
87     init( physicalConnection,
88           stmtText,
89           is_callable,
90           result_set_type,
91           result_set_concurrency,
92           columnIndexes,
93           columnNames,
94           autogeneratedKeys,
95           resultSetHoldability
96           );
97     }
98
99     void init( Connection JavaDoc physicalConnection,
100            String JavaDoc stmtText,
101            boolean is_callable,
102            int result_set_type,
103            int result_set_concurrency,
104            int[] columnIndexes, //jdbc3
105
String JavaDoc[] columnNames, //jdbc3
106
Integer JavaDoc autogeneratedKeys, //jdbc3
107
Integer JavaDoc resultSetHoldability) //jdbc3
108
{
109     this.physicalConnection = physicalConnection;
110     this.stmtText = stmtText;
111     this.is_callable = is_callable;
112     this.result_set_type = result_set_type;
113     this.result_set_concurrency = result_set_concurrency;
114     this.columnIndexes = columnIndexes;
115     this.columnNames = columnNames;
116     this.autogeneratedKeys = autogeneratedKeys;
117     this.resultSetHoldability = resultSetHoldability;
118     }
119     
120     static boolean equals(StatementCacheKey _this, Object JavaDoc o)
121     {
122     //TODO: assert( _this != null )
123

124     if ( _this == o )
125         return true;
126     if (o instanceof StatementCacheKey)
127         {
128         StatementCacheKey sck = (StatementCacheKey) o;
129
130 // System.err.println( sck.physicalConnection + " " +
131
// _this.physicalConnection + " equals? " +
132
// sck.physicalConnection.equals( _this.physicalConnection ) );
133

134         return
135             sck.physicalConnection.equals(_this.physicalConnection) &&
136             sck.stmtText.equals(_this.stmtText) &&
137             sck.is_callable == _this.is_callable &&
138             sck.result_set_type == _this.result_set_type &&
139             sck.result_set_concurrency == _this.result_set_concurrency &&
140             Arrays.equals( sck.columnIndexes, _this.columnIndexes ) &&
141             Arrays.equals( sck.columnNames, _this.columnNames ) &&
142             ObjectUtils.eqOrBothNull( sck.autogeneratedKeys, _this.autogeneratedKeys ) &&
143             ObjectUtils.eqOrBothNull( sck.resultSetHoldability, _this.resultSetHoldability );
144         }
145     else
146         return false;
147     }
148     
149     static int hashCode(StatementCacheKey _this)
150     {
151     return
152         _this.physicalConnection.hashCode() ^
153         _this.stmtText.hashCode() ^
154         (_this.is_callable ? 1 : 0) ^
155         _this.result_set_type ^
156         _this.result_set_concurrency ^
157         ArrayUtils.hashOrZeroArray( _this.columnIndexes ) ^
158         ArrayUtils.hashOrZeroArray( _this.columnNames ) ^
159         ObjectUtils.hashOrZero( _this.autogeneratedKeys ) ^ //this is okay -- genuine constants are non-zer0
160
ObjectUtils.hashOrZero( _this.resultSetHoldability ); //this is okay -- genuine constants are non-zer0
161
}
162
163     public String JavaDoc toString()
164     {
165     StringBuffer JavaDoc out = new StringBuffer JavaDoc(128);
166     out.append("[" + this.getClass().getName() + ": ");
167     out.append("physicalConnection->" + physicalConnection);
168     out.append(", stmtText->" + stmtText);
169     out.append(", is_callable->" + is_callable);
170     out.append(", result_set_type->" + result_set_type);
171     out.append(", result_set_concurrency->" + result_set_concurrency);
172     out.append(", columnIndexes->" + columnIndexes);
173     out.append(", columnNames->" + columnNames);
174     out.append(", autogeneratedKeys->" + autogeneratedKeys);
175     out.append(", resultSetHoldability->" + resultSetHoldability);
176     out.append(']');
177     return out.toString();
178     }
179 }
180
181
182
Popular Tags