KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > OracleOciCollection


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.quercus.lib.db;
31
32 import com.caucho.quercus.annotation.ReturnNullAsFalse;
33 import com.caucho.quercus.env.BooleanValue;
34 import com.caucho.quercus.env.Env;
35 import com.caucho.quercus.env.LongValue;
36 import com.caucho.quercus.env.Value;
37 import com.caucho.util.L10N;
38
39 import java.lang.reflect.Constructor JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.sql.Array JavaDoc;
42 import java.sql.Connection JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47
48 /**
49  * Quercus Oracle OCI-Collection object oriented API.
50  */

51 public class OracleOciCollection {
52   private static final Logger JavaDoc log = Logger.getLogger(OracleOciCollection.class.getName());
53   private static final L10N L = new L10N(OracleOciCollection.class);
54
55   // The Oracle array descriptor
56
private Object JavaDoc _arrayDescriptor;
57
58   // The Oracle collection
59
private Array JavaDoc _collection;
60
61   // The cached Java collection
62
private ArrayList JavaDoc _javaCollection;
63
64   // The Oracle JDBC connection
65
private Connection JavaDoc _jdbcConn;
66
67   // Cache class oracle.sql.ARRAY
68
private static Class JavaDoc classOracleARRAY;
69
70   static {
71     try {
72       classOracleARRAY = Class.forName("oracle.sql.ARRAY");
73     } catch (Exception JavaDoc e) {
74       L.l("Unable to load ARRAY class oracle.sql.ARRAY.");
75     }
76   }
77
78   /**
79    * Constructor for OracleOciCollection
80    */

81   OracleOciCollection(Connection JavaDoc jdbcConn,
82                       Object JavaDoc arrayDescriptor)
83   {
84     _jdbcConn = jdbcConn;
85
86     _arrayDescriptor = arrayDescriptor;
87
88     _collection = null;
89
90     _javaCollection = new ArrayList JavaDoc();
91   }
92
93   /**
94    * Appends element to the collection
95    *
96    * @param value can be a string or a number
97    */

98   public boolean append(Env env,
99                         Value value)
100   {
101     try {
102
103       _javaCollection.add(value.toJavaObject());
104
105       return true;
106
107     } catch (Exception JavaDoc ex) {
108       log.log(Level.FINE, ex.toString(), ex);
109       return false;
110     }
111   }
112
113   /**
114    * Assigns a value to the collection from another existing collection
115    */

116   public boolean assign(Env env,
117                         OracleOciCollection fromCollection)
118   {
119     try {
120
121       _javaCollection.addAll(fromCollection.getJavaCollection());
122
123       return true;
124
125     } catch (Exception JavaDoc ex) {
126       log.log(Level.FINE, ex.toString(), ex);
127       return false;
128     }
129   }
130
131   /**
132    * Assigns a value to the element of the collection
133    *
134    * @param index 1-based index
135    * @param value can be a string or a number
136    */

137   public boolean assignElem(Env env,
138                             int index,
139                             Value value)
140   {
141     try {
142
143       if ((index < 1) || (index > _javaCollection.size())) {
144         return false;
145       }
146
147       _javaCollection.set(index-1, value.toJavaObject());
148
149       return true;
150
151     } catch (Exception JavaDoc ex) {
152       log.log(Level.FINE, ex.toString(), ex);
153       return false;
154     }
155   }
156
157   /**
158    * Frees the resources associated with the collection object
159    */

160   public boolean free(Env env)
161   {
162     try {
163
164       _collection = null;
165
166       _javaCollection = null;
167
168       return true;
169
170     } catch (Exception JavaDoc ex) {
171       log.log(Level.FINE, ex.toString(), ex);
172       return false;
173     }
174   }
175
176   /**
177    * Returns the underlying Oracle collection
178    */

179   protected Array JavaDoc getCollection()
180   {
181     try {
182
183       // oracle.sql.ARRAY array = new oracle.sql.ARRAY(arrayDesc, jdbcConn, arrayValues);
184

185       Class JavaDoc clArrayDescriptor = Class.forName("oracle.sql.ArrayDescriptor");
186
187       Constructor JavaDoc constructor = classOracleARRAY.getDeclaredConstructor(new Class JavaDoc[]
188         {clArrayDescriptor, Connection JavaDoc.class, Object JavaDoc.class});
189
190       Object JavaDoc elements[] = _javaCollection.toArray();
191
192       _collection = (Array JavaDoc) constructor.newInstance(new Object JavaDoc[]
193         {_arrayDescriptor, _jdbcConn, elements});
194
195       if (_collection != null) {
196         // Optimization
197
Method JavaDoc setAutoBuffering
198           = classOracleARRAY.getDeclaredMethod("setAutoBuffering",
199                                                new Class JavaDoc[] {Boolean.TYPE});
200         setAutoBuffering.invoke(_collection, new Object JavaDoc[] {true});
201       }
202
203       return _collection;
204
205     } catch (Exception JavaDoc ex) {
206       log.log(Level.FINE, ex.toString(), ex);
207       return null;
208     }
209   }
210
211   /**
212    * Returns value of the element by index (1-based)
213    */

214   public Value getElem(Env env,
215                        int index)
216   {
217     try {
218
219       if ((index < 1) || (index > _javaCollection.size())) {
220         return BooleanValue.FALSE;
221       }
222
223       return env.wrapJava(_javaCollection.get(index-1));
224
225     } catch (Exception JavaDoc ex) {
226       log.log(Level.FINE, ex.toString(), ex);
227       return BooleanValue.FALSE;
228     }
229   }
230
231   /**
232    * Returns the underlying Java collection
233    */

234   protected ArrayList JavaDoc getJavaCollection()
235   {
236     return _javaCollection;
237   }
238
239   /**
240    * Returns the maximum number of elements in the collection
241    * If the returned value is 0, then the number of elements
242    * is not limited. Returns FALSE in case of error.
243    */

244   @ReturnNullAsFalse
245   public LongValue max(Env env)
246   {
247     try {
248
249       return LongValue.create(0);
250
251     } catch (Exception JavaDoc ex) {
252       log.log(Level.FINE, ex.toString(), ex);
253       return null;
254     }
255   }
256
257   /**
258    * Returns size of the collection
259    */

260   @ReturnNullAsFalse
261   public LongValue size(Env env)
262   {
263     try {
264
265       return LongValue.create(_javaCollection.size());
266
267     } catch (Exception JavaDoc ex) {
268       log.log(Level.FINE, ex.toString(), ex);
269       return null;
270     }
271   }
272
273   /**
274    * Trims num elements from the end of the collection
275    */

276   public boolean trim(Env env,
277                       int num)
278   {
279     try {
280
281       if (num < 0) {
282         return false;
283       }
284
285       if (num == 0) {
286         return true;
287       }
288
289       int length = _javaCollection.size();
290
291       if (num > length) {
292         num = length;
293       }
294
295       int i = length - num;
296
297       _javaCollection.subList(i, length).clear();
298
299       return true;
300
301     } catch (Exception JavaDoc ex) {
302       log.log(Level.FINE, ex.toString(), ex);
303       return false;
304     }
305   }
306
307   public String JavaDoc toString() {
308     return "OracleOciCollection()";
309   }
310 }
311
Popular Tags