use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class Issue1865 method test_for_select.
public void test_for_select() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "select * from t where id = 2 and name = 'wenshao'";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement();
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
assertTrue(queryBlock.removeCondition("name = 'wenshao'"));
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = 2", stmt.toString());
assertTrue(queryBlock.removeCondition("id = 2"));
assertEquals("SELECT *\n" + "FROM t", stmt.toString());
queryBlock.addCondition("id = 3");
assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = 3", stmt.toString());
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class Issue2876 method test_0.
public void test_0() throws Exception {
String sql = "SELECT CONCAT(\"'\",b.PRIMARY_ID,\"'\") \n" + "FROM s_user_session_attributes a \n" + "LEFT JOIN s_user_session b ON a.SESSION_PRIMARY_ID=b.PRIMARY_ID \n" + "WHERE a.ATTRIBUTE_NAME='KAPTCHA_SESSION_KEY' AND b.LAST_ACCESS_TIME <= 1540429945459";
System.out.println(sql);
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
SQLSelectStatement stmt = (SQLSelectStatement) stmtList.get(0);
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
SQLJoinTableSource joinTableSource = (SQLJoinTableSource) queryBlock.getFrom();
assertEquals("a", joinTableSource.getLeft().getAlias());
assertEquals("b", joinTableSource.getRight().getAlias());
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class SQLASTVisitorAdapterTest method test_adapter.
public void test_adapter() throws Exception {
SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
new SQLBinaryOpExpr().accept(adapter);
new SQLInListExpr().accept(adapter);
new SQLSelectQueryBlock().accept(adapter);
new SQLDropTableStatement().accept(adapter);
new SQLCreateTableStatement().accept(adapter);
new SQLDeleteStatement().accept(adapter);
new SQLCurrentOfCursorExpr().accept(adapter);
new SQLInsertStatement().accept(adapter);
new SQLUpdateStatement().accept(adapter);
new SQLNotNullConstraint().accept(adapter);
new SQLMethodInvokeExpr().accept(adapter);
new SQLCallStatement().accept(adapter);
new SQLSomeExpr().accept(adapter);
new SQLAnyExpr().accept(adapter);
new SQLAllExpr().accept(adapter);
new SQLDefaultExpr().accept(adapter);
new SQLCommentStatement().accept(adapter);
new SQLDropViewStatement().accept(adapter);
new SQLSavePointStatement().accept(adapter);
new SQLReleaseSavePointStatement().accept(adapter);
new SQLCreateDatabaseStatement().accept(adapter);
new SQLAlterTableDropIndex().accept(adapter);
new SQLOver().accept(adapter);
new SQLWithSubqueryClause().accept(adapter);
new SQLAlterTableAlterColumn().accept(adapter);
new SQLAlterTableStatement().accept(adapter);
new SQLAlterTableDisableConstraint().accept(adapter);
new SQLAlterTableEnableConstraint().accept(adapter);
new SQLColumnCheck().accept(adapter);
new SQLExprHint().accept(adapter);
new SQLAlterTableDropConstraint().accept(adapter);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class Resolve_AllColumn_Test method test_resolve.
public void test_resolve() throws Exception {
SchemaRepository repository = new SchemaRepository(DbType.mysql);
repository.acceptDDL("create table t_emp(emp_id bigint, name varchar(20));");
SQLStatement stmt = SQLUtils.parseSingleMysqlStatement("select * from t_emp");
repository.resolve(stmt, SchemaResolveVisitor.Option.ResolveAllColumn);
assertEquals("SELECT emp_id, name\n" + "FROM t_emp", stmt.toString());
SQLSelectQueryBlock queryBlock = ((SQLSelectStatement) stmt).getSelect().getQueryBlock();
SQLIdentifierExpr expr = (SQLIdentifierExpr) queryBlock.getSelectList().get(0).getExpr();
assertNotNull(expr.getResolvedColumn());
new SQLASTVisitorAdapter() {
public boolean visit(SQLSelectQueryBlock queryBlock) {
final List<SQLSelectItem> selectList = queryBlock.getSelectList();
for (int i = 0; i < selectList.size(); i++) {
final SQLSelectItem selectItem = selectList.get(i);
final SQLExpr expr = selectItem.getExpr();
if (expr instanceof SQLAllColumnExpr) {
} else if (expr instanceof SQLPropertyExpr && ((SQLPropertyExpr) expr).getName().equals("*")) {
}
}
return true;
}
};
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.
the class MySqlSelectTest_148_extract method test_0.
public void test_0() throws Exception {
String sql = "SELECT EXTRACT(YEAR FROM '2008-01-02') >> 3 FROM corona_select_one_db_one_tb";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals(1, statementList.size());
assertEquals("SELECT EXTRACT(YEAR FROM '2008-01-02') >> 3\n" + "FROM corona_select_one_db_one_tb", stmt.toString());
assertEquals("SELECT EXTRACT(YEAR FROM ?) >> ?\n" + "FROM corona_select_one_db_one_tb", ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, VisitorFeature.OutputParameterizedZeroReplaceNotUseOriginalSql));
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
assertEquals(1, queryBlock.getSelectList().size());
assertEquals("EXTRACT(YEAR FROM '2008-01-02') >> 3", queryBlock.getSelectList().get(0).getExpr().toString());
}
Aggregations