Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package acolyte.jdbc;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Timestamp;
import java.sql.Array;
import java.sql.Date;
import java.sql.Time;
import javax.sql.rowset.serial.SerialBlob;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.io.IOUtils;
/**
* Type-safe list of row.
*
* @author Cedric Chantepie
*/
public abstract class RowList {
/**
* Returns unmodifiable rows list.
* @return the list of row
*/
public abstract List getRows();
/**
* Appends |row|.
*
* @param row the row to be appended
* @return Updated row list
* @deprecated Append operation with multiple column values provided by sub-classes.
*/
protected abstract RowList append(R row);
/**
* Returns copy of row list with updated column names/labels.
*
* @param columnIndex Index of column (first index is 1)
* @param label Column name/label
* @return a row list with specified column label
* @see Column#name
*/
public abstract RowList withLabel(int columnIndex, String label);
/**
* Returns copy of row list with updated metadata,
* including whether specified column is |nullable|.
*
* @param columnIndex Index of column (first index is 1)
* @param nullable Is column nullable?
* @return a row list with given nullable flag
* @see Column#nullable
*/
public abstract RowList withNullable(int columnIndex, boolean nullable);
/**
* Returns result set from these rows.
*
* @param maxRows Limit for the maximum number of rows.
* If <= 0 no limit will be set.
* If the limit is set and exceeded, the excess rows are silently dropped.
* @return ResultSet for this list of rows
*/
public RowResultSet resultSet(int maxRows) {
if (maxRows <= 0) {
return new RowResultSet(getRows());
} // end of if
return new RowResultSet(getRows().subList(0, maxRows));
} // end of resultSet
/**
* Returns result set from these rows (without row limit).
*
* @see #resultSet(int)
* @return ResultSet for this list of rows
*/
public RowResultSet resultSet() {
return resultSet(0);
} // end of resultSet
/**
* Returns this row list wrapped as handler result.
* @return Query result from this list of rows
*/
public QueryResult asResult() {
return new QueryResult.Default(this);
} // end of asResult
/**
* Returns ordered classes of columns.
* @return Classes of columns
*/
public abstract List> getColumnClasses();
/**
* Gets column mappings, from index to label.
*
* @return Column mappings, or empty map (not null) if none
*/
public abstract Map getColumnLabels();
/**
* Gets column mappings, from index to nullable flag.
*
* @return Column mappings, or empty map (not null) if none
*/
public abstract Map getColumnNullables();
// --- Shared ---
/**
* Nil row list instance
*/
public static final NilRowList Nil = new NilRowList();
// --- Inner classes ---
/**
* Nil row list.
*/
private static final class NilRowList extends RowList {
// --- Properties ---
/**
* Empty list of row
*/
private final List rows =
new ArrayList(0);
/**
* Empty list of column classes
*/
private final List> colClasses = new ArrayList>(0);
/**
* Empty labels
*/
private final Map labels =
new HashMap(0);
/**
* Empty flags
*/
private final Map nullables =
new HashMap(0);
// --- Constructors ---
/**
* No-arg constructor.
*/
private NilRowList() { }
// ---
/**
* Returns empty list of row
*/
public List getRows() {
return this.rows;
} // end of getRows
/**
* Returns unchanged nil row list.
*/
public NilRowList append(final Row.Nothing row) { return this; }
/**
* Returns unchanged nil row list.
*/
public NilRowList withLabel(final int columnIndex, final String label) {
return this;
} // end of withLabel
/**
* Returns unchanged nil row list.
*/
public NilRowList withNullable(final int columnIndex,
final boolean nullable) {
return this;
} // end of withNullable
/**
* Returns empty list of columns classes.
*/
public List> getColumnClasses() {
return this.colClasses;
} // end of getColumnClasses
/**
* Returns empty list of column labels.
*/
public Map getColumnLabels() {
return this.labels;
} // end of getColumnLabels
/**
* Returns empty list of nullable flags.
*/
public Map getColumnNullables() {
return this.nullables;
} // end of getColumnNullables
} // end of class Nil
/**
* Creates column definition.
*
* @param the type of the column
* @param columnClass the class of the column
* @param name the column name
* @return the column definition
* @throws IllegalArgumentException if |columnClass| is null,
* or |name| is empty.
*/
public static Column Column(final Class columnClass,
final String name) {
return new Column(columnClass, name);
} // end of column
/**
* Result set made from list of row.
*
* @param the row type
*/
public final class RowResultSet extends AbstractResultSet {
final List> columnClasses;
final Map columnLabels;
List rows;
final AbstractStatement statement;
final SQLWarning warning;
private Object last;
// --- Constructors ---
/**
* Constructor
* @param rows the list of rows
*/
protected RowResultSet(final List rows) {
if (rows == null) {
// Impossible
throw new IllegalArgumentException();
} // end of if
this.columnClasses = getColumnClasses();
this.columnLabels = getColumnLabels();
this.rows = Collections.unmodifiableList(rows);
this.statement = null; // detached
this.warning = null;
this.last = null;
super.fetchSize = rows.size();
} // end of
/**
* Copy constructor.
*
* @param rows the list of rows
* @param last the cursor to last result
* @param statement the associated statement
* @param warning the SQL warning
*/
private RowResultSet(final List rows,
final Object last,
final AbstractStatement statement,
final SQLWarning warning) {
if (rows == null) {
// Impossible
throw new IllegalArgumentException();
} // end of if
this.columnClasses = getColumnClasses();
this.columnLabels = getColumnLabels();
this.rows = Collections.unmodifiableList(rows);
this.statement = statement;
this.warning = warning;
this.last = null;
super.fetchSize = rows.size();
if (this.statement != null && super.fetchSize > 0 &&
"true".equals(this.statement.connection.getProperties().
get("acolyte.resultSet.initOnFirstRow"))) {
// Initially move to first row, contrary to JDBC specs
this.row = 1;
}
} // end of
// ---
/**
* Returns updated resultset, attached with given |statement|.
*
* @param statement the associated statement
* @return Result set associated with given statement
*/
public RowResultSet withStatement(final AbstractStatement statement) {
return new RowResultSet(this.rows, this.last,
statement, this.warning);
} // end of withStatement
/**
* Returns updated resultset, with given |warning|.
*
* @param warning the SQL warning
* @return Result set associated with SQL warning
*/
public RowResultSet withWarning(final SQLWarning warning) {
return new RowResultSet(this.rows, this.last,
this.statement, warning);
} // end of withWarning
/**
* {@inheritDoc}
*/
public SQLWarning getWarnings() throws SQLException {
return this.warning;
} // end of getWarnings
/**
* {@inheritDoc}
*/
public boolean equals(final Object o) {
if (o == null || !(o instanceof RowResultSet)) {
return false;
} // end of if
// ---
@SuppressWarnings("unchecked")
final RowResultSet other = (RowResultSet) o;
return new EqualsBuilder().
append(this.rows, other.rows).
append(this.last, other.last).
append(this.columnClasses, other.columnClasses).
append(this.columnLabels, other.columnLabels).
isEquals();
} // end of equals
/**
* {@inheritDoc}
*/
public int hashCode() {
return new HashCodeBuilder(11, 9).
append(this.rows).
append(this.last).
append(this.columnClasses).
append(this.columnLabels).
toHashCode();
} // end of hashCode
// --- ResultSet implementation ---
/**
* {@inheritDoc}
*/
public AbstractStatement getStatement() {
return this.statement;
} // end of getStatement
/**
* {@inheritDoc}
*/
public void setFetchSize(final int maxRows) throws SQLException {
checkClosed();
synchronized(this) {
if (maxRows > this.rows.size()) {
return;
}
this.rows = this.rows.subList(0, maxRows);
}
} // end of setFetchSize
/**
* {@inheritDoc}
*/
public int getFetchSize() throws SQLException {
checkClosed();
return this.fetchSize;
} // end of getFetchSize
/**
* {@inheritDoc}
*/
public boolean wasNull() throws SQLException {
checkClosed();
return (this.last == null);
} // end of wasNull
/**
* {@inheritDoc}
*/
public Object getObject(final int columnIndex) throws SQLException {
checkClosed();
if (!isOn()) {
throw new SQLException("Not on a row");
} // end of if
final int idx = columnIndex - 1;
final List