MySQLComQueryPacketExecutor.java 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query;
19

20
import lombok.Getter;
21 22 23 24 25 26 27 28 29 30 31 32
import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLColumnType;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnDefinition41Packet;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnFieldDetailFlag;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLFieldCountPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.MySQLTextResultSetRowPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLEofPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
33
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
L
Liang Zhang 已提交
34
import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
35 36 37 38 39
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
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.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
40
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
41 42 43 44
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFactory;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
45 46 47 48 49 50 51 52

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
53
 * COM_QUERY command packet executor for MySQL.
54
 */
55
public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
56
    
57
    private final TextProtocolBackendHandler textProtocolBackendHandler;
58
    
59 60
    @Getter
    private volatile boolean isQueryResponse;
61
    
62 63 64 65
    @Getter
    private volatile boolean isUpdateResponse;
    
    @Getter
66 67
    private volatile boolean isErrorResponse;
    
68 69
    private int currentSequenceId;
    
70
    public MySQLComQueryPacketExecutor(final MySQLComQueryPacket comQueryPacket, final BackendConnection backendConnection) {
71
        textProtocolBackendHandler = TextProtocolBackendHandlerFactory.newInstance(DatabaseTypes.getActualDatabaseType("MySQL"), comQueryPacket.getSql(), backendConnection);
72 73 74
    }
    
    @Override
L
Liang Zhang 已提交
75
    public Collection<DatabasePacket<?>> execute() throws SQLException {
76
        if (ProxySchemaContexts.getInstance().getSchemaContexts().isCircuitBreak()) {
77
            return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
78
        }
79
        BackendResponse backendResponse = getBackendResponse();
80 81 82
        if (backendResponse instanceof QueryResponse) {
            isQueryResponse = true;
            return createQueryPackets((QueryResponse) backendResponse);
83 84
        }
        if (backendResponse instanceof UpdateResponse) {
85
            isUpdateResponse = true;
86
            return Collections.singletonList(createUpdatePacket((UpdateResponse) backendResponse));
87
        }
88 89
        isErrorResponse = true;
        return Collections.singletonList(createErrorPacket(((ErrorResponse) backendResponse).getCause()));
90 91
    }
    
92 93 94 95 96 97 98 99 100 101 102 103
    private BackendResponse getBackendResponse() {
        BackendResponse result;
        try {
            result = textProtocolBackendHandler.execute();
        // CHECKSTYLE:OFF
        } catch (final Exception ex) {
        // CHECKSTYLE:OFF
            result = new ErrorResponse(ex);
        }
        return result;
    }
    
L
Liang Zhang 已提交
104 105
    private Collection<DatabasePacket<?>> createQueryPackets(final QueryResponse backendResponse) {
        Collection<DatabasePacket<?>> result = new LinkedList<>();
106 107 108
        List<QueryHeader> queryHeader = backendResponse.getQueryHeaders();
        result.add(new MySQLFieldCountPacket(++currentSequenceId, queryHeader.size()));
        for (QueryHeader each : queryHeader) {
109
            result.add(new MySQLColumnDefinition41Packet(++currentSequenceId, getColumnFieldDetailFlag(each), each.getSchema(), each.getTable(), each.getTable(), 
110
                    each.getColumnLabel(), each.getColumnName(), each.getColumnLength(), MySQLColumnType.valueOfJDBCType(each.getColumnType()), each.getDecimals()));
111 112 113 114
        }
        result.add(new MySQLEofPacket(++currentSequenceId));
        return result;
    }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    private int getColumnFieldDetailFlag(final QueryHeader header) {
        int result = 0;
        if (header.isPrimaryKey()) {
            result += MySQLColumnFieldDetailFlag.PRIMARY_KEY.getValue();
        }
        if (header.isNotNull()) {
            result += MySQLColumnFieldDetailFlag.NOT_NULL.getValue();
        }
        if (!header.isSigned()) {
            result += MySQLColumnFieldDetailFlag.UNSIGNED.getValue();
        }
        if (header.isAutoIncrement()) {
            result += MySQLColumnFieldDetailFlag.AUTO_INCREMENT.getValue();
        }
        return result;
    }
132
    
133 134 135 136 137 138 139 140
    private MySQLOKPacket createUpdatePacket(final UpdateResponse updateResponse) {
        return new MySQLOKPacket(1, updateResponse.getUpdateCount(), updateResponse.getLastInsertId());
    }
    
    private MySQLErrPacket createErrorPacket(final Exception cause) {
        return MySQLErrPacketFactory.newInstance(1, cause);
    }
    
141 142 143 144 145 146 147 148 149 150
    @Override
    public boolean next() throws SQLException {
        return textProtocolBackendHandler.next();
    }
    
    @Override
    public MySQLPacket getQueryData() throws SQLException {
        return new MySQLTextResultSetRowPacket(++currentSequenceId, textProtocolBackendHandler.getQueryData().getData());
    }
}