Search in sources :

Example 1 with MySqlLoadDataInFileStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement in project druid by alibaba.

the class MySqlStatementParser method parseLoadDataInFile.

protected MySqlLoadDataInFileStatement parseLoadDataInFile() {
    acceptIdentifier("DATA");
    MySqlLoadDataInFileStatement stmt = new MySqlLoadDataInFileStatement();
    if (identifierEquals(LOW_PRIORITY)) {
        stmt.setLowPriority(true);
        lexer.nextToken();
    }
    if (identifierEquals("CONCURRENT")) {
        stmt.setConcurrent(true);
        lexer.nextToken();
    }
    if (identifierEquals(LOCAL)) {
        stmt.setLocal(true);
        lexer.nextToken();
    }
    acceptIdentifier("INFILE");
    SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr();
    stmt.setFileName(fileName);
    if (lexer.token() == Token.REPLACE) {
        stmt.setReplicate(true);
        lexer.nextToken();
    }
    if (identifierEquals(IGNORE)) {
        stmt.setIgnore(true);
        lexer.nextToken();
    }
    accept(Token.INTO);
    accept(Token.TABLE);
    SQLName tableName = exprParser.name();
    stmt.setTableName(tableName);
    if (identifierEquals(CHARACTER)) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() != Token.LITERAL_CHARS) {
            throw new ParserException("syntax error, illegal charset");
        }
        String charset = lexer.stringVal();
        lexer.nextToken();
        stmt.setCharset(charset);
    }
    if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) {
        lexer.nextToken();
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("OPTIONALLY")) {
            stmt.setColumnsEnclosedOptionally(true);
            lexer.nextToken();
        }
        if (identifierEquals("ENCLOSED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEnclosedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("ESCAPED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEscaped(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals("LINES")) {
        lexer.nextToken();
        if (identifierEquals("STARTING")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesStartingBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals(IGNORE)) {
        lexer.nextToken();
        stmt.setIgnoreLinesNumber(this.exprParser.expr());
        acceptIdentifier("LINES");
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.SET) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getSetList(), stmt);
    }
    return stmt;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlLoadDataInFileStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement)

Example 2 with MySqlLoadDataInFileStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement in project druid by alibaba.

the class LoadDataSQLParserTest method test_load_data.

public void test_load_data() throws Exception {
    String sql = "load DATA  local INFILE '/opt/test.txt' IGNORE INTO TABLE test  CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'  ESCAPED BY  '\\\\'   LINES TERMINATED BY '\r\n' (id,sid,asf) ";
    SQLStatementParser parser = new MySqlStatementParser(sql);
    // 
    MySqlLoadDataInFileStatement stmt = (MySqlLoadDataInFileStatement) parser.parseStatement();
    Assert.assertEquals("utf8", stmt.getCharset());
    Assert.assertEquals(",", ((SQLCharExpr) stmt.getColumnsTerminatedBy()).getText());
    Assert.assertEquals("\"", ((SQLCharExpr) stmt.getColumnsEnclosedBy()).getText());
    Assert.assertEquals("\r\n", ((SQLCharExpr) stmt.getLinesTerminatedBy()).getText());
    Assert.assertEquals("\\", ((SQLCharExpr) stmt.getColumnsEscaped()).getText());
}
Also used : MySqlLoadDataInFileStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Aggregations

MySqlLoadDataInFileStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1