This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch rc/1.3.1
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 040ab6505de684a4fd1b36f710e0740dfe4c3715
Author: CritasWang <[email protected]>
AuthorDate: Fri Apr 19 15:38:44 2024 +0800

    fix(jdbc): when parameter is string ,append "\"" (#12371)
---
 .../apache/iotdb/jdbc/IoTDBPreparedStatement.java  |  7 ++++++-
 .../iotdb/jdbc/IoTDBPreparedStatementTest.java     | 23 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git 
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBPreparedStatement.java
 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBPreparedStatement.java
index c93c1d55027..6b7bd8e40ac 100644
--- 
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBPreparedStatement.java
+++ 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBPreparedStatement.java
@@ -880,7 +880,12 @@ public class IoTDBPreparedStatement extends IoTDBStatement 
implements PreparedSt
 
   @Override
   public void setString(int parameterIndex, String x) {
-    this.parameters.put(parameterIndex, x);
+    // if the sql is insert and the value is not a string literal, add double 
quotes
+    if (sql.trim().toUpperCase().startsWith("INSERT") && !x.startsWith("\"") 
&& !x.endsWith("'")) {
+      this.parameters.put(parameterIndex, "\"" + x + "\"");
+    } else {
+      this.parameters.put(parameterIndex, x);
+    }
   }
 
   @Override
diff --git 
a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBPreparedStatementTest.java
 
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBPreparedStatementTest.java
index f236ea12043..b7d8203cf93 100644
--- 
a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBPreparedStatementTest.java
+++ 
b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBPreparedStatementTest.java
@@ -375,4 +375,27 @@ public class IoTDBPreparedStatementTest {
         "INSERT INTO root.ln.wf01.wt02(time,a,b,c,d,e,f) 
VALUES(2020-01-01T10:10:10,false,123,123234345,123.423,-1323.0,\"abc\")",
         argument.getValue().getStatement());
   }
+
+  @Test
+  public void testInsertStatement4() throws Exception {
+    String sql = "INSERT INTO root.ln.wf01.wt02(time,a,b,c,d,e,f) 
VALUES(?,?,?,?,?,?,?)";
+
+    IoTDBPreparedStatement ps =
+        new IoTDBPreparedStatement(connection, client, sessionId, sql, zoneId);
+    ps.setObject(1, "2020-01-01 10:10:10", Types.TIMESTAMP, -1);
+    ps.setObject(2, false, Types.BOOLEAN, -1);
+    ps.setObject(3, 123, Types.INTEGER, -1);
+    ps.setObject(4, 123234345, Types.BIGINT);
+    ps.setObject(5, 123.423f, Types.FLOAT);
+    ps.setObject(6, -1323.0, Types.DOUBLE);
+    ps.setObject(7, "abc", Types.VARCHAR);
+    ps.execute();
+
+    ArgumentCaptor<TSExecuteStatementReq> argument =
+        ArgumentCaptor.forClass(TSExecuteStatementReq.class);
+    verify(client).executeStatementV2(argument.capture());
+    assertEquals(
+        "INSERT INTO root.ln.wf01.wt02(time,a,b,c,d,e,f) 
VALUES(2020-01-01T10:10:10,false,123,123234345,123.423,-1323.0,\"abc\")",
+        argument.getValue().getStatement());
+  }
 }

Reply via email to