Skip to content

Commit

Permalink
(#29) Moved responsibility for type conversions to the Row class. Ind…
Browse files Browse the repository at this point in the history
…ex mapping stays in JdbcCallableStatement
  • Loading branch information
svettwer committed Feb 8, 2019
1 parent 9033831 commit 43317c5
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.consol.citrus.db.driver;

import com.consol.citrus.db.driver.data.Row;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.http.client.HttpClient;

import java.io.InputStream;
Expand Down Expand Up @@ -74,42 +73,42 @@ public boolean wasNull() throws SQLException {

@Override
public String getString(final int parameterIndex) throws SQLException {
return (String) getData(parameterIndex, String.class);
return (String) getDataRow().getValue(parameterIndex-1, String.class);
}

@Override
public boolean getBoolean(final int parameterIndex) throws SQLException {
return (boolean) getData(parameterIndex, boolean.class);
return (boolean) getDataRow().getValue(parameterIndex-1, boolean.class);
}

@Override
public byte getByte(final int parameterIndex) throws SQLException {
return (byte) getData(parameterIndex, byte.class);
return (byte) getDataRow().getValue(parameterIndex-1, byte.class);
}

@Override
public short getShort(final int parameterIndex) throws SQLException {
return (short) getData(parameterIndex, short.class);
return (short) getDataRow().getValue(parameterIndex-1, short.class);
}

@Override
public int getInt(final int parameterIndex) throws SQLException {
return (int) getData(parameterIndex, int.class);
return (int) getDataRow().getValue(parameterIndex-1, int.class);
}

@Override
public long getLong(final int parameterIndex) throws SQLException {
return (long) getData(parameterIndex, long.class);
return (long) getDataRow().getValue(parameterIndex-1, long.class);
}

@Override
public float getFloat(final int parameterIndex) throws SQLException {
return (float) getData(parameterIndex, float.class);
return (float) getDataRow().getValue(parameterIndex-1, float.class);
}

@Override
public double getDouble(final int parameterIndex) throws SQLException {
return (double) getData(parameterIndex, double.class);
return (double) getDataRow().getValue(parameterIndex-1, double.class);
}

@Override
Expand All @@ -119,32 +118,32 @@ public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throw

@Override
public byte[] getBytes(final int parameterIndex) throws SQLException {
return (byte[]) getData(parameterIndex, byte[].class);
return (byte[]) getDataRow().getValue(parameterIndex-1, byte[].class);
}

@Override
public Date getDate(final int parameterIndex) throws SQLException {
return (Date) getData(parameterIndex, Date.class);
return (Date) getDataRow().getValue(parameterIndex-1, Date.class);
}

@Override
public Time getTime(final int parameterIndex) throws SQLException {
return (Time) getData(parameterIndex, Time.class);
return (Time) getDataRow().getValue(parameterIndex-1, Time.class);
}

@Override
public Timestamp getTimestamp(final int parameterIndex) throws SQLException {
return (Timestamp) getData(parameterIndex, Timestamp.class);
return (Timestamp) getDataRow().getValue(parameterIndex-1, Timestamp.class);
}

@Override
public Object getObject(final int parameterIndex) throws SQLException {
return getData(parameterIndex, Object.class);
return getDataRow().getValue(parameterIndex-1, Object.class);
}

@Override
public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException {
return (BigDecimal) getData(parameterIndex, BigDecimal.class);
return (BigDecimal) getDataRow().getValue(parameterIndex-1, BigDecimal.class);
}

@Override
Expand Down Expand Up @@ -339,72 +338,72 @@ public void setNull(final String parameterName, final int sqlType, final String

@Override
public String getString(final String parameterName) throws SQLException {
return (String) getData(parameterName, String.class);
return (String) getDataRow().getValue(parameterName, String.class);
}

@Override
public boolean getBoolean(final String parameterName) throws SQLException {
return (boolean) getData(parameterName, boolean.class);
return (boolean) getDataRow().getValue(parameterName, boolean.class);
}

@Override
public byte getByte(final String parameterName) throws SQLException {
return (byte) getData(parameterName, byte.class);
return (byte) getDataRow().getValue(parameterName, byte.class);
}

@Override
public short getShort(final String parameterName) throws SQLException {
return (short) getData(parameterName, short.class);
return (short) getDataRow().getValue(parameterName, short.class);
}

@Override
public int getInt(final String parameterName) throws SQLException {
return (int) getData(parameterName, int.class);
return (int) getDataRow().getValue(parameterName, int.class);
}

@Override
public long getLong(final String parameterName) throws SQLException {
return (long) getData(parameterName, long.class);
return (long) getDataRow().getValue(parameterName, long.class);
}

@Override
public float getFloat(final String parameterName) throws SQLException {
return (float) getData(parameterName, float.class);
return (float) getDataRow().getValue(parameterName, float.class);
}

@Override
public double getDouble(final String parameterName) throws SQLException {
return (double) getData(parameterName, double.class);
return (double) getDataRow().getValue(parameterName, double.class);
}

@Override
public byte[] getBytes(final String parameterName) throws SQLException {
return (byte[]) getData(parameterName, byte[].class);
return (byte[]) getDataRow().getValue(parameterName, byte[].class);
}

@Override
public Date getDate(final String parameterName) throws SQLException {
return (Date) getData(parameterName, Date.class);
return (Date) getDataRow().getValue(parameterName, Date.class);
}

@Override
public Time getTime(final String parameterName) throws SQLException {
return (Time) getData(parameterName, Time.class);
return (Time) getDataRow().getValue(parameterName, Time.class);
}

@Override
public Timestamp getTimestamp(final String parameterName) throws SQLException {
return (Timestamp) getData(parameterName, Timestamp.class);
return (Timestamp) getDataRow().getValue(parameterName, Timestamp.class);
}

@Override
public Object getObject(final String parameterName) throws SQLException {
return getData(parameterName, Object.class);
return getDataRow().getValue(parameterName, Object.class);
}

@Override
public BigDecimal getBigDecimal(final String parameterName) throws SQLException {
return (BigDecimal) getData(parameterName, BigDecimal.class);
return (BigDecimal) getDataRow().getValue(parameterName, BigDecimal.class);
}

@Override
Expand Down Expand Up @@ -660,20 +659,6 @@ private void setOutParameter(final String parameterName) {
setParameter(parameterName, "?");
}

private <T> Object getData(final int parameterIndex, final Class<T> clazz) throws SQLException {
final Object value = getDataRow().getValue(parameterIndex - 1);
return convertData(value, clazz);
}

private <T> Object getData(final String parameterName, final Class<T> clazz) throws SQLException {
final Object value = getDataRow().getValue(parameterName);
return convertData(value, clazz);
}

private <T> Object convertData(final Object value, final Class<T> clazz) {
return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz);
}

private Row getDataRow() throws SQLException {
if(Objects.isNull(resultRow)){
resultRow = dataSet.getNextRow();
Expand Down
51 changes: 39 additions & 12 deletions driver/src/main/java/com/consol/citrus/db/driver/data/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package com.consol.citrus.db.driver.data;

import org.apache.commons.beanutils.ConvertUtils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;

/**
* @author Christoph Deppisch
*/
public class Row {

/** Row values with column name as key */
Expand All @@ -35,36 +34,60 @@ public class Row {

/**
* Gets set of column names available in this row.
* @return
* @return A list of column values
*/
public List<String> getColumns() {
return Arrays.asList(values.keySet().toArray(new String[values.size()]));
return Arrays.asList(values.keySet().toArray(new String[0]));
}

/**
* Gets the row value identified by its column name.
* @param columnName
* @return
* @param columnName The name to get the value for
* @return The value of that column
*/
public Object getValue(final String columnName) {
lastValue = values.get(columnName);
return lastValue;
}

/**
* Gets the row value identified by its column name converted into the given type
* @param parameterName The name to get the value for
* @param clazz The class to convert the value to
* @param <T> Generic parameter to ensure correct conversion
* @return The converted object
*/
public <T> Object getValue(final String parameterName, final Class<T> clazz) {
final Object value = getValue(parameterName);
return convertData(value, clazz);
}

/**
* Gets the row value identified by its column index.
* @param columnIndex
* @return
* @param columnIndex The column index to get the value from
* @return The object of that index
*/
public Object getValue(final int columnIndex) {
lastValue = values.values().toArray()[columnIndex];
return lastValue;
}

/**
* Gets the row value identified by its column index converted into the given type
* @param columnIndex The index to get the value from
* @param clazz The class to convert the value to
* @param <T> Generic parameter to ensure correct conversion
* @return The converted object
*/
public <T> Object getValue(final int columnIndex, final Class<T> clazz){
final Object value = getValue(columnIndex);
return convertData(value, clazz);
}

/**
* Gets the values.
*
* @return
* @return The values of the row identified by column manes
*/
public Map<String, Object> getValues() {
return values;
Expand All @@ -73,12 +96,16 @@ public Map<String, Object> getValues() {
/**
* Sets the values.
*
* @param values
* @param values The values to set in the Row
*/
public void setValues(final SortedMap<String, Object> values) {
this.values = values;
}

private <T> Object convertData(final Object value, final Class<T> clazz) {
return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz);
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
Expand All @@ -94,7 +121,7 @@ public int hashCode() {

/**
* Gets the lastValue.
* @return
* @return The last value as object
*/
public Object getLastValue() {
return lastValue;
Expand Down
Loading

0 comments on commit 43317c5

Please sign in to comment.