All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.data.JdbcDataSetStrategy Maven / Gradle / Ivy

/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MetaModel.  If not, see .
 */
package dk.eobjects.metamodel.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dk.eobjects.metamodel.MetaModelException;
import dk.eobjects.metamodel.query.Query;
import dk.eobjects.metamodel.query.SelectItem;

class JdbcDataSetStrategy implements IDataSetStrategy {

	private static final Log _log = LogFactory
			.getLog(JdbcDataSetStrategy.class);
	private Statement _statement;
	private ResultSet _resultSet;
	private SelectItem[] _selectItems;
	private Row _row;

	public JdbcDataSetStrategy(Query query, Statement statement,
			ResultSet resultSet) {
		if (query == null || statement == null || resultSet == null) {
			throw new IllegalArgumentException("Arguments cannot be null");
		}
		List items = query.getSelectClause().getItems();
		_selectItems = items.toArray(new SelectItem[items.size()]);
		_statement = statement;
		_resultSet = resultSet;
	}

	public JdbcDataSetStrategy(SelectItem[] selectItems, Statement statement,
			ResultSet resultSet) {
		if (selectItems == null || statement == null || resultSet == null) {
			throw new IllegalArgumentException("Arguments cannot be null");
		}
		_selectItems = selectItems;
		_statement = statement;
		_resultSet = resultSet;
	}

	public Row getRow() throws MetaModelException {
		return _row;
	}

	public boolean next() throws MetaModelException {
		try {
			boolean result = _resultSet.next();
			if (result) {
				Object[] values = new Object[_selectItems.length];
				for (int i = 0; i < values.length; i++) {
					values[i] = _resultSet.getObject(i + 1);
				}
				_row = new Row(_selectItems, values);
			} else {
				_row = null;
			}
			return result;
		} catch (SQLException e) {
			_log.error(e);
		}
		return false;
	}

	public void close() {
		if (_resultSet != null) {
			try {
				_resultSet.close();
				_resultSet = null;
			} catch (SQLException e) {
				_log.info("Could not close ResultSet", e);
			}
		}
		if (_statement != null) {
			try {
				_statement.close();
				_statement = null;
			} catch (SQLException e) {
				_log.info("Could not close Statement", e);
			}
		}
	}

	public SelectItem[] getSelectItems() {
		return _selectItems;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy