Search in sources :

Example 1 with SchemaRepository

use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.

the class CanalSQLSchemaTest method test_persistent.

@Test
public void test_persistent() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = " create table example_vc_tbl( c1 int not null auto_increment primary key," + "c2 varchar(70), vc1 int as (length(c2)) virtual," + "DIM_SUM varchar(128) AS (MD5(UPPER(CONCAT(c2, c1)))) PERSISTENT)";
    repository.console(sql);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("example_vc_tbl");
    Assert.assertTrue(table.findColumn("DIM_SUM") != null);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 2 with SchemaRepository

use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.

the class SchemaStatTest15 method test_schemaStat_2.

public void test_schemaStat_2() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    repository.acceptDDL("create table table1 (fid bigint, f1 varchar(100), f2 varchar(100));");
    repository.acceptDDL("create view view1 as select fid table1 where fid = 1;");
    SchemaObject viewObject = repository.findView("view1");
    System.out.println(viewObject);
    assertEquals("view1", viewObject.getName());
    assertEquals(SchemaObjectType.View, viewObject.getType());
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 3 with SchemaRepository

use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.

the class SchemaStatTest20_issue3993 method test_schemaStat.

public void test_schemaStat() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "select * from tableA where COALESCE(service_code,'/') <> '0821';";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(repository);
    stmt.accept(statVisitor);
    System.out.println("Tables : " + statVisitor.getTables());
    System.out.println("columns : " + statVisitor.getColumns());
    // System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println("conditions : " + statVisitor.getConditions());
    assertEquals(2, statVisitor.getColumns().size());
    assertEquals(1, statVisitor.getConditions().size());
    assertEquals(1, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 4 with SchemaRepository

use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.

the class SchemaStatTest22_yiran method test_schemaStat.

public void test_schemaStat() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    repository.console("CREATE TABLE DEPT (depno INT4, dname ENC_TEXT)");
    String sql = "SELECT * FROM ( SELECT dname FROM DEPT) X WHERE X.dname='cs';";
    // sql = "SELECT * FROM ( SELECT * FROM DEPT) X WHERE X.dname='cs'";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(repository);
    stmt.accept(statVisitor);
    SQLSelectQueryBlock q = ((SQLSelectStatement) stmt).getSelect().getFirstQueryBlock();
    SQLPropertyExpr whereLeft = (SQLPropertyExpr) ((SQLBinaryOpExpr) q.getWhere()).getLeft();
    SQLColumnDefinition resolvedColumn = whereLeft.getResolvedColumn();
    assertNotNull(resolvedColumn);
    System.out.println("Tables : " + statVisitor.getTables());
    System.out.println("columns : " + statVisitor.getColumns());
    // System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println("conditions : " + statVisitor.getConditions());
    assertEquals(1, statVisitor.getColumns().size());
    assertEquals(1, statVisitor.getConditions().size());
    assertEquals(0, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 5 with SchemaRepository

use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.

the class SchemaStatTest18 method test_schemaStat.

public void test_schemaStat() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "SELECT * FROM t WHERE 3 > f1 -1;";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(repository);
    stmt.accept(statVisitor);
    System.out.println("Tables : " + statVisitor.getTables());
    System.out.println(statVisitor.getColumns());
    // System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println(statVisitor.getConditions());
    assertEquals(2, statVisitor.getColumns().size());
    assertEquals(1, statVisitor.getConditions().size());
    assertEquals(0, statVisitor.getFunctions().size());
    assertTrue(statVisitor.containsTable("t"));
    assertTrue(statVisitor.containsColumn("t", "f1"));
    assertEquals("t.f1 < 4", statVisitor.getConditions().get(0).toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Aggregations

SchemaRepository (com.alibaba.druid.sql.repository.SchemaRepository)34 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)19 SchemaObject (com.alibaba.druid.sql.repository.SchemaObject)13 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)11 Test (org.junit.Test)11 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)8 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 AdsStatementParser (com.alibaba.druid.sql.dialect.ads.parser.AdsStatementParser)1 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)1 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)1