Young-Leo commented on code in PR #17027: URL: https://github.com/apache/iotdb/pull/17027#discussion_r2754407585
########## iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/stmt/PreparedParameterSerializer.java: ########## @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.rpc.stmt; + +import org.apache.tsfile.enums.TSDataType; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.sql.Types; +import java.util.ArrayList; +import java.util.List; + +/** Serializer for PreparedStatement parameters. */ +public class PreparedParameterSerializer { + + public static class DeserializedParam { + public final TSDataType type; + public final Object value; + + DeserializedParam(TSDataType type, Object value) { + this.type = type; + this.value = value; + } + + public boolean isNull() { + return type == TSDataType.UNKNOWN || value == null; + } + } + + private PreparedParameterSerializer() {} + + /** 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); + for (int i = 0; i < count; i++) { + serializeParameter(dos, values[i], jdbcTypes[i]); + } + + dos.flush(); + return ByteBuffer.wrap(baos.toByteArray()); + } catch (IOException e) { + throw new RuntimeException("Failed to serialize parameters", e); + } + } + + private static void serializeParameter(DataOutputStream dos, Object value, int jdbcType) + throws IOException { + if (value == null || jdbcType == Types.NULL) { + dos.writeByte(TSDataType.UNKNOWN.serialize()); + return; + } + + switch (jdbcType) { + case Types.BOOLEAN: + dos.writeByte(TSDataType.BOOLEAN.serialize()); + dos.writeByte((Boolean) value ? 1 : 0); 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]
