Young-Leo commented on code in PR #17027:
URL: https://github.com/apache/iotdb/pull/17027#discussion_r2759150381


##########
iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/stmt/PreparedParameterSerde.java:
##########
@@ -47,80 +48,70 @@ public boolean isNull() {
     }
   }
 
-  private PreparedParameterSerializer() {}
+  private PreparedParameterSerde() {}
 
   /** Serialize parameters to binary format. */
   public static ByteBuffer serialize(Object[] values, int[] jdbcTypes, int 
count) {
-    try {
-      ByteArrayOutputStream baos = new ByteArrayOutputStream();
-      DataOutputStream dos = new DataOutputStream(baos);
-
-      dos.writeInt(count);
+    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
+      ReadWriteIOUtils.write(count, outputStream);
       for (int i = 0; i < count; i++) {
-        serializeParameter(dos, values[i], jdbcTypes[i]);
+        serializeParameter(outputStream, values[i], jdbcTypes[i]);
       }
-
-      dos.flush();
-      return ByteBuffer.wrap(baos.toByteArray());
+      return ByteBuffer.wrap(outputStream.toByteArray());
     } catch (IOException e) {
-      throw new RuntimeException("Failed to serialize parameters", e);
+      // Should not happen with ByteArrayOutputStream
+      throw new IllegalStateException("Failed to serialize parameters", e);
     }
   }
 
-  private static void serializeParameter(DataOutputStream dos, Object value, 
int jdbcType)
+  private static void serializeParameter(OutputStream outputStream, Object 
value, int jdbcType)
       throws IOException {
     if (value == null || jdbcType == Types.NULL) {
-      dos.writeByte(TSDataType.UNKNOWN.serialize());
+      ReadWriteIOUtils.write(TSDataType.UNKNOWN, outputStream);
       return;
     }
 
     switch (jdbcType) {
       case Types.BOOLEAN:
-        dos.writeByte(TSDataType.BOOLEAN.serialize());
-        dos.writeByte((Boolean) value ? 1 : 0);
+        ReadWriteIOUtils.write(TSDataType.BOOLEAN, outputStream);
+        ReadWriteIOUtils.write((Boolean) value, outputStream);

Review Comment:
   Fixed.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java:
##########
@@ -1637,31 +1616,48 @@ public TSStatus 
deallocatePreparedStatement(TSDeallocatePreparedReq req) {
     }
   }
 
-  private Literal convertToLiteral(DeserializedParam param) {
+  private Pair<Literal, String> convertToLiteralWithString(DeserializedParam 
param) {
     if (param.isNull()) {
-      return new NullLiteral();
+      return new Pair<>(new NullLiteral(), "NULL");
     }
 
     switch (param.type) {
       case BOOLEAN:
-        return new BooleanLiteral((Boolean) param.value ? "true" : "false");
+        String boolStr = (boolean) param.value ? "true" : "false";
+        return new Pair<>(new BooleanLiteral(boolStr), boolStr);
       case INT32:
       case INT64:
-        return new LongLiteral(String.valueOf(param.value));
+        String numStr = String.valueOf(param.value);
+        return new Pair<>(new LongLiteral(numStr), numStr);
       case FLOAT:
-        return new DoubleLiteral((Float) param.value);
+        String floatStr = String.valueOf(param.value);
+        return new Pair<>(new DoubleLiteral((Float) param.value), floatStr);
       case DOUBLE:
-        return new DoubleLiteral((Double) param.value);
+        String doubleStr = String.valueOf(param.value);
+        return new Pair<>(new DoubleLiteral((Double) param.value), doubleStr);
       case TEXT:
       case STRING:
-        return new StringLiteral((String) param.value);
+        String strVal = (String) param.value;
+        // Escape single quotes for SQL
+        String escapedStr = "'" + strVal.replace("'", "''") + "'";
+        return new Pair<>(new StringLiteral(strVal), escapedStr);
       case BLOB:
-        return new BinaryLiteral((byte[]) param.value);
+        byte[] bytes = (byte[]) param.value;
+        String hexStr = "X'" + bytesToHex(bytes) + "'";
+        return new Pair<>(new BinaryLiteral(bytes), hexStr);
       default:
         throw new IllegalArgumentException("Unknown parameter type: " + 
param.type);
     }
   }
 
+  private static String bytesToHex(byte[] bytes) {
+    StringBuilder sb = new StringBuilder(bytes.length * 2);
+    for (byte b : bytes) {
+      sb.append(String.format("%02X", b));
+    }
+    return sb.toString();
+  }

Review Comment:
   Updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to