com.mycila.testing.plugin.db.SqlDataImpl Maven / Gradle / Ivy
/**
* Copyright (C) 2008 Mathieu Carbou
*
* 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 com.mycila.testing.plugin.db;
import com.mycila.testing.plugin.db.api.SqlColumn;
import com.mycila.testing.plugin.db.api.SqlColumnHeader;
import com.mycila.testing.plugin.db.api.SqlData;
import com.mycila.testing.plugin.db.api.SqlResults;
import com.mycila.testing.plugin.db.api.SqlRow;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
/**
* @author Mathieu Carbou ([email protected])
*/
final class SqlDataImpl implements SqlData {
final SqlResults results;
final SqlRow sqlRow;
final SqlColumn sqlColumn;
final Object object;
SqlDataImpl(SqlResults results, SqlRow sqlRow, SqlColumn sqlColumn, ResultSet resultSet) {
this.results = results;
this.sqlRow = sqlRow;
this.sqlColumn = sqlColumn;
try {
this.object = JdbcUtils.getResultSetValue(resultSet, sqlColumn.index() + 1, sqlColumn.header().type());
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public int rowIndex() {
return sqlRow.index();
}
public SqlRow row() {
return sqlRow;
}
public int columnIndex() {
return sqlColumn.index();
}
public SqlColumn column() {
return sqlColumn;
}
public SqlColumnHeader header() {
return sqlColumn.header();
}
@SuppressWarnings({"unchecked"})
public Object value() {
return object;
}
public boolean isNull() {
return object == null;
}
public URL asURL() {
return as(URL.class, Converters.unconvertible());
}
public Blob asBlob() {
return as(Blob.class, Converters.unconvertible());
}
public Clob asClob() {
return as(Clob.class, Converters.unconvertible());
}
public Boolean asBoolean() {
return as(Boolean.class, Converters.BOOL_CONVERTER);
}
public Byte asByte() {
return as(Byte.class, Converters.BYTE_CONVERTER);
}
public Short asShort() {
return as(Short.class, Converters.SHORT_CONVERTER);
}
public Integer asInt() {
return as(Integer.class, Converters.INTEGER_CONVERTER);
}
public Long asLong() {
return as(Long.class, Converters.LONG_CONVERTER);
}
public Float asFloat() {
return as(Float.class, Converters.FLOAT_CONVERTER);
}
public Double asDouble() {
return as(Double.class, Converters.DOUBLE_CONVERTER);
}
public BigDecimal asBigDecimal() {
return as(BigDecimal.class, Converters.BIGDECIMAL_CONVERTER);
}
public Object[] asArray() {
return as(Object[].class, Converters.