Search in sources :

Example 26 with SchemaRepository

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

the class CanalSQLSchemaTest 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");
    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 27 with SchemaRepository

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

the class CanalSQLSchemaTest method test_json_index.

@Test
public void test_json_index() throws Throwable {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = " CREATE TABLE `articles` ( \n" + " \t`article_id` bigint NOT NULL AUTO_INCREMENT, \n" + " \t`tags` json DEFAULT NULL, PRIMARY KEY (`article_id`), \n" + " \tKEY `articles_tags` (\n" + " \t\t(\n" + " \t\t\tcast(json_extract(`tags`,_utf8mb4'$[*]') \n" + " \t\t\t\tas char(40) array)\n" + " \t\t)\n" + " \t)\n" + " ) ENGINE=InnoDB AUTO_INCREMENT=1054 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
    repository.console(sql);
    repository.setDefaultSchema("test");
    SchemaObject table = repository.findTable("articles");
    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 28 with SchemaRepository

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

the class MySqlCreateTable_showColumns_test_3 method test_0.

public void test_0() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "create table yushitai_test.card_record ( id bigint auto_increment) auto_increment=256 " + "alter table yushitai_test.card_record add index index_name(name) ;" + "alter table yushitai_test.card_record add index index_name(name) ;" + "alter table yushitai_test.card_record add Constraint pk_id PRIMARY KEY (id);" + "alter table yushitai_test.card_record add Constraint pk_id PRIMARY KEY (id);";
    repository.console(sql);
    repository.setDefaultSchema("yushitai_test");
    SchemaObject table = repository.findTable("card_record");
    assertEquals("CREATE TABLE card_record (\n" + "\tid bigint AUTO_INCREMENT,\n" + "\tINDEX index_name(name),\n" + "\tPRIMARY KEY (id)\n" + ") AUTO_INCREMENT = 256", table.getStatement().toString());
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 29 with SchemaRepository

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

the class MySqlCreateTable_showColumns_test_2 method test_0.

public void test_0() throws Exception {
    SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
    String sql = "create table yushitai_test.card_record ( id bigint auto_increment) auto_increment=256 " + "; alter table yushitai_test.card_record add column customization_id bigint unsigned NOT NULL COMMENT 'TEST' ;" + "; alter table yushitai_test.card_record add column customization_id bigint unsigned NOT NULL COMMENT 'TEST' ;";
    repository.console(sql);
    repository.setDefaultSchema("yushitai_test");
    SchemaObject table = repository.findTable("card_record");
    assertEquals("CREATE TABLE card_record (\n" + "\tid bigint AUTO_INCREMENT,\n" + "\tcustomization_id bigint UNSIGNED NOT NULL COMMENT 'TEST'\n" + ") AUTO_INCREMENT = 256", table.getStatement().toString());
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Example 30 with SchemaRepository

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

the class MySqlCreateTableTest115 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE IF NOT EXISTS `schema`.`Employee` (\n" + "   id int(10),\n" + "   `Employee_id` VARCHAR(45) NOT NULL ,\n" + "   `Name` VARCHAR(255) NULL ,\n" + "   `idAddresses` VARCHAR(45) NULL ,\n" + "   PRIMARY KEY (`Employee_id`) ,\n" + "   INDEX `employee_id` USING BTREE (id(5)),\n" + "   CONSTRAINT `fkEmployee_Addresses`\n" + "   FOREIGN KEY `fkEmployee_Addresses` (`idAddresses`)\n" + "   REFERENCES `schema`.`Addresses` (`idAddresses`)\n" + "   ON DELETE NO ACTION\n" + "   ON UPDATE NO ACTION\n" + " ) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals(7, stmt.getTableElementList().size());
    assertEquals("CREATE TABLE IF NOT EXISTS `schema`.`Employee` (\n" + "\tid int(10),\n" + "\t`Employee_id` VARCHAR(45) NOT NULL,\n" + "\t`Name` VARCHAR(255) NULL,\n" + "\t`idAddresses` VARCHAR(45) NULL,\n" + "\tPRIMARY KEY (`Employee_id`),\n" + "\tINDEX `employee_id` USING BTREE(id(5)),\n" + "\tCONSTRAINT `fkEmployee_Addresses` FOREIGN KEY `fkEmployee_Addresses` (`idAddresses`) REFERENCES `schema`.`Addresses` (`idAddresses`) ON DELETE NO ACTION ON UPDATE NO ACTION\n" + ") ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin", stmt.toString());
    SchemaStatVisitor v = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
    stmt.accept(v);
    assertEquals(5, v.getColumns().size());
    SQLColumnDefinition column = stmt.findColumn("Employee_id");
    assertNotNull(column);
    assertEquals(1, column.getConstraints().size());
    assertTrue(column.isPrimaryKey());
    assertEquals(Types.VARCHAR, column.jdbcType());
    assertEquals("CREATE TABLE IF NOT EXISTS `schema`.`Employee` (\n" + "\tid int(10),\n" + "\t`Employee_id` VARCHAR(45) NOT NULL,\n" + "\t`Name` VARCHAR(255) NULL,\n" + "\t`idAddresses` VARCHAR(45) NULL,\n" + "\tPRIMARY KEY (`Employee_id`),\n" + "\tINDEX `employee_id` USING BTREE(id(5)),\n" + "\tFOREIGN KEY `fkEmployee_Addresses` (`idAddresses`) REFERENCES `schema`.`Addresses` (`idAddresses`) ON DELETE NO ACTION ON UPDATE NO ACTION\n" + ") ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin", stmt.clone().toString());
    SchemaRepository repository = new SchemaRepository(DbType.mysql);
    repository.acceptDDL(sql);
    assertEquals("CREATE TABLE IF NOT EXISTS `Employee` (\n" + "\tid int(10),\n" + "\t`Employee_id` VARCHAR(45) NOT NULL,\n" + "\t`Name` VARCHAR(255) NULL,\n" + "\t`idAddresses` VARCHAR(45) NULL,\n" + "\tPRIMARY KEY (`Employee_id`),\n" + "\tINDEX `employee_id` USING BTREE(id(5)),\n" + "\tFOREIGN KEY `fkEmployee_Addresses` (`idAddresses`) REFERENCES `schema`.`Addresses` (`idAddresses`) ON DELETE NO ACTION ON UPDATE NO ACTION\n" + ") ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin", repository.findTable("Employee").getStatement().toString());
}
Also used : MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) 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