001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.javascript;
018:
019: import java.sql.ResultSet;
020: import java.sql.ResultSetMetaData;
021: import java.sql.SQLException;
022:
023: import org.apache.commons.lang.BooleanUtils;
024: import org.mozilla.javascript.Context;
025: import org.mozilla.javascript.JavaScriptException;
026: import org.mozilla.javascript.NotAFunctionException;
027: import org.mozilla.javascript.PropertyException;
028: import org.mozilla.javascript.Scriptable;
029: import org.mozilla.javascript.ScriptableObject;
030:
031: /**
032: *
033: * @version CVS $Id: ScriptableResult.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public class ScriptableResult extends ScriptableObject {
036:
037: public String getClassName() {
038: return "Result";
039: }
040:
041: public ScriptableResult() {
042: }
043:
044: public static class Row extends ScriptableObject {
045:
046: public String getClassName() {
047: return "Row";
048: }
049:
050: public boolean has(String name, Scriptable start) {
051: return super .has(name.toUpperCase(), start);
052: }
053:
054: public Object get(String name, Scriptable start) {
055: return super .get(name.toUpperCase(), start);
056: }
057:
058: public void put(String name, Scriptable start, Object value) {
059: super .put(name.toUpperCase(), start, value);
060: }
061: }
062:
063: ScriptableResult(Scriptable scope, ResultSet rs, int startRow,
064: int maxRows) throws SQLException, PropertyException,
065: NotAFunctionException, JavaScriptException {
066: Context cx = Context.getCurrentContext();
067: Scriptable rowMap = cx.newObject(scope, "Array");
068: put("rows", this , rowMap);
069: Scriptable rowByIndex = cx.newObject(scope, "Array");
070: put("rowsByIndex", this , rowByIndex);
071:
072: ResultSetMetaData rsmd = rs.getMetaData();
073: int noOfColumns = rsmd.getColumnCount();
074:
075: // Create the column name array
076: Scriptable columnNames = cx.newObject(scope, "Array",
077: new Object[] { new Integer(noOfColumns) });
078: put("columnNames", this , columnNames);
079: for (int i = 1; i <= noOfColumns; i++) {
080: columnNames.put(i - 1, columnNames, rsmd.getColumnName(i));
081: }
082:
083: // Throw away all rows upto startRow
084: for (int i = 0; i < startRow; i++) {
085: rs.next();
086: }
087:
088: // Process the remaining rows upto maxRows
089: int processedRows = 0;
090: int index = 0;
091: boolean isLimited = false;
092: while (rs.next()) {
093: if ((maxRows != -1) && (processedRows == maxRows)) {
094: isLimited = true;
095: break;
096: }
097: Scriptable columns = cx.newObject(scope, "Array",
098: new Object[] { new Integer(noOfColumns) });
099: Scriptable columnMap = new Row();
100: columnMap.setParentScope(columns.getParentScope());
101: columnMap.setPrototype(getObjectPrototype(scope));
102:
103: // JDBC uses 1 as the lowest index!
104: for (int i = 1; i <= noOfColumns; i++) {
105: Object value = rs.getObject(i);
106: if (rs.wasNull()) {
107: value = null;
108: }
109: columns.put(i - 1, columns, value);
110: columnMap.put(rsmd.getColumnName(i), columnMap, value);
111: }
112: rowMap.put(index, rowMap, columnMap);
113: rowByIndex.put(index, rowByIndex, columns);
114: processedRows++;
115: index++;
116: }
117: put("rowCount", this , new Integer(index));
118: put("isLimitedByMaxRows", this, BooleanUtils
119: .toBooleanObject(isLimited));
120: }
121: }
|