/* * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package org.opencloudb.net.mysql; import io.netty.buffer.ByteBuf; import java.sql.SQLNonTransientException; import org.apache.log4j.Logger; import org.opencloudb.MycatSystem; import org.opencloudb.config.ErrorCode; import org.opencloudb.config.Fields; import org.opencloudb.config.model.SchemaConfig; import org.opencloudb.mysql.PacketUtil; import org.opencloudb.net.ConnectionInfo; import org.opencloudb.net.FrontSession; import org.opencloudb.route.RouteResultset; import org.opencloudb.route.RouteResultsetNode; import org.opencloudb.server.parser.ServerParse; import org.opencloudb.util.StringUtil; /** * @author mycat */ public class ExplainHandler { private static final Logger logger = Logger.getLogger(ExplainHandler.class); private static final RouteResultsetNode[] EMPTY_ARRAY = new RouteResultsetNode[0]; private static final int FIELD_COUNT = 2; private static final FieldPacket[] fields = new FieldPacket[FIELD_COUNT]; static { fields[0] = PacketUtil.getField("DATA_NODE", Fields.FIELD_TYPE_VAR_STRING); fields[1] = PacketUtil.getField("SQL", Fields.FIELD_TYPE_VAR_STRING); } public static void handle(String stmt, FrontSession session, int offset) { stmt = stmt.substring(offset); ConnectionInfo conInfo = session.getConInfo(); String charset = conInfo.getCharset(); RouteResultset rrs = getRouteResultset(session, stmt); if (rrs == null) return; ByteBuf buffer = session.allocate(512, 1024 * 4); // write header ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT); byte packetId = header.packetId; buffer = header.write(buffer); // write fields for (FieldPacket field : fields) { field.packetId = ++packetId; buffer = field.write(buffer); } // write eof EOFPacket eof = new EOFPacket(); eof.packetId = ++packetId; buffer = eof.write(buffer); // write rows RouteResultsetNode[] rrsn = (rrs != null) ? rrs.getNodes() : EMPTY_ARRAY; for (RouteResultsetNode node : rrsn) { RowDataPacket row = getRow(node, charset); row.packetId = ++packetId; buffer = row.write(buffer); } // write last eof EOFPacket lastEof = new EOFPacket(); lastEof.packetId = ++packetId; buffer = lastEof.write(buffer); // post write session.write(buffer); } private static RowDataPacket getRow(RouteResultsetNode node, String charset) { RowDataPacket row = new RowDataPacket(FIELD_COUNT); row.add(StringUtil.encode(node.getName(), charset)); row.add(StringUtil.encode(node.getStatement(), charset)); return row; } private static RouteResultset getRouteResultset(FrontSession session, String stmt) { ConnectionInfo conInfo = session.getConInfo(); String db = conInfo.getSchema(); if (db == null) { session.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected"); return null; } SchemaConfig schema = MycatSystem.getInstance().getConfig() .getSchemas().get(db); if (schema == null) { session.writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'"); return null; } try { int sqlType = ServerParse.parse(stmt) & 0xff; return MycatSystem .getInstance() .getRouterService() .route(MycatSystem.getInstance().getConfig().getSystem(), schema, sqlType, stmt, conInfo.getCharset(), null); } catch (SQLNonTransientException e) { StringBuilder s = new StringBuilder(); logger.warn(s.append(session.getConInfo()).append(stmt).toString() + " error:" + e); String msg = e.getMessage(); session.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e .getClass().getSimpleName() : msg); return null; } } }