com.j256.ormlite.stmt.RawResultsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ormlite-core Show documentation
Show all versions of ormlite-core Show documentation
Lightweight Object Relational Model (ORM) for persisting objects to SQL databases.
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;
}
}
}