/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.eclipse.datatools.connectivity.oda.openmrs.impl; import java.math.BigDecimal; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; import java.util.List; import java.util.ListIterator; import java.util.Map; 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; /** * Implementation class of IResultSet for an ODA runtime driver.
* 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 { private int m_maxRows, m_currentRowId; private boolean wasNull = false; private IResultSetMetaData resultSetMetaData; private ListIterator> iter; private List> data; private Map curRow; /** * Constructor that overrides the default, to accept necessary arguments. * * @param rsmd the current result set meta data * @param tokens the array of Tokens to be used in this ResultSet * @param data the data structure holding the data from the query */ public ResultSet(IResultSetMetaData rsmd, List> data) { this.resultSetMetaData = rsmd; this.data = data; this.iter = this.data.listIterator(); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getMetaData() */ public IResultSetMetaData getMetaData() throws OdaException { return resultSetMetaData; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#setMaxRows(int) */ public void setMaxRows(int max) throws OdaException { m_maxRows = 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 m_maxRows; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#next() */ public boolean next() throws OdaException { // Obey the row limit if it exists if (getMaxRows() > 0 && m_currentRowId > getMaxRows()) { return false; } // Iterate through the List elements and set the current row if (iter.hasNext()) { m_currentRowId++; curRow = iter.next(); return true; } // There was no row left return false; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#close() */ public void close() throws OdaException { // nullify some pointers for garbage collection resultSetMetaData = null; iter = null; data = null; curRow = null; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getRow() */ public int getRow() throws OdaException { return m_currentRowId; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getString(int) */ public String getString(int index) throws OdaException { return getString(resultSetMetaData.getColumnName(index)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getString(java.lang.String) */ public String getString(String columnName) throws OdaException { String ret = curRow.get(columnName); wasNull = (ret == null); return ret; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getInt(int) */ public int getInt(int index) throws OdaException { return Integer.parseInt(getString(index)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getInt(java.lang.String) */ public int getInt(String columnName) throws OdaException { return Integer.parseInt(getString(columnName)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDouble(int) */ public double getDouble(int index) throws OdaException { String doubleRes = getString(index); if (doubleRes.equals("null")) return -1; else return Double.parseDouble(getString(index)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDouble(java.lang.String) */ public double getDouble(String columnName) throws OdaException { return Double.parseDouble(getString(columnName)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBigDecimal(int) */ public BigDecimal getBigDecimal(int index) throws OdaException { return new BigDecimal(getString(index)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBigDecimal(java.lang.String) */ public BigDecimal getBigDecimal(String columnName) throws OdaException { return new BigDecimal(getString(columnName)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDate(int) */ public Date getDate(int index) throws OdaException { // return Date.valueOf(getString(index)); String date = getString(index); if (date.equals("")) return null; else return new Date(Long.parseLong(date)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getDate(java.lang.String) */ public Date getDate(String columnName) throws OdaException { // return Date.valueOf(getString(columnName)); return new Date(Long.parseLong(getString(columnName))); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTime(int) */ public Time getTime(int index) throws OdaException { // return Time.valueOf(getString(index)); String time = getString(index); if (time.equals("")) return null; else return new Time(Long.parseLong(time)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTime(java.lang.String) */ public Time getTime(String columnName) throws OdaException { // return Time.valueOf(getString(columnName)); return new Time(Long.parseLong(getString(columnName))); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTimestamp(int) */ public Timestamp getTimestamp(int index) throws OdaException { // return Timestamp.valueOf(getString(index)); String timestamp = getString(index); if (timestamp.equals("")) return null; else return new Timestamp(Long.parseLong(timestamp)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getTimestamp(java.lang.String) */ public Timestamp getTimestamp(String columnName) throws OdaException { // return Timestamp.valueOf(getString(columnName)); return new Timestamp(Long.parseLong(getString(columnName))); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBoolean(int) */ public boolean getBoolean(int index) throws OdaException { return Boolean.parseBoolean(getString(index)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBoolean(java.lang.String) */ public boolean getBoolean(String columnName) throws OdaException { return Boolean.parseBoolean(getString(columnName)); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBlob(int) */ public IBlob getBlob(int index) throws OdaException { wasNull = true; throw new UnsupportedOperationException(); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getBlob(java.lang.String) */ public IBlob getBlob(String columnName) throws OdaException { wasNull = true; throw new UnsupportedOperationException(); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getClob(int) */ public IClob getClob(int index) throws OdaException { wasNull = true; throw new UnsupportedOperationException(); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#getClob(java.lang.String) */ public IClob getClob(String columnName) throws OdaException { wasNull = true; throw new UnsupportedOperationException(); } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#wasNull() */ public boolean wasNull() throws OdaException { return wasNull; } /* * @see org.eclipse.datatools.connectivity.oda.IResultSet#findColumn(java.lang.String) */ public int findColumn(String columnName) throws OdaException { for (int i = 0; i < resultSetMetaData.getColumnCount(); i++) { if (resultSetMetaData.getColumnName(i).equals(columnName)) { return i; } } throw new OdaException("Column with name " + columnName + " does not exist."); } }