Search in sources :

Example 66 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SchemaRepository method findView.

public SchemaObject findView(SQLName name) {
    if (name instanceof SQLIdentifierExpr) {
        return findView(((SQLIdentifierExpr) name).getName());
    }
    if (name instanceof SQLPropertyExpr) {
        SQLPropertyExpr propertyExpr = (SQLPropertyExpr) name;
        SQLExpr owner = propertyExpr.getOwner();
        String schema;
        String catalog = null;
        if (owner instanceof SQLIdentifierExpr) {
            schema = ((SQLIdentifierExpr) owner).getName();
        } else if (owner instanceof SQLPropertyExpr) {
            schema = ((SQLPropertyExpr) owner).getName();
            catalog = ((SQLPropertyExpr) owner).getOwnernName();
        } else {
            return null;
        }
        long tableHashCode64 = propertyExpr.nameHashCode64();
        Schema schemaObj = findSchema(schema, false);
        if (schemaObj != null) {
            SchemaObject table = schemaObj.findView(tableHashCode64);
            if (table != null) {
                return table;
            }
        }
        String ddl = loadDDL(catalog, schema, propertyExpr.getName());
        if (ddl == null) {
            schemaObj = findSchema(schema, true);
        } else {
            List<SQLStatement> stmtList = SQLUtils.parseStatements(ddl, schemaDbType);
            for (SQLStatement stmt : stmtList) {
                accept(stmt);
            }
            if (stmtList.size() == 1) {
                SQLStatement stmt = stmtList.get(0);
                if (stmt instanceof SQLCreateTableStatement) {
                    SQLCreateTableStatement createStmt = (SQLCreateTableStatement) stmt;
                    String schemaName = createStmt.getSchema();
                    schemaObj = findSchema(schemaName, true);
                }
            }
        }
        if (schemaObj == null) {
            return null;
        }
        return schemaObj.findView(tableHashCode64);
    }
    return null;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 67 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SQLExprParserTest method test_binary.

public void test_binary() throws Exception {
    SQLExprParser exprParser = new SQLExprParser("AGE > 5");
    SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) exprParser.expr();
    Assert.assertEquals(SQLBinaryOperator.GreaterThan, binaryOpExpr.getOperator());
    SQLIdentifierExpr left = (SQLIdentifierExpr) binaryOpExpr.getLeft();
    SQLIntegerExpr right = (SQLIntegerExpr) binaryOpExpr.getRight();
    Assert.assertEquals("AGE", left.getName());
    Assert.assertEquals(5, right.getNumber().intValue());
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExprParser(com.alibaba.druid.sql.parser.SQLExprParser)

Example 68 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class Demo2 method test_0.

public void test_0() throws Exception {
    String sql = "select * from user where uid = 2 and uname = ?";
    List<Object> parameters = new ArrayList<Object>();
    parameters.add(1);
    parameters.add("wenshao");
    SQLStatementParser parser = new MySqlStatementParser(sql);
    // 
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement first = (SQLStatement) stmtList.get(0);
    MyVisitor visitor = new MyVisitor();
    first.accept(visitor);
    SQLExpr firstVar = visitor.getVariantList().get(0);
    int userId;
    if (firstVar instanceof SQLVariantRefExpr) {
        int varIndex = (Integer) firstVar.getAttribute("varIndex");
        userId = (Integer) parameters.get(varIndex);
    } else {
        userId = ((SQLNumericLiteralExpr) firstVar).getNumber().intValue();
    }
    String tableName;
    if (userId == 1) {
        tableName = "user_1";
    } else {
        tableName = "user_x";
    }
    for (SQLExprTableSource tableSource : visitor.getTableSourceList()) {
        SQLExpr expr = tableSource.getExpr();
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equals("user")) {
                tableSource.setExpr(tableName);
            }
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr proExpr = (SQLPropertyExpr) expr;
            String ident = proExpr.getName();
            if (ident.equals("user")) {
                proExpr.setName(tableName);
            }
        }
    }
    String realSql = SQLUtils.toOracleString(first);
    System.out.println(realSql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 69 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SQLConstraintImpl method simplify.

public void simplify() {
    if (getName() instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identExpr = (SQLIdentifierExpr) getName();
        String columnName = identExpr.getName();
        String normalized = SQLUtils.normalize(columnName, dbType);
        if (columnName != normalized) {
            this.setName(normalized);
        }
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 70 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SQLCreateDatabaseStatement method setServer.

public boolean setServer(String server) {
    if (name == null) {
        return false;
    }
    if (name instanceof SQLIdentifierExpr) {
        SQLPropertyExpr propertyExpr = new SQLPropertyExpr(new SQLIdentifierExpr(server), ((SQLIdentifierExpr) name).getName());
        propertyExpr.setParent(this);
        name = propertyExpr;
        return true;
    }
    if (name instanceof SQLPropertyExpr) {
        SQLPropertyExpr propertyExpr = (SQLPropertyExpr) name;
        SQLExpr owner = propertyExpr.getOwner();
        if (owner instanceof SQLIdentifierExpr) {
            propertyExpr.setOwner(new SQLIdentifierExpr(server));
            return true;
        } else if (owner instanceof SQLPropertyExpr) {
            ((SQLPropertyExpr) owner).setName(server);
            return true;
        }
    }
    return false;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)152 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)68 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)45 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)17 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)16 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)10 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)10 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)9 ArrayList (java.util.ArrayList)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8