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 Sep 12, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.data.connection.mdx;
018:
019: import mondrian.olap.Axis;
020: import mondrian.olap.Connection;
021: import mondrian.olap.Result;
022:
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:
028: /**
029: * @author wseyler
030: *
031: * TODO To change the template for this generated type comment go to Window -
032: * Preferences - Java - Code Style - Code Templates
033: */
034: public class MDXResultSet implements IPentahoResultSet {
035: private static final int columnAxis = 0;
036:
037: private static final int rowAxis = 1;
038:
039: int columnCount = 0;
040:
041: int rowCount = 0;
042:
043: Result nativeResultSet;
044:
045: Connection nativeConnection;
046:
047: int rowIndex = 0;
048:
049: MDXMetaData mdxMetaData = null;
050:
051: /**
052: * @param result
053: */
054: public MDXResultSet(Result nativeResultSet,
055: Connection nativeConnection) {
056: super ();
057:
058: this .nativeResultSet = nativeResultSet;
059: this .nativeConnection = nativeConnection;
060:
061: Axis[] axis = nativeResultSet.getAxes();
062: if (axis == null || axis.length == 0) {
063: columnCount = 0;
064: rowCount = 0;
065: } else if (axis.length == 1) {
066: columnCount = axis[columnAxis].getPositions().size();
067: rowCount = 0;
068: } else {
069: columnCount = axis[columnAxis].getPositions().size();
070: rowCount = axis[rowAxis].getPositions().size();
071: }
072:
073: mdxMetaData = new MDXMetaData(this .nativeResultSet);
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.pentaho.connection.IPentahoResultSet#getMetaData()
080: */
081: public IPentahoMetaData getMetaData() {
082: return mdxMetaData;
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.pentaho.connection.IPentahoResultSet#next()
089: */
090: public Object[] next() {
091: Object[] result = null;
092:
093: if (rowIndex < rowCount) {
094: result = new Object[columnCount];
095: for (int i = 0; i < columnCount; i++) {
096: result[i] = getValueAt(rowIndex, i);
097: }
098: rowIndex++;
099: }
100: return result;
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see org.pentaho.connection.IPentahoResultSet#close()
107: */
108: public void close() {
109: nativeResultSet.close();
110: }
111:
112: /*
113: * (non-Javadoc)
114: *
115: * @see org.pentaho.connection.IPentahoResultSet#closeConnection()
116: */
117: public void closeConnection() {
118: nativeResultSet.close();
119: nativeConnection.close();
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.pentaho.core.runtime.IDisposable#dispose()
126: */
127: public void dispose() {
128: closeConnection();
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see org.pentaho.connection.IPentahoResultSet#isScrollable()
135: */
136: public boolean isScrollable() {
137: // TODO Auto-generated method stub
138: return false;
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see org.pentaho.connection.IPentahoResultSet#getValueAt(int, int)
145: */
146: public Object getValueAt(int row, int column) {
147: int[] key = new int[2];
148: key[0] = column;
149: key[1] = row;
150: return nativeResultSet.getCell(key).getValue();
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see org.pentaho.connection.IPentahoResultSet#getRowCount()
157: */
158: public int getRowCount() {
159: return mdxMetaData.getRowHeaders().length;
160: }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see org.pentaho.connection.IPentahoResultSet#getColumnCount()
166: */
167: public int getColumnCount() {
168: return mdxMetaData.getColumnCount();
169: }
170:
171: public IPentahoResultSet memoryCopy() {
172: try {
173: IPentahoMetaData metadata = getMetaData();
174: Object columnHeaders[][] = metadata.getColumnHeaders();
175: Object rowHeaders[][] = metadata.getRowHeaders();
176:
177: MemoryMetaData cachedMetaData = new MemoryMetaData(
178: columnHeaders, rowHeaders);
179: MemoryResultSet cachedResultSet = new MemoryResultSet(
180: cachedMetaData);
181:
182: Object[] rowObjects = next();
183: while (rowObjects != null) {
184: cachedResultSet.addRow(rowObjects);
185: rowObjects = next();
186: }
187: return cachedResultSet;
188: } finally {
189: close();
190: }
191: }
192:
193: public void beforeFirst() {
194: rowIndex = 0;
195: }
196:
197: public Object[] getDataColumn(int column) {
198: int oldIndex = rowIndex; // save our current iteration location
199:
200: beforeFirst();
201: Object[] result = new Object[getRowCount()];
202: int index = 0;
203: Object[] rowData = next();
204: while (rowData != null) {
205: result[index] = rowData[column];
206: index++;
207: rowData = next();
208: }
209:
210: rowIndex = oldIndex; // restore the old iteration location
211:
212: return result;
213: }
214:
215: public Object[] getDataRow(int row) {
216: int oldIndex = rowIndex; // save our current iteration location
217:
218: rowIndex = row;
219: Object[] rowData = next();
220: rowIndex = oldIndex;
221:
222: return rowData;
223: }
224:
225: }
|