Java Code Examples for com.alibaba.druid.sql.parser.Token

The following examples show how to use com.alibaba.druid.sql.parser.Token. 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: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void parseFrom(SQLSelectQueryBlock queryBlock) {
    if (lexer.token() != Token.FROM) {
        return;
    }

    lexer.nextTokenIdent();

    if (lexer.token() == Token.UPDATE) { // taobao returning to urgly syntax
        updateStmt = this.parseUpdateStatment();
        List<SQLExpr> returnning = updateStmt.getReturning();
        for (SQLSelectItem item : queryBlock.getSelectList()) {
            SQLExpr itemExpr = item.getExpr();
            itemExpr.setParent(updateStmt);
            returnning.add(itemExpr);
        }
        returningFlag = true;
        return;
    }

    queryBlock.setFrom(parseTableSource());
}
 
Example 2
Source Project: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
    if (lexer.identifierEquals(FnvHash.Constants.USING)) {
        return tableSource;
    }

    parseIndexHintList(tableSource);

    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        this.exprParser.names(((SQLExprTableSource) tableSource).getPartitions(), tableSource);
        accept(Token.RPAREN);
    }

    return super.parseTableSourceRest(tableSource);
}
 
Example 3
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 6 votes vote down vote up
protected SQLExpr bracketRest(SQLExpr expr) {
    Number index;

    if (lexer.token() == Token.LITERAL_INT) {
        index = lexer.integerValue();
        lexer.nextToken();
    } else {
        throw new ParserException("error : " + lexer.stringVal());
    }

    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
        methodInvokeExpr.getParameters().add(new SQLIntegerExpr(index));
    }
    lexer.nextToken();
    expr = primaryRest(expr);
    return expr;
}
 
Example 4
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 6 votes vote down vote up
protected SQLExpr parseExtract() {
    SQLExpr expr;
    if (lexer.token() != Token.IDENTIFIER) {
        throw new ParserException("syntax error. " + lexer.info());
    }

    String unitVal = lexer.stringVal();
    SQLIntervalUnit unit = SQLIntervalUnit.valueOf(unitVal.toUpperCase());
    lexer.nextToken();

    accept(Token.FROM);

    SQLExpr value = expr();

    MySqlExtractExpr extract = new MySqlExtractExpr();
    extract.setValue(value);
    extract.setUnit(unit);
    accept(Token.RPAREN);

    expr = extract;

    return primaryRest(expr);
}
 
Example 5
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 6 votes vote down vote up
public SQLColumnDefinition parseColumn() {
    SQLColumnDefinition column = new SQLColumnDefinition();
    column.setDbType(dbType);
    column.setName(name());
    column.setDataType(parseDataType());

    if (lexer.identifierEquals(FnvHash.Constants.GENERATED)) {
        lexer.nextToken();
        acceptIdentifier("ALWAYS");
        accept(Token.AS);
        accept(Token.LPAREN);
        SQLExpr expr = this.expr();
        accept(Token.RPAREN);
        column.setGeneratedAlawsAs(expr);
    }

    return parseColumnRest(column);
}
 
Example 6
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 6 votes vote down vote up
public SQLName nameRest(SQLName name) {
    if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
        lexer.nextToken();
        MySqlUserName userName = new MySqlUserName();
        userName.setUserName(((SQLIdentifierExpr) name).getName());

        if (lexer.token() == Token.LITERAL_CHARS) {
            userName.setHost("'" + lexer.stringVal() + "'");
        } else {
            userName.setHost(lexer.stringVal());
        }
        lexer.nextToken();

        if (lexer.token() == Token.IDENTIFIED) {
            lexer.nextToken();
            accept(Token.BY);
            userName.setIdentifiedBy(lexer.stringVal());
            lexer.nextToken();
        }

        return userName;
    }
    return super.nameRest(name);
}
 
Example 7
Source Project: dble   Source File: DbleCreateTableParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
private boolean parseTableOptionCharsetOrCollate(MySqlCreateTableStatement stmt) {
    if (lexer.identifierEquals("CHARACTER")) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
        stmt.getTableOptions().put("CHARACTER SET", this.exprParser.expr());
        return true;
    }

    if (lexer.identifierEquals("CHARSET")) {
        lexer.nextToken();
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
        stmt.getTableOptions().put("CHARSET", this.exprParser.expr());
        return true;
    }

    if (lexer.identifierEquals("COLLATE")) {
        lexer.nextToken();
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
        stmt.getTableOptions().put("COLLATE", this.exprParser.expr());
        return true;
    }

    return false;
}
 
Example 8
Source Project: Zebra   Source File: SQLParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean handle(Token lastToken, String comment) {
	if (comment.contains("zebra")) {
		zebraHintComment = comment;
	} else {
		otherHintComments.add(comment);
	}

	return false;
}
 
Example 9
Source Project: elasticsearch-sql   Source File: ESActionFactory.java    License: Apache License 2.0 5 votes vote down vote up
private static SQLExpr toSqlExpr(String sql) {
    SQLExprParser parser = new ElasticSqlExprParser(sql); //zhongshu-comment 这个SQLExprParser parser应该就是语法解析器
    SQLExpr expr = parser.expr(); //zhongshu-comment 这个expr应该就是解析sql之后得到的AST了

    //zhongshu-comment 调用parser.expr()方法解析完sql语句后,发现最后一个token不是End Of File的话,即该sql语句貌似是残缺的,可能是用户输入了一个未结束的sql
    if (parser.getLexer().token() != Token.EOF) {
        throw new ParserException("illegal sql expr : " + sql);
    }

    return expr;
}
 
Example 10
Source Project: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 5 votes vote down vote up
protected SQLTableSource primaryTableSourceRest(SQLTableSource tableSource) {
    parseIndexHintList(tableSource);

    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        this.exprParser.names(((SQLExprTableSource) tableSource).getPartitions(), tableSource);
        accept(Token.RPAREN);
    }

    return tableSource;
}
 
Example 11
Source Project: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 5 votes vote down vote up
private void parseIndexHint(MySqlIndexHintImpl hint) {
    if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
    } else {
        accept(Token.KEY);
    }

    if (lexer.token() == Token.FOR) {
        lexer.nextToken();

        if (lexer.token() == Token.JOIN) {
            lexer.nextToken();
            hint.setOption(MySqlIndexHint.Option.JOIN);
        } else if (lexer.token() == Token.ORDER) {
            lexer.nextToken();
            accept(Token.BY);
            hint.setOption(MySqlIndexHint.Option.ORDER_BY);
        } else {
            accept(Token.GROUP);
            accept(Token.BY);
            hint.setOption(MySqlIndexHint.Option.GROUP_BY);
        }
    }

    accept(Token.LPAREN);
    if (lexer.token() == Token.PRIMARY) {
        lexer.nextToken();
        hint.getIndexList().add(new SQLIdentifierExpr("PRIMARY"));
    } else {
        this.exprParser.names(hint.getIndexList());
    }
    accept(Token.RPAREN);
}
 
Example 12
Source Project: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SQLUnionQuery unionRest(SQLUnionQuery union) {
    if (lexer.token() == Token.LIMIT) {
        union.setLimit(this.exprParser.parseLimit());
    }
    return super.unionRest(union);
}
 
Example 13
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public void parseHints(List hints) {
    while (lexer.token() == Token.HINT) {
        SQLCommentHint hint = new SQLCommentHint(lexer.stringVal());

        if (lexer.getCommentCount() > 0) {
            hint.addBeforeComment(lexer.readAndResetComments());
        }

        hints.add(hint);
        lexer.nextToken();
    }
}
 
Example 14
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 5 votes vote down vote up
public SQLName userName() {
    SQLName name = this.name();
    if (lexer.token() == Token.LPAREN && name.hashCode64() == FnvHash.Constants.CURRENT_USER) {
        lexer.nextToken();
        accept(Token.RPAREN);
        return name;
    }

    return (SQLName) userNameRest(name);
}
 
Example 15
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 5 votes vote down vote up
protected SQLExpr parsePosition() {

        SQLExpr subStr = this.primary();
        accept(Token.IN);
        SQLExpr str = this.expr();
        accept(Token.RPAREN);

        SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
        locate.addParameter(subStr);
        locate.addParameter(str);

        return primaryRest(locate);
    }
 
Example 16
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 5 votes vote down vote up
protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr) {
    if (lexer.token() == Token.ORDER) {
        SQLOrderBy orderBy = this.parseOrderBy();
        aggregateExpr.putAttribute("ORDER BY", orderBy);
    }
    if (lexer.identifierEquals(FnvHash.Constants.SEPARATOR)) {
        lexer.nextToken();

        SQLExpr seperator = this.primary();
        seperator.setParent(aggregateExpr);

        aggregateExpr.putAttribute("SEPARATOR", seperator);
    }
    return aggregateExpr;
}
 
Example 17
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 5 votes vote down vote up
public MySqlOrderingExpr parseSelectGroupByItem() {
    MySqlOrderingExpr item = new MySqlOrderingExpr();

    item.setExpr(expr());

    if (lexer.token() == Token.ASC) {
        lexer.nextToken();
        item.setType(SQLOrderingSpecification.ASC);
    } else if (lexer.token() == Token.DESC) {
        lexer.nextToken();
        item.setType(SQLOrderingSpecification.DESC);
    }

    return item;
}
 
Example 18
Source Project: Mycat2   Source File: MycatSelectParser.java    License: GNU General Public License v3.0 4 votes vote down vote up
public void parseTop()
{
    if (lexer.token() == Token.TOP)
    {
        lexer.nextToken();

        boolean paren = false;
        if (lexer.token() == Token.LPAREN)
        {
            paren = true;
            lexer.nextToken();
        }

        if (paren)
        {
            accept(Token.RPAREN);
        }

        if (lexer.token() == Token.LITERAL_INT)
        {
            lexer.mark();
            lexer.nextToken();
        }
        if (lexer.token() == Token.IDENTIFIER)
        {
            lexer.nextToken();

        }
        if (lexer.token() == Token.EQ||lexer.token() == Token.DOT)
        {
            lexer.nextToken();
        } else  if(lexer.token() != Token.STAR)
        {
            lexer.reset();
        }
        if (lexer.token() == Token.PERCENT)
        {
            lexer.nextToken();
        }


    }


}
 
Example 19
Source Project: Mycat2   Source File: MycatExprParser.java    License: GNU General Public License v3.0 4 votes vote down vote up
public void parseTop()
{
    if (lexer.token() == Token.TOP)
    {
        lexer.nextToken();

        boolean paren = false;
        if (lexer.token() == Token.LPAREN)
        {
            paren = true;
            lexer.nextToken();
        }

        if (paren)
        {
            accept(Token.RPAREN);
        }

        if (lexer.token() == Token.LITERAL_INT)
        {
            lexer.mark();
            lexer.nextToken();
        }
        if (lexer.token() == Token.IDENTIFIER)
        {
            lexer.nextToken();

        }
        if (lexer.token() == Token.EQ||lexer.token() == Token.DOT)
        {
            lexer.nextToken();
        } else  if(lexer.token() != Token.STAR)
        {
            lexer.reset();
        }
        if (lexer.token() == Token.PERCENT)
        {
            lexer.nextToken();
        }


    }


}
 
Example 20
Source Project: elasticsearch-sql   Source File: ElasticSqlSelectParser.java    License: Apache License 2.0 4 votes vote down vote up
protected MySqlUpdateStatement parseUpdateStatment() {
    MySqlUpdateStatement update = new MySqlUpdateStatement();

    lexer.nextToken();

    if (lexer.identifierEquals(FnvHash.Constants.LOW_PRIORITY)) {
        lexer.nextToken();
        update.setLowPriority(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.IGNORE)) {
        lexer.nextToken();
        update.setIgnore(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.COMMIT_ON_SUCCESS)) {
        lexer.nextToken();
        update.setCommitOnSuccess(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.ROLLBACK_ON_FAIL)) {
        lexer.nextToken();
        update.setRollBackOnFail(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.QUEUE_ON_PK)) {
        lexer.nextToken();
        update.setQueryOnPk(true);
    }

    if (lexer.identifierEquals(FnvHash.Constants.TARGET_AFFECT_ROW)) {
        lexer.nextToken();
        SQLExpr targetAffectRow = this.exprParser.expr();
        update.setTargetAffectRow(targetAffectRow);
    }

    if (lexer.identifierEquals(FnvHash.Constants.FORCE)) {
        lexer.nextToken();

        if (lexer.token() == Token.ALL) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONS");
            update.setForceAllPartitions(true);
        } else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)){
            lexer.nextToken();
            update.setForceAllPartitions(true);
        } else if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            SQLName partition = this.exprParser.name();
            update.setForcePartition(partition);
        } else {
            throw new ParserException("TODO. " + lexer.info());
        }
    }

    while (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(update.getHints());
    }

    SQLSelectParser selectParser = this.exprParser.createSelectParser();
    SQLTableSource updateTableSource = selectParser.parseTableSource();
    update.setTableSource(updateTableSource);

    accept(Token.SET);

    for (;;) {
        SQLUpdateSetItem item = this.exprParser.parseUpdateSetItem();
        update.addItem(item);

        if (lexer.token() != Token.COMMA) {
            break;
        }

        lexer.nextToken();
    }

    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        update.setWhere(this.exprParser.expr());
    }

    update.setOrderBy(this.exprParser.parseOrderBy());
    update.setLimit(this.exprParser.parseLimit());

    return update;
}
 
Example 21
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 4 votes vote down vote up
private SQLExpr userNameRest(SQLExpr expr) {
    if (lexer.token() != Token.VARIANT || !lexer.stringVal().startsWith("@")) {
        return expr;
    }

    MySqlUserName userName = new MySqlUserName();
    if (expr instanceof SQLCharExpr) {
        userName.setUserName(((SQLCharExpr) expr).toString());
    } else {
        userName.setUserName(((SQLIdentifierExpr) expr).getName());
    }


    String strVal = lexer.stringVal();
    lexer.nextToken();

    if (strVal.length() > 1) {
        userName.setHost(strVal.substring(1));
        return userName;
    }

    if (lexer.token() == Token.LITERAL_CHARS) {
        userName.setHost("'" + lexer.stringVal() + "'");
    } else {
        userName.setHost(lexer.stringVal());
    }
    lexer.nextToken();

    if (lexer.token() == Token.IDENTIFIED) {
        Lexer.SavePoint mark = lexer.mark();

        lexer.nextToken();
        if (lexer.token() == Token.BY) {
            lexer.nextToken();
            if (lexer.identifierEquals(FnvHash.Constants.PASSWORD)) {
                lexer.reset(mark);
            } else {
                userName.setIdentifiedBy(lexer.stringVal());
                lexer.nextToken();
            }
        } else {
            lexer.reset(mark);
        }
    }

    return userName;
}
 
Example 22
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 4 votes vote down vote up
protected SQLExpr parseMatch() {

        MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();

        if (lexer.token() == Token.RPAREN) {
            lexer.nextToken();
        } else {
            exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
            accept(Token.RPAREN);
        }

        acceptIdentifier("AGAINST");

        accept(Token.LPAREN);
        SQLExpr against = primary();
        matchAgainstExpr.setAgainst(against);

        if (lexer.token() == Token.IN) {
            lexer.nextToken();
            if (lexer.identifierEquals(FnvHash.Constants.NATURAL)) {
                lexer.nextToken();
                acceptIdentifier("LANGUAGE");
                acceptIdentifier("MODE");
                if (lexer.token() == Token.WITH) {
                    lexer.nextToken();
                    acceptIdentifier("QUERY");
                    acceptIdentifier("EXPANSION");
                    matchAgainstExpr.setSearchModifier(MySqlMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
                } else {
                    matchAgainstExpr.setSearchModifier(MySqlMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE);
                }
            } else if (lexer.identifierEquals(FnvHash.Constants.BOOLEAN)) {
                lexer.nextToken();
                acceptIdentifier("MODE");
                matchAgainstExpr.setSearchModifier(MySqlMatchAgainstExpr.SearchModifier.IN_BOOLEAN_MODE);
            } else {
                throw new ParserException("syntax error. " + lexer.info());
            }
        } else if (lexer.token() == Token.WITH) {
            throw new ParserException("TODO. " + lexer.info());
        }

        accept(Token.RPAREN);

        return primaryRest(matchAgainstExpr);
    }
 
Example 23
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public MySqlPrimaryKey parsePrimaryKey() {
    accept(Token.PRIMARY);
    accept(Token.KEY);

    MySqlPrimaryKey primaryKey = new MySqlPrimaryKey();

    if (lexer.identifierEquals(FnvHash.Constants.USING)) {
        lexer.nextToken();
        primaryKey.setIndexType(lexer.stringVal());
        lexer.nextToken();
    }

    if (lexer.token() != Token.LPAREN) {
        SQLName name = this.name();
        primaryKey.setName(name);
    }

    accept(Token.LPAREN);
    for (;;) {
        SQLExpr expr;
        if (lexer.token() == Token.LITERAL_ALIAS) {
            expr = this.name();
        } else {
            expr = this.expr();
        }
        primaryKey.addColumn(expr);
        if (!(lexer.token() == (Token.COMMA))) {
            break;
        } else {
            lexer.nextToken();
        }
    }
    accept(Token.RPAREN);

    if (lexer.identifierEquals(FnvHash.Constants.USING)) {
        lexer.nextToken();
        primaryKey.setIndexType(lexer.stringVal());
        lexer.nextToken();
    }

    return primaryKey;
}
 
Example 24
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 4 votes vote down vote up
public SQLPartition parsePartition() {
    accept(Token.PARTITION);

    SQLPartition partitionDef = new SQLPartition();

    partitionDef.setName(this.name());

    SQLPartitionValue values = this.parsePartitionValues();
    if (values != null) {
        partitionDef.setValues(values);
    }

    for (;;) {
        boolean storage = false;
        if (lexer.identifierEquals(FnvHash.Constants.DATA)) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            partitionDef.setDataDirectory(this.expr());
        } else if (lexer.token() == Token.TABLESPACE) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLName tableSpace = this.name();
            partitionDef.setTablespace(tableSpace);
        } else if (lexer.token() == Token.INDEX) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            partitionDef.setIndexDirectory(this.expr());
        } else if (lexer.identifierEquals(FnvHash.Constants.MAX_ROWS)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr maxRows = this.primary();
            partitionDef.setMaxRows(maxRows);
        } else if (lexer.identifierEquals(FnvHash.Constants.MIN_ROWS)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr minRows = this.primary();
            partitionDef.setMaxRows(minRows);
        } else if (lexer.identifierEquals(FnvHash.Constants.ENGINE) || //
                (storage = (lexer.token() == Token.STORAGE || lexer.identifierEquals(FnvHash.Constants.STORAGE)))) {
            if (storage) {
                lexer.nextToken();
            }
            acceptIdentifier("ENGINE");

            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }

            SQLName engine = this.name();
            partitionDef.setEngine(engine);
        } else if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr comment = this.primary();
            partitionDef.setComment(comment);
        } else {
            break;
        }
    }

    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();

        for (;;) {
            acceptIdentifier("SUBPARTITION");

            SQLName subPartitionName = this.name();
            SQLSubPartition subPartition = new SQLSubPartition();
            subPartition.setName(subPartitionName);

            partitionDef.addSubPartition(subPartition);

            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }

        accept(Token.RPAREN);
    }
    return partitionDef;
}