Search in sources :

Example 11 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class MySqlCreateTableTest10 method test_0.

public void test_0() throws Exception {
    String sql = //
    "create table TACCOUNT (" + //
    "        ID varchar(36) not null," + //
    "        ACCOUNT varchar(100) not null," + //
    "        account_money double precision," + //
    "        NAME varchar(100) not null," + //
    "        TYPE integer," + //
    "        primary key (ID)" + "    )";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(5, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("TACCOUNT")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "ACCOUNT")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "account_money")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "NAME")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("TACCOUNT", "TYPE")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 12 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class MySqlSchemaStatVisitorTest_pk_0 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE `m_dt` (\n" + "  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',\n" + "  `gmt_create` datetime NOT NULL COMMENT '创建时间',\n" + "  `gmt_modified` datetime NOT NULL COMMENT '修改时间',\n" + "  `instance_id` int(11) NOT NULL COMMENT '实例id',\n" + "  `schema_name` varchar(128) NOT NULL COMMENT '数据库schema名称',\n" + "  `state` tinyint(4) NOT NULL DEFAULT '0' COMMENT ' x dd ',\n" + "  `busi_user` varchar(64) DEFAULT NULL COMMENT 'JDBC业务用户',\n" + "  `bzp` varchar(128) DEFAULT NULL COMMENT 'bcp',\n" + "  `ecd` varchar(32) DEFAULT NULL COMMENT 'xxx',\n" + "  `last_sync_time` datetime DEFAULT NULL COMMENT '最后同步时间',\n" + "  `catalog_name` varchar(128) NOT NULL COMMENT '物理库名称',\n" + "  `search_name` varchar(256) NOT NULL COMMENT '用于搜索,区分不同数据库的不同字段信息',\n" + "  `db_type` tinyint(4) NOT NULL COMMENT '数据库类型,和meta_instance表一致',\n" + "  `et` varchar(32) NOT NULL DEFAULT '' COMMENT 'et',\n" + "  `ae` varchar(32) DEFAULT NULL COMMENT 'ae',\n" + "  PRIMARY KEY (`id`),\n" + "  UNIQUE KEY `uk_instanceid_schemaname` (`instance_id`,`catalog_name`,`schema_name`),\n" + "  KEY `idx_schema_name` (`schema_name`),\n" + "  KEY `idx_instance_id_state` (`instance_id`,`id`,`state`),\n" + "  KEY `idx_search_name` (`search_name`(255))\n" + ") ENGINE=InnoDB AUTO_INCREMENT=408695 DEFAULT CHARSET=utf8 COMMENT='数据库表信息'";
    //		sql = "select columnName from table1 where id in (select id from table3 where name = ?)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println(sql);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("m_dt"));
    Assert.assertEquals(15, visitor.getColumns().size());
    {
        TableStat.Column column = visitor.getColumn("m_dt", "id");
        assertNotNull(column);
        assertTrue(column.isPrimaryKey());
    }
    {
        TableStat.Column column = visitor.getColumn("m_dt", "schema_name");
        assertNotNull(column);
        assertTrue(column.isUnique());
    }
    {
        TableStat.Column column = visitor.getColumn("m_dt", "instance_id");
        assertNotNull(column);
        assertTrue(column.isUnique());
    }
    for (SQLTableElement element : stmt.getTableElementList()) {
        if (element instanceof SQLUnique) {
            SQLName name = ((SQLUnique) element).getName();
            if (name != null) {
                String uniqueName = name.toString();
            }
        }
    }
// Assert.assertEquals(true, visitor.getFields().contains(new
// Column("users", "id")));
// Assert.assertEquals(true, visitor.getFields().contains(new
// Column("users", "name")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLUnique(com.alibaba.druid.sql.ast.statement.SQLUnique) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 13 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class SQLServerCreateTableTest_1 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE [projects] (" + //
    "[id] int NOT NULL IDENTITY(1, 1) PRIMARY KEY, " + //
    "[name] NVARCHAR(256), [description] NVARCHAR(2000), " + //
    "[enabled] bit DEFAULT 1 NOT NULL, " + //
    "[scope] NVARCHAR(3), " + //
    "[qualifier] NVARCHAR(3), " + //
    "[kee] NVARCHAR(400), " + //
    "[root_id] int, " + //
    "[profile_id] int, " + //
    "[language] NVARCHAR(5), " + //
    "[copy_resource_id] int, " + "[long_name] NVARCHAR(256)) ";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(12, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("projects")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("projects", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("projects", "name")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("projects", "enabled")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("projects", "scope")));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 14 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class SQLServerCreateTableTest_3 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE dbo.cwd_directory (" + //
    "ID NUMERIC NOT NULL, directory_name NVARCHAR(255), " + //
    "lower_directory_name NVARCHAR(255), created_date DATETIME, " + //
    "updated_date DATETIME, active int, description NVARCHAR(255), " + //
    "impl_class NVARCHAR(255), lower_impl_class NVARCHAR(255), " + //
    "directory_type NVARCHAR(60), directory_position NUMERIC, " + "CONSTRAINT PK_cwd_directory PRIMARY KEY (ID))";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    Assert.assertEquals(//
    "CREATE TABLE dbo.cwd_directory (" + //
    "\n\tID NUMERIC NOT NULL," + //
    "\n\tdirectory_name NVARCHAR(255)," + //
    "\n\tlower_directory_name NVARCHAR(255)," + //
    "\n\tcreated_date DATETIME," + //
    "\n\tupdated_date DATETIME," + //
    "\n\tactive int," + //
    "\n\tdescription NVARCHAR(255)," + //
    "\n\timpl_class NVARCHAR(255)," + //
    "\n\tlower_impl_class NVARCHAR(255)," + //
    "\n\tdirectory_type NVARCHAR(60)," + //
    "\n\tdirectory_position NUMERIC," + //
    "\n\tCONSTRAINT PK_cwd_directory PRIMARY KEY (ID)" + "\n)", SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER));
    Assert.assertEquals(//
    "create table dbo.cwd_directory (" + //
    "\n\tID NUMERIC not null," + //
    "\n\tdirectory_name NVARCHAR(255)," + //
    "\n\tlower_directory_name NVARCHAR(255)," + //
    "\n\tcreated_date DATETIME," + //
    "\n\tupdated_date DATETIME," + //
    "\n\tactive int," + //
    "\n\tdescription NVARCHAR(255)," + //
    "\n\timpl_class NVARCHAR(255)," + //
    "\n\tlower_impl_class NVARCHAR(255)," + //
    "\n\tdirectory_type NVARCHAR(60)," + //
    "\n\tdirectory_position NUMERIC," + //
    "\n\tconstraint PK_cwd_directory primary key (ID)" + "\n)", SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(12, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.cwd_directory")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.cwd_directory", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.cwd_directory", "directory_name")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.cwd_directory", "lower_directory_name")));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 15 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class SQLServerCreateTableTest_7 method test_0.

public void test_0() throws Exception {
    String sql = "create table leave_jpa (" + " id bigint identity not null, " + " user_id varchar(255), " + " primary key (id)" + ")";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals("CREATE TABLE leave_jpa (" + "\n\tid bigint DEFAULT NULL IDENTITY," + "\n\tuser_id varchar(255)," + "\n\tPRIMARY KEY (id)" + "\n)", output);
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("leave_jpa")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("leave_jpa", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("leave_jpa", "user_id")));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)16 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)11 Column (com.alibaba.druid.stat.TableStat.Column)10 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)8 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)8 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)4 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)3 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1 SQLAllExpr (com.alibaba.druid.sql.ast.expr.SQLAllExpr)1 SQLAnyExpr (com.alibaba.druid.sql.ast.expr.SQLAnyExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLSomeExpr (com.alibaba.druid.sql.ast.expr.SQLSomeExpr)1 SQLAlterTableAlterColumn (com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn)1