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

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

Go to download

The eobjects.dk MetaModel is a common domain model, query-engine and optimizer for different kinds of datastores.

The newest version!
/*
 * Copyright 2008 eobjects.dk
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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