1 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 ; 40 import java.lang.reflect.Method ; 41 import java.sql.Array ; 42 import java.sql.Connection ; 43 import java.util.ArrayList ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 48 51 public class OracleOciCollection { 52 private static final Logger log = Logger.getLogger(OracleOciCollection.class.getName()); 53 private static final L10N L = new L10N(OracleOciCollection.class); 54 55 private Object _arrayDescriptor; 57 58 private Array _collection; 60 61 private ArrayList _javaCollection; 63 64 private Connection _jdbcConn; 66 67 private static Class classOracleARRAY; 69 70 static { 71 try { 72 classOracleARRAY = Class.forName("oracle.sql.ARRAY"); 73 } catch (Exception e) { 74 L.l("Unable to load ARRAY class oracle.sql.ARRAY."); 75 } 76 } 77 78 81 OracleOciCollection(Connection jdbcConn, 82 Object arrayDescriptor) 83 { 84 _jdbcConn = jdbcConn; 85 86 _arrayDescriptor = arrayDescriptor; 87 88 _collection = null; 89 90 _javaCollection = new ArrayList (); 91 } 92 93 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 ex) { 108 log.log(Level.FINE, ex.toString(), ex); 109 return false; 110 } 111 } 112 113 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 ex) { 126 log.log(Level.FINE, ex.toString(), ex); 127 return false; 128 } 129 } 130 131 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 ex) { 152 log.log(Level.FINE, ex.toString(), ex); 153 return false; 154 } 155 } 156 157 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 ex) { 171 log.log(Level.FINE, ex.toString(), ex); 172 return false; 173 } 174 } 175 176 179 protected Array getCollection() 180 { 181 try { 182 183 185 Class clArrayDescriptor = Class.forName("oracle.sql.ArrayDescriptor"); 186 187 Constructor constructor = classOracleARRAY.getDeclaredConstructor(new Class [] 188 {clArrayDescriptor, Connection .class, Object .class}); 189 190 Object elements[] = _javaCollection.toArray(); 191 192 _collection = (Array ) constructor.newInstance(new Object [] 193 {_arrayDescriptor, _jdbcConn, elements}); 194 195 if (_collection != null) { 196 Method setAutoBuffering 198 = classOracleARRAY.getDeclaredMethod("setAutoBuffering", 199 new Class [] {Boolean.TYPE}); 200 setAutoBuffering.invoke(_collection, new Object [] {true}); 201 } 202 203 return _collection; 204 205 } catch (Exception ex) { 206 log.log(Level.FINE, ex.toString(), ex); 207 return null; 208 } 209 } 210 211 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 ex) { 226 log.log(Level.FINE, ex.toString(), ex); 227 return BooleanValue.FALSE; 228 } 229 } 230 231 234 protected ArrayList getJavaCollection() 235 { 236 return _javaCollection; 237 } 238 239 244 @ReturnNullAsFalse 245 public LongValue max(Env env) 246 { 247 try { 248 249 return LongValue.create(0); 250 251 } catch (Exception ex) { 252 log.log(Level.FINE, ex.toString(), ex); 253 return null; 254 } 255 } 256 257 260 @ReturnNullAsFalse 261 public LongValue size(Env env) 262 { 263 try { 264 265 return LongValue.create(_javaCollection.size()); 266 267 } catch (Exception ex) { 268 log.log(Level.FINE, ex.toString(), ex); 269 return null; 270 } 271 } 272 273 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 ex) { 302 log.log(Level.FINE, ex.toString(), ex); 303 return false; 304 } 305 } 306 307 public String toString() { 308 return "OracleOciCollection()"; 309 } 310 } 311 | Popular Tags |