Search in sources :

Example 16 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ServerLoadDataInfileHandler method buildResultSet.

private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
    statement.setLocal(true);
    // druid will filter path, reset it now
    SQLLiteralExpr fn = new SQLCharExpr(fileName);
    statement.setFileName(fn);
    // replace IGNORE X LINES in SQL to avoid  IGNORING X LINE in every node.
    String srcStatement = this.ignoreLinesDelete(statement.toString());
    RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
    rrs.setLoadData(true);
    rrs.setStatement(srcStatement);
    rrs.setFinishedRoute(true);
    int size = routeMap.size();
    RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
    int index = 0;
    for (Map.Entry<String, LoadData> entry : routeMap.entrySet()) {
        RouteResultsetNode rrNode = new RouteResultsetNode(entry.getKey(), ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
        rrNode.setStatement(srcStatement);
        LoadData newLoadData = new LoadData();
        ObjectUtil.copyProperties(loadData, newLoadData);
        newLoadData.setLocal(true);
        LoadData loadData1 = entry.getValue();
        if (loadData1.getFileName() != null) {
            newLoadData.setFileName(loadData1.getFileName());
        } else {
            newLoadData.setData(loadData1.getData());
        }
        rrNode.setLoadData(newLoadData);
        routeResultsetNodes[index] = rrNode;
        index++;
    }
    rrs.setNodes(routeResultsetNodes);
    return rrs;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) LoadData(com.actiontech.dble.sqlengine.mpp.LoadData) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 17 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ServerLoadDataInfileHandler method end.

@Override
public void end(byte packId) {
    isStartLoadData = false;
    this.packID = packId;
    // empty packet for end
    saveByteOrToFile(null, true);
    List<SQLExpr> columns = statement.getColumns();
    String tableSimpleName = statement.getTableName().getSimpleName();
    if (isHasStoreToFile) {
        parseFileByLine(tempFile, loadData.getCharset(), loadData.getLineTerminatedBy());
    } else {
        String content = new String(tempByteBuffer.toByteArray(), Charset.forName(loadData.getCharset()));
        // List<String> lines = Splitter.on(loadData.getLineTerminatedBy()).omitEmptyStrings().splitToList(content);
        CsvParserSettings settings = new CsvParserSettings();
        settings.setMaxColumns(65535);
        settings.setMaxCharsPerColumn(65535);
        settings.getFormat().setLineSeparator(loadData.getLineTerminatedBy());
        settings.getFormat().setDelimiter(loadData.getFieldTerminatedBy().charAt(0));
        if (loadData.getEnclose() != null) {
            settings.getFormat().setQuote(loadData.getEnclose().charAt(0));
        }
        if (loadData.getEscape() != null) {
            settings.getFormat().setQuoteEscape(loadData.getEscape().charAt(0));
        }
        settings.getFormat().setNormalizedNewline(loadData.getLineTerminatedBy().charAt(0));
        /*
             *  fix bug #1074 : LOAD DATA local INFILE导入的所有Boolean类型全部变成了false
             *  不可见字符将在CsvParser被当成whitespace过滤掉, 使用settings.trimValues(false)来避免被过滤掉
             *  FIXME : 设置trimValues(false)之后, 会引起字段值前后的空白字符无法被过滤!
             */
        settings.trimValues(false);
        CsvParser parser = new CsvParser(settings);
        try {
            parser.beginParsing(new StringReader(content));
            String[] row = null;
            int ignoreNumber = 0;
            if (statement.getIgnoreLinesNumber() != null && !"".equals(statement.getIgnoreLinesNumber().toString())) {
                ignoreNumber = Integer.parseInt(statement.getIgnoreLinesNumber().toString());
            }
            while ((row = parser.parseNext()) != null) {
                if (ignoreNumber == 0) {
                    parseOneLine(columns, tableSimpleName, row, true, loadData.getLineTerminatedBy());
                } else {
                    ignoreNumber--;
                }
            }
        } finally {
            parser.stopParsing();
        }
    }
    RouteResultset rrs = buildResultSet(routeResultMap);
    if (rrs != null) {
        flushDataToFile();
        serverConnection.getSession2().execute(rrs);
    }
// sendOk(++packID);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) CsvParser(com.univocity.parsers.csv.CsvParser) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 18 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ServerLoadDataInfileHandler method parseOneLine.

private void parseOneLine(List<SQLExpr> columns, String table, String[] line, boolean toFile, String lineEnd) {
    if (loadData.getEnclose() != null && loadData.getEnclose().charAt(0) > 0x0020) {
        for (int i = 0; i < line.length; i++) {
            line[i] = line[i].trim();
        }
    }
    RouteResultset rrs = tryDirectRoute(sql, line);
    if (rrs == null || rrs.getNodes() == null || rrs.getNodes().length == 0) {
        String insertSql = makeSimpleInsert(columns, line, table);
        rrs = serverConnection.routeSQL(insertSql, ServerParse.INSERT);
    }
    if (rrs == null || rrs.getNodes() == null || rrs.getNodes().length == 0) {
    // do nothing
    } else {
        for (RouteResultsetNode routeResultsetNode : rrs.getNodes()) {
            String name = routeResultsetNode.getName();
            LoadData data = routeResultMap.get(name);
            if (data == null) {
                data = new LoadData();
                data.setCharset(loadData.getCharset());
                data.setEnclose(loadData.getEnclose());
                data.setFieldTerminatedBy(loadData.getFieldTerminatedBy());
                data.setLineTerminatedBy(loadData.getLineTerminatedBy());
                data.setEscape(loadData.getEscape());
                routeResultMap.put(name, data);
            }
            String jLine = joinField(line, data);
            if (data.getData() == null) {
                data.setData(Lists.newArrayList(jLine));
            } else {
                data.getData().add(jLine);
            }
            if (toFile && data.getData().size() > 10000) {
                // avoid OOM
                saveDataToFile(data, name);
            }
        }
    }
}
Also used : LoadData(com.actiontech.dble.sqlengine.mpp.LoadData) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 19 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ServerConnection method routeSQL.

public RouteResultset routeSQL(String sql, int type) {
    String db = this.schema;
    if (db == null) {
        writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "No Database selected");
        return null;
    }
    SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(db);
    if (schema == null) {
        writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown Database '" + db + "'");
        return null;
    }
    RouteResultset rrs;
    try {
        rrs = DbleServer.getInstance().getRouterService().route(schema, type, sql, this);
    } catch (Exception e) {
        executeException(e, sql);
        return null;
    }
    return rrs;
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 20 with RouteResultset

use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.

the class ServerConnection method routeEndExecuteSQL.

private void routeEndExecuteSQL(String sql, int type, SchemaConfig schema) {
    RouteResultset rrs;
    try {
        rrs = DbleServer.getInstance().getRouterService().route(schema, type, sql, this);
        if (rrs == null) {
            return;
        }
        if (rrs.getSqlType() == ServerParse.DDL) {
            addTableMetaLock(rrs);
            if (DbleServer.getInstance().getTmManager().getCatalogs().get(rrs.getSchema()).getView(rrs.getTable()) != null) {
                DbleServer.getInstance().getTmManager().removeMetaLock(rrs.getSchema(), rrs.getTable());
                String msg = "Table '" + rrs.getTable() + "' already exists as a view";
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            }
        }
    } catch (Exception e) {
        executeException(e, sql);
        return;
    }
    session.endRoute(rrs);
    session.execute(rrs);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RouteResultset(com.actiontech.dble.route.RouteResultset)

Aggregations

RouteResultset (com.actiontech.dble.route.RouteResultset)22 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)8 SQLNonTransientException (java.sql.SQLNonTransientException)8 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)6 TableConfig (com.actiontech.dble.config.model.TableConfig)4 SQLException (java.sql.SQLException)4 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)3 IOException (java.io.IOException)3 FetchStoreNodeOfChildTableHandler (com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler)2 DruidShardingParseInfo (com.actiontech.dble.route.parser.druid.DruidShardingParseInfo)2 LoadData (com.actiontech.dble.sqlengine.mpp.LoadData)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 ByteBuffer (java.nio.ByteBuffer)2 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)1 ShowTablesHandler (com.actiontech.dble.backend.mysql.nio.handler.ShowTablesHandler)1 ShowVariablesHandler (com.actiontech.dble.backend.mysql.nio.handler.ShowVariablesHandler)1 SingleNodeHandler (com.actiontech.dble.backend.mysql.nio.handler.SingleNodeHandler)1 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)1 ServerConfig (com.actiontech.dble.config.ServerConfig)1