1 16 17 package org.pentaho.data.connection.mdx; 18 19 import mondrian.olap.Axis; 20 import mondrian.olap.Connection; 21 import mondrian.olap.Result; 22 23 import org.pentaho.core.connection.IPentahoMetaData; 24 import org.pentaho.core.connection.IPentahoResultSet; 25 import org.pentaho.core.connection.memory.MemoryMetaData; 26 import org.pentaho.core.connection.memory.MemoryResultSet; 27 28 34 public class MDXResultSet implements IPentahoResultSet { 35 private static final int columnAxis = 0; 36 37 private static final int rowAxis = 1; 38 39 int columnCount = 0; 40 41 int rowCount = 0; 42 43 Result nativeResultSet; 44 45 Connection nativeConnection; 46 47 int rowIndex = 0; 48 49 MDXMetaData mdxMetaData = null; 50 51 54 public MDXResultSet(Result nativeResultSet, Connection nativeConnection) { 55 super(); 56 57 this.nativeResultSet = nativeResultSet; 58 this.nativeConnection = nativeConnection; 59 60 Axis[] axis = nativeResultSet.getAxes(); 61 if (axis == null || axis.length == 0) { 62 columnCount = 0; 63 rowCount = 0; 64 } else if (axis.length == 1) { 65 columnCount = axis[columnAxis].positions.length; 66 rowCount = 0; 67 } else { 68 columnCount = axis[columnAxis].positions.length; 69 rowCount = axis[rowAxis].positions.length; 70 } 71 72 mdxMetaData = new MDXMetaData(this.nativeResultSet); 73 } 74 75 80 public IPentahoMetaData getMetaData() { 81 return mdxMetaData; 82 } 83 84 89 public Object [] next() { 90 Object [] result = null; 91 92 if (rowIndex < rowCount) { 93 result = new Object [columnCount]; 94 for (int i = 0; i < columnCount; i++) { 95 result[i] = getValueAt(rowIndex, i); 96 } 97 rowIndex++; 98 } 99 return result; 100 } 101 102 107 public void close() { 108 nativeResultSet.close(); 109 } 110 111 116 public void closeConnection() { 117 nativeResultSet.close(); 118 nativeConnection.close(); 119 } 120 121 126 public void dispose() { 127 closeConnection(); 128 } 129 130 135 public boolean isScrollable() { 136 return false; 138 } 139 140 145 public Object getValueAt(int row, int column) { 146 int[] key = new int[2]; 147 key[0] = column; 148 key[1] = row; 149 return nativeResultSet.getCell(key).getValue(); 150 } 151 152 157 public int getRowCount() { 158 return mdxMetaData.getRowHeaders().length; 159 } 160 161 166 public int getColumnCount() { 167 return mdxMetaData.getColumnCount(); 168 } 169 170 public IPentahoResultSet memoryCopy() { 171 try { 172 IPentahoMetaData metadata = getMetaData(); 173 Object columnHeaders[][] = metadata.getColumnHeaders(); 174 Object rowHeaders[][] = metadata.getRowHeaders(); 175 176 MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, rowHeaders); 177 MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData); 178 179 Object [] rowObjects = next(); 180 while (rowObjects != null) { 181 cachedResultSet.addRow(rowObjects); 182 rowObjects = next(); 183 } 184 return cachedResultSet; 185 } finally { 186 close(); 187 } 188 } 189 190 public void beforeFirst() { 191 rowIndex = 0; 192 } 193 194 public Object [] getDataColumn(int column) { 195 int oldIndex = rowIndex; 197 beforeFirst(); 198 Object [] result = new Object [getRowCount()]; 199 int index = 0; 200 Object [] rowData = next(); 201 while (rowData != null) { 202 result[index] = rowData[column]; 203 index++; 204 rowData = next(); 205 } 206 207 rowIndex = oldIndex; 209 return result; 210 } 211 212 public Object [] getDataRow(int row) { 213 int oldIndex = rowIndex; 215 rowIndex = row; 216 Object [] rowData = next(); 217 rowIndex = oldIndex; 218 219 return rowData; 220 } 221 222 } 223 | Popular Tags |