Search in sources :

Example 1 with MySqlOrderingExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr in project druid by alibaba.

the class MySqlCreateTableParser method parseConstraint.

protected SQLTableConstraint parseConstraint() {
    SQLName name = null;
    boolean hasConstaint = false;
    if (lexer.token() == (Token.CONSTRAINT)) {
        hasConstaint = true;
        lexer.nextToken();
    }
    if (lexer.token() == Token.IDENTIFIER) {
        name = this.exprParser.name();
    }
    if (lexer.token() == (Token.KEY)) {
        lexer.nextToken();
        MySqlKey key = new MySqlKey();
        key.setHasConstaint(hasConstaint);
        if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
            SQLName indexName = this.exprParser.name();
            if (indexName != null) {
                key.setIndexName(indexName);
            }
        }
        // 5.5语法 USING BTREE 放在index 名字后
        if (identifierEquals("USING")) {
            lexer.nextToken();
            key.setIndexType(lexer.stringVal());
            lexer.nextToken();
        }
        accept(Token.LPAREN);
        for (; ; ) {
            SQLExpr expr = this.exprParser.expr();
            if (lexer.token() == Token.ASC) {
                lexer.nextToken();
                expr = new MySqlOrderingExpr(expr, SQLOrderingSpecification.ASC);
            } else if (lexer.token() == Token.DESC) {
                lexer.nextToken();
                expr = new MySqlOrderingExpr(expr, SQLOrderingSpecification.DESC);
            }
            key.addColumn(expr);
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
            }
        }
        accept(Token.RPAREN);
        if (name != null) {
            key.setName(name);
        }
        if (identifierEquals("USING")) {
            lexer.nextToken();
            key.setIndexType(lexer.stringVal());
            lexer.nextToken();
        }
        return key;
    }
    if (lexer.token() == Token.PRIMARY) {
        MySqlPrimaryKey pk = this.getExprParser().parsePrimaryKey();
        pk.setName(name);
        pk.setHasConstaint(hasConstaint);
        return (SQLTableConstraint) pk;
    }
    if (lexer.token() == Token.UNIQUE) {
        MySqlUnique uk = this.getExprParser().parseUnique();
        uk.setName(name);
        uk.setHasConstaint(hasConstaint);
        return (SQLTableConstraint) uk;
    }
    if (lexer.token() == Token.FOREIGN) {
        MysqlForeignKey fk = this.getExprParser().parseForeignKey();
        fk.setName(name);
        fk.setHasConstraint(hasConstaint);
        return (SQLTableConstraint) fk;
    }
    throw new ParserException("TODO :" + lexer.token());
}
Also used : MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr) SQLTableConstraint(com.alibaba.druid.sql.ast.statement.SQLTableConstraint) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with MySqlOrderingExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr in project druid by alibaba.

the class MySqlExprParser method parseUnique.

public MySqlUnique parseUnique() {
    accept(Token.UNIQUE);
    if (lexer.token() == Token.KEY) {
        lexer.nextToken();
    }
    if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
    }
    MySqlUnique unique = new MySqlUnique();
    if (lexer.token() != Token.LPAREN) {
        SQLName indexName = name();
        unique.setIndexName(indexName);
    }
    //5.5语法 USING BTREE 放在index 名字后
    if (identifierEquals("USING")) {
        lexer.nextToken();
        unique.setIndexType(lexer.stringVal());
        lexer.nextToken();
    }
    accept(Token.LPAREN);
    for (; ; ) {
        SQLExpr column = this.expr();
        if (lexer.token() == Token.ASC) {
            column = new MySqlOrderingExpr(column, SQLOrderingSpecification.ASC);
            lexer.nextToken();
        } else if (lexer.token() == Token.DESC) {
            column = new MySqlOrderingExpr(column, SQLOrderingSpecification.DESC);
            lexer.nextToken();
        }
        unique.addColumn(column);
        if (!(lexer.token() == (Token.COMMA))) {
            break;
        } else {
            lexer.nextToken();
        }
    }
    accept(Token.RPAREN);
    if (identifierEquals("USING")) {
        lexer.nextToken();
        unique.setIndexType(lexer.stringVal());
        lexer.nextToken();
    }
    return unique;
}
Also used : MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with MySqlOrderingExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr in project druid by alibaba.

the class SQLSelectParser method parseGroupByItem.

protected SQLExpr parseGroupByItem() {
    SQLExpr item = this.exprParser.expr();
    if (JdbcConstants.MYSQL.equals(getDbType())) {
        if (lexer.token() == Token.DESC) {
            // skip
            lexer.nextToken();
            item = new MySqlOrderingExpr(item, SQLOrderingSpecification.DESC);
        } else if (lexer.token() == Token.ASC) {
            // skip
            lexer.nextToken();
            item = new MySqlOrderingExpr(item, SQLOrderingSpecification.ASC);
        }
    }
    return item;
}
Also used : MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr)

Example 4 with MySqlOrderingExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr in project Mycat-Server by MyCATApache.

the class DruidSelectParser method buildGroupByCols.

private String[] buildGroupByCols(List<SQLExpr> groupByItems, Map<String, String> aliaColumns) {
    String[] groupByCols = new String[groupByItems.size()];
    for (int i = 0; i < groupByItems.size(); i++) {
        SQLExpr sqlExpr = groupByItems.get(i);
        String column = null;
        if (sqlExpr instanceof SQLIdentifierExpr) {
            column = ((SQLIdentifierExpr) sqlExpr).getName();
        } else if (sqlExpr instanceof SQLMethodInvokeExpr) {
            column = ((SQLMethodInvokeExpr) sqlExpr).toString();
        } else if (sqlExpr instanceof MySqlOrderingExpr) {
            //todo czn
            SQLExpr expr = ((MySqlOrderingExpr) sqlExpr).getExpr();
            if (expr instanceof SQLName) {
                //不要转大写 2015-2-10 sohudo StringUtil.removeBackquote(expr.getSimpleName().toUpperCase());
                column = StringUtil.removeBackquote(((SQLName) expr).getSimpleName());
            } else {
                column = StringUtil.removeBackquote(expr.toString());
            }
        } else if (sqlExpr instanceof SQLPropertyExpr) {
            /**
				 * 针对子查询别名,例如select id from (select h.id from hotnews h  union select h.title from hotnews h ) as t1 group by t1.id;
				 */
            column = sqlExpr.toString();
        }
        if (column == null) {
            column = sqlExpr.toString();
        }
        int dotIndex = column.indexOf(".");
        int bracketIndex = column.indexOf("(");
        //通过判断含有括号来决定是否为函数列
        if (dotIndex != -1 && bracketIndex == -1) {
            //此步骤得到的column必须是不带.的,有别名的用别名,无别名的用字段名
            column = column.substring(dotIndex + 1);
        }
        //column;
        groupByCols[i] = getAliaColumn(aliaColumns, column);
    }
    return groupByCols;
}
Also used : MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with MySqlOrderingExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr in project druid by alibaba.

the class MySqlExprParser method parseSelectGroupByItem.

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;
}
Also used : MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr)

Aggregations

MySqlOrderingExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr)6 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 MySqlUnique (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique)2 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLTableConstraint (com.alibaba.druid.sql.ast.statement.SQLTableConstraint)1 MySqlKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey)1 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)1 MysqlForeignKey (com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1