From 0bb7138aee0bc76f11bf021a1a4c050e4fe3f235 Mon Sep 17 00:00:00 2001 From: Waleed Fateem <72769898+sfc-gh-wfateem@users.noreply.github.com> Date: Mon, 15 Jan 2024 08:19:32 -0600 Subject: [PATCH] SNOW-1003959: Implement toString() in SnowflakePreparedStatementV1 (#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. --- .../jdbc/SnowflakePreparedStatementV1.java | 4 ++++ .../jdbc/PreparedStatement2LatestIT.java | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/net/snowflake/client/jdbc/SnowflakePreparedStatementV1.java b/src/main/java/net/snowflake/client/jdbc/SnowflakePreparedStatementV1.java index aba023e4e..5be0e53b1 100644 --- a/src/main/java/net/snowflake/client/jdbc/SnowflakePreparedStatementV1.java +++ b/src/main/java/net/snowflake/client/jdbc/SnowflakePreparedStatementV1.java @@ -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(); + } } diff --git a/src/test/java/net/snowflake/client/jdbc/PreparedStatement2LatestIT.java b/src/test/java/net/snowflake/client/jdbc/PreparedStatement2LatestIT.java index ba3d4617a..a034aed0c 100644 --- a/src/test/java/net/snowflake/client/jdbc/PreparedStatement2LatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/PreparedStatement2LatestIT.java @@ -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)+)+$")); + } + } }