Search in sources :

Example 16 with SchemaRepository

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());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 17 with SchemaRepository

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);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 18 with SchemaRepository

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);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 19 with SchemaRepository

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"));
}
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 20 with SchemaRepository

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"));
}
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