Search in sources :

Example 96 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class SchemaStatTest8 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "SELECT * FROM AQLQCAT " + "JOIN AQLQCATB " + "   ON AQLQCAT.ZZ = AQLQCATB.RR " + "       AND trim(AQLQCAT.BB) = AQLQCATB.DD " + "WHERE AQLQCAT.MANDT = 'A0' " + "   AND AQLQCATB.NUM = 'A1'";
    DbType dbType = JdbcConstants.ORACLE;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    // System.out.println(statVisitor.getColumns());
    // System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    Assert.assertEquals(8, statVisitor.getColumns().size());
    Assert.assertEquals(6, statVisitor.getConditions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 97 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class SQLite_SelectTest_0 method test_0.

public void test_0() throws Exception {
    String sql = "select first_name, last_name from mytable limit 3, 4";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.SQLITE);
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    // System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 98 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class PGSelectTest63 method test_0.

public void test_0() throws Exception {
    String sql = "select * from a where a.id in (VALUES (4096), (4097))";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    assertEquals("SELECT *\n" + "FROM a\n" + "WHERE a.id IN (VALUES ((4096), (4097))", SQLUtils.toPGString(stmt));
    assertEquals("select *\n" + "from a\n" + "where a.id in (values ((4096), (4097))", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    assertEquals(1, stmtList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    assertEquals(2, visitor.getColumns().size());
    assertEquals(1, visitor.getTables().size());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 99 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class PGSelectTest64 method test_0.

public void test_0() throws Exception {
    String sql = "select * from public.newtable \n" + "where EXTRACT(EPOCH FROM timestamptz (column1)) >= EXTRACT(EPOCH FROM timestamptz '2017-09-10 00:00:00') \n" + "limit 10 offset 0";
    System.out.println(sql);
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    assertEquals("SELECT *\n" + "FROM public.newtable\n" + "WHERE EXTRACT (EPOCH FROM timestamptz(column1)) >= EXTRACT (EPOCH FROM TIMESTAMP  WITH TIME ZONE '2017-09-10 00:00:00')\n" + "LIMIT 10 OFFSET 0", SQLUtils.toPGString(stmt));
    assertEquals("select *\n" + "from public.newtable\n" + "where extract (EPOCH from timestamptz(column1)) >= extract (EPOCH from timestamp  with time zone '2017-09-10 00:00:00')\n" + "limit 10 offset 0", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    assertEquals(1, stmtList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    assertEquals(2, visitor.getColumns().size());
    assertEquals(1, visitor.getTables().size());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 100 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class PGSelectTest69_interval method test_0.

public void test_0() throws Exception {
    String sql = "SELECT\n" + "\n" + "        job_id,\n" + "        task_id,\n" + "        process_id,\n" + "        job_type,\n" + "        job_status,\n" + "        execute_server,\n" + "        execute_result,\n" + "        execute_times,\n" + "        execute_begin,\n" + "        execute_end,\n" + "        timeout_advice,\n" + "        create_time,\n" + "        update_time,\n" + "        opr_user,\n" + "        opr_time\n" + "\n" + "        FROM robot_job j\n" + "\n" + "        WHERE j.job_status = 1 AND j.timeout_advice = 0\n" + "        AND ( j.execute_begin <= NOW()-cast( ? as interval)*60)";
    System.out.println(sql);
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    assertEquals("SELECT job_id, task_id, process_id, job_type, job_status\n" + "\t, execute_server, execute_result, execute_times, execute_begin, execute_end\n" + "\t, timeout_advice, create_time, update_time, opr_user, opr_time\n" + "FROM robot_job j\n" + "WHERE j.job_status = 1\n" + "\tAND j.timeout_advice = 0\n" + "\tAND j.execute_begin <= NOW() - CAST(? AS interval) * 60", SQLUtils.toPGString(stmt));
    assertEquals("select job_id, task_id, process_id, job_type, job_status\n" + "\t, execute_server, execute_result, execute_times, execute_begin, execute_end\n" + "\t, timeout_advice, create_time, update_time, opr_user, opr_time\n" + "from robot_job j\n" + "where j.job_status = 1\n" + "\tand j.timeout_advice = 0\n" + "\tand j.execute_begin <= NOW() - cast(? as interval) * 60", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    assertEquals(1, stmtList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    // System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    assertEquals(15, visitor.getColumns().size());
    assertEquals(1, visitor.getTables().size());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)437 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)434 TableStat (com.alibaba.druid.stat.TableStat)68 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)61 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)51 SQLUtils (com.alibaba.druid.sql.SQLUtils)35 DbType (com.alibaba.druid.DbType)21 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)12 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)12 Test (org.junit.Test)12 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)11 SchemaRepository (com.alibaba.druid.sql.repository.SchemaRepository)11 Column (com.alibaba.druid.stat.TableStat.Column)10 InputStream (java.io.InputStream)9 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)6 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)6 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4