Skip to content

Commit 3e15e97

Browse files
mbladelsebersole
authored andcommitted
0d67d64 rewrite hana blob extractors
1 parent 055d84c commit 3e15e97

File tree

2 files changed

+156
-212
lines changed

2 files changed

+156
-212
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HANALegacyDialect.java

+78-106
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
import org.hibernate.type.descriptor.ValueBinder;
112112
import org.hibernate.type.descriptor.ValueExtractor;
113113
import org.hibernate.type.descriptor.WrapperOptions;
114-
import org.hibernate.type.descriptor.java.DataHelper;
115114
import org.hibernate.type.descriptor.java.DoubleJavaType;
116115
import org.hibernate.type.descriptor.java.JavaType;
117116
import org.hibernate.type.descriptor.jdbc.BasicBinder;
@@ -163,6 +162,8 @@
163162
import static org.hibernate.type.descriptor.DateTimeUtils.appendAsDate;
164163
import static org.hibernate.type.descriptor.DateTimeUtils.appendAsTime;
165164
import static org.hibernate.type.descriptor.DateTimeUtils.appendAsTimestampWithMicros;
165+
import static org.hibernate.type.descriptor.java.DataHelper.extractBytes;
166+
import static org.hibernate.type.descriptor.java.DataHelper.extractString;
166167

167168
/**
168169
* An SQL dialect for legacy versions of the SAP HANA Platform up tu and including 2.0 SPS 04.
@@ -1497,7 +1498,7 @@ public OutputStream setAsciiStream(long pos) throws SQLException {
14971498

14981499
@Override
14991500
public long position(Clob searchstr, long start) throws SQLException {
1500-
return this.data.indexOf( DataHelper.extractString( searchstr ), (int) ( start - 1 ) );
1501+
return this.data.indexOf( extractString( searchstr ), (int) ( start - 1 ) );
15011502
}
15021503

15031504
@Override
@@ -1536,6 +1537,47 @@ public void free() throws SQLException {
15361537
}
15371538
}
15381539

1540+
private static class BlobExtractor<X> extends BasicExtractor<X> {
1541+
private final int maxLobPrefetchSize;
1542+
1543+
public BlobExtractor(JavaType<X> javaType, JdbcType jdbcType, int maxLobPrefetchSize) {
1544+
super( javaType, jdbcType );
1545+
this.maxLobPrefetchSize = maxLobPrefetchSize;
1546+
}
1547+
1548+
private X doExtract(Blob blob, WrapperOptions options) throws SQLException {
1549+
final X result;
1550+
if ( blob == null ) {
1551+
result = getJavaType().wrap( null, options );
1552+
}
1553+
else if ( blob.length() < maxLobPrefetchSize ) {
1554+
result = getJavaType().wrap( blob, options );
1555+
blob.free();
1556+
}
1557+
else {
1558+
final MaterializedBlob materialized = new MaterializedBlob( extractBytes( blob.getBinaryStream() ) );
1559+
blob.free();
1560+
result = getJavaType().wrap( materialized, options );
1561+
}
1562+
return result;
1563+
}
1564+
1565+
@Override
1566+
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
1567+
return doExtract( rs.getBlob( paramIndex ), options );
1568+
}
1569+
1570+
@Override
1571+
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
1572+
return doExtract( statement.getBlob( index ), options );
1573+
}
1574+
1575+
@Override
1576+
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
1577+
return doExtract( statement.getBlob( name ), options );
1578+
}
1579+
}
1580+
15391581
private static class HANAStreamBlobType implements JdbcType {
15401582

15411583
private static final long serialVersionUID = -2476600722093442047L;
@@ -1601,38 +1643,8 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
16011643

16021644
@Override
16031645
public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
1604-
return new BasicExtractor<>( javaType, this ) {
1605-
private X extract(Blob blob, WrapperOptions options) throws SQLException {
1606-
if ( blob == null ) {
1607-
return null;
1608-
}
1609-
if ( blob.length() < HANALegacyDialect.HANAStreamBlobType.this.maxLobPrefetchSize ) {
1610-
X result = javaType.wrap( blob, options );
1611-
blob.free();
1612-
return result;
1613-
}
1614-
Blob materializedBlob = new MaterializedBlob( DataHelper.extractBytes( blob.getBinaryStream() ) );
1615-
blob.free();
1616-
return javaType.wrap( materializedBlob, options );
1617-
}
1618-
1619-
@Override
1620-
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
1621-
return extract( rs.getBlob( paramIndex ), options );
1622-
}
1623-
1624-
@Override
1625-
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
1626-
return extract( statement.getBlob( index ), options );
1627-
}
1628-
1629-
@Override
1630-
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
1631-
return extract( statement.getBlob( name ), options );
1632-
}
1633-
};
1646+
return new BlobExtractor<>( javaType, this, maxLobPrefetchSize );
16341647
}
1635-
16361648
}
16371649

16381650
// the ClobTypeDescriptor and NClobTypeDescriptor for HANA are slightly
@@ -1704,55 +1716,39 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
17041716
@Override
17051717
public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
17061718
return new BasicExtractor<>( javaType, this ) {
1707-
private X extract(Clob clob, WrapperOptions options) throws SQLException {
1719+
private X doExtract(Clob clob, WrapperOptions options) throws SQLException {
1720+
final X result;
17081721
if ( clob == null ) {
1709-
return null;
1722+
result = getJavaType().wrap( null, options );
17101723
}
1711-
1712-
if ( clob.length() < HANALegacyDialect.HANAClobJdbcType.this.maxLobPrefetchSize ) {
1713-
X retVal = javaType.wrap(clob, options);
1724+
else if ( clob.length() < maxLobPrefetchSize ) {
1725+
result = getJavaType().wrap(clob, options);
17141726
clob.free();
1715-
return retVal;
17161727
}
1717-
NClob materializedNClob = new MaterializedNClob( DataHelper.extractString( clob ) );
1718-
clob.free();
1719-
return javaType.wrap( materializedNClob, options );
1728+
else {
1729+
final MaterializedNClob materialized = new MaterializedNClob( extractString( clob ) );
1730+
clob.free();
1731+
result = getJavaType().wrap( materialized, options );
1732+
}
1733+
return result;
17201734
}
17211735

17221736
@Override
17231737
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
1724-
Clob rsClob;
1725-
if ( HANALegacyDialect.HANAClobJdbcType.this.useUnicodeStringTypes ) {
1726-
rsClob = rs.getNClob( paramIndex );
1727-
}
1728-
else {
1729-
rsClob = rs.getClob( paramIndex );
1730-
}
1731-
return extract( rsClob, options );
1738+
final Clob clob = useUnicodeStringTypes ? rs.getNClob( paramIndex ) : rs.getClob( paramIndex );
1739+
return doExtract( clob, options );
17321740
}
17331741

17341742
@Override
17351743
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
1736-
Clob rsClob;
1737-
if ( HANALegacyDialect.HANAClobJdbcType.this.useUnicodeStringTypes ) {
1738-
rsClob = statement.getNClob( index );
1739-
}
1740-
else {
1741-
rsClob = statement.getClob( index );
1742-
}
1743-
return extract( rsClob, options );
1744+
final Clob clob = useUnicodeStringTypes ? statement.getNClob( index ) : statement.getClob( index );
1745+
return doExtract( clob, options );
17441746
}
17451747

17461748
@Override
17471749
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
1748-
Clob rsClob;
1749-
if ( HANALegacyDialect.HANAClobJdbcType.this.useUnicodeStringTypes ) {
1750-
rsClob = statement.getNClob( name );
1751-
}
1752-
else {
1753-
rsClob = statement.getClob( name );
1754-
}
1755-
return extract( rsClob, options );
1750+
final Clob clob = useUnicodeStringTypes ? statement.getNClob( name ) : statement.getClob( name );
1751+
return doExtract( clob, options );
17561752
}
17571753
};
17581754
}
@@ -1826,32 +1822,36 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
18261822
@Override
18271823
public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
18281824
return new BasicExtractor<>( javaType, this ) {
1829-
private X extract(NClob nclob, WrapperOptions options) throws SQLException {
1825+
private X doExtract(NClob nclob, WrapperOptions options) throws SQLException {
1826+
final X result;
18301827
if ( nclob == null ) {
1831-
return null;
1828+
result = getJavaType().wrap( null, options );
1829+
}
1830+
else if ( nclob.length() < maxLobPrefetchSize ) {
1831+
result = javaType.wrap(nclob, options);
1832+
nclob.free();
18321833
}
1833-
if ( nclob.length() < maxLobPrefetchSize ) {
1834-
X retVal = javaType.wrap(nclob, options);
1834+
else {
1835+
final MaterializedNClob materialized = new MaterializedNClob( extractString( nclob ) );
18351836
nclob.free();
1836-
return retVal;
1837+
result = getJavaType().wrap( materialized, options );
18371838
}
1838-
NClob materializedNClob = new MaterializedNClob( DataHelper.extractString( nclob ) );
1839-
nclob.free();
1840-
return javaType.wrap( materializedNClob, options );
1839+
return result;
18411840
}
1841+
18421842
@Override
18431843
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
1844-
return extract( rs.getNClob( paramIndex ), options );
1844+
return doExtract( rs.getNClob( paramIndex ), options );
18451845
}
18461846

18471847
@Override
18481848
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
1849-
return extract( statement.getNClob( index ), options );
1849+
return doExtract( statement.getNClob( index ), options );
18501850
}
18511851

18521852
@Override
18531853
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
1854-
return extract( statement.getNClob( name ), options );
1854+
return doExtract( statement.getNClob( name ), options );
18551855
}
18561856
};
18571857
}
@@ -1892,35 +1892,7 @@ public String toString() {
18921892

18931893
@Override
18941894
public <X> ValueExtractor<X> getExtractor(final JavaType<X> javaType) {
1895-
return new BasicExtractor<>( javaType, this ) {
1896-
private X extract(Blob blob, WrapperOptions options) throws SQLException {
1897-
if ( blob == null ) {
1898-
return null;
1899-
}
1900-
if ( blob.length() < maxLobPrefetchSize ) {
1901-
X retVal = javaType.wrap(blob, options);
1902-
blob.free();
1903-
return retVal;
1904-
}
1905-
Blob materializedBlob = new MaterializedBlob( DataHelper.extractBytes( blob.getBinaryStream() ) );
1906-
blob.free();
1907-
return javaType.wrap( materializedBlob, options );
1908-
}
1909-
@Override
1910-
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
1911-
return extract( rs.getBlob( paramIndex ) , options );
1912-
}
1913-
1914-
@Override
1915-
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
1916-
return extract( statement.getBlob( index ), options );
1917-
}
1918-
1919-
@Override
1920-
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
1921-
return extract( statement.getBlob( name ), options );
1922-
}
1923-
};
1895+
return new BlobExtractor<>( javaType, this, maxLobPrefetchSize );
19241896
}
19251897

19261898
@Override

0 commit comments

Comments
 (0)