NIFI-4352: Add CLOB and NCLOB support to PutSQL and ExecuteSQL

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #2145.
This commit is contained in:
Koji Kawamura 2017-09-12 11:37:12 +09:00 committed by Pierre Villard
parent e52e9acc59
commit 1e70e24267
2 changed files with 32 additions and 0 deletions

View File

@ -54,6 +54,7 @@ import javax.xml.bind.DatatypeConverter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
@ -929,6 +930,16 @@ public class PutSQL extends AbstractSessionFactoryProcessor {
case Types.LONGVARCHAR:
stmt.setString(parameterIndex, parameterValue);
break;
case Types.CLOB:
try (final StringReader reader = new StringReader(parameterValue)) {
stmt.setCharacterStream(parameterIndex, reader);
}
break;
case Types.NCLOB:
try (final StringReader reader = new StringReader(parameterValue)) {
stmt.setNCharacterStream(parameterIndex, reader);
}
break;
default:
stmt.setObject(parameterIndex, parameterValue, jdbcType);
break;

View File

@ -33,6 +33,7 @@ import static java.sql.Types.LONGNVARCHAR;
import static java.sql.Types.LONGVARBINARY;
import static java.sql.Types.LONGVARCHAR;
import static java.sql.Types.NCHAR;
import static java.sql.Types.NCLOB;
import static java.sql.Types.NUMERIC;
import static java.sql.Types.NVARCHAR;
import static java.sql.Types.REAL;
@ -47,11 +48,14 @@ import static java.sql.Types.VARCHAR;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@ -278,6 +282,22 @@ public class JdbcCommon {
continue;
}
if (javaSqlType == NCLOB) {
NClob nClob = rs.getNClob(i);
if (nClob != null) {
final Reader characterStream = nClob.getCharacterStream();
long numChars = (int) nClob.length();
final CharBuffer buffer = CharBuffer.allocate((int) numChars);
characterStream.read(buffer);
buffer.flip();
rec.put(i - 1, buffer.toString());
nClob.free();
} else {
rec.put(i - 1, null);
}
continue;
}
if (javaSqlType == BLOB) {
Blob blob = rs.getBlob(i);
if (blob != null) {
@ -454,6 +474,7 @@ public class JdbcCommon {
case NVARCHAR:
case VARCHAR:
case CLOB:
case NCLOB:
builder.name(columnName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
break;