001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Dec 27, 2006
014: * @author mdamour
015: */
016: package org.pentaho.data.connection.hql;
017:
018: import java.util.List;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.hibernate.type.Type;
023: import org.pentaho.commons.connection.IPentahoMetaData;
024: import org.pentaho.commons.connection.IPentahoResultSet;
025: import org.pentaho.commons.connection.memory.MemoryMetaData;
026: import org.pentaho.commons.connection.memory.MemoryResultSet;
027: import org.pentaho.messages.Messages;
028:
029: /**
030: * @author mdamour
031: *
032: * TODO To change the template for this generated type comment go to Window -
033: * Preferences - Java - Code Style - Code Templates
034: */
035: public class HQLResultSet implements IPentahoResultSet {
036: private static final int COUNT_NEVER_OBTAINED = -2;
037:
038: private int rowCount = COUNT_NEVER_OBTAINED;
039:
040: private int columnCount = COUNT_NEVER_OBTAINED;
041:
042: private static final Log log = LogFactory
043: .getLog(HQLResultSet.class);
044:
045: private IPentahoMetaData metadata;
046:
047: private List nativeResultSet = null;
048:
049: private int index = 0;
050:
051: /**
052: *
053: */
054: public HQLResultSet(List list, String columnNames[],
055: Type columnTypes[]) {
056: super ();
057: try {
058: nativeResultSet = list;
059: metadata = new HQLMetaData(list, this , columnNames,
060: columnTypes);
061: } catch (Exception e) {
062: // TODO Auto-generated catch block
063: log
064: .error(
065: Messages
066: .getErrorString("SQLResultSet.ERROR_0004_GET_METADATA"), e); //$NON-NLS-1$
067: // e.printStackTrace();
068: throw new RuntimeException(e);
069: }
070: }
071:
072: public void setMetaData(IPentahoMetaData metadata) {
073: this .metadata = metadata;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.pentaho.connection.IPentahoResultSet#getMetaData()
080: */
081: public IPentahoMetaData getMetaData() {
082: return metadata;
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.pentaho.connection.IPentahoResultSet#next() returns null if no
089: * more rows
090: */
091: public Object[] next() {
092: try {
093: if (index < nativeResultSet.size()) {
094: Object row = nativeResultSet.get(index++);
095: if (row instanceof Object[]) {
096: return (Object[]) row;
097: } else {
098: Object myRow[] = new Object[1];
099: myRow[0] = row;
100: return myRow;
101: }
102: }
103: } catch (Exception e) {
104: log.error(Messages
105: .getErrorString("SQLResultSet.ERROR_0005_NEXT"), e); //$NON-NLS-1$
106: }
107: return null;
108: }
109:
110: public void closeConnection() {
111: close();
112: }
113:
114: public void close() {
115: }
116:
117: public void dispose() {
118: closeConnection();
119: }
120:
121: public boolean isScrollable() {
122: return false;
123: }
124:
125: /**
126: * Returns the column count from the result set.
127: *
128: * @return the column count.
129: */
130: public int getColumnCount() {
131: if (columnCount != COUNT_NEVER_OBTAINED) {
132: // We have already calculated column count, return what we have.
133: return columnCount;
134: }
135: if (nativeResultSet.size() > 0) {
136: Object row = nativeResultSet.get(0);
137: if (row instanceof Object[]) {
138: columnCount = ((Object[]) row).length;
139: } else {
140: columnCount = 1;
141: }
142: return columnCount;
143: }
144: return 0;
145: }
146:
147: /**
148: * Get a rowCount from the resultset. If the resultset
149: *
150: * @return the row count.
151: */
152: public int getRowCount() {
153: if (rowCount != COUNT_NEVER_OBTAINED) {
154: // We have already calculated rowcount, return what we have
155: return rowCount;
156: }
157: rowCount = nativeResultSet.size();
158: return rowCount;
159: }
160:
161: /**
162: * Returns the value of the specified row and the specified column from
163: * within the resultset.
164: *
165: * @param row
166: * the row index.
167: * @param column
168: * the column index.
169: * @return the value.
170: */
171: public Object getValueAt(int row, int column) {
172: if (row < nativeResultSet.size()) {
173: Object rowObj = nativeResultSet.get(row);
174: if (rowObj instanceof Object[]) {
175: return ((Object[]) rowObj)[column];
176: } else {
177: return rowObj;
178: }
179: }
180: return null;
181: }
182:
183: public IPentahoResultSet memoryCopy() {
184: try {
185: IPentahoMetaData localMetadata = getMetaData();
186: Object columnHeaders[][] = localMetadata.getColumnHeaders();
187: MemoryMetaData cachedMetaData = new MemoryMetaData(
188: columnHeaders, null);
189: MemoryResultSet cachedResultSet = new MemoryResultSet(
190: cachedMetaData);
191: Object[] rowObjects = next();
192: while (rowObjects != null) {
193: cachedResultSet.addRow(rowObjects);
194: rowObjects = next();
195: }
196: return cachedResultSet;
197: } finally {
198: close();
199: }
200: }
201:
202: public void beforeFirst() {
203: try {
204: index = 0;
205: } catch (Exception e) {
206: log
207: .error(
208: Messages
209: .getErrorString("SQLResultSet.ERROR_0003_BEFORE_FIRST"), e); //$NON-NLS-1$
210: // TODO Auto-generated catch block
211: e.printStackTrace();
212: }
213: }
214:
215: public Object[] getDataColumn(int column) {
216: Object[] result = null;
217: result = new Object[getRowCount()];
218: for (int row = 0; row < result.length; row++) {
219: result[row] = getValueAt(row, column);
220: }
221: return result;
222: }
223:
224: public Object[] getDataRow(int row) {
225: Object[] rowData = new Object[this .getColumnCount()];
226: for (int column = 0; column < rowData.length; column++) {
227: rowData[column] = getValueAt(row, column);
228: }
229: return rowData;
230: }
231: }
|