KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.mchange.v2.coalesce.*;
30
31 final class MemoryCoalescedStatementCacheKey extends StatementCacheKey
32 {
33     //MT: not thread-safe, but protected within the find() method
34
// by StatementCacheKey.class lock
35
final static Coalescer keyCoalescer = CoalescerFactory.createCoalescer( true, false );
36
37     static StatementCacheKey _find( Connection JavaDoc pcon, Method JavaDoc stmtProducingMethod, Object JavaDoc[] args )
38     {
39     ///BEGIN FIND LOGIC///
40
String JavaDoc stmtText = (String JavaDoc) args[0];
41     boolean is_callable = stmtProducingMethod.getName().equals("prepareCall");
42     int result_set_type;
43     int result_set_concurrency;
44
45     int[] columnIndexes;
46     String JavaDoc[] columnNames;
47     Integer JavaDoc autogeneratedKeys;
48     Integer JavaDoc resultSetHoldability;
49
50     if (args.length == 1)
51         {
52         result_set_type = ResultSet.TYPE_FORWARD_ONLY;
53         result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
54         columnIndexes = null;
55         columnNames = null;
56         autogeneratedKeys = null;
57         resultSetHoldability = null;
58         }
59     else if (args.length == 2)
60         {
61         Class JavaDoc[] argTypes = stmtProducingMethod.getParameterTypes();
62         if (argTypes[1].isArray())
63             {
64             Class JavaDoc baseType = argTypes[1].getComponentType();
65             if (baseType == int.class) //second arg is columnIndexes
66
{
67                 result_set_type = ResultSet.TYPE_FORWARD_ONLY;
68                 result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
69                 columnIndexes = (int[]) args[1];
70                 columnNames = null;
71                 autogeneratedKeys = null;
72                 resultSetHoldability = null;
73                 }
74             else if (baseType == String JavaDoc.class)
75                 {
76                 result_set_type = ResultSet.TYPE_FORWARD_ONLY;
77                 result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
78                 columnIndexes = null;
79                 columnNames = (String JavaDoc[]) args[1];
80                 autogeneratedKeys = null;
81                 resultSetHoldability = null;
82                 }
83             else
84                 throw new IllegalArgumentException JavaDoc("c3p0 probably needs to be updated for some new " +
85                                    "JDBC spec! As of JDBC3, we expect two arg statement " +
86                                    "producing methods where the second arg is either " +
87                                    "an int, int array, or String array.");
88             }
89         else //it should be a boxed int, autogeneratedKeys
90
{
91             result_set_type = ResultSet.TYPE_FORWARD_ONLY;
92             result_set_concurrency = ResultSet.CONCUR_READ_ONLY;
93             columnIndexes = null;
94             columnNames = null;
95             autogeneratedKeys = (Integer JavaDoc) args[1];
96             resultSetHoldability = null;
97             }
98         }
99     else if (args.length == 3)
100         {
101         result_set_type = ((Integer JavaDoc) args[1]).intValue();
102         result_set_concurrency = ((Integer JavaDoc) args[2]).intValue();
103         columnIndexes = null;
104         columnNames = null;
105         autogeneratedKeys = null;
106         resultSetHoldability = null;
107         }
108     else if (args.length == 4)
109         {
110         result_set_type = ((Integer JavaDoc) args[1]).intValue();
111         result_set_concurrency = ((Integer JavaDoc) args[2]).intValue();
112         columnIndexes = null;
113         columnNames = null;
114         autogeneratedKeys = null;
115         resultSetHoldability = (Integer JavaDoc) args[3];
116         }
117     else
118         throw new IllegalArgumentException JavaDoc("Unexpected number of args to " +
119                            stmtProducingMethod.getName() );
120     ///END FIND LOGIC///
121

122
123     StatementCacheKey uncanonical
124         = new MemoryCoalescedStatementCacheKey( pcon,
125                              stmtText,
126                              is_callable,
127                              result_set_type,
128                              result_set_concurrency,
129                              columnIndexes,
130                              columnNames,
131                              autogeneratedKeys,
132                              resultSetHoldability );
133     return (StatementCacheKey) keyCoalescer.coalesce( uncanonical );
134     }
135
136     MemoryCoalescedStatementCacheKey( Connection JavaDoc physicalConnection,
137                       String JavaDoc stmtText,
138                       boolean is_callable,
139                       int result_set_type,
140                       int result_set_concurrency,
141                       int[] columnIndexes,
142                       String JavaDoc[] columnNames,
143                       Integer JavaDoc autogeneratedKeys,
144                       Integer JavaDoc resultSetHoldability )
145     {
146     super( physicalConnection,
147            stmtText,
148            is_callable,
149            result_set_type,
150            result_set_concurrency,
151            columnIndexes,
152            columnNames,
153            autogeneratedKeys,
154            resultSetHoldability );
155     }
156
157     public boolean equals( Object JavaDoc o )
158     { return StatementCacheKey.equals( this, o ); }
159
160     public int hashCode()
161     { return StatementCacheKey.hashCode( this ); }
162 }
163
164
165
166
Popular Tags