Search in sources :

Example 86 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowFullTables method response.

/**
 * response method.
 * @param c
 */
public static void response(ServerConnection c, String stmt, int type) {
    String showSchemal = SchemaUtil.parseShowTableSchema(stmt);
    String cSchema = showSchemal == null ? c.getSchema() : showSchemal;
    SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(cSchema);
    if (schema != null) {
        // 不分库的schema,show tables从后端 mysql中查
        String node = schema.getDataNode();
        if (!Strings.isNullOrEmpty(node)) {
            c.execute(stmt, ServerParse.SHOW);
            return;
        }
    } else {
        c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
    }
    // 分库的schema,直接从SchemaConfig中获取所有表名
    Map<String, String> parm = buildFields(c, stmt);
    Set<String> tableSet = getTableSet(c, parm);
    int i = 0;
    byte packetId = 0;
    header.packetId = ++packetId;
    fields[i] = PacketUtil.getField("Tables in " + parm.get(SCHEMA_KEY), Fields.FIELD_TYPE_VAR_STRING);
    fields[i].packetId = ++packetId;
    fields[i + 1] = PacketUtil.getField("Table_type  ", Fields.FIELD_TYPE_VAR_STRING);
    fields[i + 1].packetId = ++packetId;
    eof.packetId = ++packetId;
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    packetId = eof.packetId;
    for (String name : tableSet) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(name.toLowerCase(), c.getCharset()));
        row.add(StringUtil.encode("BASE TABLE", c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 87 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowTables method response.

/**
 * response method.
 * @param c
 */
public static void response(ServerConnection c, String stmt, int type) {
    String showSchemal = SchemaUtil.parseShowTableSchema(stmt);
    String cSchema = showSchemal == null ? c.getSchema() : showSchemal;
    SchemaConfig schema = MycatServer.getInstance().getConfig().getSchemas().get(cSchema);
    if (schema != null) {
        // 不分库的schema,show tables从后端 mysql中查
        String node = schema.getDataNode();
        if (!Strings.isNullOrEmpty(node)) {
            c.execute(stmt, ServerParse.SHOW);
            return;
        }
    } else {
        c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected");
        return;
    }
    // 分库的schema,直接从SchemaConfig中获取所有表名
    Map<String, String> parm = buildFields(c, stmt);
    java.util.Set<String> tableSet = getTableSet(c, parm);
    int i = 0;
    byte packetId = 0;
    header.packetId = ++packetId;
    fields[i] = PacketUtil.getField("Tables in " + parm.get(SCHEMA_KEY), Fields.FIELD_TYPE_VAR_STRING);
    fields[i++].packetId = ++packetId;
    eof.packetId = ++packetId;
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    packetId = eof.packetId;
    for (String name : tableSet) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(name.toLowerCase(), c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // post write
    c.write(buffer);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 88 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class SelectDatabase method response.

public static void response(ServerConnection c) {
    ByteBuffer buffer = c.allocate();
    buffer = header.write(buffer, c, true);
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    buffer = eof.write(buffer, c, true);
    byte packetId = eof.packetId;
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    row.add(StringUtil.encode(c.getSchema(), c.getCharset()));
    row.packetId = ++packetId;
    buffer = row.write(buffer, c, true);
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 89 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class SelectVariables method execute.

public static void execute(ServerConnection c, String sql) {
    String subSql = sql.substring(sql.indexOf("SELECT") + 6);
    List<String> splitVar = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(subSql);
    splitVar = convert(splitVar);
    int FIELD_COUNT = splitVar.size();
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    FieldPacket[] fields = new FieldPacket[FIELD_COUNT];
    int i = 0;
    byte packetId = 0;
    header.packetId = ++packetId;
    for (int i1 = 0, splitVarSize = splitVar.size(); i1 < splitVarSize; i1++) {
        String s = splitVar.get(i1);
        fields[i] = PacketUtil.getField(s, Fields.FIELD_TYPE_VAR_STRING);
        fields[i++].packetId = ++packetId;
    }
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    // byte packetId = eof.packetId;
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    for (int i1 = 0, splitVarSize = splitVar.size(); i1 < splitVarSize; i1++) {
        String s = splitVar.get(i1);
        String value = variables.get(s) == null ? "" : variables.get(s);
        row.add(value.getBytes());
    }
    row.packetId = ++packetId;
    buffer = row.write(buffer, c, true);
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) FieldPacket(io.mycat.net.mysql.FieldPacket) ByteBuffer(java.nio.ByteBuffer)

Example 90 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class JDBCConnection method ouputResultSet.

private void ouputResultSet(ServerConnection sc, String sql) throws SQLException {
    ResultSet rs = null;
    Statement stmt = null;
    try {
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        List<FieldPacket> fieldPks = new LinkedList<FieldPacket>();
        ResultSetUtil.resultSetToFieldPacket(sc.getCharset(), fieldPks, rs, this.isSpark);
        int colunmCount = fieldPks.size();
        ByteBuffer byteBuf = sc.allocate();
        ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
        headerPkg.fieldCount = fieldPks.size();
        headerPkg.packetId = ++packetId;
        byteBuf = headerPkg.write(byteBuf, sc, true);
        byteBuf.flip();
        byte[] header = new byte[byteBuf.limit()];
        byteBuf.get(header);
        byteBuf.clear();
        List<byte[]> fields = new ArrayList<byte[]>(fieldPks.size());
        Iterator<FieldPacket> itor = fieldPks.iterator();
        while (itor.hasNext()) {
            FieldPacket curField = itor.next();
            curField.packetId = ++packetId;
            byteBuf = curField.write(byteBuf, sc, false);
            byteBuf.flip();
            byte[] field = new byte[byteBuf.limit()];
            byteBuf.get(field);
            byteBuf.clear();
            fields.add(field);
        }
        EOFPacket eofPckg = new EOFPacket();
        eofPckg.packetId = ++packetId;
        byteBuf = eofPckg.write(byteBuf, sc, false);
        byteBuf.flip();
        byte[] eof = new byte[byteBuf.limit()];
        byteBuf.get(eof);
        byteBuf.clear();
        this.respHandler.fieldEofResponse(header, fields, eof, this);
        // output row
        while (rs.next()) {
            RowDataPacket curRow = new RowDataPacket(colunmCount);
            for (int i = 0; i < colunmCount; i++) {
                int j = i + 1;
                if (MysqlDefs.isBianry((byte) fieldPks.get(i).type)) {
                    curRow.add(rs.getBytes(j));
                } else if (fieldPks.get(i).type == MysqlDefs.FIELD_TYPE_DECIMAL || fieldPks.get(i).type == (MysqlDefs.FIELD_TYPE_NEW_DECIMAL - 256)) {
                    // field type is unsigned byte
                    // ensure that do not use scientific notation format
                    BigDecimal val = rs.getBigDecimal(j);
                    curRow.add(StringUtil.encode(val != null ? val.toPlainString() : null, sc.getCharset()));
                } else {
                    curRow.add(StringUtil.encode(rs.getString(j), sc.getCharset()));
                }
            }
            curRow.packetId = ++packetId;
            byteBuf = curRow.write(byteBuf, sc, false);
            byteBuf.flip();
            byte[] row = new byte[byteBuf.limit()];
            byteBuf.get(row);
            byteBuf.clear();
            this.respHandler.rowResponse(row, this);
        }
        fieldPks.clear();
        // end row
        eofPckg = new EOFPacket();
        eofPckg.packetId = ++packetId;
        byteBuf = eofPckg.write(byteBuf, sc, false);
        byteBuf.flip();
        eof = new byte[byteBuf.limit()];
        byteBuf.get(eof);
        sc.recycle(byteBuf);
        this.respHandler.rowEofResponse(eof, this);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) BigDecimal(java.math.BigDecimal) FieldPacket(io.mycat.net.mysql.FieldPacket)

Aggregations

EOFPacket (io.mycat.net.mysql.EOFPacket)154 ByteBuffer (java.nio.ByteBuffer)153 FieldPacket (io.mycat.net.mysql.FieldPacket)146 RowDataPacket (io.mycat.net.mysql.RowDataPacket)137 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)18 NIOProcessor (io.mycat.net.NIOProcessor)14 ServerConnection (io.mycat.server.ServerConnection)12 UserStat (io.mycat.statistic.stat.UserStat)12 Map (java.util.Map)10 SchemaConfig (io.mycat.config.model.SchemaConfig)8 ArrayList (java.util.ArrayList)7 MycatConfig (io.mycat.config.MycatConfig)6 FrontendConnection (io.mycat.net.FrontendConnection)6 BackendConnection (io.mycat.backend.BackendConnection)4 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)4 UserConfig (io.mycat.config.model.UserConfig)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 TreeSet (java.util.TreeSet)3