You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "exceptionfactory (via GitHub)" <gi...@apache.org> on 2023/06/14 20:03:33 UTC

[GitHub] [nifi] exceptionfactory commented on a diff in pull request #7380: NIFI-11691: Support VARBINARY and LONGVARBINARY types in PutDatabaseRecord

exceptionfactory commented on code in PR #7380:
URL: https://github.com/apache/nifi/pull/7380#discussion_r1230099971


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutDatabaseRecord.java:
##########
@@ -868,6 +868,33 @@ private void setParameter(PreparedStatement ps, int index, Object value, int fie
                     throw new IOException("Unable to parse data as CLOB/String " + value, e.getCause());
                 }
             }
+        } else if (sqlType == Types.VARBINARY || sqlType == Types.LONGVARBINARY) {
+            if (fieldSqlType == Types.ARRAY || fieldSqlType == Types.VARCHAR) {
+                if (!(value instanceof byte[])) {
+                    if (value == null) {
+                        try {
+                            ps.setNull(index, Types.BLOB);
+                            return;
+                        } catch (SQLException e) {
+                            throw new IOException("Unable to setNull() on prepared statement" , e);
+                        }
+                    } else {
+                        throw new IOException("Expected VARBINARY/LONGVARBINARY to be of type byte[] but is instead " + value.getClass().getName());
+                    }
+                }
+                byte[] byteArray = (byte[]) value;
+                try {
+                    ps.setBytes(index, byteArray);
+                } catch (SQLException e) {
+                    throw new IOException("Unable to parse binary data " + value, e.getCause());
+                }
+            } else {
+                try {
+                    ps.setBytes(index, value.toString().getBytes(StandardCharsets.UTF_8));
+                } catch (SQLException e) {
+                    throw new IOException("Unable to parse binary data " + value, e.getCause());

Review Comment:
   As noted above, recommend replacing `value` with the length of the bytes.



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutDatabaseRecord.java:
##########
@@ -868,6 +868,33 @@ private void setParameter(PreparedStatement ps, int index, Object value, int fie
                     throw new IOException("Unable to parse data as CLOB/String " + value, e.getCause());
                 }
             }
+        } else if (sqlType == Types.VARBINARY || sqlType == Types.LONGVARBINARY) {
+            if (fieldSqlType == Types.ARRAY || fieldSqlType == Types.VARCHAR) {
+                if (!(value instanceof byte[])) {
+                    if (value == null) {
+                        try {
+                            ps.setNull(index, Types.BLOB);
+                            return;
+                        } catch (SQLException e) {
+                            throw new IOException("Unable to setNull() on prepared statement" , e);
+                        }
+                    } else {
+                        throw new IOException("Expected VARBINARY/LONGVARBINARY to be of type byte[] but is instead " + value.getClass().getName());
+                    }
+                }
+                byte[] byteArray = (byte[]) value;
+                try {
+                    ps.setBytes(index, byteArray);
+                } catch (SQLException e) {
+                    throw new IOException("Unable to parse binary data " + value, e.getCause());

Review Comment:
   Given the potential size of these objects, recommend replacing `value` the array length to avoid extremely large log messages.



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutDatabaseRecord.java:
##########
@@ -868,6 +868,33 @@ private void setParameter(PreparedStatement ps, int index, Object value, int fie
                     throw new IOException("Unable to parse data as CLOB/String " + value, e.getCause());
                 }
             }
+        } else if (sqlType == Types.VARBINARY || sqlType == Types.LONGVARBINARY) {
+            if (fieldSqlType == Types.ARRAY || fieldSqlType == Types.VARCHAR) {
+                if (!(value instanceof byte[])) {
+                    if (value == null) {
+                        try {
+                            ps.setNull(index, Types.BLOB);
+                            return;
+                        } catch (SQLException e) {
+                            throw new IOException("Unable to setNull() on prepared statement" , e);
+                        }
+                    } else {
+                        throw new IOException("Expected VARBINARY/LONGVARBINARY to be of type byte[] but is instead " + value.getClass().getName());
+                    }
+                }
+                byte[] byteArray = (byte[]) value;
+                try {
+                    ps.setBytes(index, byteArray);
+                } catch (SQLException e) {
+                    throw new IOException("Unable to parse binary data " + value, e.getCause());
+                }
+            } else {
+                try {
+                    ps.setBytes(index, value.toString().getBytes(StandardCharsets.UTF_8));

Review Comment:
   It might be worth adding a comment on this line, since `value.toString()` may not provide an expected representation of the underlying object type. Perhaps there should be some additional type checking in this conditional to determine if `value` is an array type or collection type, otherwise it seems like this could produce unexpected results.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org