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());
}
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", "*"));
}
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;
}
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);
}
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);
}
Aggregations