You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2024/11/08 03:38:40 UTC

(iotdb) branch master updated: Adjustment of setString method in IoTDBPreparedStatement

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

jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new cae7061e869 Adjustment of setString method in IoTDBPreparedStatement
cae7061e869 is described below

commit cae7061e869d6e802dd336afc9fa132b89ca7d14
Author: CritasWang <cr...@outlook.com>
AuthorDate: Fri Nov 8 11:38:34 2024 +0800

    Adjustment of setString method in IoTDBPreparedStatement
---
 .../src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java   | 10 ++++++++++
 .../java/org/apache/iotdb/jdbc/IoTDBPreparedStatement.java     |  9 ++++++---
 .../src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java    |  9 +++++++++
 .../java/org/apache/iotdb/jdbc/IoTDBPreparedStatementTest.java |  8 +++++---
 4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
index e33bde2c160..06da42f8d66 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
@@ -31,6 +31,7 @@ import org.apache.iotdb.service.rpc.thrift.TSOpenSessionResp;
 import org.apache.iotdb.service.rpc.thrift.TSProtocolVersion;
 import org.apache.iotdb.service.rpc.thrift.TSSetTimeZoneReq;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.thrift.TException;
 import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.protocol.TCompactProtocol;
@@ -101,6 +102,15 @@ public class IoTDBConnection implements Connection {
 
   private String userName;
 
+  // default is tree
+  public String getSqlDialect() {
+    if (params != null && StringUtils.isNotBlank(params.getSqlDialect())) {
+      return params.getSqlDialect();
+    } else {
+      return "tree";
+    }
+  }
+
   // ms is 1_000, us is 1_000_000, ns is 1_000_000_000
   private int timeFactor = 1_000;
 
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 72e5c8b0818..a466f2b569e 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
@@ -894,9 +894,12 @@ public class IoTDBPreparedStatement extends IoTDBStatement implements PreparedSt
 
   @Override
   public void setString(int parameterIndex, String 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 + "\"");
+    // if the sql is an insert statement and the value is not a string literal, add single quotes
+    // The table model only supports single quotes, the tree model sql both single and double quotes
+    if (sql.trim().toUpperCase().startsWith("INSERT")
+        && !((x.startsWith("'") && x.endsWith("'"))
+            || ((x.startsWith("\"") && x.endsWith("\"")) && "tree".equals(getSqlDialect())))) {
+      this.parameters.put(parameterIndex, "'" + x + "'");
     } else {
       this.parameters.put(parameterIndex, x);
     }
diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
index def4daf643b..f29b18031f1 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
@@ -30,6 +30,7 @@ import org.apache.iotdb.service.rpc.thrift.TSExecuteBatchStatementReq;
 import org.apache.iotdb.service.rpc.thrift.TSExecuteStatementReq;
 import org.apache.iotdb.service.rpc.thrift.TSExecuteStatementResp;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.thrift.TException;
 import org.apache.tsfile.common.conf.TSFileConfig;
 
@@ -766,4 +767,12 @@ public class IoTDBStatement implements Statement {
   public int getTimeFactor() {
     return connection.getTimeFactor();
   }
+
+  public String getSqlDialect() {
+    if (connection != null && StringUtils.isNotBlank(connection.getSqlDialect())) {
+      return connection.getSqlDialect().toLowerCase();
+    } else {
+      return "tree";
+    }
+  }
 }
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 b7d8203cf93..1a523d9459f 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
@@ -332,7 +332,7 @@ public class IoTDBPreparedStatementTest {
   @SuppressWarnings("resource")
   @Test
   public void testInsertStatement2() throws Exception {
-    String sql = "INSERT INTO root.ln.wf01.wt01(time,a,b,c,d,e,f) VALUES(?,?,?,?,?,?,?)";
+    String sql = "INSERT INTO root.ln.wf01.wt01(time,a,b,c,d,e,f,g,h) VALUES(?,?,?,?,?,?,?,?,?)";
 
     IoTDBPreparedStatement ps =
         new IoTDBPreparedStatement(connection, client, sessionId, sql, zoneId);
@@ -343,13 +343,15 @@ public class IoTDBPreparedStatementTest {
     ps.setFloat(5, 123.423f);
     ps.setDouble(6, -1323.0);
     ps.setString(7, "\"abc\"");
+    ps.setString(8, "abc");
+    ps.setString(9, "'abc'");
     ps.execute();
 
     ArgumentCaptor<TSExecuteStatementReq> argument =
         ArgumentCaptor.forClass(TSExecuteStatementReq.class);
     verify(client).executeStatementV2(argument.capture());
     assertEquals(
-        "INSERT INTO root.ln.wf01.wt01(time,a,b,c,d,e,f) VALUES(2017-11-01T00:13:00,false,123,123234345,123.423,-1323.0,\"abc\")",
+        "INSERT INTO root.ln.wf01.wt01(time,a,b,c,d,e,f,g,h) VALUES(2017-11-01T00:13:00,false,123,123234345,123.423,-1323.0,\"abc\",'abc','abc')",
         argument.getValue().getStatement());
   }
 
@@ -395,7 +397,7 @@ public class IoTDBPreparedStatementTest {
         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\")",
+        "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());
   }
 }