1 16 package org.apache.cocoon.components.flow.javascript; 17 import java.sql.Connection ; 18 import java.sql.PreparedStatement ; 19 import java.sql.ResultSet ; 20 import java.sql.ResultSetMetaData ; 21 import java.sql.SQLException ; 22 23 import org.mozilla.javascript.Context; 24 import org.mozilla.javascript.Function; 25 import org.mozilla.javascript.JavaScriptException; 26 import org.mozilla.javascript.Scriptable; 27 import org.mozilla.javascript.ScriptableObject; 28 import org.mozilla.javascript.Undefined; 29 import org.mozilla.javascript.Wrapper; 30 31 72 public class ScriptableConnection extends ScriptableObject { 73 74 Connection connection; 75 Scriptable wrapper; 76 77 static Object wrap(final Scriptable wrapper, 78 final Scriptable wrapped, 79 Object obj) { 80 if (obj instanceof Function) { 81 return wrap(wrapper, wrapped, (Function)obj); 82 } 83 return obj; 84 } 85 86 87 static Function wrap(final Scriptable wrapper, 88 final Scriptable wrapped, 89 final Function fun) { 90 return new Function() { 91 public Object call(Context cx, Scriptable scope, Scriptable thisObj, 92 Object [] args) throws JavaScriptException { 93 if (thisObj == wrapper) { 94 thisObj = wrapped; 95 } 96 return fun.call(cx, scope, thisObj, args); 97 } 98 99 public Scriptable construct(Context cx, Scriptable scope, 100 Object [] args) 101 throws JavaScriptException { 102 return fun.construct(cx, scope, args); 103 } 104 105 public String getClassName() { 106 return fun.getClassName(); 107 } 108 109 public Object get(String name, Scriptable start) { 110 return fun.get(name, fun); 111 } 112 113 public Object get(int index, Scriptable start) { 114 return fun.get(index, fun); 115 } 116 117 public boolean has(String name, Scriptable start) { 118 return fun.has(name, start); 119 } 120 121 public boolean has(int index, Scriptable start) { 122 return fun.has(index, start); 123 } 124 125 public void put(String name, Scriptable start, Object value) { 126 fun.put(name, start, value); 127 } 128 129 public void put(int index, Scriptable start, Object value) { 130 fun.put(index, start, value); 131 } 132 133 public void delete(String name) { 134 fun.delete(name); 135 } 136 137 public void delete(int index) { 138 fun.delete(index); 139 } 140 141 public Scriptable getPrototype() { 142 return fun.getPrototype(); 143 } 144 145 public void setPrototype(Scriptable prototype) { 146 } 147 148 public Scriptable getParentScope() { 149 return fun.getParentScope(); 150 } 151 152 public void setParentScope(Scriptable parent) { 153 } 154 155 public Object [] getIds() { 156 return fun.getIds(); 157 } 158 159 public Object getDefaultValue(Class hint) { 160 return fun.getDefaultValue(hint); 161 } 162 163 public boolean hasInstance(Scriptable instance) { 164 return fun.hasInstance(instance); 165 } 166 167 }; 168 } 169 170 public String getClassName() { 171 return "Database"; 172 } 173 174 public ScriptableConnection() { 175 } 176 177 public static void finishInit(Scriptable proto) { 178 } 179 180 public static Scriptable jsConstructor(Context cx, Object [] args, 181 Function ctorObj, 182 boolean inNewExpr) 183 throws Exception { 184 Connection conn = null; 185 if (args.length > 0) { 186 Object arg = args[0]; 187 if (arg instanceof Wrapper) { 188 arg = ((Wrapper)arg).unwrap(); 189 } 190 if (arg instanceof Connection ) { 191 conn = (Connection )arg; 192 } 193 } 194 if (conn == null) { 195 throw new JavaScriptException("expected an instance of java.sql.Connection"); 196 } 197 ScriptableConnection result = new ScriptableConnection(ctorObj, conn); 198 return result; 199 } 200 201 202 public ScriptableConnection(Scriptable parent, Connection conn) { 203 this.connection = conn; 204 this.wrapper = Context.toObject(connection, parent); 205 } 206 207 public Object jsFunction_query(String sql, Object params, 208 int startRow, int maxRows, 209 Object funObj) 210 throws JavaScriptException { 211 212 PreparedStatement stmt = null; 213 try { 214 stmt = connection.prepareStatement(sql); 215 Scriptable array = (Scriptable)params; 216 if (array != Undefined.instance) { 217 int len = (int) 218 Context.toNumber(ScriptableObject.getProperty(array, "length")); 219 for (int i = 0; i < len; i++) { 220 Object val = ScriptableObject.getProperty(array, i); 221 if (val instanceof Wrapper) { 222 val = ((Wrapper)val).unwrap(); 223 } 224 if (val == Scriptable.NOT_FOUND) { 225 val = null; 226 } 227 stmt.setObject(i + 1, val); 228 } 229 } 230 ResultSet rs = stmt.executeQuery(); 231 if (maxRows == 0) { 232 maxRows = -1; 233 } 234 if (funObj instanceof Function) { 235 Context cx = Context.getCurrentContext(); 236 Function fun = (Function)funObj; 237 ResultSetMetaData rsmd = rs.getMetaData(); 238 int noOfColumns = rsmd.getColumnCount(); 239 for (int i = 0; i < startRow; i++) { 241 rs.next(); 242 } 243 int processedRows = 0; 245 Scriptable scope = getTopLevelScope(this); 246 Scriptable proto = getObjectPrototype(scope); 247 Object [] args; 248 while (rs.next()) { 249 if ((maxRows != -1) && (processedRows == maxRows)) { 250 break; 251 } 252 Scriptable row = new ScriptableResult.Row(); 253 row.setParentScope(scope); 254 row.setPrototype(proto); 255 for (int i = 1; i <= noOfColumns; i++) { 256 Object value = rs.getObject(i); 257 if (rs.wasNull()) { 258 value = null; 259 } 260 row.put(rsmd.getColumnName(i), row, value); 261 } 262 args = new Object [1]; 263 args[0] = row; 264 fun.call(cx, scope, scope, args); 265 } 266 return Undefined.instance; 267 } else { 268 ScriptableResult s = new ScriptableResult(this, rs, 269 startRow, maxRows); 270 s.setParentScope(getTopLevelScope(this)); 271 s.setPrototype(getClassPrototype(this, s.getClassName())); 272 return s; 273 } 274 } catch (JavaScriptException e) { 275 throw e; 276 } catch (Exception e) { 277 throw new JavaScriptException(e); 278 } finally { 279 try { 280 if (stmt != null) { 281 stmt.close(); 282 } 283 } catch (SQLException sqle) { 284 throw new JavaScriptException(sqle); 285 } 286 } 287 } 288 289 public int jsFunction_update(String sql, Object params) 290 throws JavaScriptException { 291 PreparedStatement stmt = null; 292 try { 293 stmt = connection.prepareStatement(sql); 294 Scriptable array = (Scriptable)params; 295 if (array != Undefined.instance) { 296 int len = (int) 297 Context.toNumber(ScriptableObject.getProperty(array, "length")); 298 for (int i = 0; i < len; i++) { 299 Object val = ScriptableObject.getProperty(array, i); 300 if (val instanceof Wrapper) { 301 val = ((Wrapper)val).unwrap(); 302 } 303 if (val == Scriptable.NOT_FOUND) { 304 val = null; 305 } 306 stmt.setObject(i + 1, val); 307 } 308 } 309 stmt.execute(); 310 return stmt.getUpdateCount(); 311 } catch (Exception e) { 312 throw new JavaScriptException(e); 313 } finally { 314 try { 315 if (stmt != null) { 316 stmt.close(); 317 } 318 } catch (SQLException sqle) { 319 throw new JavaScriptException(sqle); 320 } 321 } 322 } 323 324 public Object get(String name, Scriptable start) { 325 if (wrapper != null) { 326 Object result = wrapper.get(name, wrapper); 327 if (result != NOT_FOUND) { 328 return wrap(this, wrapper, result); 329 } 330 } 331 return super.get(name, start); 332 } 333 334 public boolean has(String name, Scriptable start) { 335 if (wrapper != null) { 336 if (wrapper.has(name, wrapper)) { 337 return true; 338 } 339 } 340 return super.has(name, start); 341 } 342 343 public boolean has(int index, Scriptable start) { 344 if (wrapper != null) { 345 if (wrapper.has(index, start)) { 346 return true; 347 } 348 } 349 return super.has(index, start); 350 } 351 352 public Object get(int index, Scriptable start) { 353 if (wrapper != null) { 354 Object result = wrapper.get(index, start); 355 if (result != NOT_FOUND) { 356 return wrap(this, wrapper, result); 357 } 358 } 359 return super.get(index, start); 360 } 361 362 public void put(String name, Scriptable start, Object value) { 363 if (wrapper != null) { 364 wrapper.put(name, wrapper, value); 365 return; 366 } 367 super.put(name, start, value); 368 } 369 370 } 371 372 373 374 | Popular Tags |