Search in sources :

Example 21 with SchemaRepository

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

the class SchemaStatTest17 method test_schemaStat.

public void test_schemaStat() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "SELECT * FROM t WHERE f1 -1 < 3;";
    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)

Example 22 with SchemaRepository

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

the class SchemaStatTest19 method test_schemaStat.

public void test_schemaStat() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "select * from table1 a left outer join table2 b on a.id=b.id";
    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(statVisitor.getConditions());
    assertEquals(4, statVisitor.getColumns().size());
    assertEquals(2, statVisitor.getConditions().size());
    assertEquals(0, statVisitor.getFunctions().size());
    assertTrue(statVisitor.containsTable("table1"));
    assertTrue(statVisitor.containsColumn("table1", "*"));
    assertTrue(statVisitor.containsColumn("table1", "id"));
    assertTrue(statVisitor.containsColumn("table2", "id"));
    assertTrue(statVisitor.containsColumn("table2", "*"));
}
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 23 with SchemaRepository

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

the class SQLUnifiedVisitor method visit.

@Override
public boolean visit(MySqlSelectQueryBlock x) {
    SchemaRepository repository = new SchemaRepository(DbType.mysql);
    repository.resolve(x);
    List<SQLSelectItem> selectList = x.getSelectList();
    for (SQLSelectItem item : selectList) {
        if (item.getExpr() instanceof SQLPropertyExpr) {
            SQLPropertyExpr expr = (SQLPropertyExpr) item.getExpr();
            SQLTableSource resolvedTableSource = expr.getResolvedTableSource();
            if (resolvedTableSource != null) {
                String alias = resolvedTableSource.getAlias();
                if (alias != null) {
                    expr.setOwner(alias);
                } else {
                    expr.setOwner(resolvedTableSource.computeAlias());
                }
            }
        }
        item.setAlias(null);
    }
    Collections.sort(selectList, new Comparator<SQLSelectItem>() {

        @Override
        public int compare(SQLSelectItem o1, SQLSelectItem o2) {
            return o1.toString().compareToIgnoreCase(o2.toString());
        }
    });
    if (x.getFrom() != null) {
        x.getFrom().accept(this);
    }
    if (x.getWhere() != null) {
        x.getWhere().accept(this);
    }
    if (x.getGroupBy() != null) {
        x.getGroupBy().accept(this);
    }
    return false;
}
Also used : SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 24 with SchemaRepository

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

the class CanalSQLSchemaTest method testSimple.

@Test
public void testSimple() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql1 = "CREATE TABLE `table_x1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, " + "`key1` longtext NOT NULL COMMENT 'key1', `value1` longtext NOT NULL COMMENT 'value1', PRIMARY KEY (`id`) )" + "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
    String sql2 = " CREATE TABLE IF NOT EXISTS `table_x1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT," + "`key1` longtext NOT NULL COMMENT 'key1',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
    repository.console(sql1);
    repository.console(sql2);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("table_x1");
    Assert.assertTrue(table.findColumn("value1") != null);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 25 with SchemaRepository

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

the class CanalSQLSchemaTest method test_block_format.

@Test
public void test_block_format() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = " CREATE TABLE `parent` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT," + "`created_at` timestamp NULL DEFAULT NULL, " + "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 BLOCK_FORMAT=ENCRYPTED";
    repository.console(sql);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("parent");
    Assert.assertTrue(table.findColumn("id") != null);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

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