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

com.j256.ormlite.stmt.RawResultsImpl Maven / Gradle / Ivy

Go to download

Lightweight Object Relational Model (ORM) for persisting objects to SQL databases.

There is a newer version: 6.1
Show newest version
package com.j256.ormlite.stmt;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.dao.ObjectCache;
import com.j256.ormlite.misc.IOUtils;
import com.j256.ormlite.support.CompiledStatement;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.support.DatabaseConnection;

/**
 * Handler for our raw results objects which does the conversion for various different results: String[], Object[], and
 * user defined T.
 * 
 * @author graywatson
 */
public class RawResultsImpl implements GenericRawResults {

	private SelectIterator iterator;
	private final String[] columnNames;

	public RawResultsImpl(ConnectionSource connectionSource, DatabaseConnection connection, String query,
			Class clazz, CompiledStatement compiledStmt, GenericRowMapper rowMapper, ObjectCache objectCache)
			throws SQLException {
		iterator =
				new SelectIterator(clazz, null, rowMapper, connectionSource, connection, compiledStmt, query,
						objectCache);
		/*
		 * NOTE: we _have_ to get these here before the results object is closed if there are no results
		 */
		this.columnNames = iterator.getRawResults().getColumnNames();
	}

	@Override
	public int getNumberColumns() {
		return columnNames.length;
	}

	@Override
	public String[] getColumnNames() {
		return columnNames;
	}

	@Override
	public List getResults() throws SQLException {
		List results = new ArrayList();
		try {
			while (iterator.hasNext()) {
				results.add(iterator.next());
			}
			return results;
		} finally {
			IOUtils.closeThrowSqlException(this, "raw results iterator");
		}
	}

	@Override
	public T getFirstResult() throws SQLException {
		try {
			if (iterator.hasNextThrow()) {
				return iterator.nextThrow();
			} else {
				return null;
			}
		} finally {
			IOUtils.closeThrowSqlException(this, "raw results iterator");
		}
	}

	@Override
	public CloseableIterator iterator() {
		return iterator;
	}

	@Override
	public CloseableIterator closeableIterator() {
		return iterator;
	}

	@Override
	public void close() throws IOException {
		if (iterator != null) {
			iterator.close();
			iterator = null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy