/*
 * 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.proxy.backend.text.admin;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.sql.SQLException;

/**
 * Backend handler for unicast.
 */
@RequiredArgsConstructor
public final class UnicastBackendHandler implements TextProtocolBackendHandler {
    
    private final DatabaseCommunicationEngineFactory databaseCommunicationEngineFactory = DatabaseCommunicationEngineFactory.getInstance();
    
    private final String sql;
    
    private final SQLStatement sqlStatement;
    
    private final BackendConnection backendConnection;
    
    private DatabaseCommunicationEngine databaseCommunicationEngine;
    
    @Override
    public BackendResponse execute() {
        // TODO we should remove set default ShardingSphere schema after parser can recognize all DAL broadcast SQL.
        if (null == backendConnection.getSchema()) {
            return new ErrorResponse(new NoDatabaseSelectedException());
        }
        databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection);
        return databaseCommunicationEngine.execute();
    }
    
    @Override
    public boolean next() throws SQLException {
        return databaseCommunicationEngine.next();
    }
    
    @Override
    public QueryData getQueryData() throws SQLException {
        return databaseCommunicationEngine.getQueryData();
    }
}

@@ -896,16 +896,78 @@
 ructor;%0A
+import org.apache.shardingsphere.infra.context.SchemaContext;%0A
 import o
@@ -1550,16 +1550,91 @@
 ryData;%0A
+import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;%0A
 import o
@@ -1814,16 +1814,38 @@
 ception;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -2402,24 +2402,347 @@
 execute() %7B%0A
+        if (null == backendConnection.getSchema()) %7B%0A            Map%3CString, SchemaContext%3E schemaContexts = ProxySchemaContexts.getInstance().getSchemaContexts().getSchemaContexts();%0A            if (schemaContexts.isEmpty()) %7B%0A                return new ErrorResponse(new NoDatabaseSelectedException());%0A            %7D%0A    
         // T
@@ -2850,35 +2850,27 @@
 QL.%0A        
-if (null ==
+   
  backendConn
@@ -2880,91 +2880,64 @@
 ion.
-getSchema()) %7B%0A            return new ErrorResponse(new NoDatabaseSelectedException
+setCurrentSchema(schemaContexts.keySet().iterator().next
 ());

Fix bug for UnicastBackendHandler with schema (#7182)