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;
}
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());
}
Aggregations