use of com.alibaba.druid.sql.parser.ParserException 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.parser.ParserException in project druid by alibaba.
the class MySqlStatementParser method parseProcedureStatementList.
/**
* parse procedure statement block
*/
private void parseProcedureStatementList(List<SQLStatement> statementList, int max) {
for (; ; ) {
if (max != -1) {
if (statementList.size() >= max) {
return;
}
}
if (lexer.token() == Token.EOF) {
return;
}
if (lexer.token() == Token.END) {
return;
}
if (lexer.token() == Token.ELSE) {
return;
}
if (lexer.token() == (Token.SEMI)) {
lexer.nextToken();
continue;
}
if (lexer.token() == Token.WHEN) {
return;
}
if (lexer.token() == Token.UNTIL) {
return;
}
// select into
if (lexer.token() == (Token.SELECT)) {
statementList.add(this.parseSelectInto());
continue;
}
// update
if (lexer.token() == (Token.UPDATE)) {
statementList.add(parseUpdateStatement());
continue;
}
// create
if (lexer.token() == (Token.CREATE)) {
statementList.add(parseCreate());
continue;
}
// insert
if (lexer.token() == Token.INSERT) {
statementList.add(parseInsert());
continue;
}
// delete
if (lexer.token() == (Token.DELETE)) {
statementList.add(parseDeleteStatement());
continue;
}
// call
if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) {
statementList.add(this.parseCall());
continue;
}
// begin
if (lexer.token() == Token.BEGIN) {
statementList.add(this.parseBlock());
continue;
}
if (lexer.token() == Token.VARIANT) {
SQLExpr variant = this.exprParser.primary();
if (variant instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant;
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) {
SQLSetStatement stmt = new SQLSetStatement(binaryOpExpr.getLeft(), binaryOpExpr.getRight(), getDbType());
statementList.add(stmt);
continue;
}
}
accept(Token.COLONEQ);
SQLExpr value = this.exprParser.expr();
SQLSetStatement stmt = new SQLSetStatement(variant, value, getDbType());
statementList.add(stmt);
continue;
}
// select
if (lexer.token() == Token.LPAREN) {
char ch = lexer.current();
int bp = lexer.bp();
lexer.nextToken();
if (lexer.token() == Token.SELECT) {
lexer.reset(bp, ch, Token.LPAREN);
statementList.add(this.parseSelect());
continue;
} else {
throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
}
// assign statement
if (lexer.token() == Token.SET) {
statementList.add(this.parseAssign());
continue;
}
// while statement
if (lexer.token() == Token.WHILE) {
statementList.add(this.parseWhile());
continue;
}
// loop statement
if (lexer.token() == Token.LOOP) {
statementList.add(this.parseLoop());
continue;
}
// if statement
if (lexer.token() == Token.IF) {
statementList.add(this.parseIf());
continue;
}
// case statement
if (lexer.token() == Token.CASE) {
statementList.add(this.parseCase());
continue;
}
// declare statement
if (lexer.token() == Token.DECLARE) {
char markChar = lexer.current();
int markBp = lexer.bp();
lexer.nextToken();
lexer.nextToken();
if (// cursor declare statement
lexer.token() == Token.CURSOR) {
lexer.reset(markBp, markChar, Token.DECLARE);
statementList.add(this.parseCursorDeclare());
} else if (identifierEquals("HANDLER")) {
//DECLARE异常处理程序 [add by zhujun 2016-04-16]
lexer.reset(markBp, markChar, Token.DECLARE);
statementList.add(this.parseDeclareHandler());
} else if (lexer.token() == Token.CONDITION) {
//DECLARE异常 [add by zhujun 2016-04-17]
lexer.reset(markBp, markChar, Token.DECLARE);
statementList.add(this.parseDeclareCondition());
} else {
lexer.reset(markBp, markChar, Token.DECLARE);
statementList.add(this.parseDeclare());
}
continue;
}
// leave statement
if (lexer.token() == Token.LEAVE) {
statementList.add(this.parseLeave());
continue;
}
// iterate statement
if (lexer.token() == Token.ITERATE) {
statementList.add(this.parseIterate());
continue;
}
// repeat statement
if (lexer.token() == Token.REPEAT) {
statementList.add(this.parseRepeat());
continue;
}
// open cursor
if (lexer.token() == Token.OPEN) {
statementList.add(this.parseOpen());
continue;
}
// close cursor
if (lexer.token() == Token.CLOSE) {
statementList.add(this.parseClose());
continue;
}
// fetch cursor into
if (lexer.token() == Token.FETCH) {
statementList.add(this.parseFetch());
continue;
}
if (lexer.token() == Token.IDENTIFIER) {
String label = lexer.stringVal();
char ch = lexer.current();
int bp = lexer.bp();
lexer.nextToken();
if (lexer.token() == Token.VARIANT && lexer.stringVal().equals(":")) {
lexer.nextToken();
if (lexer.token() == Token.LOOP) {
// parse loop statement
statementList.add(this.parseLoop(label));
} else if (lexer.token() == Token.WHILE) {
// parse while statement with label
statementList.add(this.parseWhile(label));
} else if (lexer.token() == Token.BEGIN) {
// parse begin-end statement with label
statementList.add(this.parseBlock(label));
} else if (lexer.token() == Token.REPEAT) {
// parse repeat statement with label
statementList.add(this.parseRepeat(label));
}
continue;
} else {
lexer.reset(bp, ch, Token.IDENTIFIER);
}
}
throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class MySqlStatementParser method parseDeleteStatement.
public MySqlDeleteStatement parseDeleteStatement() {
MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement();
if (lexer.token() == Token.DELETE) {
lexer.nextToken();
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == Token.HINT) {
this.getExprParser().parseHints(deleteStatement.getHints());
}
if (identifierEquals(LOW_PRIORITY)) {
deleteStatement.setLowPriority(true);
lexer.nextToken();
}
if (identifierEquals("QUICK")) {
deleteStatement.setQuick(true);
lexer.nextToken();
}
if (identifierEquals(IGNORE)) {
deleteStatement.setIgnore(true);
lexer.nextToken();
}
if (lexer.token() == Token.IDENTIFIER) {
deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
if (lexer.token() == Token.FROM) {
lexer.nextToken();
SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
deleteStatement.setFrom(tableSource);
}
} else if (lexer.token() == Token.FROM) {
lexer.nextToken();
deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
} else {
throw new ParserException("syntax error");
}
if (identifierEquals("USING")) {
lexer.nextToken();
SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
deleteStatement.setUsing(tableSource);
}
}
if (lexer.token() == (Token.WHERE)) {
lexer.nextToken();
SQLExpr where = this.exprParser.expr();
deleteStatement.setWhere(where);
}
if (lexer.token() == (Token.ORDER)) {
SQLOrderBy orderBy = exprParser.parseOrderBy();
deleteStatement.setOrderBy(orderBy);
}
deleteStatement.setLimit(this.exprParser.parseLimit());
return deleteStatement;
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class MySqlStatementParser method parseDeclare.
/**
* parse declare statement
*/
public MySqlDeclareStatement parseDeclare() {
MySqlDeclareStatement stmt = new MySqlDeclareStatement();
accept(Token.DECLARE);
// lexer.nextToken();
for (; ; ) {
SQLDeclareItem item = new SQLDeclareItem();
item.setName(exprParser.name());
stmt.addVar(item);
if (lexer.token() == Token.COMMA) {
accept(Token.COMMA);
continue;
} else if (lexer.token() != Token.EOF) {
// var type
item.setDataType(exprParser.parseDataType());
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
SQLExpr defaultValue = this.exprParser.primary();
item.setValue(defaultValue);
}
break;
} else {
throw new ParserException("TODO");
}
}
return stmt;
}
use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.
the class MySqlExprParser method primary.
public SQLExpr primary() {
final Token tok = lexer.token();
if (identifierEquals("outfile")) {
lexer.nextToken();
SQLExpr file = primary();
SQLExpr expr = new MySqlOutFileExpr(file);
return primaryRest(expr);
}
switch(tok) {
case LITERAL_ALIAS:
String aliasValue = lexer.stringVal();
lexer.nextToken();
return primaryRest(new SQLCharExpr(aliasValue));
case VARIANT:
SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal());
lexer.nextToken();
if (varRefExpr.getName().equalsIgnoreCase("@@global")) {
accept(Token.DOT);
varRefExpr = new SQLVariantRefExpr(lexer.stringVal(), true);
lexer.nextToken();
} else if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) {
varRefExpr.setName("@'" + lexer.stringVal() + "'");
lexer.nextToken();
} else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) {
varRefExpr.setName("@@'" + lexer.stringVal() + "'");
lexer.nextToken();
}
return primaryRest(varRefExpr);
case VALUES:
lexer.nextToken();
if (lexer.token() != Token.LPAREN) {
throw new ParserException("syntax error, illegal values clause");
}
return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
case BINARY:
lexer.nextToken();
if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI || lexer.token() == Token.EOF) {
return new SQLIdentifierExpr("BINARY");
} else {
SQLUnaryExpr binaryExpr = new SQLUnaryExpr(SQLUnaryOperator.BINARY, expr());
return primaryRest(binaryExpr);
}
case CACHE:
case GROUP:
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr(lexer.stringVal()));
default:
return super.primary();
}
}
Aggregations