Search in sources :

Example 56 with SQLSelectQueryBlock

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());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) DbType(com.alibaba.druid.DbType)

Example 57 with SQLSelectQueryBlock

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());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 58 with SQLSelectQueryBlock

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);
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLAllExpr(com.alibaba.druid.sql.ast.expr.SQLAllExpr) SQLReleaseSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLDropViewStatement(com.alibaba.druid.sql.ast.statement.SQLDropViewStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAlterTableDisableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint) SQLSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLSavePointStatement) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLSomeExpr(com.alibaba.druid.sql.ast.expr.SQLSomeExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLExprHint(com.alibaba.druid.sql.ast.statement.SQLExprHint) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLCommentStatement(com.alibaba.druid.sql.ast.statement.SQLCommentStatement) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLAnyExpr(com.alibaba.druid.sql.ast.expr.SQLAnyExpr) SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLAlterTableEnableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint)

Example 59 with SQLSelectQueryBlock

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;
        }
    };
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) 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) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 60 with SQLSelectQueryBlock

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());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)63 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)24 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)23 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)20 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)15 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)15 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)14 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)14 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)12 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)12 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)11 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)9 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)7 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)7 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)7 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)5 SQLUnionQuery (com.alibaba.druid.sql.ast.statement.SQLUnionQuery)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4