/*
 * 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.backend.response.query;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchema;
import org.apache.shardingsphere.shardingproxy.backend.schema.ShardingSchema;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Collection;

/**
 * Query header.
 *
 * @author zhangliang
 */
@RequiredArgsConstructor
@Getter
public final class QueryHeader {
    
    private final String schema;
    
    private final String table;
    
    private final String columnLabel;
    
    private final String columnName;
    
    private final int columnLength;
    
    private final Integer columnType;
    
    private final int decimals;
    
    public QueryHeader(final ResultSetMetaData resultSetMetaData, final LogicSchema logicSchema, final int columnIndex) throws SQLException {
        this.schema = logicSchema.getName();
        if (logicSchema instanceof ShardingSchema) {
            Collection<String> tableNames = logicSchema.getShardingRule().getLogicTableNames(resultSetMetaData.getTableName(columnIndex));
            this.table = tableNames.isEmpty() ? "" : tableNames.iterator().next();
        } else {
            this.table = resultSetMetaData.getTableName(columnIndex);
        }
        this.columnLabel = resultSetMetaData.getColumnLabel(columnIndex);
        this.columnName = resultSetMetaData.getColumnName(columnIndex);
        this.columnLength = resultSetMetaData.getColumnDisplaySize(columnIndex);
        this.columnType = resultSetMetaData.getColumnType(columnIndex);
        this.decimals = resultSetMetaData.getScale(columnIndex);
    }
}

@@ -887,52 +887,47 @@
 bok.
-Getter;%0Aimport lombok.RequiredArgsConstructo
+AllArgsConstructor;%0Aimport lombok.Gette
 r;%0Ai
@@ -1229,16 +1229,11 @@
 */%0A@
-Required
+All
 Args
@@ -1369,38 +1369,32 @@
    %0A    private 
-final 
 String columnLab
@@ -1410,30 +1410,24 @@
     private 
-final 
 String colum
@@ -2488,10 +2488,404 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * Set column label and column name.%0A     * %0A     * @param logicColumnName logic column name%0A     */%0A    public void setColumnLabelAndName(final String logicColumnName) %7B%0A        if (columnLabel.equals(columnName)) %7B%0A            columnLabel = logicColumnName;%0A            columnName = logicColumnName;%0A        %7D else %7B%0A            columnName = logicColumnName;%0A        %7D%0A    %7D%0A
 %7D%0A

add setColumnLabelAndName()