Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
1. JDBC: Fix statement close invalidating live result sets of other statements on the same connection - [#39503](https://github.com/apache/shardingsphere/pull/39503)
1. JDBC: Fix select * is not supported for associated query - [#39131](https://github.com/apache/shardingsphere/pull/39131)
1. JDBC: Fix `ResultSet#getCharacterStream` returning the serialized column index instead of the column value for memory query results - [#40431](https://github.com/apache/shardingsphere/pull/40431)
1. JDBC: Fix `ResultSet#getBinaryStream`, `getAsciiStream` and `getUnicodeStream` returning Java-serialized bytes instead of the column value for memory query results - [#40524](https://github.com/apache/shardingsphere/pull/40524)
1. Proxy: Resolve MySQL prepared statement parameter columns for where clause - [#38382](https://github.com/apache/shardingsphere/pull/38382)
1. Proxy: Resolve PostgreSQL prepared statement parameter types before bind - [#38384](https://github.com/apache/shardingsphere/pull/38384)
1. Proxy: Exclude derived projections from PostgreSQL prepared statement row descriptions - [#40194](https://github.com/apache/shardingsphere/issues/40194)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPI;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
Expand Down Expand Up @@ -72,4 +74,22 @@ default Optional<ResultSetTimeValueReader> createTimeValueReader() {
default Object getDefaultValue(final ResultSet resultSet, final int columnIndex, final int columnType) throws SQLException {
return resultSet.getObject(columnIndex);
}

/**
* Judge whether ASCII and Unicode streams of binary values are hexadecimal text.
*
* @return is hexadecimal binary character stream or not
*/
default boolean isHexadecimalBinaryCharacterStream() {
return false;
}

/**
* Get charset of ASCII streams converted from character values.
*
* @return charset of ASCII streams
*/
default Charset getAsciiStreamCharset() {
return StandardCharsets.US_ASCII;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.apache.shardingsphere.database.connector.core.resultset.DialectResultSetMapper;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
Expand Down Expand Up @@ -58,6 +60,16 @@ private boolean isOracleTimestampWithTimeZone(final int columnType) {
return ORACLE_TIMESTAMP_WITH_TIME_ZONE == columnType || ORACLE_TIMESTAMP_WITH_LOCAL_TIME_ZONE == columnType;
}

@Override
public boolean isHexadecimalBinaryCharacterStream() {
return true;
}

@Override
public Charset getAsciiStreamCharset() {
return StandardCharsets.ISO_8859_1;
}

@Override
public String getDatabaseType() {
return "Oracle";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public interface QueryResult extends ExecuteResult, AutoCloseable {
* Get input stream.
*
* @param columnIndex column index
* @param type class type of data value
* @return input stream
* @param type stream type, such as Ascii, Unicode or Binary
* @return input stream, or null if the column value is SQL NULL
* @throws SQLException SQL exception
*/
InputStream getInputStream(int columnIndex, String type) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.memory;

import org.apache.shardingsphere.database.connector.core.resultset.DialectResultSetMapper;
import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.metadata.JDBCQueryResultMetaData;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.AbstractMemoryQueryResult;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
Expand All @@ -37,10 +41,19 @@ public JDBCMemoryQueryResult(final ResultSet resultSet, final DatabaseType datab
}

public JDBCMemoryQueryResult(final ResultSet resultSet, final DatabaseType databaseType, final boolean containsJDBCResultSet) throws SQLException {
super(new JDBCQueryResultMetaData(resultSet.getMetaData()), new QueryResultDataRowLoader(databaseType).load(resultSet.getMetaData().getColumnCount(), resultSet));
super(new JDBCQueryResultMetaData(resultSet.getMetaData()), new QueryResultDataRowLoader(databaseType).load(resultSet.getMetaData().getColumnCount(), resultSet),
isHexadecimalBinaryCharacterStream(databaseType), getAsciiStreamCharset(databaseType));
jdbcResultSet = containsJDBCResultSet ? resultSet : null;
}

private static boolean isHexadecimalBinaryCharacterStream(final DatabaseType databaseType) {
return DatabaseTypedSPILoader.findService(DialectResultSetMapper.class, databaseType).map(DialectResultSetMapper::isHexadecimalBinaryCharacterStream).orElse(false);
}

private static Charset getAsciiStreamCharset(final DatabaseType databaseType) {
return DatabaseTypedSPILoader.findService(DialectResultSetMapper.class, databaseType).map(DialectResultSetMapper::getAsciiStreamCharset).orElse(StandardCharsets.US_ASCII);
}

@Override
public Optional<ResultSet> getJDBCResultSet() {
return Optional.ofNullable(jdbcResultSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@

package org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory;

import com.google.common.io.BaseEncoding;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Clob;
Expand All @@ -56,12 +54,23 @@ public abstract class AbstractMemoryQueryResult implements QueryResult {
@Getter
private long rowCount;

private final boolean hexadecimalBinaryCharacterStream;

private final Charset asciiStreamCharset;

private boolean wasNull;

protected AbstractMemoryQueryResult(final QueryResultMetaData metaData, final Collection<MemoryQueryResultDataRow> rows) {
this(metaData, rows, false, StandardCharsets.US_ASCII);
}

protected AbstractMemoryQueryResult(final QueryResultMetaData metaData, final Collection<MemoryQueryResultDataRow> rows, final boolean hexadecimalBinaryCharacterStream,
final Charset asciiStreamCharset) {
this.metaData = metaData;
this.rows = rows.iterator();
rowCount = rows.size();
this.hexadecimalBinaryCharacterStream = hexadecimalBinaryCharacterStream;
this.asciiStreamCharset = asciiStreamCharset;
}

@Override
Expand Down Expand Up @@ -90,20 +99,43 @@ public final Object getCalendarValue(final int columnIndex, final Class<?> type,
}

@Override
public final InputStream getInputStream(final int columnIndex, final String type) {
public final InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
Object value = currentRow.getValue().get(columnIndex - 1);
wasNull = null == value;
return getInputStream(value);
if (null == value) {
return null;
}
if (value instanceof byte[]) {
return hexadecimalBinaryCharacterStream && !"Binary".equals(type) ? getHexadecimalInputStream((byte[]) value, type) : new ByteArrayInputStream((byte[]) value);
}
if (value instanceof Blob) {
return ((Blob) value).getBinaryStream();
}
if ("Unicode".equals(type)) {
return new ByteArrayInputStream(getString(value).getBytes(StandardCharsets.UTF_16BE));
}
if (value instanceof Clob) {
return ((Clob) value).getAsciiStream();
}
if (value instanceof SQLXML) {
return "Ascii".equals(type) ? new ByteArrayInputStream(((SQLXML) value).getString().getBytes(asciiStreamCharset)) : ((SQLXML) value).getBinaryStream();
}
// TODO Support connection property character encoding
return new ByteArrayInputStream(value.toString().getBytes("Ascii".equals(type) ? asciiStreamCharset : StandardCharsets.UTF_8));
}

@SneakyThrows(IOException.class)
private InputStream getInputStream(final Object value) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(value);
objectOutputStream.flush();
objectOutputStream.close();
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
private InputStream getHexadecimalInputStream(final byte[] value, final String type) {
return new ByteArrayInputStream(BaseEncoding.base16().encode(value).getBytes("Unicode".equals(type) ? StandardCharsets.UTF_16BE : StandardCharsets.US_ASCII));
}

private String getString(final Object value) throws SQLException {
if (value instanceof Clob) {
return ((Clob) value).getSubString(1L, (int) ((Clob) value).length());
}
if (value instanceof SQLXML) {
return ((SQLXML) value).getString();
}
return value.toString();
}

@Override
Expand Down
Loading
Loading