This is an automated email from the ASF dual-hosted git repository.
zhangyonglun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8ecc928 Support more java types for getObject (#7601)
8ecc928 is described below
commit 8ecc928fe2e72edd8be9fb72e2deeced1d9ff29e
Author: xbkaishui <[email protected]>
AuthorDate: Wed Sep 30 17:31:10 2020 +0800
Support more java types for getObject (#7601)
* refactor change YamlRuleSchemaMetaData same with RuleSchemaMetaData
* change for cr
* add test of default constructor
* Support url and bigdecimal data
* fix test fail
* change as cr comment
* change as suggestions
* add final
---
.../core/resultset/DatabaseMetaDataResultSet.java | 40 +++++++++++++++
.../driver/jdbc/core/resultset/ResultSetUtil.java | 51 +++++++++++++++++-
...stractUnsupportedDatabaseMetaDataResultSet.java | 32 ------------
.../resultset/DatabaseMetaDataResultSetTest.java | 60 ++++++++++++++++++++--
.../jdbc/core/resultset/ResultSetUtilTest.java | 46 ++++++++++++++++-
.../UnSupportedDatabaseMetaDataResultSetTest.java | 30 -----------
6 files changed, 190 insertions(+), 69 deletions(-)
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
index 21d4504..2326cfd 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
@@ -22,6 +22,8 @@ import
org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedData
import org.apache.shardingsphere.infra.rule.DataNodeRoutedRule;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import java.math.BigDecimal;
+import java.net.URL;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
@@ -144,6 +146,32 @@ public final class DatabaseMetaDataResultSet extends
AbstractUnsupportedDatabase
}
@Override
+ public BigDecimal getBigDecimal(final int columnIndex, final int scale)
throws SQLException {
+ return getBigDecimal(columnIndex, true, scale);
+ }
+
+ @Override
+ public BigDecimal getBigDecimal(final String columnLabel, final int scale)
throws SQLException {
+ return getBigDecimal(findColumn(columnLabel), scale);
+ }
+
+ @Override
+ public BigDecimal getBigDecimal(final int columnIndex) throws SQLException
{
+ return getBigDecimal(columnIndex, false, 0);
+ }
+
+ private BigDecimal getBigDecimal(final int columnIndex, final boolean
needScale, final int scale) throws SQLException {
+ checkClosed();
+ checkColumnIndex(columnIndex);
+ return (BigDecimal)
ResultSetUtil.convertBigDecimalValue(currentDatabaseMetaDataObject.getObject(columnIndex),
needScale, scale);
+ }
+
+ @Override
+ public BigDecimal getBigDecimal(final String columnLabel) throws
SQLException {
+ return getBigDecimal(findColumn(columnLabel));
+ }
+
+ @Override
public String getString(final int columnIndex) throws SQLException {
checkClosed();
checkColumnIndex(columnIndex);
@@ -298,6 +326,18 @@ public final class DatabaseMetaDataResultSet extends
AbstractUnsupportedDatabase
}
@Override
+ public URL getURL(final int columnIndex) throws SQLException {
+ checkClosed();
+ checkColumnIndex(columnIndex);
+ return (URL)
ResultSetUtil.convertValue(currentDatabaseMetaDataObject.getObject(columnIndex),
URL.class);
+ }
+
+ @Override
+ public URL getURL(final String columnLabel) throws SQLException {
+ return getURL(findColumn(columnLabel));
+ }
+
+ @Override
public ResultSetMetaData getMetaData() throws SQLException {
checkClosed();
return resultSetMetaData;
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
index 416da8a..56fb8b3 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtil.java
@@ -25,6 +25,8 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import java.math.BigDecimal;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
@@ -41,7 +43,7 @@ public final class ResultSetUtil {
/**
* Convert value via expected class type.
- *
+ *
* @param value original value
* @param convertType expected class type
* @return converted value
@@ -49,7 +51,7 @@ public final class ResultSetUtil {
public static Object convertValue(final Object value, final Class<?>
convertType) {
if (null == value) {
return convertNullValue(convertType);
- }
+ }
if (value.getClass() == convertType) {
return value;
}
@@ -62,6 +64,9 @@ public final class ResultSetUtil {
if (LocalTime.class.equals(convertType)) {
return convertLocalTimeValue(value);
}
+ if (URL.class.equals(convertType)) {
+ return convertURL(value);
+ }
if (value instanceof Number) {
return convertNumberValue(value, convertType);
}
@@ -77,6 +82,48 @@ public final class ResultSetUtil {
return value;
}
}
+
+ private static Object convertURL(final Object value) {
+ String val = value.toString();
+ try {
+ return new URL(val);
+ } catch (final MalformedURLException ex) {
+ throw new ShardingSphereException("Unsupported Date type: URL
value %s", value);
+ }
+ }
+
+ /**
+ * Convert object to BigDecimal.
+ *
+ * @param value current db object
+ * @param needScale need scale
+ * @param scale scale size
+ * @return big decimal
+ */
+ public static Object convertBigDecimalValue(final Object value, final
boolean needScale, final int scale) {
+ if (null == value) {
+ return convertNullValue(BigDecimal.class);
+ }
+ if (value.getClass() == BigDecimal.class) {
+ return adjustBigDecimalResult((BigDecimal) value, needScale,
scale);
+ }
+ if (value instanceof Number || value instanceof String) {
+ BigDecimal bigDecimal = new BigDecimal(value.toString());
+ return adjustBigDecimalResult(bigDecimal, needScale, scale);
+ }
+ throw new ShardingSphereException("Unsupported Date type: BigDecimal
value %s", value);
+ }
+
+ private static BigDecimal adjustBigDecimalResult(final BigDecimal value,
final boolean needScale, final int scale) {
+ if (needScale) {
+ try {
+ return value.setScale(scale);
+ } catch (final ArithmeticException ex) {
+ return value.setScale(scale, BigDecimal.ROUND_HALF_UP);
+ }
+ }
+ return value;
+ }
private static Object convertLocalDateTimeValue(final Object value) {
Timestamp timestamp = (Timestamp) value;
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedDatabaseMetaDataResultSet.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedDatabaseMetaDataResultSet.java
index aedd08c..dbb637a 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedDatabaseMetaDataResultSet.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedDatabaseMetaDataResultSet.java
@@ -19,8 +19,6 @@ package org.apache.shardingsphere.driver.jdbc.unsupported;
import java.io.InputStream;
import java.io.Reader;
-import java.math.BigDecimal;
-import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
@@ -40,26 +38,6 @@ import java.util.Calendar;
public abstract class AbstractUnsupportedDatabaseMetaDataResultSet extends
AbstractUnsupportedOperationResultSet {
@Override
- public final BigDecimal getBigDecimal(final int columnIndex, final int
scale) throws SQLException {
- throw new SQLFeatureNotSupportedException("getBigDecimal");
- }
-
- @Override
- public final BigDecimal getBigDecimal(final String columnLabel, final int
scale) throws SQLException {
- throw new SQLFeatureNotSupportedException("getBigDecimal");
- }
-
- @Override
- public final BigDecimal getBigDecimal(final int columnIndex) throws
SQLException {
- throw new SQLFeatureNotSupportedException("getBigDecimal");
- }
-
- @Override
- public final BigDecimal getBigDecimal(final String columnLabel) throws
SQLException {
- throw new SQLFeatureNotSupportedException("getBigDecimal");
- }
-
- @Override
public final InputStream getAsciiStream(final int columnIndex) throws
SQLException {
throw new SQLFeatureNotSupportedException("getAsciiStream");
}
@@ -175,16 +153,6 @@ public abstract class
AbstractUnsupportedDatabaseMetaDataResultSet extends Abstr
}
@Override
- public final URL getURL(final int columnIndex) throws SQLException {
- throw new SQLFeatureNotSupportedException("getURL");
- }
-
- @Override
- public final URL getURL(final String columnLabel) throws SQLException {
- throw new SQLFeatureNotSupportedException("getURL");
- }
-
- @Override
public final SQLXML getSQLXML(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getSQLXML");
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSetTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSetTest.java
index f0bb8a9..5863d27 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSetTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSetTest.java
@@ -24,6 +24,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.math.BigDecimal;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.sql.ResultSet;
@@ -66,6 +69,16 @@ public final class DatabaseMetaDataResultSetTest {
private static final Date DATE = new Date(System.currentTimeMillis());
+ private static final String URL_COLUMN_LABEL = "URL";
+
+ private static URL url;
+
+ private static final String BIG_DECIMAL_COLUMN_LABEL = "BIGDECIMAL";
+
+ private static final BigDecimal BIGDECIMAL = BigDecimal.valueOf(12.22);
+
+ private static final BigDecimal BIGDECIMAL_SCALA_ONE =
BigDecimal.valueOf(12.2);
+
private static final String INDEX_NAME_COLUMN_LABEL = "INDEX_NAME";
private static final String ACTUAL_INDEX_NAME = "idx_index_test_table_0";
@@ -78,19 +91,22 @@ public final class DatabaseMetaDataResultSetTest {
private DatabaseMetaDataResultSet databaseMetaDataResultSet;
@Before
- public void setUp() throws SQLException {
+ public void setUp() throws SQLException, MalformedURLException {
+ url = new URL("http://apache.org/");
mockResultSetMetaData();
databaseMetaDataResultSet = new
DatabaseMetaDataResultSet(mockResultSet(),
Collections.singletonList(mockShardingRule()));
}
private void mockResultSetMetaData() throws SQLException {
- when(resultSetMetaData.getColumnCount()).thenReturn(6);
+ when(resultSetMetaData.getColumnCount()).thenReturn(8);
when(resultSetMetaData.getColumnLabel(1)).thenReturn(TABLE_NAME_COLUMN_LABEL);
when(resultSetMetaData.getColumnLabel(2)).thenReturn(NON_TABLE_NAME_COLUMN_LABEL);
when(resultSetMetaData.getColumnLabel(3)).thenReturn(NUMBER_COLUMN_LABEL);
when(resultSetMetaData.getColumnLabel(4)).thenReturn(BYTES_COLUMN_LABEL);
when(resultSetMetaData.getColumnLabel(5)).thenReturn(DATE_COLUMN_LABEL);
when(resultSetMetaData.getColumnLabel(6)).thenReturn(INDEX_NAME_COLUMN_LABEL);
+ when(resultSetMetaData.getColumnLabel(7)).thenReturn(URL_COLUMN_LABEL);
+
when(resultSetMetaData.getColumnLabel(8)).thenReturn(BIG_DECIMAL_COLUMN_LABEL);
}
private ResultSet mockResultSet() throws SQLException {
@@ -102,6 +118,8 @@ public final class DatabaseMetaDataResultSetTest {
when(result.getObject(4)).thenReturn(BYTES);
when(result.getObject(5)).thenReturn(DATE);
when(result.getString(6)).thenReturn(ACTUAL_INDEX_NAME);
+ when(result.getObject(7)).thenReturn(url);
+ when(result.getObject(8)).thenReturn(BIGDECIMAL);
when(result.getType()).thenReturn(ResultSet.TYPE_FORWARD_ONLY);
when(result.getConcurrency()).thenReturn(ResultSet.CONCUR_READ_ONLY);
when(result.next()).thenReturn(true, true, false);
@@ -303,6 +321,42 @@ public final class DatabaseMetaDataResultSetTest {
}
@Test
+ public void assertGetURLWithIndex() throws SQLException {
+ databaseMetaDataResultSet.next();
+ assertThat(databaseMetaDataResultSet.getURL(7), is(url));
+ }
+
+ @Test
+ public void assertGetURLWithLabel() throws SQLException {
+ databaseMetaDataResultSet.next();
+ assertThat(databaseMetaDataResultSet.getURL(URL_COLUMN_LABEL),
is(url));
+ }
+
+ @Test
+ public void assertGetBigDecimalWithIndex() throws SQLException {
+ databaseMetaDataResultSet.next();
+ assertThat(databaseMetaDataResultSet.getBigDecimal(8), is(BIGDECIMAL));
+ }
+
+ @Test
+ public void assertGetBigDecimalWithLabel() throws SQLException {
+ databaseMetaDataResultSet.next();
+
assertThat(databaseMetaDataResultSet.getBigDecimal(BIG_DECIMAL_COLUMN_LABEL),
is(BIGDECIMAL));
+ }
+
+ @Test
+ public void assertGetBigDecimalWithIndexAndScale() throws SQLException {
+ databaseMetaDataResultSet.next();
+ assertThat(databaseMetaDataResultSet.getBigDecimal(8, 1),
is(BIGDECIMAL_SCALA_ONE));
+ }
+
+ @Test
+ public void assertGetBigDecimalWithLabelAndScale() throws SQLException {
+ databaseMetaDataResultSet.next();
+
assertThat(databaseMetaDataResultSet.getBigDecimal(BIG_DECIMAL_COLUMN_LABEL,
1), is(BIGDECIMAL_SCALA_ONE));
+ }
+
+ @Test
public void assertGetMetaData() throws SQLException {
assertThat(databaseMetaDataResultSet.getMetaData(),
is(resultSetMetaData));
}
@@ -355,7 +409,7 @@ public final class DatabaseMetaDataResultSetTest {
@Test(expected = SQLException.class)
public void assertGetObjectOutOfIndexRange() throws SQLException {
databaseMetaDataResultSet.next();
- databaseMetaDataResultSet.getObject(7);
+ databaseMetaDataResultSet.getObject(9);
}
@Test(expected = SQLException.class)
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
index 2eb46af..d1e747c 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ResultSetUtilTest.java
@@ -20,10 +20,13 @@ package
org.apache.shardingsphere.driver.jdbc.core.resultset;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.junit.Test;
import java.math.BigDecimal;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
@@ -31,6 +34,7 @@ import java.util.Date;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
public final class ResultSetUtilTest {
@@ -40,7 +44,7 @@ public final class ResultSetUtilTest {
assertThat(ResultSetUtil.convertValue(object, String.class),
is(object.toString()));
assertThat(ResultSetUtil.convertValue("1", int.class), is("1"));
}
-
+
@Test
public void assertConvertLocalDateTime() {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
@@ -100,7 +104,7 @@ public final class ResultSetUtilTest {
public void assertConvertByteArrayValueSuccess() {
byte[] bytesValue = {};
assertThat(ResultSetUtil.convertValue(bytesValue, byte.class),
is(bytesValue));
- assertThat(ResultSetUtil.convertValue(new byte[] {1}, byte.class),
is((byte) 1));
+ assertThat(ResultSetUtil.convertValue(new byte[]{1}, byte.class),
is((byte) 1));
assertThat(ResultSetUtil.convertValue(Shorts.toByteArray((short) 1),
short.class), is((short) 1));
assertThat(ResultSetUtil.convertValue(Ints.toByteArray(1), int.class),
is(1));
assertThat(ResultSetUtil.convertValue(Longs.toByteArray(1L),
long.class), is(1L));
@@ -109,6 +113,44 @@ public final class ResultSetUtilTest {
assertThat(ResultSetUtil.convertValue(Longs.toByteArray(1L),
BigDecimal.class), is(new BigDecimal("1")));
}
+ @SneakyThrows(MalformedURLException.class)
+ @Test
+ public void assertConvertURLValue() {
+ String urlString = "http://apache.org";
+ URL url = (URL) ResultSetUtil.convertValue(urlString, URL.class);
+ assertNotNull(url);
+ assertThat(url, is(new URL(urlString)));
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertConvertURLValueError() {
+ String urlString = "no-exist:apache.org";
+ ResultSetUtil.convertValue(urlString, URL.class);
+ }
+
+ @Test
+ public void assertConvertBigDecimalValue() {
+ BigDecimal bigDecimal = (BigDecimal)
ResultSetUtil.convertBigDecimalValue("12", false, 0);
+ assertThat(bigDecimal, is(BigDecimal.valueOf(12)));
+ }
+
+ @Test
+ public void assertConvertBigDecimalValueNull() {
+ BigDecimal bigDecimal = (BigDecimal)
ResultSetUtil.convertBigDecimalValue(null, false, 0);
+ assertNull(bigDecimal);
+ }
+
+ @Test
+ public void assertConvertBigDecimalValueWithScale() {
+ BigDecimal bigDecimal = (BigDecimal)
ResultSetUtil.convertBigDecimalValue("12.243", true, 2);
+ assertThat(bigDecimal, is(BigDecimal.valueOf(12.24)));
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertConvertBigDecimalValueError() {
+ ResultSetUtil.convertBigDecimalValue(new Date(), true, 2);
+ }
+
@Test(expected = ShardingSphereException.class)
public void assertConvertDateValueError() {
ResultSetUtil.convertValue(new Date(), int.class);
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnSupportedDatabaseMetaDataResultSetTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnSupportedDatabaseMetaDataResultSetTest.java
index 06d9c20..653d216 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnSupportedDatabaseMetaDataResultSetTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnSupportedDatabaseMetaDataResultSetTest.java
@@ -42,26 +42,6 @@ public final class UnSupportedDatabaseMetaDataResultSetTest {
}
@Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetBigDecimalWithIndex() throws SQLException {
- databaseMetaDataResultSet.getBigDecimal(1);
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetBigDecimalWithLabel() throws SQLException {
- databaseMetaDataResultSet.getBigDecimal("");
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetBigDecimalWithIndexAndScale() throws SQLException {
- databaseMetaDataResultSet.getBigDecimal(1, 1);
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetBigDecimalWithLabelAndScale() throws SQLException {
- databaseMetaDataResultSet.getBigDecimal("", 1);
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
public void assertGetAsciiStreamWithIndex() throws SQLException {
databaseMetaDataResultSet.getAsciiStream(1);
}
@@ -167,16 +147,6 @@ public final class
UnSupportedDatabaseMetaDataResultSetTest {
}
@Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetURLWithIndex() throws SQLException {
- databaseMetaDataResultSet.getURL(1);
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
- public void assertGetURLWithLabel() throws SQLException {
- databaseMetaDataResultSet.getURL("");
- }
-
- @Test(expected = SQLFeatureNotSupportedException.class)
public void assertGetSQLXMLWithIndex() throws SQLException {
databaseMetaDataResultSet.getSQLXML(1);
}