| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.reportdsl.oda.datamart.impl; |
| |
| import java.io.PrintWriter; |
| import java.io.StringWriter; |
| import java.math.BigDecimal; |
| import java.sql.Date; |
| import java.sql.Time; |
| import java.sql.Timestamp; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.apache.commons.lang.ArrayUtils; |
| import org.eclipse.birt.core.data.DataType; |
| import org.eclipse.datatools.connectivity.oda.IBlob; |
| import org.eclipse.datatools.connectivity.oda.IClob; |
| import org.eclipse.datatools.connectivity.oda.IResultSet; |
| import org.eclipse.datatools.connectivity.oda.IResultSetMetaData; |
| import org.eclipse.datatools.connectivity.oda.OdaException; |
| import org.eclipse.datatools.connectivity.oda.impl.Blob; |
| import org.eclipse.osbp.ui.api.customfields.IBlobService; |
| import org.eclipse.osbp.xtext.datamart.common.olap.Cell2dTable; |
| import org.eclipse.osbp.xtext.datamart.common.olap.DerivedCell; |
| import org.eclipse.osbp.xtext.table.common.CellSetIndexedContainer; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * Implementation class of IResultSet for an ODA runtime driver. |
| * <br> |
| * For demo purpose, the auto-generated method stubs have |
| * hard-coded implementation that returns a pre-defined set |
| * of meta-data and query results. |
| * A custom ODA driver is expected to implement own data source specific |
| * behavior in its place. |
| */ |
| public class ResultSet implements IResultSet { |
| |
| public static final int DEFAULT_MAX_ROWS = 1000; |
| private static final int CURSOR_INITIAL_VALUE = -1; |
| private Cell2dTable fCellTable = null; |
| private IResultSetMetaData fResultSetMetaData = null; |
| private int fMaxRows = 0; //no limit |
| private int fCursor = CURSOR_INITIAL_VALUE; |
| private boolean fWasNull = false; |
| |
| private Map<Integer,Integer> fMapCellTableToResultSet; |
| private Map<Integer,Integer> fMapResultSetToCellTable; |
| private IBlobService fBlobService; |
| private static final Logger LOGGER = LoggerFactory.getLogger(ResultSet.class); |
| |
| /** |
| * Constructor |
| * @param sData a two-dimension array which holds the data extracted from a |
| * csv file. |
| * @param rsmd the metadata of sData |
| */ |
| ResultSet( Cell2dTable cellTable, IResultSetMetaData rsmd, IBlobService blobService ) { |
| fCellTable = cellTable; |
| fResultSetMetaData = rsmd; |
| fMapCellTableToResultSet = new HashMap<Integer, Integer>(); |
| fMapResultSetToCellTable = new HashMap<Integer, Integer>(); |
| fBlobService = blobService; |
| try { |
| for (int resultSetIndex = 1; resultSetIndex <= rsmd.getColumnCount(); resultSetIndex++) { |
| String columnName = rsmd.getColumnName(resultSetIndex); |
| int cellTableIndex = cellTable.getHeaderIndex(columnName); |
| fMapCellTableToResultSet.put(cellTableIndex, resultSetIndex); |
| fMapResultSetToCellTable.put(resultSetIndex, cellTableIndex); |
| String type = rsmd.getColumnTypeName(resultSetIndex); |
| LOGGER.debug("column:{} type:{} cellIdx:{} resultSetIdx:{}", columnName, type, cellTableIndex, resultSetIndex); |
| } |
| } catch (OdaException e) { |
| StringWriter sw = new StringWriter(); |
| e.printStackTrace(new PrintWriter(sw)); |
| String exceptionDetails = sw.toString(); |
| LOGGER.error("exception occured:{}", exceptionDetails); |
| } |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getMetaData() |
| */ |
| public IResultSetMetaData getMetaData() throws OdaException { |
| return fResultSetMetaData; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#setMaxRows(int) |
| */ |
| public void setMaxRows( int max ) throws OdaException { |
| fMaxRows = max; |
| } |
| |
| /** |
| * Returns the maximum number of rows that can be fetched from this result set. |
| * @return the maximum number of rows to fetch. |
| */ |
| protected int getMaxRows() { |
| return fMaxRows; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#next() |
| */ |
| public boolean next() throws OdaException { |
| if( ( fMaxRows <= 0 ? false : fCursor >= fMaxRows - 1 ) |
| || fCursor >= fCellTable.getRowCount() - 1 ) { |
| fCursor = CURSOR_INITIAL_VALUE; |
| return false; |
| } |
| fCursor++; |
| return true; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#close() |
| */ |
| public void close() throws OdaException { |
| fCursor = 0; |
| fCellTable = null; |
| fResultSetMetaData = null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getRow() |
| */ |
| public int getRow() throws OdaException { |
| validateCursorState(); |
| return fCursor; |
| } |
| |
| /** |
| * @param cellTableIndex |
| * @return cell for the cellTable index given |
| * @throws OdaException |
| */ |
| private DerivedCell getCellTableCell( int cellTableIndex ) throws OdaException { |
| validateCursorState(); |
| DerivedCell result = fCellTable.getCell(cellTableIndex, fCursor); |
| fWasNull = result == null ? true : false; |
| return result; |
| } |
| |
| public DerivedCell getCell( int resultSetIndex ) throws OdaException { |
| Integer cellTableIndex = fMapResultSetToCellTable.get(resultSetIndex); |
| if (cellTableIndex == null) { |
| LOGGER.error("cellTable column #{} could not be mapped to resultSet column", resultSetIndex); |
| } else { |
| return getCellTableCell(cellTableIndex); |
| } |
| return null; |
| } |
| |
| public DerivedCell getCell( String columnName ) throws OdaException { |
| return getCellTableCell( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getString(int) |
| */ |
| public String getString( int resultSetIndex ) throws OdaException { |
| DerivedCell cell = getCell(resultSetIndex); |
| String result = cell == null ? null : cell.getFormattedValue(); |
| if (result != null && result.isEmpty() |
| && (fResultSetMetaData.getColumnType(resultSetIndex) == DataType.DATE_TYPE |
| || fResultSetMetaData.getColumnType(resultSetIndex) == DataType.SQL_DATE_TYPE)) { |
| result = null; |
| } |
| LOGGER.debug("resultSetIdx:{} String:{}", resultSetIndex, result); |
| return result; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getString(java.lang.String) |
| */ |
| public String getString( String columnName ) throws OdaException { |
| return getString( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getInt(int) |
| */ |
| public int getInt( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| try { |
| if(object instanceof BigDecimal) { |
| LOGGER.debug("resultSetIdx:{} BigDecimal from int:{}", resultSetIndex, ((BigDecimal)object).intValue()); |
| return ((BigDecimal)object).intValue(); |
| } |
| if(object instanceof Double) { |
| LOGGER.debug("resultSetIdx:{} Double from int:{}", resultSetIndex, ((Double)object).intValue()); |
| return ((Double)object).intValue(); |
| } |
| if(object instanceof Integer) { |
| LOGGER.debug("resultSetIdx:{} Integer from int:{}", resultSetIndex, ((Integer)object).intValue()); |
| return ((Integer)object).intValue(); |
| } |
| if(object instanceof Long) { |
| LOGGER.debug("resultSetIdx:{} Long from int:{}", resultSetIndex, ((Long)object).intValue()); |
| return ((Long)object).intValue(); |
| } |
| } |
| catch (Exception e) { |
| StringWriter sw = new StringWriter(); |
| e.printStackTrace(new PrintWriter(sw)); |
| String exceptionDetails = sw.toString(); |
| LOGGER.error("exception occured:{}", exceptionDetails); |
| } |
| return -1; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getInt(java.lang.String) |
| */ |
| public int getInt( String columnName ) throws OdaException { |
| return getInt( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDouble(int) |
| */ |
| public double getDouble( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| if (object instanceof BigDecimal) { |
| LOGGER.debug("resultSetIdx:{} BigDecimal from Double:{}", resultSetIndex, ((BigDecimal)object).doubleValue()); |
| return ((BigDecimal)object).doubleValue(); |
| } |
| else if (object instanceof Double) { |
| LOGGER.debug("resultSetIdx:{} Double from Double:{}", resultSetIndex, ((Double)object).doubleValue()); |
| return ((Double)object); |
| } |
| return -1.0; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDouble(java.lang.String) |
| */ |
| public double getDouble( String columnName ) throws OdaException { |
| return getDouble( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBigDecimal(int) |
| */ |
| public BigDecimal getBigDecimal( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| if (object instanceof BigDecimal) { |
| LOGGER.debug("resultSetIdx:{} BigDecimal from BigDecimal:{}", resultSetIndex, (BigDecimal)object); |
| return (BigDecimal)object; |
| } |
| else if (object instanceof Double) { |
| LOGGER.debug("resultSetIdx:{} Double from BigDecimal:{}", resultSetIndex, BigDecimal.valueOf((Double)object)); |
| return BigDecimal.valueOf((Double)object); |
| } |
| return null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBigDecimal(java.lang.String) |
| */ |
| public BigDecimal getBigDecimal( String columnName ) throws OdaException { |
| return getBigDecimal( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDate(int) |
| */ |
| public Date getDate( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| LOGGER.debug("resultSetIdx:{} Date from Timestamp:{}", resultSetIndex, object instanceof Timestamp ? new Date(((Timestamp)object).getTime()) : null); |
| return object instanceof Timestamp ? new Date(((Timestamp)object).getTime()) : null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDate(java.lang.String) |
| */ |
| public Date getDate( String columnName ) throws OdaException { |
| return getDate( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTime(int) |
| */ |
| public Time getTime( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| LOGGER.debug("resultSetIdx:{} Time from Timestamp:{}", resultSetIndex, object instanceof Timestamp ? new Time(((Timestamp)object).getTime()) : null); |
| return object instanceof Timestamp ? new Time(((Timestamp)object).getTime()) : null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTime(java.lang.String) |
| */ |
| public Time getTime( String columnName ) throws OdaException { |
| return getTime( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTimestamp(int) |
| */ |
| public Timestamp getTimestamp( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| LOGGER.debug("resultSetIdx:{} Timestamp:{}", resultSetIndex, object instanceof Timestamp ? (Timestamp)object : null); |
| return object instanceof Timestamp ? (Timestamp)object : null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTimestamp(java.lang.String) |
| */ |
| public Timestamp getTimestamp( String columnName ) throws OdaException { |
| return getTimestamp( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBlob(int) |
| */ |
| public IBlob getBlob( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| if (object instanceof String) { |
| LOGGER.debug("resultSetIdx:{} Blob from String:{}", resultSetIndex, (String)object); |
| byte[] byteArrayImage = fBlobService.getByteArrayImage((String)object, 2); |
| if (byteArrayImage != null) { |
| return new Blob(byteArrayImage); |
| } else { |
| return new Blob(ArrayUtils.EMPTY_BYTE_ARRAY); |
| } |
| } |
| if (object instanceof IBlob) { |
| LOGGER.debug("resultSetIdx:{} Blob from IBlob", resultSetIndex); |
| return (IBlob) object; |
| } |
| return null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBlob(java.lang.String) |
| */ |
| public IBlob getBlob( String columnName ) throws OdaException { |
| return getBlob( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getClob(int) |
| */ |
| public IClob getClob( int resultSetIndex ) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| if (object instanceof IClob) { |
| LOGGER.debug("resultSetIdx:{} Clob from IClob", resultSetIndex); |
| return (IClob) object; |
| } |
| return null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#getClob(java.lang.String) |
| */ |
| public IClob getClob( String columnName ) throws OdaException { |
| return getClob( findColumn( columnName ) ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#wasNull() |
| */ |
| public boolean wasNull() throws OdaException { |
| return fWasNull; |
| } |
| |
| /** |
| * @see org.eclipse.datatools.connectivity.oda.IResultSet#findColumn(java.lang.String) |
| * @return <b>resultSet column index</b> <i><u>not</u> the cellTable column index</i> |
| */ |
| public int findColumn( String columnName ) throws OdaException { |
| String trimmedColumnName = columnName.trim(); |
| int columnCount = getMetaData().getColumnCount(); |
| for( int i = 1; i <= columnCount; i++ ) { |
| if( trimmedColumnName.equalsIgnoreCase( |
| getMetaData().getColumnName( i ) ) ) { |
| return i; |
| } |
| } |
| LOGGER.error("COLUMN NOT FOUND {}", columnName ); |
| return 0; |
| } |
| |
| /** |
| * Validate whether the cursor has been initialized and at a valid row. |
| * @throws OdaException if the cursor is not initialized |
| */ |
| private void validateCursorState() throws OdaException { |
| if( fCursor < 0 ) { |
| LOGGER.error("CURSOR HAS NOT BEEN INITIALIZED"); |
| } |
| } |
| |
| public boolean getBoolean(int resultSetIndex) throws OdaException { |
| Object object = getObject(resultSetIndex); |
| if (object instanceof Boolean) { |
| LOGGER.debug("resultSetIdx:{} Boolean from Boolean:{}", resultSetIndex, ((Boolean)object).booleanValue()); |
| return ((Boolean)object).booleanValue(); |
| } else if (object instanceof String) { |
| LOGGER.debug("resultSetIdx:{} Boolean from String:{}", resultSetIndex, Boolean.valueOf((String)object)); |
| return Boolean.valueOf((String)object); |
| } else if (object instanceof Integer) { |
| LOGGER.debug("resultSetIdx:{} Boolean from Integer:{}", resultSetIndex, ((Integer)object)!=0); |
| return ((Integer)object)!=0; |
| } |
| return false; |
| } |
| |
| public boolean getBoolean(String columnName) throws OdaException { |
| return getBoolean( findColumn( columnName ) ); |
| } |
| |
| @Override |
| public Object getObject(int resultSetIndex) throws OdaException { |
| DerivedCell cell = getCell(resultSetIndex); |
| return cell == null ? null : cell.getValue(); |
| } |
| |
| @Override |
| public Object getObject(String columnName) throws OdaException { |
| return getObject( findColumn( columnName ) ); |
| } |
| } |