/*
 * 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.shardingsphere.shardingproxy.transport.postgresql.packet.command.query.text;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.shardingproxy.transport.postgresql.packet.PostgreSQLPacket;
import org.apache.shardingsphere.shardingproxy.transport.postgresql.packet.PostgreSQLPacketPayload;
import org.apache.shardingsphere.shardingproxy.transport.postgresql.packet.command.PostgreSQLCommandPacketType;

import java.util.List;

/**
 * PostgreSQL data row packet.
 *
 * @author zhangyonglun
 */
@RequiredArgsConstructor
@Getter
public final class PostgreSQLDataRowPacket implements PostgreSQLPacket {
    
    private final char messageType = PostgreSQLCommandPacketType.DATA_ROW.getValue();
    
    private final List<Object> data;
    
    @Override
    public void write(final PostgreSQLPacketPayload payload) {
        payload.writeInt2(data.size());
        for (Object each : data) {
            if (null == each) {
                payload.writeInt1('\0');
            } else {
                if (each instanceof byte[]) {
                    payload.writeInt4(((byte[]) each).length);
                    payload.writeBytes((byte[]) each);
                } else {
                    String columnData = each.toString();
                    payload.writeInt4(columnData.length());
                    payload.writeStringEOF(columnData);
                }
            }
        }
    }
    
    @Override
    public int getSequenceId() {
        return 0;
    }
}

@@ -1814,14 +1814,20 @@
 eInt
-1('%5C0'
+4(0xFFFFFFFF
 );%0A 

#1892, fix length when row data is null