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