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);
}
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);
}
}
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));
}
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());
}
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"));
}
Aggregations