Search in sources :

Example 1 with RequestFilePacket

use of io.mycat.net.mysql.RequestFilePacket in project Mycat-Server by MyCATApache.

the class ServerLoadDataInfileHandler method start.

@Override
public void start(String sql) {
    clear();
    this.sql = sql;
    SQLStatementParser parser = new MycatStatementParser(sql);
    statement = (MySqlLoadDataInFileStatement) parser.parseStatement();
    fileName = parseFileName(sql);
    if (fileName == null) {
        serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, " file name is null !");
        clear();
        return;
    }
    schema = MycatServer.getInstance().getConfig().getSchemas().get(serverConnection.getSchema());
    tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
    tableName = statement.getTableName().getSimpleName().toUpperCase();
    tableConfig = schema.getTables().get(tableName);
    if (tableConfig.getRule().getRuleAlgorithm() instanceof SlotFunction) {
        shoudAddSlot = true;
    }
    tempPath = SystemConfig.getHomePath() + File.separator + "temp" + File.separator + serverConnection.getId() + File.separator;
    tempFile = tempPath + "clientTemp.txt";
    tempByteBuffer = new ByteArrayOutputStream();
    List<SQLExpr> columns = statement.getColumns();
    if (tableConfig != null) {
        String pColumn = getPartitionColumn();
        if (pColumn != null && columns != null && columns.size() > 0) {
            for (int i = 0, columnsSize = columns.size(); i < columnsSize; i++) {
                String column = StringUtil.removeBackquote(columns.get(i).toString());
                if (pColumn.equalsIgnoreCase(column)) {
                    partitionColumnIndex = i;
                }
                if ("_slot".equalsIgnoreCase(column)) {
                    shoudAddSlot = false;
                }
            }
        }
    }
    if (shoudAddSlot) {
        columns.add(new SQLIdentifierExpr("_slot"));
    }
    parseLoadDataPram();
    if (statement.isLocal()) {
        isStartLoadData = true;
        //向客户端请求发送文件
        ByteBuffer buffer = serverConnection.allocate();
        RequestFilePacket filePacket = new RequestFilePacket();
        filePacket.fileName = fileName.getBytes();
        filePacket.packetId = 1;
        filePacket.write(buffer, serverConnection, true);
    } else {
        if (!new File(fileName).exists()) {
            serverConnection.writeErrMessage(ErrorCode.ER_FILE_NOT_FOUND, fileName + " is not found!");
            clear();
        } else {
            parseFileByLine(fileName, loadData.getCharset(), loadData.getLineTerminatedBy());
            RouteResultset rrs = buildResultSet(routeResultMap);
            if (rrs != null) {
                flushDataToFile();
                isStartLoadData = false;
                serverConnection.getSession2().execute(rrs, ServerParse.LOAD_DATA_INFILE_SQL);
            }
        }
    }
}
Also used : RequestFilePacket(io.mycat.net.mysql.RequestFilePacket) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) ByteBuffer(java.nio.ByteBuffer) SlotFunction(io.mycat.route.function.SlotFunction) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 RequestFilePacket (io.mycat.net.mysql.RequestFilePacket)1 RouteResultset (io.mycat.route.RouteResultset)1 SlotFunction (io.mycat.route.function.SlotFunction)1 MycatStatementParser (io.mycat.route.parser.druid.MycatStatementParser)1 ByteBuffer (java.nio.ByteBuffer)1