Commit 54e9da56 authored by terrymanu's avatar terrymanu
Browse files

refactor CommandPacket to interface

parent 5ef67a1a
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ public final class MySQLFrontendHandler extends FrontendHandler {
                    CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, connectionId, payload);
                    for (DatabasePacket each : commandPacket.execute().getPackets()) {
                        context.writeAndFlush(each);
                        if (each instanceof OKPacket) {
                            return;
                        }
                    }
                    while (commandPacket.next()) {
                        // TODO try to use wait notify
+4 −10
Original line number Diff line number Diff line
@@ -18,28 +18,22 @@
package io.shardingsphere.proxy.transport.mysql.packet;

import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * MySQL packet.
 *
 * @author zhangliang
 */
@RequiredArgsConstructor
@Getter
public abstract class MySQLPacket implements DatabasePacket {
public interface MySQLPacket extends DatabasePacket {
    
    public static final int PAYLOAD_LENGTH = 3;
    int PAYLOAD_LENGTH = 3;
    
    public static final int SEQUENCE_LENGTH = 1;
    
    private final int sequenceId;
    int SEQUENCE_LENGTH = 1;
    
    /**
     * Write packet to byte buffer.
     *
     * @param payload packet payload to be write
     */
    public abstract void write(MySQLPacketPayload payload);
    void write(MySQLPacketPayload payload);
}
+4 −8
Original line number Diff line number Diff line
@@ -27,30 +27,26 @@ import io.shardingsphere.proxy.transport.mysql.packet.command.reponse.CommandRes
 * @author zhangliang
 * @author wangkai
 */
public abstract class CommandPacket extends MySQLPacket {
    
    public CommandPacket(final int sequenceId) {
        super(sequenceId);
    }
public interface CommandPacket extends MySQLPacket {
    
    /**
     * Execute command.
     * 
     * @return result packets to be sent
     */
    public abstract CommandResponsePackets execute();
    CommandResponsePackets execute();
    
    /**
     * Goto next result value.
     *
     * @return has more result value or not
     */
    public abstract boolean next();
    boolean next();
    
    /**
     * Get result value.
     *
     * @return database packet of result value
     */
    public abstract DatabasePacket getResultValue();
    DatabasePacket getResultValue();
}
+7 −6
Original line number Diff line number Diff line
@@ -22,20 +22,21 @@ import io.shardingsphere.proxy.transport.mysql.constant.ServerErrorCode;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import io.shardingsphere.proxy.transport.mysql.packet.command.reponse.CommandResponsePackets;
import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * Unsupported command packet.
 *
 * @author zhangliang
 */
public final class UnsupportedCommandPacket extends CommandPacket {
@RequiredArgsConstructor
public final class UnsupportedCommandPacket implements CommandPacket {
    
    private final CommandPacketType type;
    @Getter
    private final int sequenceId;
    
    public UnsupportedCommandPacket(final int sequenceId, final CommandPacketType type) {
        super(sequenceId);
        this.type = type;
    }
    private final CommandPacketType type;
    
    @Override
    public CommandResponsePackets execute() {
+6 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket;
import io.shardingsphere.proxy.transport.mysql.packet.command.reponse.CommandResponsePackets;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
@@ -31,12 +32,15 @@ import lombok.extern.slf4j.Slf4j;
 * @author zhangyonglun
 */
@Slf4j
public class ComStmtClosePacket extends CommandPacket {
public class ComStmtClosePacket implements CommandPacket {
    
    @Getter
    private final int sequenceId;
    
    private final int statementId;
    
    public ComStmtClosePacket(final int sequenceId, final MySQLPacketPayload payload) {
        super(sequenceId);
        this.sequenceId = sequenceId;
        statementId = payload.readInt4();
    }
    
Loading