| /** |
| * |
| * 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 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.eclipse.osbp.xtext.reportdsl.oda.datamart.i18n.Messages; |
| |
| /** |
| * 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; |
| |
| ResultSetMetaData( String[] colNames, DataTypes[] colTypes, String[] colLabels ) throws OdaException { |
| if( colNames == null ) { |
| throw new OdaException( Messages.getString( "common_ARGUMENT_CANNOT_BE_NULL" ) ); //$NON-NLS-1$ |
| } |
| this.columnNames = colNames; |
| this.columnTypes = colTypes; |
| if (colLabels == null) { |
| this.columnLabels = colNames; |
| } |
| else { |
| this.columnLabels = colLabels; |
| } |
| } |
| |
| ResultSetMetaData( HashMap<String, EType> typesMap) throws OdaException { |
| 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(String key : typesMap.keySet()) { |
| columnTypes[idx++] = DataTypes.typeFor(typesMap.get(key)); |
| } |
| } |
| |
| 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 ) { |
| throw new OdaException( String.format( |
| "%s.getColumnName(%d):\n- Index %d is out of range of available column indices %d-%d", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| )); //$NON-NLS-1$ |
| } |
| 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 ) { |
| throw new OdaException( String.format( |
| "%s.getColumnLabel(%d):\n- Index %d is out of range of available column indices %d-%d", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| )); //$NON-NLS-1$ |
| } |
| return this.columnLabels[index - 1].trim( ); |
| } |
| } |
| |
| public DataTypes getColumnDataType( int index ) throws OdaException { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| throw new OdaException( String.format( |
| "%s.getColumnDataType(%d):\n- Index %d is out of range of available column indices %d-%d", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| )); //$NON-NLS-1$ |
| } |
| 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 ) { |
| throw new OdaException( String.format( |
| "%s.getColumnType(%d):\n- Index %d is out of range of available column indices %d-%d", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| )); //$NON-NLS-1$ |
| } |
| return columnTypes[index-1].getBirtDataTypeId(); |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnTypeName(int) |
| */ |
| public String getColumnTypeName( int index ) throws OdaException { |
| if ( index > getColumnCount( ) || index < 1 ) { |
| throw new OdaException( String.format( |
| "%s.getColumnTypeName(%d):\n- Index %d is out of range of available column indices %d-%d", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| 1, |
| getColumnCount() |
| )); //$NON-NLS-1$ |
| } |
| try { |
| return Driver.getNativeDataTypeName(columnTypes[index-1].getNativeOdaDataTypeId()); |
| } |
| catch (OdaException oe) { |
| throw new OdaException( String.format( |
| "%s.getColumnTypeName(%d):\n- internal exception on index %d: label='%s' / name='%s' / %s='%s'\n- caused by:\n- %s", |
| getClass().getCanonicalName(), |
| index, |
| index, |
| columnLabels[index-1], |
| columnNames[index-1], |
| columnTypes[index-1].getClass().getCanonicalName(), |
| columnTypes[index-1].toString(), |
| oe.getLocalizedMessage() |
| )); //$NON-NLS-1$ |
| } |
| } |
| |
| /* |
| * @see org.eclipse.datatools.connectivity.oda.IResultSetMetaData#getColumnDisplayLength(int) |
| */ |
| public int getColumnDisplayLength( int index ) throws OdaException { |
| throw new UnsupportedOperationException( String.format( |
| "%s.getColumnDisplayLength(%d):\n- Method is unsupported!", |
| getClass().getCanonicalName(), |
| index |
| )); |
| } |
| |
| /* |
| * @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; |
| } |
| } |