Search in sources :

Example 1 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project Mycat-Server by MyCATApache.

the class DruidSelectParser method buildOrderByCols.

protected LinkedHashMap<String, Integer> buildOrderByCols(List<SQLSelectOrderByItem> orderByItems, Map<String, String> aliaColumns) {
    LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
    for (int i = 0; i < orderByItems.size(); i++) {
        SQLOrderingSpecification type = orderByItems.get(i).getType();
        //orderColumn只记录字段名称,因为返回的结果集是不带表名的。
        SQLExpr expr = orderByItems.get(i).getExpr();
        String col;
        if (expr instanceof SQLName) {
            col = ((SQLName) expr).getSimpleName();
        } else {
            col = expr.toString();
        }
        if (type == null) {
            type = SQLOrderingSpecification.ASC;
        }
        //此步骤得到的col必须是不带.的,有别名的用别名,无别名的用字段名
        col = getAliaColumn(aliaColumns, col);
        map.put(col, type == SQLOrderingSpecification.ASC ? OrderCol.COL_ORDER_TYPE_ASC : OrderCol.COL_ORDER_TYPE_DESC);
    }
    return map;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLOrderingSpecification(com.alibaba.druid.sql.ast.SQLOrderingSpecification) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project Mycat-Server by MyCATApache.

the class MycatStatementParser method parseLoadDataInFile.

//此处注释掉,以修正后端jdbc方式时,delete语句解析出错的情况
//
//    public SQLSelectParser createSQLSelectParser()
//    {
//        return new MycatSelectParser(this.selectExprParser);
//    }
@Override
protected MySqlLoadDataInFileStatement parseLoadDataInFile() {
    acceptIdentifier("DATA");
    LoadDataStatement stmt = new LoadDataStatement();
    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) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLName(com.alibaba.druid.sql.ast.SQLName)

Example 3 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method visit.

public boolean visit(MySqlCreateTableStatement x) {
    SQLName sqlName = x.getName();
    if (sqlName != null) {
        String table = sqlName.toString();
        if (table.startsWith("`")) {
            table = table.substring(1, table.length() - 1);
        }
        setCurrentTable(table);
    }
    return false;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName)

Example 4 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method visit.

public boolean visit(MySqlInsertStatement x) {
    SQLName sqlName = x.getTableName();
    if (sqlName != null) {
        String table = sqlName.toString();
        if (table.startsWith("`")) {
            table = table.substring(1, table.length() - 1);
        }
        setCurrentTable(sqlName.toString());
    }
    return false;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName)

Example 5 with SQLName

use of com.alibaba.druid.sql.ast.SQLName in project Mycat-Server by MyCATApache.

the class GlobalTableUtil method hasGlobalColumn.

private static boolean hasGlobalColumn(SQLStatement statement) {
    for (SQLTableElement tableElement : ((SQLCreateTableStatement) statement).getTableElementList()) {
        SQLName sqlName = null;
        if (tableElement instanceof SQLColumnDefinition) {
            sqlName = ((SQLColumnDefinition) tableElement).getName();
        }
        if (sqlName != null) {
            String simpleName = sqlName.getSimpleName();
            simpleName = StringUtil.removeBackquote(simpleName);
            if (tableElement instanceof SQLColumnDefinition && GLOBAL_TABLE_MYCAT_COLUMN.equalsIgnoreCase(simpleName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Aggregations

SQLName (com.alibaba.druid.sql.ast.SQLName)102 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)33 TableStat (com.alibaba.druid.stat.TableStat)20 ParserException (com.alibaba.druid.sql.parser.ParserException)17 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)8 SQLObject (com.alibaba.druid.sql.ast.SQLObject)6 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)6 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)6 WallContext (com.alibaba.druid.wall.WallContext)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 WallSqlTableStat (com.alibaba.druid.wall.WallSqlTableStat)5 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)4 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)4 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)4 SQLSubPartition (com.alibaba.druid.sql.ast.SQLSubPartition)3 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3