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

refactor: merge PgPreparedStatement#setBinaryStream int and long methods #3165

Merged
merged 3 commits into from
May 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 13 additions & 18 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,24 +504,12 @@ public void setUnicodeStream(@Positive int parameterIndex, @Nullable InputStream
@Override
public void setBinaryStream(@Positive int parameterIndex, @Nullable InputStream x,
@NonNegative int length) throws SQLException {
checkClosed();

if (x == null) {
setNull(parameterIndex, Types.VARBINARY);
return;
}

if (length < 0) {
throw new PSQLException(GT.tr("Invalid stream length {0}.", length),
PSQLState.INVALID_PARAMETER_VALUE);
}

// Version 7.2 supports BinaryStream for the PG bytea type
// As the spec/javadoc for this method indicate this is to be used for
// large binary values (i.e. LONGVARBINARY) PG doesn't have a separate
// long binary datatype, but with toast the bytea datatype is capable of
// handling very large values.
preparedParameters.setBytea(parameterIndex, x, length);
setBinaryStream(parameterIndex, x, (long) length);
}

@Override
Expand Down Expand Up @@ -1589,16 +1577,23 @@ public void setCharacterStream(@Positive int parameterIndex,
public void setBinaryStream(@Positive int parameterIndex, @Nullable InputStream value,
@NonNegative @IntRange(from = 0, to = Integer.MAX_VALUE) long length)
throws SQLException {
checkClosed();

if (value == null) {
setNull(parameterIndex, Oid.BYTEA);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was setNull(parameterIndex, Types.VARBINARY); in the first method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but in method setNull(int parameterIndex, int sqlType), “Types.VARBINARY” and “Oid.BYTEA” are equivalent.

case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
   oid = Oid.BYTEA;
   break;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll withdraw my criticism

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glad to help with the improvement of PG

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a constant from java.sql.Types, not an Oid.
So Oid.BYTEA is not valid here, and it causes tests to fail.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the reminder. I made a mistake.
I confused PrepareStatement.setNull and preparedParameters.setNull, PrepareStatement.setNull should never use oid as a parameter.

return;
}

//noinspection ConstantConditions
if (length > Integer.MAX_VALUE) {
throw new PSQLException(GT.tr("Object is too large to send over the protocol."),
PSQLState.NUMERIC_CONSTANT_OUT_OF_RANGE);
} else if (length < 0) {
throw new PSQLException(GT.tr("Invalid stream length {0}.", length),
PSQLState.INVALID_PARAMETER_VALUE);
}
if (value == null) {
preparedParameters.setNull(parameterIndex, Oid.BYTEA);
} else {
preparedParameters.setBytea(parameterIndex, value, (int) length);
}

preparedParameters.setBytea(parameterIndex, value, (int) length);
}

@Override
Expand Down