Search in sources :

Example 46 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class OracleSelectTest36 method test_0.

public void test_0() throws Exception {
    // 
    String sql = // 
    "select ID,name from fastsql_test where (name>=? or name is null) and card_id<?";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    String result = SQLUtils.toOracleString(stmt);
    String result_lcase = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
    Assert.assertEquals("SELECT ID, name" + "\nFROM fastsql_test" + "\nWHERE (name >= ?" + "\n\t\tOR name IS NULL)" + "\n\tAND card_id < ?", result);
    Assert.assertEquals("select ID, name" + "\nfrom fastsql_test" + "\nwhere (name >= ?" + "\n\t\tor name is null)" + "\n\tand card_id < ?", result_lcase);
    {
        SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
        SQLBinaryOpExpr where = (SQLBinaryOpExpr) queryBlock.getWhere();
        Assert.assertEquals(SQLBinaryOperator.BooleanAnd, where.getOperator());
        SQLBinaryOpExpr left = (SQLBinaryOpExpr) where.getLeft();
        Assert.assertEquals(SQLBinaryOperator.BooleanOr, left.getOperator());
        SQLBinaryOpExpr nameGTEQ = (SQLBinaryOpExpr) left.getLeft();
        Assert.assertEquals(SQLBinaryOperator.GreaterThanOrEqual, nameGTEQ.getOperator());
        SQLBinaryOpExpr nameIS = (SQLBinaryOpExpr) left.getRight();
        Assert.assertEquals(SQLBinaryOperator.Is, nameIS.getOperator());
    }
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("fastsql_test")));
    Assert.assertEquals(3, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("fastsql_test", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("fastsql_test", "name")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("fastsql_test", "card_id")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
Also used : SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 47 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class DruidDataSource method db2ValidationQueryCheck.

private void db2ValidationQueryCheck() {
    if (validationQuery == null) {
        return;
    }
    if (validationQuery.length() == 0) {
        return;
    }
    SQLStatementParser sqlStmtParser = SQLParserUtils.createSQLStatementParser(validationQuery, this.dbTypeName);
    List<SQLStatement> stmtList = sqlStmtParser.parseStatementList();
    if (stmtList.size() != 1) {
        return;
    }
    SQLStatement stmt = stmtList.get(0);
    if (!(stmt instanceof SQLSelectStatement)) {
        return;
    }
    SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        if (((SQLSelectQueryBlock) query).getFrom() == null) {
            LOG.error("invalid db2 validationQuery. " + validationQuery + ", may should be : " + validationQuery + " FROM SYSDUMMY");
        }
    }
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 48 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class DruidDataSource method oracleValidationQueryCheck.

private void oracleValidationQueryCheck() {
    if (validationQuery == null) {
        return;
    }
    if (validationQuery.length() == 0) {
        return;
    }
    SQLStatementParser sqlStmtParser = SQLParserUtils.createSQLStatementParser(validationQuery, this.dbTypeName);
    List<SQLStatement> stmtList = sqlStmtParser.parseStatementList();
    if (stmtList.size() != 1) {
        return;
    }
    SQLStatement stmt = stmtList.get(0);
    if (!(stmt instanceof SQLSelectStatement)) {
        return;
    }
    SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        if (((SQLSelectQueryBlock) query).getFrom() == null) {
            LOG.error("invalid oracle validationQuery. " + validationQuery + ", may should be : " + validationQuery + " FROM DUAL");
        }
    }
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 49 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class SQLAnyExpr method computeDataType.

public SQLDataType computeDataType() {
    if (subQuery == null) {
        return null;
    }
    SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
    if (queryBlock == null) {
        return null;
    }
    List<SQLSelectItem> selectList = queryBlock.getSelectList();
    if (selectList.size() == 1) {
        return selectList.get(0).computeDataType();
    }
    return null;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 50 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock in project druid by alibaba.

the class SQLSomeExpr method computeDataType.

public SQLDataType computeDataType() {
    if (subQuery == null) {
        return null;
    }
    SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
    if (queryBlock == null) {
        return null;
    }
    List<SQLSelectItem> selectList = queryBlock.getSelectList();
    if (selectList.size() == 1) {
        return selectList.get(0).computeDataType();
    }
    return null;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

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