Search in sources :

Example 6 with SchemaRepository

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

the class SchemaStatTest21_issue3980 method test_schemaStat.

public void test_schemaStat() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "select n.user_name,\n" + "n.user_passwd,\n" + "n.user_project,\n" + "n.start_date,\n" + "n.end_date\n" + "from (\n" + "select t.name as user_name,\n" + "t.passwd as user_passwd,\n" + "cast(from_unixtime(t.from_time, \"yyyyMMdd\") as int) as start_date,\n" + "cast(from_unixtime(t.to_time, \"yyyyMMdd\") as int) as end_date\n" + "from tableA as t\n" + "where t.user_id = 1\n" + "union all\n" + "select p.project as user_project\n" + "from tableB as p\n" + "where p.project_id = 10\n" + ") as n;";
    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());
    // group by
    System.out.println(statVisitor.getGroupByColumns());
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println("conditions : " + statVisitor.getConditions());
    System.out.println("functions : " + statVisitor.getFunctions());
    assertEquals(7, statVisitor.getColumns().size());
    assertEquals(2, statVisitor.getConditions().size());
    assertEquals(2, statVisitor.getFunctions().size());
    SQLPropertyExpr expr = (SQLPropertyExpr) statVisitor.getFunctions().get(0).getArguments().get(0);
    SQLIdentifierExpr tableAlias = (SQLIdentifierExpr) expr.getOwner();
    tableAlias.getResolvedTableSource();
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 7 with SchemaRepository

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

the class FastsqlSchemaTest 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");
    System.out.println(table.getStatement().toString());
    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 8 with SchemaRepository

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

the class FastsqlSchemaTest method test_invisible.

@Test
public void test_invisible() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = " CREATE TABLE `proposal_order_info` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT," + "`created_at` timestamp NULL DEFAULT NULL, " + "PRIMARY KEY (`id`) , " + "KEY `idx_create_time` (`created_at`) /*!80000 INVISIBLE */" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 BLOCK_FORMAT=ENCRYPTED";
    repository.console(sql);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("proposal_order_info");
    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 9 with SchemaRepository

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

the class FastsqlSchemaTest 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");
    System.out.println(table.getStatement().toString());
    Assert.assertTrue(table.findColumn("c1") != null);
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository) Test(org.junit.Test)

Example 10 with SchemaRepository

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

the class MySqlCreateTable_showColumns_test_4 method test_0.

public void test_0() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "create table tmp_eric (pk int key, ia int unique);";
    SQLStatement stmt = SQLUtils.parseSingleMysqlStatement(sql);
    assertEquals("CREATE TABLE tmp_eric (\n" + "\tpk int PRIMARY KEY,\n" + "\tia int UNIQUE\n" + ");", stmt.toString());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) 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