第六章 shardingsphere源码-SQL解析

文章详细描述了Shardingsphere中的SQL解析过程,包括词法解析和语法解析,以及ANTLR在生成解析器中的应用。重点介绍了从MySQL编解码到CommandExecutor的具体步骤,展示了SQL解析如何转化为抽象语法树并执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、解析引擎

上一篇文章说了shardingsphere数据加密的整体流程,已经SQL的一生>整体数据加解密的流程

1.什么是SQL解析

SQL 是比较简单的。 不过,它依然是一门完善的编程语言,因此对 SQL 的语法进行解析,与解析其他编程语言(如:Java 语言、C 语言、Go 语言等)并无本质区别。

解析过程分为词法解析和语法解析。 词法解析器用于将 SQL 拆解为不可再分的原子符号,称为 Token。并根据不同数据库方言所提供的字典,将其归类为关键字,表达式,字面量和操作符。 再使用语法解析器将词法解析器的输出转换为抽象语法树。

再shardingsphere官网中,有一个sql解析为语法树的例子:

SELECT id, name FROM t_user WHERE status = 'ACTIVE' AND age > 18

在这里插入图片描述

2.具体sql解析流程

由mysql编解码引擎来解码,MySQLPacketCodecEngine的decode方法

向启动的代理中发送一个sql,如SELECT * FROM tttttt1_copy1,如果想要捕捉请求的报文,可以在decode中,添加代码
必须转化为ascii码,咱们才可以看明白,二进制->十六进制->ascii码。这个报文的十六进制:
1E 00 00 00 03 53 45 4C 45 43 54 20 2A 20 46 52 4F 4D 20 60 74 74 74 74 74 74 31 5F 63 6F 70 79 31 60
转换为:ascii码:
SELECT * FROM tttttt1_copy1 ,53 对应的是S

public void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out) {
        // 自己写的,将二进制01转为为16进制
        System.out.println("ByteBuf readableBytes length: " + in.readableBytes());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < in.readableBytes(); i++) {
        sb.append(String.format("%02X ", in.getByte(i)));
        }
        System.out.println("sixteen ByteBuf hex: " + sb);
        
        int payloadLength = in.markReaderIndex().readUnsignedMediumLE();
        int remainPayloadLength = SEQUENCE_LENGTH + payloadLength;
        if (in.readableBytes() < remainPayloadLength) {
            in.resetReaderIndex();
            return;
        }
        short sequenceId = in.readUnsignedByte();
        context.channel().attr(MySQLConstants.MYSQL_SEQUENCE_ID).get().set(sequenceId + 1);
        ByteBuf message = in.readRetainedSlice(payloadLength);
        if (MAX_PACKET_LENGTH == payloadLength) {
            pendingMessages.add(message);
        } else if (pendingMessages.isEmpty()) {
            out.add(message);
        } else {
            aggregateMessages(context, message, out);
        }
    }

16进制转换为ascii码

    public String hexToASCII(String hexStr) {
        StringBuilder output = new StringBuilder();
        // 去除空格并将 16 进制字符串转换为整数,再转换为对应的 ASCII 字符
        String[] hexArray = hexStr.split(" ");
        for (String hex : hexArray) {
            if (!hex.isEmpty()) {
                output.append((char) Integer.parseInt(hex, 16));
            }
        }
        return output.toString();
    }

然后再CommandExecutorTask进行统一的流转,无论是什么类型的都必须要走

    private boolean executeCommand(final ChannelHandlerContext context, final PacketPayload payload) throws SQLException {
        CommandExecuteEngine commandExecuteEngine = databaseProtocolFrontendEngine.getCommandExecuteEngine(); 
        CommandPacketType type = commandExecuteEngine.getCommandPacketType(payload);
        CommandPacket commandPacket = commandExecuteEngine.getCommandPacket(payload, type, connectionSession);
        CommandExecutor commandExecutor = commandExecuteEngine.getCommandExecutor(type, commandPacket, connectionSession);
        try {
            Collection<DatabasePacket<?>> responsePackets = commandExecutor.execute();
            if (responsePackets.isEmpty()) {
                return false;
            }
            responsePackets.forEach(context::write);
            if (commandExecutor instanceof QueryCommandExecutor) {
                commandExecuteEngine.writeQueryData(context, connectionSession.getBackendConnection(), (QueryCommandExecutor) commandExecutor, responsePackets.size());
            }
            return true;
        } catch (final SQLException | ShardingSphereSQLException | SQLDialectException ex) {
            databaseProtocolFrontendEngine.handleException(connectionSession, ex);
            throw ex;
        } finally {
            commandExecutor.close();
        }
    }

commandExecuteEngine.getCommandPacketType(payload); 获取命令包的类型

本sql,实际上是一个COM_QUERY,根据请求包 0x03 来进行匹配,所有的包的类型在MySQLCommandPacketType类中

CommandPacket commandPacket = commandExecuteEngine.getCommandPacket(payload, type, connectionSession);

这个会从请求报文中,读取出sql,也就是
在这里插入图片描述

CommandExecutor commandExecutor = commandExecuteEngine.getCommandExecutor(type, commandPacket, connectionSession);

获取执行器,不同的sql解析的步骤实际上不太相同,就以这个 COM_QUERY 简单查询为例,根据前面获取的命令包类型,
进入到不同的执行器中,这个sql也就是MySQLComQueryPacketExecutor

    public static CommandExecutor newInstance(final MySQLCommandPacketType commandPacketType, final CommandPacket commandPacket, final ConnectionSession connectionSession) throws SQLException {
        log.debug("Execute packet type: {}, value: {}", commandPacketType, commandPacket);
        switch (commandPacketType) {
            case COM_QUIT:
                return new MySQLComQuitExecutor();
            case COM_INIT_DB:
                return new MySQLComInitDbExecutor((MySQLComInitDbPacket) commandPacket, connectionSession);
            case COM_FIELD_LIST:
                return new MySQLComFieldListPacketExecutor((MySQLComFieldListPacket) commandPacket, connectionSession);
            case COM_QUERY:
                return new MySQLComQueryPacketExecutor((MySQLComQueryPacket) commandPacket, connectionSession);
            case COM_PING:
                return new MySQLComPingExecutor(connectionSession);
            case COM_STMT_PREPARE:
                return new MySQLComStmtPrepareExecutor((MySQLComStmtPreparePacket) commandPacket, connectionSession);
            case COM_STMT_EXECUTE:
                return new MySQLComStmtExecuteExecutor((MySQLComStmtExecutePacket) commandPacket, connectionSession);
            case COM_STMT_SEND_LONG_DATA:
                return new MySQLComStmtSendLongDataExecutor((MySQLComStmtSendLongDataPacket) commandPacket, connectionSession);
            case COM_STMT_RESET:
                return new MySQLComStmtResetExecutor((MySQLComStmtResetPacket) commandPacket, connectionSession);
            case COM_STMT_CLOSE:
                return new MySQLComStmtCloseExecutor((MySQLComStmtClosePacket) commandPacket, connectionSession);
            case COM_SET_OPTION:
                return new MySQLComSetOptionExecutor((MySQLComSetOptionPacket) commandPacket, connectionSession);
            case COM_RESET_CONNECTION:
                return new MySQLComResetConnectionExecutor(connectionSession);
            default:
                return new MySQLUnsupportedCommandExecutor(commandPacketType);
        }
    }

进去 这个执行器之后,有一个parseSql,也就是解析sql

    public MySQLComQueryPacketExecutor(final MySQLComQueryPacket packet, final ConnectionSession connectionSession) throws SQLException {
        this.connectionSession = connectionSession;
        DatabaseType databaseType = TypedSPIRegistry.getRegisteredService(DatabaseType.class, "MySQL");
        SQLStatement sqlStatement = parseSql(packet.getSql(), databaseType);
        proxyBackendHandler = areMultiStatements(connectionSession, sqlStatement, packet.getSql()) ? new MySQLMultiStatementsHandler(connectionSession, sqlStatement, packet.getSql())
                : ProxyBackendHandlerFactory.newInstance(databaseType, packet.getSql(), sqlStatement, connectionSession, packet.getHintValueContext());
        characterSet = connectionSession.getAttributeMap().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).get().getId();
    }

统一由sql解析引擎进行解析,SQLParserExecutor

    public ParseASTNode parse(final String sql) {
       // tokenStream: 抽象语法树的节点 ,也就是词法分析,将SQL语句解析为一个不可再分的单词,各个单词位置 ; parseTree: 解析树,具体执行的是由antlr所生成的代码来进行解析的,具体包位置如下图展示
        ParseASTNode result = twoPhaseParse(sql);
        if (result.getRootNode() instanceof ErrorNode) {
            throw new SQLParsingException(sql);
        }
        return result;
    }

ANTLR 是一个强大的解析器生成器,可以用于构建语言、工具和框架。它被广泛用于构建语言、工具和框架,可以根据语法定义生成解析器,这些解析器可以构建和遍历解析树。

在ANTLR中,可以使用SQL语法规则文件来定义SQL的语法结构,然后使用ANTLR工具生成对应的解析器代码。接下来,可以使用该解析器对输入的SQL语句进行解析,生成对应的抽象语法树(AST)。最后,可以通过遍历AST来执行相应的操作,例如查询数据、修改数据等。
在这里插入图片描述

解析完成之后,visitorEngine.visit(parserEngine.parse(sql, false));

由ParseTreeVisitor创建SQLStatement,完成解析,如:该sql解析大致如下,按照上面语法树
在这里插入图片描述

二、总结

本篇主要介绍了,shardingsphere SQL解析,中间经过了什么流程,实际上真正解析的是antlr

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值