/*
 * 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.AllArgsConstructor;
import lombok.Getter;
import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchema;
import org.apache.shardingsphere.shardingproxy.backend.schema.impl.ShardingSchema;

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

/**
 * Query header.
 *
 * @author zhangliang
 */
@AllArgsConstructor
@Getter
public final class QueryHeader {
    
    private final String schema;
    
    private final String table;
    
    private String columnLabel;
    
    private String columnName;
    
    private final int columnLength;
    
    private final Integer columnType;
    
    private final int decimals;

    private final boolean signed;

    private final boolean primaryKey;

    private final boolean notNull;

    private final boolean autoIncrement;
    
    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();
            if (logicSchema.getMetaData().getTables().containsTable(resultSetMetaData.getTableName(columnIndex))) {
                this.primaryKey = logicSchema.getMetaData().getTables().get(resultSetMetaData.getTableName(columnIndex)).getColumns()
                        .get(resultSetMetaData.getColumnName(columnIndex).toLowerCase()).isPrimaryKey();
            } else {
                this.primaryKey = false;
            }
        } else {
            this.table = resultSetMetaData.getTableName(columnIndex);
            this.primaryKey = false;
        }
        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);
        this.signed = resultSetMetaData.isSigned(columnIndex);
        this.notNull = resultSetMetaData.isNullable(columnIndex) == ResultSetMetaData.columnNoNulls;
        this.autoIncrement = resultSetMetaData.isAutoIncrement(columnIndex);
    }
    
    /**
     * Set column label and column name.
     * 
     * @param logicColumnName logic column name
     */
    public void setColumnLabelAndName(final String logicColumnName) {
        if (columnLabel.equals(columnName)) {
            columnLabel = logicColumnName;
            columnName = logicColumnName;
        } else {
            columnName = logicColumnName;
        }
    }
}

@@ -1899,24 +1899,161 @@
 .getName();%0A
+        String actualTableName = null == resultSetMetaData.getTableName(columnIndex) ? %22%22 : resultSetMetaData.getTableName(columnIndex);%0A
         if (
@@ -2190,51 +2190,23 @@
 mes(
-resultSetMetaData.getTableName(columnIndex)
+actualTableName
 );%0A 
@@ -2359,51 +2359,23 @@
 ble(
-resultSetMetaData.getTableName(columnIndex)
+actualTableName
 )) %7B
@@ -2455,51 +2455,23 @@
 get(
-resultSetMetaData.getTableName(columnIndex)
+actualTableName
 ).ge
@@ -2708,51 +2708,23 @@
 e = 
-resultSetMetaData.getTableName(columnIndex)
+actualTableName
 ;%0A  

fix NullPointException for resultSetMetaData.getTableName