一、解析引擎
上一篇文章说了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