Skip to content

Commit

Permalink
Fixed timestamp string conversion error for cstmt (#2449)
Browse files Browse the repository at this point in the history
* Fixed timestamp string conversion error for cstmt

* Code review comments p1

* Fixed sproc used in test
  • Loading branch information
tkyc authored Jun 18, 2024
1 parent e8f09c2 commit 9754078
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -1643,6 +1644,8 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
op.execute(this, ((Geometry) value).serialize());
} else if (JDBCType.GEOGRAPHY == jdbcType) {
op.execute(this, ((Geography) value).serialize());
} else if (JDBCType.TIMESTAMP == jdbcType) {
op.execute(this, Timestamp.valueOf((String) value));
} else {
if (null != cryptoMeta) {
// if streaming types check for allowed data length in AE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class CallableStatementTest extends AbstractTest {
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Table"));
private static String manyParamProc = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Procedure"));
private static String currentTimeProc = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("currentTime_Procedure"));
private static String manyParamUserDefinedType = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_definedType"));
private static String zeroParamSproc = AbstractSQLGenerator
Expand Down Expand Up @@ -108,6 +110,7 @@ public static void setupTest() throws Exception {
createUserDefinedType();
createTableManyParams();
createProcedureManyParams();
createProcedureCurrentTime();
createGetObjectOffsetDateTimeProcedure(stmt);
createProcedureZeroParams();
createOutOfOrderSproc();
Expand Down Expand Up @@ -1224,6 +1227,17 @@ public void testFourPartSyntaxCallEscapeSyntax() throws SQLException {
}
}

@Test
public void testTimestampStringConversion() throws SQLException {
try (CallableStatement stmt = connection.prepareCall("{call " + currentTimeProc + "(?)}")) {
String timestamp = "2024-05-29 15:35:53.461";
stmt.setObject(1, timestamp, Types.TIMESTAMP);
stmt.registerOutParameter(1, Types.TIMESTAMP);
stmt.execute();
stmt.getObject("currentTimeStamp");
}
}

/**
* Cleanup after test
*
Expand All @@ -1242,6 +1256,7 @@ public static void cleanup() throws SQLException {
TestUtils.dropProcedureIfExists(zeroParamSproc, stmt);
TestUtils.dropProcedureIfExists(outOfOrderSproc, stmt);
TestUtils.dropProcedureIfExists(byParamNameSproc, stmt);
TestUtils.dropProcedureIfExists(currentTimeProc, stmt);
TestUtils.dropFunctionIfExists(userDefinedFunction, stmt);
}
}
Expand Down Expand Up @@ -1294,6 +1309,14 @@ private static void createProcedureManyParams() throws SQLException {
}
}

private static void createProcedureCurrentTime() throws SQLException {
String sql = "CREATE PROCEDURE " + currentTimeProc + " @currentTimeStamp datetime = null OUTPUT " +
"AS BEGIN SET @currentTimeStamp = CURRENT_TIMESTAMP; END";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
}
}

private static void createTableManyParams() throws SQLException {
String type = manyParamUserDefinedType;
String sql = "CREATE TABLE" + manyParamsTable + " (c1 " + type + " null, " + "c2 " + type + " null, " + "c3 "
Expand Down

0 comments on commit 9754078

Please sign in to comment.