Search in sources :

Example 36 with OracleStatementParser

use of com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser in project druid by alibaba.

the class OracleAnalyticTest method test_1.

public void test_1() throws Exception {
    String sql = "SELECT submit_date, num_votes, TRUNC(AVG(num_votes) OVER(PARTITION BY submit_date ORDER BY submit_date ROWS UNBOUNDED PRECEDING)) AVG_VOTE_PER_DAY\n" + "FROM vote_count\n" + "ORDER BY submit_date;\n";
    String expect = "SELECT submit_date, num_votes, TRUNC(AVG(num_votes) OVER (PARTITION BY submit_date ORDER BY submit_date ROWS UNBOUNDED PRECEDING)) AS AVG_VOTE_PER_DAY\n" + "FROM vote_count\n" + "ORDER BY submit_date;\n";
    OracleStatementParser parser = new OracleStatementParser(sql);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
    String text = TestUtils.outputOracle(stmt);
    Assert.assertEquals(expect, text);
    System.out.println(text);
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 37 with OracleStatementParser

use of com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser in project druid by alibaba.

the class OracleAnyTest method test_any.

public void test_any() throws Exception {
    String sql = "SELECT country, prod, year, s FROM sales_view " + "MODEL PARTITION BY (country) " + "DIMENSION BY (prod, year) MEASURES (sale s) " + "IGNORE NAV " + "UNIQUE DIMENSION RULES UPSERT SEQUENTIAL ORDER (s[ANY, 2000] = 0) " + "ORDER BY country, prod, year;";
    String expect = "SELECT country, prod, year, s\n" + "FROM sales_view\n" + "MODEL\n" + "\tPARTITION BY (country)\n" + "\tDIMENSION BY (prod, year)\n" + "\tMEASURES (sale s)\n" + "\tIGNORE NAV\n" + "\tUNIQUE DIMENSION\n" + "\tRULES UPSERT SEQUENTIAL ORDER (s[ANY, 2000] = 0)\n" + "ORDER BY country, prod, year;\n";
    OracleStatementParser parser = new OracleStatementParser(sql);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
    String text = TestUtils.outputOracle(stmt);
    Assert.assertEquals(expect, text);
    System.out.println(text);
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 38 with OracleStatementParser

use of com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser in project druid by alibaba.

the class OracleCallTest method test_0.

public void test_0() throws Exception {
    String sql = "call ORACLE_OCM.MGMT_CONFIG.collect_config (  )";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    statemen.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(0, visitor.getTables().size());
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("departments")));
    Assert.assertEquals(0, visitor.getColumns().size());
//        Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("departments", "department_id")));
}
Also used : OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 39 with OracleStatementParser

use of com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser in project druid by alibaba.

the class OracleDeleteTest_2 method test_0.

public void test_0() throws Exception {
    String sql = "DELETE FROM (SELECT * FROM ABC WHERE ID=6000)";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    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());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ABC")));
    //
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("ABC", "ID")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "commission_pct")));
    {
        String output = SQLUtils.toOracleString(stmt);
        Assert.assertEquals("DELETE FROM (\n" + "\tSELECT *\n" + "\tFROM ABC\n" + "\tWHERE ID = 6000\n" + //
        ")", output);
    }
    {
        String output = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("delete from (\n" + "\tselect *\n" + "\tfrom ABC\n" + "\twhere ID = 6000\n" + //
        ")", output);
    }
}
Also used : OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 40 with OracleStatementParser

use of com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser in project druid by alibaba.

the class OracleDropDbLinkTest method test_1.

public void test_1() throws Exception {
    //
    String sql = "DROP DATABASE LINK local;";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statement = statementList.get(0);
    print(statementList);
    Assert.assertEquals("DROP DATABASE LINK local", SQLUtils.toSQLString(statement, JdbcConstants.ORACLE));
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    statement.accept(visitor);
    Assert.assertEquals(0, visitor.getTables().size());
}
Also used : OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Aggregations

OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)313 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)273 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)254 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)39 Column (com.alibaba.druid.stat.TableStat.Column)19 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)11 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 OracleParameterizedOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleParameterizedOutputVisitor)3 SQLMergeStatement (com.alibaba.druid.sql.ast.statement.SQLMergeStatement)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 Condition (com.alibaba.druid.stat.TableStat.Condition)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 ArrayList (java.util.ArrayList)2 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1