Search in sources :

Example 1 with RouteResultset

use of com.alibaba.cobar.route.RouteResultset in project cobar by alibaba.

the class ServerConnection method execute.

public void execute(String sql, int type) {
    // 状态检查
    if (txInterrupted) {
        writeErrMessage(ErrorCode.ER_YES, "Transaction error, need to rollback.");
        return;
    }
    // 检查当前使用的DB
    String db = this.schema;
    if (db == null) {
        writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return;
    }
    SchemaConfig schema = CobarServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return;
    }
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = ServerRouter.route(schema, sql, this.charset, this);
    } catch (SQLNonTransientException e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(sql).toString(), e);
        String msg = e.getMessage();
        writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return;
    }
    // session执行
    session.execute(rrs, type);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Example 2 with RouteResultset

use of com.alibaba.cobar.route.RouteResultset in project cobar by alibaba.

the class ExplainHandler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    stmt = stmt.substring(offset);
    RouteResultset rrs = getRouteResultset(c, stmt);
    if (rrs == null)
        return;
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.packetId;
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        field.packetId = ++packetId;
        buffer = field.write(buffer, c);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c);
    // write rows
    RouteResultsetNode[] rrsn = (rrs != null) ? rrs.getNodes() : EMPTY_ARRAY;
    for (RouteResultsetNode node : rrsn) {
        RowDataPacket row = getRow(node, c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(com.alibaba.cobar.net.mysql.ResultSetHeaderPacket) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Aggregations

RouteResultset (com.alibaba.cobar.route.RouteResultset)2 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)1 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)1 ResultSetHeaderPacket (com.alibaba.cobar.net.mysql.ResultSetHeaderPacket)1 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)1 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)1 ByteBuffer (java.nio.ByteBuffer)1 SQLNonTransientException (java.sql.SQLNonTransientException)1