Search in sources :

Example 11 with SchemaRepository

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

the class MySqlCreateTableTest84 method test_one.

public void test_one() throws Exception {
    String sql = "CREATE TABLE `test` (\n" + "  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',\n" + "  `c_tinyint` tinyint(4) DEFAULT '1' COMMENT 'tinyint',\n" + "  `c_smallint` smallint(6) DEFAULT 0 COMMENT 'smallint',\n" + "  `c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',\n" + "  `c_int` int(11) DEFAULT NULL COMMENT 'int',\n" + "  `c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',\n" + "  `c_decimal` decimal(10,3) DEFAULT NULL COMMENT 'decimal',\n" + "  `c_date` date DEFAULT '0000-00-00' COMMENT 'date',\n" + "  `c_datetime` datetime DEFAULT '0000-00-00 00:00:00' COMMENT 'datetime',\n" + "  `c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',\n" + "  `c_time` time DEFAULT NULL COMMENT 'time',\n" + "  `c_char` char(10) DEFAULT NULL COMMENT 'char',\n" + "  `c_varchar` varchar(10) DEFAULT 'hello' COMMENT 'varchar',\n" + "  `c_blob` blob COMMENT 'blob',\n" + "  `c_text` text COMMENT 'text',\n" + "  `c_mediumtext` mediumtext COMMENT 'mediumtext',\n" + "  `c_longblob` longblob COMMENT 'longblob',\n" + "  PRIMARY KEY (`id`),\n" + "  UNIQUE KEY `uk_a` (`c_tinyint`),\n" + "  KEY `k_b` (`c_smallint`),\n" + "  KEY `k_c` (`c_mediumint`,`c_int`)\n" + ") ENGINE=InnoDB AUTO_INCREMENT=1769503 DEFAULT CHARSET=utf8mb4 COMMENT='10000000';";
    MySqlStatementParser parser = new MySqlStatementParser(sql, SQLParserFeature.KeepComments);
    SQLStatement stmt = parser.parseCreateTable();
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    // 
    // Column column = visitor.getColumn("tb_custom_vip_show_message", "custom_vip_show_message_seq");
    // assertNotNull(column);
    // assertEquals("INT", column.getDataType());
    System.out.println(stmt);
    {
        String output = SQLUtils.toMySqlString(stmt);
        assertEquals("CREATE TABLE `test` (\n" + "\t`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',\n" + "\t`c_tinyint` tinyint(4) DEFAULT '1' COMMENT 'tinyint',\n" + "\t`c_smallint` smallint(6) DEFAULT 0 COMMENT 'smallint',\n" + "\t`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',\n" + "\t`c_int` int(11) DEFAULT NULL COMMENT 'int',\n" + "\t`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',\n" + "\t`c_decimal` decimal(10, 3) DEFAULT NULL COMMENT 'decimal',\n" + "\t`c_date` date DEFAULT '0000-00-00' COMMENT 'date',\n" + "\t`c_datetime` datetime DEFAULT '0000-00-00 00:00:00' COMMENT 'datetime',\n" + "\t`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',\n" + "\t`c_time` time DEFAULT NULL COMMENT 'time',\n" + "\t`c_char` char(10) DEFAULT NULL COMMENT 'char',\n" + "\t`c_varchar` varchar(10) DEFAULT 'hello' COMMENT 'varchar',\n" + "\t`c_blob` blob COMMENT 'blob',\n" + "\t`c_text` text COMMENT 'text',\n" + "\t`c_mediumtext` mediumtext COMMENT 'mediumtext',\n" + "\t`c_longblob` longblob COMMENT 'longblob',\n" + "\tPRIMARY KEY (`id`),\n" + "\tUNIQUE KEY `uk_a` (`c_tinyint`),\n" + "\tKEY `k_b` (`c_smallint`),\n" + "\tKEY `k_c` (`c_mediumint`, `c_int`)\n" + ") ENGINE = InnoDB AUTO_INCREMENT = 1769503 CHARSET = utf8mb4 COMMENT '10000000'", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        assertEquals("create table `test` (\n" + "\t`id` bigint(20) unsigned not null auto_increment comment 'id',\n" + "\t`c_tinyint` tinyint(4) default '1' comment 'tinyint',\n" + "\t`c_smallint` smallint(6) default 0 comment 'smallint',\n" + "\t`c_mediumint` mediumint(9) default null comment 'mediumint',\n" + "\t`c_int` int(11) default null comment 'int',\n" + "\t`c_bigint` bigint(20) default null comment 'bigint',\n" + "\t`c_decimal` decimal(10, 3) default null comment 'decimal',\n" + "\t`c_date` date default '0000-00-00' comment 'date',\n" + "\t`c_datetime` datetime default '0000-00-00 00:00:00' comment 'datetime',\n" + "\t`c_timestamp` timestamp null default null comment 'timestamp',\n" + "\t`c_time` time default null comment 'time',\n" + "\t`c_char` char(10) default null comment 'char',\n" + "\t`c_varchar` varchar(10) default 'hello' comment 'varchar',\n" + "\t`c_blob` blob comment 'blob',\n" + "\t`c_text` text comment 'text',\n" + "\t`c_mediumtext` mediumtext comment 'mediumtext',\n" + "\t`c_longblob` longblob comment 'longblob',\n" + "\tprimary key (`id`),\n" + "\tunique key `uk_a` (`c_tinyint`),\n" + "\tkey `k_b` (`c_smallint`),\n" + "\tkey `k_c` (`c_mediumint`, `c_int`)\n" + ") engine = InnoDB auto_increment = 1769503 charset = utf8mb4 comment '10000000'", output);
    }
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    repository.console(sql);
    assertEquals("CREATE TABLE `test` (\n" + "\t`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',\n" + "\t`c_tinyint` tinyint(4) DEFAULT '1' COMMENT 'tinyint',\n" + "\t`c_smallint` smallint(6) DEFAULT 0 COMMENT 'smallint',\n" + "\t`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',\n" + "\t`c_int` int(11) DEFAULT NULL COMMENT 'int',\n" + "\t`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',\n" + "\t`c_decimal` decimal(10, 3) DEFAULT NULL COMMENT 'decimal',\n" + "\t`c_date` date DEFAULT '0000-00-00' COMMENT 'date',\n" + "\t`c_datetime` datetime DEFAULT '0000-00-00 00:00:00' COMMENT 'datetime',\n" + "\t`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',\n" + "\t`c_time` time DEFAULT NULL COMMENT 'time',\n" + "\t`c_char` char(10) DEFAULT NULL COMMENT 'char',\n" + "\t`c_varchar` varchar(10) DEFAULT 'hello' COMMENT 'varchar',\n" + "\t`c_blob` blob COMMENT 'blob',\n" + "\t`c_text` text COMMENT 'text',\n" + "\t`c_mediumtext` mediumtext COMMENT 'mediumtext',\n" + "\t`c_longblob` longblob COMMENT 'longblob',\n" + "\tPRIMARY KEY (`id`),\n" + "\tUNIQUE KEY `uk_a` (`c_tinyint`),\n" + "\tKEY `k_b` (`c_smallint`),\n" + "\tKEY `k_c` (`c_mediumint`, `c_int`)\n" + ") ENGINE = InnoDB AUTO_INCREMENT = 1769503 CHARSET = utf8mb4 COMMENT '10000000'", repository.console("show create table test"));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 12 with SchemaRepository

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

the class OracleSelectTest106 method test_0.

public void test_0() throws Exception {
    // 
    String sql = "select * from a join b on a.id = b.aid where a.cid = 1";
    System.out.println(sql);
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    System.out.println(stmt.toString());
    assertEquals(1, statementList.size());
    SchemaRepository repository = new SchemaRepository(DbType.oracle);
    repository.resolve(stmt);
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(DbType.oracle);
    stmt.accept(visitor);
    TableStat.Column cid = visitor.getColumn("a", "cid");
    assertNotNull(cid);
    assertTrue(cid.isWhere());
    TableStat.Condition condition = visitor.getConditions().get(2);
    assertTrue(condition.getColumn().isWhere());
    assertSame(cid, condition.getColumn());
    {
        String text = SQLUtils.toOracleString(stmt);
        assertEquals("SELECT *\n" + "FROM a\n" + "\tJOIN b ON a.id = b.aid \n" + "WHERE a.cid = 1", text);
    }
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    assertEquals(2, visitor.getTables().size());
    assertEquals(5, visitor.getColumns().size());
    assertEquals(3, visitor.getConditions().size());
    assertEquals(1, visitor.getRelationships().size());
    assertEquals(0, visitor.getOrderByColumns().size());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 13 with SchemaRepository

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

the class OracleSelectTest108 method test_0.

public void test_0() throws Exception {
    // 
    String sql = "SELECT\n" + "  OBJID,\n" + "  NAME,\n" + "  OWNER,\n" + "  OWNERID,\n" + "  TABLESPACE,\n" + "  TSNO,\n" + "  FILENO,\n" + "  BLOCKNO,\n" + "  AUDIT$,\n" + "  COMMENT$,\n" + "  CLUSTERFLAG,\n" + "  PCTFREE$,\n" + "  PCTUSED$,\n" + "  INITRANS,\n" + "  MAXTRANS,\n" + "  DEGREE,\n" + "  INSTANCES,\n" + "  CACHE,\n" + "  PROPERTY,\n" + "  DEFLOG,\n" + "  TSDEFLOG,\n" + "  ROID,\n" + "  ROWCNT,\n" + "  BLKCNT,\n" + "  AVGRLEN,\n" + "  TFLAGS,\n" + "  TRIGFLAG,\n" + "  OBJSTATUS,\n" + "  XDBOOL\n" + "FROM SYS.EXU10TABU T$\n" + "WHERE NOT EXISTS(SELECT NAME\n" + "                 FROM SYS.EXU8NXPU N$\n" + "                 WHERE N$.NAME = T$.NAME AND N$.TYPE = 2)\n" + "ORDER BY T$.XDBOOL DESC, T$.NAME";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
    System.out.println(stmt.toString());
    assertEquals(1, statementList.size());
    SchemaRepository repository = new SchemaRepository(DbType.oracle);
    repository.resolve(stmt);
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(DbType.oracle);
    stmt.accept(visitor);
    {
        String text = SQLUtils.toOracleString(stmt);
        assertEquals("SELECT OBJID, NAME, OWNER, OWNERID, TABLESPACE\n" + "\t, TSNO, FILENO, BLOCKNO, AUDIT$, COMMENT$\n" + "\t, CLUSTERFLAG, PCTFREE$, PCTUSED$, INITRANS, MAXTRANS\n" + "\t, DEGREE, INSTANCES, CACHE, PROPERTY, DEFLOG\n" + "\t, TSDEFLOG, ROID, ROWCNT, BLKCNT, AVGRLEN\n" + "\t, TFLAGS, TRIGFLAG, OBJSTATUS, XDBOOL\n" + "FROM SYS.EXU10TABU T$\n" + "WHERE NOT EXISTS (\n" + "\tSELECT NAME\n" + "\tFROM SYS.EXU8NXPU N$\n" + "\tWHERE N$.NAME = T$.NAME\n" + "\t\tAND N$.TYPE = 2\n" + ")\n" + "ORDER BY T$.XDBOOL DESC, T$.NAME", text);
    }
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    assertEquals(2, visitor.getTables().size());
    assertEquals(31, visitor.getColumns().size());
    assertEquals(3, visitor.getConditions().size());
    assertEquals(1, visitor.getRelationships().size());
    assertEquals(2, visitor.getOrderByColumns().size());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 14 with SchemaRepository

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

the class Resolve_AllColumn_Test method test_resolve_1.

public void test_resolve_1() 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 * from (select * from t_emp) x");
    repository.resolve(stmt, SchemaResolveVisitor.Option.ResolveAllColumn);
    assertEquals("SELECT emp_id, name\n" + "FROM (\n" + "\tSELECT emp_id, name\n" + "\tFROM t_emp\n" + ") x", stmt.toString());
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 15 with SchemaRepository

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

the class Resolve_AllColumn_Test method test_resolve_2.

public void test_resolve_2() 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 * from (select * from t_emp union all select * from t_emp) x");
    repository.resolve(stmt, SchemaResolveVisitor.Option.ResolveAllColumn);
    assertEquals("SELECT emp_id, name\n" + "FROM (\n" + "\tSELECT emp_id, name\n" + "\tFROM t_emp\n" + "\tUNION ALL\n" + "\tSELECT emp_id, name\n" + "\tFROM t_emp\n" + ") x", 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