use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.
the class Resolve_AllColumn_Test_1 method test_resolve.
public void test_resolve() throws Exception {
SchemaRepository repository = new SchemaRepository(DbType.mysql);
repository.acceptDDL("create table t_emp(emp_id bigint, name varchar(20));");
SQLStatement stmt = SQLUtils.parseSingleMysqlStatement("select 1 as tag, * from t_emp");
repository.resolve(stmt, SchemaResolveVisitor.Option.ResolveAllColumn);
assertEquals("SELECT 1 AS tag, emp_id, name\n" + "FROM t_emp", stmt.toString());
}
use of com.alibaba.druid.sql.repository.SchemaRepository in project canal by alibaba.
the class FastsqlSchemaTest method test_json_index.
@Test
public void test_json_index() throws Throwable {
SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
String sql = " CREATE TABLE `articles` ( `article_id` bigint NOT NULL AUTO_INCREMENT," + " `tags` json DEFAULT NULL, PRIMARY KEY (`article_id`)," + " KEY `articles_tags` ((cast(json_extract(`tags`,_utf8mb4'$[*]') as char(40) array)))" + ") ENGINE=InnoDB AUTO_INCREMENT=1054 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
repository.console(sql);
repository.setDefaultSchema("test");
SchemaObject table = repository.findTable("articles");
System.out.println(table.getStatement().toString());
Assert.assertTrue(table.findColumn("article_id") != null);
}
use of com.alibaba.druid.sql.repository.SchemaRepository in project canal by alibaba.
the class FastsqlSchemaTest 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");
System.out.println(table.getStatement().toString());
Assert.assertTrue(table.findColumn("id") != null);
}
use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.
the class SchemaStatTest16 method test_schemaStat.
public void test_schemaStat() throws Exception {
SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
String sql = "SELECT\n" + "\tid AS id,\n" + "\tactivity_code AS activityCode,\n" + "\tactivity_name AS activityName,\n" + "\tstart_time AS startTime,\n" + "\tend_time AS endTime,\n" + "\t`state`,\n" + "\tbonus_type AS bonusType,\n" + "\tsend_num AS sendNum,\n" + "\tpoints,\n" + "\texchange_flag AS exchangeFlag,\n" + "\texchange_points AS exchangePoints,\n" + "\tauth_code_key AS authCodeKey,\n" + "\tauth_code_key_repeat AS authCodeKeyRepeat,\n" + "\t`type`,\n" + "\tmobile_flag AS mobileFlag,\n" + "\tactivity_code_list AS activityCodeList,\n" + "\tregister_product_id AS registerProductId,\n" + "\tadd_user AS addUser,\n" + "\tadd_time AS addTime,\n" + "\tmod_user AS modUser,\n" + "\tdelete_flag AS deleteFlag\n" + "FROM\n" + "\ttb_activity\n" + "WHERE\n" + "\tid != 1\n" + "AND id NOT IN (1, 2)\n" + "and DATE_FORMAT(add_time, '%Y-%m-%d') = '2018-08-08';";
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(21, statVisitor.getColumns().size());
assertEquals(3, statVisitor.getConditions().size());
assertEquals(1, statVisitor.getFunctions().size());
assertTrue(statVisitor.containsTable("tb_activity"));
assertTrue(statVisitor.containsColumn("tb_activity", "id"));
}
use of com.alibaba.druid.sql.repository.SchemaRepository in project druid by alibaba.
the class SchemaStatTest15 method test_schemaStat.
public void test_schemaStat() throws Exception {
SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
repository.acceptDDL("create table table1 (fid bigint, f1 varchar(100), f2 varchar(100))");
String sql = "select * from table1 t where t.f3 = 3";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL);
SQLStatement stmt = parser.parseStatementList().get(0);
System.out.println(stmt);
repository.resolve(stmt);
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(4, statVisitor.getColumns().size());
assertEquals(1, statVisitor.getConditions().size());
assertEquals(0, statVisitor.getFunctions().size());
assertTrue(statVisitor.containsTable("table1"));
assertTrue(statVisitor.containsColumn("table1", "f1"));
assertTrue(statVisitor.containsColumn("table1", "f2"));
assertTrue(statVisitor.containsColumn("UNKNOWN", "f3"));
}
Aggregations