Skip to content

Commit

Permalink
SNOW-1003959: Implement toString() in SnowflakePreparedStatementV1 (#…
Browse files Browse the repository at this point in the history
…1604)

Invoking the PreparedStatement's toString() method will now return the actual SQL query and
query ID if available rahter than the object's hashCode. This is helpful when using features
in frameworks like Spring Boot to monitor and log information about slow running queries.
  • Loading branch information
sfc-gh-wfateem authored Jan 15, 2024
1 parent cd42c68 commit 0bb7138
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -966,4 +966,8 @@ public void resultSetMetadataHandler(SFBaseResultSet resultSet) throws SQLExcept
alreadyDescribed = true;
}
}

public String toString() {
return (this.sql != null) ? this.sql + " - Query ID: " + this.getQueryID() : super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,24 @@ public void testConsecutiveBatchInsertError() throws SQLException {
assertTrue(prepStatement.unwrap(SnowflakePreparedStatementV1.class).isArrayBindSupported());
}
}

@Test
public void testToString() throws SQLException {
try (Connection connection = init()) {
PreparedStatement prepStatement =
connection.prepareStatement("select current_version() --testing toString()");

// Query ID is going to be null since we didn't execute the statement yet
assertEquals(
"select current_version() --testing toString() - Query ID: null",
prepStatement.toString());

prepStatement.executeQuery();
assertTrue(
prepStatement
.toString()
.matches(
"select current_version\\(\\) --testing toString\\(\\) - Query ID: (\\d|\\w)+(-(\\d|\\w)+)+$"));
}
}
}

0 comments on commit 0bb7138

Please sign in to comment.