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.TreeSet;
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.
*/
@Deprecated
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);
public abstract RowList withCycling(boolean cycling);
public abstract boolean isCycling();
/**
* 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(), this.isCycling());
} // end of if
return new RowResultSet(getRows().subList(0, maxRows),
this.isCycling());
} // 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);
private final boolean cycling = false;
// --- 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 unchanged nil row list.
*/
public NilRowList withCycling(final boolean cycling) {
return this;
} // end of withCycling
public boolean isCycling() {
return this.cycling;
} // end of isCycling
/**
* 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;
final Map columnNullables;
List rows;
final AbstractStatement statement;
final SQLWarning warning;
private Object last;
// TODO: tableName
// --- Constructors ---
/**
* Constructor
* @param rows the list of rows
*/
protected RowResultSet(final List rows, final boolean cycling) {
this(rows, null, null, null, cycling);
} // 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
* @param cycling
*/
private RowResultSet(final List rows,
final Object last,
final AbstractStatement statement,
final SQLWarning warning,
final boolean cycling) {
this(getColumnClasses(), getColumnLabels(), getColumnNullables(),
rows, last, statement, warning, cycling);
} // end of
/**
* Copy constructor.
*
* @param columnClasses the column classes
* @param columnLabels the column labels
* @param columnNullables the nullable flags for the columns
* @param rows the list of rows
* @param last the cursor to last result
* @param statement the associated statement
* @param warning the SQL warning
* @param cycling
*/
private RowResultSet(final List> columnClasses,
final Map columnLabels,
final Map columnNullables,
final List rows,
final Object last,
final AbstractStatement statement,
final SQLWarning warning,
final boolean cycling) {
if (columnClasses == null || columnLabels == null ||
columnNullables == null || rows == null) {
// Impossible
throw new IllegalArgumentException();
} // end of if
this.columnClasses = columnClasses;
this.columnLabels = columnLabels;
this.columnNullables = columnNullables;
this.rows = Collections.unmodifiableList(rows);
this.statement = statement;
this.warning = warning;
this.last = null;
super.fetchSize = rows.size();
super.cycling = cycling;
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, this.cycling);
} // end of withStatement
/**
* Returns updated resultset, with cycling set.
*/
public RowResultSet withCycling(final boolean cycling) {
return new RowResultSet(this.rows, this.last,
this.statement, this.warning, cycling);
} // end of withCycling
/**
* 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, this.cycling);
} // end of withWarning
/**
* Returns update resultset, with rows only including values
* for the specified column.
*
* @param columnNames the names of the columns
* @return Result set with the projected rows
*/
public RowResultSet withProjection(final String[] columnNames) {
final ArrayList indexes = new ArrayList();
final HashMap labels =
new HashMap();
final HashMap nullables =
new HashMap();
int i = 1;
for (final String name : columnNames) {
final Integer index = this.columnLabels.get(name);
final Boolean nable = this.columnNullables.get(index);
if (index != null) {
final Integer updIdx = new Integer(i++);
indexes.add(index);
labels.put(name, updIdx);
nullables.put(updIdx, nable);
}
}
return withProjection(indexes, labels, nullables);
} // end of withProjection
public RowResultSet withProjection(final int[] columnIndexes) {
final ArrayList indexes = new ArrayList();
final HashMap labels =
new HashMap();
final HashMap nullables =
new HashMap();
int upd = 1;
for (final int index : columnIndexes) {
for (final String columnName : this.columnLabels.keySet()) {
final Integer i = this.columnLabels.get(columnName);
if (i.intValue() == index) {
final Boolean nable = this.columnNullables.get(i);
final Integer updIdx = new Integer(upd++);
indexes.add(i);
labels.put(columnName, updIdx);
nullables.put(updIdx, nable);
}
}
}
return withProjection(indexes, labels, nullables);
}
private RowResultSet withProjection(final List columnIndexes, final Map labels, final Map nullables) {
final ArrayList> classes =
new ArrayList>(labels.size());
for (final String name : labels.keySet()) {
Integer index = labels.get(name);
Class> cls = this.columnClasses.get(index.intValue()-1);
classes.add(cls);
}
// ---
final ArrayList projected =
new ArrayList(this.rows.size());
for (final R row : this.rows) {
final List