| /** |
| * |
| * 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.util.HashMap; |
| import java.util.Map.Entry; |
| |
| import org.eclipse.datatools.connectivity.oda.IResultSetMetaData; |
| import org.eclipse.datatools.connectivity.oda.OdaException; |
| import org.eclipse.osbp.ui.api.datamart.IDataMart.EType; |
| import org.eclipse.osbp.xtext.reportdsl.common.DataTypes; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * Implementation class of IResultSetMetaData 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 ResultSetMetaData implements IResultSetMetaData { |
| private String[] columnNames = null; |
| private DataTypes[] columnTypes = null; |
| private String[] columnLabels = null; |
| private static final Logger LOGGER = LoggerFactory.getLogger(ResultSetMetaData.class); |
| |
| ResultSetMetaData( String[] colNames, DataTypes[] colTypes, String[] colLabels ) { |
| if( colNames == null ) { |
| LOGGER.error("ARGUMENT CANNOT BE NULL"); |
| } |
| this.columnNames = colNames; |
| this.columnTypes = colTypes; |
| if (colLabels == null) { |
| this.columnLabels = colNames; |
| } |
| else { |
| this.columnLabels = colLabels; |
| } |
| } |
| |
| ResultSetMetaData( HashMap<String, EType> typesMap) { |
| columnNames = typesMap.keySet().toArray(new String[0]); |
| for(int idx=0; idx<columnNames.length; idx++) { |
| columnNames[idx] = columnNames[idx].split("\\.")[1]; |
| } |
| columnLabels = typesMap.keySet().toArray(new String[0]); |
| for(int idx=0; idx<columnLabels.length; idx++) { |
| columnLabels[idx] = columnLabels[idx].split("\\.")[1]; |
| } |
| columnTypes = new DataTypes[typesMap.keySet().size()]; |
| int idx = 0; |
| for(Entry<String, EType> entrySet : typesMap.entrySet()) { |
| columnTypes[idx++] = DataTypes.typeFor(entrySet.getValue()); |
| } |
| } |
| |
| public void setColumnTypes(DataTypes[] colTypes) { |
| this.columnTypes = colTypes; |
| } |
| |
| public void setColumnLables(String[] colLabels) { |
| this.columnLabels = colLabels; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnCount() |
| */ |
| public int getColumnCount() throws OdaException { |
| return this.columnNames.length; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnName(int) |
| */ |
| public String getColumnName( int index ) throws OdaException { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| LOGGER.error("{}.getColumnName({}):\n- Index {} is out of range of available column indices {}-{}", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| ); |
| } |
| return this.columnNames[index - 1].trim( ); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnLabel(int) |
| */ |
| public String getColumnLabel( int index ) throws OdaException { |
| if (columnLabels == null) { |
| return getColumnName( index ); // default |
| } |
| else { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| LOGGER.error("{}.getColumnLabel({}):\n- Index {} is out of range of available column indices {}-{}", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| ); |
| } |
| return this.columnLabels[index - 1].trim( ); |
| } |
| } |
| |
| public DataTypes getColumnDataType( int index ) throws OdaException { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| LOGGER.error("{}.getColumnDataType({}):\n- Index {} is out of range of available column indices {}-{}", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| ); |
| } |
| return columnTypes[index-1]; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnType(int) |
| */ |
| public int getColumnType( int index ) throws OdaException { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| LOGGER.error("{}.getColumnType({}):\n- Index {} is out of range of available column indices {}-{}", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| ); |
| } |
| return columnTypes[index-1].getNativeOdaDataTypeId(); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnTypeName(int) |
| */ |
| public String getColumnTypeName( int index ) { |
| try { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| LOGGER.error("{}.getColumnTypeName({}):\n- Index {} is out of range of available column indices {}-{}", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| ); |
| return null; |
| } |
| return Driver.getNativeDataTypeName(columnTypes[index-1].getNativeOdaDataTypeId()); |
| } |
| catch (OdaException oe) { |
| LOGGER.error("{}.getColumnTypeName({}):\n- internal exception on index {}: label='{}' / name='{}' / {}='{}'\n- caused by:\n- {}", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| columnLabels[index-1], |
| columnNames[index-1], |
| columnTypes[index-1].getClass().getCanonicalName(), |
| columnTypes[index-1].toString(), |
| oe.getLocalizedMessage() |
| ); |
| } |
| return null; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnDisplayLength(int) |
| */ |
| public int getColumnDisplayLength( int index ) throws OdaException { |
| LOGGER.error("{}.getColumnDisplayLength({}):\n- Method is unsupported!", |
| getClass().getCanonicalName(), |
| index |
| ); |
| return 0; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getPrecision(int) |
| */ |
| public int getPrecision( int index ) throws OdaException { |
| return -1; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getScale(int) |
| */ |
| public int getScale( int index ) throws OdaException { |
| return -1; |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#isNullable(int) |
| */ |
| public int isNullable( int index ) throws OdaException { |
| return IResultSetMetaData.columnNullableUnknown; |
| } |
| } |