Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3954 : Sharding-JDBC querying support PostgreSQL array type #6524

Merged
merged 4 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ private Object getRowValue(final ResultSet resultSet, final int columnIndex) thr
case Types.VARBINARY:
case Types.LONGVARBINARY:
return resultSet.getBlob(columnIndex);
case Types.ARRAY:
return resultSet.getArray(columnIndex);
default:
return resultSet.getObject(columnIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.queryresult;

import java.sql.Array;
import org.apache.shardingsphere.infra.executor.sql.QueryResult;

import java.io.InputStream;
Expand Down Expand Up @@ -87,6 +88,8 @@ public Object getValue(final int columnIndex, final Class<?> type) throws SQLExc
return resultSet.getBlob(columnIndex);
} else if (Clob.class == type) {
return resultSet.getClob(columnIndex);
} else if (Array.class == type) {
return resultSet.getArray(columnIndex);
} else {
return resultSet.getObject(columnIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.executor.sql.jdbc.queryresult;

import java.sql.Array;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.queryresult.MemoryQueryResult;
import org.hamcrest.core.Is;
Expand Down Expand Up @@ -306,6 +307,17 @@ public void assertGetValueByLongVarBinary() throws SQLException {
assertFalse(actual.next());
}

@Test
public void assertGetValueByArray() throws SQLException {
ResultSet resultSet = getMockedResultSet(Types.ARRAY);
Array value = mock(Array.class);
when(resultSet.getArray(1)).thenReturn(value);
MemoryQueryResult actual = new MemoryQueryResult(resultSet);
assertTrue(actual.next());
assertThat(actual.getValue(1, Array.class), is(value));
assertFalse(actual.next());
}

private ResultSet getMockedResultSet(final int columnTypes) throws SQLException {
ResultSet resultSet = mock(ResultSet.class);
when(resultSet.next()).thenReturn(true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.executor.sql.jdbc.queryresult;

import java.sql.Array;
import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.queryresult.StreamQueryResult;
import org.junit.Test;

Expand Down Expand Up @@ -150,6 +151,14 @@ public void assertGetValueByClob() throws SQLException {
assertThat(new StreamQueryResult(resultSet).getValue(1, Clob.class), is(value));
}

@Test
public void assertGetValueByArray() throws SQLException {
ResultSet resultSet = mock(ResultSet.class);
Array value = mock(Array.class);
when(resultSet.getArray(1)).thenReturn(value);
assertThat(new StreamQueryResult(resultSet).getValue(1, Array.class), is(value));
}

@Test
public void assertGetValueByTimestamp() throws SQLException {
ResultSet resultSet = mock(ResultSet.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import java.sql.Array;
import org.apache.shardingsphere.driver.jdbc.adapter.AbstractResultSetAdapter;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
Expand Down Expand Up @@ -317,12 +318,22 @@ public Blob getBlob(final String columnLabel) throws SQLException {
public Clob getClob(final int columnIndex) throws SQLException {
return (Clob) mergeResultSet.getValue(columnIndex, Clob.class);
}

@Override
public Clob getClob(final String columnLabel) throws SQLException {
return getClob(getIndexFromColumnLabelAndIndexMap(columnLabel));
}

@Override
public Array getArray(final int columnIndex) throws SQLException {
return (Array) mergeResultSet.getValue(columnIndex, Array.class);
}

@Override
public Array getArray(final String columnLabel) throws SQLException {
return getArray(getIndexFromColumnLabelAndIndexMap(columnLabel));
}

@Override
public URL getURL(final int columnIndex) throws SQLException {
return (URL) mergeResultSet.getValue(columnIndex, URL.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
Expand Down Expand Up @@ -113,6 +114,16 @@ public final Statement getStatement() throws SQLException {
throw new SQLFeatureNotSupportedException("getStatement");
}

@Override
public final Array getArray(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getArray");
}

@Override
public final Array getArray(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getArray");
}

@Override
public final Blob getBlob(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getBlob");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
Expand Down Expand Up @@ -106,6 +107,16 @@ public final Timestamp getTimestamp(final String columnLabel, final Calendar cal
throw new SQLFeatureNotSupportedException("getTimestamp");
}

@Override
public final Array getArray(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getArray");
}

@Override
public final Array getArray(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getArray");
}

@Override
public final InputStream getAsciiStream(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getAsciiStream");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.shardingsphere.driver.jdbc.unsupported;

import java.io.Reader;
import java.sql.Array;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.RowId;
Expand Down Expand Up @@ -181,16 +180,6 @@ public final Ref getRef(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getRef");
}

@Override
tuohai666 marked this conversation as resolved.
Show resolved Hide resolved
public final Array getArray(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getArray");
}

@Override
public final Array getArray(final String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("getArray");
}

@Override
public final RowId getRowId(final int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("getRowId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import java.sql.Array;
import java.time.LocalDateTime;
import java.time.ZoneId;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
Expand Down Expand Up @@ -418,6 +419,20 @@ public void assertGetClobWithColumnLabel() throws SQLException {
assertThat(shardingSphereResultSet.getClob("label"), is(clob));
}

@Test
public void assertGetArrayWithColumnIndex() throws SQLException {
Array array = mock(Array.class);
when(mergeResultSet.getValue(1, Array.class)).thenReturn(array);
assertThat(shardingSphereResultSet.getArray(1), is(array));
}

@Test
public void assertGetArrayWithColumnLabel() throws SQLException {
Array array = mock(Array.class);
when(mergeResultSet.getValue(1, Array.class)).thenReturn(array);
assertThat(shardingSphereResultSet.getArray("label"), is(array));
}

@Test
public void assertGetURLWithColumnIndex() throws SQLException, MalformedURLException {
when(mergeResultSet.getValue(1, URL.class)).thenReturn(new URL("http://xxx.xxx"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.driver.orchestration.internal.circuit.resultset;

import java.sql.Array;
import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationResultSet;

import java.io.InputStream;
Expand Down Expand Up @@ -338,6 +339,16 @@ public Statement getStatement() {
return null;
}

@Override
public Array getArray(final int columnIndex) {
return null;
}

@Override
public Array getArray(final String columnLabel) {
return null;
}

@Override
public Blob getBlob(final int columnIndex) {
return null;
Expand Down