Skip to content

Commit

Permalink
Merge ad20aa2 into 7113291
Browse files Browse the repository at this point in the history
  • Loading branch information
Saisimon committed Jul 16, 2019
2 parents 7113291 + ad20aa2 commit 90a93ba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
Expand Up @@ -53,8 +53,6 @@ public static Object getValue(final ResultSet resultSet, final int columnIndex)
public static Object getValueByColumnType(final ResultSet resultSet, final int columnIndex) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
switch (metaData.getColumnType(columnIndex)) {
case Types.BIT:
return resultSet.getBytes(columnIndex);
case Types.BOOLEAN:
return resultSet.getBoolean(columnIndex);
case Types.TINYINT:
Expand Down
Expand Up @@ -62,7 +62,7 @@ public void setUp() {
public void assertGetValueByBit() {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.BIT);
byte[] bytes = {1};
when(resultSet.getBytes(1)).thenReturn(bytes);
when(resultSet.getObject(1)).thenReturn(bytes);
assertThat((byte[]) QueryResultUtil.getValue(resultSet, 1), is(bytes));
}

Expand Down
Expand Up @@ -17,15 +17,20 @@

package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.exception.ShardingException;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.shardingsphere.core.exception.ShardingException;

import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

/**
* ResultSet utility class.
*
Expand Down Expand Up @@ -54,6 +59,9 @@ public static Object convertValue(final Object value, final Class<?> convertType
if (value instanceof Date) {
return convertDateValue(value, convertType);
}
if (value instanceof byte[]) {
return convertByteArrayValue(value, convertType);
}
if (String.class.equals(convertType)) {
return value.toString();
} else {
Expand Down Expand Up @@ -123,4 +131,20 @@ private static Object convertDateValue(final Object value, final Class<?> conver
throw new ShardingException("Unsupported Date type:%s", convertType);
}
}

private static Object convertByteArrayValue(final Object value, final Class<?> convertType) {
byte[] bs = (byte[]) value;
switch (bs.length) {
case 1:
return convertNumberValue(bs[0], convertType);
case Shorts.BYTES:
return convertNumberValue(Shorts.fromByteArray(bs), convertType);
case Ints.BYTES:
return convertNumberValue(Ints.fromByteArray(bs), convertType);
case Longs.BYTES:
return convertNumberValue(Longs.fromByteArray(bs), convertType);
default:
return value;
}
}
}
Expand Up @@ -20,6 +20,10 @@
import org.apache.shardingsphere.core.exception.ShardingException;
import org.junit.Test;

import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
Expand Down Expand Up @@ -83,6 +87,19 @@ public void assertConvertDateValueSuccess() {
assertThat((Timestamp) ResultSetUtil.convertValue(now, Timestamp.class), is(new Timestamp(now.getTime())));
}

@Test
public void assertConvertByteArrayValueSuccess() {
byte[] bs = new byte[] {};
assertThat((byte[]) ResultSetUtil.convertValue(bs, byte.class), is(bs));
assertThat((byte) ResultSetUtil.convertValue(new byte[] {1}, byte.class), is((byte) 1));
assertThat((short) ResultSetUtil.convertValue(Shorts.toByteArray((short)1), short.class), is((short) 1));
assertThat((int) ResultSetUtil.convertValue(Ints.toByteArray(1), int.class), is(1));
assertThat((long) ResultSetUtil.convertValue(Longs.toByteArray(1L), long.class), is(1L));
assertThat((double) ResultSetUtil.convertValue(Longs.toByteArray(1L), double.class), is(1d));
assertThat((float) ResultSetUtil.convertValue(Longs.toByteArray(1L), float.class), is(1f));
assertThat((BigDecimal) ResultSetUtil.convertValue(Longs.toByteArray(1L), BigDecimal.class), is(new BigDecimal("1")));
}

@Test(expected = ShardingException.class)
public void assertConvertDateValueError() {
ResultSetUtil.convertValue(new Date(), int.class);
Expand Down
Expand Up @@ -64,6 +64,8 @@ public void write(final MySQLPacketPayload payload) {
payload.writeStringLenenc(each.toString().split("\\.")[0]);
} else if (each instanceof BigDecimal) {
payload.writeStringLenenc(((BigDecimal) each).toPlainString());
} else if (each instanceof Boolean) {
payload.writeBytesLenenc((Boolean)each ? new byte[]{1} : new byte[]{0});
} else {
payload.writeStringLenenc(each.toString());
}
Expand Down

0 comments on commit 90a93ba

Please sign in to comment.