Search in sources :

Example 16 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr 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")) {
                identExpr.setName(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 17 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLInsertVisitor method supplyAutoIncrementColumn.

private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
    boolean isPreparedStatement = !getParameters().isEmpty();
    GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
    if (isPreparedStatement) {
        generatedKeyContext.getColumns().addAll(autoIncrementColumns);
    }
    TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
    for (String each : autoIncrementColumns) {
        SQLExpr sqlExpr;
        Object id = tableRule.generateId(each);
        generatedKeyContext.putValue(each, id);
        if (isPreparedStatement) {
            sqlExpr = new SQLVariantRefExpr("?");
            getParameters().add(id);
            ((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
        } else {
            sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
        }
        getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
        columns.add(new SQLIdentifierExpr(each));
        values.add(sqlExpr);
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) GeneratedKeyContext(com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 18 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLEvalVisitorTest method testVisitNullValue.

@Test
public void testVisitNullValue() throws Exception {
    SQLVariantRefExpr expr = new SQLVariantRefExpr("?");
    expr.setIndex(1);
    MySQLEvalVisitor visitor = new MySQLEvalVisitor();
    visitor.setParameters(Lists.<Object>newArrayList(1, null));
    expr.accept(visitor);
    assertThat(SQLEvalVisitorUtils.getValue(expr), is(MySQLEvalVisitor.EVAL_VALUE_NULL));
}
Also used : SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) Test(org.junit.Test)

Example 19 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLEvalVisitorTest method testVisitWrongName.

@Test
public void testVisitWrongName() throws Exception {
    SQLVariantRefExpr expr = new SQLVariantRefExpr("");
    expr.setIndex(1);
    MySQLEvalVisitor visitor = new MySQLEvalVisitor();
    visitor.setParameters(Lists.<Object>newArrayList(1, 2));
    expr.accept(visitor);
    assertThat(SQLEvalVisitorUtils.getValue(expr), nullValue());
}
Also used : SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) Test(org.junit.Test)

Example 20 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLEvalVisitorTest method testVisitNotOverride.

@Test
public void testVisitNotOverride() throws Exception {
    SQLVariantRefExpr expr = new SQLVariantRefExpr("?");
    expr.setIndex(1);
    expr.getAttributes().put(MySQLEvalVisitor.EVAL_VALUE, "test");
    MySQLEvalVisitor visitor = new MySQLEvalVisitor();
    visitor.setParameters(Lists.<Object>newArrayList(1, 2));
    expr.accept(visitor);
    assertThat((String) SQLEvalVisitorUtils.getValue(expr), is("test"));
}
Also used : SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) Test(org.junit.Test)

Aggregations

SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)22 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 Test (org.junit.Test)5 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 ArrayList (java.util.ArrayList)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)2 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2