Java Code Examples for com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: dble   Source File: DbleOutputVisitor.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void columnsParameter(MySqlLoadDataInFileStatement x) {
    if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) {
        print0(ucase ? " COLUMNS" : " columns");
        if (x.getColumnsTerminatedBy() != null) {
            print0(ucase ? " TERMINATED BY " : " terminated by ");
            x.getColumnsTerminatedBy().accept(this);
        }

        if (x.getColumnsEnclosedBy() != null) {
            if (x.isColumnsEnclosedOptionally()) {
                print0(ucase ? " OPTIONALLY" : " optionally");
            }
            print0(ucase ? " ENCLOSED BY " : " enclosed by ");
            x.getColumnsEnclosedBy().accept(this);
        }

        if (x.getColumnsEscaped() != null) {
            print0(ucase ? " ESCAPED BY " : " escaped by ");
            x.getColumnsEscaped().accept(this);
        }
    }
}
 
Example 2
Source Project: Mycat2   Source File: HintDataNodeHandler.java    License: GNU General Public License v3.0 5 votes vote down vote up
private LoadData parseLoadDataPram(String sql , String connectionCharset)
{
	SQLStatementParser parser = new MycatStatementParser(sql);
	MySqlLoadDataInFileStatement statement = (MySqlLoadDataInFileStatement) parser.parseStatement();

	LoadData loadData = new LoadData();
	SQLTextLiteralExpr rawLineEnd = (SQLTextLiteralExpr) statement.getLinesTerminatedBy();
	String lineTerminatedBy = rawLineEnd == null ? "\n" : rawLineEnd.getText();
	loadData.setLineTerminatedBy(lineTerminatedBy);

	SQLTextLiteralExpr rawFieldEnd = (SQLTextLiteralExpr) statement.getColumnsTerminatedBy();
	String fieldTerminatedBy = rawFieldEnd == null ? "\t" : rawFieldEnd.getText();
	loadData.setFieldTerminatedBy(fieldTerminatedBy);

	SQLTextLiteralExpr rawEnclosed = (SQLTextLiteralExpr) statement.getColumnsEnclosedBy();
	String enclose = rawEnclosed == null ? null : rawEnclosed.getText();
	loadData.setEnclose(enclose);

	SQLTextLiteralExpr escapseExpr =  (SQLTextLiteralExpr)statement.getColumnsEscaped() ;
	String escapse=escapseExpr==null?"\\":escapseExpr.getText();
	loadData.setEscape(escapse);
	String charset = statement.getCharset() != null ? statement.getCharset() : connectionCharset;
	loadData.setCharset(charset);

	String fileName = parseFileName(sql);
	if(StringUtils.isBlank(fileName)){
		throw new RuntimeException(" file name is null !");
	}

	loadData.setFileName(fileName);

	return loadData ;
}
 
Example 3
Source Project: dble   Source File: DbleOutputVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void linesParameter(MySqlLoadDataInFileStatement x) {
    if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) {
        print0(ucase ? " LINES" : " lines");
        if (x.getLinesStartingBy() != null) {
            print0(ucase ? " STARTING BY " : " starting by ");
            x.getLinesStartingBy().accept(this);
        }

        if (x.getLinesTerminatedBy() != null) {
            print0(ucase ? " TERMINATED BY " : " terminated by ");
            x.getLinesTerminatedBy().accept(this);
        }
    }
}
 
Example 4
@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());
    if (schema == null){
        throw new RuntimeException("please sql:use schema  before load data");
    }
    tableId2DataNodeCache = (LayerCachePool) MycatServer.getInstance().getCacheService().getCachePool("TableID2DataNodeCache");
    tableName = statement.getTableName().getSimpleName().toUpperCase();
    tableConfig = schema.getTables().get(tableName);
  if(  tableConfig.getRule() != null && 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);
            }

        }
    }
}
 
Example 5
Source Project: Mycat2   Source File: LoadDataOutputVisitor.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean visit(MySqlLoadDataInFileStatement x) {
    print("LOAD DATA ");

    if (x.isLowPriority()) {
        print("LOW_PRIORITY ");
    }

    if (x.isConcurrent()) {
        print("CONCURRENT ");
    }

    if (x.isLocal()) {
        print("LOCAL ");
    }

    print("INFILE ");

    x.getFileName().accept(this);

    if (x.isReplicate()) {
        print(" REPLACE ");
    }

    if (x.isIgnore()) {
        print(" IGNORE ");
    }

    print(" INTO TABLE ");
    x.getTableName().accept(this);
    if(x.getCharset()!=null)
    {
        print(" CHARACTER SET ");
        print("'"+x.getCharset()+"'");
    }

    if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) {
        print(" COLUMNS");
        if (x.getColumnsTerminatedBy() != null) {
            print(" TERMINATED BY ");
            x.getColumnsTerminatedBy().accept(this);
        }

        if (x.getColumnsEnclosedBy() != null) {
            if (x.isColumnsEnclosedOptionally()) {
                print(" OPTIONALLY");
            }
            print(" ENCLOSED BY ");
            x.getColumnsEnclosedBy().accept(this);
        }

        if (x.getColumnsEscaped() != null) {
            print(" ESCAPED BY ");
            x.getColumnsEscaped().accept(this);
        }
    }

    if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) {
        print(" LINES");
        if (x.getLinesStartingBy() != null) {
            print(" STARTING BY ");
            x.getLinesStartingBy().accept(this);
        }

        if (x.getLinesTerminatedBy() != null) {
            print(" TERMINATED BY ");
            x.getLinesTerminatedBy().accept(this);
        }
    }

    if(x.getIgnoreLinesNumber() != null) {
        print(" IGNORE ");
        x.getIgnoreLinesNumber().accept(this);
        print(" LINES");
    }

    if (x.getColumns().size() != 0) {
        print(" (");
        printAndAccept(x.getColumns(), ", ");
        print(")");
    }

    if (x.getSetList().size() != 0) {
        print(" SET ");
        printAndAccept(x.getSetList(), ", ");
    }

    return false;
}
 
Example 6
Source Project: dble   Source File: DbleOutputVisitor.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean visit(MySqlLoadDataInFileStatement x) {
    print0(ucase ? "LOAD DATA " : "load data ");

    if (x.isLowPriority()) {
        print0(ucase ? "LOW_PRIORITY " : "low_priority ");
    }

    if (x.isConcurrent()) {
        print0(ucase ? "CONCURRENT " : "concurrent ");
    }

    if (x.isLocal()) {
        print0(ucase ? "LOCAL " : "local ");
    }

    print0(ucase ? "INFILE " : "infile ");

    x.getFileName().accept(this);

    if (x.isReplicate()) {
        print0(ucase ? " REPLACE " : " replace ");
    }

    if (x.isIgnore()) {
        print0(ucase ? " IGNORE " : " ignore ");
    }

    print0(ucase ? " INTO TABLE " : " into table ");
    x.getTableName().accept(this);

    if (x.getCharset() != null && !"".equals(x.getCharset())) {
        print0(ucase ? " CHARACTER SET " + x.getCharset().toUpperCase() + " " : " character set " + x.getCharset() + " ");
    }
    columnsParameter(x);
    linesParameter(x);

    if (x.getColumns().size() != 0) {
        print0(" (");
        printAndAccept(x.getColumns(), ", ");
        print(')');
    }

    if (x.getSetList().size() != 0) {
        print0(ucase ? " SET " : " set ");
        printAndAccept(x.getSetList(), ", ");
    }

    return false;
}